mihaibudiu commented on code in PR #4280: URL: https://github.com/apache/calcite/pull/4280#discussion_r2032168693
########## core/src/main/java/org/apache/calcite/rel/rules/JoinConditionOrExpansionRule.java: ########## @@ -0,0 +1,299 @@ +/* + * 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.calcite.rel.rules; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Planner rule that matches a + * {@link org.apache.calcite.rel.core.Join} + * and expands OR clauses in join conditions. + * + * <p>this rule would expand the OR condition into + * two separate join conditions, allowing the optimizer + * to handle these conditions more effectively. + */ [email protected] +public class JoinConditionOrExpansionRule + extends RelRule<JoinConditionOrExpansionRule.Config> + implements TransformationRule { + + /** Creates an JoinConditionExpansionOrRule. */ + protected JoinConditionOrExpansionRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + Join join = call.rel(0); + RelBuilder relBuilder = call.builder(); + List<RexNode> orConds = RelOptUtil.disjunctions(join.getCondition()); Review Comment: This expansion is most useful when some of the orConds are conjunctions of table columns. In fact, I think you don't even need to expand ALL the orConds, you could stop after one of them. Is it worth analyzing the orConds and making a list of the ones that can lead to conjunctions that can be efficiently handled by equijoins and expanding only these? ########## core/src/test/java/org/apache/calcite/test/JdbcTest.java: ########## @@ -1975,6 +1975,69 @@ private void checkResultSetMetaData(Connection connection, String sql) "c0=Drink; c1=Dairy; c2=USA; c3=WA; c4=Bellingham"); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-6930">[CALCITE-6930] + * Implementing JoinConditionOrExpansionRule</a>. */ + @Test void testJoinConditionOrExpansionRule() { + final String sql = "" + + "SELECT \"t1\".\"deptno\", \"t1\".\"empid\", \"t2\".\"deptno\", \"t2\".\"empid\"\n" + + "FROM \"hr\".\"emps\" AS \"t1\"\n" + + "INNER JOIN (\n" + + " SELECT (\"deptno\" + 10) AS \"deptno\", (\"empid\" + 100) AS \"empid\" FROM \"hr\".\"emps\"\n" + + ") AS \"t2\"\n" + + "ON (\"t1\".\"deptno\" = \"t2\".\"deptno\") OR (\"t1\".\"empid\" = \"t2\".\"empid\")"; + + String[] returns = new String[] { + "deptno=20; empid=200; deptno=20; empid=200", + "deptno=20; empid=200; deptno=20; empid=210", + "deptno=20; empid=200; deptno=20; empid=250"}; + + CalciteAssert.hr() + .query(sql) + .returnsUnordered(returns); + + CalciteAssert.hr() + .query(sql) + .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> { + planner.addRule(CoreRules.JOIN_CONDITION_OR_EXPANSION_RULE); + planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE); + }) + .returnsUnordered(returns); + } + + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-6930">[CALCITE-6930] + * Implementing JoinConditionOrExpansionRule</a>. */ + @Test void testJoinConditionOrExpansionRuleLeft() { Review Comment: could you also add a test where the input table is a multiset, so we can check that multisets are handled correctly? You can make a multiset for example using `emp UNION ALL emp`. ########## core/src/test/java/org/apache/calcite/test/JdbcTest.java: ########## @@ -1975,6 +1975,69 @@ private void checkResultSetMetaData(Connection connection, String sql) "c0=Drink; c1=Dairy; c2=USA; c3=WA; c4=Bellingham"); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-6930">[CALCITE-6930] + * Implementing JoinConditionOrExpansionRule</a>. */ + @Test void testJoinConditionOrExpansionRule() { + final String sql = "" + + "SELECT \"t1\".\"deptno\", \"t1\".\"empid\", \"t2\".\"deptno\", \"t2\".\"empid\"\n" + + "FROM \"hr\".\"emps\" AS \"t1\"\n" + + "INNER JOIN (\n" + + " SELECT (\"deptno\" + 10) AS \"deptno\", (\"empid\" + 100) AS \"empid\" FROM \"hr\".\"emps\"\n" + + ") AS \"t2\"\n" + + "ON (\"t1\".\"deptno\" = \"t2\".\"deptno\") OR (\"t1\".\"empid\" = \"t2\".\"empid\")"; + + String[] returns = new String[] { + "deptno=20; empid=200; deptno=20; empid=200", + "deptno=20; empid=200; deptno=20; empid=210", + "deptno=20; empid=200; deptno=20; empid=250"}; + + CalciteAssert.hr() + .query(sql) + .returnsUnordered(returns); + + CalciteAssert.hr() + .query(sql) + .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> { + planner.addRule(CoreRules.JOIN_CONDITION_OR_EXPANSION_RULE); + planner.removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE); + }) + .returnsUnordered(returns); + } + + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-6930">[CALCITE-6930] + * Implementing JoinConditionOrExpansionRule</a>. */ + @Test void testJoinConditionOrExpansionRuleLeft() { + final String sql = "" Review Comment: how about a test as well where some of the values that appear in the condition are null? Mostly to check that the expansion such as `t1.age=t2.age AND t1.id≠t2.id` works correctly when NULLs are involved. -- 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]
