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

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 284dddd129 GH-37863: [Java] Add typed getters for StructVector (#37916)
284dddd129 is described below

commit 284dddd129fae072ec3e7ae269520b54c8decdb4
Author: James Duong <[email protected]>
AuthorDate: Thu Sep 28 06:22:26 2023 -0700

    GH-37863: [Java] Add typed getters for StructVector (#37916)
    
    ### Rationale for this change
    Add methods for getting child vectors as specific vector subtypes
    for convenience.
    
    ### What changes are included in this PR?
    Add child getters which let the caller specify the target vector type to 
StructVector.
    
    ### Are these changes tested?
    Yes.
    
    ### Are there any user-facing changes?
    No.
    * Closes: #37863
    
    Authored-by: James Duong <[email protected]>
    Signed-off-by: David Li <[email protected]>
---
 .../apache/arrow/vector/complex/NonNullableStructVector.java | 12 ++++++++++++
 .../test/java/org/apache/arrow/vector/TestStructVector.java  |  8 ++++++++
 2 files changed, 20 insertions(+)

diff --git 
a/java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java
 
b/java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java
index 4da2668121..7d724656cd 100644
--- 
a/java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java
+++ 
b/java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java
@@ -374,6 +374,18 @@ public class NonNullableStructVector extends 
AbstractStructVector {
     return getChildByOrdinal(id);
   }
 
+  /**
+   * Gets a child vector by ordinal position and casts to the specified class.
+   */
+  public <V extends ValueVector> V getVectorById(int id, Class<V> clazz) {
+    ValueVector untyped = getVectorById(id);
+    if (clazz.isInstance(untyped)) {
+      return clazz.cast(untyped);
+    }
+    throw new ClassCastException("Id " + id + " had the wrong type. Expected " 
+ clazz.getCanonicalName() +
+        " but was " + untyped.getClass().getCanonicalName());
+  }
+
   @Override
   public void setValueCount(int valueCount) {
     for (final ValueVector v : getChildren()) {
diff --git 
a/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java 
b/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java
index b4c3048000..552d5752f2 100644
--- a/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java
+++ b/java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java
@@ -282,4 +282,12 @@ public class TestStructVector {
     }
   }
 
+  @Test
+  public void testTypedGetters() {
+    try (final StructVector s1 = StructVector.empty("s1", allocator)) {
+      s1.addOrGet("struct_child", FieldType.nullable(MinorType.INT.getType()), 
IntVector.class);
+      assertEquals(IntVector.class, s1.getChild("struct_child", 
IntVector.class).getClass());
+      assertEquals(IntVector.class, s1.getVectorById(0, 
IntVector.class).getClass());
+    }
+  }
 }

Reply via email to