xiedeyantu commented on code in PR #4322: URL: https://github.com/apache/calcite/pull/4322#discussion_r2053141657
########## core/src/main/java/org/apache/calcite/rel/rules/MinusToFilterRule.java: ########## @@ -0,0 +1,130 @@ +/* + * 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.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; + +import org.immutables.value.Value; + +/** + * Rule that replaces {@link org.apache.calcite.rel.core.Minus} + * operator with {@link org.apache.calcite.rel.core.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 RelNode leftInput = minus.getInput(0); + final RelNode rightInput = minus.getInput(1); + + final RelBuilder builder = call.builder(); + + final RelNode leftBase = getBaseSource(leftInput); + final RelNode rightBase = getBaseSource(rightInput); + + RexNode rightCond = extractFilterCond(rightInput); + if (!RexUtil.isDeterministic(rightCond)) { + return; + } + + // Verify they come from the same source + if (!leftBase.equals(rightBase)) { + return; + } + + final RexNode finalCond = + builder.and(extractFilterCond(leftInput), builder.not(rightCond)); + + builder.push(leftBase) + .filter(finalCond) + .distinct(); + + call.transformTo(builder.build()); + } + + /** + * Returns the base source of the given node, + * skipping first filter. + */ + private static RelNode getBaseSource(RelNode node) { + RelNode stripped = node.stripped(); + if (stripped instanceof Filter) { + return ((Filter) stripped).getInput(); + } else { + return node; + } + } + + private static RexNode extractFilterCond(RelNode node) { + RelNode stripped = node.stripped(); + if (stripped instanceof Filter) { + return ((Filter) stripped).getCondition(); + } + return node.getCluster().getRexBuilder().makeLiteral(true); Review Comment: > you can match on Minus(Filter, Filter) Great idea, this is better. -- 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]
