Skip to content

Commit 19fe6eb

Browse files
MMMarcyJesseLovelace
authored andcommitted
Now getTable of BigQueryImpl checks if the provided TableId has the project set. If yes, the set projectId is used, otherwise the projectId used when creating the BigQuery client will be used. The additional test checks this behavior by calling getTableId with an empty projectId.
1 parent 5e0bca4 commit 19fe6eb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.google.cloud.bigquery.spi.v2.BigQueryRpc;
4040
import com.google.common.annotations.VisibleForTesting;
4141
import com.google.common.base.Function;
42+
import com.google.common.base.Strings;
4243
import com.google.common.base.Supplier;
4344
import com.google.common.collect.ImmutableList;
4445
import com.google.common.collect.Iterables;
@@ -401,7 +402,12 @@ public Table getTable(final String datasetId, final String tableId, TableOption.
401402

402403
@Override
403404
public Table getTable(TableId tableId, TableOption... options) {
404-
final TableId completeTableId = tableId.setProjectId(getOptions().getProjectId());
405+
// More context about why this: https://github.com/googleapis/google-cloud-java/issues/3808
406+
final TableId completeTableId = tableId.setProjectId(
407+
Strings.isNullOrEmpty(tableId.getProject())
408+
? getOptions().getProjectId()
409+
: tableId.getProject()
410+
);
405411
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
406412
try {
407413
com.google.api.services.bigquery.model.Table answer =

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,20 @@ public void testGetTableFromTableIdWithProject() {
603603
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
604604
}
605605

606+
@Test
607+
public void testGetTableFromTableIdWithoutProject() {
608+
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
609+
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
610+
EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
611+
.andReturn(tableInfo.toPb());
612+
EasyMock.replay(bigqueryRpcMock);
613+
BigQueryOptions bigQueryOptions =
614+
createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
615+
bigquery = bigQueryOptions.getService();
616+
Table table = bigquery.getTable(tableId);
617+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
618+
}
619+
606620
@Test
607621
public void testGetTableWithSelectedFields() {
608622
Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance();

0 commit comments

Comments
 (0)