Vladsz83 commented on code in PR #11770:
URL: https://github.com/apache/ignite/pull/11770#discussion_r2239427368


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/HashJoinPlannerTest.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.planner;
+
+import java.util.List;
+import org.apache.calcite.plan.RelOptPlanner.CannotPlanException;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteHashJoin;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteSort;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.jetbrains.annotations.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.rel.RelFieldCollation.Direction.ASCENDING;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
+
+/** */
+public class HashJoinPlannerTest extends AbstractPlannerTest {
+    /** */
+    private static final String[] DISABLED_RULES = {"NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter",
+        "JoinCommuteRule"};
+
+    /** */
+    private static final String[] JOIN_TYPES = {"LEFT", "RIGHT", "INNER", 
"FULL"};
+
+    /** */
+    @Test
+    public void testHashJoinKeepsLeftCollation() throws Exception {
+        TestTable tbl1 = createSimpleTable();
+        TestTable tbl2 = createComplexTable();
+
+        IgniteSchema schema = createSchema(tbl1, tbl2);
+
+        String sql = "select t1.ID, t2.ID1 "
+                + "from TEST_TBL_CMPLX t2 "
+                + "join TEST_TBL t1 on t1.id = t2.id1 "
+                + "order by t2.ID1 NULLS LAST, t2.ID2 NULLS LAST";
+
+        RelNode plan = physicalPlan(sql, schema, DISABLED_RULES);
+
+        assertEquals(0, findNodes(plan, byClass(IgniteSort.class)).size());
+        assertEquals(1, findNodes(plan, byClass(IgniteHashJoin.class)).size());
+        assertNotNull(findFirstNode(plan, byClass(IgniteHashJoin.class)));
+    }
+
+    /** */
+    @Test
+    public void testHashJoinErasesRightCollation() throws Exception {
+        TestTable tbl1 = createSimpleTable();
+        TestTable tbl2 = createComplexTable();
+
+        IgniteSchema schema = createSchema(tbl1, tbl2);
+
+        String sql = "select t1.ID, t2.ID1 "
+                + "from TEST_TBL t1 "
+                + "join TEST_TBL_CMPLX t2 on t1.id = t2.id1 "
+                + "order by t2.ID1 NULLS LAST, t2.ID2 NULLS LAST";
+
+        IgniteRel plan = physicalPlan(sql, schema, DISABLED_RULES);
+
+        assertNotNull(findFirstNode(plan, byClass(IgniteHashJoin.class)));
+        assertNotNull(sortOnTopOfJoin(plan));
+    }
+
+    /** */
+    @Test
+    public void testHashJoinWinsOnSkewedLeftInput() throws Exception {
+        TestTable smallTbl = createSimpleTable("SMALL_TBL", 1000);
+        TestTable largeTbl = createSimpleTable("LARGE_TBL", 500_000);
+
+        IgniteSchema schema = createSchema(smallTbl, largeTbl);
+
+        assertPlan(
+            "select t1.ID, t1.INT_VAL, t2.ID, t2.INT_VAL from LARGE_TBL t1 
join SMALL_TBL t2 on t1.INT_VAL = t2.INT_VAL",
+            schema,
+            nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class)),
+            "JoinCommuteRule"
+        );
+
+        assertPlan(
+            "select t1.ID, t1.INT_VAL, t2.ID, t2.INT_VAL from SMALL_TBL t1 
join LARGE_TBL t2 on t1.INT_VAL = t2.INT_VAL",
+            schema,
+            nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class).negate()),
+            "JoinCommuteRule"
+        );
+
+        // Merge join can consume less CPU resources.
+        assertPlan(
+            "select t1.ID, t1.INT_VAL, t2.ID, t2.INT_VAL from SMALL_TBL t1 
join LARGE_TBL t2 on t1.ID = t2.ID",
+            schema,
+            nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class).negate()),
+            "JoinCommuteRule"
+        );
+    }
+
+    /** */
+    private static @Nullable IgniteSort sortOnTopOfJoin(IgniteRel root) {
+        List<IgniteSort> sortNodes = findNodes(root, byClass(IgniteSort.class)
+            .and(node -> node.getInputs().size() == 1 && node.getInput(0) 
instanceof Join));
+
+        if (sortNodes.size() > 1)
+            throw new IllegalStateException("Incorrect sort nodes number: 
expected 1, actual " + sortNodes.size());
+
+        return sortNodes.isEmpty() ? null : sortNodes.get(0);
+    }
+
+    /** */
+    @Test
+    public void testHashJoinApplied() throws Exception {
+        for (List<Object> paramSet : testJoinIsAppliedParameters()) {
+            assert paramSet != null && paramSet.size() == 2;
+
+            String sql = (String)paramSet.get(0);
+
+            boolean canBePlanned = (Boolean)paramSet.get(1);
+
+            TestTable tbl = createTable("T1", IgniteDistributions.single(), 
"ID", Integer.class, "C1", Integer.class);
+
+            IgniteSchema schema = createSchema(tbl);
+
+            for (String type : JOIN_TYPES) {
+                String sql0 = String.format(sql, type);
+
+                if (canBePlanned)
+                    assertPlan(sql0, schema, 
nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class)), DISABLED_RULES);
+                else {
+                    assertThrows(null, () -> physicalPlan(sql0, schema, 
DISABLED_RULES), CannotPlanException.class,
+                        "There are not enough rules");
+                }
+            }
+        }
+    }
+
+    /** */
+    private static List<List<Object>> testJoinIsAppliedParameters() {
+        return F.asList(
+            F.asList("select t1.c1 from t1 %s join t1 t2 using(c1)", true),
+            F.asList("select t1.c1 from t1 %s join t1 t2 on t1.c1 = t2.c1", 
true),

Review Comment:
   There are two-column tests below. Yes, some scans are pushed down and join 
uses only one equi join pair.



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to