Skip to content

Commit

Permalink
apacheGH-37863: [Java] Add typed getters for StructVector (apache#37916)
Browse files Browse the repository at this point in the history
### 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: apache#37863

Authored-by: James Duong <[email protected]>
Signed-off-by: David Li <[email protected]>
  • Loading branch information
jduo authored and dgreiss committed Feb 17, 2024
1 parent e4438e8 commit df04687
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ public ValueVector getVectorById(int id) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,12 @@ public void testAddChildVectorsWithDuplicatedFieldNamesForConflictPolicyReplace(
}
}

@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());
}
}
}

0 comments on commit df04687

Please sign in to comment.