jbonofre commented on code in PR #1147:
URL: https://github.com/apache/arrow-java/pull/1147#discussion_r4718383118
##########
adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java:
##########
@@ -36,9 +36,7 @@ public AvroStringConsumer(VarCharVector vector) {
@Override
public void consume(Decoder decoder) throws IOException {
- // cacheBuffer is initialized null and create in the first consume,
- // if its capacity < size to read, decoder will create a new one with new
capacity.
- cacheBuffer = decoder.readBytes(cacheBuffer);
- vector.setSafe(currentIndex++, cacheBuffer, 0, cacheBuffer.limit());
+ cachedUtf8 = decoder.readString(cachedUtf8);
+ vector.setSafe(currentIndex++, cachedUtf8.getBytes(), 0,
cachedUtf8.getByteLength());
Review Comment:
This change was already merged to `main`. Please rebase and resolve the
conflicts.
##########
.gitignore:
##########
@@ -32,3 +32,6 @@ cmake_install.cmake
dependency-reduced-pom.xml
install_manifest.txt
target/
+.claude/
+CLAUDE.md
+*.class
Review Comment:
Please revert these changes to `.gitignore` as they are unrelated.
##########
adapter/avro/src/main/java/org/apache/arrow/adapter/avro/AvroToArrowUtils.java:
##########
@@ -431,7 +431,7 @@ private static Consumer createSkipConsumer(Schema schema) {
case UNION:
List<Consumer> unionDelegates =
schema.getTypes().stream().map(s ->
createSkipConsumer(s)).collect(Collectors.toList());
- skipFunction = decoder ->
unionDelegates.get(decoder.readInt()).consume(decoder);
+ skipFunction = decoder ->
unionDelegates.get(decoder.readIndex()).consume(decoder);
Review Comment:
Using `decoder.readIndex()` here is right, but note that `case NULL:` below
is currently `skipFunction = decoder -> {};`. When skipping a nullable union
whose value is `null`, `ValidatingDecoder` still expects `decoder.readNull()`
to advance past `Symbol.NULL`:
```java
case NULL:
skipFunction = decoder -> decoder.readNull();
break;
```
##########
adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroUnionsConsumer.java:
##########
@@ -42,7 +42,7 @@ public AvroUnionsConsumer(UnionVector vector, Consumer[]
delegates, Types.MinorT
@Override
public void consume(Decoder decoder) throws IOException {
- int fieldIndex = decoder.readInt();
+ int fieldIndex = decoder.readIndex();
Review Comment:
Note that when `AvroUnionsConsumer` delegates a `null` union branch to
`AvroNullConsumer`, `AvroNullConsumer#consume` currently only increments
`currentIndrex++` without calling `decoder.readNull()`. Calling
`decoder.readNull()` in `AvroNullConsumer#consume` is needed for
`AvroUnionsConsumer` to work with `ValidatingDecoder` when a union value is
`null`.
##########
adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java:
##########
@@ -474,4 +482,27 @@ public void testNullableUnionType() throws Exception {
checkPrimitiveResult(expected, vector);
}
+
+ @Test
+ public void testNullableStringTypeWithValidatingDecoder() throws Exception {
+ Schema schema = getSchema("test_nullable_string.avsc");
+
+ GenericRecord record = new GenericData.Record(schema);
+ record.put(0, "hello");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ BinaryEncoder encoder = new EncoderFactory().directBinaryEncoder(out,
null);
+ DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
+ writer.write(record, encoder);
+ encoder.flush();
+
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Decoder binaryDecoder = DecoderFactory.get().directBinaryDecoder(in, null);
+ Decoder validatingDecoder = DecoderFactory.get().validatingDecoder(schema,
binaryDecoder);
+
+ VectorSchemaRoot root = AvroToArrow.avroToArrow(schema, validatingDecoder,
config);
+ assertEquals(1, root.getRowCount());
+ FieldVector vector = root.getVector("f0");
+ assertEquals("hello", vector.getObject(0).toString());
+ }
Review Comment:
I have two suggestions here:
1. Please test both non-null and `null` records so both branches of
`AvroNullableConsumer` (`typeIndex == nullIndex` and `typeIndex != nullIndex`)
are covered with `ValidatingDecoder`.
2. Wrap `VectorSchemaRoot` in a try-with-resources block so the allocated
vector buffers are closed after the assertion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]