Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions c/src/main/java/org/apache/arrow/c/ArrayImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.arrow.vector.dictionary.Dictionary;
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.DictionaryEncoding;

/** Importer for {@link ArrowArray}. */
Expand Down Expand Up @@ -83,6 +84,15 @@ private void importChild(ArrayImporter parent, ArrowArray src) {
}

private void doImport(ArrowArray.Snapshot snapshot) {
// A non-zero offset is only meaningful for arrays that have buffers to offset into.
// Null arrays carry no buffers, so their offset is inert and safe to ignore.
checkState(
snapshot.offset == 0
|| snapshot.length == 0
|| vector.getField().getType().getTypeID() == ArrowType.ArrowTypeID.Null,
"ArrowArray struct has non-zero offset (%s), which is not supported",
snapshot.offset);

// First import children (required for reconstituting parent array data)
long[] children =
NativeUtil.toJavaArray(snapshot.children, checkedCastToInt(snapshot.n_children));
Expand Down
3 changes: 2 additions & 1 deletion c/src/main/java/org/apache/arrow/c/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
/**
* Functions for working with the C data interface.
*
* <p>This API is EXPERIMENTAL. Note that currently only 64bit systems are supported.
* <p>This API is EXPERIMENTAL. Note that currently only 64bit systems are supported. Importing
* {@link ArrowArray ArrowArrays} with a non-zero offset is not supported.
*/
public final class Data {

Expand Down
59 changes: 59 additions & 0 deletions c/src/test/java/org/apache/arrow/c/RoundtripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,65 @@ public void testImportReleasedArray() {
}
}

@Test
public void testImportArrayWithNonZeroOffset() {
try (IntVector source = new IntVector("source", allocator);
IntVector destination = new IntVector("destination", allocator);
ArrowArray array = ArrowArray.allocateNew(allocator)) {
setVector(source, 1, 2, 3);
Data.exportVector(allocator, source, null, array);

ArrowArray.Snapshot snapshot = array.snapshot();
snapshot.offset = 1;
array.save(snapshot);

Exception e =
assertThrows(
IllegalStateException.class,
() -> Data.importIntoVector(allocator, array, destination, null));
assertEquals(
"ArrowArray struct has non-zero offset (1), which is not supported", e.getMessage());
}
}

@Test
public void testImportEmptyArrayWithNonZeroOffset() {
try (IntVector source = new IntVector("source", allocator);
IntVector destination = new IntVector("destination", allocator);
ArrowArray array = ArrowArray.allocateNew(allocator)) {
setVector(source, 1, 2, 3);
Data.exportVector(allocator, source, null, array);

ArrowArray.Snapshot snapshot = array.snapshot();
snapshot.offset = source.getValueCount();
snapshot.length = 0;
array.save(snapshot);

Data.importIntoVector(allocator, array, destination, null);
assertEquals(0, destination.getValueCount());
}
}

@Test
public void testImportNullArrayWithNonZeroOffset() {
try (NullVector source = new NullVector("source", 10);
NullVector destination = new NullVector("destination");
ArrowArray array = ArrowArray.allocateNew(allocator)) {
Data.exportVector(allocator, source, null, array);

// Mimic a sliced null array. Arrow C++ propagates the slice offset onto the struct
// even for null arrays, which export no buffers at all (n_buffers == 0), so there is
// nothing for the offset to apply to and the import is safe.
ArrowArray.Snapshot snapshot = array.snapshot();
snapshot.offset = 2;
snapshot.length = 8;
array.save(snapshot);

Data.importIntoVector(allocator, array, destination, null);
assertEquals(8, destination.getValueCount());
}
}

@Test
public void testArrayStructReuse() {
// Consumer allocates empty structures
Expand Down
5 changes: 5 additions & 0 deletions docs/source/cdata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ C Data Interface
Arrow supports exchanging data without copying or serialization within the same process
through :external+arrow:ref:`c-data-interface`, even between different language runtimes.

.. note::

The Arrow Java C Data Interface implementation does not support importing arrays with
a non-zero offset.

Java to Python
--------------

Expand Down
Loading