If the location of the job is not "US" or "EU", the {@code jobId} must specify the job + * location. + * + *
This method cannot be used in conjunction with {@link QueryJobConfiguration#dryRun()} + * queries. Since dry-run queries are not actually executed, there's no way to retrieve results. + * + *
See {@link #query(QueryJobConfiguration, JobOption...)} for examples on populating a {@link
+ * QueryJobConfiguration}.
+ *
+ * @throws BigQueryException upon failure
+ * @throws InterruptedException if the current thread gets interrupted while waiting for the query
+ * to complete
+ * @throws JobException if the job completes unsuccessfully
+ */
+ Object queryWithTimeout(
+ QueryJobConfiguration configuration, JobId jobId, Long timeoutMs, JobOption... options)
+ throws InterruptedException, JobException;
+
/**
* Returns results of the query associated with the provided job.
*
diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java
index 088d15c09..ac8fce708 100644
--- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java
+++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java
@@ -1925,48 +1925,10 @@ public Boolean call() throws IOException {
@Override
public TableResult query(QueryJobConfiguration configuration, JobOption... options)
throws InterruptedException, JobException {
- Job.checkNotDryRun(configuration, "query");
-
- configuration =
- configuration.toBuilder()
- .setJobCreationMode(getOptions().getDefaultJobCreationMode())
- .build();
-
- Span querySpan = null;
- if (getOptions().isOpenTelemetryTracingEnabled()
- && getOptions().getOpenTelemetryTracer() != null) {
- querySpan =
- getOptions()
- .getOpenTelemetryTracer()
- .spanBuilder("com.google.cloud.bigquery.BigQuery.query")
- .setAllAttributes(otelAttributesFromOptions(options))
- .startSpan();
- }
- try (Scope queryScope = querySpan != null ? querySpan.makeCurrent() : null) {
- // If all parameters passed in configuration are supported by the query() method on the
- // backend,
- // put on fast path
- QueryRequestInfo requestInfo =
- new QueryRequestInfo(configuration, getOptions().getUseInt64Timestamps());
- if (requestInfo.isFastQuerySupported(null)) {
- String projectId = getOptions().getProjectId();
- QueryRequest content = requestInfo.toPb();
- if (getOptions().getLocation() != null) {
- content.setLocation(getOptions().getLocation());
- }
- return queryRpc(projectId, content, options);
- }
- // Otherwise, fall back to the existing create query job logic
- return create(JobInfo.of(configuration), options).getQueryResults();
- } finally {
- if (querySpan != null) {
- querySpan.end();
- }
- }
+ return query(configuration, null, options);
}
- private TableResult queryRpc(
- final String projectId, final QueryRequest content, JobOption... options)
+ private Object queryRpc(final String projectId, final QueryRequest content, JobOption... options)
throws InterruptedException {
com.google.api.services.bigquery.model.QueryResponse results;
Span queryRpc = null;
@@ -2030,7 +1992,7 @@ public com.google.api.services.bigquery.model.QueryResponse call()
// here, but this is left as future work.
JobId jobId = JobId.fromPb(results.getJobReference());
Job job = getJob(jobId, options);
- return job.getQueryResults();
+ return job;
}
if (results.getPageToken() != null) {
@@ -2070,16 +2032,35 @@ public com.google.api.services.bigquery.model.QueryResponse call()
@Override
public TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption... options)
throws InterruptedException, JobException {
+ Object result = queryWithTimeout(configuration, jobId, null, options);
+ if (result instanceof Job) {
+ return ((Job) result).getQueryResults();
+ }
+ return (TableResult) result;
+ }
+
+ @Override
+ public Object queryWithTimeout(
+ QueryJobConfiguration configuration, JobId jobId, Long timeoutMs, JobOption... options)
+ throws InterruptedException, JobException {
Job.checkNotDryRun(configuration, "query");
+ // If JobCreationMode is not explicitly set, update it with default value;
+ if (configuration.getJobCreationMode() == null) {
+ configuration =
+ configuration.toBuilder()
+ .setJobCreationMode(getOptions().getDefaultJobCreationMode())
+ .build();
+ }
+
Span querySpan = null;
if (getOptions().isOpenTelemetryTracingEnabled()
&& getOptions().getOpenTelemetryTracer() != null) {
querySpan =
getOptions()
.getOpenTelemetryTracer()
- .spanBuilder("com.google.cloud.bigquery.BigQuery.query")
- .setAllAttributes(jobId.getOtelAttributes())
+ .spanBuilder("com.google.cloud.bigquery.BigQuery.queryWithTimeout")
+ .setAllAttributes(jobId != null ? jobId.getOtelAttributes() : null)
.setAllAttributes(otelAttributesFromOptions(options))
.startSpan();
}
@@ -2095,18 +2076,23 @@ && getOptions().getOpenTelemetryTracer() != null) {
// fail with "Access denied" if the project do not have enough permissions to run the job.
String projectId =
- jobId.getProject() != null ? jobId.getProject() : getOptions().getProjectId();
+ jobId != null && jobId.getProject() != null
+ ? jobId.getProject()
+ : getOptions().getProjectId();
QueryRequest content = requestInfo.toPb();
// Be careful when setting the location, if a location is specified in the BigQueryOption or
// JobId the job created by the query method will be in that location, even if the table to
// be
// queried is in a different location. This may cause the query to fail with
// "BigQueryException: Not found"
- if (jobId.getLocation() != null) {
+ if (jobId != null && jobId.getLocation() != null) {
content.setLocation(jobId.getLocation());
} else if (getOptions().getLocation() != null) {
content.setLocation(getOptions().getLocation());
}
+ if (timeoutMs != null) {
+ content.setTimeoutMs(timeoutMs);
+ }
return queryRpc(projectId, content, options);
}
diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java
index c0367beae..393455e36 100644
--- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java
+++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java
@@ -2610,6 +2610,29 @@ PROJECT, JOB, null, optionMap(Job.DEFAULT_QUERY_WAIT_OPTIONS)))
PROJECT, DATASET, TABLE, Collections.