Skip to content

GH-834: Fix Avro nullable consumer to use readIndex for unions - #1147

Open
Kaustav-Sarkar wants to merge 1 commit into
apache:mainfrom
Kaustav-Sarkar:gh-834
Open

Kaustav-Sarkar wants to merge 1 commit into
apache:mainfrom
Kaustav-Sarkar:gh-834

Conversation

@Kaustav-Sarkar

@Kaustav-Sarkar Kaustav-Sarkar commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

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

@Kaustav-Sarkar

Copy link
Copy Markdown
Contributor Author

Can a maintainer add labels, I do not have permission

@jbonofre jbonofre added the bug-fix PRs that fix a big. label Nov 27, 2026
@apache apache deleted a comment from github-actions Bot Nov 27, 2026
@github-actions github-actions Bot added this to the 20.1.0 milestone Nov 27, 2026
Comment thread .gitignore
Comment on lines +35 to +37
.claude/
CLAUDE.md
*.class

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert these changes to .gitignore as they are unrelated.

Comment on lines +39 to +40
cachedUtf8 = decoder.readString(cachedUtf8);
vector.setSafe(currentIndex++, cachedUtf8.getBytes(), 0, cachedUtf8.getByteLength());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +486 to +507
@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());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PRs that fix a big.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avro adapter: AvroNullableConsumer should use readIndex

2 participants