Skip to content

Commit

Permalink
[java] Fix avro bytes conversion for BQ storage (#33412)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RustedBones authored Jan 6, 2025
1 parent d5248c1 commit b3e7f55
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b3e7f55

Please sign in to comment.