mihaibudiu commented on code in PR #4322: URL: https://github.com/apache/calcite/pull/4322#discussion_r2055309183
########## core/src/main/java/org/apache/calcite/rel/rules/MinusToFilterRule.java: ########## @@ -0,0 +1,133 @@ +/* + * 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.Minus; +import org.apache.calcite.rex.RexBuilder; +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 com.google.common.collect.ImmutableList; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +/** + * Rule that replaces {@link Minus} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * + * <p>Example transformation: + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class MinusToFilterRule + extends RelRule<MinusToFilterRule.Config> + implements TransformationRule { + + /** Creates an MinusToFilterRule. */ + protected MinusToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + final Minus minus = call.rel(0); + if (minus.all || minus.getInputs().size() != 2) { Review Comment: I said that withOperandSupplier will only accept minus with two inputs. isn't that right? ########## core/src/main/java/org/apache/calcite/rel/rules/MinusToFilterRule.java: ########## @@ -0,0 +1,133 @@ +/* + * 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.Minus; +import org.apache.calcite.rex.RexBuilder; +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 com.google.common.collect.ImmutableList; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +/** + * Rule that replaces {@link Minus} operator with {@link Filter} + * when both inputs are from the same source with only filter conditions differing. + * + * <p>Example transformation: + * <blockquote><pre> + * SELECT mgr, comm FROM emp WHERE mgr = 12 + * EXCEPT + * SELECT mgr, comm FROM emp WHERE comm = 5 + * + * to + * + * SELECT DISTINCT mgr, comm FROM emp + * WHERE mgr = 12 AND NOT(comm = 5) + * </pre></blockquote> + */ [email protected] +public class MinusToFilterRule + extends RelRule<MinusToFilterRule.Config> + implements TransformationRule { + + /** Creates an MinusToFilterRule. */ + protected MinusToFilterRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + final Minus minus = call.rel(0); + if (minus.all || minus.getInputs().size() != 2) { + return; + } + final RelBuilder builder = call.builder(); + final RelNode leftInput = call.rel(1); + final Filter rightInput = call.rel(2); + + if (!RexUtil.isDeterministic(rightInput.getCondition())) { + return; + } + + final Pair<RelNode, @Nullable RexNode> leftBase = getUnfilteredInputAndMergeCond(leftInput); + final Pair<RelNode, @Nullable RexNode> rightBase = getUnfilteredInputAndMergeCond(rightInput); + if (!leftBase.left.equals(rightBase.left)) { + return; + } + + // Right input is Filter, right cond should be not null + assert rightBase.right != null; + final RexNode finalCond = leftBase.right != null + ? builder.and(leftBase.right, builder.not(rightBase.right)) + : builder.not(rightBase.right); + + builder.push(leftBase.left) + .filter(finalCond) + .distinct(); + + call.transformTo(builder.build()); + } + + private static Pair<RelNode, @Nullable RexNode> getUnfilteredInputAndMergeCond( + final RelNode node) { + RexBuilder builder = node.getCluster().getRexBuilder(); + RelNode stripped = node.stripped(); + RexNode cond = null; + while (stripped instanceof Filter) { Review Comment: I mean the JavaDoc for the rule. -- 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]
