Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 460ef31

Browse files
authoredOct 17, 2022
fix: properly handle external table schema on table update (#2236)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #2235 ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 7bc59a7 commit 460ef31

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed
 

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,7 @@ public Table create(TableInfo tableInfo, TableOption... options) {
288288
? getOptions().getProjectId()
289289
: tableInfo.getTableId().getProject())
290290
.toPb();
291-
// Set schema on the Table for permanent external table
292-
if (tablePb.getExternalDataConfiguration() != null) {
293-
tablePb.setSchema(tablePb.getExternalDataConfiguration().getSchema());
294-
// clear table schema on ExternalDataConfiguration
295-
tablePb.getExternalDataConfiguration().setSchema(null);
296-
}
291+
handleExternalTableSchema(tablePb);
297292
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
298293
try {
299294
return Table.fromPb(
@@ -313,6 +308,16 @@ public com.google.api.services.bigquery.model.Table call() {
313308
}
314309
}
315310

311+
private void handleExternalTableSchema(
312+
final com.google.api.services.bigquery.model.Table tablePb) {
313+
// Set schema on the Table for permanent external table
314+
if (tablePb.getExternalDataConfiguration() != null) {
315+
tablePb.setSchema(tablePb.getExternalDataConfiguration().getSchema());
316+
// clear table schema on ExternalDataConfiguration
317+
tablePb.getExternalDataConfiguration().setSchema(null);
318+
}
319+
}
320+
316321
@Override
317322
public Routine create(RoutineInfo routineInfo, RoutineOption... options) {
318323
final com.google.api.services.bigquery.model.Routine routinePb =
@@ -679,6 +684,7 @@ public Table update(TableInfo tableInfo, TableOption... options) {
679684
? getOptions().getProjectId()
680685
: tableInfo.getTableId().getProject())
681686
.toPb();
687+
handleExternalTableSchema(tablePb);
682688
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
683689
try {
684690
return Table.fromPb(

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

+38
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,25 @@ public void testCreateTable() {
812812
verify(bigqueryRpcMock).create(tableInfo.toPb(), EMPTY_RPC_OPTIONS);
813813
}
814814

815+
@Test
816+
public void tesCreateExternalTable() {
817+
TableInfo createTableInfo =
818+
TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build())
819+
.setProjectId(OTHER_PROJECT);
820+
821+
com.google.api.services.bigquery.model.Table expectedCreateInput =
822+
createTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb());
823+
expectedCreateInput.getExternalDataConfiguration().setSchema(null);
824+
when(bigqueryRpcMock.create(expectedCreateInput, EMPTY_RPC_OPTIONS))
825+
.thenReturn(createTableInfo.toPb());
826+
BigQueryOptions bigQueryOptions =
827+
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
828+
bigquery = bigQueryOptions.getService();
829+
Table table = bigquery.create(createTableInfo);
830+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(createTableInfo)), table);
831+
verify(bigqueryRpcMock).create(expectedCreateInput, EMPTY_RPC_OPTIONS);
832+
}
833+
815834
@Test
816835
public void testCreateTableWithoutProject() {
817836
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
@@ -1189,6 +1208,25 @@ public void testUpdateTable() {
11891208
verify(bigqueryRpcMock).patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS);
11901209
}
11911210

1211+
@Test
1212+
public void testUpdateExternalTableWithNewSchema() {
1213+
TableInfo updatedTableInfo =
1214+
TableInfo.of(TABLE_ID, ExternalTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).build())
1215+
.setProjectId(OTHER_PROJECT);
1216+
1217+
com.google.api.services.bigquery.model.Table expectedPatchInput =
1218+
updatedTableInfo.toPb().setSchema(TABLE_SCHEMA.toPb());
1219+
expectedPatchInput.getExternalDataConfiguration().setSchema(null);
1220+
when(bigqueryRpcMock.patch(expectedPatchInput, EMPTY_RPC_OPTIONS))
1221+
.thenReturn(updatedTableInfo.toPb());
1222+
BigQueryOptions bigQueryOptions =
1223+
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
1224+
bigquery = bigQueryOptions.getService();
1225+
Table table = bigquery.update(updatedTableInfo);
1226+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table);
1227+
verify(bigqueryRpcMock).patch(expectedPatchInput, EMPTY_RPC_OPTIONS);
1228+
}
1229+
11921230
@Test
11931231
public void testUpdateTableWithoutProject() {
11941232
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);

0 commit comments

Comments
 (0)
Failed to load comments.