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 ;
4344
4445  /** 
4546   * Constructs a new instance. 
@@ -49,29 +50,56 @@ public RangeEqualsVisitor(ValueVector right, int leftStart, int rightStart, int
4950    this .rightStart  = rightStart ;
5051    this .right  = right ;
5152    this .length  = length ;
53+     Preconditions .checkArgument (length  >= 0 , "length must be non negative" );
54+   }
55+ 
56+   /** 
57+    * Reset start indices and length for reuse purpose. 
58+    */ 
59+   public  void  reset (int  leftStart , int  rightStart , int  length ) {
60+     this .leftStart  = leftStart ;
61+     this .rightStart  = rightStart ;
62+     this .length  = length ;
63+   }
64+ 
65+   private  void  validateIndices (ValueVector  left ) {
66+     Preconditions .checkArgument (leftStart  >= 0  && leftStart  < left .getValueCount (),
67+         "leftStart %s out of range[0, %s]:" , 0 , left .getValueCount ());
68+     Preconditions .checkArgument ((leftStart  + length ) <= left .getValueCount (),
69+         "(leftStart + length) %s out of range[0, %s]:" , 0 , left .getValueCount ());
70+     Preconditions .checkArgument (rightStart  >= 0  && rightStart  < right .getValueCount (),
71+         "rightStart %s out of range[0, %s]:" , 0 , right .getValueCount ());
72+     Preconditions .checkArgument ((rightStart  + length ) <= right .getValueCount (),
73+         "(rightStart + length) %s out of range[0, %s]:" , 0 , right .getValueCount ());
5274  }
5375
5476  public  boolean  visit (BaseFixedWidthVector  left ) {
77+     validateIndices (left );
5578    return  compareBaseFixedWidthVectors (left );
5679  }
5780
5881  public  boolean  visit (BaseVariableWidthVector  left ) {
82+     validateIndices (left );
5983    return  compareBaseVariableWidthVectors (left );
6084  }
6185
6286  public  boolean  visit (ListVector  left ) {
87+     validateIndices (left );
6388    return  compareListVectors (left );
6489  }
6590
6691  public  boolean  visit (FixedSizeListVector  left ) {
92+     validateIndices (left );
6793    return  compareFixedSizeListVectors (left );
6894  }
6995
7096  public  boolean  visit (NonNullableStructVector  left ) {
97+     validateIndices (left );
7198    return  compareStructVectors (left );
7299  }
73100
74101  public  boolean  visit (UnionVector  left ) {
102+     validateIndices (left );
75103    return  compareUnionVectors (left );
76104  }
77105
@@ -152,14 +180,14 @@ protected boolean compareBaseFixedWidthVectors(BaseFixedWidthVector left) {
152180
153181      int  typeWidth  = left .getTypeWidth ();
154182      if  (!isNull ) {
155-         int  startByteLeft  = typeWidth  * leftIndex ;
156-         int  endByteLeft  = typeWidth  * (leftIndex  + 1 );
183+         int  startIndexLeft  = typeWidth  * leftIndex ;
184+         int  endIndexLeft  = typeWidth  * (leftIndex  + 1 );
157185
158-         int  startByteRight  = typeWidth  * rightIndex ;
159-         int  endByteRight  = typeWidth  * (rightIndex  + 1 );
186+         int  startIndexRight  = typeWidth  * rightIndex ;
187+         int  endIndexRight  = typeWidth  * (rightIndex  + 1 );
160188
161-         int  ret  = ByteFunctionHelpers .equal (left .getDataBuffer (), startByteLeft ,  endByteLeft ,
162-             right .getDataBuffer (), startByteRight ,  endByteRight );
189+         int  ret  = ByteFunctionHelpers .equal (left .getDataBuffer (), startIndexLeft ,  endIndexLeft ,
190+             right .getDataBuffer (), startIndexRight ,  endIndexRight );
163191
164192        if  (ret  == 0 ) {
165193          return  false ;
@@ -186,14 +214,14 @@ protected boolean compareBaseVariableWidthVectors(BaseVariableWidthVector left)
186214      int  offsetWidth  = BaseVariableWidthVector .OFFSET_WIDTH ;
187215
188216      if  (!isNull ) {
189-         final  int  startByteLeft  = left .getOffsetBuffer ().getInt (leftIndex  * offsetWidth );
190-         final  int  endByteLeft  = left .getOffsetBuffer ().getInt ((leftIndex  + 1 ) * offsetWidth );
217+         final  int  startIndexLeft  = left .getOffsetBuffer ().getInt (leftIndex  * offsetWidth );
218+         final  int  endIndexLeft  = left .getOffsetBuffer ().getInt ((leftIndex  + 1 ) * offsetWidth );
191219
192-         final  int  startByteRight  = right .getOffsetBuffer ().getInt (rightIndex  * offsetWidth );
193-         final  int  endByteRight  = right .getOffsetBuffer ().getInt ((rightIndex  + 1 ) * offsetWidth );
220+         final  int  startIndexRight  = right .getOffsetBuffer ().getInt (rightIndex  * offsetWidth );
221+         final  int  endIndexRight  = right .getOffsetBuffer ().getInt ((rightIndex  + 1 ) * offsetWidth );
194222
195-         int  ret  = ByteFunctionHelpers .equal (left .getDataBuffer (), startByteLeft ,  endByteLeft ,
196-             right .getDataBuffer (), startByteRight ,  endByteRight );
223+         int  ret  = ByteFunctionHelpers .equal (left .getDataBuffer (), startIndexLeft ,  endIndexLeft ,
224+             right .getDataBuffer (), startIndexRight ,  endIndexRight );
197225
198226        if  (ret  == 0 ) {
199227          return  false ;
@@ -220,21 +248,21 @@ protected boolean compareListVectors(ListVector left) {
220248      int  offsetWidth  = BaseRepeatedValueVector .OFFSET_WIDTH ;
221249
222250      if  (!isNull ) {
223-         final  int  startByteLeft  = left .getOffsetBuffer ().getInt (leftIndex  * offsetWidth );
224-         final  int  endByteLeft  = left .getOffsetBuffer ().getInt ((leftIndex  + 1 ) * offsetWidth );
251+         final  int  startIndexLeft  = left .getOffsetBuffer ().getInt (leftIndex  * offsetWidth );
252+         final  int  endIndexLeft  = left .getOffsetBuffer ().getInt ((leftIndex  + 1 ) * offsetWidth );
225253
226-         final  int  startByteRight  = right .getOffsetBuffer ().getInt (rightIndex  * offsetWidth );
227-         final  int  endByteRight  = right .getOffsetBuffer ().getInt ((rightIndex  + 1 ) * offsetWidth );
254+         final  int  startIndexRight  = right .getOffsetBuffer ().getInt (rightIndex  * offsetWidth );
255+         final  int  endIndexRight  = right .getOffsetBuffer ().getInt ((rightIndex  + 1 ) * offsetWidth );
228256
229-         if  ((endByteLeft  - startByteLeft ) != (endByteRight  - startByteRight )) {
257+         if  ((endIndexLeft  - startIndexLeft ) != (endIndexRight  - startIndexRight )) {
230258          return  false ;
231259        }
232260
233261        ValueVector  leftDataVector  = left .getDataVector ();
234262        ValueVector  rightDataVector  = ((ListVector )right ).getDataVector ();
235263
236-         if  (!leftDataVector .accept (new  RangeEqualsVisitor (rightDataVector , startByteLeft ,
237-             startByteRight , (endByteLeft  - startByteLeft )))) {
264+         if  (!leftDataVector .accept (new  RangeEqualsVisitor (rightDataVector , startIndexLeft ,
265+             startIndexRight , (endIndexLeft  - startIndexLeft )))) {
238266          return  false ;
239267        }
240268      }
@@ -263,21 +291,21 @@ protected boolean compareFixedSizeListVectors(FixedSizeListVector left) {
263291      int  listSize  = left .getListSize ();
264292
265293      if  (!isNull ) {
266-         final  int  startByteLeft  = leftIndex  * listSize ;
267-         final  int  endByteLeft  = (leftIndex  + 1 ) * listSize ;
294+         final  int  startIndexLeft  = leftIndex  * listSize ;
295+         final  int  endIndexLeft  = (leftIndex  + 1 ) * listSize ;
268296
269-         final  int  startByteRight  = rightIndex  * listSize ;
270-         final  int  endByteRight  = (rightIndex  + 1 ) * listSize ;
297+         final  int  startIndexRight  = rightIndex  * listSize ;
298+         final  int  endIndexRight  = (rightIndex  + 1 ) * listSize ;
271299
272-         if  ((endByteLeft  - startByteLeft ) != (endByteRight  - startByteRight )) {
300+         if  ((endIndexLeft  - startIndexLeft ) != (endIndexRight  - startIndexRight )) {
273301          return  false ;
274302        }
275303
276304        ValueVector  leftDataVector  = left .getDataVector ();
277305        ValueVector  rightDataVector  = ((FixedSizeListVector )right ).getDataVector ();
278306
279-         if  (!leftDataVector .accept (new  RangeEqualsVisitor (rightDataVector , startByteLeft ,  startByteRight ,
280-             (endByteLeft  - startByteLeft )))) {
307+         if  (!leftDataVector .accept (new  RangeEqualsVisitor (rightDataVector , startIndexLeft ,  startIndexRight ,
308+             (endIndexLeft  - startIndexLeft )))) {
281309          return  false ;
282310        }
283311      }
0 commit comments