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 16751039a GH-1386: Zero-pad inline views during split and transfer 
(#1387)
16751039a is described below

commit 16751039a12a0d3f3262ca21cd78c7ba8055f006
Author: Tiefang Hu <[email protected]>
AuthorDate: Tue Nov 17 02:10:01 2026 +0800

    GH-1386: Zero-pad inline views during split and transfer (#1387)
    
    ## What's Changed
    
    `splitAndTransfer` allocates a new view buffer but only writes the
    length and payload of inline values. When the allocator returns non-zero
    memory, the remaining bytes violate the view layout's zero-padding
    requirement. Clear just the unused inline bytes after copying the
    payload; the long-value path is unchanged.
    
    Add deterministic regressions for UTF-8 and binary views using buffers
    prefilled with `0xff`. They cover lengths 0, 1, 11, 12 and 13, nulls, a
    non-zero split offset, source preservation and allocator cleanup. Both
    new tests fail against the unmodified implementation with an unexpected
    `0xff` padding byte.
    
    Validation on JDK 21:
    - `mvn -B -ntp -Dmaven.gitcommitid.nativegit=true -pl vector -am verify`
    passed. The vector module ran 1193 cases in each of its two configured
    allocator executions, with 4 skipped per execution and no failures or
    errors.
    - The full `TestVariableWidthViewVector` class passed all 59 cases in
    each execution, with no skips.
    - Spotless and Checkstyle passed. The native Git option is needed
    locally for the existing Git plugin's worktree support; no build
    configuration was changed.
    
    Closes #1386.
    
    <sub>AI-generated PR description. Source investigation, implementation
    and automated review assisted by Codex and DeepSeek.</sub>
---
 .../arrow/vector/BaseVariableWidthViewVector.java  |   5 +
 .../arrow/vector/TestVariableWidthViewVector.java  | 166 +++++++++++++++++++++
 2 files changed, 171 insertions(+)

diff --git 
a/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java 
b/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java
index d9c48e6a8..2c66deb1a 100644
--- 
a/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java
+++ 
b/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java
@@ -904,6 +904,11 @@ public abstract class BaseVariableWidthViewVector extends 
BaseValueVector
         readPosition += LENGTH_WIDTH;
         // set data by copying the required portion from the source buffer
         target.viewBuffer.setBytes(writePosition, viewBuffer, readPosition, 
stringLength);
+        // zero the unused bytes of the inline view: the target view buffer is 
freshly allocated
+        // and the format requires the bytes after the value to be zero padded.
+        if (stringLength < INLINE_SIZE) {
+          target.viewBuffer.setZero(writePosition + stringLength, INLINE_SIZE 
- stringLength);
+        }
       } else {
         // handle non-inline buffer
         final int readBufIndex =
diff --git 
a/vector/src/test/java/org/apache/arrow/vector/TestVariableWidthViewVector.java 
b/vector/src/test/java/org/apache/arrow/vector/TestVariableWidthViewVector.java
index de786f789..d84290161 100644
--- 
a/vector/src/test/java/org/apache/arrow/vector/TestVariableWidthViewVector.java
+++ 
b/vector/src/test/java/org/apache/arrow/vector/TestVariableWidthViewVector.java
@@ -89,6 +89,10 @@ public class TestVariableWidthViewVector {
   private static final byte[] STR7 = 
"K01234567891234567K".getBytes(StandardCharsets.UTF_8);
   // long string (length > 12)
   private static final byte[] STR8 = 
"M012345678912345678M".getBytes(StandardCharsets.UTF_8);
+  // short value (length <= 12) used to verify the zero padding of inline views
+  private static final byte[] PAD1 = "a".getBytes(StandardCharsets.UTF_8);
+  // short value (length <= 12) used to verify the zero padding of inline views
+  private static final byte[] PAD11 = 
"0123456789a".getBytes(StandardCharsets.UTF_8);
   private static final String EMPTY_SCHEMA_PATH = "";
 
   private BufferAllocator allocator;
@@ -2939,4 +2943,166 @@ public class TestVariableWidthViewVector {
       assertTrue(e.getMessage().contains("(offset 1024, length 64) is outside 
data buffer 0"));
     }
   }
+
+  /**
+   * Builds the source values used by the zero padding tests: a sentinel value 
that stays outside
+   * the split range followed by an empty value, a 1 byte, an 11 byte and a 12 
byte value (all
+   * inline views), a 13 byte value (stored in a data buffer) and a null value.
+   */
+  private static byte[][] zeroPaddingTestData() {
+    return new byte[][] {STR3, new byte[0], PAD1, PAD11, STR1, STR2, null};
+  }
+
+  /**
+   * ARROW-1386: the view format documented in {@link 
BaseVariableWidthViewVector} requires the
+   * bytes after an inline value to be zero padded. This helper checks the 
padding of every view
+   * transferred by {@link BaseVariableWidthViewVector#splitAndTransferTo(int, 
int,
+   * BaseVariableWidthViewVector)}, and verifies that the split keeps the 
value count, the null
+   * count and the source vector intact. Callers pass vectors whose allocator 
hands out buffers
+   * pre-filled with 0xff, so a missing write is reported instead of being 
hidden by memory that
+   * happens to be zeroed.
+   */
+  private void testSplitAndTransferZeroPaddingHelper(
+      BaseVariableWidthViewVector targetVector,
+      BaseVariableWidthViewVector sourceVector,
+      int startIndex,
+      int length,
+      byte[][] data) {
+    final int elementSize = BaseVariableWidthViewVector.ELEMENT_SIZE;
+    final int lengthWidth = BaseVariableWidthViewVector.LENGTH_WIDTH;
+    final int prefixWidth = BaseVariableWidthViewVector.PREFIX_WIDTH;
+    final int inlineSize = BaseVariableWidthViewVector.INLINE_SIZE;
+
+    sourceVector.allocateNew(1024 * 10, 1024);
+
+    for (int i = 0; i < data.length; i++) {
+      if (data[i] == null) {
+        sourceVector.setNull(i);
+      } else {
+        sourceVector.set(i, data[i]);
+      }
+    }
+    sourceVector.setValueCount(data.length);
+
+    final ArrowBuf sourceViewBuffer = sourceVector.getDataBuffer();
+    final int sourceViewLength = (int) sourceViewBuffer.capacity();
+    final byte[] sourceViewBefore = new byte[sourceViewLength];
+    sourceViewBuffer.getBytes(0, sourceViewBefore, 0, sourceViewLength);
+    final int sourceViewRefCnt = sourceViewBuffer.refCnt();
+
+    sourceVector.splitAndTransferTo(startIndex, length, targetVector);
+
+    assertEquals(length, targetVector.getValueCount());
+    // the view buffer is allocated for the target vector, it is not shared 
with the source
+    assertEquals(sourceViewRefCnt, sourceVector.getDataBuffer().refCnt());
+    final ArrowBuf targetViewBuffer = targetVector.getDataBuffer();
+    assertTrue(targetViewBuffer.capacity() >= (long) length * elementSize);
+
+    final byte[] element = new byte[elementSize];
+    int expectedNullCount = 0;
+    for (int i = startIndex; i < startIndex + length; i++) {
+      final int targetIndex = i - startIndex;
+      final int offset = targetIndex * elementSize;
+      final byte[] expected = data[i];
+
+      if (expected == null) {
+        // the view content of a null value is unspecified, only the validity 
matters
+        expectedNullCount++;
+        assertTrue(targetVector.isNull(targetIndex));
+        continue;
+      }
+
+      assertFalse(targetVector.isNull(targetIndex));
+      assertArrayEquals(expected, targetVector.get(targetIndex));
+      assertEquals(expected.length, targetViewBuffer.getInt(offset));
+      targetViewBuffer.getBytes(offset, element, 0, elementSize);
+
+      if (expected.length <= inlineSize) {
+        assertArrayEquals(
+            expected, Arrays.copyOfRange(element, lengthWidth, lengthWidth + 
expected.length));
+        // the bytes following an inline value must be zero padded
+        for (int b = lengthWidth + expected.length; b < elementSize; b++) {
+          assertEquals(0, element[b], "inline view byte " + b + " of value " + 
i);
+        }
+      } else {
+        // long views keep the first bytes of the value as a prefix; the 
buffer index and the
+        // offset are an implementation detail of the target and are covered 
by the get() above
+        assertArrayEquals(
+            Arrays.copyOfRange(expected, 0, prefixWidth),
+            Arrays.copyOfRange(element, lengthWidth, lengthWidth + 
prefixWidth));
+      }
+    }
+    assertEquals(expectedNullCount, targetVector.getNullCount());
+
+    // the split does not modify the source vector
+    assertEquals(data.length, sourceVector.getValueCount());
+    final byte[] sourceViewAfter = new byte[sourceViewLength];
+    sourceVector.getDataBuffer().getBytes(0, sourceViewAfter, 0, 
sourceViewLength);
+    assertArrayEquals(sourceViewBefore, sourceViewAfter);
+    for (int i = 0; i < data.length; i++) {
+      if (data[i] == null) {
+        assertTrue(sourceVector.isNull(i));
+      } else {
+        assertArrayEquals(data[i], sourceVector.get(i));
+      }
+    }
+  }
+
+  /**
+   * ARROW-1386: split and transfer must zero pad the inline views of the 
target vector even when
+   * the allocated target view buffer contains stale bytes.
+   */
+  @Test
+  public void testSplitAndTransferZeroPadsInlineViews() {
+    final byte[][] data = zeroPaddingTestData();
+    final int startIndex = 1;
+    final int length = 6;
+
+    try (final DirtyingRootAllocator dirtyAllocator = new 
DirtyingRootAllocator()) {
+      try (final ViewVarCharVector targetVector =
+              newViewVarCharVector("split-target", dirtyAllocator);
+          final ViewVarCharVector sourceVector =
+              newViewVarCharVector(EMPTY_SCHEMA_PATH, dirtyAllocator)) {
+        testSplitAndTransferZeroPaddingHelper(targetVector, sourceVector, 
startIndex, length, data);
+      }
+      assertEquals(0, dirtyAllocator.getAllocatedMemory());
+    }
+  }
+
+  /** ARROW-1386: same as {@link #testSplitAndTransferZeroPadsInlineViews()} 
for binary views. */
+  @Test
+  public void testSplitAndTransferZeroPadsInlineViewsBinary() {
+    final byte[][] data = zeroPaddingTestData();
+    final int startIndex = 1;
+    final int length = 6;
+
+    try (final DirtyingRootAllocator dirtyAllocator = new 
DirtyingRootAllocator()) {
+      try (final ViewVarBinaryVector targetVector =
+              newViewVarBinaryVector("split-target", dirtyAllocator);
+          final ViewVarBinaryVector sourceVector =
+              newViewVarBinaryVector(EMPTY_SCHEMA_PATH, dirtyAllocator)) {
+        testSplitAndTransferZeroPaddingHelper(targetVector, sourceVector, 
startIndex, length, data);
+      }
+      assertEquals(0, dirtyAllocator.getAllocatedMemory());
+    }
+  }
+
+  /**
+   * Allocator that fills every buffer it hands out with 0xff. Tests using it 
do not depend on the
+   * content of recycled or freshly mapped memory, so bytes the vector code 
fails to write (for
+   * instance a missing inline view padding) are always visible.
+   */
+  private static final class DirtyingRootAllocator extends RootAllocator {
+
+    DirtyingRootAllocator() {
+      super(Integer.MAX_VALUE);
+    }
+
+    @Override
+    public ArrowBuf buffer(final long initialRequestSize) {
+      final ArrowBuf buffer = super.buffer(initialRequestSize);
+      buffer.setOne(0L, buffer.capacity());
+      return buffer;
+    }
+  }
 }

Reply via email to