924060929 commented on code in PR #12996: URL: https://github.com/apache/doris/pull/12996#discussion_r996698062
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PredicatePropagation.java: ########## @@ -0,0 +1,100 @@ +// 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.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.SlotReference; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * derive additional predicates. + * for example: + * a = b and a = 1 => b = 1 + */ +public class PredicatePropagation { + + /** + * infer additional predicates. + */ + public Set<Expression> infer(List<Expression> predicates) { + Set<Expression> inferred = Sets.newHashSet(); + for (Expression predicate : predicates) { + if (canEquivalentDeduce(predicate)) { + List<Expression> candidates = subtract(predicates, predicate); + candidates.forEach(candidate -> { + inferred.add(transform(candidate, predicate.child(0), predicate.child(1))); + }); + } + } + predicates.forEach(inferred::remove); + return inferred; + } + + private Expression transform(Expression expression, Expression source, Expression target) { + Expression rewritten = replace(expression, source, target); + if (expression.equals(rewritten)) { + rewritten = mapChildren(expression, source, target); + if (expression.equals(rewritten)) { + return expression; + } else { + return rewritten; + } + } else { + return mapChildren(rewritten, source, target); + } + } + + private Expression mapChildren(Expression expression, Expression source, Expression target) { + if (!expression.children().isEmpty()) { + List<Expression> children = Lists.newArrayList(); + for (Expression child : expression.children()) { + children.add(transform(child, source, target)); + } + return expression.withChildren(children); + } else { + return expression; + } + } + + private Expression replace(Expression expression, Expression source, Expression target) { + if (expression.equals(source)) { + return target; + } + if (expression.equals(target)) { + return source; + } + return expression; + } + + private boolean canEquivalentDeduce(Expression predicate) { + return predicate instanceof EqualTo && predicate.children().stream().allMatch(e -> e instanceof SlotReference); + } + + private List<Expression> subtract(List<Expression> expressions, Expression target) { + ArrayList<Expression> cloneList = Lists.newArrayList(expressions); + cloneList.remove(target); + return cloneList; + } +} Review Comment: Do not recursively traverse/rewrite manually, too. Use DefaultExpressionRewriter: ```java class PredicatePropagation { public Set<Expression> infer(List<Expression> predicates) { Set<Expression> inferred = Sets.newHashSet(); for (Expression predicate : predicates) { if (canEquivalentDeduce(predicate)) { Expression leftSlotEqualToRightSlot = predicate; List<Expression> newInferred = predicates.stream() .filter(p -> !p.equals(leftSlotEqualToRightSlot)) .map(p -> doInfer(leftSlotEqualToRightSlot, p)) .collect(Collectors.toList()); inferred.addAll(newInferred); } } predicates.forEach(inferred::remove); return inferred; } private Expression doInfer(Expression leftSlotEqualToRightSlot, Expression expression) { return expression.accept(new DefaultExpressionRewriter<Void>() { @Override public Expression visit(Expression expr, Void context) { expr = super.visit(expr, context); // flip leftSlot and rightSlot if (expr.equals(leftSlotEqualToRightSlot.child(0))) { return expr.child(1); } else if (expr.equals(leftSlotEqualToRightSlot.child(1))) { return expr.child(0); } else { return expr; } } }, null); } private boolean canEquivalentDeduce(Expression predicate) { return predicate instanceof EqualTo && predicate.children().stream().allMatch(e -> e instanceof SlotReference); } ``` or you can add the common rewrite method to TreeNode: ```java /** * bottom-up rewrite. * @param rewriteFunction rewrite function. * @return rewritten result. */ default NODE_TYPE rewriteUp(Function<NODE_TYPE, NODE_TYPE> rewriteFunction) { boolean changed = false; Builder<NODE_TYPE> newChildren = ImmutableList.builderWithExpectedSize(arity()); for (NODE_TYPE child : children()) { NODE_TYPE newChild = child.rewriteUp(rewriteFunction); if (child != newChild) { changed = true; } newChildren.add(newChild); } NODE_TYPE rewrittenChildren = changed ? withChildren(newChildren.build()) : (NODE_TYPE) this; return rewriteFunction.apply(rewrittenChildren); } ``` then ```java private Expression doInfer(Expression leftSlotEqualToRightSlot, Expression expression) { return expression.rewriteUp(expr -> { if (expr.equals(leftSlotEqualToRightSlot.child(0))) { return expr.child(1); } else if (expr.equals(leftSlotEqualToRightSlot.child(1))) { return expr.child(0); } else { return expr; } }); } ``` -- 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