This is an automated email from the ASF dual-hosted git repository. mbudiu pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite.git
commit e1732c4cc4342c9e09fdecce901e40d88e76d7a7 Author: Mihai Budiu <[email protected]> AuthorDate: Sun Jan 11 10:04:51 2026 -0800 [CALCITE-7367] NULLS FIRST throws ClassCastException when sorting arrays Signed-off-by: Mihai Budiu <[email protected]> --- core/src/test/resources/sql/sort.iq | 43 ++++++++++++++++++++++ .../apache/calcite/linq4j/function/Functions.java | 16 ++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/src/test/resources/sql/sort.iq b/core/src/test/resources/sql/sort.iq index ce34c82d09..fb69970e41 100644 --- a/core/src/test/resources/sql/sort.iq +++ b/core/src/test/resources/sql/sort.iq @@ -422,4 +422,47 @@ select * from "hr"."emps" limit 3000000000 offset 2500000000; java.lang.ArithmeticException: Integer overflow: 2500000000 is out of range for INT !error +# [CALCITE-7367] NULLS FIRST throws ClassCastException when sorting arrays +select * from +(values + (2, array[null, 3]), + (3, array[3, 4]), + (1, array[1, 2]), + (4, array[4, 5]), + (5, cast(null as integer array))) as t(id, arr) +order by arr nulls first; ++----+-----------+ +| ID | ARR | ++----+-----------+ +| 5 | | +| 1 | [1, 2] | +| 3 | [3, 4] | +| 4 | [4, 5] | +| 2 | [null, 3] | ++----+-----------+ +(5 rows) + +!ok + +select * from +(values + (2, array[null, 3]), + (3, array[3, 4]), + (1, array[1, 2]), + (4, array[4, 5]), + (5, cast(null as integer array))) as t(id, arr) +order by arr desc nulls first; ++----+-----------+ +| ID | ARR | ++----+-----------+ +| 5 | | +| 2 | [null, 3] | +| 4 | [4, 5] | +| 3 | [3, 4] | +| 1 | [1, 2] | ++----+-----------+ +(5 rows) + +!ok + # End sort.iq diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/function/Functions.java b/linq4j/src/main/java/org/apache/calcite/linq4j/function/Functions.java index 5f7aa258d4..73a06f42bd 100644 --- a/linq4j/src/main/java/org/apache/calcite/linq4j/function/Functions.java +++ b/linq4j/src/main/java/org/apache/calcite/linq4j/function/Functions.java @@ -537,8 +537,8 @@ private static final class SelectorEqualityComparer<T, T2> /** Nulls first comparator. */ private static class NullsFirstComparator - implements Comparator<Comparable>, Serializable { - @Override public int compare(Comparable o1, Comparable o2) { + implements Comparator<Object>, Serializable { + @Override public int compare(@Nullable Object o1, @Nullable Object o2) { if (o1 == o2) { return 0; } @@ -548,8 +548,16 @@ private static class NullsFirstComparator if (o2 == null) { return 1; } - //noinspection unchecked - return o1.compareTo(o2); + if (o1 instanceof Comparable && o2 instanceof Comparable) { + //noinspection unchecked + return ((Comparable) o1).compareTo(o2); + } else if (o1 instanceof List && o2 instanceof List) { + return compareLists((List<?>) o1, (List<?>) o2); + } else if (o1 instanceof Object[] && o2 instanceof Object[]) { + return compareObjectArrays((Object[]) o1, (Object[]) o2); + } else { + throw new IllegalArgumentException(); + } } }
