cloud-fan commented on code in PR #48225:
URL: https://github.com/apache/spark/pull/48225#discussion_r1816673858
##########
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/ArrayExpressionUtils.java:
##########
@@ -19,158 +19,191 @@
import java.util.Arrays;
import java.util.Comparator;
-import org.apache.spark.sql.catalyst.util.ArrayData;
import org.apache.spark.sql.catalyst.util.SQLOrderingUtil;
-import org.apache.spark.sql.types.ByteType$;
-import org.apache.spark.sql.types.BooleanType$;
-import org.apache.spark.sql.types.DataType;
-import org.apache.spark.sql.types.DoubleType$;
-import org.apache.spark.sql.types.FloatType$;
-import org.apache.spark.sql.types.IntegerType$;
-import org.apache.spark.sql.types.LongType$;
-import org.apache.spark.sql.types.ShortType$;
public class ArrayExpressionUtils {
- private static final Comparator<Object> booleanComp = (o1, o2) -> {
+ // comparator
+ // Boolean ascending nullable comparator
+ private static final Comparator<Boolean> booleanComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- boolean c1 = (Boolean) o1, c2 = (Boolean) o2;
- return c1 == c2 ? 0 : (c1 ? 1 : -1);
+ return o1.equals(o2) ? 0 : (o1 ? 1 : -1);
};
- private static final Comparator<Object> byteComp = (o1, o2) -> {
+ // Byte ascending nullable comparator
+ private static final Comparator<Byte> byteComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- byte c1 = (Byte) o1, c2 = (Byte) o2;
- return Byte.compare(c1, c2);
+ return Byte.compare(o1, o2);
};
- private static final Comparator<Object> shortComp = (o1, o2) -> {
+ // Short ascending nullable comparator
+ private static final Comparator<Short> shortComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- short c1 = (Short) o1, c2 = (Short) o2;
- return Short.compare(c1, c2);
+ return Short.compare(o1, o2);
};
- private static final Comparator<Object> integerComp = (o1, o2) -> {
+ // Integer ascending nullable comparator
+ private static final Comparator<Integer> integerComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- int c1 = (Integer) o1, c2 = (Integer) o2;
- return Integer.compare(c1, c2);
+ return Integer.compare(o1, o2);
};
- private static final Comparator<Object> longComp = (o1, o2) -> {
+ // Long ascending nullable comparator
+ private static final Comparator<Long> longComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- long c1 = (Long) o1, c2 = (Long) o2;
- return Long.compare(c1, c2);
+ return Long.compare(o1, o2);
};
- private static final Comparator<Object> floatComp = (o1, o2) -> {
+ // Float ascending nullable comparator
+ private static final Comparator<Float> floatComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- float c1 = (Float) o1, c2 = (Float) o2;
- return SQLOrderingUtil.compareFloats(c1, c2);
+ return SQLOrderingUtil.compareFloats(o1, o2);
};
- private static final Comparator<Object> doubleComp = (o1, o2) -> {
+ // Double ascending nullable comparator
+ private static final Comparator<Double> doubleComp = (o1, o2) -> {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
- double c1 = (Double) o1, c2 = (Double) o2;
- return SQLOrderingUtil.compareDoubles(c1, c2);
+ return SQLOrderingUtil.compareDoubles(o1, o2);
};
- public static int binarySearchNullSafe(ArrayData data, Boolean value) {
- return Arrays.binarySearch(data.toObjectArray(BooleanType$.MODULE$),
value, booleanComp);
+ // boolean
+ // boolean non-nullable
+ public static int binarySearch(boolean[] data, boolean value) {
+ int low = 0;
+ int high = data.length - 1;
+
+ while (low <= high) {
+ int mid = (low + high) >>> 1;
+ boolean midVal = data[mid];
+
+ int cmp = booleanComp.compare(midVal, value);
Review Comment:
nit: we can make it more efficient by avoiding the boxing in `booleanComp`
```
if (value == midVal) {
return mid
} else if (value) {
low = mid + 1;
} else {
high = mid - 1;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]