Skip to content

Commit

Permalink
feat: set Table.Schema for permanent external tables (#1701)
Browse files Browse the repository at this point in the history
Per discussion related to go/bq-external-table-schema
  • Loading branch information
stephaniewang526 committed Mar 11, 2022
1 parent fffdc14 commit 73e829b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Expand Up @@ -286,6 +286,12 @@ public Table create(TableInfo tableInfo, TableOption... options) {
? getOptions().getProjectId()
: tableInfo.getTableId().getProject())
.toPb();
// Set schema on the Table for permanent external table
if (tablePb.getExternalDataConfiguration() != null) {
tablePb.setSchema(tablePb.getExternalDataConfiguration().getSchema());
// clear table schema on ExternalDataConfiguration
tablePb.getExternalDataConfiguration().setSchema(null);
}
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
return Table.fromPb(
Expand Down
Expand Up @@ -1136,6 +1136,28 @@ public void testCreateExternalTable() throws InterruptedException {
assertTrue(remoteTable.delete());
}

@Test
public void testSetPermExternalTableSchema() {
String tableName = "test_create_external_table_perm";
TableId tableId = TableId.of(DATASET, tableName);
ExternalTableDefinition externalTableDefinition =
ExternalTableDefinition.newBuilder(
"gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json())
.setSchema(TABLE_SCHEMA)
.setConnectionId(
"projects/java-docs-samples-testing/locations/us/connections/DEVREL_TEST_CONNECTION")
.build();
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
Table createdTable = bigquery.create(tableInfo);

assertNotNull(createdTable);
assertEquals(DATASET, createdTable.getTableId().getDataset());
assertEquals(tableName, createdTable.getTableId().getTable());
Table remoteTable = bigquery.getTable(DATASET, tableName);
assertNotNull(remoteTable);
assertTrue(remoteTable.delete());
}

@Test
public void testCreateViewTable() throws InterruptedException {
String tableName = "test_create_view_table";
Expand Down

0 comments on commit 73e829b

Please sign in to comment.