|  | 
|  | 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