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
1 change: 0 additions & 1 deletion dev/archery/archery/integration/datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,6 @@ def _temp_path():

generate_list_view_case()
.skip_tester('C#') # Doesn't support large list views
.skip_tester('Java')
.skip_tester('JS')
.skip_tester('nanoarrow')
.skip_tester('Rust'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.complex.DenseUnionVector;
import org.apache.arrow.vector.complex.LargeListVector;
import org.apache.arrow.vector.complex.LargeListViewVector;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.ListViewVector;
import org.apache.arrow.vector.complex.MapVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
Expand Down Expand Up @@ -400,13 +402,17 @@ public List<ArrowBuf> visit(ArrowType.Duration type) {

@Override
public List<ArrowBuf> visit(ArrowType.ListView type) {
throw new UnsupportedOperationException(
"Importing buffers for view type: " + type + " not supported");
return Arrays.asList(
maybeImportBitmap(type),
importFixedBytes(type, 1, ListViewVector.OFFSET_WIDTH),
importFixedBytes(type, 2, ListViewVector.SIZE_WIDTH));
}

@Override
public List<ArrowBuf> visit(ArrowType.LargeListView type) {
throw new UnsupportedOperationException(
"Importing buffers for view type: " + type + " not supported");
return Arrays.asList(
maybeImportBitmap(type),
importFixedBytes(type, 1, LargeListViewVector.OFFSET_WIDTH),
importFixedBytes(type, 2, LargeListViewVector.SIZE_WIDTH));
}
}
8 changes: 8 additions & 0 deletions java/c/src/main/java/org/apache/arrow/c/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ static String asString(ArrowType arrowType) {
return "vu";
case BinaryView:
return "vz";
case ListView:
return "+vl";
case LargeListView:
return "+vL";
case NONE:
throw new IllegalArgumentException("Arrow type ID is NONE");
default:
Expand Down Expand Up @@ -313,6 +317,10 @@ static ArrowType asType(String format, long flags)
return new ArrowType.Utf8View();
case "vz":
return new ArrowType.BinaryView();
case "+vl":
return new ArrowType.ListView();
case "+vL":
return new ArrowType.LargeListView();
default:
String[] parts = format.split(":", 2);
if (parts.length == 2) {
Expand Down
42 changes: 42 additions & 0 deletions java/c/src/test/java/org/apache/arrow/c/RoundtripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
import org.apache.arrow.vector.compare.VectorEqualsVisitor;
import org.apache.arrow.vector.complex.FixedSizeListVector;
import org.apache.arrow.vector.complex.LargeListVector;
import org.apache.arrow.vector.complex.LargeListViewVector;
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.ListViewVector;
import org.apache.arrow.vector.complex.MapVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
Expand Down Expand Up @@ -683,6 +685,46 @@ public void testFixedSizeListVector() {
}
}

@Test
public void testListViewVector() {
try (final ListViewVector vector = ListViewVector.empty("v", allocator)) {
setVector(
vector,
Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList()),
Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList()),
new ArrayList<Integer>());
assertTrue(roundtrip(vector, ListViewVector.class));
}
}

@Test
public void testEmptyListViewVector() {
try (final ListViewVector vector = ListViewVector.empty("v", allocator)) {
setVector(vector, new ArrayList<Integer>());
assertTrue(roundtrip(vector, ListViewVector.class));
}
}

@Test
public void testLargeListViewVector() {
try (final LargeListViewVector vector = LargeListViewVector.empty("v", allocator)) {
setVector(
vector,
Arrays.stream(new int[] {1, 2}).boxed().collect(Collectors.toList()),
Arrays.stream(new int[] {3, 4}).boxed().collect(Collectors.toList()),
new ArrayList<Integer>());
assertTrue(roundtrip(vector, LargeListViewVector.class));
}
}

@Test
public void testEmptyLargeListViewVector() {
try (final LargeListViewVector vector = LargeListViewVector.empty("v", allocator)) {
setVector(vector, new ArrayList<Integer>());
assertTrue(roundtrip(vector, LargeListViewVector.class));
}
}

@Test
public void testMapVector() {
int count = 5;
Expand Down
47 changes: 47 additions & 0 deletions java/c/src/test/python/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,53 @@ def test_reader_complex_roundtrip(self):
]
self.round_trip_reader(schema, data)

def test_listview_array(self):
self.round_trip_array(lambda: pa.array(
[[], [0], [1, 2], [4, 5, 6]], pa.list_view(pa.int64())
# disabled check_metadata since in Java API the listview
# internal field name ("item") is not preserved
# during round trips (it becomes "$data$").
), check_metadata=False)

