alex-plekhanov commented on code in PR #11770:
URL: https://github.com/apache/ignite/pull/11770#discussion_r2198194390


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/ExecutionTest.java:
##########
@@ -538,6 +534,119 @@ public void testCorrelatedNestedLoopJoin() {
         }
     }
 
+    /** Tests 'Select e.id, e.name, d.name as dep_name from DEP d <JOIN TYPE> 
join EMP e on e.DEPNO = d.DEPNO'. */
+    @Test
+    public void testHashJoin() {

Review Comment:
   ExecutionTest already contains a lot of unsorted tests, maybe it's time to 
move join tests to another class. But we also have 
MergeJoinExecutionTest/NestedLoopJoinExecutionTest they look almost identical 
and looks like they cover more cases than testHashJoin test. Maybe it's better 
to extract common part and add new HashJoinExecutionTest class.



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/CalciteBasicSecondaryIndexIntegrationTest.java:
##########
@@ -278,37 +279,40 @@ public void testIndexLoopJoin() {
 
     /** */
     @Test
-    public void testMergeJoin() {
-        assertQuery("" +
-            "SELECT /*+ " + HintDefinition.MERGE_JOIN + " */ d1.name, d2.name 
FROM Developer d1, Developer d2 " +
-            "WHERE d1.depId = d2.depId")
-            .matches(containsSubPlan("IgniteMergeJoin"))
-            .returns("Bach", "Bach")
-            .returns("Beethoven", "Beethoven")
-            .returns("Beethoven", "Strauss")
-            .returns("Mozart", "Mozart")
-            .returns("Strauss", "Strauss")
-            .returns("Strauss", "Beethoven")
-            .returns("Vagner", "Vagner")
-            .returns("Chaikovsky", "Chaikovsky")
-            .returns("Verdy", "Verdy")
-            .returns("Stravinsky", "Stravinsky")
-            .returns("Rahmaninov", "Rahmaninov")
-            .returns("Shubert", "Shubert")
-            .returns("Glinka", "Glinka")
-            .returns("Arnalds", "Arnalds")
-            .returns("Glass", "Glass")
-            .returns("O'Halloran", "O'Halloran")
-            .returns("Prokofiev", "Prokofiev")
-            .returns("Yiruma", "Yiruma")
-            .returns("Cacciapaglia", "Cacciapaglia")
-            .returns("Einaudi", "Einaudi")
-            .returns("Hasaishi", "Hasaishi")
-            .returns("Marradi", "Marradi")
-            .returns("Musorgskii", "Musorgskii")
-            .returns("Rihter", "Rihter")
-            .returns("Zimmer", "Zimmer")
-            .check();
+    public void testMergeAndHashJoin() {

Review Comment:
   Two different methods? Also, perhaps it's worth to extract test to some 
other class.



##########
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:
   Please add two columns test. 
   Also, in cases like `t1.c1 = t2.c2 and t1.id = 1` second condition can be 
pushed down to table scan (at least for some types of join), perhaps these 
cases should be tested too.



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/HashJoinNode.java:
##########
@@ -0,0 +1,761 @@
+/*
+ * 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.exec.rel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.type.RelDataType;
+import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+
+/** Hash join implementor. */
+public abstract class HashJoinNode<Row> extends 
AbstractRightMaterializedJoinNode<Row> {
+    /** */
+    private static final int INITIAL_CAPACITY = 128;
+
+    /** All keys with null-fields are mapped to this object. */
+    private static final Key NULL_KEY = new Key();
+
+    /** */
+    private final int[] leftKeys;
+
+    /** */
+    private final int[] rightKeys;
+
+    /** Output row handler. */
+    protected final RowHandler<Row> outRowHnd;
+
+    /** */
+    protected final Map<Key, TouchedCollection<Row>> hashStore = new 
HashMap<>(INITIAL_CAPACITY);
+
+    /** */
+    protected Iterator<Row> rightIt = Collections.emptyIterator();
+
+    /**
+     * Creates hash join node.
+     *
+     * @param ctx Execution context.
+     * @param rowType Row type.
+     * @param info Join info.
+     * @param outRowHnd Output row handler.
+     */
+    protected HashJoinNode(ExecutionContext<Row> ctx, RelDataType rowType, 
JoinInfo info, RowHandler<Row> outRowHnd) {
+        super(ctx, rowType);
+
+        leftKeys = info.leftKeys.toIntArray();
+        rightKeys = info.rightKeys.toIntArray();
+
+        assert leftKeys.length == rightKeys.length;
+
+        this.outRowHnd = outRowHnd;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void rewindInternal() {
+        super.rewindInternal();
+
+        rightIt = Collections.emptyIterator();
+
+        hashStore.clear();
+    }
+
+    /** Creates certain join node. */
+    public static <RowT> HashJoinNode<RowT> create(
+        ExecutionContext<RowT> ctx,
+        RelDataType outRowType,
+        RelDataType leftRowType,
+        RelDataType rightRowType,
+        JoinRelType type,
+        JoinInfo info
+    ) {
+        IgniteTypeFactory typeFactory = ctx.getTypeFactory();
+        RowHandler<RowT> rowHnd = ctx.rowHandler();
+
+        switch (type) {
+            case INNER:
+                return new InnerHashJoin<>(ctx, outRowType, info, rowHnd);
+
+            case LEFT:
+                return new LeftHashJoin<>(ctx, outRowType, info, rowHnd, 
rowHnd.factory(typeFactory, rightRowType));
+
+            case RIGHT:
+                return new RightHashJoin<>(ctx, outRowType, info, rowHnd, 
rowHnd.factory(typeFactory, leftRowType));
+
+            case FULL:
+                return new FullOuterHashJoin<>(ctx, outRowType, info, rowHnd, 
rowHnd.factory(typeFactory, leftRowType),
+                    rowHnd.factory(typeFactory, rightRowType));
+
+            case SEMI:
+                return new SemiHashJoin<>(ctx, outRowType, info, rowHnd);
+
+            case ANTI:
+                return new AntiHashJoin<>(ctx, outRowType, info, rowHnd);
+
+            default:
+                throw new IllegalArgumentException("Join of type '" + type + 
"' isn't supported.");
+        }
+    }
+
+    /** */
+    protected Collection<Row> lookup(Row row) {
+        Key row0 = extractKey(row, leftKeys);
+
+        // Key with null field can't be compared with other keys.
+        if (row0 == NULL_KEY)
+            return Collections.emptyList();
+
+        TouchedCollection<Row> found = hashStore.get(row0);
+
+        if (found != null) {
+            found.touched = true;
+
+            return found.items();
+        }
+
+        return Collections.emptyList();
+    }
+
+    /** */
+    private static <RowT> Iterator<RowT> untouched(Map<Key, 
TouchedCollection<RowT>> entries) {
+        return new Iterator<RowT>() {
+            private final Iterator<TouchedCollection<RowT>> it = 
entries.values().iterator();
+
+            private Iterator<RowT> innerIt = Collections.emptyIterator();
+
+            @Override public boolean hasNext() {
+                if (innerIt.hasNext())
+                    return true;
+
+                advance();
+
+                return innerIt.hasNext();
+            }
+
+            @Override public RowT next() {
+                if (!hasNext())
+                    throw new NoSuchElementException();
+
+                return innerIt.next();
+            }
+
+            private void advance() {
+                while (it.hasNext()) {
+                    TouchedCollection<RowT> coll = it.next();
+
+                    if (!coll.touched && !coll.items().isEmpty()) {
+                        innerIt = coll.items().iterator();
+
+                        break;
+                    }
+                }
+            }
+        };

Review Comment:
   ```
           return F.flat(F.iterator(entries.values(), TouchedCollection::items, 
true, v -> !v.touched));
   ```



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/HashJoinConverterRule.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.rule;
+
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.PhysicalNode;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexVisitor;
+import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.util.Util;
+import org.apache.ignite.internal.processors.query.calcite.hint.HintDefinition;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteHashJoin;
+import org.apache.ignite.internal.util.typedef.F;
+
+/** Hash join converter rule. */
+public class HashJoinConverterRule extends AbstractIgniteJoinConverterRule {
+    /** */
+    public static final RelOptRule INSTANCE = new HashJoinConverterRule();
+
+    /** Ctor. */
+    public HashJoinConverterRule() {
+        super("HashJoinConverter", HintDefinition.HASH_JOIN);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean matchesJoin(RelOptRuleCall call) {
+        LogicalJoin join = call.rel(0);
+
+        return !F.isEmpty(join.analyzeCondition().pairs()) && 
join.analyzeCondition().isEqui() && checkConditions(join.getCondition());
+    }
+
+    /** */
+    private static boolean checkConditions(RexNode node) {
+        RexVisitor<Void> v = new RexVisitorImpl<>(true) {
+            @Override public Void visitCall(RexCall call) {
+                if (call.getOperator().getKind() != SqlKind.EQUALS && 
call.getOperator().getKind() != SqlKind.AND)
+                    throw Util.FoundOne.NULL;
+
+                return super.visitCall(call);
+            }
+        };
+
+        try {
+            node.accept(v);
+
+            return true;
+        }
+        catch (Util.FoundOne e) {
+            return false;
+        }
+    }

Review Comment:
   Conditions already updated in AI3, and they look more correct. Let's 
synchronize it.



##########
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)));

Review Comment:
   ```
           assertPlan(sql, schema, 
nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class))
               .and(nodeOrAnyChild(isInstanceOf(IgniteSort.class)).negate()), 
DISABLED_RULES);
   ```



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/JoinColocationPlannerTest.java:
##########
@@ -62,16 +66,18 @@ public void joinSameTableSimpleAff() throws Exception {
             "from TEST_TBL t1 " +
             "join TEST_TBL t2 on t1.id = t2.id";
 
-        RelNode phys = physicalPlan(sql, schema, "NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin");
+        for (String disabledRule : DISABLED_RULES) {
+            RelNode phys = physicalPlan(sql, schema, 
"NestedLoopJoinConverter", "CorrelatedNestedLoopJoin", disabledRule);
 
-        IgniteMergeJoin join = findFirstNode(phys, 
byClass(IgniteMergeJoin.class));
+            AbstractIgniteJoin join = findFirstNode(phys, 
byClass(AbstractIgniteJoin.class));
 
-        String invalidPlanMsg = "Invalid plan:\n" + RelOptUtil.toString(phys);
+            String invalidPlanMsg = "Invalid plan:\n" + 
RelOptUtil.toString(phys);
 
-        assertThat(invalidPlanMsg, join, notNullValue());
-        assertThat(invalidPlanMsg, join.distribution().function().affinity(), 
is(true));
-        assertThat(invalidPlanMsg, join.getLeft(), 
instanceOf(IgniteIndexScan.class));
-        assertThat(invalidPlanMsg, join.getRight(), 
instanceOf(IgniteIndexScan.class));
+            assertThat(invalidPlanMsg, join, notNullValue());

Review Comment:
   Maybe rewrite with assertPlan?



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/HashJoinNode.java:
##########
@@ -0,0 +1,761 @@
+/*
+ * 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.exec.rel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.type.RelDataType;
+import 
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+
+/** Hash join implementor. */
+public abstract class HashJoinNode<Row> extends 
AbstractRightMaterializedJoinNode<Row> {
+    /** */
+    private static final int INITIAL_CAPACITY = 128;
+
+    /** All keys with null-fields are mapped to this object. */
+    private static final Key NULL_KEY = new Key();
+
+    /** */
+    private final int[] leftKeys;
+
+    /** */
+    private final int[] rightKeys;
+
+    /** Output row handler. */
+    protected final RowHandler<Row> outRowHnd;
+
+    /** */
+    protected final Map<Key, TouchedCollection<Row>> hashStore = new 
HashMap<>(INITIAL_CAPACITY);

Review Comment:
   Consider reusing RuntimeHashIndex



##########
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));

Review Comment:
   ```
           assertPlan(sql, schema, nodeOrAnyChild(isInstanceOf(IgniteSort.class)
               .and(input(isInstanceOf(IgniteHashJoin.class)))), 
DISABLED_RULES);
   ```



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