Skip to content

Commit 3815f20

Browse files
committed
GH-812: Fix appending to an empty UnionVector
1 parent 91b4a2c commit 3815f20

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ public ValueVector visit(UnionVector deltaVector, Void value) {
601601
UnionVector targetUnionVector = (UnionVector) targetVector;
602602
int newValueCount = targetVector.getValueCount() + deltaVector.getValueCount();
603603

604-
// make sure there is enough capacity
605-
while (targetUnionVector.getValueCapacity() < newValueCount) {
604+
// Child vectors are created and expanded below; an empty union has zero value capacity.
605+
while (targetUnionVector.getTypeBuffer().capacity() / UnionVector.TYPE_WIDTH < newValueCount) {
606606
targetUnionVector.reAlloc();
607607
}
608608

vector/src/test/java/org/apache/arrow/vector/util/TestVectorAppender.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,26 @@ public void testAppendEmptyStructVector() {
804804
}
805805
}
806806

807+
@Test
808+
public void testAppendToEmptyUnionVector() {
809+
try (BufferAllocator limitedAllocator = allocator.newChildAllocator("empty union", 0, 1048576);
810+
UnionVector target = UnionVector.empty("target", limitedAllocator);
811+
UnionVector delta = UnionVector.empty("delta", limitedAllocator)) {
812+
delta.setType(0, Types.MinorType.FLOAT4);
813+
delta.setType(1, Types.MinorType.FLOAT4);
814+
Float4Vector values = delta.getFloat4Vector();
815+
values.allocateNew();
816+
ValueVectorDataPopulator.setVector(values, 1f, 2f);
817+
delta.setValueCount(2);
818+
819+
delta.accept(new VectorAppender(target), null);
820+
821+
assertEquals(2, target.getValueCount());
822+
assertEquals(1f, target.getObject(0));
823+
assertEquals(2f, target.getObject(1));
824+
}
825+
}
826+
807827
@Test
808828
public void testAppendUnionVector() {
809829
final int length1 = 10;

0 commit comments

Comments
 (0)