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 f10e2196d GH-390: Refresh vector lookup when synchronizing the schema 
(#1389)
f10e2196d is described below

commit f10e2196dbfc8970bf1775fc4fa515ffab638674
Author: Tiefang Hu <[email protected]>
AuthorDate: Sun Nov 15 13:50:57 2026 +0800

    GH-390: Refresh vector lookup when synchronizing the schema (#1389)
    
    ## What's Changed
    
    After a child type is promoted, `syncSchema()` updates the schema but
    leaves the vector lookup keyed by the old `Field` values. Looking up a
    vector using the new schema returns null, and `JsonFileWriter` then
    throws a NullPointerException. Refresh the Field-keyed lookup in the
    schema-change branch, preserving vector identity and ordering.
    
    Add regressions using two rows of promoted nested-list data: direct
    lookup verifies the new Field, removal of the stale key, unchanged
    values and row count, and a second synchronization that makes no change.
    A real JSON write/read round trip verifies the schema and both nested
    row values. The unchanged implementation fails the lookup assertion and
    throws in the writer.
    
    Validation on JDK 21:
    - `mvn -B -ntp -Dmaven.gitcommitid.nativegit=true -pl vector -am verify`
    passed, including dependency modules.
    - The vector module ran 1193 cases in each of its two configured
    allocator executions, with 4 skipped per execution and no failures or
    errors. All 11 `TestVectorSchemaRoot` cases passed in each execution
    without skips.
    - Spotless and Checkstyle passed. The native Git option handles the
    existing Git plugin's local worktree compatibility; no build
    configuration was changed.
    
    Closes #390.
    
    Maintainer note: the CI workflows are `action_required`, and my account
    cannot add repository labels. Please add `bug-fix` and approve the runs
    when appropriate. Remote CI and maintainer review remain pending.
    
    <sub>AI-generated PR description. Investigation, implementation and
    automated review assisted by Codex and DeepSeek.</sub>
---
 .../org/apache/arrow/vector/VectorSchemaRoot.java  |  6 ++
 .../apache/arrow/vector/TestVectorSchemaRoot.java  | 81 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)

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 4c1fbf761..26a64ed0d 100644
--- a/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java
+++ b/vector/src/main/java/org/apache/arrow/vector/VectorSchemaRoot.java
@@ -312,6 +312,12 @@ public class VectorSchemaRoot implements AutoCloseable {
         
this.fieldVectors.stream().map(ValueVector::getField).collect(Collectors.toList());
     if (!oldFields.equals(newFields)) {
       this.schema = new Schema(newFields);
+      // Field equality is structural, and promotion (e.g. a promoted 
Struct/List child)
+      // produces new Field instances, so the existing keys would no longer 
match.
+      fieldVectorsMap.clear();
+      for (int i = 0; i < newFields.size(); i++) {
+        fieldVectorsMap.put(newFields.get(i), fieldVectors.get(i));
+      }
       return true;
     }
     return false;
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 bd3113f8b..78a4e92d5 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestVectorSchemaRoot.java
@@ -19,9 +19,13 @@ package org.apache.arrow.vector;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -30,6 +34,8 @@ import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
 import org.apache.arrow.vector.complex.ListVector;
 import org.apache.arrow.vector.complex.impl.UnionListWriter;
+import org.apache.arrow.vector.ipc.JsonFileReader;
+import org.apache.arrow.vector.ipc.JsonFileWriter;
 import org.apache.arrow.vector.types.pojo.ArrowType;
 import org.apache.arrow.vector.types.pojo.Field;
 import org.apache.arrow.vector.types.pojo.FieldType;
@@ -37,9 +43,12 @@ import org.apache.arrow.vector.types.pojo.Schema;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 public class TestVectorSchemaRoot {
 
+  @TempDir public Path tempDir;
+
   private BufferAllocator allocator;
 
   @BeforeEach
@@ -344,4 +353,76 @@ public class TestVectorSchemaRoot {
       assertFalse(schemaRoot.syncSchema());
     }
   }
+
+  @Test
+  public void testSchemaSyncRefreshesFieldVectorLookup() {
+    try (VectorSchemaRoot root = createBatch()) {
+      final FieldVector listVector = root.getVector("listCol");
+      // createBatch() writes two list rows but leaves the root row count 
unset.
+      root.setRowCount(listVector.getValueCount());
+      final int rowCount = root.getRowCount();
+      assertEquals(2, rowCount);
+      final Field staleField = root.getSchema().getFields().get(0);
+      final Object oldValue = listVector.getObject(0);
+
+      assertTrue(root.syncSchema());
+
+      // Promotion produced a new Field instance for the same, unchanged 
vector.
+      final Field syncedField = root.getSchema().getFields().get(0);
+      assertNotEquals(staleField, syncedField);
+
+      // Looking up by the synced Field must resolve to the very same vector 
instance.
+      assertNotNull(root.getVector(syncedField));
+      assertSame(listVector, root.getVector(syncedField));
+      assertSame(listVector, root.getVector("listCol"));
+
+      // The stale structural key is gone because the Field itself changed.
+      assertNull(root.getVector(staleField));
+
+      // Syncing the schema leaves the row count and the vector data untouched.
+      assertEquals(rowCount, root.getRowCount());
+      assertEquals(oldValue, listVector.getObject(0));
+
+      // A second sync is a no-op and the refreshed lookup stays correct.
+      assertFalse(root.syncSchema());
+      assertSame(listVector, root.getVector(syncedField));
+    }
+  }
+
+  @Test
+  public void testJsonWriteAndReadAfterSchemaSync() throws Exception {
+    final Path jsonFile = tempDir.resolve("promoted-schema.json");
+    try (VectorSchemaRoot root = createBatch()) {
+      final FieldVector expectedVector = root.getVector("listCol");
+      // createBatch() writes two list rows but leaves the root row count 
unset.
+      root.setRowCount(expectedVector.getValueCount());
+      final int rowCount = root.getRowCount();
+      assertEquals(2, rowCount);
+      assertTrue(root.syncSchema());
+      final Schema syncedSchema = root.getSchema();
+
+      // Writing resolves every vector through 
VectorSchemaRoot#getVector(Field), so a
+      // stale Field-keyed lookup makes this write fail.
+      try (JsonFileWriter writer =
+          new JsonFileWriter(jsonFile.toFile(), 
JsonFileWriter.config().pretty(true))) {
+        writer.start(syncedSchema, null);
+        writer.write(root);
+      }
+
+      try (BufferAllocator readerAllocator =
+              allocator.newChildAllocator("json-reader", 0, Long.MAX_VALUE);
+          JsonFileReader reader = new JsonFileReader(jsonFile.toFile(), 
readerAllocator)) {
+        assertEquals(syncedSchema, reader.start());
+        try (VectorSchemaRoot readRoot = reader.read()) {
+          assertNotNull(readRoot);
+          assertEquals(rowCount, readRoot.getRowCount());
+          final FieldVector actualVector = readRoot.getVector("listCol");
+          // The JSON schema round-trips exactly, so the nested rows compare 
structurally.
+          assertEquals(expectedVector.getObject(0), actualVector.getObject(0));
+          assertEquals(expectedVector.getObject(1), actualVector.getObject(1));
+        }
+        assertNull(reader.read());
+      }
+    }
+  }
 }

Reply via email to