924060929 commented on code in PR #12996:
URL: https://github.com/apache/doris/pull/12996#discussion_r1015094281


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PredicatePropagation.java:
##########
@@ -0,0 +1,102 @@
+// 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.trees.expressions.ComparisonPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * derive additional predicates.
+ * for example:
+ * a = b and a = 1 => b = 1
+ */
+public class PredicatePropagation {
+
+    /**
+     * infer additional predicates.
+     */
+    public Set<Expression> infer(Set<Expression> predicates) {
+        Set<Expression> inferred = Sets.newHashSet();
+        for (Expression predicate : predicates) {
+            if (canEquivalentInfer(predicate)) {
+                List<Expression> newInferred = predicates.stream()
+                        .filter(p -> !p.equals(predicate))
+                        .map(p -> doInfer(predicate, p))
+                        .collect(Collectors.toList());
+                inferred.addAll(newInferred);
+            }
+        }
+        inferred.removeAll(predicates);
+        return inferred;
+    }
+
+    /**
+     * Use the left or right child of `leftSlotEqualToRightSlot` to replace 
the left or right child of `expression`
+     * Now only support infer `ComparisonPredicate`.
+     * TODO: We should determine whether `expression` satisfies the condition 
for replacement
+     *       eg: Satisfy `expression` is non-deterministic
+     */
+    private Expression doInfer(Expression leftSlotEqualToRightSlot, Expression 
expression) {
+        return expression.accept(new DefaultExpressionRewriter<Void>() {
+
+            @Override
+            public Expression visit(Expression expr, Void context) {
+                return expr;
+            }
+
+            @Override
+            public Expression visitComparisonPredicate(ComparisonPredicate cp, 
Void context) {
+                if (!cp.left().isConstant() && !cp.right().isConstant()) {
+                    return cp;
+                }
+                return super.visit(cp, context);
+            }
+
+            @Override
+            public Expression visitSlotReference(SlotReference slotReference, 
Void context) {
+                if (slotReference.equals(leftSlotEqualToRightSlot.child(0))) {
+                    return leftSlotEqualToRightSlot.child(1);
+                } else if 
(slotReference.equals(leftSlotEqualToRightSlot.child(1))) {
+                    return leftSlotEqualToRightSlot.child(0);
+                } else {
+                    return slotReference;
+                }
+            }

Review Comment:
   Declare the shape in the visitComparisonPredicate will more clearer.
   ```java
   @Override
   public Expression visitComparisonPredicate(ComparisonPredicate cp, Void 
context) {
       if (cp.left().isSlot() && cp.right().isConstant()) {
           return swapSlot(cp);
       } else if (cp.left().isConstant() && cp.right().isSlot()) {
           return swapSlot(cp);
       }
       return super.visit(cp, context);
   }
   
   private Expression swapSlot(Expression expr) {
       return expr.rewriteUp(e -> {
           if (e.equals(leftSlotEqualToRightSlot.child(0)) {
               return leftSlotEqualToRightSlot.child(1);
           } else if (e.equals(leftSlotEqualToRightSlot.child(1)) {
               return leftSlotEqualToRightSlot.child(0);
           } else {
               return e;
           }
       });
   }
   ```



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