Skip to content

Commit b2d7a6a

Browse files
committed
GH-1190: Reserve view slots for empty view vector values
1 parent cb24576 commit b2d7a6a

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthViewVector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,8 @@ public final int getTotalValueLengthUpToIndex(int index) {
14051405
}
14061406

14071407
protected final void handleSafe(int index, int dataLength) {
1408-
final long targetCapacity = roundUpToMultipleOf16((long) index * ELEMENT_SIZE + dataLength);
1408+
// The view buffer stores one fixed-width view per value; payload bytes are allocated separately.
1409+
final long targetCapacity = roundUpToMultipleOf16(((long) index + 1) * ELEMENT_SIZE);
14091410
if (viewBuffer.capacity() < targetCapacity) {
14101411
reallocViewBuffer(targetCapacity);
14111412
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,26 @@ public void testSizeOfViewBufferElements() {
540540
}
541541
}
542542

543+
@ParameterizedTest
544+
@MethodSource({"vectorCreatorProvider"})
545+
public void testSetSafeEmptyValueAtViewBufferBoundary(
546+
Function<BufferAllocator, BaseVariableWidthViewVector> vectorCreator) {
547+
try (final BaseVariableWidthViewVector vector = vectorCreator.apply(allocator)) {
548+
final byte[] emptyValue = new byte[0];
549+
vector.allocateNew();
550+
final int valueCapacity = vector.getValueCapacity();
551+
552+
for (int i = 0; i <= valueCapacity; i++) {
553+
vector.setSafe(i, emptyValue);
554+
}
555+
556+
vector.setValueCount(valueCapacity + 1);
557+
assertTrue(vector.getValueCapacity() > valueCapacity);
558+
assertEquals(0, vector.getValueLength(valueCapacity));
559+
assertArrayEquals(emptyValue, vector.get(valueCapacity));
560+
}
561+
}
562+
543563
@Test
544564
public void testNullableVarType1() {
545565

0 commit comments

Comments
 (0)