From ab962368db9f92bd3eaed78423f1f04fb0fe31d8 Mon Sep 17 00:00:00 2001 From: dragonknightk <37928252+dragonknightk@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:32:40 +0800 Subject: [PATCH] [Avro] Use readString in AvroStringConsumer ## What's Changed AvroStringConsumer read string fields with Decoder.readBytes. That works with BinaryDecoder, because the binary encoding for string and bytes is the same. ResolvingDecoder checks the reader schema and throws AvroTypeException: "Attempt to process a bytes when a string was expected." ValidatingDecoder throws the same error, and this change fixes both. The consumer now calls Decoder.readString and copies the Utf8 bytes into the VarCharVector, reusing the Utf8 instance the same way the old code reused a ByteBuffer. The new test reads "v1", an empty string and a non-ASCII string through a ResolvingDecoder and checks the vector holds the same values; the empty string after a longer value and the multi-byte characters check that only the valid bytes of the reused Utf8 are copied. Closes issue 832. Signed-off-by: dragonknightk <37928252+dragonknightk@users.noreply.github.com> --- .../avro/consumers/AvroStringConsumer.java | 12 +++--- .../arrow/adapter/avro/AvroToArrowTest.java | 43 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java b/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java index 164d595e9c..dc77d1b258 100644 --- a/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java +++ b/adapter/avro/src/main/java/org/apache/arrow/adapter/avro/consumers/AvroStringConsumer.java @@ -17,9 +17,9 @@ 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 @@ -27,7 +27,7 @@ */ public class AvroStringConsumer extends BaseAvroConsumer { - private ByteBuffer cacheBuffer; + private Utf8 cacheUtf8; /** Instantiate a AvroStringConsumer. */ public AvroStringConsumer(VarCharVector vector) { @@ -36,9 +36,9 @@ 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()); + // cacheUtf8 starts null and is reused; the Utf8 grows its backing array + // when needed, so only getByteLength() bytes are valid. + cacheUtf8 = decoder.readString(cacheUtf8); + vector.setSafe(currentIndex++, cacheUtf8.getBytes(), 0, cacheUtf8.getByteLength()); } } diff --git a/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java b/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java index 899ea40301..29718866e7 100644 --- a/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java +++ b/adapter/avro/src/test/java/org/apache/arrow/adapter/avro/AvroToArrowTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -25,6 +26,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.arrow.adapter.avro.consumers.AvroStringConsumer; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.VarCharVector; import org.apache.arrow.vector.VectorSchemaRoot; @@ -34,6 +36,11 @@ import org.apache.avro.Schema; import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericRecord; +import org.apache.avro.io.BinaryDecoder; +import org.apache.avro.io.BinaryEncoder; +import org.apache.avro.io.DecoderFactory; +import org.apache.avro.io.EncoderFactory; +import org.apache.avro.io.ResolvingDecoder; import org.junit.jupiter.api.Test; public class AvroToArrowTest extends AvroTestBase { @@ -49,6 +56,42 @@ public void testStringType() throws Exception { checkPrimitiveResult(data, vector); } + /** + * Regression for issue 832. {@code AvroStringConsumer} used {@code Decoder.readBytes}, so a + * {@link ResolvingDecoder} whose reader schema is a string threw {@code AvroTypeException}: + * "Attempt to process a bytes when a string was expected." Values are written with {@code + * Encoder.writeString} and include an empty string and a non-ASCII literal so the reused {@code + * Utf8} buffer is copied with the correct byte length. + */ + @Test + public void testStringTypeWithResolvingDecoder() throws Exception { + Schema schema = getSchema("test_primitive_string.avsc"); + List data = Arrays.asList("v1", "", "你好世界"); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null); + for (String value : data) { + encoder.writeString(value); + } + encoder.flush(); + + DecoderFactory factory = DecoderFactory.get(); + BinaryDecoder binaryDecoder = factory.binaryDecoder(out.toByteArray(), null); + ResolvingDecoder resolvingDecoder = factory.resolvingDecoder(schema, schema, binaryDecoder); + + VarCharVector vector = new VarCharVector("str", config.getAllocator()); + try (AvroStringConsumer consumer = new AvroStringConsumer(vector)) { + vector.allocateNew(); + for (int i = 0; i < data.size(); i++) { + resolvingDecoder.configure(binaryDecoder); + consumer.consume(resolvingDecoder); + resolvingDecoder.drain(); + } + vector.setValueCount(data.size()); + checkPrimitiveResult(data, vector); + } + } + @Test public void testNullableStringType() throws Exception { Schema schema = getSchema("test_nullable_string.avsc");