def test_empty_listview_array(self):
with pa.BufferOutputStream() as bos:
schema = pa.schema([pa.field("f0", pa.list_view(pa.int32()), True)])
with ipc.new_stream(bos, schema) as writer:
src = pa.RecordBatch.from_arrays(
[pa.array([[]], pa.list_view(pa.int32()))], schema=schema)
writer.write(src)
data_bytes = bos.getvalue()

def recreate_batch():
with pa.input_stream(data_bytes) as ios:
with ipc.open_stream(ios) as reader:
return reader.read_next_batch()

self.round_trip_record_batch(recreate_batch)

def test_largelistview_array(self):
self.round_trip_array(lambda: pa.array(
[[], [0], [1, 2], [4, 5, 6]], pa.large_list_view(pa.int64())
# disabled check_metadata since in Java API the listview
# internal field name ("item") is not preserved
# during round trips (it becomes "$data$").
), check_metadata=False)

def test_empty_largelistview_array(self):
with pa.BufferOutputStream() as bos:
schema = pa.schema([pa.field("f0", pa.large_list_view(pa.int32()), True)])
with ipc.new_stream(bos, schema) as writer:
src = pa.RecordBatch.from_arrays(
[pa.array([[]], pa.large_list_view(pa.int32()))], schema=schema)
writer.write(src)
data_bytes = bos.getvalue()

def recreate_batch():
with pa.input_stream(data_bytes) as ios:
with ipc.open_stream(ios) as reader:
return reader.read_next_batch()

self.round_trip_record_batch(recreate_batch)

if __name__ == '__main__':
unittest.main(verbosity=2)
Original file line number Diff line number Diff line change
Expand Up @@ -305,38 +305,43 @@ public void setValueCount(int valueCount) {
while (valueCount > getOffsetBufferValueCapacity()) {
reallocateBuffers();
}
final int childValueCount = valueCount == 0 ? 0 : getLengthOfChildVector();
final int childValueCount = valueCount == 0 ? 0 : getMaxViewEndChildVector();
vector.setValueCount(childValueCount);
}

