morrySnow commented on code in PR #16411:
URL: https://github.com/apache/doris/pull/16411#discussion_r1105428879


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java:
##########
@@ -101,6 +101,9 @@ public enum RuleType {
     ELIMINATE_GROUP_BY_CONSTANT(RuleTypeClass.REWRITE),
     ELIMINATE_ORDER_BY_CONSTANT(RuleTypeClass.REWRITE),
     INFER_PREDICATES(RuleTypeClass.REWRITE),
+    INFER_FILTER_NOT_NULL(RuleTypeClass.REWRITE),
+    INFER_JOIN_NOT_NULL(RuleTypeClass.REWRITE),

Review Comment:
   not used rule type?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java:
##########
@@ -45,8 +45,8 @@
  * Abstract class for all Expression in Nereids.
  */
 public abstract class Expression extends AbstractTreeNode<Expression> 
implements ExpressionTrait {
-
-    private static final String INPUT_CHECK_ERROR_MESSAGE = "argument %d 
requires %s type, however '%s' is of %s type";
+    // Mask this expression is generated by rule, should be removed.
+    public boolean isGeneratedIsNotNull = false;

Review Comment:
   should we process this attr in each withXXX function? i think we will lose 
this tag after rewrite.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateNotNull.java:
##########
@@ -0,0 +1,80 @@
+// 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.doris.nereids.rules.rewrite.logical;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.PlanUtils;
+import org.apache.doris.nereids.util.TypeUtils;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * EliminateNotNull.
+ */
+public class EliminateNotNull extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalFilter()
+            .when(filter -> filter.getConjuncts().stream().anyMatch(expr -> 
expr.isGeneratedIsNotNull))
+            .then(filter -> {
+                // 1. get all slots from `is not null`.
+                // 2. infer nonNullable slots.
+                // 3. remove `is not null` predicates.
+                Set<Expression> removeIsNotNullPredicates = Sets.newHashSet();
+                List<Slot> slotsFromIsNotNull = Lists.newArrayList();
+                filter.getConjuncts().stream()
+                        .filter(expr -> !expr.isGeneratedIsNotNull)
+                        .forEach(expr -> {
+                            Optional<Slot> notNullSlot = 
TypeUtils.isNotNull(expr);
+                            if (notNullSlot.isPresent()) {
+                                slotsFromIsNotNull.add(notNullSlot.get());
+                            } else {
+                                removeIsNotNullPredicates.add(expr);
+                            }
+                        });
+                Set<Slot> inferNonNotSlots = 
ExpressionUtils.inferNotNullSlots(removeIsNotNullPredicates);
+
+                Set<Expression> keepIsNotNull = slotsFromIsNotNull.stream()
+                        .filter(ExpressionTrait::nullable)
+                        .filter(slot -> !inferNonNotSlots.contains(slot))
+                        .map(slot -> new Not(new 
IsNull(slot))).collect(Collectors.toSet());
+
+                // merge removeIsNotNullPredicates and keepIsNotNull into a 
new ImmutableSet
+                Set<Expression> newPredicates = 
ImmutableSet.<Expression>builder()
+                        .addAll(removeIsNotNullPredicates)
+                        .addAll(keepIsNotNull)
+                        .build();

Review Comment:
   this rule is hard to understand, please add some example to explain it 



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to