diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 16814e99a3..b2e939df09 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -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 optionsMap = optionMap(options); try { return Table.fromPb( diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index 9cab253a21..1ca5001dc8 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -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";