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
ARROW-6355: [Java] Make range equal visitor reusable #5195
Changes from 1 commit
a1f7046
53c1e0b
7482414
073bc78
ffe0e6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,11 @@ public class ApproxEqualsVisitor extends RangeEqualsVisitor { | |
private DiffFunction<Double> doubleDiffFunction = | ||
(Double value1, Double value2) -> Math.abs(value1 - value2); | ||
|
||
public ApproxEqualsVisitor(float floatEpsilon, double doubleEpsilon) { | ||
this.floatEpsilon = floatEpsilon; | ||
this.doubleEpsilon = doubleEpsilon; | ||
} | ||
|
||
public ApproxEqualsVisitor(ValueVector right, float epsilon) { | ||
this (right, epsilon, epsilon, true); | ||
} | ||
|
@@ -66,9 +71,9 @@ public ApproxEqualsVisitor(ValueVector right, float floatEpsilon, double doubleE | |
*/ | ||
public ApproxEqualsVisitor(ValueVector right, float floatEpsilon, double doubleEpsilon, boolean typeCheckNeeded, | ||
int leftStart, int rightStart, int length) { | ||
super(right, rightStart, leftStart, length, typeCheckNeeded); | ||
this.floatEpsilon = floatEpsilon; | ||
this.doubleEpsilon = doubleEpsilon; | ||
set(right, rightStart, leftStart, length, typeCheckNeeded); | ||
} | ||
|
||
public void setFloatDiffFunction(DiffFunction<Float> floatDiffFunction) { | ||
|
@@ -102,9 +107,9 @@ protected boolean compareUnionVectors(UnionVector left) { | |
return false; | ||
} | ||
|
||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(floatEpsilon, doubleEpsilon); | ||
for (int k = 0; k < leftChildren.size(); k++) { | ||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(rightChildren.get(k), | ||
floatEpsilon, doubleEpsilon); | ||
visitor.set(rightChildren.get(k), 0, 0, rightChildren.get(k).getValueCount(), true); | ||
if (!leftChildren.get(k).accept(visitor, null)) { | ||
return false; | ||
} | ||
|
@@ -121,9 +126,9 @@ protected boolean compareStructVectors(NonNullableStructVector left) { | |
return false; | ||
} | ||
|
||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(floatEpsilon, doubleEpsilon); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also do this for compareListVectors/compareFixedSizeLiseVectors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems compareListVector/compareFixedSizeListVectors in this class does not change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry. Fixed now. |
||
for (String name : left.getChildFieldNames()) { | ||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(rightVector.getChild(name), | ||
floatEpsilon, doubleEpsilon); | ||
visitor.set(rightVector.getChild(name), 0, 0, rightVector.getChild(name).getValueCount(), true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are two other ways to pass along parameters did you consider these options ? The drawback of the current approach is that it's mandating a strict order between (visitor.set and the accept call) i.e the accept call must always be preceeded by the visitor.set call (not doing so will be caught at runtime only). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pravindra Sounds good. |
||
if (!left.getChild(name).accept(visitor, null)) { | ||
return false; | ||
} | ||
|
@@ -134,7 +139,7 @@ protected boolean compareStructVectors(NonNullableStructVector left) { | |
|
||
@Override | ||
protected boolean compareListVectors(ListVector left) { | ||
|
||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(floatEpsilon, doubleEpsilon); | ||
for (int i = 0; i < length; i++) { | ||
int leftIndex = leftStart + i; | ||
int rightIndex = rightStart + i; | ||
|
@@ -160,8 +165,8 @@ protected boolean compareListVectors(ListVector left) { | |
ValueVector leftDataVector = left.getDataVector(); | ||
ValueVector rightDataVector = ((ListVector)right).getDataVector(); | ||
|
||
if (!leftDataVector.accept(new ApproxEqualsVisitor(rightDataVector, floatEpsilon, doubleEpsilon, | ||
typeCheckNeeded, startIndexLeft, startIndexRight, (endIndexLeft - startIndexLeft)), null)) { | ||
visitor.set(rightDataVector, startIndexRight, startIndexLeft, endIndexLeft - startIndexLeft, true); | ||
if (!leftDataVector.accept(visitor, null)) { | ||
return false; | ||
} | ||
} | ||
|
@@ -175,6 +180,7 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) { | |
return false; | ||
} | ||
|
||
ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(floatEpsilon, doubleEpsilon); | ||
for (int i = 0; i < length; i++) { | ||
int leftIndex = leftStart + i; | ||
int rightIndex = rightStart + i; | ||
|
@@ -200,8 +206,8 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) { | |
ValueVector leftDataVector = left.getDataVector(); | ||
ValueVector rightDataVector = ((FixedSizeListVector)right).getDataVector(); | ||
|
||
if (!leftDataVector.accept(new ApproxEqualsVisitor(rightDataVector, floatEpsilon, doubleEpsilon, | ||
typeCheckNeeded, startIndexLeft, startIndexRight, (endIndexLeft - startIndexLeft)), null)) { | ||
visitor.set(rightDataVector, rightStart, leftStart, endIndexLeft - startIndexLeft, true); | ||
if (!leftDataVector.accept(visitor, null)) { | ||
return false; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,23 +37,24 @@ | |
*/ | ||
public class RangeEqualsVisitor implements VectorVisitor<Boolean, Void> { | ||
|
||
protected final ValueVector right; | ||
protected int leftStart; | ||
protected int rightStart; | ||
protected int length; | ||
protected ValueVector right; | ||
protected int leftStart = -1; | ||
protected int rightStart = -1; | ||
protected int length = -1; | ||
|
||
protected boolean typeCheckNeeded = true; | ||
|
||
/** | ||
* The default constructor. | ||
*/ | ||
public RangeEqualsVisitor() { | ||
} | ||
|
||
/** | ||
* Constructs a new instance. | ||
*/ | ||
public RangeEqualsVisitor(ValueVector right, int rightStart, int leftStart, int length, boolean typeCheckNeeded) { | ||
this.leftStart = leftStart; | ||
this.rightStart = rightStart; | ||
this.right = right; | ||
this.length = length; | ||
this.typeCheckNeeded = typeCheckNeeded; | ||
Preconditions.checkArgument(length >= 0, "length must be non negative"); | ||
set(right, rightStart, leftStart, length, typeCheckNeeded); | ||
} | ||
|
||
/** | ||
|
@@ -63,6 +64,18 @@ public RangeEqualsVisitor(ValueVector right, int leftStart, int rightStart, int | |
this(right, rightStart, leftStart, length, true); | ||
} | ||
|
||
/** | ||
* Sets the parameters for comparison. | ||
*/ | ||
public void set(ValueVector right, int rightStart, int leftStart, int length, boolean typeCheckNeeded) { | ||
this.right = right; | ||
this.leftStart = leftStart; | ||
this.rightStart = rightStart; | ||
this.length = length; | ||
this.typeCheckNeeded = typeCheckNeeded; | ||
Preconditions.checkArgument(length >= 0, "length must be non negative"); | ||
} | ||
|
||
/** | ||
* Do some validation work, like type check and indices check. | ||
*/ | ||
|
@@ -81,6 +94,12 @@ protected boolean validate(ValueVector left) { | |
Preconditions.checkArgument((rightStart + length) <= right.getValueCount(), | ||
"(rightStart + length) %s out of range[0, %s].", 0, right.getValueCount()); | ||
|
||
Preconditions.checkArgument(left != null, | ||
"left vector cannot be null"); | ||
|
||
Preconditions.checkArgument(right != null, | ||
"right vector cannot be null"); | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -144,9 +163,10 @@ protected boolean compareUnionVectors(UnionVector left) { | |
return false; | ||
} | ||
|
||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(); | ||
for (int k = 0; k < leftChildren.size(); k++) { | ||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(rightChildren.get(k), | ||
leftStart, rightStart, length); | ||
visitor.set(rightChildren.get(k), | ||
rightStart, leftStart, length, true); | ||
if (!leftChildren.get(k).accept(visitor, null)) { | ||
return false; | ||
} | ||
|
@@ -162,9 +182,10 @@ protected boolean compareStructVectors(NonNullableStructVector left) { | |
return false; | ||
} | ||
|
||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(); | ||
for (String name : left.getChildFieldNames()) { | ||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(rightVector.getChild(name), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also for compareListVectors/compareFixedSizeListVectors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Thanks for the good point. |
||
leftStart, rightStart, length); | ||
visitor.set(rightVector.getChild(name), | ||
rightStart, leftStart, length, true); | ||
if (!left.getChild(name).accept(visitor, null)) { | ||
return false; | ||
} | ||
|
@@ -236,7 +257,7 @@ protected boolean compareBaseVariableWidthVectors(BaseVariableWidthVector left) | |
} | ||
|
||
protected boolean compareListVectors(ListVector left) { | ||
|
||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(); | ||
for (int i = 0; i < length; i++) { | ||
int leftIndex = leftStart + i; | ||
int rightIndex = rightStart + i; | ||
|
@@ -262,8 +283,8 @@ protected boolean compareListVectors(ListVector left) { | |
ValueVector leftDataVector = left.getDataVector(); | ||
ValueVector rightDataVector = ((ListVector)right).getDataVector(); | ||
|
||
if (!leftDataVector.accept(new RangeEqualsVisitor(rightDataVector, startIndexLeft, | ||
startIndexRight, (endIndexLeft - startIndexLeft)), null)) { | ||
visitor.set(rightDataVector, startIndexRight, startIndexLeft, endIndexLeft - startIndexLeft, true); | ||
if (!leftDataVector.accept(visitor, null)) { | ||
return false; | ||
} | ||
} | ||
|
@@ -277,6 +298,7 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) { | |
return false; | ||
} | ||
|
||
RangeEqualsVisitor visitor = new RangeEqualsVisitor(); | ||
for (int i = 0; i < length; i++) { | ||
int leftIndex = leftStart + i; | ||
int rightIndex = rightStart + i; | ||
|
@@ -302,8 +324,8 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) { | |
ValueVector leftDataVector = left.getDataVector(); | ||
ValueVector rightDataVector = ((FixedSizeListVector)right).getDataVector(); | ||
|
||
if (!leftDataVector.accept(new RangeEqualsVisitor(rightDataVector, startIndexLeft, startIndexRight, | ||
(endIndexLeft - startIndexLeft)), null)) { | ||
visitor.set(rightDataVector, startIndexRight, startIndexLeft, endIndexLeft - startIndexLeft, true); | ||
if (!leftDataVector.accept(visitor, null)) { | ||
return false; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we give a message if visitor is directly used without setting params?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the code to make leftStart, rightStart and length have -1 as default value. Since each visit method calls validate method first, if the set method is not called, an exception will be thrown.