-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-6250: [Java] Implement ApproxEqualsVisitor comparing approx for floating point #5118
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
233 changes: 233 additions & 0 deletions
233
java/vector/src/main/java/org/apache/arrow/vector/compare/ApproxEqualsVisitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.arrow.vector.compare; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.arrow.vector.BaseFixedWidthVector; | ||
| import org.apache.arrow.vector.FieldVector; | ||
| import org.apache.arrow.vector.Float4Vector; | ||
| import org.apache.arrow.vector.Float8Vector; | ||
| import org.apache.arrow.vector.ValueVector; | ||
| import org.apache.arrow.vector.complex.BaseRepeatedValueVector; | ||
| import org.apache.arrow.vector.complex.FixedSizeListVector; | ||
| import org.apache.arrow.vector.complex.ListVector; | ||
| import org.apache.arrow.vector.complex.NonNullableStructVector; | ||
| import org.apache.arrow.vector.complex.UnionVector; | ||
|
|
||
| /** | ||
| * Visitor to compare floating point. | ||
| */ | ||
| public class ApproxEqualsVisitor extends RangeEqualsVisitor { | ||
|
|
||
| /** | ||
| * The float/double values are treated as equal as long as the delta is <= epsilon. | ||
| */ | ||
| private final float epsilon; | ||
|
|
||
| public ApproxEqualsVisitor(ValueVector right, float epsilon) { | ||
| this (right, epsilon, true); | ||
| } | ||
|
|
||
| public ApproxEqualsVisitor(ValueVector right, float epsilon, boolean typeCheckNeeded) { | ||
| this (right, epsilon, typeCheckNeeded, 0, 0, right.getValueCount()); | ||
| } | ||
|
|
||
| public ApproxEqualsVisitor(ValueVector right, float epsilon, boolean typeCheckNeeded, | ||
| int leftStart, int rightStart, int length) { | ||
| super(right, rightStart, leftStart, length, typeCheckNeeded); | ||
| this.epsilon = epsilon; | ||
| } | ||
|
|
||
| @Override | ||
| public Boolean visit(BaseFixedWidthVector left, Void value) { | ||
| if (left instanceof Float4Vector) { | ||
| return validate(left) && float4ApproxEquals((Float4Vector) left); | ||
| } else if (left instanceof Float8Vector) { | ||
| return validate(left) && float8ApproxEquals((Float8Vector) left); | ||
| } else { | ||
| return super.visit(left, value); | ||
tianchen92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean compareUnionVectors(UnionVector left) { | ||
|
|
||
| UnionVector rightVector = (UnionVector) right; | ||
|
|
||
| List<FieldVector> leftChildren = left.getChildrenFromFields(); | ||
| List<FieldVector> rightChildren = rightVector.getChildrenFromFields(); | ||
|
|
||
| if (leftChildren.size() != rightChildren.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int k = 0; k < leftChildren.size(); k++) { | ||
| ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(rightChildren.get(k), | ||
| epsilon); | ||
| if (!leftChildren.get(k).accept(visitor, null)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean compareStructVectors(NonNullableStructVector left) { | ||
|
|
||
| NonNullableStructVector rightVector = (NonNullableStructVector) right; | ||
|
|
||
| if (!left.getChildFieldNames().equals(rightVector.getChildFieldNames())) { | ||
| return false; | ||
| } | ||
|
|
||
| for (String name : left.getChildFieldNames()) { | ||
| ApproxEqualsVisitor visitor = new ApproxEqualsVisitor(rightVector.getChild(name), | ||
| epsilon); | ||
| if (!left.getChild(name).accept(visitor, null)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean compareListVectors(ListVector left) { | ||
|
|
||
| for (int i = 0; i < length; i++) { | ||
| int leftIndex = leftStart + i; | ||
| int rightIndex = rightStart + i; | ||
|
|
||
| boolean isNull = left.isNull(leftIndex); | ||
| if (isNull != right.isNull(rightIndex)) { | ||
| return false; | ||
| } | ||
|
|
||
| int offsetWidth = BaseRepeatedValueVector.OFFSET_WIDTH; | ||
|
|
||
| if (!isNull) { | ||
| final int startIndexLeft = left.getOffsetBuffer().getInt(leftIndex * offsetWidth); | ||
| final int endIndexLeft = left.getOffsetBuffer().getInt((leftIndex + 1) * offsetWidth); | ||
|
|
||
| final int startIndexRight = right.getOffsetBuffer().getInt(rightIndex * offsetWidth); | ||
| final int endIndexRight = right.getOffsetBuffer().getInt((rightIndex + 1) * offsetWidth); | ||
|
|
||
| if ((endIndexLeft - startIndexLeft) != (endIndexRight - startIndexRight)) { | ||
| return false; | ||
| } | ||
|
|
||
| ValueVector leftDataVector = left.getDataVector(); | ||
| ValueVector rightDataVector = ((ListVector)right).getDataVector(); | ||
|
|
||
| if (!leftDataVector.accept(new ApproxEqualsVisitor(rightDataVector, epsilon, typeCheckNeeded, | ||
| startIndexLeft, startIndexRight, (endIndexLeft - startIndexLeft)), null)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| protected boolean compareFixedSizeListVectors(FixedSizeListVector left) { | ||
|
|
||
| if (left.getListSize() != ((FixedSizeListVector)right).getListSize()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < length; i++) { | ||
| int leftIndex = leftStart + i; | ||
| int rightIndex = rightStart + i; | ||
|
|
||
| boolean isNull = left.isNull(leftIndex); | ||
| if (isNull != right.isNull(rightIndex)) { | ||
| return false; | ||
| } | ||
|
|
||
| int listSize = left.getListSize(); | ||
|
|
||
| if (!isNull) { | ||
| final int startIndexLeft = leftIndex * listSize; | ||
| final int endIndexLeft = (leftIndex + 1) * listSize; | ||
|
|
||
| final int startIndexRight = rightIndex * listSize; | ||
| final int endIndexRight = (rightIndex + 1) * listSize; | ||
|
|
||
| if ((endIndexLeft - startIndexLeft) != (endIndexRight - startIndexRight)) { | ||
| return false; | ||
| } | ||
|
|
||
| ValueVector leftDataVector = left.getDataVector(); | ||
| ValueVector rightDataVector = ((FixedSizeListVector)right).getDataVector(); | ||
|
|
||
| if (!leftDataVector.accept(new ApproxEqualsVisitor(rightDataVector, epsilon, typeCheckNeeded, | ||
| startIndexLeft, startIndexRight, (endIndexLeft - startIndexLeft)), null)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean float4ApproxEquals(Float4Vector left) { | ||
|
|
||
| for (int i = 0; i < length; i++) { | ||
| int leftIndex = leftStart + i; | ||
| int rightIndex = rightStart + i; | ||
|
|
||
| boolean isNull = left.isNull(leftIndex); | ||
|
|
||
| if (isNull != right.isNull(rightIndex)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!isNull) { | ||
|
|
||
| float leftValue = left.get(leftIndex); | ||
| float rightValue = ((Float4Vector)right).get(rightIndex); | ||
| if (Math.abs(leftValue - rightValue) > epsilon) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean float8ApproxEquals(Float8Vector left) { | ||
| for (int i = 0; i < length; i++) { | ||
| int leftIndex = leftStart + i; | ||
| int rightIndex = rightStart + i; | ||
|
|
||
| boolean isNull = left.isNull(leftIndex); | ||
|
|
||
| if (isNull != right.isNull(rightIndex)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!isNull) { | ||
|
|
||
| double leftValue = left.get(leftIndex); | ||
| double rightValue = ((Float8Vector)right).get(rightIndex); | ||
| if (Math.abs(leftValue - rightValue) > epsilon) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.