From 9e04bc2a8dd1be75dcfff87d9ca88bd9d5156fe8 Mon Sep 17 00:00:00 2001 From: liyafan82 Date: Mon, 26 Aug 2019 20:23:19 +0800 Subject: [PATCH] [ARROW-6355][Java] Make range equal visitor reusable --- .../vector/compare/ApproxEqualsVisitor.java | 15 +++++--- .../vector/compare/RangeEqualsVisitor.java | 37 +++++++++++++------ .../dictionary/DictionaryHashTable.java | 7 ++-- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/java/vector/src/main/java/org/apache/arrow/vector/compare/ApproxEqualsVisitor.java b/java/vector/src/main/java/org/apache/arrow/vector/compare/ApproxEqualsVisitor.java index 3e9783f43d6da..56c594bad3ada 100644 --- a/java/vector/src/main/java/org/apache/arrow/vector/compare/ApproxEqualsVisitor.java +++ b/java/vector/src/main/java/org/apache/arrow/vector/compare/ApproxEqualsVisitor.java @@ -49,6 +49,11 @@ public class ApproxEqualsVisitor extends RangeEqualsVisitor { private DiffFunction 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 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); 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; } diff --git a/java/vector/src/main/java/org/apache/arrow/vector/compare/RangeEqualsVisitor.java b/java/vector/src/main/java/org/apache/arrow/vector/compare/RangeEqualsVisitor.java index d69cb7cd0f308..7c0b4aeeeceaa 100644 --- a/java/vector/src/main/java/org/apache/arrow/vector/compare/RangeEqualsVisitor.java +++ b/java/vector/src/main/java/org/apache/arrow/vector/compare/RangeEqualsVisitor.java @@ -37,23 +37,24 @@ */ public class RangeEqualsVisitor implements VectorVisitor { - 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); } /** @@ -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. */ @@ -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; } @@ -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; } diff --git a/java/vector/src/main/java/org/apache/arrow/vector/dictionary/DictionaryHashTable.java b/java/vector/src/main/java/org/apache/arrow/vector/dictionary/DictionaryHashTable.java index d012c45471c8b..fe378e1e7c068 100644 --- a/java/vector/src/main/java/org/apache/arrow/vector/dictionary/DictionaryHashTable.java +++ b/java/vector/src/main/java/org/apache/arrow/vector/dictionary/DictionaryHashTable.java @@ -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; } }