Skip to content

Commit 226a20f

Browse files
committed
refactor
1 parent 694d9f6 commit 226a20f

File tree

15 files changed

+358
-377
lines changed

15 files changed

+358
-377
lines changed

java/vector/src/main/codegen/templates/UnionVector.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.ArrayList;
3838
import java.util.Collections;
3939
import java.util.Iterator;
40+
import org.apache.arrow.vector.compare.CompareUtility;
4041
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
4142
import org.apache.arrow.vector.complex.impl.ComplexCopier;
4243
import org.apache.arrow.vector.util.CallBack;
@@ -670,6 +671,8 @@ public boolean equals(int index, ValueVector to, int toIndex) {
670671
if (!this.getField().getType().equals(to.getField().getType())) {
671672
return false;
672673
}
674+
CompareUtility.checkIndices(this, index, to, toIndex);
675+
673676
UnionVector that = (UnionVector) to;
674677
ValueVector leftVector = getVector(index);
675678
ValueVector rightVector = that.getVector(toIndex);

java/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.arrow.memory.util.ArrowBufPointer;
2727
import org.apache.arrow.memory.util.ByteFunctionHelpers;
2828
import org.apache.arrow.util.Preconditions;
29+
import org.apache.arrow.vector.compare.CompareUtility;
2930
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
3031
import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
3132
import org.apache.arrow.vector.types.pojo.Field;
@@ -875,34 +876,16 @@ public boolean equals(int index, ValueVector to, int toIndex) {
875876
if (to == null) {
876877
return false;
877878
}
879+
878880
if (!this.getField().getType().equals(to.getField().getType())) {
879881
return false;
880882
}
881883

882-
BaseFixedWidthVector that = (BaseFixedWidthVector) to;
883-
884-
boolean isNull = isNull(index);
885-
if (isNull != that.isNull(toIndex)) {
886-
return false;
887-
}
884+
CompareUtility.checkIndices(this, index, to, toIndex);
888885

889-
if (!isNull) {
890-
if (this.isSet(index) != that.isSet(toIndex)) {
891-
return false;
892-
}
893-
894-
int leftStart = typeWidth * index;
895-
int leftEnd = typeWidth * (index + 1);
896-
897-
int rightStart = typeWidth * toIndex;
898-
int rightEnd = typeWidth * (toIndex + 1);
899-
900-
int ret = ByteFunctionHelpers.equal(this.getDataBuffer(), leftStart, leftEnd,
901-
that.getDataBuffer(), rightStart, rightEnd);
902-
return ret == 1;
903-
}
886+
BaseFixedWidthVector that = (BaseFixedWidthVector) to;
904887

905-
return true;
888+
return CompareUtility.compare(this, index, that, toIndex);
906889
}
907890

908891
@Override

java/vector/src/main/java/org/apache/arrow/vector/BaseVariableWidthVector.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.arrow.memory.util.ArrowBufPointer;
2929
import org.apache.arrow.memory.util.ByteFunctionHelpers;
3030
import org.apache.arrow.util.Preconditions;
31+
import org.apache.arrow.vector.compare.CompareUtility;
3132
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
3233
import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
3334
import org.apache.arrow.vector.types.pojo.Field;
@@ -1374,26 +1375,11 @@ public boolean equals(int index, ValueVector to, int toIndex) {
13741375
return false;
13751376
}
13761377

1377-
BaseVariableWidthVector that = (BaseVariableWidthVector) to;
1378-
1379-
boolean isNull = isNull(index);
1380-
if (isNull != that.isNull(toIndex)) {
1381-
return false;
1382-
}
1378+
CompareUtility.checkIndices(this, index, to, toIndex);
13831379

1384-
if (!isNull) {
1385-
final int leftStart = getStartOffset(index);
1386-
final int leftEnd = getStartOffset(index + 1);
1387-
1388-
final int rightStart = that.getStartOffset(toIndex);
1389-
final int rightEnd = that.getStartOffset(toIndex + 1);
1390-
1391-
int ret = ByteFunctionHelpers.equal(this.getDataBuffer(), leftStart, leftEnd,
1392-
that.getDataBuffer(), rightStart, rightEnd);
1393-
return ret == 1;
1394-
}
1380+
BaseVariableWidthVector that = (BaseVariableWidthVector) to;
13951381

1396-
return true;
1382+
return CompareUtility.compare(this, index, that, toIndex);
13971383
}
13981384

13991385
@Override

