diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index c4d6f0f7ae88..3cfaa9629609 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -39,6 +39,7 @@ import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; +import com.google.common.base.Strings; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -401,7 +402,12 @@ public Table getTable(final String datasetId, final String tableId, TableOption. @Override public Table getTable(TableId tableId, TableOption... options) { - final TableId completeTableId = tableId.setProjectId(getOptions().getProjectId()); + // More context about why this: https://github.com/googleapis/google-cloud-java/issues/3808 + final TableId completeTableId = tableId.setProjectId( + Strings.isNullOrEmpty(tableId.getProject()) + ? getOptions().getProjectId() + : tableId.getProject() + ); final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Table answer = diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index fe0d8572d46f..8291045b34d1 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -603,6 +603,20 @@ public void testGetTableFromTableIdWithProject() { assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); } + @Test + public void testGetTableFromTableIdWithoutProject() { + TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); + TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); + EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + .andReturn(tableInfo.toPb()); + EasyMock.replay(bigqueryRpcMock); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + Table table = bigquery.getTable(tableId); + assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); + } + @Test public void testGetTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance();