This is an automated email from the ASF dual-hosted git repository.
lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git
The following commit(s) were added to refs/heads/main by this push:
new f92585c2 GH-704: Fix initialization of offset buffer when exporting
VarChar vectors through C Data Interface (#705)
f92585c2 is described below
commit f92585c272ea4b2db5b1717f6642f3edc8f99903
Author: Kristin Cowalcijk <[email protected]>
AuthorDate: Tue Apr 8 14:11:32 2025 +0800
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 .
---
.../java/org/apache/arrow/c/RoundtripTest.java | 23 ++++++++++++++++++++++
.../arrow/vector/BaseLargeVariableWidthVector.java | 2 +-
.../arrow/vector/BaseVariableWidthVector.java | 2 +-
3 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
b/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
index 67ab282d..6d68449c 100644
--- a/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
+++ b/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
@@ -38,6 +38,8 @@ import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.memory.util.hash.ArrowBufHasher;
+import org.apache.arrow.vector.BaseLargeVariableWidthVector;
+import org.apache.arrow.vector.BaseVariableWidthVector;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.BitVector;
import org.apache.arrow.vector.DateDayVector;
@@ -181,6 +183,13 @@ public class RoundtripTest {
clazz.isInstance(imported),
String.format("expected %s but was %s", clazz, imported.getClass()));
result = VectorEqualsVisitor.vectorEquals(vector, imported);
+
+ if (imported instanceof BaseVariableWidthVector
+ || imported instanceof BaseLargeVariableWidthVector) {
+ ArrowBuf offsetBuffer = imported.getOffsetBuffer();
+ assertTrue(offsetBuffer.capacity() > 0);
+ assertEquals(0, offsetBuffer.getInt(0));
+ }
}
// Check that the ref counts of the buffers are the same after the
roundtrip
@@ -602,6 +611,13 @@ public class RoundtripTest {
}
}
+ @Test
+ public void testEmptyVarCharVector() {
+ try (final VarCharVector vector = new VarCharVector("v", allocator)) {
+ assertTrue(roundtrip(vector, VarCharVector.class));
+ }
+ }
+
@Test
public void testLargeVarBinaryVector() {
try (final LargeVarBinaryVector vector = new LargeVarBinaryVector("",
allocator)) {
@@ -635,6 +651,13 @@ public class RoundtripTest {
}
}
+ @Test
+ public void testEmptyLargeVarCharVector() {
+ try (final LargeVarCharVector vector = new LargeVarCharVector("v",
allocator)) {
+ assertTrue(roundtrip(vector, LargeVarCharVector.class));
+ }
+ }
+
@Test
public void testListVector() {
try (final ListVector vector = ListVector.empty("v", allocator)) {
diff --git
a/vector/src/main/java/org/apache/arrow/vector/BaseLargeVariableWidthVector.java
b/vector/src/main/java/org/apache/arrow/vector/BaseLargeVariableWidthVector.java
index 552a896e..7e0d0aff 100644
---
a/vector/src/main/java/org/apache/arrow/vector/BaseLargeVariableWidthVector.java
+++
b/vector/src/main/java/org/apache/arrow/vector/BaseLargeVariableWidthVector.java
@@ -496,7 +496,7 @@ public abstract class BaseLargeVariableWidthVector extends
BaseValueVector
private ArrowBuf allocateOffsetBuffer(final long size) {
ArrowBuf offsetBuffer = allocator.buffer(size);
offsetBuffer.readerIndex(0);
- initOffsetBuffer();
+ offsetBuffer.setZero(0, offsetBuffer.capacity());
return offsetBuffer;
}
diff --git
a/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java
b/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java
index aaccec60..7b8d2cdf 100644
--- a/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java
+++ b/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java
@@ -514,7 +514,7 @@ public abstract class BaseVariableWidthVector extends
BaseValueVector
final int curSize = (int) size;
ArrowBuf offsetBuffer = allocator.buffer(curSize);
offsetBuffer.readerIndex(0);
- initOffsetBuffer();
+ offsetBuffer.setZero(0, offsetBuffer.capacity());
return offsetBuffer;
}