This is an automated email from the ASF dual-hosted git repository.

vldpyatkov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ab9cc96452 IGNITE-29203 SQL Calcite: Fix equality, hashing and 
ordering of keys containing ROW and ARRAY values (#13742)
0ab9cc96452 is described below

commit 0ab9cc96452f4c45158c2447391968e18aee0a9f
Author: Vladislav Pyatkov <[email protected]>
AuthorDate: Mon Oct 26 19:02:42 2026 +0300

    IGNITE-29203 SQL Calcite: Fix equality, hashing and ordering of keys 
containing ROW and ARRAY values (#13742)
---
 .../calcite/exec/exp/ExpressionFactoryImpl.java    |  14 ++
 .../query/calcite/exec/exp/agg/GroupKey.java       |   6 +-
 .../query/calcite/trait/DistributionFunction.java  |   5 +-
 .../query/calcite/util/SqlValueUtils.java          | 149 ++++++++++++++++
 .../integration/RowKeyEqualityIntegrationTest.java | 189 +++++++++++++++++++++
 .../query/calcite/util/SqlValueUtilsTest.java      | 167 ++++++++++++++++++
 .../ignite/testsuites/IntegrationTestSuite.java    |   2 +
 .../apache/ignite/testsuites/UtilTestSuite.java    |   2 +
 8 files changed, 529 insertions(+), 5 deletions(-)

diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ExpressionFactoryImpl.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ExpressionFactoryImpl.java
index 4af8a6bda03..35e05df36d6 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ExpressionFactoryImpl.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ExpressionFactoryImpl.java
@@ -39,6 +39,7 @@ import com.google.common.primitives.Primitives;
 import org.apache.calcite.DataContext;
 import org.apache.calcite.adapter.enumerable.EnumUtils;
 import org.apache.calcite.linq4j.function.Function1;
+import org.apache.calcite.linq4j.function.Functions;
 import org.apache.calcite.linq4j.tree.BlockBuilder;
 import org.apache.calcite.linq4j.tree.Expression;
 import org.apache.calcite.linq4j.tree.Expressions;
@@ -257,9 +258,22 @@ public class ExpressionFactoryImpl<Row> implements 
ExpressionFactory<Row> {
     /** */
     @SuppressWarnings("rawtypes")
     private static int compare(Object o1, Object o2, int nullComparison) {
+        if (o1 == o2)
+            return 0;
+        else if (o1 == null)
+            return nullComparison;
+        else if (o2 == null)
+            return -nullComparison;
+
         if (BinaryUtils.isBinaryObjectImpl(o1))
             return compareBinary(o1, o2, nullComparison);
 
+        if (o1 instanceof List && o2 instanceof List)
+            return Functions.compareLists((List<?>)o1, (List<?>)o2);
+
+        if (o1 instanceof Object[] && o2 instanceof Object[])
+            return Functions.compareObjectArrays((Object[])o1, (Object[])o2);
+
         final Comparable c1 = (Comparable)o1;
         final Comparable c2 = (Comparable)o2;
         return RelFieldCollation.compare(c1, c2, nullComparison);
diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/GroupKey.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/GroupKey.java
index c0ef2f9cfff..c36770de02e 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/GroupKey.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/GroupKey.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.internal.processors.query.calcite.exec.exp.agg;
 
-import java.util.Objects;
 import com.google.common.collect.ImmutableList;
 import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryRawReader;
@@ -27,6 +26,7 @@ import org.apache.ignite.binary.BinaryWriter;
 import org.apache.ignite.binary.Binarylizable;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.ArrayRowHandler;
 import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.util.SqlValueUtils;
 import org.jetbrains.annotations.Nullable;
 
 /**
@@ -97,7 +97,7 @@ public class GroupKey<Row> implements Binarylizable {
             return false;
 
         for (int i = 0; i < colCnt; i++) {
-            if (!Objects.equals(hnd.get(i, row), other.hnd.get(i, other.row)))
+            if (!SqlValueUtils.deepEquals(hnd.get(i, row), other.hnd.get(i, 
other.row)))
                 return false;
         }
 
@@ -109,7 +109,7 @@ public class GroupKey<Row> implements Binarylizable {
         int hashCode = 0;
 
         for (int i = 0; i < hnd.columnCount(row); i++)
-            hashCode = hashCode * 31 + Objects.hashCode(hnd.get(i, row));
+            hashCode = hashCode * 31 + SqlValueUtils.deepHashCode(hnd.get(i, 
row));
 
         return hashCode;
     }
diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/trait/DistributionFunction.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/trait/DistributionFunction.java
index d5368e39d2b..447955e818c 100644
--- 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/trait/DistributionFunction.java
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/trait/DistributionFunction.java
@@ -28,6 +28,7 @@ import org.apache.calcite.util.ImmutableIntList;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
 import 
org.apache.ignite.internal.processors.query.calcite.metadata.AffinityService;
 import 
org.apache.ignite.internal.processors.query.calcite.metadata.ColocationGroup;
+import org.apache.ignite.internal.processors.query.calcite.util.SqlValueUtils;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.U;
@@ -261,8 +262,8 @@ public abstract class DistributionFunction {
                     assert F.isEmpty(assignment) || assignment.size() == 1;
             }
 
-            AffinityAdapter<Row> aff = new 
AffinityAdapter<>(affSrvc.affinity(CU.UNDEFINED_CACHE_ID), k.toIntArray(),
-                ctx.rowHandler());
+            AffinityAdapter<Row> aff = new AffinityAdapter<>(val -> 
U.safeAbs(SqlValueUtils.deepHashCode(val)),
+                k.toIntArray(), ctx.rowHandler());
 
             return new Partitioned<>(assignments, aff);
         }
diff --git 
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtils.java
 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtils.java
new file mode 100644
index 00000000000..739adf4d02a
--- /dev/null
+++ 
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtils.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.jetbrains.annotations.Nullable;
+
+/** Content equality and hashing for SQL keys. */
+public final class SqlValueUtils {
+    /** */
+    private SqlValueUtils() {
+        // No-op.
+    }
+
+    /** Compares ROWs, ARRAYs and MAPs recursively, using scalar equality at 
the leaves. */
+    // TODO https://issues.apache.org/jira/browse/IGNITE-29207
+    // Replace calls to this method with Functions.deepEquals when upgrading 
to Calcite 1.43.0.
+    public static boolean deepEquals(@Nullable Object left, @Nullable Object 
right) {
+        if (left == right)
+            return true;
+
+        if (left == null || right == null)
+            return false;
+
+        List<?> leftList = asList(left);
+        List<?> rightList = asList(right);
+
+        if (leftList != null && rightList != null) {
+            if (leftList.size() != rightList.size())
+                return false;
+
+            for (int i = 0; i < leftList.size(); i++) {
+                if (!deepEquals(leftList.get(i), rightList.get(i)))
+                    return false;
+            }
+
+            return true;
+        }
+
+        if (leftList != null || rightList != null)
+            return false;
+
+        if (left instanceof Map && right instanceof Map)
+            return mapsEqual((Map<?, ?>)left, (Map<?, ?>)right);
+
+        if (left.getClass().isArray() && right.getClass().isArray())
+            return Arrays.deepEquals(new Object[] {left}, new Object[] 
{right});
+
+        return left.equals(right);
+    }
+
+    /** Maps are unordered; each entry must match exactly one entry, including 
when keys contain ROWs. */
+    private static boolean mapsEqual(Map<?, ?> left, Map<?, ?> right) {
+        if (left.size() != right.size())
+            return false;
+
+        List<Map.Entry<?, ?>> unmatched = new ArrayList<>(right.entrySet());
+
+        for (Map.Entry<?, ?> entry : left.entrySet()) {
+            boolean found = false;
+
+            for (int i = 0; i < unmatched.size(); i++) {
+                Map.Entry<?, ?> other = unmatched.get(i);
+
+                if (deepEquals(entry.getKey(), other.getKey()) && 
deepEquals(entry.getValue(), other.getValue())) {
+                    unmatched.remove(i);
+                    found = true;
+
+                    break;
+                }
+            }
+
+            if (!found)
+                return false;
+        }
+
+        return true;
+    }
+
+    /** Hash consistent with {@link #deepEquals}. */
+    // TODO https://issues.apache.org/jira/browse/IGNITE-29207
+    // Replace calls with Functions.deepHashCode when upgrading to Calcite 
1.43.0.
+    public static int deepHashCode(@Nullable Object val) {
+        if (val == null)
+            return 0x789d;
+
+        List<?> list = asList(val);
+
+        if (list != null) {
+            int hash = 1;
+
+            for (Object item : list)
+                hash = 31 * hash + deepHashCode(item);
+
+            return hash;
+        }
+
+        if (val instanceof Map) {
+            int hash = 0;
+
+            for (Map.Entry<?, ?> entry : ((Map<?, ?>)val).entrySet())
+                hash += deepHashCode(entry.getKey()) ^ 
deepHashCode(entry.getValue());
+
+            return hash;
+        }
+
+        if (val.getClass().isArray())
+            return Arrays.deepHashCode(new Object[] {val});
+
+        return val.hashCode();
+    }
+
+    /**
+     * Views the internal ROW ({@code Object[]}) or ARRAY ({@link List}) 
representation as a sequence.
+     * Other {@link Iterable} implementations and {@link Iterator} are not 
supported.
+     *
+     * @param val SQL value to inspect.
+     * @return List view for ROW or ARRAY values, or {@code null} for other 
values, including scalars,
+     *     maps and primitive arrays.
+     */
+    private static @Nullable List<?> asList(Object val) {
+        if (val instanceof List)
+            return (List<?>)val;
+
+        assert !(val instanceof Iterable) && !(val instanceof Iterator)
+            : "Unsupported SQL sequence representation: " + 
val.getClass().getName();
+
+        return val instanceof Object[] ? Arrays.asList((Object[])val) : null;
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RowKeyEqualityIntegrationTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RowKeyEqualityIntegrationTest.java
new file mode 100644
index 00000000000..93030362b42
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RowKeyEqualityIntegrationTest.java
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.integration;
+
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.processors.query.QueryUtils;
+import org.apache.ignite.internal.processors.query.calcite.QueryChecker;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static 
org.apache.ignite.internal.processors.query.calcite.QueryChecker.containsSubPlan;
+
+/** Checks that independently constructed ROW keys are compared and hashed by 
their contents. */
+public class RowKeyEqualityIntegrationTest extends 
AbstractBasicIntegrationTest {
+    /** Physical aggregation plans; each corresponds to one converter rule. */
+    private static final String[] AGGREGATE_VARIANTS = {
+        "ColocatedHash", "MapReduceHash", "ColocatedSort", "MapReduceSort"
+    };
+
+    /** Force hash aggregation so that ordering cannot hide an equality or 
hashing defect. */
+    private static final String HASH_AGG_HINT = "/*+ 
DISABLE_RULE('ColocatedSortAggregateConverterRule', "
+        + "'MapReduceSortAggregateConverterRule') */ ";
+
+    /** Primary key of the first record with val = 1. */
+    private int firstId;
+
+    /** Primary key of the second record with val = 1, owned by another node. 
*/
+    private int secondId;
+
+    /** Each record has a different primary node; the first two have equal 
values. */
+    @BeforeEach
+    public void createTable() {
+        sql("CREATE TABLE row_keys (id INT PRIMARY KEY, val INT)");
+
+        String cacheName = QueryUtils.createTableCacheName("PUBLIC", 
"ROW_KEYS");
+
+        firstId = primaryKey(grid(0).cache(cacheName));
+        secondId = primaryKey(grid(1).cache(cacheName));
+        int thirdId = primaryKey(grid(2).cache(cacheName));
+
+        sql("INSERT INTO row_keys VALUES (?, 1), (?, 1), (?, 2)", firstId, 
secondId, thirdId);
+    }
+
+    /** DISTINCT must retain two keys, regardless of whether the value is 
wrapped in ROW or ARRAY. */
+    @ParameterizedTest(name = "{0}: {1}")
+    @MethodSource("distinctCases")
+    public void testDistinct(String aggregate, String key) {
+        String hint = Arrays.stream(AGGREGATE_VARIANTS)
+            .filter(variant -> !variant.equals(aggregate))
+            .map(variant -> variant + "AggregateConverterRule")
+            .collect(Collectors.joining("', '", "/*+ DISABLE_RULE('", "') */ 
"));
+
+        QueryChecker checker = assertQuery("SELECT " + hint + "DISTINCT " + 
key + " FROM row_keys");
+
+        if (aggregate.startsWith("MapReduce")) {
+            checker.matches(containsSubPlan("Ignite" + 
aggregate.replace("MapReduce", "Map") + "Aggregate"))
+                .matches(containsSubPlan("Ignite" + 
aggregate.replace("MapReduce", "Reduce") + "Aggregate"));
+        }
+        else
+            checker.matches(containsSubPlan("Ignite" + aggregate + 
"Aggregate"));
+
+        checker.resultSize(2).check();
+    }
+
+    /** Every key representation is checked with all four aggregation plans. */
+    private static Stream<Arguments> distinctCases() {
+        return Arrays.stream(AGGREGATE_VARIANTS).flatMap(aggregate ->
+            Stream.of("val", "ARRAY[val]", "ROW(val, 'x')", "ARRAY[ROW(val, 
'x')]")
+                .map(key -> Arguments.of(aggregate, key)));
+    }
+
+    /** Ordering complex values must respect direction and explicit placement 
of null keys. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"ARRAY[val]", "ROW('x', val)", "ARRAY[ROW('x', 
val)]"})
+    public void testOrderBy(String key) {
+        String qry = "SELECT id FROM (SELECT id, CASE WHEN val IS NULL THEN 
NULL ELSE " + key + " END AS k "
+            + "FROM (VALUES (1, 2), (2, 1), (3, 3), (4, CAST(NULL AS 
INTEGER))) t(id, val)) ORDER BY k ";
+
+        assertQuery(qry + "ASC NULLS 
FIRST").ordered().returns(4).returns(2).returns(1).returns(3).check();
+        assertQuery(qry + "ASC NULLS 
LAST").ordered().returns(2).returns(1).returns(3).returns(4).check();
+        assertQuery(qry + "DESC NULLS 
FIRST").ordered().returns(4).returns(3).returns(1).returns(2).check();
+        assertQuery(qry + "DESC NULLS 
LAST").ordered().returns(3).returns(1).returns(2).returns(4).check();
+    }
+
+    /** Calcite orders null elements after non-null elements within an 
ascending ROW or ARRAY. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"ARRAY[val]", "ROW('x', val)", "ARRAY[ROW('x', 
val)]"})
+    public void testOrderByWithNullElements(String key) {
+        String qry = "SELECT id FROM (SELECT id, " + key + " AS k "
+            + "FROM (VALUES (1, 2), (2, 1), (3, 3), (4, CAST(NULL AS 
INTEGER))) t(id, val)) ORDER BY k ";
+
+        assertQuery(qry + 
"ASC").ordered().returns(2).returns(1).returns(3).returns(4).check();
+        assertQuery(qry + 
"DESC").ordered().returns(4).returns(3).returns(1).returns(2).check();
+    }
+
+    /** Equal keys must share a group of size two; the different key forms a 
group of size one. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"val", "ARRAY[val]", "ROW(val, 'x')", 
"ARRAY[ROW(val, 'x')]"})
+    public void testGroupBy(String key) {
+        assertQuery("SELECT " + HASH_AGG_HINT + "COUNT(*) FROM (SELECT " + key 
+ " AS k FROM row_keys) GROUP BY k")
+            .matches(containsSubPlan("HashAggregate"))
+            .returns(2L)
+            .returns(1L)
+            .check();
+    }
+
+    /** UNION must remove equal keys constructed in separate input branches. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"val", "ARRAY[val]", "ROW(val, 'x')", 
"ARRAY[ROW(val, 'x')]"})
+    public void testUnion(String key) {
+        assertQuery("SELECT " + HASH_AGG_HINT + "COUNT(*) FROM ("
+            + "SELECT " + key + " FROM row_keys WHERE id = ? UNION "
+            + "SELECT " + key + " FROM row_keys WHERE id <> ?)")
+            .withParams(firstId, firstId)
+            .returns(2L)
+            .check();
+    }
+
+    /** INTERSECT must find the equal key in both inputs and exclude the 
different key. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"val", "ARRAY[val]", "ROW(val, 'x')", 
"ARRAY[ROW(val, 'x')]"})
+    public void testIntersect(String key) {
+        assertQuery("SELECT COUNT(*) FROM ("
+            + "SELECT " + key + " FROM row_keys WHERE id = ? INTERSECT "
+            + "SELECT " + key + " FROM row_keys WHERE id <> ?)")
+            .withParams(firstId, firstId)
+            .returns(1L)
+            .check();
+    }
+
+    /** Hash joins must find the same ARRAY-of-ROW key constructed on either 
side. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {"val", "ARRAY[val]", "ROW(val, 'x')", 
"ARRAY[ROW(val, 'x')]"})
+    public void testHashJoin(String key) {
+        assertQuery("SELECT /*+ DISABLE_RULE('MergeJoinConverter', 
'NestedLoopJoinConverter', "
+            + "'CorrelatedNestedLoopJoin') */ l.id, r.id FROM "
+            + "(SELECT id, " + key + " AS k FROM row_keys WHERE id = ?) l JOIN 
"
+            + "(SELECT id, " + key + " AS k FROM row_keys WHERE id <> ?) r ON 
l.k = r.k")
+            .withParams(firstId, firstId)
+            .matches(containsSubPlan("HashJoin"))
+            .returns(firstId, secondId)
+            .check();
+    }
+
+    /** Null fields and additional nesting must not prevent duplicate 
elimination. */
+    @ParameterizedTest(name = "{0}")
+    @ValueSource(strings = {
+        "ARRAY[ROW(val, CAST(NULL AS INTEGER))]",
+        "ARRAY[ROW(ROW(val, 'x'), ARRAY[val])]",
+        "MAP['key', ROW(val, 'x')]",
+        "MAP[ROW(val, 'x'), 'value']"
+    })
+    public void testNestedKeys(String key) {
+        assertQuery("SELECT COUNT(*) FROM (SELECT " + HASH_AGG_HINT + 
"DISTINCT " + key + " FROM row_keys)")
+            .returns(2L)
+            .check();
+    }
+
+    /** Recursive UNION must eliminate independently constructed duplicates in 
the seed and subsequent iterations. */
+    @Test
+    public void testRecursiveUnion() {
+        assertQuery("WITH RECURSIVE t(n, k) AS ("
+            + "SELECT val, ARRAY[ROW(val, 'x')] FROM row_keys WHERE val = 1 
UNION "
+            + "SELECT n + 1, ARRAY[ROW(n + 1, 'x')] FROM t WHERE n < 2) SELECT 
COUNT(*) FROM t")
+            .returns(2L)
+            .check();
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtilsTest.java
 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtilsTest.java
new file mode 100644
index 00000000000..e91caed0ddb
--- /dev/null
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/util/SqlValueUtilsTest.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.util;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import com.google.common.collect.ImmutableList;
+import 
org.apache.ignite.internal.processors.query.calcite.exec.ArrayRowHandler;
+import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import 
org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.GroupKey;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.AffinityService;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.ColocationGroup;
+import org.apache.ignite.internal.processors.query.calcite.trait.Destination;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/** Equality, hash consistency and distribution for SQL keys. */
+public class SqlValueUtilsTest {
+    /** Arrays and list representations of the same nested ROW must compare 
and hash alike. */
+    @Test
+    public void testNestedRows() {
+        Object left = List.of(new Object[] {1, "x", null}, new Object[] {2, 
new byte[] {3, 4}});
+        Object right = Arrays.asList(Arrays.asList(1, "x", null), 
Arrays.asList(2, new byte[] {3, 4}));
+
+        assertSameValue(left, right);
+        assertFalse(SqlValueUtils.deepEquals(left, 
Collections.singletonList(new Object[] {1, "x", null})));
+        assertFalse(SqlValueUtils.deepEquals(new Object[] {1, "x"}, new 
Object[] {1, "y"}));
+        assertFalse(SqlValueUtils.deepEquals(new Object[] {1, "x"}, new 
Object[] {"x", 1}));
+        assertFalse(SqlValueUtils.deepEquals(new Object[] {1}, 1));
+        assertFalse(SqlValueUtils.deepEquals(left, null));
+        assertSameValue(null, null);
+    }
+
+    /** MAP equality ignores entry order and compares both keys and values by 
contents. */
+    @Test
+    public void testMapsWithRows() {
+        Map<Object, Object> left = new LinkedHashMap<>();
+        Map<Object, Object> right = new LinkedHashMap<>();
+
+        left.put(new Object[] {1}, new Object[] {"a", null});
+        left.put(new Object[] {2}, new Object[] {"b", 3});
+        right.put(new Object[] {2}, new Object[] {"b", 3});
+        right.put(new Object[] {1}, new Object[] {"a", null});
+
+        assertSameValue(left, right);
+
+        right.clear();
+        right.put(new Object[] {1}, new Object[] {"a", null});
+        right.put(new Object[] {1}, new Object[] {"a", null});
+
+        assertFalse(SqlValueUtils.deepEquals(left, right));
+    }
+
+    /** Scalars and collections without ROWs use the same equality and hashing 
methods. */
+    @Test
+    public void testSimpleValues() {
+        assertSameValue("key", new String("key"));
+        assertSameValue(42, 42);
+        assertSameValue(Arrays.asList(1, null, 2), Arrays.asList(1, null, 2));
+
+        Map<String, Integer> left = new LinkedHashMap<>();
+        Map<String, Integer> right = new LinkedHashMap<>();
+
+        left.put("a", 1);
+        left.put("b", null);
+        right.put("b", null);
+        right.put("a", 1);
+
+        assertSameValue(left, right);
+        assertFalse(SqlValueUtils.deepEquals("key", "other"));
+        assertFalse(SqlValueUtils.deepEquals(42, 43));
+    }
+
+    /** Equal independent keys must address the same hash bucket without 
changing join null rules. */
+    @Test
+    public void testGroupKeys() {
+        Object[] left = {Collections.singletonList(new Object[] {1, "x", 
null})};
+        Object[] right = {Collections.singletonList(new Object[] {1, "x", 
null})};
+        GroupKey<Object[]> first = GroupKey.of(left, ArrayRowHandler.INSTANCE);
+        GroupKey<Object[]> second = GroupKey.of(right, 
ArrayRowHandler.INSTANCE);
+        Map<GroupKey<Object[]>, String> idx = new HashMap<>();
+
+        idx.put(first, "found");
+
+        assertEquals(first, second);
+        assertEquals(first.hashCode(), second.hashCode());
+        assertEquals("found", idx.get(second));
+        assertNull(GroupKey.of(new Object[] {null}, ArrayRowHandler.INSTANCE, 
false));
+        assertNull(GroupKey.of(new Object[] {null}, ArrayRowHandler.INSTANCE, 
ImmutableList.of(true)));
+        assertNotNull(GroupKey.of(new Object[] {null}, 
ArrayRowHandler.INSTANCE, ImmutableList.of(false)));
+        assertNotNull(GroupKey.of(left, ArrayRowHandler.INSTANCE, false));
+    }
+
+    /** Hash distribution uses content hashes; cache affinity still uses its 
configured service. */
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testDistribution() {
+        ExecutionContext<Object[]> ctx = mock(ExecutionContext.class);
+
+        when(ctx.rowHandler()).thenReturn(ArrayRowHandler.INSTANCE);
+
+        List<List<UUID>> assignments = IntStream.range(0, 32)
+            .mapToObj(i -> List.of(new UUID(0, 
i))).collect(Collectors.toList());
+        ColocationGroup grp = ColocationGroup.forAssignments(assignments);
+        IgniteDistribution distribution = IgniteDistributions.hash(List.of(0));
+        AffinityService aff = cacheId -> val -> 7;
+        Destination<Object[]> dest = distribution.destination(ctx, aff, grp);
+
+        for (int i = 0; i < 32; i++) {
+            Object[] left = {Collections.singletonList(new Object[] {i})};
+            Object[] right = {Collections.singletonList(new Object[] {i})};
+
+            assertEquals(dest.targets(left), dest.targets(right));
+        }
+
+        assertNotEquals(dest.targets(new Object[] 
{Collections.singletonList(new Object[] {0})}),
+            dest.targets(new Object[] {Collections.singletonList(new Object[] 
{1})}));
+
+        assertEquals(assignments.get(42 % 32), dest.targets(new Object[] 
{42}));
+        assertEquals(dest.targets(new Object[] {Arrays.asList(1, null, 2)}),
+            dest.targets(new Object[] {Arrays.asList(1, null, 2)}));
+        assertEquals(dest.targets(new Object[] {"key"}), dest.targets(new 
Object[] {new String("key")}));
+
+        Destination<Object[]> affDest = IgniteDistributions.affinity(0, 1, 
"cache").destination(ctx, aff, grp);
+
+        assertEquals(assignments.get(7), affDest.targets(new Object[] {42}));
+    }
+
+    /** */
+    private static void assertSameValue(Object left, Object right) {
+        assertTrue(SqlValueUtils.deepEquals(left, right));
+        assertTrue(SqlValueUtils.deepEquals(right, left));
+        assertEquals(SqlValueUtils.deepHashCode(left), 
SqlValueUtils.deepHashCode(right));
+    }
+}
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
index 118b68988ba..c3f5ad621da 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
@@ -67,6 +67,7 @@ import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryBloc
 import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryMetadataIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.QueryWithPartitionsIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.RecursiveCteIntegrationTest;
+import 
org.apache.ignite.internal.processors.query.calcite.integration.RowKeyEqualityIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.RunningQueriesIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.ScalarInIntegrationTest;
 import 
org.apache.ignite.internal.processors.query.calcite.integration.SelectByKeyFieldTest;
@@ -132,6 +133,7 @@ import org.junit.platform.suite.api.Suite;
     TableDmlIntegrationTest.class,
     DataTypesTest.class,
     NestedResultTypesIntegrationTest.class,
+    RowKeyEqualityIntegrationTest.class,
     IndexSpoolIntegrationTest.class,
     HashSpoolIntegrationTest.class,
     IndexScanlIntegrationTest.class,
diff --git 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/UtilTestSuite.java 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/UtilTestSuite.java
index 446bb0337ce..9968d1868b4 100644
--- 
a/modules/calcite/src/test/java/org/apache/ignite/testsuites/UtilTestSuite.java
+++ 
b/modules/calcite/src/test/java/org/apache/ignite/testsuites/UtilTestSuite.java
@@ -26,6 +26,7 @@ import 
org.apache.ignite.internal.processors.query.calcite.exec.task.QueryBlocki
 import 
org.apache.ignite.internal.processors.query.calcite.exec.task.QueryTasksQueueTest;
 import 
org.apache.ignite.internal.processors.query.calcite.exec.tracker.MemoryTrackerTest;
 import org.apache.ignite.internal.processors.query.calcite.type.OtherTypeTest;
+import 
org.apache.ignite.internal.processors.query.calcite.util.SqlValueUtilsTest;
 import org.apache.ignite.internal.processors.query.calcite.util.TypeUtilsTest;
 import org.junit.platform.suite.api.SelectClasses;
 import org.junit.platform.suite.api.Suite;
@@ -44,6 +45,7 @@ import org.junit.platform.suite.api.Suite;
     QueryTasksQueueTest.class,
     OtherTypeTest.class,
     TypeUtilsTest.class,
+    SqlValueUtilsTest.class,
     IgniteFunctionParameterTest.class,
 })
 public class UtilTestSuite {

Reply via email to