This is an automated email from the ASF dual-hosted git repository.
apitrou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new fac08404aa ARROW-16674: [Java] C data interface: Reading as nioBuffer
from imported buffer causes error (#13249)
fac08404aa is described below
commit fac08404aa7018e6bcab515125ca99856d624d89
Author: Hongze Zhang <[email protected]>
AuthorDate: Wed Sep 14 17:49:15 2022 +0800
ARROW-16674: [Java] C data interface: Reading as nioBuffer from imported
buffer causes error (#13249)
Lead-authored-by: Hongze Zhang <[email protected]>
Co-authored-by: Hongze Zhang <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
---
.../java/org/apache/arrow/c/ArrayImporter.java | 7 ++++---
.../java/org/apache/arrow/c/RoundtripTest.java | 17 +++++++++++++++++
.../java/org/apache/arrow/memory/ArrowBuf.java | 22 +++++++++++-----------
3 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java
b/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java
index e82cef6a8a..4da3806575 100644
--- a/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java
+++ b/java/c/src/main/java/org/apache/arrow/c/ArrayImporter.java
@@ -141,9 +141,10 @@ final class ArrayImporter {
for (long bufferPtr : buffers) {
ArrowBuf buffer = null;
if (bufferPtr != NULL) {
- // TODO(roee88): an API for getting the size for each buffer is not yet
- // available
- buffer = new ArrowBuf(referenceManager, null, Integer.MAX_VALUE,
bufferPtr);
+ // See ARROW-17720: [Java] C data interface: Add API to compute
imported buffer size
+ int capacity = Integer.MAX_VALUE;
+ buffer = new ArrowBuf(referenceManager, null, capacity, bufferPtr);
+ buffer.writerIndex(capacity);
}
result.add(buffer);
}
diff --git a/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
b/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
index 6a2b476b0c..fb7714fac0 100644
--- a/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
+++ b/java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
@@ -715,6 +716,22 @@ public class RoundtripTest {
}
}
+ @Test
+ public void testImportedBufferAsNioBuffer() {
+ IntVector imported;
+ try (final IntVector vector = new IntVector("v", allocator)) {
+ setVector(vector, 1, 2, 3, null);
+ imported = (IntVector) vectorRoundtrip(vector);
+ }
+ ArrowBuf dataBuffer = imported.getDataBuffer();
+ ByteBuffer nioBuffer = dataBuffer.nioBuffer().asReadOnlyBuffer();
+ nioBuffer.order(ByteOrder.nativeOrder());
+ assertEquals(1, nioBuffer.getInt(0));
+ assertEquals(2, nioBuffer.getInt(1 << 2));
+ assertEquals(3, nioBuffer.getInt(2 << 2));
+ imported.close();
+ }
+
@Test
public void testImportReleasedArray() {
// Consumer allocates empty structures
diff --git
a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java
b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java
index 7ff11e95d0..446b1e9fb9 100644
---
a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java
+++
b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/ArrowBuf.java
@@ -74,23 +74,23 @@ public final class ArrowBuf implements AutoCloseable {
private long writerIndex;
private final HistoricalLog historicalLog = BaseAllocator.DEBUG ?
new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, "ArrowBuf[%d]",
id) : null;
- private volatile long length;
+ private volatile long capacity;
/**
* Constructs a new ArrowBuf.
*
* @param referenceManager The memory manager to track memory usage and
reference count of this buffer
- * @param length The byte length of this buffer
+ * @param capacity The capacity in bytes of this buffer
*/
public ArrowBuf(
final ReferenceManager referenceManager,
final BufferManager bufferManager,
- final long length,
+ final long capacity,
final long memoryAddress) {
this.referenceManager = referenceManager;
this.bufferManager = bufferManager;
this.addr = memoryAddress;
- this.length = length;
+ this.capacity = capacity;
this.readerIndex = 0;
this.writerIndex = 0;
if (BaseAllocator.DEBUG) {
@@ -136,7 +136,7 @@ public final class ArrowBuf implements AutoCloseable {
}
public long capacity() {
- return length;
+ return capacity;
}
/**
@@ -146,14 +146,14 @@ public final class ArrowBuf implements AutoCloseable {
*/
public synchronized ArrowBuf capacity(long newCapacity) {
- if (newCapacity == length) {
+ if (newCapacity == capacity) {
return this;
}
Preconditions.checkArgument(newCapacity >= 0);
- if (newCapacity < length) {
- length = newCapacity;
+ if (newCapacity < capacity) {
+ capacity = newCapacity;
return this;
}
@@ -195,8 +195,8 @@ public final class ArrowBuf implements AutoCloseable {
*/
public ArrowBuf slice(long index, long length) {
- Preconditions.checkPositionIndex(index, this.length);
- Preconditions.checkPositionIndex(index + length, this.length);
+ Preconditions.checkPositionIndex(index, this.capacity);
+ Preconditions.checkPositionIndex(index + length, this.capacity);
/*
* Re the behavior of reference counting, see
http://netty.io/wiki/reference-counted-objects
@@ -235,7 +235,7 @@ public final class ArrowBuf implements AutoCloseable {
@Override
public String toString() {
- return String.format("ArrowBuf[%d], address:%d, length:%d", id,
memoryAddress(), length);
+ return String.format("ArrowBuf[%d], address:%d, capacity:%d", id,
memoryAddress(), capacity);
}
@Override