Repository navigation
refactor: resolve BigQuery schema on workers #4638
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
Open
fbiville
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
fbiville:neo4j/bq-schema-in-workers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...rc/main/java/com/google/cloud/teleport/v2/neo4j/providers/bigquery/BigQueryRowParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (C) 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.cloud.teleport.v2.neo4j.providers.bigquery; | ||
|
|
||
| import com.google.api.services.bigquery.model.TableSchema; | ||
| import java.io.Serial; | ||
| import org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils; | ||
| import org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord; | ||
| import org.apache.beam.sdk.schemas.Schema; | ||
| import org.apache.beam.sdk.transforms.SerializableFunction; | ||
| import org.apache.beam.sdk.values.Row; | ||
|
|
||
| /** | ||
| * Parses BigQuery records into Beam {@link Row rows} using the BigQuery schema supplied by the | ||
| * reader at execution time. | ||
| * | ||
| * <p>Resolving the schema on the worker (rather than when the pipeline graph is built) keeps all | ||
| * BigQuery connectivity out of the launcher: no dry-run job is needed before the pipeline starts, | ||
| * and no launch identity requires BigQuery permissions. | ||
| */ | ||
| public class BigQueryRowParser implements SerializableFunction<SchemaAndRecord, Row> { | ||
|
|
||
| @Serial private static final long serialVersionUID = 1L; | ||
|
|
||
| private static final BigQueryUtils.ConversionOptions CONVERSION_OPTIONS = | ||
| BigQueryUtils.ConversionOptions.builder() | ||
| // Beam's DATETIME only holds millisecond precision, like the TableRow based conversion | ||
| // that this parser replaces. Truncating matches that behaviour instead of failing on | ||
| // BigQuery's microsecond timestamps. | ||
| .setTruncateTimestamps(BigQueryUtils.ConversionOptions.TruncateTimestamps.TRUNCATE) | ||
| .build(); | ||
|
|
||
| // All records of a given read share the same TableSchema instance. Caching the last conversion | ||
| // avoids rebuilding the Beam schema for every row. | ||
| private transient volatile CachedSchema cachedSchema; | ||
|
|
||
| @Override | ||
| public Row apply(SchemaAndRecord schemaAndRecord) { | ||
| Schema beamSchema = beamSchemaFor(schemaAndRecord.getTableSchema()); | ||
| return BigQueryUtils.toBeamRow(schemaAndRecord.getRecord(), beamSchema, CONVERSION_OPTIONS); | ||
| } | ||
|
|
||
| private Schema beamSchemaFor(TableSchema tableSchema) { | ||
| CachedSchema cached = this.cachedSchema; | ||
| if (cached != null && cached.tableSchema == tableSchema) { | ||
| return cached.beamSchema; | ||
| } | ||
| var resolved = new CachedSchema(tableSchema, BigQueryUtils.fromTableSchema(tableSchema)); | ||
| this.cachedSchema = resolved; | ||
| return resolved.beamSchema; | ||
| } | ||
|
|
||
| private record CachedSchema(TableSchema tableSchema, Schema beamSchema) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...o-neo4j/src/main/java/com/google/cloud/teleport/v2/neo4j/utils/RuntimeSchemaRowCoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * Copyright (C) 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.cloud.teleport.v2.neo4j.utils; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.cache.Cache; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import com.google.common.io.ByteStreams; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.io.Serial; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.apache.beam.model.pipeline.v1.SchemaApi; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.RowCoder; | ||
| import org.apache.beam.sdk.coders.VarIntCoder; | ||
| import org.apache.beam.sdk.schemas.Schema; | ||
| import org.apache.beam.sdk.schemas.SchemaTranslation; | ||
| import org.apache.beam.sdk.values.Row; | ||
| import org.jspecify.annotations.NonNull; | ||
|
|
||
| /** | ||
| * Coder for {@link Row rows} whose schema is only known at pipeline execution time. | ||
| * | ||
| * <p>Beam's regular {@link RowCoder} needs a schema when the pipeline graph is built. Source reads | ||
| * whose schema can only be resolved by a worker (for instance BigQuery queries) cannot provide one | ||
| * at that point. This coder therefore writes the schema along with each element, so that decoding | ||
| * does not depend on any build-time information. | ||
| * | ||
| * <p>The schema is encoded with Beam's portable schema protobuf rather than Java serialization: it | ||
| * is substantially more compact and does not embed the random {@link Schema#getUUID() UUID} that | ||
| * every {@code Schema} instance carries. | ||
| */ | ||
| public class RuntimeSchemaRowCoder extends Coder<Row> { | ||
|
|
||
| public static final RuntimeSchemaRowCoder INSTANCE = new RuntimeSchemaRowCoder(); | ||
|
|
||
| @Serial private static final long serialVersionUID = 1L; | ||
|
|
||
| private static final VarIntCoder LENGTH_CODER = VarIntCoder.of(); | ||
|
|
||
| // A pipeline only ever sees a handful of distinct schemas; caching avoids re-serializing the | ||
| // same schema for every encoded element and reparsing it for every decoded element. The caches | ||
| // are bounded with an approximate-LRU eviction policy to guard against pathological schema | ||
| // churn: evicting an entry costs recomputation, never correctness, because the schema travels | ||
| // with each element. Cache hits must not serialize the hot path, since every harness thread | ||
| // encodes and decodes elements. | ||
| @VisibleForTesting static final int MAX_CACHED_SCHEMAS = 64; | ||
|
|
||
| private static final Cache<Schema, EncodedSchema> ENCODERS = | ||
| CacheBuilder.newBuilder().maximumSize(MAX_CACHED_SCHEMAS).build(); | ||
|
|
||
| private static final Cache<SchemaKey, EncodedSchema> DECODERS = | ||
| CacheBuilder.newBuilder().maximumSize(MAX_CACHED_SCHEMAS).build(); | ||
|
|
||
| @Override | ||
| public void encode(Row value, @NonNull OutputStream out) throws IOException { | ||
| var encodedSchema = encodedFor(value.getSchema()); | ||
| LENGTH_CODER.encode(encodedSchema.bytes.length, out); | ||
| out.write(encodedSchema.bytes); | ||
| encodedSchema.coder.encode(value, out); | ||
| } | ||
|
|
||
| @Override | ||
| public Row decode(@NonNull InputStream in) throws IOException { | ||
| int schemaSize = LENGTH_CODER.decode(in); | ||
| var serializedSchema = new byte[schemaSize]; | ||
| ByteStreams.readFully(in, serializedSchema); | ||
| var key = new SchemaKey(serializedSchema); | ||
| var encodedSchema = DECODERS.getIfPresent(key); | ||
| if (encodedSchema == null) { | ||
| try { | ||
| encodedSchema = | ||
| DECODERS.get( | ||
| key, | ||
| () -> { | ||
| var loaded = EncodedSchema.fromProtoBytes(serializedSchema); | ||
| // Decoded rows are re-encoded downstream: warm the encoder cache too. The loader | ||
| // runs once per distinct schema, even when threads race on the same miss. | ||
| ENCODERS.put(loaded.schema, loaded); | ||
| return loaded; | ||
| }); | ||
| } catch (ExecutionException e) { | ||
| if (e.getCause() instanceof IOException cause) { | ||
| throw cause; | ||
| } | ||
| throw new IOException("Unable to decode the row schema", e.getCause()); | ||
| } | ||
| } | ||
| return encodedSchema.coder.decode(in); | ||
| } | ||
|
|
||
| @Override | ||
| public Object structuralValue(Row value) { | ||
| // Delegating to the schema's RowCoder avoids re-encoding the schema bytes on every structural | ||
| // comparison. Equal rows under equal schemas encode identically, so this remains consistent | ||
| // with encoded equality. | ||
| return encodedFor(value.getSchema()).coder.structuralValue(value); | ||
| } | ||
|
|
||
| @Override | ||
| public void verifyDeterministic() { | ||
| // For a given schema, encoding is deterministic: the schema protobuf deliberately omits the | ||
| // random Schema UUID, and Beam-provided logical types serialize deterministically. | ||
| } | ||
|
|
||
| @Override | ||
| public @NonNull List<? extends Coder<?>> getCoderArguments() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| return o instanceof RuntimeSchemaRowCoder; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(RuntimeSchemaRowCoder.class); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static int cachedEncoderCount() { | ||
| return (int) ENCODERS.size(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static int cachedDecoderCount() { | ||
| return (int) DECODERS.size(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static void resetCaches() { | ||
| ENCODERS.invalidateAll(); | ||
| DECODERS.invalidateAll(); | ||
| } | ||
|
|
||
| private static EncodedSchema encodedFor(Schema schema) { | ||
| var encodedSchema = ENCODERS.getIfPresent(schema); | ||
| if (encodedSchema == null) { | ||
| // Put through the map view so that size-based eviction still applies. | ||
| encodedSchema = ENCODERS.asMap().computeIfAbsent(schema, EncodedSchema::new); | ||
| } | ||
| return encodedSchema; | ||
| } | ||
|
|
||
| /** Content-based key for a serialized schema, so decoding can hit the cache before parsing. */ | ||
| private record SchemaKey(byte[] bytes) { | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| return o instanceof SchemaKey && Arrays.equals(bytes, ((SchemaKey) o).bytes); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Arrays.hashCode(bytes); | ||
| } | ||
| } | ||
|
|
||
| private static final class EncodedSchema { | ||
| private final Schema schema; | ||
| private final byte[] bytes; | ||
| private final RowCoder coder; | ||
|
|
||
| private EncodedSchema(Schema schema) { | ||
| this(schema, SchemaTranslation.schemaToProto(schema, true, false).toByteArray()); | ||
| } | ||
|
|
||
| private EncodedSchema(Schema schema, byte[] bytes) { | ||
| this.schema = schema; | ||
| this.bytes = bytes; | ||
| this.coder = RowCoder.of(schema); | ||
| } | ||
|
|
||
| private static EncodedSchema fromProtoBytes(byte[] bytes) throws IOException { | ||
| try { | ||
| Schema schema = SchemaTranslation.schemaFromProto(SchemaApi.Schema.parseFrom(bytes)); | ||
| return new EncodedSchema(schema, bytes); | ||
| } catch (IOException e) { | ||
| throw new IOException("Unable to decode the row schema", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.