Repository navigation
GH-834: Fix Avro nullable consumer to use readIndex for unions - #1147
Kaustav-Sarkar wants to merge 1 commit into
Conversation
|
Can a maintainer add labels, I do not have permission |
| .claude/ | ||
| CLAUDE.md | ||
| *.class |
There was a problem hiding this comment.
Please revert these changes to .gitignore as they are unrelated.
| cachedUtf8 = decoder.readString(cachedUtf8); | ||
| vector.setSafe(currentIndex++, cachedUtf8.getBytes(), 0, cachedUtf8.getByteLength()); |
There was a problem hiding this comment.
This change was already merged to main. Please rebase and resolve the conflicts.
| 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); |
There was a problem hiding this 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:
case NULL:
skipFunction = decoder -> decoder.readNull();
break;| @Override | ||
| public void consume(Decoder decoder) throws IOException { | ||
| int fieldIndex = decoder.readInt(); | ||
| int fieldIndex = decoder.readIndex(); |
There was a problem hiding this 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.
| @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()); | ||
| } |
There was a problem hiding this comment.
I have two suggestions here:
- Please test both non-null and
nullrecords so both branches ofAvroNullableConsumer(typeIndex == nullIndexandtypeIndex != nullIndex) are covered withValidatingDecoder. - Wrap
VectorSchemaRootin a try-with-resources block so the allocated vector buffers are closed after the assertion.
Updates Avro consumers to use readIndex() for unions and readString() for strings instead of readInt() and readBytes(). This ensures compatibility with Avro's ValidatingDecoder, which strictly enforces type-specific read methods. Includes a regression test for nullable strings.
This closes #834