Skip to content

Commit 270f866

Browse files
feat: Use location in BigQueryOption as the default for query (#3047)
* feat: Use location in BigQueryOption as the default for query * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat: Use location in BigQueryOption as the default for query * feat: Use location in BigQueryOption as the default for query * feat: Use location in BigQueryOption as the default for query --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a710632 commit 270f866

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,9 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
13391339
if (requestInfo.isFastQuerySupported(null)) {
13401340
String projectId = getOptions().getProjectId();
13411341
QueryRequest content = requestInfo.toPb();
1342+
if (getOptions().getLocation() != null) {
1343+
content.setLocation(getOptions().getLocation());
1344+
}
13421345
return queryRpc(projectId, content, options);
13431346
}
13441347
// Otherwise, fall back to the existing create query job logic
@@ -1437,12 +1440,14 @@ public TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOp
14371440
String projectId =
14381441
jobId.getProject() != null ? jobId.getProject() : getOptions().getProjectId();
14391442
QueryRequest content = requestInfo.toPb();
1440-
// Be careful when setting the location in JobId, if a location is specified in the JobId,
1441-
// the job created by the query method will be in that location, even if the table to be
1443+
// Be careful when setting the location, if a location is specified in the BigQueryOption or
1444+
// JobId the job created by the query method will be in that location, even if the table to be
14421445
// queried is in a different location. This may cause the query to fail with
14431446
// "BigQueryException: Not found"
14441447
if (jobId.getLocation() != null) {
14451448
content.setLocation(jobId.getLocation());
1449+
} else if (getOptions().getLocation() != null) {
1450+
content.setLocation(getOptions().getLocation());
14461451
}
14471452

14481453
return queryRpc(projectId, content, options);

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

+41
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,47 @@ public void testFastQueryRequestCompleted() throws InterruptedException {
19941994
QUERY_JOB_CONFIGURATION_FOR_QUERY.getDefaultDataset().getDataset(),
19951995
requestPb.getDefaultDataset().getDatasetId());
19961996
assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache());
1997+
assertNull(requestPb.getLocation());
1998+
1999+
verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture());
2000+
}
2001+
2002+
@Test
2003+
public void testFastQueryRequestCompletedWithLocation() throws InterruptedException {
2004+
com.google.api.services.bigquery.model.QueryResponse queryResponsePb =
2005+
new com.google.api.services.bigquery.model.QueryResponse()
2006+
.setCacheHit(false)
2007+
.setJobComplete(true)
2008+
.setKind("bigquery#queryResponse")
2009+
.setPageToken(null)
2010+
.setRows(ImmutableList.of(TABLE_ROW))
2011+
.setSchema(TABLE_SCHEMA.toPb())
2012+
.setTotalBytesProcessed(42L)
2013+
.setTotalRows(BigInteger.valueOf(1L));
2014+
2015+
when(bigqueryRpcMock.queryRpc(eq(PROJECT), requestPbCapture.capture()))
2016+
.thenReturn(queryResponsePb);
2017+
2018+
BigQueryOptions options = createBigQueryOptionsForProjectWithLocation(PROJECT, rpcFactoryMock);
2019+
bigquery = options.getService();
2020+
TableResult result = bigquery.query(QUERY_JOB_CONFIGURATION_FOR_QUERY);
2021+
assertNull(result.getNextPage());
2022+
assertNull(result.getNextPageToken());
2023+
assertFalse(result.hasNextPage());
2024+
assertThat(result.getSchema()).isEqualTo(TABLE_SCHEMA);
2025+
assertThat(result.getTotalRows()).isEqualTo(1);
2026+
for (FieldValueList row : result.getValues()) {
2027+
assertThat(row.get(0).getBooleanValue()).isFalse();
2028+
assertThat(row.get(1).getLongValue()).isEqualTo(1);
2029+
}
2030+
2031+
QueryRequest requestPb = requestPbCapture.getValue();
2032+
assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.getQuery(), requestPb.getQuery());
2033+
assertEquals(
2034+
QUERY_JOB_CONFIGURATION_FOR_QUERY.getDefaultDataset().getDataset(),
2035+
requestPb.getDefaultDataset().getDatasetId());
2036+
assertEquals(QUERY_JOB_CONFIGURATION_FOR_QUERY.useQueryCache(), requestPb.getUseQueryCache());
2037+
assertEquals(LOCATION, requestPb.getLocation());
19972038

19982039
verify(bigqueryRpcMock).queryRpc(eq(PROJECT), requestPbCapture.capture());
19992040
}

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

