Skip to content

Commit

Permalink
feat: Allow passing autodetect_schema on table update (#2661)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
emkornfield committed May 5, 2023
1 parent 4165e55 commit 4c01698
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Expand Up @@ -376,6 +376,14 @@ public static TableOption fields(TableField... fields) {
return new TableOption(
BigQueryRpc.Option.FIELDS, Helper.selector(TableField.REQUIRED_FIELDS, fields));
}

/**
* Returns an option to specify the schema of the table (only applicable for external tables)
* should be autodetected when updating the table from the underlying source.
*/
public static TableOption autodetectSchema(boolean autodetect) {
return new TableOption(BigQueryRpc.Option.AUTODETECT_SCHEMA, autodetect);
}
}

/* Class for specifying IAM options. */
Expand Down
Expand Up @@ -46,6 +46,7 @@ enum Option {
DELETE_CONTENTS("deleteContents"),
ALL_DATASETS("all"),
ALL_USERS("allUsers"),
AUTODETECT_SCHEMA("autodetectSchema"),
LABEL_FILTER("filter"),
MIN_CREATION_TIME("minCreationTime"),
MAX_CREATION_TIME("maxCreationTime"),
Expand Down
Expand Up @@ -279,6 +279,7 @@ public Table patch(Table table, Map<Option, ?> options) {
.patch(reference.getProjectId(), reference.getDatasetId(), reference.getTableId(), table)
.setPrettyPrint(false)
.setFields(Option.FIELDS.getString(options))
.setAutodetectSchema(BigQueryRpc.Option.AUTODETECT_SCHEMA.getBoolean(options))
.execute();
} catch (IOException ex) {
throw translate(ex);
Expand Down
Expand Up @@ -1260,6 +1260,24 @@ public void testUpdateTableWithSelectedFields() {
.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture());
}

@Test
public void testUpdateTableWithAutoDetectSchema() {
TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build();
TableInfo updatedTableInfoWithProject =
TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build();
when(bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture()))
.thenReturn(updatedTableInfoWithProject.toPb());
bigquery = options.getService();
Table table = bigquery.update(updatedTableInfo, BigQuery.TableOption.autodetectSchema(true));
Boolean selector =
(Boolean) capturedOptions.getValue().get(BigQueryRpc.Option.AUTODETECT_SCHEMA);
assertTrue(selector);
assertEquals(
new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table);
verify(bigqueryRpcMock)
.patch(eq(updatedTableInfoWithProject.toPb()), capturedOptions.capture());
}

@Test
public void testInsertAllWithRowIdShouldRetry() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Expand Down
Expand Up @@ -1566,6 +1566,45 @@ public void testSetPermExternalTableSchema() {
assertTrue(remoteTable.delete());
}

@Test
public void testUpdatePermExternableTableWithAutodetectSchemaUpdatesSchema() {
String tableName = "test_create_external_table_perm_with_auto_detect";
TableId tableId = TableId.of(DATASET, tableName);
Schema setSchema = Schema.of(TIMESTAMP_FIELD_SCHEMA, STRING_FIELD_SCHEMA);

ExternalTableDefinition externalTableDefinition =
ExternalTableDefinition.newBuilder(
"gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json())
.setSchema(setSchema)
.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);
assertEquals(setSchema, remoteTable.getDefinition().getSchema());

Table updatedTable =
bigquery.update(
createdTable
.toBuilder()
.setDefinition(
((ExternalTableDefinition) createdTable.getDefinition())
.toBuilder()
.setSchema(null)
.setAutodetect(true)
.build())
.build(),
BigQuery.TableOption.autodetectSchema(true));
// Schema should change.
assertTrue(!updatedTable.getDefinition().getSchema().equals(setSchema));

assertTrue(remoteTable.delete());
}

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

0 comments on commit 4c01698

Please sign in to comment.