This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 66d70858e GH-832: [Avro] Use readString in AvroStringConsumer (#1520)
66d70858e is described below

commit 66d70858e5d9e93816d9e3d237d5b1058c781787
Author: 龙骑士k <[email protected]>
AuthorDate: Tue Nov 24 08:19:46 2026 +0000

    GH-832: [Avro] Use readString in AvroStringConsumer (#1520)
    
    ## 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.
    
    Closes #832.
    
    ## AI Disclosure
    
    Human-led contribution. Grok Bot, working for dragonknightk, assisted
    with this change, including the code, tests, and description. All
    submitted content was reviewed by a human before submission.
    
    Signed-off-by: dragonknightk 
<[email protected]>
---
 .../adapter/avro/consumers/AvroStringConsumer.java | 12 +++---
 .../apache/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 164d595e9..dc77d1b25 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 @@ import org.apache.avro.io.Decoder;
  */
 public class AvroStringConsumer extends BaseAvroConsumer<VarCharVector> {
 
-  private ByteBuffer cacheBuffer;
+  private Utf8 cacheUtf8;
 
   /** Instantiate a AvroStringConsumer. */
   public AvroStringConsumer(VarCharVector vector) {
@@ -36,9 +36,9 @@ public class AvroStringConsumer extends 
BaseAvroConsumer<VarCharVector> {
 
   @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 899ea4030..29718866e 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 @@ package org.apache.arrow.adapter.avro;
 
 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.Arrays;
 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.arrow.vector.complex.StructVector;
 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 class AvroToArrowTest extends AvroTestBase {
     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<String> 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");

Reply via email to