Skip to content

Commit 284dddd

Browse files
authored
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 <duong.james@gmail.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent c9674bc commit 284dddd

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

java/vector/src/main/java/org/apache/arrow/vector/complex/NonNullableStructVector.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,18 @@ public ValueVector getVectorById(int id) {
374374
return getChildByOrdinal(id);
375375
}
376376

377+
/**
378+
* Gets a child vector by ordinal position and casts to the specified class.
379+
*/
380+
public <V extends ValueVector> V getVectorById(int id, Class<V> clazz) {
381+
ValueVector untyped = getVectorById(id);
382+
if (clazz.isInstance(untyped)) {
383+
return clazz.cast(untyped);
384+
}
385+
throw new ClassCastException("Id " + id + " had the wrong type. Expected " + clazz.getCanonicalName() +
386+
" but was " + untyped.getClass().getCanonicalName());
387+
}
388+
377389
@Override
378390
public void setValueCount(int valueCount) {
379391
for (final ValueVector v : getChildren()) {

java/vector/src/test/java/org/apache/arrow/vector/TestStructVector.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,12 @@ public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyReplace(
282282
}
283283
}
284284

285+
@Test
286+
public void testTypedGetters() {
287+
try (final StructVector s1 = StructVector.empty("s1", allocator)) {
288+
s1.addOrGet("struct_child", FieldType.nullable(MinorType.INT.getType()), IntVector.class);
289+
assertEquals(IntVector.class, s1.getChild("struct_child", IntVector.class).getClass());
290+
assertEquals(IntVector.class, s1.getVectorById(0, IntVector.class).getClass());
291+
}
292+
}
285293
}

0 commit comments

Comments
 (0)