xiedeyantu commented on code in PR #4322:
URL: https://github.com/apache/calcite/pull/4322#discussion_r2056078245
##########
core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java:
##########
@@ -10407,4 +10407,38 @@ private void
checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
.withRule(CoreRules.JOIN_CONDITION_PUSH)
.check();
}
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6973">[CALCITE-6973]
+ * Add rule for convert Minus to Filter</a>. */
+ @Test void testMinusToFilterRule() {
+ final String sql = "SELECT mgr, comm FROM emp WHERE mgr = 12\n"
+ + "EXCEPT\n"
+ + "SELECT mgr, comm FROM emp WHERE comm = 5\n";
+ sql(sql)
+ .withPreRule(CoreRules.PROJECT_FILTER_TRANSPOSE)
+ .withRule(CoreRules.MINUS_TO_FILTER)
+ .check();
+ }
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6973">[CALCITE-6973]
+ * Add rule for convert Minus to Filter</a>. */
+ @Test void testMinusToFilterRule2() {
+ final Function<RelBuilder, RelNode> relFn = b -> b
+ .scan("EMP")
+ .project(b.field("MGR"), b.field("COMM"))
+ .filter(b.lessThan(b.field("MGR"), b.literal(20)))
Review Comment:
@NobiGo @asolimando Done!
##########
core/src/main/java/org/apache/calcite/rel/rules/MinusToFilterRule.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.
+ * This rule enables merging all filter conditions into a composite condition
+ * when processing consecutively nested filters.
+ *
+ * <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());
+ }
+
+ /**
+ * This method iteratively traverses the input node, stripping away
+ * and extracting conditions from Filter operators at each level,
+ * ultimately merging all conditions into a composite condition
+ * while returning the first encountered non-Filter operator.
+ */
+ 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) {
+ Filter filter = (Filter) stripped;
+ if (cond == null) {
+ cond = filter.getCondition();
+ } else {
+ cond =
+ RexUtil.composeConjunction(builder, ImmutableList.of(cond,
filter.getCondition()));
+ }
+ stripped = filter.getInput().stripped();
+ }
+ return Pair.of(stripped, cond);
+ }
+
+ /** Rule configuration. */
+ @Value.Immutable
+ public interface Config extends RelRule.Config {
+ Config DEFAULT = ImmutableMinusToFilterRule.Config.of()
+ .withOperandFor(Minus.class, RelNode.class, Filter.class);
+
+ @Override default MinusToFilterRule toRule() {
+ return new MinusToFilterRule(this);
+ }
+
+ /** Defines an operand tree for the given classes. */
+ default Config withOperandFor(Class<? extends Minus> minusClass,
+ Class<? extends RelNode> relNodeClass, Class<? extends Filter>
filterClass) {
+ return withOperandSupplier(
+ b0 -> b0.operand(minusClass).inputs(
+ b1 -> b1.operand(relNodeClass).anyInputs(),
Review Comment:
Done!
--
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]