mihaibudiu commented on code in PR #4366: URL: https://github.com/apache/calcite/pull/4366#discussion_r2083624011
########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with + // input indices for map-based grouping. + Map<Pair<RelNode, Integer>, List<@Nullable RexNode>> sourceToConds = + new LinkedHashMap<>(); + + RelNode firstSource = first.left; + sourceToConds.computeIfAbsent(Pair.of(firstSource, null), + k -> new ArrayList<>()).add(first.right); + + for (int i = 1; i < inputs.size(); i++) { + final RelNode input = inputs.get(i).stripped(); + final Pair<RelNode, @Nullable RexNode> pair = extractSourceAndCond(input); + sourceToConds.computeIfAbsent(Pair.of(pair.left, pair.right != null ? null : i), + k -> new ArrayList<>()).add(pair.right); + } + + if (sourceToConds.size() == inputs.size()) { + return; + } + + int branchCount = 0; + for (Map.Entry<Pair<RelNode, Integer>, List<@Nullable RexNode>> entry + : sourceToConds.entrySet()) { + Pair<RelNode, Integer> left = entry.getKey(); + List<@Nullable RexNode> conds = entry.getValue(); + // If the condition is met (refer to extractSourceAndCond method), + // directly add its corresponding input to the new inputs list. + if (conds.size() == 1 && conds.get(0) == null) { + builder.push(left.left); + branchCount++; + continue; + } + + RexNode combinedCond = + combineConditions(builder, conds, setOp, left.left == firstSource); + + builder.push(left.left) + .filter(combinedCond); + branchCount++; + } + + buildSetOp(builder, branchCount, setOp) Review Comment: the distinct is no longer necessary if you do the setop. if there is only one group, it is ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with + // input indices for map-based grouping. + Map<Pair<RelNode, Integer>, List<@Nullable RexNode>> sourceToConds = + new LinkedHashMap<>(); + + RelNode firstSource = first.left; + sourceToConds.computeIfAbsent(Pair.of(firstSource, null), + k -> new ArrayList<>()).add(first.right); + + for (int i = 1; i < inputs.size(); i++) { + final RelNode input = inputs.get(i).stripped(); + final Pair<RelNode, @Nullable RexNode> pair = extractSourceAndCond(input); + sourceToConds.computeIfAbsent(Pair.of(pair.left, pair.right != null ? null : i), + k -> new ArrayList<>()).add(pair.right); + } + + if (sourceToConds.size() == inputs.size()) { + return; + } + + int branchCount = 0; + for (Map.Entry<Pair<RelNode, Integer>, List<@Nullable RexNode>> entry + : sourceToConds.entrySet()) { + Pair<RelNode, Integer> left = entry.getKey(); + List<@Nullable RexNode> conds = entry.getValue(); + // If the condition is met (refer to extractSourceAndCond method), + // directly add its corresponding input to the new inputs list. + if (conds.size() == 1 && conds.get(0) == null) { + builder.push(left.left); + branchCount++; + continue; + } + + RexNode combinedCond = + combineConditions(builder, conds, setOp, left.left == firstSource); + + builder.push(left.left) + .filter(combinedCond); + branchCount++; + } + + buildSetOp(builder, branchCount, setOp) Review Comment: you should not do the setop if there is only one input left ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with Review Comment: the checker is unhappy about your missing annotations ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> Review Comment: you should also describe what happens if there are multiple inputs here. only one setop will be sufficient for that case. ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with + // input indices for map-based grouping. + Map<Pair<RelNode, Integer>, List<@Nullable RexNode>> sourceToConds = + new LinkedHashMap<>(); + + RelNode firstSource = first.left; + sourceToConds.computeIfAbsent(Pair.of(firstSource, null), + k -> new ArrayList<>()).add(first.right); + + for (int i = 1; i < inputs.size(); i++) { + final RelNode input = inputs.get(i).stripped(); + final Pair<RelNode, @Nullable RexNode> pair = extractSourceAndCond(input); + sourceToConds.computeIfAbsent(Pair.of(pair.left, pair.right != null ? null : i), + k -> new ArrayList<>()).add(pair.right); + } + + if (sourceToConds.size() == inputs.size()) { + return; + } + + int branchCount = 0; + for (Map.Entry<Pair<RelNode, Integer>, List<@Nullable RexNode>> entry + : sourceToConds.entrySet()) { + Pair<RelNode, Integer> left = entry.getKey(); + List<@Nullable RexNode> conds = entry.getValue(); + // If the condition is met (refer to extractSourceAndCond method), + // directly add its corresponding input to the new inputs list. + if (conds.size() == 1 && conds.get(0) == null) { + builder.push(left.left); + branchCount++; + continue; + } + + RexNode combinedCond = + combineConditions(builder, conds, setOp, left.left == firstSource); + + builder.push(left.left) + .filter(combinedCond); + branchCount++; + } + + buildSetOp(builder, branchCount, setOp) + .distinct(); + call.transformTo(builder.build()); + } + + private static RelBuilder buildSetOp(RelBuilder builder, int count, RelNode setOp) { + if (setOp instanceof Union) { + return builder.union(false, count); + } else if (setOp instanceof Intersect) { + return builder.intersect(false, count); + } else if (setOp instanceof Minus) { + return builder.minus(false, count); + } + // unreachable + throw new IllegalStateException("unreachable code"); + } + + private static Pair<RelNode, @Nullable RexNode> extractSourceAndCond(RelNode input) { + if (input instanceof Filter) { + Filter filter = (Filter) input; + if (!RexUtil.isDeterministic(filter.getCondition()) + || RexUtil.SubQueryFinder.containsSubQuery(filter)) { + // If filter has non-deterministic expressions or subqueries, + // just return current input and null cond. + return Pair.of(input, null); + } + return Pair.of(filter.getInput().stripped(), filter.getCondition()); + } + return Pair.of(input.stripped(), + input.getCluster().getRexBuilder().makeLiteral(true)); + } + + private static RexNode andFirstNotRest(RelBuilder builder, List<RexNode> conds) { Review Comment: a short comment would help, although the function is pretty clear builds `conds.get(0) && !conds.get(1) && ... ` ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with Review Comment: what makes a position "invalid?" you haven't explained yet ########## core/src/main/java/org/apache/calcite/rel/rules/SetOpToFilterRule.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.core.Intersect; +import org.apache.calcite.rel.core.Minus; +import org.apache.calcite.rel.core.SetOp; +import org.apache.calcite.rel.core.Union; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Pair; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Rule that replaces {@link SetOp} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * For nested filters, the rule {@link CoreRules#FILTER_MERGE} + * should be used prior to invoking this one. + * + * <p>Example: + * + * <p>UNION + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * UNION + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 OR comm = 5 + * </pre></blockquote> + * + * <p>INTERSECT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * INTERSECT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND comm = 5 + * </pre></blockquote> + * + * <p>EXCEPT + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * is rewritten to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class SetOpToFilterRule + extends RelRule<SetOpToFilterRule.Config> + implements TransformationRule { + + /** Creates an SetOpToFilterRule. */ + protected SetOpToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + config.matchHandler().accept(this, call); + } + + private void match(RelOptRuleCall call) { + final SetOp setOp = call.rel(0); + final List<RelNode> inputs = setOp.getInputs(); + if (setOp.all || inputs.size() < 2) { + return; + } + + final RelBuilder builder = call.builder(); + Pair<RelNode, RexNode> first = extractSourceAndCond(inputs.get(0).stripped()); + // Map to store conditions grouped by <sourceRelNode, inputPosition>. + // Valid cond positions are set to null and others are tagged with + // input indices for map-based grouping. + Map<Pair<RelNode, Integer>, List<@Nullable RexNode>> sourceToConds = + new LinkedHashMap<>(); + + RelNode firstSource = first.left; + sourceToConds.computeIfAbsent(Pair.of(firstSource, null), + k -> new ArrayList<>()).add(first.right); + + for (int i = 1; i < inputs.size(); i++) { + final RelNode input = inputs.get(i).stripped(); + final Pair<RelNode, @Nullable RexNode> pair = extractSourceAndCond(input); + sourceToConds.computeIfAbsent(Pair.of(pair.left, pair.right != null ? null : i), + k -> new ArrayList<>()).add(pair.right); + } + + if (sourceToConds.size() == inputs.size()) { + return; + } + + int branchCount = 0; + for (Map.Entry<Pair<RelNode, Integer>, List<@Nullable RexNode>> entry + : sourceToConds.entrySet()) { + Pair<RelNode, Integer> left = entry.getKey(); + List<@Nullable RexNode> conds = entry.getValue(); + // If the condition is met (refer to extractSourceAndCond method), + // directly add its corresponding input to the new inputs list. + if (conds.size() == 1 && conds.get(0) == null) { + builder.push(left.left); + branchCount++; + continue; + } + + RexNode combinedCond = + combineConditions(builder, conds, setOp, left.left == firstSource); + + builder.push(left.left) + .filter(combinedCond); + branchCount++; + } + + buildSetOp(builder, branchCount, setOp) Review Comment: but maybe the builder takes care of both of these? -- 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]
