|
25 | 25 | import com.google.api.gax.paging.Page; |
26 | 26 | import com.google.api.services.bigquery.model.ErrorProto; |
27 | 27 | import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
| 28 | +import com.google.api.services.bigquery.model.ProjectList; |
28 | 29 | import com.google.api.services.bigquery.model.QueryRequest; |
29 | 30 | import com.google.api.services.bigquery.model.TableDataInsertAllRequest; |
30 | 31 | import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows; |
|
65 | 66 |
|
66 | 67 | final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery { |
67 | 68 |
|
| 69 | + private static class ProjectPageFetcher implements NextPageFetcher<Project> { |
| 70 | + |
| 71 | + private static final long serialVersionUID = 1L; |
| 72 | + private final Map<BigQueryRpc.Option, ?> requestOptions; |
| 73 | + private final BigQueryOptions serviceOptions; |
| 74 | + |
| 75 | + ProjectPageFetcher( |
| 76 | + BigQueryOptions serviceOptions, String cursor, Map<BigQueryRpc.Option, ?> optionMap) { |
| 77 | + this.requestOptions = |
| 78 | + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); |
| 79 | + this.serviceOptions = serviceOptions; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Page<Project> getNextPage() { |
| 84 | + return listProjects(serviceOptions, requestOptions); |
| 85 | + } |
| 86 | + } |
| 87 | + |
68 | 88 | private static class DatasetPageFetcher implements NextPageFetcher<Dataset> { |
69 | 89 |
|
70 | 90 | private static final long serialVersionUID = -3057564042439021278L; |
@@ -307,6 +327,72 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException |
307 | 327 | } |
308 | 328 | } |
309 | 329 |
|
| 330 | + @Override |
| 331 | + @BetaApi |
| 332 | + public Page<Project> listProjects(ProjectListOption... options) { |
| 333 | + Span projectsList = null; |
| 334 | + if (getOptions().isOpenTelemetryTracingEnabled() |
| 335 | + && getOptions().getOpenTelemetryTracer() != null) { |
| 336 | + projectsList = |
| 337 | + getOptions() |
| 338 | + .getOpenTelemetryTracer() |
| 339 | + .spanBuilder("com.google.cloud.bigquery.BigQuery.listProjects") |
| 340 | + .setAllAttributes(otelAttributesFromOptions(options)) |
| 341 | + .startSpan(); |
| 342 | + } |
| 343 | + try (Scope projectsListScope = projectsList != null ? projectsList.makeCurrent() : null) { |
| 344 | + return listProjects(getOptions(), optionMap(options)); |
| 345 | + } finally { |
| 346 | + if (projectsList != null) { |
| 347 | + projectsList.end(); |
| 348 | + } |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + private static Page<Project> listProjects( |
| 353 | + final BigQueryOptions serviceOptions, final Map<BigQueryRpc.Option, ?> optionsMap) { |
| 354 | + try { |
| 355 | + Tuple<String, Iterable<ProjectList.Projects>> result = |
| 356 | + BigQueryRetryHelper.runWithRetries( |
| 357 | + new Callable<Tuple<String, Iterable<ProjectList.Projects>>>() { |
| 358 | + @Override |
| 359 | + public Tuple<String, Iterable<ProjectList.Projects>> call() { |
| 360 | + return serviceOptions.getBigQueryRpcV2().listProjects(optionsMap); |
| 361 | + } |
| 362 | + }, |
| 363 | + serviceOptions.getRetrySettings(), |
| 364 | + serviceOptions.getResultRetryAlgorithm(), |
| 365 | + serviceOptions.getClock(), |
| 366 | + EMPTY_RETRY_CONFIG, |
| 367 | + serviceOptions.isOpenTelemetryTracingEnabled(), |
| 368 | + serviceOptions.getOpenTelemetryTracer()); |
| 369 | + String nextPageToken = result.x(); |
| 370 | + Iterable<Project> projects = |
| 371 | + Iterables.transform( |
| 372 | + result.y() != null ? result.y() : ImmutableList.<ProjectList.Projects>of(), |
| 373 | + new Function<ProjectList.Projects, Project>() { |
| 374 | + @Override |
| 375 | + public Project apply(ProjectList.Projects projectPb) { |
| 376 | + return new Project( |
| 377 | + projectPb.getId(), |
| 378 | + projectPb.getNumericId() != null |
| 379 | + ? String.valueOf(projectPb.getNumericId()) |
| 380 | + : null, |
| 381 | + projectPb.getProjectReference() != null |
| 382 | + ? projectPb.getProjectReference().getProjectId() |
| 383 | + : null, |
| 384 | + projectPb.getFriendlyName()); |
| 385 | + } |
| 386 | + }); |
| 387 | + return new PageImpl<>( |
| 388 | + new ProjectPageFetcher(serviceOptions, nextPageToken, optionsMap), |
| 389 | + nextPageToken, |
| 390 | + projects); |
| 391 | + } catch (BigQueryRetryHelperException e) { |
| 392 | + throw BigQueryException.translateAndThrow(e); |
| 393 | + } |
| 394 | + } |
| 395 | + |
310 | 396 | @Override |
311 | 397 | public Table create(TableInfo tableInfo, TableOption... options) { |
312 | 398 | final com.google.api.services.bigquery.model.Table tablePb = |
@@ -2083,11 +2169,15 @@ && getOptions().getOpenTelemetryTracer() != null) { |
2083 | 2169 | .startSpan(); |
2084 | 2170 | } |
2085 | 2171 | try (Scope queryScope = querySpan != null ? querySpan.makeCurrent() : null) { |
2086 | | - // If all parameters passed in configuration are supported by the query() method on the |
2087 | | - // backend, put on fast path |
| 2172 | + // The fast query path (jobs.query API) is preferred to reduce latency by avoiding |
| 2173 | + // the slow fallback path (jobs.insert API). We will opt to use it if the configuration |
| 2174 | + // and JobId allow (i.e. if all parameters passed in configuration are supported). |
2088 | 2175 | QueryRequestInfo requestInfo = |
2089 | 2176 | new QueryRequestInfo(configuration, getOptions().getDataFormatOptions()); |
2090 | | - if (requestInfo.isFastQuerySupported(jobId)) { |
| 2177 | + // Fast query path is not possible if job is specified in the JobID object. |
| 2178 | + // Respect Job field value in JobId specified by user. |
| 2179 | + // Specifying it will force the query to take the slower path. |
| 2180 | + if (requestInfo.isFastQuerySupported() && (jobId == null || jobId.getJob() == null)) { |
2091 | 2181 | // Be careful when setting the projectID in JobId, if a projectID is specified in the JobId, |
2092 | 2182 | // the job created by the query method will use that project. This may cause the query to |
2093 | 2183 | // fail with "Access denied" if the project do not have enough permissions to run the job. |
|
0 commit comments