protected int getLengthOfChildVector() {
/**
* Get the end of the child vector via the maximum view length. This method deduces the length by
* considering the condition i.e., argmax_i(offsets[i] + size[i]).
*
* @return the end of the child vector.
*/
protected int getMaxViewEndChildVector() {
int maxOffsetSizeSum = offsetBuffer.getInt(0) + sizeBuffer.getInt(0);
int minOffset = offsetBuffer.getInt(0);
for (int i = 0; i < valueCount; i++) {
int currentOffset = offsetBuffer.getInt((long) i * OFFSET_WIDTH);
int currentSize = sizeBuffer.getInt((long) i * SIZE_WIDTH);
int currentSum = currentOffset + currentSize;

maxOffsetSizeSum = Math.max(maxOffsetSizeSum, currentSum);
minOffset = Math.min(minOffset, currentOffset);
}

return maxOffsetSizeSum - minOffset;
return maxOffsetSizeSum;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lidavidm what I have done in the first implementation is incorrect, I think the test case I had didn't tackle this case, but the C data interface tests caught it. Sorry about that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This need a little bit of work... I am improving it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated.

}

protected int getLengthOfChildVectorByIndex(int index) {
/**
* Get the end of the child vector via the maximum view length of the child vector by index.
*
* @return the end of the child vector by index
*/
protected int getMaxViewEndChildVectorByIndex(int index) {
int maxOffsetSizeSum = offsetBuffer.getInt(0) + sizeBuffer.getInt(0);
int minOffset = offsetBuffer.getInt(0);
for (int i = 0; i < index; i++) {
int currentOffset = offsetBuffer.getInt((long) i * OFFSET_WIDTH);
int currentSize = sizeBuffer.getInt((long) i * SIZE_WIDTH);
int currentSum = currentOffset + currentSize;

maxOffsetSizeSum = Math.max(maxOffsetSizeSum, currentSum);
minOffset = Math.min(minOffset, currentOffset);
}

return maxOffsetSizeSum - minOffset;
return maxOffsetSizeSum;
}

/**
Expand Down Expand Up @@ -390,7 +395,7 @@ public int startNewValue(int index) {
}

if (index > 0) {
final int prevOffset = getLengthOfChildVectorByIndex(index);
final int prevOffset = getMaxViewEndChildVectorByIndex(index);
offsetBuffer.setInt((long) index * OFFSET_WIDTH, prevOffset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,38 +304,44 @@ public void setValueCount(int valueCount) {
while (valueCount > getOffsetBufferValueCapacity()) {
reallocateBuffers();
}
final int childValueCount = valueCount == 0 ? 0 : getLengthOfChildVector();
final int childValueCount = valueCount == 0 ? 0 : getMaxViewEndChildVector();
vector.setValueCount(childValueCount);
}

protected int getLengthOfChildVector() {
/**
* Get the end of the child vector via the maximum view length. This method deduces the length by
* considering the condition i.e., argmax_i(offsets[i] + size[i]).
*
* @return the end of the child vector.
*/
protected int getMaxViewEndChildVector() {
int maxOffsetSizeSum = offsetBuffer.getInt(0) + sizeBuffer.getInt(0);
int minOffset = offsetBuffer.getInt(0);
for (int i = 0; i < valueCount; i++) {
int currentOffset = offsetBuffer.getInt(i * OFFSET_WIDTH);
int currentSize = sizeBuffer.getInt(i * SIZE_WIDTH);
int currentSum = currentOffset + currentSize;

maxOffsetSizeSum = Math.max(maxOffsetSizeSum, currentSum);
minOffset = Math.min(minOffset, currentOffset);
}

return maxOffsetSizeSum - minOffset;
return maxOffsetSizeSum;
}

protected int getLengthOfChildVectorByIndex(int index) {
/**
* Get the end of the child vector via the maximum view length of the child vector by index.
*
* @return the end of the child vector by index
*/
protected int getMaxViewEndChildVectorByIndex(int index) {
int maxOffsetSizeSum = offsetBuffer.getInt(0) + sizeBuffer.getInt(0);
int minOffset = offsetBuffer.getInt(0);
// int minOffset = offsetBuffer.getInt(0);
for (int i = 0; i < index; i++) {
int currentOffset = offsetBuffer.getInt(i * OFFSET_WIDTH);
int currentSize = sizeBuffer.getInt(i * SIZE_WIDTH);
int currentSum = currentOffset + currentSize;

maxOffsetSizeSum = Math.max(maxOffsetSizeSum, currentSum);
minOffset = Math.min(minOffset, currentOffset);
}

return maxOffsetSizeSum - minOffset;
return maxOffsetSizeSum;
}

/**
Expand Down Expand Up @@ -389,7 +395,7 @@ public int startNewValue(int index) {
}

if (index > 0) {
final int prevOffset = getLengthOfChildVectorByIndex(index);
final int prevOffset = getMaxViewEndChildVectorByIndex(index);
offsetBuffer.setInt(index * OFFSET_WIDTH, prevOffset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ public List<ArrowBuf> getFieldBuffers() {
*/
@Override
public void exportCDataBuffers(List<ArrowBuf> buffers, ArrowBuf buffersPtr, long nullValue) {
throw new UnsupportedOperationException("exportCDataBuffers Not implemented yet");
exportBuffer(validityBuffer, buffers, buffersPtr, nullValue, true);
exportBuffer(offsetBuffer, buffers, buffersPtr, nullValue, true);
exportBuffer(sizeBuffer, buffers, buffersPtr, nullValue, true);
}

@Override
Expand Down Expand Up @@ -851,7 +853,7 @@ public int startNewValue(int index) {
}

if (index > 0) {
final int prevOffset = getLengthOfChildVectorByIndex(index);
final int prevOffset = getMaxViewEndChildVectorByIndex(index);
offsetBuffer.setInt(index * OFFSET_WIDTH, prevOffset);
}

Expand Down Expand Up @@ -943,7 +945,7 @@ public void setValueCount(int valueCount) {
}
}
/* valueCount for the data vector is the current end offset */
final long childValueCount = (valueCount == 0) ? 0 : getLengthOfChildVector();
final long childValueCount = (valueCount == 0) ? 0 : getMaxViewEndChildVector();
/* set the value count of data vector and this will take care of
* checking whether data buffer needs to be reallocated.
* TODO: revisit when 64-bit vectors are supported
Expand Down Expand Up @@ -1001,7 +1003,7 @@ public double getDensity() {
if (valueCount == 0) {
return 0.0D;
}
final double totalListSize = getLengthOfChildVector();
final double totalListSize = getMaxViewEndChildVector();
return totalListSize / valueCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ public int startNewValue(int index) {
}

if (index > 0) {
final int prevOffset = getLengthOfChildVectorByIndex(index);
final int prevOffset = getMaxViewEndChildVectorByIndex(index);
offsetBuffer.setInt(index * OFFSET_WIDTH, prevOffset);
}

Expand Down Expand Up @@ -942,7 +942,7 @@ public void setValueCount(int valueCount) {
}
}
/* valueCount for the data vector is the current end offset */
final int childValueCount = (valueCount == 0) ? 0 : getLengthOfChildVector();
final int childValueCount = (valueCount == 0) ? 0 : getMaxViewEndChildVector();
/* set the value count of data vector and this will take care of
* checking whether data buffer needs to be reallocated.
*/
Expand Down Expand Up @@ -1005,7 +1005,7 @@ public double getDensity() {
if (valueCount == 0) {
return 0.0D;
}
final double totalListSize = getLengthOfChildVector();
final double totalListSize = getMaxViewEndChildVector();
return totalListSize / valueCount;
}

Expand Down
Loading