Skip to content

Commit f92585c

Browse files
authored
GH-704: Fix initialization of offset buffer when exporting VarChar vectors through C Data Interface (#705)
## What's Changed This patch fixes the initialization of offset buffers when exporting variable width arrays through Arrow C Data Interface. The original code incorrectly mess up with the member `this.offsetBuffer` while we should actually initialize the newly allocated offsetBuffer. I think the diff itself will be quite self-explanatory. Closes #704 and probably #88 .
1 parent 42a126f commit f92585c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

c/src/test/java/org/apache/arrow/c/RoundtripTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.apache.arrow.memory.BufferAllocator;
3939
import org.apache.arrow.memory.RootAllocator;
4040
import org.apache.arrow.memory.util.hash.ArrowBufHasher;
41+
import org.apache.arrow.vector.BaseLargeVariableWidthVector;
42+
import org.apache.arrow.vector.BaseVariableWidthVector;
4143
import org.apache.arrow.vector.BigIntVector;
4244
import org.apache.arrow.vector.BitVector;
4345
import org.apache.arrow.vector.DateDayVector;
@@ -181,6 +183,13 @@ boolean roundtrip(FieldVector vector, Class<?> clazz) {
181183
clazz.isInstance(imported),
182184
String.format("expected %s but was %s", clazz, imported.getClass()));
183185
result = VectorEqualsVisitor.vectorEquals(vector, imported);
186+
187+
if (imported instanceof BaseVariableWidthVector
188+
|| imported instanceof BaseLargeVariableWidthVector) {
189+
ArrowBuf offsetBuffer = imported.getOffsetBuffer();
190+
assertTrue(offsetBuffer.capacity() > 0);
191+
assertEquals(0, offsetBuffer.getInt(0));
192+
}
184193
}
185194

186195
// Check that the ref counts of the buffers are the same after the roundtrip
@@ -602,6 +611,13 @@ public void testVarCharVector() {
602611
}
603612
}
604613

614+
@Test
615+
public void testEmptyVarCharVector() {
616+
try (final VarCharVector vector = new VarCharVector("v", allocator)) {
617+
assertTrue(roundtrip(vector, VarCharVector.class));
618+
}
619+
}
620+
605621
@Test
606622
public void testLargeVarBinaryVector() {
607623
try (final LargeVarBinaryVector vector = new LargeVarBinaryVector("", allocator)) {
@@ -635,6 +651,13 @@ public void testLargeVarCharVector() {
635651
}
636652
}
637653

654+
@Test
655+
public void testEmptyLargeVarCharVector() {
656+
try (final LargeVarCharVector vector = new LargeVarCharVector("v", allocator)) {
657+
assertTrue(roundtrip(vector, LargeVarCharVector.class));
658+
}
659+
}
660+
638661
@Test
639662
public void testListVector() {
640663
try (final ListVector vector = ListVector.empty("v", allocator)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private void allocateBytes(final long valueBufferSize, final int valueCount) {
496496
private ArrowBuf allocateOffsetBuffer(final long size) {
497497
ArrowBuf offsetBuffer = allocator.buffer(size);
498498
offsetBuffer.readerIndex(0);
499-
initOffsetBuffer();
499+
offsetBuffer.setZero(0, offsetBuffer.capacity());
500500
return offsetBuffer;
501501
}
502502

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ private ArrowBuf allocateOffsetBuffer(final long size) {
514514
final int curSize = (int) size;
515515
ArrowBuf offsetBuffer = allocator.buffer(curSize);
516516
offsetBuffer.readerIndex(0);
517-
initOffsetBuffer();
517+
offsetBuffer.setZero(0, offsetBuffer.capacity());
518518
return offsetBuffer;
519519
}
520520

0 commit comments

Comments
 (0)