Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-43966: [Java] Check for nullabilities when comparing StructVector #43968

Merged
merged 1 commit into from
Sep 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.arrow.vector.complex.ListVector;
import org.apache.arrow.vector.complex.ListViewVector;
import org.apache.arrow.vector.complex.NonNullableStructVector;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.complex.UnionVector;

/** Visitor to compare a range of values for vectors. */
Expand Down Expand Up @@ -345,6 +346,20 @@ protected boolean compareDenseUnionVectors(Range range) {
return true;
}

private boolean compareStructVectorsInternal(
NonNullableStructVector leftVector, NonNullableStructVector rightVector, Range range) {
List<String> leftChildNames = leftVector.getChildFieldNames();
for (String name : leftChildNames) {
RangeEqualsVisitor visitor =
createInnerVisitor(
leftVector.getChild(name), rightVector.getChild(name), /*type comparator*/ null);
if (!visitor.rangeEquals(range)) {
return false;
}
}
return true;
}

protected boolean compareStructVectors(Range range) {
NonNullableStructVector leftVector = (NonNullableStructVector) left;
NonNullableStructVector rightVector = (NonNullableStructVector) right;
Expand All @@ -354,15 +369,49 @@ protected boolean compareStructVectors(Range range) {
return false;
}

for (String name : leftChildNames) {
RangeEqualsVisitor visitor =
createInnerVisitor(
leftVector.getChild(name), rightVector.getChild(name), /*type comparator*/ null);
if (!visitor.rangeEquals(range)) {
if (!(leftVector instanceof StructVector || rightVector instanceof StructVector)) {
// neither struct vector is nullable
return compareStructVectorsInternal(leftVector, rightVector, range);
}

Range subRange = new Range(0, 0, 0);
boolean lastIsNull = true;
int lastNullIndex = -1;
for (int i = 0; i < range.getLength(); i++) {
int leftIndex = range.getLeftStart() + i;
int rightIndex = range.getRightStart() + i;
boolean isLeftNull = leftVector.isNull(leftIndex);
boolean isRightNull = rightVector.isNull(rightIndex);

if (isLeftNull != isRightNull) {
// exactly one slot is null, unequal
return false;
}
if (isLeftNull) {
// slots are null
if (!lastIsNull) {
subRange
.setLeftStart(range.getLeftStart() + lastNullIndex + 1)
.setRightStart(range.getRightStart() + lastNullIndex + 1)
.setLength(i - (lastNullIndex + 1));
if (!compareStructVectorsInternal(leftVector, rightVector, subRange)) {
return false;
}
}
lastIsNull = true;
lastNullIndex = i;
} else {
// slots are not null
lastIsNull = false;
}
}
if (!lastIsNull) {
subRange
.setLeftStart(range.getLeftStart() + lastNullIndex + 1)
.setRightStart(range.getRightStart() + lastNullIndex + 1)
.setLength(range.getLength() - (lastNullIndex + 1));
return compareStructVectorsInternal(leftVector, rightVector, subRange);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,25 +434,39 @@ public void testStructVectorRangeEquals() {
NullableStructWriter writer1 = vector1.getWriter();
writer1.allocate();

writeStructVector(writer1, 0, 0L);
writeStructVector(writer1, 1, 10L);
writeStructVector(writer1, 2, 20L);
writeStructVector(writer1, 3, 30L);
writeStructVector(writer1, 4, 40L);
writeStructVector(writer1, 5, 50L);
writer1.setValueCount(5);
writer1.setValueCount(6);
lidavidm marked this conversation as resolved.
Show resolved Hide resolved

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

writeStructVector(writer2, 0, 00L);
writeStructVector(writer2, 0, 0L);
writeStructVector(writer2, 2, 20L);
writeStructVector(writer2, 3, 30L);
writeStructVector(writer2, 4, 40L);
writeStructVector(writer2, 0, 0L);
writer2.setValueCount(5);

RangeEqualsVisitor visitor = new RangeEqualsVisitor(vector1, vector2);
assertTrue(visitor.rangeEquals(new Range(1, 1, 3)));
assertTrue(visitor.rangeEquals(new Range(2, 1, 3)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe write a new test to evaluate the new case?


// different nullability but same values
vector1.setNull(3);
assertFalse(visitor.rangeEquals(new Range(2, 1, 3)));
// both null and same values
vector2.setNull(2);
assertTrue(visitor.rangeEquals(new Range(2, 1, 3)));
// both not null but different values
assertFalse(visitor.rangeEquals(new Range(2, 1, 4)));
// both null but different values
vector1.setNull(5);
vector2.setNull(4);
assertTrue(visitor.rangeEquals(new Range(2, 1, 4)));
}
}

Expand Down
Loading