2020import java .util .List ;
2121
2222import org .apache .arrow .memory .util .ByteFunctionHelpers ;
23+ import org .apache .arrow .util .Preconditions ;
2324import org .apache .arrow .vector .BaseFixedWidthVector ;
2425import org .apache .arrow .vector .BaseVariableWidthVector ;
2526import org .apache .arrow .vector .FieldVector ;
3738public class RangeEqualsVisitor {
3839
3940 protected final ValueVector right ;
40- protected final int leftStart ;
41- protected final int rightStart ;
42- protected final int length ;
41+ protected int leftStart ;
42+ protected int rightStart ;
43+ protected int length ;
44+
45+ protected boolean typeCheckNeeded = true ;
4346
4447 /**
4548 * Constructs a new instance.
4649 */
47- public RangeEqualsVisitor (ValueVector right , int leftStart , int rightStart , int length ) {
50+ public RangeEqualsVisitor (ValueVector right , int rightStart , int leftStart , int length , boolean typeCheckNeeded ) {
4851 this .leftStart = leftStart ;
4952 this .rightStart = rightStart ;
5053 this .right = right ;
5154 this .length = length ;
55+ this .typeCheckNeeded = typeCheckNeeded ;
56+ Preconditions .checkArgument (length >= 0 , "length must be non negative" );
57+ }
58+
59+ /**
60+ * Constructs a new instance.
61+ */
62+ public RangeEqualsVisitor (ValueVector right , int leftStart , int rightStart , int length ) {
63+ this (right , rightStart , leftStart , length , true );
64+ }
65+
66+ /**
67+ * Do some validation work, like type check and indices check.
68+ */
69+ private boolean validate (ValueVector left ) {
70+
71+ if (!compareValueVector (left , right )) {
72+ return false ;
73+ }
74+
75+ Preconditions .checkArgument (leftStart >= 0 ,
76+ "leftStart %s must be non negative." , leftStart );
77+ Preconditions .checkArgument ((leftStart + length ) <= left .getValueCount (),
78+ "(leftStart + length) %s out of range[0, %s]." , 0 , left .getValueCount ());
79+ Preconditions .checkArgument (rightStart >= 0 ,
80+ "rightStart %s must be non negative." , rightStart );
81+ Preconditions .checkArgument ((rightStart + length ) <= right .getValueCount (),
82+ "(rightStart + length) %s out of range[0, %s]." , 0 , right .getValueCount ());
83+
84+ return true ;
5285 }
5386
5487 public boolean visit (BaseFixedWidthVector left ) {
55- return compareBaseFixedWidthVectors (left );
88+ return validate ( left ) && compareBaseFixedWidthVectors (left );
5689 }
5790
5891 public boolean visit (BaseVariableWidthVector left ) {
59- return compareBaseVariableWidthVectors (left );
92+ return validate ( left ) && compareBaseVariableWidthVectors (left );
6093 }
6194
6295 public boolean visit (ListVector left ) {
63- return compareListVectors (left );
96+ return validate ( left ) && compareListVectors (left );
6497 }
6598
6699 public boolean visit (FixedSizeListVector left ) {
67- return compareFixedSizeListVectors (left );
100+ return validate ( left ) && compareFixedSizeListVectors (left );
68101 }
69102
70103 public boolean visit (NonNullableStructVector left ) {
71- return compareStructVectors (left );
104+ return validate ( left ) && compareStructVectors (left );
72105 }
73106
74107 public boolean visit (UnionVector left ) {
75- return compareUnionVectors (left );
108+ return validate ( left ) && compareUnionVectors (left );
76109 }
77110
78111 public boolean visit (ZeroVector left ) {
79- return compareValueVector (left , right );
112+ return validate (left );
80113 }
81114
82115 public boolean visit (ValueVector left ) {
83116 throw new UnsupportedOperationException ();
84117 }
85118
86119 protected boolean compareValueVector (ValueVector left , ValueVector right ) {
120+ if (!typeCheckNeeded ) {
121+ return true ;
122+ }
87123 return left .getField ().getType ().equals (right .getField ().getType ());
88124 }
89125
90126 protected boolean compareUnionVectors (UnionVector left ) {
91127
92- if (!compareValueVector (left , right )) {
93- return false ;
94- }
95-
96128 UnionVector rightVector = (UnionVector ) right ;
97129
98130 List <FieldVector > leftChildren = left .getChildrenFromFields ();
@@ -113,9 +145,6 @@ protected boolean compareUnionVectors(UnionVector left) {
113145 }
114146
115147 protected boolean compareStructVectors (NonNullableStructVector left ) {
116- if (!compareValueVector (left , right )) {
117- return false ;
118- }
119148
120149 NonNullableStructVector rightVector = (NonNullableStructVector ) right ;
121150
@@ -136,10 +165,6 @@ protected boolean compareStructVectors(NonNullableStructVector left) {
136165
137166 protected boolean compareBaseFixedWidthVectors (BaseFixedWidthVector left ) {
138167
139- if (!compareValueVector (left , right )) {
140- return false ;
141- }
142-
143168 for (int i = 0 ; i < length ; i ++) {
144169 int leftIndex = leftStart + i ;
145170 int rightIndex = rightStart + i ;
@@ -152,14 +177,14 @@ protected boolean compareBaseFixedWidthVectors(BaseFixedWidthVector left) {
152177
153178 int typeWidth = left .getTypeWidth ();
154179 if (!isNull ) {
155- int startByteLeft = typeWidth * leftIndex ;
156- int endByteLeft = typeWidth * (leftIndex + 1 );
180+ int startIndexLeft = typeWidth * leftIndex ;
181+ int endIndexLeft = typeWidth * (leftIndex + 1 );
157182
158- int startByteRight = typeWidth * rightIndex ;
159- int endByteRight = typeWidth * (rightIndex + 1 );
183+ int startIndexRight = typeWidth * rightIndex ;
184+ int endIndexRight = typeWidth * (rightIndex + 1 );
160185
161- int ret = ByteFunctionHelpers .equal (left .getDataBuffer (), startByteLeft , endByteLeft ,
162- right .getDataBuffer (), startByteRight , endByteRight );
186+ int ret = ByteFunctionHelpers .equal (left .getDataBuffer (), startIndexLeft , endIndexLeft ,
187+ right .getDataBuffer (), startIndexRight , endIndexRight );
163188
164189 if (ret == 0 ) {
165190 return false ;
@@ -170,9 +195,6 @@ protected boolean compareBaseFixedWidthVectors(BaseFixedWidthVector left) {
170195 }
171196
172197 protected boolean compareBaseVariableWidthVectors (BaseVariableWidthVector left ) {
173- if (!compareValueVector (left , right )) {
174- return false ;
175- }
176198
177199 for (int i = 0 ; i < length ; i ++) {
178200 int leftIndex = leftStart + i ;
@@ -186,14 +208,14 @@ protected boolean compareBaseVariableWidthVectors(BaseVariableWidthVector left)
186208 int offsetWidth = BaseVariableWidthVector .OFFSET_WIDTH ;
187209
188210 if (!isNull ) {
189- final int startByteLeft = left .getOffsetBuffer ().getInt (leftIndex * offsetWidth );
190- final int endByteLeft = left .getOffsetBuffer ().getInt ((leftIndex + 1 ) * offsetWidth );
211+ final int startIndexLeft = left .getOffsetBuffer ().getInt (leftIndex * offsetWidth );
212+ final int endIndexLeft = left .getOffsetBuffer ().getInt ((leftIndex + 1 ) * offsetWidth );
191213
192- final int startByteRight = right .getOffsetBuffer ().getInt (rightIndex * offsetWidth );
193- final int endByteRight = right .getOffsetBuffer ().getInt ((rightIndex + 1 ) * offsetWidth );
214+ final int startIndexRight = right .getOffsetBuffer ().getInt (rightIndex * offsetWidth );
215+ final int endIndexRight = right .getOffsetBuffer ().getInt ((rightIndex + 1 ) * offsetWidth );
194216
195- int ret = ByteFunctionHelpers .equal (left .getDataBuffer (), startByteLeft , endByteLeft ,
196- right .getDataBuffer (), startByteRight , endByteRight );
217+ int ret = ByteFunctionHelpers .equal (left .getDataBuffer (), startIndexLeft , endIndexLeft ,
218+ right .getDataBuffer (), startIndexRight , endIndexRight );
197219
198220 if (ret == 0 ) {
199221 return false ;
@@ -204,9 +226,6 @@ protected boolean compareBaseVariableWidthVectors(BaseVariableWidthVector left)
204226 }
205227
206228 protected boolean compareListVectors (ListVector left ) {
207- if (!compareValueVector (left , right )) {
208- return false ;
209- }
210229
211230 for (int i = 0 ; i < length ; i ++) {
212231 int leftIndex = leftStart + i ;
@@ -220,21 +239,21 @@ protected boolean compareListVectors(ListVector left) {
220239 int offsetWidth = BaseRepeatedValueVector .OFFSET_WIDTH ;
221240
222241 if (!isNull ) {
223- final int startByteLeft = left .getOffsetBuffer ().getInt (leftIndex * offsetWidth );
224- final int endByteLeft = left .getOffsetBuffer ().getInt ((leftIndex + 1 ) * offsetWidth );
242+ final int startIndexLeft = left .getOffsetBuffer ().getInt (leftIndex * offsetWidth );
243+ final int endIndexLeft = left .getOffsetBuffer ().getInt ((leftIndex + 1 ) * offsetWidth );
225244
226- final int startByteRight = right .getOffsetBuffer ().getInt (rightIndex * offsetWidth );
227- final int endByteRight = right .getOffsetBuffer ().getInt ((rightIndex + 1 ) * offsetWidth );
245+ final int startIndexRight = right .getOffsetBuffer ().getInt (rightIndex * offsetWidth );
246+ final int endIndexRight = right .getOffsetBuffer ().getInt ((rightIndex + 1 ) * offsetWidth );
228247
229- if ((endByteLeft - startByteLeft ) != (endByteRight - startByteRight )) {
248+ if ((endIndexLeft - startIndexLeft ) != (endIndexRight - startIndexRight )) {
230249 return false ;
231250 }
232251
233252 ValueVector leftDataVector = left .getDataVector ();
234253 ValueVector rightDataVector = ((ListVector )right ).getDataVector ();
235254
236- if (!leftDataVector .accept (new RangeEqualsVisitor (rightDataVector , startByteLeft ,
237- startByteRight , (endByteLeft - startByteLeft )))) {
255+ if (!leftDataVector .accept (new RangeEqualsVisitor (rightDataVector , startIndexLeft ,
256+ startIndexRight , (endIndexLeft - startIndexLeft )))) {
238257 return false ;
239258 }
240259 }
@@ -243,9 +262,6 @@ protected boolean compareListVectors(ListVector left) {
243262 }
244263
245264 protected boolean compareFixedSizeListVectors (FixedSizeListVector left ) {
246- if (!compareValueVector (left , right )) {
247- return false ;
248- }
249265
250266 if (left .getListSize () != ((FixedSizeListVector )right ).getListSize ()) {
251267 return false ;
@@ -263,26 +279,25 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) {
263279 int listSize = left .getListSize ();
264280
265281 if (!isNull ) {
266- final int startByteLeft = leftIndex * listSize ;
267- final int endByteLeft = (leftIndex + 1 ) * listSize ;
282+ final int startIndexLeft = leftIndex * listSize ;
283+ final int endIndexLeft = (leftIndex + 1 ) * listSize ;
268284
269- final int startByteRight = rightIndex * listSize ;
270- final int endByteRight = (rightIndex + 1 ) * listSize ;
285+ final int startIndexRight = rightIndex * listSize ;
286+ final int endIndexRight = (rightIndex + 1 ) * listSize ;
271287
272- if ((endByteLeft - startByteLeft ) != (endByteRight - startByteRight )) {
288+ if ((endIndexLeft - startIndexLeft ) != (endIndexRight - startIndexRight )) {
273289 return false ;
274290 }
275291
276292 ValueVector leftDataVector = left .getDataVector ();
277293 ValueVector rightDataVector = ((FixedSizeListVector )right ).getDataVector ();
278294
279- if (!leftDataVector .accept (new RangeEqualsVisitor (rightDataVector , startByteLeft , startByteRight ,
280- (endByteLeft - startByteLeft )))) {
295+ if (!leftDataVector .accept (new RangeEqualsVisitor (rightDataVector , startIndexLeft , startIndexRight ,
296+ (endIndexLeft - startIndexLeft )))) {
281297 return false ;
282298 }
283299 }
284300 }
285301 return true ;
286302 }
287-
288303}
0 commit comments