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 4c01698

Browse files
authoredMay 5, 2023
feat: Allow passing autodetect_schema on table update (#2661)
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: - Didn't open an issue because this seems trivial, happy to adapt if necessary. - [x ] Ensure the tests and linter pass (mvn test verify - [x ] Code coverage does not decrease (if any source code was changed) - [x ] Appropriate docs were updated (if necessary)
1 parent 4165e55 commit 4c01698

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
 

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

+8
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ public static TableOption fields(TableField... fields) {
376376
return new TableOption(
377377
BigQueryRpc.Option.FIELDS, Helper.selector(TableField.REQUIRED_FIELDS, fields));
378378
}
379+
380+
/**
381+
* Returns an option to specify the schema of the table (only applicable for external tables)
382+
* should be autodetected when updating the table from the underlying source.
383+
*/
384+
public static TableOption autodetectSchema(boolean autodetect) {
385+
return new TableOption(BigQueryRpc.Option.AUTODETECT_SCHEMA, autodetect);
386+
}
379387
}
380388

381389
/* Class for specifying IAM options. */

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum Option {
4646
DELETE_CONTENTS("deleteContents"),
4747
ALL_DATASETS("all"),
4848
ALL_USERS("allUsers"),
49+
AUTODETECT_SCHEMA("autodetectSchema"),
4950
LABEL_FILTER("filter"),
5051
MIN_CREATION_TIME("minCreationTime"),
5152
MAX_CREATION_TIME("maxCreationTime"),

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public Table patch(Table table, Map<Option, ?> options) {
279279
.patch(reference.getProjectId(), reference.getDatasetId(), reference.getTableId(), table)
280280
.setPrettyPrint(false)
281281
.setFields(Option.FIELDS.getString(options))
282+
.setAutodetectSchema(BigQueryRpc.Option.AUTODETECT_SCHEMA.getBoolean(options))
282283
.execute();
283284
} catch (IOException ex) {
284285
throw translate(ex);

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

+18
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,24 @@ public void testUpdateTableWithSelectedFields() {
12601260
.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture());
12611261
}
12621262

1263+
@Test
1264+
public void testUpdateTableWithAutoDetectSchema() {
1265+
TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build();
1266+
TableInfo updatedTableInfoWithProject =
1267+
TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build();
1268+
when(bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()))
1269+
.thenReturn(updatedTableInfoWithProject.toPb());
1270+
bigquery = options.getService();
1271+
Table table = bigquery.update(updatedTableInfo, BigQuery.TableOption.autodetectSchema(true));
1272+
Boolean selector =
1273+
(Boolean) capturedOptions.getValue().get(BigQueryRpc.Option.AUTODETECT_SCHEMA);
1274+
assertTrue(selector);
1275+
assertEquals(
1276+
new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table);
1277+
verify(bigqueryRpcMock)
1278+
.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture());
1279+
}
1280+
12631281
@Test
12641282
public void testInsertAllWithRowIdShouldRetry() {
12651283
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");

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

+39
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,45 @@ public void testSetPermExternalTableSchema() {
15661566
assertTrue(remoteTable.delete());
15671567
}
15681568

1569+
@Test
1570+
public void testUpdatePermExternableTableWithAutodetectSchemaUpdatesSchema() {
1571+
String tableName = "test_create_external_table_perm_with_auto_detect";
1572+
TableId tableId = TableId.of(DATASET, tableName);
1573+
Schema setSchema = Schema.of(TIMESTAMP_FIELD_SCHEMA, STRING_FIELD_SCHEMA);
1574+
1575+
ExternalTableDefinition externalTableDefinition =
1576+
ExternalTableDefinition.newBuilder(
1577+
"gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json())
1578+
.setSchema(setSchema)
1579+
.build();
1580+
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
1581+
Table createdTable = bigquery.create(tableInfo);
1582+
1583+
assertNotNull(createdTable);
1584+
assertEquals(DATASET, createdTable.getTableId().getDataset());
1585+
assertEquals(tableName, createdTable.getTableId().getTable());
1586+
Table remoteTable = bigquery.getTable(DATASET, tableName);
1587+
assertNotNull(remoteTable);
1588+
assertEquals(setSchema, remoteTable.getDefinition().getSchema());
1589+
1590+
Table updatedTable =
1591+
bigquery.update(
1592+
createdTable
1593+
.toBuilder()
1594+
.setDefinition(
1595+
((ExternalTableDefinition) createdTable.getDefinition())
1596+
.toBuilder()
1597+
.setSchema(null)
1598+
.setAutodetect(true)
1599+
.build())
1600+
.build(),
1601+
BigQuery.TableOption.autodetectSchema(true));
1602+
// Schema should change.
1603+
assertTrue(!updatedTable.getDefinition().getSchema().equals(setSchema));
1604+
1605+
assertTrue(remoteTable.delete());
1606+
}
1607+
15691608
@Test
15701609
public void testCreateViewTable() throws InterruptedException {
15711610
String tableName = "test_create_view_table";

0 commit comments

Comments
 (0)
Failed to load comments.