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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/InferPredicate.java:
##########
@@ -0,0 +1,225 @@
+// 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.expression.rewrite;
+
+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.InPredicate;
+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.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class InferPredicate {
+
+    private final Map<Expression, Set<Expression>> equivalentMap = 
Maps.newHashMap();
+    private final Map<Expression, Set<Expression>> predicateMap = 
Maps.newHashMap();
+
+    private final Visitor visitor = new Visitor();
+
+    public List<Expression> inferEffectivePredicate(Expression predicate) {
+        List<Expression> infers = infer(predicate);
+        return infers.stream().filter(p -> p.getInputSlots().size() == 
1).collect(Collectors.toList());
+    }
+
+
+    public List<Expression> infer(Expression predicate) {
+        List<Expression> splits = 
ExpressionUtils.extractConjunction(predicate);
+        splits.forEach(p -> p.accept(visitor, new InferContext()));
+        Set<Slot> inputSlots = predicate.getInputSlots();
+        Set<Expression> inferred = Sets.newLinkedHashSet();
+        inferred.addAll(splits);
+        for (Slot inputSlot : inputSlots) {
+            for (Expression search : equivalentSearch(inputSlot)) {
+                if (search instanceof EqualTo) {
+                    if (!inferred.contains(search) && 
!inferred.contains(((EqualTo) search).commute())) {
+                        inferred.add(search);
+                    }
+                    continue;
+                }
+                inferred.add(search);
+            }
+        }
+        splits.forEach(inferred::remove);
+        return ImmutableList.copyOf(inferred);
+    }
+
+    private static class InferContext {
+        Expression parent;
+
+        public Expression getParent() {
+            return parent;
+        }
+
+        public void setParent(Expression parent) {
+            this.parent = parent;
+        }
+    }
+
+    private class Visitor extends ExpressionVisitor<Void, InferContext> {
+
+        @Override
+        public Void visit(Expression expr, InferContext context) {
+            return null;
+        }
+
+        @Override
+        public Void visitEqualTo(EqualTo equalTo, InferContext context) {
+            Expression left = equalTo.left();
+            Expression right = equalTo.right();
+            if (left.isSlot() && right.isSlot()) {
+                Set<Expression> leftSet = equivalentMap.getOrDefault(left, 
Sets.newHashSet());
+                leftSet.add(right);
+                equivalentMap.putIfAbsent(left, leftSet);
+                Set<Expression> rightSet = equivalentMap.getOrDefault(right, 
Sets.newHashSet());
+                rightSet.add(left);
+                equivalentMap.putIfAbsent(right, rightSet);
+            }
+            visitComparisonPredicate(equalTo, context);
+            return null;
+        }
+
+        @Override
+        public Void visitComparisonPredicate(ComparisonPredicate cp, 
InferContext context) {
+            Optional<Expression> left = extractSlot(cp.left());
+            Optional<Expression> right = extractSlot(cp.right());
+            Expression target = null;
+            if (left.isPresent() && cp.right().isConstant()) {
+                target = left.get();
+            } else if (right.isPresent() && cp.left().isConstant()) {
+                target = right.get();
+            }
+            if (Objects.nonNull(target)) {
+                Set<Expression> targetSet = predicateMap.getOrDefault(target, 
Sets.newHashSet());
+                addPredicate(cp, targetSet, context);
+                predicateMap.putIfAbsent(target, targetSet);
+            }
+            return null;
+        }
+
+        @Override
+        public Void visitInPredicate(InPredicate inPredicate, InferContext 
context) {
+            Optional<Expression> cmp = 
extractSlot(inPredicate.getCompareExpr());
+            if (cmp.isPresent() && isAllConstant(inPredicate.getOptions())) {
+                Set<Expression> predicates = 
predicateMap.getOrDefault(cmp.get(), Sets.newHashSet());
+                addPredicate(inPredicate, predicates, context);
+                predicateMap.putIfAbsent(cmp.get(), predicates);
+            }
+            return null;
+        }
+
+        @Override
+        public Void visitIsNull(IsNull isNull, InferContext context) {
+            Optional<Expression> child = extractSlot(isNull.child());
+            if (child.isPresent()) {
+                Set<Expression> predicates = 
predicateMap.getOrDefault(child.get(), Sets.newHashSet());
+                addPredicate(isNull, predicates, context);
+                predicateMap.putIfAbsent(child.get(), predicates);
+            }
+            return null;
+        }
+
+        @Override
+        public Void visitNot(Not not, InferContext context) {
+            context.setParent(not);
+            not.child().accept(this, context);
+            return null;
+        }
+
+        private void addPredicate(Expression expr, Set<Expression> predicates, 
InferContext context) {
+            if (Objects.nonNull(context.getParent())) {
+                predicates.add(context.getParent());
+                return;
+            }
+            predicates.add(expr);
+        }
+
+        private boolean isAllConstant(List<Expression> expressions) {
+            return expressions.stream().allMatch(Expression::isConstant);
+        }
+
+        private Optional<Expression> extractSlot(Expression expression) {
+            Set<Slot> inputSlots = expression.getInputSlots();
+            if (inputSlots.size() == 1) {
+                for (Slot slot : inputSlots) {
+                    return Optional.of(slot);
+                }
+            }
+            return Optional.empty();
+        }
+
+    }
+
+    private Set<Expression> equivalentSearch(Expression slot) {

Review Comment:
   This method is a bit obscure, could we have some examples?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/InferPredicate.java:
##########
@@ -0,0 +1,225 @@
+// 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.expression.rewrite;
+
+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.InPredicate;
+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.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class InferPredicate {

Review Comment:
   We should have some examples to explain how this class works.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/ReplaceSlot.java:
##########
@@ -0,0 +1,46 @@
+// 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.util;
+
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
+
+import java.util.Map;
+import java.util.Objects;
+
+public class ReplaceSlot extends DefaultExpressionRewriter<Map<Expression, 
Expression>> {

Review Comment:
   It seems this is duplicated with `ExpressionUtil.replace`?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushDownJoinOtherCondition.java:
##########
@@ -62,7 +64,14 @@ public Rule build() {
             if (!join.getOtherJoinCondition().isPresent()) {
                 return null;
             }
+            InferPredicate inferPredicate = new InferPredicate();

Review Comment:
   Hmm. I'd suggest to adding a dedicated rule to infer predicates, rather than 
have side effects on predicates pushdonw rules.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushPredicatesThroughJoin.java:
##########
@@ -95,6 +97,12 @@ public Rule build() {
 
             Expression filterPredicates = filter.getPredicates();
 
+            InferPredicate inferPredicate = new InferPredicate();

Review Comment:
   ditto



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/InferPredicate.java:
##########
@@ -0,0 +1,225 @@
+// 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.expression.rewrite;
+
+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.InPredicate;
+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.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.util.ExpressionUtils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class InferPredicate {
+
+    private final Map<Expression, Set<Expression>> equivalentMap = 
Maps.newHashMap();

Review Comment:
   Please add some comments for these members.



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