Skip to content

Commit

Permalink
apacheGH-43638: [Java] LargeListViewVector RangeEqualVisitor and Type…
Browse files Browse the repository at this point in the history
…EqualVisitor integration (apache#43642)

### Rationale for this change

LargeListViewVector requires `RangeEqualVisitor` and `TypeEqualVisitor` to support the C Data interface. 

### What changes are included in this PR?

Adding `RangeEqualVisitor`, `TypeEqualVisitor` and the corresponding test cases. 

### Are these changes tested?

Yes. 

### Are there any user-facing changes?

No
* GitHub Issue: apache#43638

Authored-by: Vibhatha Abeykoon <vibhatha@gmail.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
vibhatha authored Aug 13, 2024
1 parent 39fe6fc commit 483bc7b
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.apache.arrow.vector.ExtensionTypeVector;
import org.apache.arrow.vector.NullVector;
import org.apache.arrow.vector.ValueVector;
import org.apache.arrow.vector.complex.BaseLargeRepeatedValueViewVector;
import org.apache.arrow.vector.complex.BaseRepeatedValueVector;
import org.apache.arrow.vector.complex.BaseRepeatedValueViewVector;
import org.apache.arrow.vector.complex.DenseUnionVector;
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.NonNullableStructVector;
Expand Down Expand Up @@ -244,6 +246,14 @@ public Boolean visit(ListViewVector left, Range range) {
return compareListViewVectors(range);
}

@Override
public Boolean visit(LargeListViewVector left, Range range) {
if (!validate(left)) {
return false;
}
return compareLargeListViewVectors(range);
}

protected RangeEqualsVisitor createInnerVisitor(
ValueVector leftInner,
ValueVector rightInner,
Expand Down Expand Up @@ -759,4 +769,51 @@ protected boolean compareListViewVectors(Range range) {
}
return true;
}

protected boolean compareLargeListViewVectors(Range range) {
LargeListViewVector leftVector = (LargeListViewVector) left;
LargeListViewVector rightVector = (LargeListViewVector) right;

RangeEqualsVisitor innerVisitor =
createInnerVisitor(
leftVector.getDataVector(), rightVector.getDataVector(), /*type comparator*/ null);
Range innerRange = new Range();

for (int i = 0; i < range.getLength(); i++) {
int leftIndex = range.getLeftStart() + i;
int rightIndex = range.getRightStart() + i;

boolean isNull = leftVector.isNull(leftIndex);
if (isNull != rightVector.isNull(rightIndex)) {
return false;
}

int offsetWidth = BaseLargeRepeatedValueViewVector.OFFSET_WIDTH;
int sizeWidth = BaseLargeRepeatedValueViewVector.SIZE_WIDTH;

if (!isNull) {
final int startIndexLeft =
leftVector.getOffsetBuffer().getInt((long) leftIndex * offsetWidth);
final int leftSize = leftVector.getSizeBuffer().getInt((long) leftIndex * sizeWidth);

final int startIndexRight =
rightVector.getOffsetBuffer().getInt((long) rightIndex * offsetWidth);
final int rightSize = rightVector.getSizeBuffer().getInt((long) rightIndex * sizeWidth);

if (leftSize != rightSize) {
return false;
}

innerRange =
innerRange
.setRightStart(startIndexRight)
.setLeftStart(startIndexLeft)
.setLength(leftSize);
if (!innerVisitor.rangeEquals(innerRange)) {
return false;
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.arrow.vector.complex.DenseUnionVector;
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.NonNullableStructVector;
Expand Down Expand Up @@ -130,6 +131,11 @@ public Boolean visit(ListViewVector left, Void value) {
return compareField(left.getField(), right.getField());
}

@Override
public Boolean visit(LargeListViewVector left, Void value) {
return compareField(left.getField(), right.getField());
}

private boolean compareField(Field leftField, Field rightField) {

if (leftField == rightField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.arrow.vector.complex.DenseUnionVector;
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.NonNullableStructVector;
Expand Down Expand Up @@ -65,4 +66,9 @@ public interface VectorVisitor<OUT, IN> {
default OUT visit(ListViewVector left, IN value) {
throw new UnsupportedOperationException("VectorVisitor for ListViewVector is not supported.");
}

default OUT visit(LargeListViewVector left, IN value) {
throw new UnsupportedOperationException(
"VectorVisitor for LargeListViewVector is not supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public int hashCode(int index, ArrowBufHasher hasher) {

@Override
public <OUT, IN> OUT accept(VectorVisitor<OUT, IN> visitor, IN value) {
throw new UnsupportedOperationException();
return visitor.visit(this, value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
import org.apache.arrow.vector.compare.VectorEqualsVisitor;
import org.apache.arrow.vector.complex.DenseUnionVector;
import org.apache.arrow.vector.complex.FixedSizeListVector;
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.StructVector;
import org.apache.arrow.vector.complex.UnionVector;
import org.apache.arrow.vector.complex.impl.NullableStructWriter;
import org.apache.arrow.vector.complex.impl.UnionLargeListViewWriter;
import org.apache.arrow.vector.complex.impl.UnionListViewWriter;
import org.apache.arrow.vector.complex.impl.UnionListWriter;
import org.apache.arrow.vector.holders.NullableIntHolder;
Expand Down Expand Up @@ -2910,6 +2912,35 @@ public void testListViewVectorEqualsWithNull() {
}
}

@Test
public void testLargeListViewVectorEqualsWithNull() {
try (final LargeListViewVector vector1 = LargeListViewVector.empty("largelistview", allocator);
final LargeListViewVector vector2 =
LargeListViewVector.empty("largelistview", allocator); ) {

UnionLargeListViewWriter writer1 = vector1.getWriter();
writer1.allocate();

// set some values
writeLargeListViewVector(writer1, new int[] {1, 2});
writeLargeListViewVector(writer1, new int[] {3, 4});
writeLargeListViewVector(writer1, new int[] {});
writer1.setValueCount(3);

UnionLargeListViewWriter writer2 = vector2.getWriter();
writer2.allocate();

// set some values
writeLargeListViewVector(writer2, new int[] {1, 2});
writeLargeListViewVector(writer2, new int[] {3, 4});
writer2.setValueCount(3);

VectorEqualsVisitor visitor = new VectorEqualsVisitor();

assertFalse(visitor.vectorEquals(vector1, vector2));
}
}

@Test
public void testListVectorEquals() {
try (final ListVector vector1 = ListVector.empty("list", allocator);
Expand Down Expand Up @@ -2974,6 +3005,39 @@ public void testListViewVectorEquals() {
}
}

@Test
public void testLargeListViewVectorEquals() {
try (final LargeListViewVector vector1 = LargeListViewVector.empty("largelistview", allocator);
final LargeListViewVector vector2 =
LargeListViewVector.empty("largelistview", allocator); ) {

UnionLargeListViewWriter writer1 = vector1.getWriter();
writer1.allocate();

// set some values
writeLargeListViewVector(writer1, new int[] {1, 2});
writeLargeListViewVector(writer1, new int[] {3, 4});
writeLargeListViewVector(writer1, new int[] {5, 6});
writer1.setValueCount(3);

UnionLargeListViewWriter writer2 = vector2.getWriter();
writer2.allocate();

// set some values
writeLargeListViewVector(writer2, new int[] {1, 2});
writeLargeListViewVector(writer2, new int[] {3, 4});
writer2.setValueCount(2);

VectorEqualsVisitor visitor = new VectorEqualsVisitor();
assertFalse(visitor.vectorEquals(vector1, vector2));

writeLargeListViewVector(writer2, new int[] {5, 6});
writer2.setValueCount(3);

assertTrue(visitor.vectorEquals(vector1, vector2));
}
}

@Test
public void testListVectorSetNull() {
try (final ListVector vector = ListVector.empty("list", allocator)) {
Expand Down Expand Up @@ -3020,6 +3084,29 @@ public void testListViewVectorSetNull() {
}
}

@Test
public void testLargeListViewVectorSetNull() {
try (final LargeListViewVector vector = LargeListViewVector.empty("largelistview", allocator)) {
UnionLargeListViewWriter writer = vector.getWriter();
writer.allocate();

writeLargeListViewVector(writer, new int[] {1, 2});
writeLargeListViewVector(writer, new int[] {3, 4});
writeLargeListViewVector(writer, new int[] {5, 6});
vector.setNull(3);
vector.setNull(4);
vector.setNull(5);
writer.setValueCount(6);

assertEquals(vector.getObject(0), Arrays.asList(1, 2));
assertEquals(vector.getObject(1), Arrays.asList(3, 4));
assertEquals(vector.getObject(2), Arrays.asList(5, 6));
assertTrue(vector.isNull(3));
assertTrue(vector.isNull(4));
assertTrue(vector.isNull(5));
}
}

@Test
public void testStructVectorEqualsWithNull() {

Expand Down Expand Up @@ -3359,6 +3446,14 @@ private void writeListViewVector(UnionListViewWriter writer, int[] values) {
writer.endListView();
}

private void writeLargeListViewVector(UnionLargeListViewWriter writer, int[] values) {
writer.startListView();
for (int v : values) {
writer.integer().writeInt(v);
}
writer.endListView();
}

@Test
public void testVariableVectorGetEndOffset() {
try (final VarCharVector vector1 = new VarCharVector("v1", allocator);
Expand Down
Loading

0 comments on commit 483bc7b

Please sign in to comment.