Skip to content

Commit 73e829b

Browse files
feat: set Table.Schema for permanent external tables (#1701)
Per discussion related to go/bq-external-table-schema
1 parent fffdc14 commit 73e829b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ public Table create(TableInfo tableInfo, TableOption... options) {
286286
? getOptions().getProjectId()
287287
: tableInfo.getTableId().getProject())
288288
.toPb();
289+
// Set schema on the Table for permanent external table
290+
if (tablePb.getExternalDataConfiguration() != null) {
291+
tablePb.setSchema(tablePb.getExternalDataConfiguration().getSchema());
292+
// clear table schema on ExternalDataConfiguration
293+
tablePb.getExternalDataConfiguration().setSchema(null);
294+
}
289295
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
290296
try {
291297
return Table.fromPb(

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,28 @@ public void testCreateExternalTable() throws InterruptedException {
11361136
assertTrue(remoteTable.delete());
11371137
}
11381138

1139+
@Test
1140+
public void testSetPermExternalTableSchema() {
1141+
String tableName = "test_create_external_table_perm";
1142+
TableId tableId = TableId.of(DATASET, tableName);
1143+
ExternalTableDefinition externalTableDefinition =
1144+
ExternalTableDefinition.newBuilder(
1145+
"gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json())
1146+
.setSchema(TABLE_SCHEMA)
1147+
.setConnectionId(
1148+
"projects/java-docs-samples-testing/locations/us/connections/DEVREL_TEST_CONNECTION")
1149+
.build();
1150+
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
1151+
Table createdTable = bigquery.create(tableInfo);
1152+
1153+
assertNotNull(createdTable);
1154+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1155+
assertEquals(tableName, createdTable.getTableId().getTable());
1156+
Table remoteTable = bigquery.getTable(DATASET, tableName);
1157+
assertNotNull(remoteTable);
1158+
assertTrue(remoteTable.delete());
1159+
}
1160+
11391161
@Test
11401162
public void testCreateViewTable() throws InterruptedException {
11411163
String tableName = "test_create_view_table";

0 commit comments

Comments
 (0)