korlov42 commented on code in PR #3608:
URL: https://github.com/apache/ignite-3/pull/3608#discussion_r1570350490


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/IgniteHashJoin.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sql.engine.rel;
+
+import java.util.List;
+import java.util.Set;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.util.Util;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCost;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory;
+import org.apache.ignite.internal.sql.engine.util.Commons;
+
+/**
+ * Relational operator that represent hash join algo.
+ */
+public class IgniteHashJoin extends AbstractIgniteJoin {
+    private static final String REL_TYPE_NAME = "HashJoin";
+
+    public IgniteHashJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode 
left, RelNode right,
+            RexNode condition, Set<CorrelationId> variablesSet, JoinRelType 
joinType) {
+        super(cluster, traitSet, left, right, condition, variablesSet, 
joinType);
+    }
+
+    /** Constructor. */
+    public IgniteHashJoin(RelInput input) {
+        this(input.getCluster(),
+                input.getTraitSet().replace(IgniteConvention.INSTANCE),
+                input.getInputs().get(0),
+                input.getInputs().get(1),
+                input.getExpression("condition"),
+                
Set.copyOf(Commons.transform(input.getIntegerList("variablesSet"), 
CorrelationId::new)),
+                input.getEnum("joinType", JoinRelType.class));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {
+        IgniteCostFactory costFactory = (IgniteCostFactory) 
planner.getCostFactory();
+
+        double rowCount = 0;
+        double leftRowCount = mq.getRowCount(getLeft());
+        double rightRowCount = mq.getRowCount(getRight());
+
+        if (Double.isInfinite(leftRowCount) || 
Double.isInfinite(rightRowCount)) {
+            return planner.getCostFactory().makeInfiniteCost();
+        }
+
+        rowCount += leftRowCount;
+        // believe in some kind of keys equality
+        rowCount += Util.nLogN(rightRowCount);
+
+        double rightSize = rightRowCount * 
(getRight().getRowType().getFieldCount() * 8) * IgniteCost.AVERAGE_FIELD_SIZE;

Review Comment:
   why `8`? 



##########
modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/BaseSqlIntegrationTest.java:
##########
@@ -111,19 +111,29 @@ protected enum JoinType {
         NESTED_LOOP(
                 "CorrelatedNestedLoopJoin",
                 "JoinCommuteRule",
-                "MergeJoinConverter"
+                "MergeJoinConverter",
+                "HashJoinConverter"
         ),
 
         MERGE(
                 "CorrelatedNestedLoopJoin",
                 "JoinCommuteRule",
-                "NestedLoopJoinConverter"
+                "NestedLoopJoinConverter",
+                "HashJoinConverter"
         ),
 
         CORRELATED(
                 "MergeJoinConverter",
                 "JoinCommuteRule",
-                "NestedLoopJoinConverter"
+                "NestedLoopJoinConverter",
+                "HashJoinConverter"
+        ),
+
+        HASHJOIN(
+                "MergeJoinConverter",
+                        "JoinCommuteRule",
+                        "NestedLoopJoinConverter",
+                        "CorrelatedNestedLoopJoin"

Review Comment:
   wrong indentation 



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/HashJoinPlannerTest.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.sql.engine.planner;
+
+import static 
org.apache.ignite.internal.sql.engine.planner.CorrelatedSubqueryPlannerTest.createTestTable;
+
+import java.util.stream.Stream;
+import org.apache.calcite.plan.RelOptPlanner.CannotPlanException;
+import org.apache.ignite.internal.sql.engine.rel.IgniteHashJoin;
+import org.apache.ignite.internal.sql.engine.schema.IgniteSchema;
+import org.apache.ignite.internal.sql.engine.schema.IgniteTable;
+import org.apache.ignite.internal.testframework.IgniteTestUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/** HashJoin planner test. */
+public class HashJoinPlannerTest extends AbstractPlannerTest {
+    private static final String[] disabledRules = {"NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter"};
+
+    private static final String[] joinTypes = {"LEFT", "RIGHT", "INNER", "FULL 
OUTER"};
+
+    /** Check that only appropriate conditions are acceptable for hash join. */
+    @ParameterizedTest()
+    @MethodSource("joinConditions")
+    @SuppressWarnings("ThrowableNotThrown")
+    public void hashJoinAppliedConditions(String sql, boolean canBePlanned) 
throws Exception {
+        IgniteTable tbl = createTestTable("ID", "C1");
+
+        IgniteSchema schema = createSchema(tbl);
+
+        for (String type : joinTypes) {
+            String sql0 = String.format(sql, type);
+
+            if (canBePlanned) {
+                assertPlan(sql0, schema, 
nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class)), disabledRules);
+            } else {
+                IgniteTestUtils.assertThrowsWithCause(() -> physicalPlan(sql0, 
schema, disabledRules),
+                        CannotPlanException.class,
+                        "There are not enough rules");
+            }
+        }
+    }
+
+    private static Stream<Arguments> joinConditions() {
+        return Stream.of(
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
t2.c1", true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 using(c1)", 
true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
1", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 ON t1.id is 
not distinct from t2.c1", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
?", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
OCTET_LENGTH('TEST')", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
t2.c1 and t1.ID > t2.ID", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
1", false),

Review Comment:
   what is the difference between this case and the one on line 64?



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/JoinColocationPlannerTest.java:
##########
@@ -70,10 +78,105 @@ public void joinSameTableSimpleAff() throws Exception {
     }
 
     /**
-     * Join of the same tables with a complex affinity is expected to be 
colocated.
+     * Join of the same tables with a simple affinity is expected to be 
colocated.
+     */
+    @Test
+    public void joinSameTableSimpleAffHashJoin() throws Exception {
+        IgniteTable tbl = simpleTable("TEST_TBL", DEFAULT_TBL_SIZE);
+
+        IgniteSchema schema = createSchema(tbl);
+
+        String sql = "select count(*) "
+                + "from TEST_TBL t1 "
+                + "join TEST_TBL t2 on t1.id = t2.id";
+
+        // Only hash join
+        RelNode phys = physicalPlan(sql, schema, "NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter");
+
+        AbstractIgniteJoin join = findFirstNode(phys, 
byClass(AbstractIgniteJoin.class));
+        List<RelNode> joinNodes = findNodes(phys, 
byClass(AbstractIgniteJoin.class));
+
+        String invalidPlanMsg = "Invalid plan:\n" + RelOptUtil.toString(phys);
+
+        assertThat(invalidPlanMsg, joinNodes.size(), equalTo(1));
+        assertThat(invalidPlanMsg, join, notNullValue());
+        assertThat(invalidPlanMsg, join.distribution().function().affinity(), 
is(true));
+    }
+
+    /**
+     * Hash join need to preserve left collation.
      */
     @Test
-    public void joinSameTableComplexAff() throws Exception {
+    public void hashJoinCheckLeftCollationsPropagation() throws Exception {
+        IgniteTable tbl1 = simpleTable("TEST_TBL", DEFAULT_TBL_SIZE);
+        IgniteTable tbl2 = complexTbl("TEST_TBL_CMPLX");
+
+        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";
+
+        // Only hash join
+        RelNode phys = physicalPlan(sql, schema, "NestedLoopJoinConverter",
+                "CorrelatedNestedLoopJoin", "MergeJoinConverter", 
"JoinCommuteRule");
+
+        AbstractIgniteJoin join = findFirstNode(phys, 
byClass(AbstractIgniteJoin.class));
+        List<RelNode> joinNodes = findNodes(phys, 
byClass(AbstractIgniteJoin.class));
+        List<RelNode> sortNodes = findNodes(phys, byClass(IgniteSort.class));
+
+        String invalidPlanMsg = "Invalid plan:\n" + RelOptUtil.toString(phys);
+
+        assertThat(invalidPlanMsg, sortNodes.size(), equalTo(0));
+        assertThat(invalidPlanMsg, joinNodes.size(), equalTo(1));
+        assertThat(invalidPlanMsg, join, notNullValue());
+    }
+
+    /**
+     * Hash join erase right collation.
+     */
+    @Test
+    public void hashJoinCheckRightCollations() throws Exception {

Review Comment:
   better to move this test to HashJoinPlannerTest



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/IgniteHashJoin.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sql.engine.rel;
+
+import java.util.List;
+import java.util.Set;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.util.Util;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCost;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory;
+import org.apache.ignite.internal.sql.engine.util.Commons;
+
+/**
+ * Relational operator that represent hash join algo.
+ */
+public class IgniteHashJoin extends AbstractIgniteJoin {
+    private static final String REL_TYPE_NAME = "HashJoin";
+
+    public IgniteHashJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode 
left, RelNode right,
+            RexNode condition, Set<CorrelationId> variablesSet, JoinRelType 
joinType) {
+        super(cluster, traitSet, left, right, condition, variablesSet, 
joinType);
+    }
+
+    /** Constructor. */
+    public IgniteHashJoin(RelInput input) {
+        this(input.getCluster(),
+                input.getTraitSet().replace(IgniteConvention.INSTANCE),
+                input.getInputs().get(0),
+                input.getInputs().get(1),
+                input.getExpression("condition"),
+                
Set.copyOf(Commons.transform(input.getIntegerList("variablesSet"), 
CorrelationId::new)),
+                input.getEnum("joinType", JoinRelType.class));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {
+        IgniteCostFactory costFactory = (IgniteCostFactory) 
planner.getCostFactory();
+
+        double rowCount = 0;
+        double leftRowCount = mq.getRowCount(getLeft());
+        double rightRowCount = mq.getRowCount(getRight());
+
+        if (Double.isInfinite(leftRowCount) || 
Double.isInfinite(rightRowCount)) {
+            return planner.getCostFactory().makeInfiniteCost();
+        }
+
+        rowCount += leftRowCount;
+        // believe in some kind of keys equality
+        rowCount += Util.nLogN(rightRowCount);
+
+        double rightSize = rightRowCount * 
(getRight().getRowType().getFieldCount() * 8) * IgniteCost.AVERAGE_FIELD_SIZE;
+
+        return costFactory.makeCost(rowCount, rowCount * 
IgniteCost.ROW_PASS_THROUGH_COST, 0, rightSize, 0);

Review Comment:
   TBH, I don't quite understand logic behind cost estimation... Could you hare 
some idea please? 



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/HashJoinPlannerTest.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.sql.engine.planner;
+
+import static 
org.apache.ignite.internal.sql.engine.planner.CorrelatedSubqueryPlannerTest.createTestTable;
+
+import java.util.stream.Stream;
+import org.apache.calcite.plan.RelOptPlanner.CannotPlanException;
+import org.apache.ignite.internal.sql.engine.rel.IgniteHashJoin;
+import org.apache.ignite.internal.sql.engine.schema.IgniteSchema;
+import org.apache.ignite.internal.sql.engine.schema.IgniteTable;
+import org.apache.ignite.internal.testframework.IgniteTestUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/** HashJoin planner test. */
+public class HashJoinPlannerTest extends AbstractPlannerTest {
+    private static final String[] disabledRules = {"NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter"};
+
+    private static final String[] joinTypes = {"LEFT", "RIGHT", "INNER", "FULL 
OUTER"};
+
+    /** Check that only appropriate conditions are acceptable for hash join. */
+    @ParameterizedTest()
+    @MethodSource("joinConditions")
+    @SuppressWarnings("ThrowableNotThrown")
+    public void hashJoinAppliedConditions(String sql, boolean canBePlanned) 
throws Exception {
+        IgniteTable tbl = createTestTable("ID", "C1");
+
+        IgniteSchema schema = createSchema(tbl);
+
+        for (String type : joinTypes) {
+            String sql0 = String.format(sql, type);
+
+            if (canBePlanned) {
+                assertPlan(sql0, schema, 
nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class)), disabledRules);
+            } else {
+                IgniteTestUtils.assertThrowsWithCause(() -> physicalPlan(sql0, 
schema, disabledRules),
+                        CannotPlanException.class,
+                        "There are not enough rules");
+            }
+        }
+    }
+
+    private static Stream<Arguments> joinConditions() {
+        return Stream.of(
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
t2.c1", true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 using(c1)", 
true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
1", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 ON t1.id is 
not distinct from t2.c1", false),

Review Comment:
   let's file a ticket to support `is not distinct` predicate



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/JoinColocationPlannerTest.java:
##########
@@ -70,10 +78,105 @@ public void joinSameTableSimpleAff() throws Exception {
     }
 
     /**
-     * Join of the same tables with a complex affinity is expected to be 
colocated.
+     * Join of the same tables with a simple affinity is expected to be 
colocated.
+     */
+    @Test
+    public void joinSameTableSimpleAffHashJoin() throws Exception {
+        IgniteTable tbl = simpleTable("TEST_TBL", DEFAULT_TBL_SIZE);
+
+        IgniteSchema schema = createSchema(tbl);
+
+        String sql = "select count(*) "
+                + "from TEST_TBL t1 "
+                + "join TEST_TBL t2 on t1.id = t2.id";
+
+        // Only hash join
+        RelNode phys = physicalPlan(sql, schema, "NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter");
+
+        AbstractIgniteJoin join = findFirstNode(phys, 
byClass(AbstractIgniteJoin.class));
+        List<RelNode> joinNodes = findNodes(phys, 
byClass(AbstractIgniteJoin.class));
+
+        String invalidPlanMsg = "Invalid plan:\n" + RelOptUtil.toString(phys);
+
+        assertThat(invalidPlanMsg, joinNodes.size(), equalTo(1));
+        assertThat(invalidPlanMsg, join, notNullValue());
+        assertThat(invalidPlanMsg, join.distribution().function().affinity(), 
is(true));
+    }
+
+    /**
+     * Hash join need to preserve left collation.
      */
     @Test
-    public void joinSameTableComplexAff() throws Exception {
+    public void hashJoinCheckLeftCollationsPropagation() throws Exception {

Review Comment:
   better to move this test to HashJoinPlannerTest



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/IgniteHashJoin.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sql.engine.rel;
+
+import java.util.List;
+import java.util.Set;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.util.Util;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCost;
+import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory;
+import org.apache.ignite.internal.sql.engine.util.Commons;
+
+/**
+ * Relational operator that represent hash join algo.
+ */
+public class IgniteHashJoin extends AbstractIgniteJoin {
+    private static final String REL_TYPE_NAME = "HashJoin";
+
+    public IgniteHashJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode 
left, RelNode right,
+            RexNode condition, Set<CorrelationId> variablesSet, JoinRelType 
joinType) {
+        super(cluster, traitSet, left, right, condition, variablesSet, 
joinType);
+    }
+
+    /** Constructor. */
+    public IgniteHashJoin(RelInput input) {
+        this(input.getCluster(),
+                input.getTraitSet().replace(IgniteConvention.INSTANCE),
+                input.getInputs().get(0),
+                input.getInputs().get(1),
+                input.getExpression("condition"),
+                
Set.copyOf(Commons.transform(input.getIntegerList("variablesSet"), 
CorrelationId::new)),
+                input.getEnum("joinType", JoinRelType.class));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery 
mq) {
+        IgniteCostFactory costFactory = (IgniteCostFactory) 
planner.getCostFactory();
+
+        double rowCount = 0;
+        double leftRowCount = mq.getRowCount(getLeft());
+        double rightRowCount = mq.getRowCount(getRight());
+
+        if (Double.isInfinite(leftRowCount) || 
Double.isInfinite(rightRowCount)) {
+            return planner.getCostFactory().makeInfiniteCost();
+        }
+
+        rowCount += leftRowCount;
+        // believe in some kind of keys equality
+        rowCount += Util.nLogN(rightRowCount);

Review Comment:
   what does this mean? 



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/HashJoinPlannerTest.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.sql.engine.planner;
+
+import static 
org.apache.ignite.internal.sql.engine.planner.CorrelatedSubqueryPlannerTest.createTestTable;
+
+import java.util.stream.Stream;
+import org.apache.calcite.plan.RelOptPlanner.CannotPlanException;
+import org.apache.ignite.internal.sql.engine.rel.IgniteHashJoin;
+import org.apache.ignite.internal.sql.engine.schema.IgniteSchema;
+import org.apache.ignite.internal.sql.engine.schema.IgniteTable;
+import org.apache.ignite.internal.testframework.IgniteTestUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/** HashJoin planner test. */
+public class HashJoinPlannerTest extends AbstractPlannerTest {
+    private static final String[] disabledRules = {"NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter"};
+
+    private static final String[] joinTypes = {"LEFT", "RIGHT", "INNER", "FULL 
OUTER"};
+
+    /** Check that only appropriate conditions are acceptable for hash join. */
+    @ParameterizedTest()
+    @MethodSource("joinConditions")
+    @SuppressWarnings("ThrowableNotThrown")
+    public void hashJoinAppliedConditions(String sql, boolean canBePlanned) 
throws Exception {
+        IgniteTable tbl = createTestTable("ID", "C1");
+
+        IgniteSchema schema = createSchema(tbl);
+
+        for (String type : joinTypes) {
+            String sql0 = String.format(sql, type);
+
+            if (canBePlanned) {
+                assertPlan(sql0, schema, 
nodeOrAnyChild(isInstanceOf(IgniteHashJoin.class)), disabledRules);
+            } else {
+                IgniteTestUtils.assertThrowsWithCause(() -> physicalPlan(sql0, 
schema, disabledRules),
+                        CannotPlanException.class,
+                        "There are not enough rules");
+            }
+        }
+    }
+
+    private static Stream<Arguments> joinConditions() {
+        return Stream.of(
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
t2.c1", true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 using(c1)", 
true),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
1", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 ON t1.id is 
not distinct from t2.c1", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
?", false),
+                Arguments.of("select t1.c1 from t1 %s join t1 t2 on t1.c1 = 
OCTET_LENGTH('TEST')", false),

Review Comment:
   let's add similar test case but with attribute of a table as argument to a 
function 



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/HashJoinPlannerTest.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.sql.engine.planner;
+
+import static 
org.apache.ignite.internal.sql.engine.planner.CorrelatedSubqueryPlannerTest.createTestTable;
+
+import java.util.stream.Stream;
+import org.apache.calcite.plan.RelOptPlanner.CannotPlanException;
+import org.apache.ignite.internal.sql.engine.rel.IgniteHashJoin;
+import org.apache.ignite.internal.sql.engine.schema.IgniteSchema;
+import org.apache.ignite.internal.sql.engine.schema.IgniteTable;
+import org.apache.ignite.internal.testframework.IgniteTestUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/** HashJoin planner test. */
+public class HashJoinPlannerTest extends AbstractPlannerTest {
+    private static final String[] disabledRules = {"NestedLoopJoinConverter", 
"CorrelatedNestedLoopJoin", "MergeJoinConverter"};
+
+    private static final String[] joinTypes = {"LEFT", "RIGHT", "INNER", "FULL 
OUTER"};
+
+    /** Check that only appropriate conditions are acceptable for hash join. */
+    @ParameterizedTest()
+    @MethodSource("joinConditions")
+    @SuppressWarnings("ThrowableNotThrown")
+    public void hashJoinAppliedConditions(String sql, boolean canBePlanned) 
throws Exception {

Review Comment:
   let's also add test cases without disabled rules to verify that HashJoin is 
used by optimizer as expected



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/rel/ExecutionTest.java:
##########
@@ -55,14 +57,16 @@
 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;
 
 /**
  * ExecutionTest.
  * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
  */
 public class ExecutionTest extends AbstractExecutionTest<Object[]> {
-    @Test
-    public void testSimpleExecution() {
+    @ParameterizedTest(name = "join algo : {0}")
+    @ValueSource(strings = {"NLJoin", "HashJoin"})
+    public void testSimpleExecution(String joinImplementation) {

Review Comment:
   please move all tests related to HashJoinNode to dedicated class



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rule/HashJoinConverterRule.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.sql.engine.rule;
+
+import static org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty;
+
+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.sql.engine.rel.IgniteConvention;
+import org.apache.ignite.internal.sql.engine.rel.IgniteHashJoin;
+
+/**
+ * Hash join converter.
+ */
+public class HashJoinConverterRule extends 
AbstractIgniteConverterRule<LogicalJoin> {
+    public static final RelOptRule INSTANCE = new HashJoinConverterRule();
+
+    /**
+     * Creates a converter.
+     */
+    public HashJoinConverterRule() {
+        super(LogicalJoin.class, "HashJoinConverter");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        LogicalJoin logicalJoin = call.rel(0);
+
+        return !nullOrEmpty(logicalJoin.analyzeCondition().pairs())
+                && logicalJoin.analyzeCondition().isEqui() && 
acceptableConditions(logicalJoin.getCondition());
+    }
+
+    private static boolean acceptableConditions(RexNode node) {

Review Comment:
   what was the reason? It would be nice to cover reasoning in a comment to 
this method



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

Reply via email to