Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ boolean isFastQuerySupported(JobId jobId) {
&& config.getTableDefinitions() == null
&& config.getTimePartitioning() == null
&& config.getUserDefinedFunctions() == null
&& config.getWriteDisposition() == null
&& config.getJobCreationMode() != JobCreationMode.JOB_CREATION_REQUIRED;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, wouldn't this just end up always doing a fast query (default is set to required)? IIUC, I think should be a fast query only for JobCreationMode.JOB_CREATION_OPTIONAL?

Is there any performance impact or behavioral change if default to a fast query even if a user explicitly sets job_required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the questions.

qq, wouldn't this just end up always doing a fast query (default is set to required)?

Yes, this is intended.

IIUC, I think should be a fast query only for JobCreationMode.JOB_CREATION_OPTIONAL?

Fast query should be for both. The BigQuery backend always creates a job in the background and returns a jobReference (including the jobId, see https://docs.cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#QueryResponse) for all jobs.query requests, so fast query makes sense.

Is there any performance impact or behavioral change if default to a fast query even if a user explicitly sets job_required?

The result is faster queries and lower latencies. There is no behavioral change as a job is still created in the background and tracked as expected.

@lqiu96 lqiu96 Jun 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I think faster queries is always better for the user, but something seems a bit weird about JobCreationMode to me. Why even have an JOB_CREATION_OPTIONAL configuration on the client side and not just do it under the hood on the server side?

Since we have this configuration, it seems odd to have a customer specify JOB_CREATION_REQUIRED and potentially not result in a job back. Could the original issue be solved if fast query runs only when JOB_CREATION_OPTIONAL is specified? It seems like the issue was the fast query logic was running on the wrong conditions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the questions.

Why even have an JOB_CREATION_OPTIONAL configuration on the client side and not just do it under the hood on the server side?

JOB_CREATION_OPTIONAL may run stateless queries and jobReference may be null, enabling stateless optimizations.

Since we have this configuration, it seems odd to have a customer specify JOB_CREATION_REQUIRED and potentially not result in a job back.

If a customer specifies JOB_CREATION_REQUIRED, a jobReference (with a jobId) is returned in the response. (https://docs.cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#QueryResponse)

Could the original issue be solved if fast query runs only when JOB_CREATION_OPTIONAL is specified? It seems like the issue was the fast query logic was running on the wrong conditions?

JOB_CREATION_REQUIRED is the default mode. If fast query only ran for OPTIONAL, all default queries would remain on the slow fallback path. The original issue (b/522363981) was a latency issue due to fast query logic not running when JOB_CREATION_REQUIRED was true.

@lqiu96 lqiu96 Jun 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JOB_CREATION_OPTIONAL may run stateless queries and jobReference may be null, enabling stateless optimizations.

Sorry I meant, I think it's weird for BigQuery service team to even expose a JobCreationMode proto if the intention was to do fast query by default. That's why it makes me think that we shouldn't do this by default and only do it if the mode is set to optional (but if other BQ clients are doing this, then happy to stand corrected).


From what I see in b/522363981, they fixed it by explicitly setting the JOB_CREATION_OPTIONAL. I think the only fix we need to set config.getJobCreationMode() == JobCreationMode.JOB_CREATION_OPTIONAL to ensure that the benchmark uses fast query.

&& config.getWriteDisposition() == null;
}

QueryRequest toPb() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -2347,6 +2348,56 @@ void testFastQueryRequestCompleted() throws InterruptedException, IOException {
.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture());
}

@Test
void testQueryRequestRequiredJobCreationCompleted() throws InterruptedException, IOException {
JobId queryJob = JobId.of(PROJECT, JOB);
com.google.api.services.bigquery.model.QueryResponse queryResponsePb =
new com.google.api.services.bigquery.model.QueryResponse()
.setCacheHit(false)
.setJobComplete(true)
.setKind("bigquery#queryResponse")
.setPageToken(null)
.setRows(ImmutableList.of(TABLE_ROW))
.setSchema(TABLE_SCHEMA.toPb())
.setTotalBytesProcessed(42L)
.setTotalRows(BigInteger.valueOf(1L))
.setJobReference(queryJob.toPb());

QueryJobConfiguration config =
QUERY_JOB_CONFIGURATION_FOR_QUERY.toBuilder()
.setJobCreationMode(QueryJobConfiguration.JobCreationMode.JOB_CREATION_REQUIRED)
.build();

when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()))
.thenReturn(queryResponsePb);

