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 6ffb2d045 GH-301: [Vector] Allow adding a vector at the end of 
VectorSchemaRoot (#1013)
6ffb2d045 is described below

commit 6ffb2d0450effccf115203ae31da10708a35dda8
Author: Aleksei Starikov <[email protected]>
AuthorDate: Wed Mar 11 11:23:45 2026 +0100

    GH-301: [Vector] Allow adding a vector at the end of VectorSchemaRoot 
(#1013)
    
    ## What's Changed
    
    Allow adding a vector at the end of VectorSchemaRoot in the
    `VectorSchemaRoot#addVector()` method.
    
    Previously, the precondition `index < fieldVectors.size()` rejected
    `index == fieldVectors.size()`, so appending was impossible. The
    precondition is now `index <= fieldVectors.size()`, and when `index ==
    fieldVectors.size()` the new vector is appended after all existing
    vectors.
    
    The implementation of `VectorSchemaRoot#addVector()` is now aligned with
    
[BaseTable#insertVector()](https://github.com/apache/arrow-java/blob/main/vector/src/main/java/org/apache/arrow/vector/table/BaseTable.java#L156)
    
    The change is backward compatible, as it extends the functionality of
    the `VectorSchemaRoot#addVector()` method.
    
    Closes #301.
---
 .../org/apache/arrow/vector/VectorSchemaRoot.java    | 15 ++++++++++-----
 .../apache/arrow/vector/TestVectorSchemaRoot.java    | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java 
b/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java
index a7cb9ced7..4c1fbf761 100644
--- a/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java
+++ b/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java
@@ -199,13 +199,18 @@ public class VectorSchemaRoot implements AutoCloseable {
    */
   public VectorSchemaRoot addVector(int index, FieldVector vector) {
     Preconditions.checkNotNull(vector);
-    Preconditions.checkArgument(index >= 0 && index < fieldVectors.size());
+    Preconditions.checkArgument(index >= 0 && index <= fieldVectors.size());
     List<FieldVector> newVectors = new ArrayList<>();
-    for (int i = 0; i < fieldVectors.size(); i++) {
-      if (i == index) {
-        newVectors.add(vector);
+    if (index == fieldVectors.size()) {
+      newVectors.addAll(fieldVectors);
+      newVectors.add(vector);
+    } else {
+      for (int i = 0; i < fieldVectors.size(); i++) {
+        if (i == index) {
+          newVectors.add(vector);
+        }
+        newVectors.add(fieldVectors.get(i));
       }
-      newVectors.add(fieldVectors.get(i));
     }
     return new VectorSchemaRoot(newVectors);
   }
diff --git 
a/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java 
b/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java
index c121d9489..bd3113f8b 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java
@@ -171,6 +171,26 @@ public class TestVectorSchemaRoot {
     }
   }
 
+  @Test
+  public void testAddVectorAtEnd() {
+    try (final IntVector intVector1 = new IntVector("intVector1", allocator);
+        final IntVector intVector2 = new IntVector("intVector2", allocator);
+        final IntVector intVector3 = new IntVector("intVector3", allocator); ) 
{
+
+      VectorSchemaRoot original = new 
VectorSchemaRoot(Arrays.asList(intVector1, intVector2));
+      assertEquals(2, original.getFieldVectors().size());
+
+      VectorSchemaRoot newRecordBatch = original.addVector(2, intVector3);
+      assertEquals(3, newRecordBatch.getFieldVectors().size());
+      assertEquals(intVector1, newRecordBatch.getFieldVectors().get(0));
+      assertEquals(intVector2, newRecordBatch.getFieldVectors().get(1));
+      assertEquals(intVector3, newRecordBatch.getFieldVectors().get(2));
+
+      original.close();
+      newRecordBatch.close();
+    }
+  }
+
   @Test
   public void testRemoveVector() {
     try (final IntVector intVector1 = new IntVector("intVector1", allocator);

Reply via email to