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

RyanSkraba pushed a commit to branch branch-1.12
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.12 by this push:
     new 0ef3a556ba AVRO-4268: BytesWritableConverter should respect logical 
length (#3878)
0ef3a556ba is described below

commit 0ef3a556ba8c316fa22fbab621283d9815759332
Author: Harsh Srivastava <[email protected]>
AuthorDate: Sun Jul 19 18:39:59 2026 +0530

    AVRO-4268: BytesWritableConverter should respect logical length (#3878)
    
    BytesWritable.getBytes() returns the backing array, which can be larger
    than the logical length reported by BytesWritable.getLength(). Wrap
    input.getBytes() with the actual length so the Avro bytes value reflects
    the caller's data, not unused backing-array capacity.
    
    Adds convertBytesWritableRespectsLogicalLength to cover the regression.
    
    Signed-off-by: Harsh Srivastava <[email protected]>
---
 .../apache/avro/hadoop/io/AvroDatumConverterFactory.java  |  2 +-
 .../avro/hadoop/io/TestAvroDatumConverterFactory.java     | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git 
a/lang/java/mapred/src/main/java/org/apache/avro/hadoop/io/AvroDatumConverterFactory.java
 
b/lang/java/mapred/src/main/java/org/apache/avro/hadoop/io/AvroDatumConverterFactory.java
index f2e4fcca3d..a9984e08e5 100644
--- 
a/lang/java/mapred/src/main/java/org/apache/avro/hadoop/io/AvroDatumConverterFactory.java
+++ 
b/lang/java/mapred/src/main/java/org/apache/avro/hadoop/io/AvroDatumConverterFactory.java
@@ -199,7 +199,7 @@ public class AvroDatumConverterFactory extends Configured {
     /** {@inheritDoc} */
     @Override
     public ByteBuffer convert(BytesWritable input) {
-      return ByteBuffer.wrap(input.getBytes());
+      return ByteBuffer.wrap(input.getBytes(), 0, input.getLength());
     }
 
     /** {@inheritDoc} */
diff --git 
a/lang/java/mapred/src/test/java/org/apache/avro/hadoop/io/TestAvroDatumConverterFactory.java
 
b/lang/java/mapred/src/test/java/org/apache/avro/hadoop/io/TestAvroDatumConverterFactory.java
index b0d2041c1a..21a1bf361d 100644
--- 
a/lang/java/mapred/src/test/java/org/apache/avro/hadoop/io/TestAvroDatumConverterFactory.java
+++ 
b/lang/java/mapred/src/test/java/org/apache/avro/hadoop/io/TestAvroDatumConverterFactory.java
@@ -89,6 +89,21 @@ public class TestAvroDatumConverterFactory {
     assertEquals(3, bytes.get(2));
   }
 
+  @Test
+  void convertBytesWritableRespectsLogicalLength() {
+    AvroDatumConverter<BytesWritable, ByteBuffer> converter = 
mFactory.create(BytesWritable.class);
+    byte[] backing = new byte[] { 1, 2, 3, 4, 5 };
+    BytesWritable writable = new BytesWritable(backing);
+    writable.setSize(3);
+
+    ByteBuffer bytes = converter.convert(writable);
+
+    assertEquals(3, bytes.remaining());
+    assertEquals(1, bytes.get(0));
+    assertEquals(2, bytes.get(1));
+    assertEquals(3, bytes.get(2));
+  }
+
   @Test
   void convertByteWritable() {
     AvroDatumConverter<ByteWritable, GenericFixed> converter = 
mFactory.create(ByteWritable.class);

Reply via email to