From b3e7f55762bcb7a0829876c43bd6471dc77c593d Mon Sep 17 00:00:00 2001 From: Michel Davit Date: Mon, 6 Jan 2025 16:07:09 +0100 Subject: [PATCH] [java] Fix avro bytes conversion for BQ storage (#33412) Avro byte is represented with java ByteBuffer. Current implementation expects a byte array. This fails in practice with a ClassCastException: class java.nio.HeapByteBuffer cannot be cast to class [B --- .../io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java | 2 +- .../gcp/bigquery/AvroGenericRecordToStorageApiProtoTest.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java b/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java index 0b7e17b89090..c721395eec79 100644 --- a/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java +++ b/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProto.java @@ -86,7 +86,7 @@ public class AvroGenericRecordToStorageApiProto { .put(Schema.Type.STRING, Object::toString) .put(Schema.Type.BOOLEAN, Function.identity()) .put(Schema.Type.ENUM, o -> o.toString()) - .put(Schema.Type.BYTES, o -> ByteString.copyFrom((byte[]) o)) + .put(Schema.Type.BYTES, o -> ByteString.copyFrom(((ByteBuffer) o).duplicate())) .build(); // A map of supported logical types to their encoding functions. diff --git a/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProtoTest.java b/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProtoTest.java index 6a59afeed823..472173c67412 100644 --- a/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProtoTest.java +++ b/sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/AvroGenericRecordToStorageApiProtoTest.java @@ -28,6 +28,7 @@ import com.google.protobuf.Descriptors; import com.google.protobuf.DynamicMessage; import java.math.BigDecimal; +import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -308,7 +309,7 @@ enum TestEnum { Instant now = Instant.now(); baseRecord = new GenericRecordBuilder(BASE_SCHEMA) - .set("bytesValue", BYTES) + .set("bytesValue", ByteBuffer.wrap(BYTES)) .set("intValue", (int) 3) .set("longValue", (long) 4) .set("floatValue", (float) 3.14)