suibianwanwank commented on code in PR #4280: URL: https://github.com/apache/calcite/pull/4280#discussion_r2038998022
########## core/src/main/java/org/apache/calcite/rel/rules/JoinConditionOrExpansionRule.java: ########## @@ -0,0 +1,401 @@ +/* + * 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.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.tools.RelBuilder; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Planner rule that matches a + * {@link org.apache.calcite.rel.core.Join} + * and expands OR clauses in join conditions. + * + * <p>This rule expands OR conditions in join clauses into + * multiple separate join conditions, allowing the optimizer + * to handle these conditions more efficiently. + * + * <p>The following is an example of inner join, + * and other examples for other kinds of joins + * can be found in the code below. + * + * <p>Project[*] + * └── Join[OR(t1.id=t2.id, t1.age=t2.age), inner] + * ├── TableScan[t1] + * └── TableScan[t2] + * + * <p>into + * + * <p>Project[*] + * └── UnionAll + * ├── Join[t1.id=t2.id, inner] + * │ ├── TableScan[t1] + * │ └── TableScan[t2] + * └── Join[t1.age=t2.age AND t1.id≠t2.id, inner] + * ├─── TableScan[t1] + * └─── TableScan[t2] + * + */ [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(); + + RelNode converted; + switch (join.getJoinType()) { + case INNER: + converted = expandInnerJoin(join, relBuilder); + break; + case ANTI: + converted = expandAntiJoin(join, relBuilder); + break; + case LEFT: + converted = expandLeftOrRightJoin(join, true, relBuilder); + break; + case RIGHT: + converted = expandLeftOrRightJoin(join, false, relBuilder); + break; + case FULL: + converted = expandFullJoin(join, relBuilder); + break; + default: + return; + } + + if (converted == null) { + return; + } + call.transformTo(converted); + } + + private boolean skip(Join join) { Review Comment: This seems to be a precondition for whether this rule is applied or not, maybe we can put it in match? ########## core/src/main/java/org/apache/calcite/rel/rules/JoinCommuteRule.java: ########## @@ -137,6 +137,12 @@ public JoinCommuteRule(Class<? extends Join> clazz, .build(); } + public static RexNode swapJoinCond(RexNode cond, Join join, RexBuilder rexBuilder) { Review Comment: I don't know if there's a practice where one rule references another rule's method, but generally speaking, rules should be independent. You could try moving the method outside the rules. ########## core/src/main/java/org/apache/calcite/rel/rules/JoinConditionOrExpansionRule.java: ########## @@ -0,0 +1,401 @@ +/* + * 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.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.tools.RelBuilder; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Planner rule that matches a + * {@link org.apache.calcite.rel.core.Join} + * and expands OR clauses in join conditions. + * + * <p>This rule expands OR conditions in join clauses into + * multiple separate join conditions, allowing the optimizer + * to handle these conditions more efficiently. + * + * <p>The following is an example of inner join, + * and other examples for other kinds of joins + * can be found in the code below. + * + * <p>Project[*] + * └── Join[OR(t1.id=t2.id, t1.age=t2.age), inner] + * ├── TableScan[t1] + * └── TableScan[t2] + * + * <p>into + * + * <p>Project[*] + * └── UnionAll + * ├── Join[t1.id=t2.id, inner] + * │ ├── TableScan[t1] + * │ └── TableScan[t2] + * └── Join[t1.age=t2.age AND t1.id≠t2.id, inner] + * ├─── TableScan[t1] + * └─── TableScan[t2] + * + */ [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(); + + RelNode converted; + switch (join.getJoinType()) { + case INNER: + converted = expandInnerJoin(join, relBuilder); + break; + case ANTI: + converted = expandAntiJoin(join, relBuilder); + break; + case LEFT: + converted = expandLeftOrRightJoin(join, true, relBuilder); + break; + case RIGHT: + converted = expandLeftOrRightJoin(join, false, relBuilder); + break; + case FULL: + converted = expandFullJoin(join, relBuilder); + break; + default: + return; + } + + if (converted == null) { + return; + } + call.transformTo(converted); + } + + private boolean skip(Join join) { + List<RexNode> orConds = RelOptUtil.disjunctions(join.getCondition()); + return orConds.size() <= 1; + } + + private List<RexNode> splitCond(Join join) { + final RexBuilder builder = join.getCluster().getRexBuilder(); + final List<RexNode> orConds = RelOptUtil.disjunctions(join.getCondition()); + final int leftFieldCount = join.getLeft().getRowType().getFieldCount(); + + final List<RexNode> result = new ArrayList<>(); + final List<RexNode> otherBuffer = new ArrayList<>(); + + for (RexNode cond : orConds) { + if (isUsefulCond(cond, leftFieldCount)) { + if (!otherBuffer.isEmpty()) { + result.add(RexUtil.composeDisjunction(builder, otherBuffer)); + otherBuffer.clear(); + } + result.add(cond); + } else { + otherBuffer.add(cond); + } + } + + if (!otherBuffer.isEmpty()) { + result.add(RexUtil.composeDisjunction(builder, otherBuffer)); + } + + return result; + } + + private boolean isUsefulCond(RexNode node, int leftFieldCount) { + if (!(node instanceof RexCall)) { + return false; + } + + RexCall call = (RexCall) node; + SqlKind kind = call.getKind(); + switch (kind) { + case EQUALS: + case IS_NOT_DISTINCT_FROM: + RexNode left = call.getOperands().get(0); Review Comment: Perhaps this method name could be clearer, like `containEquivCond`. BTW, since Calcite is a general-purpose database with a strict type system. In many cases, the inputRef in join conditions will contain casts, which may cause some optimizations to fail. This issue exists across several rules. I'll try to collect such examples and create a new ticket to address. -- 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]