+63-10
Original file line numberDiff line numberDiff line change
@@ -6227,27 +6227,80 @@ public void testAlreadyExistJobExceptionHandling() throws InterruptedException {
62276227

62286228
@Test
62296229
public void testStatelessQueries() throws InterruptedException {
6230+
// Create local BigQuery to not contaminate global test parameters.
6231+
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
6232+
BigQuery bigQuery = bigqueryHelper.getOptions().getService();
6233+
62306234
// simulate setting the QUERY_PREVIEW_ENABLED environment variable
6231-
bigquery.getOptions().setQueryPreviewEnabled("TRUE");
6232-
assertNull(executeSimpleQuery().getJobId());
6235+
bigQuery.getOptions().setQueryPreviewEnabled("TRUE");
6236+
assertNull(executeSimpleQuery(bigQuery).getJobId());
62336237

62346238
// the flag should be case-insensitive
6235-
bigquery.getOptions().setQueryPreviewEnabled("tRuE");
6236-
assertNull(executeSimpleQuery().getJobId());
6239+
bigQuery.getOptions().setQueryPreviewEnabled("tRuE");
6240+
assertNull(executeSimpleQuery(bigQuery).getJobId());
62376241

62386242
// any other values won't enable optional job creation mode
6239-
bigquery.getOptions().setQueryPreviewEnabled("test_value");
6240-
assertNotNull(executeSimpleQuery().getJobId());
6243+
bigQuery.getOptions().setQueryPreviewEnabled("test_value");
6244+
assertNotNull(executeSimpleQuery(bigQuery).getJobId());
62416245

62426246
// reset the flag
6243-
bigquery.getOptions().setQueryPreviewEnabled(null);
6244-
assertNotNull(executeSimpleQuery().getJobId());
6247+
bigQuery.getOptions().setQueryPreviewEnabled(null);
6248+
assertNotNull(executeSimpleQuery(bigQuery).getJobId());
62456249
}
62466250

6247-
private TableResult executeSimpleQuery() throws InterruptedException {
6251+
private TableResult executeSimpleQuery(BigQuery bigQuery) throws InterruptedException {
62486252
String query = "SELECT 1 as one";
62496253
QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query).build();
6250-
TableResult result = bigquery.query(config);
6254+
TableResult result = bigQuery.query(config);
62516255
return result;
62526256
}
6257+
6258+
@Test
6259+
public void testStatelessQueriesWithLocation() throws Exception {
6260+
// This test validates BigQueryOption location is used for stateless query by verifying that the
6261+
// stateless query fails when the BigQueryOption location does not match the dataset location.
6262+
String location = "EU";
6263+
String wrongLocation = "US";
6264+
6265+
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
6266+
BigQuery bigQuery =
6267+
bigqueryHelper.getOptions().toBuilder().setLocation(location).build().getService();
6268+
6269+
Dataset dataset =
6270+
bigQuery.create(
6271+
DatasetInfo.newBuilder("locationset_" + UUID.randomUUID().toString().replace("-", "_"))
6272+
.setLocation(location)
6273+
.build());
6274+
try {
6275+
TableId tableId = TableId.of(dataset.getDatasetId().getDataset(), "sometable");
6276+
Schema schema = Schema.of(Field.of("name", LegacySQLTypeName.STRING));
6277+
TableDefinition tableDef = StandardTableDefinition.of(schema);
6278+
Table table = bigQuery.create(TableInfo.newBuilder(tableId, tableDef).build());
6279+
6280+
String query =
6281+
String.format(
6282+
"SELECT * FROM `%s.%s.%s`",
6283+
table.getTableId().getProject(),
6284+
table.getTableId().getDataset(),
6285+
table.getTableId().getTable());
6286+
6287+
// Test stateless query when BigQueryOption location matches dataset location.
6288+
bigQuery.getOptions().setQueryPreviewEnabled("TRUE");
6289+
TableResult tb = bigQuery.query(QueryJobConfiguration.of(query));
6290+
assertNull(tb.getJobId());
6291+
6292+
// Test stateless query when BigQueryOption location does not match dataset location.
6293+
try {
6294+
BigQuery bigQueryWrongLocation =
6295+
bigqueryHelper.getOptions().toBuilder().setLocation(wrongLocation).build().getService();
6296+
bigQueryWrongLocation.getOptions().setQueryPreviewEnabled("TRUE");
6297+
bigQueryWrongLocation.query(QueryJobConfiguration.of(query));
6298+
fail("querying a table with wrong location shouldn't work");
6299+
} catch (BigQueryException e) {
6300+
// Nothing to do
6301+
}
6302+
} finally {
6303+
bigQuery.delete(dataset.getDatasetId(), DatasetDeleteOption.deleteContents());
6304+
}
6305+
}
62536306
}

0 commit comments

Comments
 (0)