xiedeyantu commented on code in PR #5175:
URL: https://github.com/apache/calcite/pull/5175#discussion_r3763477679


##########
core/src/main/java/org/apache/calcite/rel/rules/OuterJoinToAntiJoinRule.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.plan.RelTraitSet;
+import org.apache.calcite.plan.Strong;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalFilter;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.type.RelDataType;
+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.apache.calcite.util.ImmutableBitSet;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that converts an outer join followed by {@code IS NULL}
+ * on its null-generating side to an anti join.
+ *
+ * <p>For example, if {@code Dept.deptno} is NOT NULL, the query
+ *
+ * <pre>{@code
+ * SELECT e.*
+ * FROM Emp AS e
+ * LEFT JOIN Dept AS d ON e.deptno = d.deptno
+ * WHERE d.deptno IS NULL
+ * }</pre>
+ *
+ * <p>is equivalent to
+ *
+ * <pre>{@code
+ * SELECT e.*
+ * FROM Emp AS e
+ * WHERE NOT EXISTS (
+ *   SELECT 1
+ *   FROM Dept AS d
+ *   WHERE e.deptno = d.deptno)
+ * }</pre>
+ *
+ * <p>The tested field must be declared NOT NULL, or the join condition must
+ * not be TRUE when the field is NULL. This prevents matched rows containing
+ * a real NULL from being mistaken for null-generated outer-join rows.
+ */
[email protected]
+public class OuterJoinToAntiJoinRule
+    extends RelRule<OuterJoinToAntiJoinRule.Config>
+    implements TransformationRule {
+
+  /** Creates an OuterJoinToAntiJoinRule. */
+  protected OuterJoinToAntiJoinRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Filter filter = call.rel(0);
+    final Join join = call.rel(1);
+
+    // Field indexes below assume that the join has no system-field prefix.
+    if (!join.getSystemFieldList().isEmpty()) {
+      return;
+    }
+    // Rewriting may change the number and order of condition evaluations.
+    if (!RexUtil.isDeterministic(filter.getCondition())
+        || !RexUtil.isDeterministic(join.getCondition())) {
+      return;
+    }
+
+    final boolean leftJoin = join.getJoinType() == JoinRelType.LEFT;
+    // Only top-level conjuncts can independently prove that a row is 
unmatched.
+    final List<RexNode> remainingConditions =
+        new ArrayList<>(RelOptUtil.conjunctions(filter.getCondition()));
+    final RexNode nullCondition =
+        findSafeNullCondition(remainingConditions, join, leftJoin);
+    if (nullCondition == null) {
+      return;
+    }
+    remainingConditions.remove(nullCondition);
+
+    final RelNode newLeft = leftJoin ? join.getLeft() : join.getRight();
+    final RelNode newRight = leftJoin ? join.getRight() : join.getLeft();
+    final RexNode condition = leftJoin
+        ? join.getCondition()
+        : JoinCommuteRule.swapJoinCond(join.getCondition(), join,
+            join.getCluster().getRexBuilder());
+    final RelTraitSet traitSet = join.getTraitSet();
+    final Join antiJoin =
+        join.copy(traitSet, condition, newLeft, newRight,

Review Comment:
   Would it be better to switch to `RelBuilder.join`?



##########
core/src/test/java/org/apache/calcite/test/OuterJoinToAntiJoinRuleTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.test;
+
+import org.apache.calcite.rel.rules.CoreRules;
+import org.apache.calcite.rel.rules.OuterJoinToAntiJoinRule;
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Test;
+
+/** Tests for {@link OuterJoinToAntiJoinRule}. */
+class OuterJoinToAntiJoinRuleTest {

Review Comment:
   We can add JIRA title in the java doc.



##########
core/src/test/resources/sql/planner.iq:
##########
@@ -685,4 +685,47 @@ EnumerableCalc(expr#0..3=[{inputs}], expr#4=[0], 
expr#5=[>($t2, $t4)], expr#6=[>
 !ok
 !set planner-rules original
 
+# OuterJoinToAntiJoinRule converts the null-generating side's IS NULL filter.

Review Comment:
   JIRA title also should be added here.



-- 
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]

Reply via email to