From e9f2f0c1ee7efc32d02234172099f7585a02052f Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 13 Dec 2018 19:51:11 +0530 Subject: [PATCH 1/4] Fix Change in TableId behaviour for non default BigQueryClient project --- .../com/google/cloud/bigquery/BigQueryImpl.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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..8c04e42e528c 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() { @@ -829,8 +833,14 @@ public TableDataWriteChannel writer( JobId jobId, WriteChannelConfiguration writeChannelConfiguration) { return new TableDataWriteChannel( getOptions(), - jobId.setProjectId(getOptions().getProjectId()), - writeChannelConfiguration.setProjectId(getOptions().getProjectId())); + jobId.setProjectId( + Strings.isNullOrEmpty(jobId.getProject()) + ? getOptions().getProjectId() + : jobId.getProject()), + writeChannelConfiguration.setProjectId( + Strings.isNullOrEmpty(jobId.getProject()) + ? getOptions().getProjectId() + : jobId.getProject())); } @VisibleForTesting From e0e812ad77556c23993251fbf97988bba115d167 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 13 Dec 2018 20:11:47 +0530 Subject: [PATCH 2/4] Fix setProjectId for TableInfo --- .../src/main/java/com/google/cloud/bigquery/TableInfo.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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() { From a6a230c416cda5e3fe8df27a42d0753c131a2c53 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 14 Dec 2018 19:06:59 +0530 Subject: [PATCH 3/4] add test cases and fix writechannel --- .../java/com/google/cloud/bigquery/BigQueryImpl.java | 10 ++-------- .../cloud/bigquery/WriteChannelConfiguration.java | 6 +++++- .../java/com/google/cloud/bigquery/TableInfoTest.java | 7 +++++++ .../cloud/bigquery/WriteChannelConfigurationTest.java | 8 ++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) 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 8c04e42e528c..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 @@ -833,14 +833,8 @@ public TableDataWriteChannel writer( JobId jobId, WriteChannelConfiguration writeChannelConfiguration) { return new TableDataWriteChannel( getOptions(), - jobId.setProjectId( - Strings.isNullOrEmpty(jobId.getProject()) - ? getOptions().getProjectId() - : jobId.getProject()), - writeChannelConfiguration.setProjectId( - Strings.isNullOrEmpty(jobId.getProject()) - ? getOptions().getProjectId() - : jobId.getProject())); + jobId.setProjectId(getOptions().getProjectId()), + writeChannelConfiguration.setProjectId(getOptions().getProjectId())); } @VisibleForTesting 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..d87d0475aa74 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); From e7049518b8e713cf012bf6c52c84c276f2e70d82 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 14 Dec 2018 20:53:54 +0530 Subject: [PATCH 4/4] fix format code --- .../test/java/com/google/cloud/bigquery/TableInfoTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d87d0475aa74..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 @@ -228,9 +228,9 @@ public void testSetProjectId() { @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()); + 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) {