java/vector/src/main/java/org/apache/arrow/vector/ExtensionTypeVector.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.arrow.memory.BufferAllocator;
2424
import org.apache.arrow.memory.OutOfMemoryException;
25+
import org.apache.arrow.vector.compare.RangeEqualsVisitor;
2526
import org.apache.arrow.vector.complex.reader.FieldReader;
2627
import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
2728
import org.apache.arrow.vector.types.Types.MinorType;
@@ -256,4 +257,9 @@ public Iterator<ValueVector> iterator() {
256257
public BufferAllocator getAllocator() {
257258
return underlyingVector.getAllocator();
258259
}
260+
261+
@Override
262+
public boolean accept(RangeEqualsVisitor visitor) {
263+
return visitor.visit(getUnderlyingVector());
264+
}
259265
}

java/vector/src/main/java/org/apache/arrow/vector/ZeroVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,6 @@ public void copyFromSafe(int fromIndex, int thisIndex, ValueVector from) {
268268

269269
@Override
270270
public boolean accept(RangeEqualsVisitor visitor) {
271-
return false;
271+
return true;
272272
}
273273
}
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.vector.compare;
19+
20+
import org.apache.arrow.memory.util.ByteFunctionHelpers;
21+
import org.apache.arrow.vector.BaseFixedWidthVector;
22+
import org.apache.arrow.vector.BaseVariableWidthVector;
23+
import org.apache.arrow.vector.ValueVector;
24+
import org.apache.arrow.vector.complex.BaseRepeatedValueVector;
25+
import org.apache.arrow.vector.complex.FixedSizeListVector;
26+
import org.apache.arrow.vector.complex.ListVector;
27+
import org.apache.arrow.vector.complex.NonNullableStructVector;
28+
29+
/**
30+
* Utility to compare two single values in two vectors with same type.
31+
*/
32+
public class CompareUtility {
33+
34+
/**
35+
* Compare {@link BaseFixedWidthVector} value equals with given indices and vectors.
36+
* @param left left vector to compare
37+
* @param leftIndex left index
38+
* @param right right vector to compare
39+
* @param rightIndex right index
40+
* @return true if equals, otherwise false
41+
*/
42+
public static boolean compare(BaseFixedWidthVector left, int leftIndex, BaseFixedWidthVector right, int rightIndex) {
43+
44+
boolean isNull = left.isNull(leftIndex);
45+
46+
if (isNull != right.isNull(rightIndex)) {
47+
return false;
48+
}
49+
50+
int typeWidth = left.getTypeWidth();
51+
if (!isNull) {
52+
int startByteLeft = typeWidth * leftIndex;
53+
int endByteLeft = typeWidth * (leftIndex + 1);
54+
55+
int startByteRight = typeWidth * rightIndex;
56+
int endByteRight = typeWidth * (rightIndex + 1);
57+
58+
int ret = ByteFunctionHelpers.equal(left.getDataBuffer(), startByteLeft, endByteLeft,
59+
right.getDataBuffer(), startByteRight, endByteRight);
60+
61+
if (ret == 0) {
62+
return false;
63+
}
64+
65+
}
66+
return true;
67+
}
68+
69+
/**
70+
* Compare {@link BaseVariableWidthVector} value equals with given indices and vectors.
71+
* @param left left vector to compare
72+
* @param leftIndex left index
73+
* @param right right vector to compare
74+
* @param rightIndex right index
75+
* @return true if equals, otherwise false
76+
*/
77+
public static boolean compare(BaseVariableWidthVector left, int leftIndex,
78+
BaseVariableWidthVector right, int rightIndex) {
79+
80+
boolean isNull = left.isNull(leftIndex);
81+
if (isNull != right.isNull(rightIndex)) {
82+
return false;
83+
}
84+
85+
int offsetWidth = BaseVariableWidthVector.OFFSET_WIDTH;
86+
87+
if (!isNull) {
88+
final int start1 = left.getOffsetBuffer().getInt(leftIndex * offsetWidth);
89+
final int end1 = left.getOffsetBuffer().getInt((leftIndex + 1) * offsetWidth);
90+
91+
final int start2 = right.getOffsetBuffer().getInt(rightIndex * offsetWidth);
92+
final int end2 = right.getOffsetBuffer().getInt((rightIndex + 1) * offsetWidth);
93+
94+
int ret = ByteFunctionHelpers.equal(left.getDataBuffer(), start1, end1,
95+
right.getDataBuffer(), start2, end2);
96+
97+
if (ret == 0) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
104+
/**
105+
* Compare {@link ListVector} value equals with given indices and vectors.
106+
* @param left left vector to compare
107+
* @param leftIndex left index
108+
* @param right right vector to compare
109+
* @param rightIndex right index
110+
* @return true if equals, otherwise false
111+
*/
112+
public static boolean compare(ListVector left, int leftIndex, ListVector right, int rightIndex) {
113+
114+
boolean isNull = left.isNull(leftIndex);
115+
if (isNull != right.isNull(rightIndex)) {
116+
return false;
117+
}
118+
119+
int offsetWidth = BaseRepeatedValueVector.OFFSET_WIDTH;
120+
121+
if (!isNull) {
122+
final int startByteLeft = left.getOffsetBuffer().getInt(leftIndex * offsetWidth);
123+
final int endByteLeft = left.getOffsetBuffer().getInt((leftIndex + 1) * offsetWidth);
124+
125+
final int startByteRight = right.getOffsetBuffer().getInt(rightIndex * offsetWidth);
126+
final int endByteRight = right.getOffsetBuffer().getInt((rightIndex + 1) * offsetWidth);
127+
128+
if ((endByteLeft - startByteLeft) != (endByteRight - startByteRight)) {
129+
return false;
130+
}
131+
132+
ValueVector dataVector1 = left.getDataVector();
133+
ValueVector dataVector2 = right.getDataVector();
134+
135+
if (!dataVector1.accept(new RangeEqualsVisitor(dataVector2, startByteLeft,
136+
startByteRight, (endByteLeft - startByteLeft)))) {
137+
return false;
138+
}
139+
}
140+
return true;
141+
}
142+
143+
/**
144+
* Compare {@link FixedSizeListVector} value equals with given indices and vectors.
145+
* @param left left vector to compare
146+
* @param leftIndex left index
147+
* @param right right vector to compare
148+
* @param rightIndex right index
149+
* @return true if equals, otherwise false
150+
*/
151+
public static boolean compare(FixedSizeListVector left, int leftIndex, FixedSizeListVector right, int rightIndex) {
152+
boolean isNull = left.isNull(leftIndex);
153+
if (isNull != right.isNull(rightIndex)) {
154+
return false;
155+
}
156+
157+
int listSize = left.getListSize();
158+
159+
if (!isNull) {
160+
final int start1 = leftIndex * listSize;
161+
final int end1 = (leftIndex + 1) * listSize;
162+
163+
final int start2 = rightIndex * listSize;
164+
final int end2 = (rightIndex + 1) * listSize;
165+
166+
if ((end1 - start1) != (end2 - start2)) {
167+
return false;
168+
}
169+
170+
ValueVector dataVector1 = left.getDataVector();
171+
ValueVector dataVector2 = right.getDataVector();
172+
173+
if (!dataVector1.accept(new RangeEqualsVisitor(dataVector2, start1, start2, (end1 - start1)))) {
174+
return false;
175+
}
176+
}
177+
return true;
178+
}
179+
180+
/**
181+
* Compare {@link NonNullableStructVector} value equals with given indices and vectors.
182+
* @param left left vector to compare
183+
* @param leftIndex left index
184+
* @param right right vector to compare
185+
* @param rightIndex right index
186+
* @return true if equals, otherwise false
187+
*/
188+
public static boolean compare(NonNullableStructVector left, int leftIndex, NonNullableStructVector right,
189+
int rightIndex) {
190+
boolean isNull = left.isNull(leftIndex);
191+
if (isNull != right.isNull(rightIndex)) {
192+
return false;
193+
}
194+
195+
if (!isNull) {
196+
if (!left.getChildFieldNames().equals(right.getChildFieldNames())) {
197+
return false;
198+
}
199+
200+
for (String name : left.getChildFieldNames()) {
201+
if (!left.getChild(name).equals(leftIndex, right.getChild(name), rightIndex)) {
202+
return false;
203+
}
204+
}
205+
}
206+
return true;
207+
}
208+
209+
/**
210+
* Check whether indices are valid.
211+
*/
212+
public static boolean checkIndices(ValueVector left, int leftIndex, ValueVector right, int rightIndex) {
213+
if (leftIndex < 0 || rightIndex < 0) {
214+
throw new IllegalStateException(String.format("indices must be non negative, left index: %s, right index: %s",
215+
leftIndex, rightIndex));
216+
}
217+
218+
if (leftIndex >= left.getValueCount() || rightIndex >= right.getValueCount()) {
219+
throw new IllegalStateException(String.format("indices mush be less than valueCount, left index: %s, " +
220+
"right index: %s, left valueCount: %s, right valueCount: %s", leftIndex, left.getValueCount(),
221+
rightIndex, right.getValueCount()));
222+
}
223+
224+
return true;
225+
}
226+
}

0 commit comments

Comments
 (0)