-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Dynamic Schema support to StorageWriteToBigQuery #39236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2b45865
7808954
1d2ceca
85b8546
8eb3754
d0c9aa6
9f7f24b
19c296a
9aface1
8645501
ab39151
3096ec7
d2743f8
fdb3b89
7850cbd
892f3d5
ce40e8e
b226c9a
a33b7f7
f08af0f
b42fda7
20969c6
3353e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run", | ||
| "modification": 16 | ||
| "modification": 21 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run", | ||
| "modification": 2 | ||
| "modification": 7 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4011,11 +4011,13 @@ private <DestinationT> WriteResult expandTyped( | |
| // TODO: If the user provided a schema, we should use that. There are things that can be | ||
| // specified in a | ||
| // BQ schema that don't have exact matches in a Beam schema (e.g. GEOGRAPHY types). | ||
| TableSchema tableSchema = BigQueryUtils.toTableSchema(input.getSchema()); | ||
| dynamicDestinations = | ||
| new ConstantSchemaDestinations<>( | ||
| dynamicDestinations, | ||
| StaticValueProvider.of(BigQueryHelpers.toJsonString(tableSchema))); | ||
| if (!hasSchema) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The definition of hasSchema conflates "the user supplied a DynamicDestinations" with "the user supplied a schema". The javadoc of useBeamSchema promises the BQ schema is inferred from the input schema. This change means that user supplied schema overrules the inferred schema, which could potentially be a breaking change if someone just passed an invalid schema while silently relying on input schema? Also I think this will fail pipelines that have DynamicDestination + getSchema returns null? because |
||
| TableSchema tableSchema = BigQueryUtils.toTableSchema(input.getSchema()); | ||
| dynamicDestinations = | ||
| new ConstantSchemaDestinations<>( | ||
| dynamicDestinations, | ||
| StaticValueProvider.of(BigQueryHelpers.toJsonString(tableSchema))); | ||
| } | ||
| } else if (writeProtoClass != null) { | ||
| if (!hasSchema) { | ||
| try { | ||
|
|
@@ -4487,6 +4489,7 @@ static void clearStaticCaches() throws ExecutionException, InterruptedException | |
| CreateTables.clearCreatedTables(); | ||
| TwoLevelMessageConverterCache.clear(); | ||
| StorageApiDynamicDestinationsTableRow.clearSchemaCache(); | ||
| StorageApiDynamicDestinationsBeamRow.clearSchemaCache(); | ||
| StorageApiWriteUnshardedRecords.clearCache(); | ||
| StorageApiWritesShardedRecords.clearCache(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,7 +32,6 @@ | |||||||||||||||||||||||||
| import org.apache.beam.sdk.options.PipelineOptions; | ||||||||||||||||||||||||||
| import org.apache.beam.sdk.transforms.SerializableFunction; | ||||||||||||||||||||||||||
| import org.apache.beam.sdk.util.Preconditions; | ||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects; | ||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Supplier; | ||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Suppliers; | ||||||||||||||||||||||||||
| import org.checkerframework.checker.nullness.qual.NonNull; | ||||||||||||||||||||||||||
|
|
@@ -94,8 +93,29 @@ public MessageConverter<T> getMessageConverter( | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| TableSchema destSchema = getSchema(destination); | ||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything here runs for every Storage Write API use. Can we scope these changes to only the portable provider? The declared schema is no longer authoritative for the proto descriptor. If there was a mismatch writing to BQ would raise an error. Is this a regression for users who relied on the declared schema being authoritative and on disagreement being loud? Some potentially changed behaviors
D = declared schema, T = existing table schema, R = a row. D = declared schema, T = existing table schema, R = a row. |
||||||||||||||||||||||||||
| TableSchema schemaToUse = destSchema; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| TableDestination tableDestination = getTable(destination); | ||||||||||||||||||||||||||
| TableReference tableReference = | ||||||||||||||||||||||||||
| tableDestination != null ? tableDestination.getTableReference() : null; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (tableReference != null && datasetService != null) { | ||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| TableSchema bqSchema = SCHEMA_CACHE.getSchema(tableReference, datasetService); | ||||||||||||||||||||||||||
| if (bqSchema != null) { | ||||||||||||||||||||||||||
| if (schemaToUse == null | ||||||||||||||||||||||||||
| || TableRowToStorageApiProto.hasExtraFields(schemaToUse, bqSchema)) { | ||||||||||||||||||||||||||
| schemaToUse = bqSchema; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||||
| // Schema cache lookup is best-effort fallback for dynamic destinations. | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return schemaUpdateOptions.isEmpty() | ||||||||||||||||||||||||||
| ? getConverter.apply(getSchema(destination)) | ||||||||||||||||||||||||||
| ? getConverter.apply(schemaToUse) | ||||||||||||||||||||||||||
| : new SchemaUpgradingTableRowConverter( | ||||||||||||||||||||||||||
| getConverter, options, datasetService, writeStreamService); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -200,9 +220,7 @@ class TableRowConverter implements MessageConverter<T> { | |||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| // Make sure we register this schema with the cache, unless there's already a more | ||||||||||||||||||||||||||
| // up-to-date schema. | ||||||||||||||||||||||||||
| localTableSchema = | ||||||||||||||||||||||||||
| MoreObjects.firstNonNull( | ||||||||||||||||||||||||||
| SCHEMA_CACHE.putSchemaIfAbsent(tableReference, localTableSchema), localTableSchema); | ||||||||||||||||||||||||||
| SCHEMA_CACHE.putSchemaIfAbsent(tableReference, localTableSchema); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| this.tableSchema = localTableSchema; | ||||||||||||||||||||||||||
| this.protoTableSchema = TableRowToStorageApiProto.schemaToProtoTableSchema(tableSchema); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can also trigger .github/trigger_files/beam_PostCommit_Python_Xlang_Gcp_Direct.json for faster validation