-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(bigquery): add ArrowDeserializer helper utility #13943
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
Draft
jinseopkim0
wants to merge
9
commits into
feat-bigquery-arrow-config
from
feat-bigquery-arrow-deserializer
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e51d2a
feat(bigquery): add ArrowDeserializer helper utility
jinseopkim0 4100042
fix(bigquery): resolve review comments in ArrowDeserializer
jinseopkim0 dc62766
fix(bigquery): use integer arithmetic for precise timestamp microseco…
jinseopkim0 20c1033
test(bigquery): add comprehensive unit test suite ArrowDeserializerTest
jinseopkim0 ccf3299
docs(bigquery): add Javadoc comments to ArrowDeserializer methods
jinseopkim0 5a45c16
refactor(bigquery): import Field and use simple short names in ArrowD…
jinseopkim0 08bb5b2
refactor(bigquery): clean up FQCNs in ArrowDeserializer and ArrowDese…
jinseopkim0 6215690
refactor(bigquery): import Arrow Field and Schema in ArrowDeserialize…
jinseopkim0 f139009
style(bigquery): format ArrowDeserializerTest with fmt-maven-plugin
jinseopkim0 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
325 changes: 325 additions & 0 deletions
325
...uery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ArrowDeserializer.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,325 @@ | ||
| /* | ||
| * Copyright 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.bigquery; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.io.BaseEncoding; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.FieldVector; | ||
| import org.apache.arrow.vector.TimeStampVector; | ||
| import org.apache.arrow.vector.VectorLoader; | ||
| import org.apache.arrow.vector.VectorSchemaRoot; | ||
| import org.apache.arrow.vector.complex.ListVector; | ||
| import org.apache.arrow.vector.complex.StructVector; | ||
| import org.apache.arrow.vector.ipc.ReadChannel; | ||
| import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; | ||
| import org.apache.arrow.vector.ipc.message.MessageSerializer; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType; | ||
| import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel; | ||
|
|
||
| /** | ||
| * Internal helper utility for converting Apache Arrow schemas and record batches into BigQuery | ||
| * Veneer objects. | ||
| */ | ||
| final class ArrowDeserializer { | ||
|
|
||
| private ArrowDeserializer() {} | ||
|
|
||
| /** | ||
| * Converts an Apache Arrow {@link org.apache.arrow.vector.types.pojo.Schema} to a BigQuery Veneer | ||
| * {@link Schema}. | ||
| * | ||
| * @param arrowSchema the Apache Arrow schema to convert | ||
| * @return the corresponding BigQuery Veneer Schema | ||
| */ | ||
| static Schema arrowSchemaToBigQuerySchema(org.apache.arrow.vector.types.pojo.Schema arrowSchema) { | ||
| List<Field> fields = new ArrayList<>(); | ||
| for (org.apache.arrow.vector.types.pojo.Field arrowField : arrowSchema.getFields()) { | ||
| fields.add(arrowFieldToBigQueryField(arrowField)); | ||
| } | ||
| return Schema.of(fields); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively converts an Apache Arrow {@link org.apache.arrow.vector.types.pojo.Field} to a | ||
| * BigQuery Veneer {@link Field}. | ||
| * | ||
| * @param arrowField the Arrow field to convert | ||
| * @return the corresponding BigQuery Veneer Field | ||
| */ | ||
| private static Field arrowFieldToBigQueryField( | ||
| org.apache.arrow.vector.types.pojo.Field arrowField) { | ||
| String name = arrowField.getName(); | ||
| ArrowType type = arrowField.getType(); | ||
| Field.Builder builder; | ||
|
|
||
| if (type instanceof ArrowType.List) { | ||
| if (arrowField.getChildren().isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "Arrow List field must have at least one child field: " + name); | ||
| } | ||
| org.apache.arrow.vector.types.pojo.Field innerField = arrowField.getChildren().get(0); | ||
| LegacySQLTypeName innerType = arrowTypeToLegacySQLTypeName(innerField.getType()); | ||
| builder = Field.newBuilder(name, innerType); | ||
| builder.setMode(Field.Mode.REPEATED); | ||
| if (!innerField.getChildren().isEmpty()) { | ||
| List<Field> subFields = new ArrayList<>(); | ||
| for (org.apache.arrow.vector.types.pojo.Field childField : innerField.getChildren()) { | ||
| subFields.add(arrowFieldToBigQueryField(childField)); | ||
| } | ||
| builder.setType(LegacySQLTypeName.RECORD, FieldList.of(subFields)); | ||
| } | ||
| } else { | ||
| LegacySQLTypeName bqType = arrowTypeToLegacySQLTypeName(type); | ||
| builder = Field.newBuilder(name, bqType); | ||
| if (arrowField.isNullable()) { | ||
| builder.setMode(Field.Mode.NULLABLE); | ||
| } else { | ||
| builder.setMode(Field.Mode.REQUIRED); | ||
| } | ||
| if (!arrowField.getChildren().isEmpty()) { | ||
| List<Field> subFields = new ArrayList<>(); | ||
| for (org.apache.arrow.vector.types.pojo.Field childField : innerFieldChildren(arrowField)) { | ||
| subFields.add(arrowFieldToBigQueryField(childField)); | ||
| } | ||
| builder.setType(LegacySQLTypeName.RECORD, FieldList.of(subFields)); | ||
| } | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| private static List<org.apache.arrow.vector.types.pojo.Field> innerFieldChildren( | ||
| org.apache.arrow.vector.types.pojo.Field arrowField) { | ||
| return arrowField.getChildren(); | ||
| } | ||
|
|
||
| /** | ||
| * Maps an Apache Arrow data type {@link ArrowType} to a BigQuery {@link LegacySQLTypeName}. | ||
| * | ||
| * @param type the Arrow data type to map | ||
| * @return the corresponding BigQuery LegacySQLTypeName | ||
| * @throws IllegalArgumentException if the Arrow type is unsupported | ||
| */ | ||
| private static LegacySQLTypeName arrowTypeToLegacySQLTypeName(ArrowType type) { | ||
| switch (type.getTypeID()) { | ||
| case Int: | ||
| return LegacySQLTypeName.INTEGER; | ||
| case FloatingPoint: | ||
| return LegacySQLTypeName.FLOAT; | ||
| case Utf8: | ||
| return LegacySQLTypeName.STRING; | ||
| case Bool: | ||
| return LegacySQLTypeName.BOOLEAN; | ||
| case Binary: | ||
| return LegacySQLTypeName.BYTES; | ||
| case Decimal: | ||
| return LegacySQLTypeName.NUMERIC; | ||
| case Timestamp: | ||
| return LegacySQLTypeName.TIMESTAMP; | ||
| case Date: | ||
| return LegacySQLTypeName.DATE; | ||
| case Time: | ||
| return LegacySQLTypeName.TIME; | ||
| case Struct: | ||
| return LegacySQLTypeName.RECORD; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported Arrow type: " + type.getTypeID()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes a raw binary Arrow record batch payload into a list of BigQuery {@link | ||
| * FieldValueList} row objects. | ||
| * | ||
| * <p>Allocates off-heap memory within a local {@link RootAllocator} scope and closes all Arrow | ||
| * vector resources before returning, guaranteeing that native memory is released. | ||
| * | ||
| * @param recordBatchBytes the raw binary Arrow record batch payload | ||
| * @param schema the target BigQuery Schema | ||
| * @param arrowSchema the Arrow schema describing the record batch structure | ||
| * @return an immutable list of FieldValueList row objects | ||
| * @throws IOException if deserialization of the Arrow record batch fails | ||
| */ | ||
| static List<FieldValueList> deserializeRecordBatch( | ||
| byte[] recordBatchBytes, Schema schema, org.apache.arrow.vector.types.pojo.Schema arrowSchema) | ||
| throws IOException { | ||
| try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) { | ||
| List<FieldVector> vectors = new ArrayList<>(); | ||
| try { | ||
| for (org.apache.arrow.vector.types.pojo.Field field : arrowSchema.getFields()) { | ||
| vectors.add(field.createVector(allocator)); | ||
| } | ||
| } catch (Throwable t) { | ||
| for (int i = vectors.size() - 1; i >= 0; i--) { | ||
| try { | ||
| vectors.get(i).close(); | ||
| } catch (Exception e) { | ||
| t.addSuppressed(e); | ||
| } | ||
| } | ||
| throw t; | ||
| } | ||
|
jinseopkim0 marked this conversation as resolved.
|
||
| try (VectorSchemaRoot root = new VectorSchemaRoot(vectors)) { | ||
| VectorLoader loader = new VectorLoader(root); | ||
| try (ArrowRecordBatch deserializedBatch = | ||
| MessageSerializer.deserializeRecordBatch( | ||
| new ReadChannel(new ByteArrayReadableSeekableByteChannel(recordBatchBytes)), | ||
| allocator)) { | ||
| loader.load(deserializedBatch); | ||
| int rowCount = root.getRowCount(); | ||
| List<FieldValueList> rows = new ArrayList<>(rowCount); | ||
| for (int i = 0; i < rowCount; i++) { | ||
| rows.add(arrowRootToFieldValueList(root, i, schema)); | ||
| } | ||
| return ImmutableList.copyOf(rows); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
jinseopkim0 marked this conversation as resolved.
jinseopkim0 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Extracts a single row at the specified index from a {@link VectorSchemaRoot} into a {@link | ||
| * FieldValueList}. | ||
| * | ||
| * @param root the VectorSchemaRoot containing column vectors | ||
| * @param rowIndex the 0-based row index to extract | ||
| * @param schema the BigQuery schema corresponding to the vectors | ||
| * @return the extracted FieldValueList row object | ||
| * @throws IllegalArgumentException if vector count does not match schema field count | ||
| */ | ||
| static FieldValueList arrowRootToFieldValueList( | ||
| VectorSchemaRoot root, int rowIndex, Schema schema) { | ||
| if (root.getFieldVectors().size() != schema.getFields().size()) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Schema mismatch: Arrow vector count (%d) does not match BigQuery schema field count (%d)", | ||
| root.getFieldVectors().size(), schema.getFields().size())); | ||
| } | ||
| List<FieldValue> fieldValues = new ArrayList<>(); | ||
| for (int colIndex = 0; colIndex < root.getFieldVectors().size(); colIndex++) { | ||
|
jinseopkim0 marked this conversation as resolved.
|
||
| FieldVector vector = root.getVector(colIndex); | ||
| Field bqField = schema.getFields().get(colIndex); | ||
| fieldValues.add(arrowVectorToFieldValue(vector, rowIndex, bqField)); | ||
| } | ||
| return FieldValueList.of(fieldValues, schema.getFields()); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a single cell value within a {@link FieldVector} to a BigQuery {@link FieldValue}. | ||
| * | ||
| * <p>Handles null values, repeated list vectors, nested struct vectors, and primitive type | ||
| * conversions. | ||
| * | ||
| * @param vector the Arrow column vector | ||
| * @param rowIndex the 0-based row index | ||
| * @param bqField the corresponding BigQuery Field definition | ||
| * @return the converted FieldValue object | ||
| */ | ||
| private static FieldValue arrowVectorToFieldValue( | ||
| FieldVector vector, int rowIndex, Field bqField) { | ||
| if (vector.isNull(rowIndex)) { | ||
| return FieldValue.of(FieldValue.Attribute.PRIMITIVE, null); | ||
| } | ||
|
|
||
| // Handle repeated fields | ||
| if (bqField.getMode() == Field.Mode.REPEATED) { | ||
| ListVector listVector = (ListVector) vector; | ||
| FieldVector dataVector = (FieldVector) listVector.getDataVector(); | ||
| int start = listVector.getElementStartIndex(rowIndex); | ||
| int end = listVector.getElementEndIndex(rowIndex); | ||
| List<FieldValue> elements = new ArrayList<>(end - start); | ||
| Field.Builder elementBuilder = Field.newBuilder(bqField.getName(), bqField.getType()); | ||
| if (bqField.getType() == LegacySQLTypeName.RECORD && bqField.getSubFields() != null) { | ||
| elementBuilder.setType(LegacySQLTypeName.RECORD, bqField.getSubFields()); | ||
| } | ||
| Field elementBqField = elementBuilder.setMode(Field.Mode.NULLABLE).build(); | ||
| for (int k = start; k < end; k++) { | ||
| elements.add(arrowVectorToFieldValue(dataVector, k, elementBqField)); | ||
| } | ||
| return FieldValue.of( | ||
| FieldValue.Attribute.REPEATED, FieldValueList.of(elements, bqField.getSubFields())); | ||
| } | ||
|
|
||
| // Handle RECORD/STRUCT fields | ||
| if (bqField.getType() == LegacySQLTypeName.RECORD) { | ||
| StructVector structVector = (StructVector) vector; | ||
| if (structVector.size() != bqField.getSubFields().size()) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Schema mismatch for field '%s': Arrow struct size (%d) does not match BigQuery subfields size (%d)", | ||
| bqField.getName(), structVector.size(), bqField.getSubFields().size())); | ||
| } | ||
| List<FieldValue> elements = new ArrayList<>(structVector.size()); | ||
| for (int colIndex = 0; colIndex < structVector.size(); colIndex++) { | ||
|
jinseopkim0 marked this conversation as resolved.
|
||
| FieldVector childVector = (FieldVector) structVector.getChildByOrdinal(colIndex); | ||
| Field childBqField = bqField.getSubFields().get(colIndex); | ||
| elements.add(arrowVectorToFieldValue(childVector, rowIndex, childBqField)); | ||
| } | ||
| return FieldValue.of( | ||
| FieldValue.Attribute.RECORD, FieldValueList.of(elements, bqField.getSubFields())); | ||
| } | ||
|
|
||
| // Handle primitive types | ||
| String stringVal; | ||
| if (bqField.getType() == LegacySQLTypeName.TIMESTAMP) { | ||
| // Arrow timestamps are long values representing epoch seconds/millis/micros/nanos. | ||
| // Standard BigQuery JSON returns timestamps as string of epoch seconds with micro precision | ||
| // (e.g. "1408452095.220000"). | ||
| TimeStampVector tsVector = (TimeStampVector) vector; | ||
| long rawVal = tsVector.get(rowIndex); | ||
| ArrowType.Timestamp tsType = (ArrowType.Timestamp) vector.getField().getType(); | ||
| long micros; | ||
| switch (tsType.getUnit()) { | ||
| case SECOND: | ||
| micros = rawVal * 1_000_000L; | ||
| break; | ||
| case MILLISECOND: | ||
| micros = rawVal * 1_000L; | ||
| break; | ||
| case MICROSECOND: | ||
| micros = rawVal; | ||
| break; | ||
| case NANOSECOND: | ||
| micros = rawVal / 1_000L; | ||
| break; | ||
| default: | ||
| micros = rawVal; | ||
| } | ||
| long seconds = micros / 1_000_000L; | ||
| long remainingMicros = Math.abs(micros % 1_000_000L); | ||
| if (micros < 0 && seconds == 0) { | ||
| stringVal = String.format(Locale.US, "-0.%06d", remainingMicros); | ||
| } else { | ||
| stringVal = String.format(Locale.US, "%d.%06d", seconds, remainingMicros); | ||
| } | ||
| } else { | ||
| Object value = vector.getObject(rowIndex); | ||
| if (value instanceof byte[]) { | ||
| stringVal = BaseEncoding.base64().encode((byte[]) value); | ||
| } else { | ||
| stringVal = String.valueOf(value); | ||
| } | ||
| } | ||
|
|
||
| return FieldValue.of(FieldValue.Attribute.PRIMITIVE, stringVal); | ||
| } | ||
| } | ||
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.