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 4370fe941 GH-200: Test a second transfer for every vector kind (#1381)
4370fe941 is described below

commit 4370fe941b4839bb315c62ad013b5d1a1c83a55f
Author: Sharang Gupta <[email protected]>
AuthorDate: Tue Nov 17 04:06:19 2026 +0000

    GH-200: Test a second transfer for every vector kind (#1381)
    
    ## What's Changed
    
    #1419 has since fixed the `DenseUnionVector` transfer that #200
    reported, so after the rebase on `main` this PR only adds tests for it.
    
    A parameterized test in `TestSplitAndTransfer` covers 18 vector kinds.
    For each, it transfers a populated vector (or splits and transfers it),
    transfers the result again, and compares it with the original using
    `VectorEqualsVisitor`. `TestDenseUnionVector` adds a transfer-twice test
    and a split-and-transfer test for a dense union with declared type ids 5
    and 10.
    
    Closes #200.
    
    ---------
    
    Co-authored-by: JB Onofré <[email protected]>
---
 .../apache/arrow/vector/TestDenseUnionVector.java  |  63 ++++++
 .../apache/arrow/vector/TestSplitAndTransfer.java  | 237 +++++++++++++++++++++
 2 files changed, 300 insertions(+)

diff --git 
a/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java 
b/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java
index b92708265..b2d8977cf 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestDenseUnionVector.java
@@ -200,6 +200,48 @@ public class TestDenseUnionVector {
     }
   }
 
+  @Test
+  public void testTransferWithDeclaredTypeIds() {
+    try (DenseUnionVector srcVector = newVectorWithTypeIds5And10()) {
+      Field srcField = srcVector.getField();
+      TransferPair pair = srcVector.getTransferPair(allocator);
+      try (DenseUnionVector destVector = (DenseUnionVector) pair.getTo()) {
+        pair.transfer();
+        destVector.setValueCount(3);
+
+        assertEquals(srcField, destVector.getField());
+        assertEquals(5, destVector.getObject(0));
+        assertEquals(true, destVector.getObject(1));
+        assertEquals(7, destVector.getObject(2));
+
+        TransferPair secondPair = destVector.getTransferPair(allocator);
+        try (DenseUnionVector secondDestVector = (DenseUnionVector) 
secondPair.getTo()) {
+          secondPair.transfer();
+
+          assertEquals(srcField, secondDestVector.getField());
+          assertEquals(3, secondDestVector.getValueCount());
+          assertEquals(5, secondDestVector.getObject(0));
+          assertEquals(true, secondDestVector.getObject(1));
+          assertEquals(7, secondDestVector.getObject(2));
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testSplitAndTransferWithDeclaredTypeIds() {
+    try (DenseUnionVector srcVector = newVectorWithTypeIds5And10()) {
+      TransferPair pair = srcVector.getTransferPair(allocator);
+      try (DenseUnionVector destVector = (DenseUnionVector) pair.getTo()) {
+        pair.splitAndTransfer(1, 2);
+
+        assertEquals(2, destVector.getValueCount());
+        assertEquals(true, destVector.getObject(0));
+        assertEquals(7, destVector.getObject(1));
+      }
+    }
+  }
+
   @Test
   public void testSplitAndTransfer() throws Exception {
     try (DenseUnionVector sourceVector =
@@ -1097,4 +1139,25 @@ public class TestDenseUnionVector {
     holder.value = value;
     return holder;
   }
+
+  /** Returns a dense union with declared type ids 5 (int) and 10 (bit), as 
IPC can produce. */
+  private DenseUnionVector newVectorWithTypeIds5And10() {
+    Field field =
+        new Field(
+            "u",
+            FieldType.nullable(new ArrowType.Union(UnionMode.Dense, new int[] 
{5, 10})),
+            List.of(
+                Field.nullable("i", MinorType.INT.getType()),
+                Field.nullable("b", MinorType.BIT.getType())));
+    DenseUnionVector vector = (DenseUnionVector) field.createVector(allocator);
+    vector.allocateNew();
+    vector.setTypeId(0, (byte) 5);
+    vector.setSafe(0, newIntHolder(5));
+    vector.setTypeId(1, (byte) 10);
+    vector.setSafe(1, newBitHolder(true));
+    vector.setTypeId(2, (byte) 5);
+    vector.setSafe(2, newIntHolder(7));
+    vector.setValueCount(3);
+    return vector;
+  }
 }
diff --git 
a/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java 
b/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
index 52df39d38..93a03d457 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestSplitAndTransfer.java
@@ -17,6 +17,7 @@
 package org.apache.arrow.vector;
 
 import static java.util.Arrays.asList;
+import static 
org.apache.arrow.vector.testing.ValueVectorDataPopulator.setVector;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -26,17 +27,28 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Stream;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.compare.VectorEqualsVisitor;
 import org.apache.arrow.vector.complex.DenseUnionVector;
 import org.apache.arrow.vector.complex.FixedSizeListVector;
+import org.apache.arrow.vector.complex.LargeListVector;
 import org.apache.arrow.vector.complex.LargeListViewVector;
 import org.apache.arrow.vector.complex.ListVector;
+import org.apache.arrow.vector.complex.ListViewVector;
 import org.apache.arrow.vector.complex.MapVector;
 import org.apache.arrow.vector.complex.StructVector;
 import org.apache.arrow.vector.complex.UnionVector;
+import org.apache.arrow.vector.complex.impl.UnionMapWriter;
+import org.apache.arrow.vector.holders.NullableBitHolder;
+import org.apache.arrow.vector.holders.NullableIntHolder;
+import org.apache.arrow.vector.types.Types.MinorType;
 import org.apache.arrow.vector.types.pojo.ArrowType;
 import org.apache.arrow.vector.types.pojo.ArrowType.Struct;
 import org.apache.arrow.vector.types.pojo.Field;
@@ -44,9 +56,15 @@ import org.apache.arrow.vector.types.pojo.FieldType;
 import org.apache.arrow.vector.util.TransferPair;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Named;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 public class TestSplitAndTransfer {
+  private static final int POPULATED_VALUE_COUNT = 3;
+
   private BufferAllocator allocator;
 
   @BeforeEach
@@ -993,4 +1011,223 @@ public class TestSplitAndTransfer {
       newMapVector.clear();
     }
   }
+
+  /**
+   * Vectors of each kind, populated with {@link #POPULATED_VALUE_COUNT} 
values. A transfer pair
+   * that misses part of the vector's state only shows when its target is 
transferred again, as with
+   * the type ids of {@link DenseUnionVector} (GH-200).
+   */
+  static Stream<Arguments> populatedVectorKinds() {
+    return Stream.of(
+        vectorKind(
+            "IntVector",
+            vectorAllocator -> {
+              IntVector vector = new IntVector("vector", vectorAllocator);
+              setVector(vector, 1, null, 3);
+              return vector;
+            }),
+        vectorKind(
+            "BitVector",
+            vectorAllocator -> {
+              BitVector vector = new BitVector("vector", vectorAllocator);
+              setVector(vector, 1, null, 0);
+              return vector;
+            }),
+        vectorKind(
+            "VarCharVector",
+            vectorAllocator -> {
+              VarCharVector vector = new VarCharVector("vector", 
vectorAllocator);
+              setVector(vector, "a", null, "c");
+              return vector;
+            }),
+        vectorKind(
+            "LargeVarCharVector",
+            vectorAllocator -> {
+              LargeVarCharVector vector = new LargeVarCharVector("vector", 
vectorAllocator);
+              setVector(vector, "a", null, "c");
+              return vector;
+            }),
+        vectorKind(
+            "ViewVarCharVector",
+            vectorAllocator -> {
+              ViewVarCharVector vector = new ViewVarCharVector("vector", 
vectorAllocator);
+              setVector(vector, "a", null, "c");
+              return vector;
+            }),
+        vectorKind(
+            "VarBinaryVector",
+            vectorAllocator -> {
+              VarBinaryVector vector = new VarBinaryVector("vector", 
vectorAllocator);
+              setVector(vector, new byte[] {1, 2}, null, new byte[] {3});
+              return vector;
+            }),
+        vectorKind(
+            "LargeVarBinaryVector",
+            vectorAllocator -> {
+              LargeVarBinaryVector vector = new LargeVarBinaryVector("vector", 
vectorAllocator);
+              setVector(vector, new byte[] {1, 2}, null, new byte[] {3});
+              return vector;
+            }),
+        vectorKind(
+            "ViewVarBinaryVector",
+            vectorAllocator -> {
+              ViewVarBinaryVector vector = new ViewVarBinaryVector("vector", 
vectorAllocator);
+              setVector(vector, new byte[] {1, 2}, null, new byte[] {3});
+              return vector;
+            }),
+        vectorKind(
+            "FixedSizeBinaryVector",
+            vectorAllocator -> {
+              FixedSizeBinaryVector vector =
+                  new FixedSizeBinaryVector("vector", vectorAllocator, 2);
+              setVector(vector, new byte[] {1, 2}, null, new byte[] {5, 6});
+              return vector;
+            }),
+        vectorKind(
+            "ListVector",
+            vectorAllocator -> {
+              ListVector vector = ListVector.empty("vector", vectorAllocator);
+              setVector(vector, asList(1, 2), null, asList(3));
+              return vector;
+            }),
+        vectorKind(
+            "LargeListVector",
+            vectorAllocator -> {
+              LargeListVector vector = LargeListVector.empty("vector", 
vectorAllocator);
+              setVector(vector, asList(1, 2), null, asList(3));
+              return vector;
+            }),
+        vectorKind(
+            "FixedSizeListVector",
+            vectorAllocator -> {
+              FixedSizeListVector vector = FixedSizeListVector.empty("vector", 
2, vectorAllocator);
+              setVector(vector, asList(1, 2), null, asList(3, 4));
+              return vector;
+            }),
+        vectorKind(
+            "ListViewVector",
+            vectorAllocator -> {
+              ListViewVector vector = ListViewVector.empty("vector", 
vectorAllocator);
+              setVector(vector, asList(1, 2), asList(), asList(3));
+              return vector;
+            }),
+        vectorKind(
+            "LargeListViewVector",
+            vectorAllocator -> {
+              LargeListViewVector vector = LargeListViewVector.empty("vector", 
vectorAllocator);
+              setVector(vector, asList(1, 2), asList(), asList(3));
+              return vector;
+            }),
+        vectorKind(
+            "MapVector",
+            vectorAllocator -> {
+              MapVector vector = MapVector.empty("vector", vectorAllocator, 
false);
+              UnionMapWriter writer = vector.getWriter();
+              writer.allocate();
+              for (int i = 0; i < POPULATED_VALUE_COUNT; i++) {
+                writer.setPosition(i);
+                writer.startMap();
+                writer.startEntry();
+                writer.key().integer().writeInt(i);
+                writer.value().integer().writeInt(i * 10);
+                writer.endEntry();
+                writer.endMap();
+              }
+              writer.setValueCount(POPULATED_VALUE_COUNT);
+              return vector;
+            }),
+        vectorKind(
+            "StructVector",
+            vectorAllocator -> {
+              StructVector vector = StructVector.empty("vector", 
vectorAllocator);
+              Map<String, List<Integer>> children = new LinkedHashMap<>();
+              children.put("a", asList(1, null, 3));
+              children.put("b", asList(4, 5, 6));
+              setVector(vector, children);
+              return vector;
+            }),
+        vectorKind(
+            "UnionVector",
+            vectorAllocator -> {
+              UnionVector vector = new UnionVector("vector", vectorAllocator, 
null, null);
+              vector.allocateNew();
+              vector.setType(0, MinorType.INT);
+              vector.setSafe(0, intHolder(5));
+              vector.setType(1, MinorType.BIT);
+              vector.setSafe(1, bitHolder(true));
+              vector.setType(2, MinorType.INT);
+              vector.setSafe(2, intHolder(7));
+              vector.setValueCount(POPULATED_VALUE_COUNT);
+              return vector;
+            }),
+        vectorKind(
+            "DenseUnionVector",
+            vectorAllocator -> {
+              DenseUnionVector vector = new DenseUnionVector("vector", 
vectorAllocator, null, null);
+              vector.allocateNew();
+              byte intTypeId =
+                  vector.registerNewTypeId(Field.nullable("", 
MinorType.INT.getType()));
+              byte bitTypeId =
+                  vector.registerNewTypeId(Field.nullable("", 
MinorType.BIT.getType()));
+              vector.setTypeId(0, intTypeId);
+              vector.setSafe(0, intHolder(5));
+              vector.setTypeId(1, bitTypeId);
+              vector.setSafe(1, bitHolder(true));
+              vector.setTypeId(2, intTypeId);
+              vector.setSafe(2, intHolder(7));
+              vector.setValueCount(POPULATED_VALUE_COUNT);
+              return vector;
+            }));
+  }
+
+  @ParameterizedTest
+  @MethodSource("populatedVectorKinds")
+  public void testTransferOfTransferredVector(Function<BufferAllocator, 
ValueVector> populated) {
+    assertTransferredTwiceKeepsValues(populated, TransferPair::transfer);
+  }
+
+  @ParameterizedTest
+  @MethodSource("populatedVectorKinds")
+  public void testTransferOfSplitAndTransferredVector(
+      Function<BufferAllocator, ValueVector> populated) {
+    assertTransferredTwiceKeepsValues(
+        populated, pair -> pair.splitAndTransfer(0, POPULATED_VALUE_COUNT));
+  }
+
+  private void assertTransferredTwiceKeepsValues(
+      Function<BufferAllocator, ValueVector> populated, Consumer<TransferPair> 
firstTransfer) {
+    try (ValueVector expected = populated.apply(allocator);
+        ValueVector source = populated.apply(allocator)) {
+      TransferPair first = source.getTransferPair(allocator);
+      try (ValueVector intermediate = first.getTo()) {
+        firstTransfer.accept(first);
+        TransferPair second = intermediate.getTransferPair(allocator);
+        try (ValueVector target = second.getTo()) {
+          second.transfer();
+          assertTrue(
+              VectorEqualsVisitor.vectorEquals(expected, target),
+              () -> "expected " + expected + " but was " + target);
+        }
+      }
+    }
+  }
+
+  private static Arguments vectorKind(
+      String name, Function<BufferAllocator, ValueVector> populate) {
+    return Arguments.of(Named.of(name, populate));
+  }
+
+  private static NullableIntHolder intHolder(int value) {
+    NullableIntHolder holder = new NullableIntHolder();
+    holder.isSet = 1;
+    holder.value = value;
+    return holder;
+  }
+
+  private static NullableBitHolder bitHolder(boolean value) {
+    NullableBitHolder holder = new NullableBitHolder();
+    holder.isSet = 1;
+    holder.value = value ? 1 : 0;
+    return holder;
+  }
 }

Reply via email to