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 9f2d46dbf46f..e30b0ffd2156 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 @@ -625,7 +625,11 @@ private static Tuple, Long> listTableData( final BigQueryOptions serviceOptions, final Map optionsMap) { try { - final TableId completeTableId = tableId.setProjectId(serviceOptions.getProjectId()); + final TableId completeTableId = + tableId.setProjectId( + Strings.isNullOrEmpty(serviceOptions.getProjectId()) + ? tableId.getProject() + : serviceOptions.getProjectId()); TableDataList result = runWithRetries( new Callable() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index 2ed0891a83fd..37831cf943e4 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.api.client.util.Data; +import com.google.api.client.util.Strings; import com.google.api.core.BetaApi; import com.google.api.services.bigquery.model.Table; import com.google.common.base.Function; @@ -396,7 +397,10 @@ public static TableInfo of(TableId tableId, TableDefinition definition) { } TableInfo setProjectId(String projectId) { - return toBuilder().setTableId(getTableId().setProjectId(projectId)).build(); + if (Strings.isNullOrEmpty(getTableId().getProject())) { + return toBuilder().setTableId(getTableId().setProjectId(projectId)).build(); + } + return this; } Table toPb() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/WriteChannelConfiguration.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/WriteChannelConfiguration.java index 308042fbf7e5..d84d8b60a392 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/WriteChannelConfiguration.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/WriteChannelConfiguration.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.client.util.Strings; import com.google.api.services.bigquery.model.JobConfigurationLoad; import com.google.cloud.bigquery.JobInfo.CreateDisposition; import com.google.cloud.bigquery.JobInfo.SchemaUpdateOption; @@ -390,7 +391,10 @@ public int hashCode() { } WriteChannelConfiguration setProjectId(String projectId) { - return toBuilder().setDestinationTable(getDestinationTable().setProjectId(projectId)).build(); + if (Strings.isNullOrEmpty(getDestinationTable().getProject())) { + return toBuilder().setDestinationTable(getDestinationTable().setProjectId(projectId)).build(); + } + return this; } com.google.api.services.bigquery.model.JobConfiguration toPb() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java index cfc0d646fc00..98434713478d 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java @@ -226,6 +226,13 @@ public void testSetProjectId() { assertEquals("project", VIEW_INFO.setProjectId("project").getTableId().getProject()); } + @Test + public void testSetProjectIdDoNotOverride() { + TableInfo tableInfo = TableInfo.of(TABLE_ID, TABLE_DEFINITION).setProjectId("project"); + tableInfo.setProjectId("not-override-project").toBuilder(); + assertEquals("project", tableInfo.getTableId().getProject()); + } + private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected, value); assertEquals(expected.getTableId(), value.getTableId()); diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/WriteChannelConfigurationTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/WriteChannelConfigurationTest.java index b3a0b7a9e90c..9fe80612a059 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/WriteChannelConfigurationTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/WriteChannelConfigurationTest.java @@ -164,6 +164,14 @@ public void testToPbAndFromPb() { compareLoadConfiguration(configuration, WriteChannelConfiguration.fromPb(configuration.toPb())); } + @Test + public void testSetProjectIdDoNotOverride() { + WriteChannelConfiguration configuration = + WriteChannelConfiguration.of(TABLE_ID).setProjectId("project"); + configuration.setProjectId("different-project").toBuilder(); + assertEquals("project", configuration.getDestinationTable().getProject()); + } + private void compareLoadConfiguration( WriteChannelConfiguration expected, WriteChannelConfiguration value) { assertEquals(expected, value);