Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ cmake_install.cmake
dependency-reduced-pom.xml
install_manifest.txt
target/
.claude/
CLAUDE.md
*.class
Comment on lines +35 to +37

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.

Original file line number Diff line number Diff line change
Expand Up @@ -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);

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;


break;
case ARRAY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public AvroNullableConsumer(Consumer<T> delegate, int nullIndex) {

@Override
public void consume(Decoder decoder) throws IOException {
int typeIndex = decoder.readInt();
int typeIndex = decoder.readIndex();
if (typeIndex == nullIndex) {
decoder.readNull();
delegate.addNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
package org.apache.arrow.adapter.avro.consumers;

import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.arrow.vector.VarCharVector;
import org.apache.avro.io.Decoder;
import org.apache.avro.util.Utf8;

/**
* Consumer which consume string type values from avro decoder. Write the data to {@link
* VarCharVector}.
*/
public class AvroStringConsumer extends BaseAvroConsumer<VarCharVector> {

private ByteBuffer cacheBuffer;
private Utf8 cachedUtf8;

/** Instantiate a AvroStringConsumer. */
public AvroStringConsumer(VarCharVector vector) {
Expand All @@ -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());
Comment on lines +39 to +40

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.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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.


ensureInnerVectorCapacity(currentIndex + 1, fieldIndex);
Consumer delegate = delegates[fieldIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -33,7 +35,13 @@
import org.apache.arrow.vector.complex.StructVector;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.junit.jupiter.api.Test;

public class AvroToArrowTest extends AvroTestBase {
Expand Down Expand Up @@ -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());
}
Comment on lines +486 to +507

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.

}
Loading