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


##########
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,
+            JoinRelType.ANTI, join.isSemiJoinDone());
+
+    // An anti join projects only its left input. Its rows are unmatched, so 
every
+    // field of the null-generating input is NULL. Reinsert typed NULLs to 
restore
+    // the outer join's row type.
+    final RelBuilder builder = call.builder().push(antiJoin);
+    final int leftCount = join.getLeft().getRowType().getFieldCount();
+    final int nullOffset = leftJoin ? leftCount : 0;
+    final List<RexNode> projects = new ArrayList<>(builder.fields());
+    insertNulls(projects, join.getRowType(), nullOffset,
+        newRight.getRowType().getFieldCount(), builder);
+
+    builder.project(projects, join.getRowType().getFieldNames())
+        .filter(filter.getVariablesSet(), remainingConditions)
+        .convert(filter.getRowType(), false);
+    call.transformTo(builder.build());
+  }
+
+  /** Returns an {@code IS NULL} condition that identifies a null-generated
+   * outer-join row, or null if there is no such condition. */

Review Comment:
   The condition must be over an input field which is not nullable in the input.
   That is implied by the JavaDoc, but is subtle, it deserves to be in the 
JavaDoc.
   You can say `rightField IS NULL` where `rightField` is a non-nullable column 
from the right input (for a left join).



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

Review Comment:
   I think having a plan before and after is more useful than having this 
equivalent SQL.
   You can keep the SQL for the original query, though.



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