Skip to content

Commit e1c594b

Browse files
committed
fix(bigquery): route JOB_CREATION_REQUIRED through fast query path
1 parent 134b78e commit e1c594b

4 files changed

Lines changed: 66 additions & 9 deletions

File tree

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryRequestInfo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ boolean isFastQuerySupported(JobId jobId) {
8787
&& config.getTableDefinitions() == null
8888
&& config.getTimePartitioning() == null
8989
&& config.getUserDefinedFunctions() == null
90-
&& config.getWriteDisposition() == null
91-
&& config.getJobCreationMode() != JobCreationMode.JOB_CREATION_REQUIRED;
90+
&& config.getWriteDisposition() == null;
9291
}
9392

9493
QueryRequest toPb() {

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static org.mockito.ArgumentMatchers.nullable;
3333
import static org.mockito.Mockito.doReturn;
3434
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.never;
3536
import static org.mockito.Mockito.times;
3637
import static org.mockito.Mockito.verify;
3738
import static org.mockito.Mockito.when;
@@ -2347,6 +2348,56 @@ void testFastQueryRequestCompleted() throws InterruptedException, IOException {
23472348
.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture());
23482349
}
23492350

2351+
@Test
2352+
void testQueryRequestRequiredJobCreationCompleted() throws InterruptedException, IOException {
2353+
JobId queryJob = JobId.of(PROJECT, JOB);
2354+
com.google.api.services.bigquery.model.QueryResponse queryResponsePb =
2355+
new com.google.api.services.bigquery.model.QueryResponse()
2356+
.setCacheHit(false)
2357+
.setJobComplete(true)
2358+
.setKind("bigquery#queryResponse")
2359+
.setPageToken(null)
2360+
.setRows(ImmutableList.of(TABLE_ROW))
2361+
.setSchema(TABLE_SCHEMA.toPb())
2362+
.setTotalBytesProcessed(42L)
2363+
.setTotalRows(BigInteger.valueOf(1L))
2364+
.setJobReference(queryJob.toPb());
2365+
2366+
QueryJobConfiguration config =
2367+
QUERY_JOB_CONFIGURATION_FOR_QUERY.toBuilder()
2368+
.setJobCreationMode(QueryJobConfiguration.JobCreationMode.JOB_CREATION_REQUIRED)
2369+
.build();
2370+
2371+
when(bigqueryRpcMock.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture()))
2372+
.thenReturn(queryResponsePb);
2373+
2374+
bigquery = options.getService();
2375+
TableResult result = bigquery.query(config);
2376+
assertNull(result.getNextPage());
2377+
assertNull(result.getNextPageToken());
2378+
assertFalse(result.hasNextPage());
2379+
assertThat(result.getSchema()).isEqualTo(TABLE_SCHEMA);
2380+
assertThat(result.getTotalRows()).isEqualTo(1);
2381+
assertThat(result.getJobId()).isEqualTo(queryJob);
2382+
for (FieldValueList row : result.getValues()) {
2383+
assertThat(row.get(0).getBooleanValue()).isFalse();
2384+
assertThat(row.get(1).getLongValue()).isEqualTo(1);
2385+
}
2386+
2387+
QueryRequest requestPb = requestPbCapture.getValue();
2388+
assertEquals(config.getQuery(), requestPb.getQuery());
2389+
assertEquals(
2390+
config.getDefaultDataset().getDataset(), requestPb.getDefaultDataset().getDatasetId());
2391+
assertEquals(config.useQueryCache(), requestPb.getUseQueryCache());
2392+
assertNull(requestPb.getLocation());
2393+
2394+
verify(bigqueryRpcMock)
2395+
.queryRpcSkipExceptionTranslation(eq(PROJECT), requestPbCapture.capture());
2396+
verify(bigqueryRpcMock, never())
2397+
.createSkipExceptionTranslation(
2398+
any(com.google.api.services.bigquery.model.Job.class), any());
2399+
}
2400+
23502401
@Test
23512402
void testFastQueryRequestCompletedWithLocation() throws InterruptedException, IOException {
23522403
com.google.api.services.bigquery.model.QueryResponse queryResponsePb =

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryRequestInfoTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,24 @@ public class QueryRequestInfoTest {
159159
QueryRequestInfo REQUEST_INFO_SUPPORTED =
160160
new QueryRequestInfo(
161161
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build());
162+
private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION_REQUIRED_SUPPORTED =
163+
QUERY_JOB_CONFIGURATION_SUPPORTED.toBuilder()
164+
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
165+
.build();
166+
QueryRequestInfo REQUEST_INFO_REQUIRED_SUPPORTED =
167+
new QueryRequestInfo(
168+
QUERY_JOB_CONFIGURATION_REQUIRED_SUPPORTED, DataFormatOptions.newBuilder().build());
162169

163170
@Test
164171
public void testIsFastQuerySupported() {
165172
JobId jobIdSupported = JobId.newBuilder().build();
166173
JobId jobIdNotSupported = JobId.newBuilder().setJob("random-job-id").build();
167174
assertEquals(false, REQUEST_INFO.isFastQuerySupported(jobIdSupported));
168175
assertEquals(true, REQUEST_INFO_SUPPORTED.isFastQuerySupported(jobIdSupported));
176+
assertEquals(true, REQUEST_INFO_REQUIRED_SUPPORTED.isFastQuerySupported(jobIdSupported));
169177
assertEquals(false, REQUEST_INFO.isFastQuerySupported(jobIdNotSupported));
170178
assertEquals(false, REQUEST_INFO_SUPPORTED.isFastQuerySupported(jobIdNotSupported));
179+
assertEquals(false, REQUEST_INFO_REQUIRED_SUPPORTED.isFastQuerySupported(jobIdNotSupported));
171180
}
172181

173182
@Test

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7335,10 +7335,9 @@ void testStatelessQueries() throws InterruptedException {
73357335
(tableResult.getJobId() != null) ^ (tableResult.getQueryId() != null),
73367336
"Exactly one of jobId or queryId should be non-null");
73377337

7338-
// Job creation takes over, no query id is created.
73397338
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED);
73407339
tableResult = executeSimpleQuery(bigQuery);
7341-
assertNull(tableResult.getQueryId());
7340+
assertNotNull(tableResult.getQueryId());
73427341
assertNotNull(tableResult.getJobId());
73437342

73447343
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED);
@@ -7402,9 +7401,8 @@ void testTableResultJobIdAndQueryId() throws InterruptedException {
74027401
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
74037402
.build();
74047403
result = bigQuery.query(configWithJob);
7405-
result = job.getQueryResults();
74067404
assertNotNull(result.getJobId());
7407-
assertNull(result.getQueryId());
7405+
assertNotNull(result.getQueryId());
74087406
}
74097407

74107408
@Test
@@ -7499,14 +7497,14 @@ void testQueryWithTimeout() throws InterruptedException {
74997497
// Allow 2 seconds of timeout value to account for random delays
75007498
assertTrue(millis < 1_000_000 * 2);
75017499

7502-
// Stateful query returns Job
7503-
// Test scenario 3 to ensure job is created if JobCreationMode is set.
7500+
// Test scenario 3 to ensure TableResult is returned with JobId if JobCreationMode is REQUIRED
75047501
config =
75057502
QueryJobConfiguration.newBuilder(query)
75067503
.setJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED)
75077504
.build();
75087505
result = bigQuery.queryWithTimeout(config, null, null);
7509-
assertTrue(result instanceof Job);
7506+
assertTrue(result instanceof TableResult);
7507+
assertNotNull(((TableResult) result).getJobId());
75107508

75117509
// Stateful query returns Job
75127510
// Test scenario 4 to ensure job is created if Query is long running.

0 commit comments

Comments
 (0)