924060929 commented on code in PR #12996: URL: https://github.com/apache/doris/pull/12996#discussion_r996606953
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EffectivePredicatesExtractor.java: ########## @@ -0,0 +1,157 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * extract effective predicates. + */ +public class EffectivePredicatesExtractor extends PlanVisitor<Set<Expression>, Void> { + + PredicatePropagation propagation = new PredicatePropagation(); + + @Override + public Set<Expression> visit(Plan plan, Void context) { + return Sets.newHashSet(); + } + + @Override + public Set<Expression> visitLogicalFilter(LogicalFilter<? extends Plan> filter, Void context) { + List<Expression> predicates = ExpressionUtils.extractConjunction(filter.getPredicates()).stream() + .filter(p -> { + if (p instanceof SubqueryExpr) { + SubqueryExpr subqueryExpr = (SubqueryExpr) p; + return subqueryExpr.getCorrelateSlots().isEmpty(); + } Review Comment: We should keep the logical orthogonality(don't handle the logic that other rules will handle )? SubqueryExpr will be replace to some join by UnnestSubquery rule. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EffectivePredicatesExtractor.java: ########## @@ -0,0 +1,157 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * extract effective predicates. + */ +public class EffectivePredicatesExtractor extends PlanVisitor<Set<Expression>, Void> { + + PredicatePropagation propagation = new PredicatePropagation(); + + @Override + public Set<Expression> visit(Plan plan, Void context) { + return Sets.newHashSet(); + } + + @Override + public Set<Expression> visitLogicalFilter(LogicalFilter<? extends Plan> filter, Void context) { + List<Expression> predicates = ExpressionUtils.extractConjunction(filter.getPredicates()).stream() + .filter(p -> { + if (p instanceof SubqueryExpr) { + SubqueryExpr subqueryExpr = (SubqueryExpr) p; + return subqueryExpr.getCorrelateSlots().isEmpty(); + } + return true; + }).collect(Collectors.toList()); + predicates.addAll(filter.child().accept(this, context)); + return getAvailableExpressions(Sets.newHashSet(predicates), filter); + } + + @Override + public Set<Expression> visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join, Void context) { + Set<Expression> predicates = Sets.newHashSet(); + Set<Expression> leftPredicates = join.left().accept(this, context); + Set<Expression> rightPredicates = join.right().accept(this, context); + switch (join.getJoinType()) { + case INNER_JOIN: + case CROSS_JOIN: + predicates.addAll(leftPredicates); + predicates.addAll(rightPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case LEFT_SEMI_JOIN: + predicates.addAll(leftPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case RIGHT_SEMI_JOIN: + predicates.addAll(rightPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case LEFT_OUTER_JOIN: + case LEFT_ANTI_JOIN: + predicates.addAll(leftPredicates); + break; + case RIGHT_OUTER_JOIN: + case RIGHT_ANTI_JOIN: + predicates.addAll(rightPredicates); + break; + default: + } + return getAvailableExpressions(predicates, join); + } + + @Override + public Set<Expression> visitLogicalProject(LogicalProject<? extends Plan> project, Void context) { + Set<Expression> childPredicates = project.child().accept(this, context); + Map<Expression, Slot> expressionSlotMap = project.getAliasToProducer() + .entrySet() + .stream() + .collect(Collectors.toMap(Entry::getValue, Entry::getKey)); + Expression expression = ExpressionUtils.replace(ExpressionUtils.and(Lists.newArrayList(childPredicates)), + expressionSlotMap); + Set<Expression> predicates = Sets.newHashSet(); + predicates.addAll(ExpressionUtils.extractConjunction(expression)); + return getAvailableExpressions(predicates, project); + } + + @Override + public Set<Expression> visitLogicalAggregate(LogicalAggregate<? extends Plan> aggregate, Void context) { + Set<Expression> childPredicates = aggregate.child().accept(this, context); + Map<Expression, Slot> expressionSlotMap = aggregate.getOutputExpressions() + .stream() + .filter(this::hasAgg) + .collect(Collectors.toMap( + namedExpr -> { + if (namedExpr instanceof Alias) { + return ((Alias) namedExpr).child(); + } else { + return namedExpr; + } + }, NamedExpression::toSlot) + ); + Expression expression = ExpressionUtils.replace(ExpressionUtils.and(Lists.newArrayList(childPredicates)), + expressionSlotMap); + Set<Expression> predicates = Sets.newHashSet(); + predicates.addAll(ExpressionUtils.extractConjunction(expression)); + return getAvailableExpressions(predicates, aggregate); + } + + public Set<Expression> getAvailableExpressions(Set<Expression> predicates, Plan plan) { + predicates.addAll(propagation.infer(Lists.newArrayList(predicates))); + return predicates.stream() + .filter(p -> new HashSet<>(plan.getOutput()).containsAll(p.getInputSlots())) + .collect(Collectors.toSet()); + } + + private boolean hasAgg(Expression expression) { + if (expression instanceof AggregateFunction) { + return true; + } + for (Expression child : expression.children()) { + if (hasAgg(child)) { + return true; + } + } + return false; + } Review Comment: ```suggestion private boolean hasAgg(Expression expression) { return expression.anyMatch(AggregateFunction.class::isInstance); } ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EffectivePredicatesExtractor.java: ########## @@ -0,0 +1,157 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * extract effective predicates. + */ +public class EffectivePredicatesExtractor extends PlanVisitor<Set<Expression>, Void> { + + PredicatePropagation propagation = new PredicatePropagation(); + + @Override + public Set<Expression> visit(Plan plan, Void context) { + return Sets.newHashSet(); + } + + @Override + public Set<Expression> visitLogicalFilter(LogicalFilter<? extends Plan> filter, Void context) { + List<Expression> predicates = ExpressionUtils.extractConjunction(filter.getPredicates()).stream() + .filter(p -> { + if (p instanceof SubqueryExpr) { + SubqueryExpr subqueryExpr = (SubqueryExpr) p; + return subqueryExpr.getCorrelateSlots().isEmpty(); + } + return true; + }).collect(Collectors.toList()); + predicates.addAll(filter.child().accept(this, context)); + return getAvailableExpressions(Sets.newHashSet(predicates), filter); + } + + @Override + public Set<Expression> visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join, Void context) { + Set<Expression> predicates = Sets.newHashSet(); + Set<Expression> leftPredicates = join.left().accept(this, context); + Set<Expression> rightPredicates = join.right().accept(this, context); + switch (join.getJoinType()) { + case INNER_JOIN: + case CROSS_JOIN: + predicates.addAll(leftPredicates); + predicates.addAll(rightPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case LEFT_SEMI_JOIN: + predicates.addAll(leftPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case RIGHT_SEMI_JOIN: + predicates.addAll(rightPredicates); + join.getOnClauseCondition().map(on -> predicates.addAll(ExpressionUtils.extractConjunction(on))); + break; + case LEFT_OUTER_JOIN: + case LEFT_ANTI_JOIN: + predicates.addAll(leftPredicates); + break; + case RIGHT_OUTER_JOIN: + case RIGHT_ANTI_JOIN: + predicates.addAll(rightPredicates); + break; + default: + } + return getAvailableExpressions(predicates, join); + } + + @Override + public Set<Expression> visitLogicalProject(LogicalProject<? extends Plan> project, Void context) { + Set<Expression> childPredicates = project.child().accept(this, context); + Map<Expression, Slot> expressionSlotMap = project.getAliasToProducer() + .entrySet() + .stream() + .collect(Collectors.toMap(Entry::getValue, Entry::getKey)); + Expression expression = ExpressionUtils.replace(ExpressionUtils.and(Lists.newArrayList(childPredicates)), + expressionSlotMap); + Set<Expression> predicates = Sets.newHashSet(); + predicates.addAll(ExpressionUtils.extractConjunction(expression)); + return getAvailableExpressions(predicates, project); + } + + @Override + public Set<Expression> visitLogicalAggregate(LogicalAggregate<? extends Plan> aggregate, Void context) { + Set<Expression> childPredicates = aggregate.child().accept(this, context); + Map<Expression, Slot> expressionSlotMap = aggregate.getOutputExpressions() + .stream() + .filter(this::hasAgg) + .collect(Collectors.toMap( + namedExpr -> { + if (namedExpr instanceof Alias) { + return ((Alias) namedExpr).child(); + } else { + return namedExpr; + } + }, NamedExpression::toSlot) + ); + Expression expression = ExpressionUtils.replace(ExpressionUtils.and(Lists.newArrayList(childPredicates)), + expressionSlotMap); + Set<Expression> predicates = Sets.newHashSet(); + predicates.addAll(ExpressionUtils.extractConjunction(expression)); + return getAvailableExpressions(predicates, aggregate); + } + + public Set<Expression> getAvailableExpressions(Set<Expression> predicates, Plan plan) { + predicates.addAll(propagation.infer(Lists.newArrayList(predicates))); + return predicates.stream() + .filter(p -> new HashSet<>(plan.getOutput()).containsAll(p.getInputSlots())) + .collect(Collectors.toSet()); + } + + private boolean hasAgg(Expression expression) { + if (expression instanceof AggregateFunction) { + return true; + } + for (Expression child : expression.children()) { + if (hasAgg(child)) { + return true; + } + } + return false; + } Review Comment: don't repeat the common recursive traversal function(in TreeNode), if no any common traversal function meet your needs, and new function to TreeNode ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/InferPredicates.java: ########## @@ -0,0 +1,123 @@ +// 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.PlanRuleFactory; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RulePromise; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * infer additional predicates for filter and join. + */ +public class InferPredicates implements PlanRuleFactory { + PredicatePropagation propagation = new PredicatePropagation(); + EffectivePredicatesExtractor predicatesExtractor = new EffectivePredicatesExtractor(); + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + inferWhere(), + inferOn() + ); + } + + private Rule inferWhere() { + return logicalFilter(any()).thenApply(ctx -> { + LogicalFilter<Plan> root = ctx.root; + Plan filter = ctx.cascadesContext.getMemo().copyOut(root.getGroupExpression().get(), false); + Set<Expression> filterPredicates = filter.accept(predicatesExtractor, null); + Set<Expression> filterChildPredicates = filter.child(0).accept(predicatesExtractor, null); + filterPredicates.removeAll(filterChildPredicates); + ExpressionUtils.extractConjunction(root.getPredicates()).forEach(filterPredicates::remove); Review Comment: I think this rule should process the current predicate, and not need to remove the predicate which exist in the child, this is the responsibility of another rule. keep the logical orthogonality too, and this rule will become more clarity, so you not need to copy out from the memo in the EffectivePredicateExtractor. ########## 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); } ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/InferPredicates.java: ########## @@ -0,0 +1,123 @@ +// 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.PlanRuleFactory; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RulePromise; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * infer additional predicates for filter and join. + */ +public class InferPredicates implements PlanRuleFactory { + PredicatePropagation propagation = new PredicatePropagation(); + EffectivePredicatesExtractor predicatesExtractor = new EffectivePredicatesExtractor(); + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + inferWhere(), + inferOn() + ); + } + + private Rule inferWhere() { + return logicalFilter(any()).thenApply(ctx -> { + LogicalFilter<Plan> root = ctx.root; + Plan filter = ctx.cascadesContext.getMemo().copyOut(root.getGroupExpression().get(), false); + Set<Expression> filterPredicates = filter.accept(predicatesExtractor, null); + Set<Expression> filterChildPredicates = filter.child(0).accept(predicatesExtractor, null); + filterPredicates.removeAll(filterChildPredicates); + ExpressionUtils.extractConjunction(root.getPredicates()).forEach(filterPredicates::remove); + if (!filterPredicates.isEmpty()) { + filterPredicates.add(root.getPredicates()); + return new LogicalFilter<>(ExpressionUtils.and(Lists.newArrayList(filterPredicates)), root.child()); + } + return root; + }).toRule(RuleType.INFER_PREDICATES_FOR_WHERE); + } + + private Rule inferOn() { + return logicalJoin(any(), any()).thenApply(ctx -> { + LogicalJoin<Plan, Plan> root = ctx.root; + JoinType joinType = root.getJoinType(); + Plan left = root.left(); + Plan right = root.right(); + Plan originalLeft = ctx.cascadesContext.getMemo().copyOut(left.getGroupExpression().get(), false); + Plan originalRight = ctx.cascadesContext.getMemo().copyOut(right.getGroupExpression().get(), false); + Optional<Expression> condition = root.getOnClauseCondition(); + Set<Expression> expressions = getAllExpressions(originalLeft, originalRight, condition); Review Comment: ditto ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EffectivePredicatesExtractor.java: ########## @@ -0,0 +1,157 @@ +// 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.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * extract effective predicates. Review Comment: What does effective mean? Please add comment to sum up. -- 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