924060929 commented on code in PR #12996: URL: https://github.com/apache/doris/pull/12996#discussion_r1010421765
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PredicatePropagation.java: ########## @@ -0,0 +1,85 @@ +// 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 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` + */ + 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 leftSlotEqualToRightSlot.child(1); + } else if (expr.equals(leftSlotEqualToRightSlot.child(1))) { + return leftSlotEqualToRightSlot.child(0); + } else { + return expr; + } + } + }, null); + } + + /** + * Currently only equivalence derivation is supported + * and requires that the left and right sides of an expression must be slot + */ + private boolean canEquivalentInfer(Expression predicate) { + return predicate instanceof EqualTo && predicate.children().stream().allMatch(e -> e instanceof SlotReference); Review Comment: This condition has an implicit condition: this rule must invoke after TypeCoercion. Because we can not infer filter when the type is not equals: for example: a.id is int , b.id is string, the filter `a.id = b.id and concat(a.id, '0') = '10'` we can not infer a new filter `concat(b.id, '0') = '10'`, because the row a.id = 1 and b.id = '1.0' can through the join `a.id = b.id` and filter `concat(a.id, '0') = '10'`, but can not through the filter `concat(b.id, '0') = '10'`. I think you can add the implicit conditions to here: ```java return predicate instanceof EqualTo && predicate.children().stream().allMatch(e -> e instanceof SlotReference) && predicate.child(0).getDataType().equals(predicate.child(1).getDataType(); ``` The second implicit condition: this rule dependent the action: every non-deterministic in the same sql should equals. for example: `a.id = b.id and a.id=rand()`, if we can not ensure this condition and infer `b.id=rand()`, it will be wrong. Another case is the filter `a.id = b.id and a.id = c.id`, infer `b.id = c.id` maybe useless and have some executing overhead, so maybe we should add some rule to delete some useless expression in the future, or just process this case: `column1 = column2 and anyExpression compareTo anyExpressionNotContainsSlotReference` So maybe we should only process the case `a.id = b.id and a.id compareTo literal` for safety? -- 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