bigquery = options.getService();
TableResult result = bigquery.query(config);
assertNull(result.getNextPage());
assertNull(result.getNextPageToken());
assertFalse(result.hasNextPage());
assertThat(result.getSchema()).isEqualTo(TABLE_SCHEMA);
assertThat(result.getTotalRows()).isEqualTo(1);
assertThat(result.getJobId()).isEqualTo(queryJob);
for (FieldValueList row : result.getValues()) {
assertThat(row.get(0).getBooleanValue()).isFalse();
assertThat(row.get(1).getLongValue()).isEqualTo(1);
}

QueryRequest requestPb = requestPbCapture.getValue();
assertEquals(config.getQuery(), requestPb.getQuery());
assertEquals(
config.getDefaultDataset().getDataset(), requestPb.getDefaultDataset().getDatasetId());
assertEquals(config.useQueryCache(), requestPb.getUseQueryCache());
assertNull(requestPb.getLocation());

verify(bigqueryRpcMock)
.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture());
verify(bigqueryRpcMock, never())
.createSkipExceptionTranslation(
any(com.google.api.services.bigquery.model.Job.class), any());
}

@Test
void testFastQueryRequestCompletedWithLocation() throws InterruptedException, IOException {
com.google.api.services.bigquery.model.QueryResponse queryResponsePb =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,24 @@ public class QueryRequestInfoTest {
QueryRequestInfo REQUEST_INFO_SUPPORTED =
new QueryRequestInfo(
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build());
private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION_REQUIRED_SUPPORTED =
QUERY_JOB_CONFIGURATION_SUPPORTED.toBuilder()
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
.build();
QueryRequestInfo REQUEST_INFO_REQUIRED_SUPPORTED =
new QueryRequestInfo(
QUERY_JOB_CONFIGURATION_REQUIRED_SUPPORTED, DataFormatOptions.newBuilder().build());

@Test
public void testIsFastQuerySupported() {
JobId jobIdSupported = JobId.newBuilder().build();
JobId jobIdNotSupported = JobId.newBuilder().setJob("random-job-id").build();
assertEquals(false, REQUEST_INFO.isFastQuerySupported(jobIdSupported));
assertEquals(true, REQUEST_INFO_SUPPORTED.isFastQuerySupported(jobIdSupported));
assertEquals(true, REQUEST_INFO_REQUIRED_SUPPORTED.isFastQuerySupported(jobIdSupported));
assertEquals(false, REQUEST_INFO.isFastQuerySupported(jobIdNotSupported));
assertEquals(false, REQUEST_INFO_SUPPORTED.isFastQuerySupported(jobIdNotSupported));
assertEquals(false, REQUEST_INFO_REQUIRED_SUPPORTED.isFastQuerySupported(jobIdNotSupported));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7341,10 +7341,9 @@ void testStatelessQueries() throws InterruptedException {
(tableResult.getJobId() != null) ^ (tableResult.getQueryId() != null),
"Exactly one of jobId or queryId should be non-null");

// Job creation takes over, no query id is created.
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED);
tableResult = executeSimpleQuery(bigQuery);
assertNull(tableResult.getQueryId());
assertNotNull(tableResult.getQueryId());
assertNotNull(tableResult.getJobId());

bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED);
Expand Down Expand Up @@ -7408,9 +7407,8 @@ void testTableResultJobIdAndQueryId() throws InterruptedException {
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
.build();
result = bigQuery.query(configWithJob);
result = job.getQueryResults();
assertNotNull(result.getJobId());
assertNull(result.getQueryId());
assertNotNull(result.getQueryId());
}

@Test
Expand Down Expand Up @@ -7505,14 +7503,14 @@ void testQueryWithTimeout() throws InterruptedException {
// Allow 2 seconds of timeout value to account for random delays
assertTrue(millis < 1_000_000 * 2);

// Stateful query returns Job
// Test scenario 3 to ensure job is created if JobCreationMode is set.
// Test scenario 3 to ensure TableResult is returned with JobId if JobCreationMode is REQUIRED
config =
QueryJobConfiguration.newBuilder(query)
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
.build();
result = bigQuery.queryWithTimeout(config, null, null);
assertTrue(result instanceof Job);
assertTrue(result instanceof TableResult);
assertNotNull(((TableResult) result).getJobId());

// Stateful query returns Job
// Test scenario 4 to ensure job is created if Query is long running.
Expand Down
Loading