Skip to content

Commit

Permalink
[ARROW-6355][Java] Make range equal visitor reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
liyafan82 committed Aug 26, 2019
1 parent c9bd6d7 commit 9e04bc2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -121,9 +126,9 @@ protected boolean compareStructVectors(NonNullableStructVector left) {
return false;
}

ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(floatEpsilon, doubleEpsilon);
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);
if (!left.getChild(name).accept(visitor, null)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,24 @@
*/
public class RangeEqualsVisitor implements VectorVisitor<Boolean, Void> {

protected final ValueVector right;
protected ValueVector right;
protected int leftStart;
protected int rightStart;
protected int length;

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);
}

/**
Expand All @@ -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.
*/
Expand Down Expand Up @@ -144,9 +157,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;
}
Expand All @@ -162,9 +176,10 @@ protected boolean compareStructVectors(NonNullableStructVector left) {
return false;
}

RangeEqualsVisitor visitor = new RangeEqualsVisitor();
for (String name : left.getChildFieldNames()) {
RangeEqualsVisitor visitor = new RangeEqualsVisitor(rightVector.getChild(name),
leftStart, rightStart, length);
visitor.set(rightVector.getChild(name),
rightStart, leftStart, length, true);
if (!left.getChild(name).accept(visitor, null)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ static final int roundUpToPowerOf2(int size) {
public int getIndex(int indexInArray, ValueVector toEncode) {
int hash = toEncode.hashCode(indexInArray);
int index = indexFor(hash, table.length);

RangeEqualsVisitor equalVisitor = new RangeEqualsVisitor();
for (DictionaryHashTable.Entry e = table[index]; e != null ; e = e.next) {
if (e.hash == hash) {
int dictIndex = e.index;

if (new RangeEqualsVisitor(dictionary, dictIndex, indexInArray, 1, false)
.equals(toEncode)) {
equalVisitor.set(dictionary, dictIndex, indexInArray, 1, false);
if (equalVisitor.equals(toEncode)) {
return dictIndex;
}
}
Expand Down

0 comments on commit 9e04bc2

Please sign in to comment.