924060929 commented on code in PR #12996: URL: https://github.com/apache/doris/pull/12996#discussion_r1015076884
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/InferPredicates.java: ########## @@ -0,0 +1,118 @@ +// 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.jobs.JobContext; +import org.apache.doris.nereids.trees.expressions.Expression; +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.trees.plans.visitor.DefaultPlanRewriter; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; + +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * infer additional predicates for `LogicalFilter` and `LogicalJoin`. + */ +public class InferPredicates extends DefaultPlanRewriter<JobContext> { + PredicatePropagation propagation = new PredicatePropagation(); + PullUpPredicates pollUpPredicates = new PullUpPredicates(); + + /** + * The logic is as follows: + * 1. poll up bottom predicate then infer additional predicates + * for example: + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id + * 1. poll up bottom predicate + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t.id = 1 + * 2. infer + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t.id = 1 and t2.id = 1 + * finally transformed sql: + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t2.id = 1 + * 2. put these predicates into `otherJoinConjuncts` , these predicates are processed in the next + * round of predicate push-down Review Comment: Good comment :D, move before the class definition. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/VisitorRewriteJob.java: ########## @@ -0,0 +1,56 @@ +// 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.jobs.rewrite; + +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.jobs.Job; +import org.apache.doris.nereids.jobs.JobContext; +import org.apache.doris.nereids.jobs.JobType; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter; + +import java.util.Objects; + +/** + * Use visitor to rewrite the plan. + */ +public class VisitorRewriteJob extends Job { + private final Group group; + + private final DefaultPlanRewriter<JobContext> planRewriter; + + /** + * Constructor. + */ + public VisitorRewriteJob(CascadesContext cascadesContext, DefaultPlanRewriter<JobContext> rewriter, boolean once) { + super(JobType.VISITOR_REWRITE, cascadesContext.getCurrentJobContext(), once); + this.group = Objects.requireNonNull(cascadesContext.getMemo().getRoot(), "group cannot be null"); + this.planRewriter = Objects.requireNonNull(rewriter, "planRewriter cannot be null"); + } + + @Override + public void execute() { + GroupExpression logicalExpression = group.getLogicalExpression(); + Plan root = context.getCascadesContext().getMemo().copyOut(logicalExpression, false); + Plan accept = root.accept(planRewriter, context); Review Comment: ```suggestion Plan rewrittenRoot = root.accept(planRewriter, context); ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/VisitorRewriteJob.java: ########## @@ -0,0 +1,56 @@ +// 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.jobs.rewrite; + +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.jobs.Job; +import org.apache.doris.nereids.jobs.JobContext; +import org.apache.doris.nereids.jobs.JobType; +import org.apache.doris.nereids.memo.Group; +import org.apache.doris.nereids.memo.GroupExpression; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter; + +import java.util.Objects; + +/** + * Use visitor to rewrite the plan. + */ +public class VisitorRewriteJob extends Job { + private final Group group; + + private final DefaultPlanRewriter<JobContext> planRewriter; + + /** + * Constructor. + */ + public VisitorRewriteJob(CascadesContext cascadesContext, DefaultPlanRewriter<JobContext> rewriter, boolean once) { + super(JobType.VISITOR_REWRITE, cascadesContext.getCurrentJobContext(), once); Review Comment: You should support once or set always once. ########## 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: the 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; } }); } ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpPredicates.java: ########## @@ -0,0 +1,141 @@ +// 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.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.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * poll up effective predicates from operator's children. + */ +public class PullUpPredicates extends PlanVisitor<Set<Expression>, Void> { + + PredicatePropagation propagation = new PredicatePropagation(); + + @Override + public Set<Expression> visit(Plan plan, Void context) { + if (plan.arity() == 1) { + return plan.child(0).accept(this, context); + } + return Sets.newHashSet(); + } + + @Override + public Set<Expression> visitLogicalFilter(LogicalFilter<? extends Plan> filter, Void context) { + List<Expression> predicates = Lists.newArrayList(filter.getConjuncts()); + 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(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(ExpressionUtils.extractConjunction(expression)); + return getAvailableExpressions(predicates, aggregate); + } + + private Set<Expression> getAvailableExpressions(Set<Expression> predicates, Plan plan) { + predicates.addAll(propagation.infer(predicates)); Review Comment: Don't use side effect to modify the variable which owner out of this function, And `getXxx` has a modify operation is odd. You should create a new predicate set and copy the predicates from the parameter. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/InferPredicates.java: ########## @@ -0,0 +1,118 @@ +// 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.jobs.JobContext; +import org.apache.doris.nereids.trees.expressions.Expression; +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.trees.plans.visitor.DefaultPlanRewriter; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.Lists; + +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * infer additional predicates for `LogicalFilter` and `LogicalJoin`. + */ +public class InferPredicates extends DefaultPlanRewriter<JobContext> { + PredicatePropagation propagation = new PredicatePropagation(); + PullUpPredicates pollUpPredicates = new PullUpPredicates(); + + /** + * The logic is as follows: + * 1. poll up bottom predicate then infer additional predicates + * for example: + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id + * 1. poll up bottom predicate + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t.id = 1 + * 2. infer + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t.id = 1 and t2.id = 1 + * finally transformed sql: + * select * from (select * from t1 where t1.id = 1) t join t2 on t.id = t2.id and t2.id = 1 + * 2. put these predicates into `otherJoinConjuncts` , these predicates are processed in the next + * round of predicate push-down + */ + @Override + public Plan visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join, JobContext context) { + join = (LogicalJoin<? extends Plan, ? extends Plan>) super.visit(join, context); + Plan left = join.left(); + Plan right = join.right(); + Set<Expression> expressions = getAllExpressions(left, right, join.getOnClauseCondition()); + List<Expression> otherJoinConjuncts = Lists.newArrayList(join.getOtherJoinConjuncts()); + switch (join.getJoinType()) { + case INNER_JOIN: + case CROSS_JOIN: + case LEFT_SEMI_JOIN: + case RIGHT_SEMI_JOIN: + otherJoinConjuncts.addAll(inferNewPredicate(left, expressions)); + otherJoinConjuncts.addAll(inferNewPredicate(right, expressions)); + break; + case LEFT_OUTER_JOIN: + case LEFT_ANTI_JOIN: + otherJoinConjuncts.addAll(inferNewPredicate(right, expressions)); + break; + case RIGHT_OUTER_JOIN: + case RIGHT_ANTI_JOIN: + otherJoinConjuncts.addAll(inferNewPredicate(left, expressions)); + break; + default: + return join; + } + return join.withOtherJoinConjuncts(otherJoinConjuncts); + } + + /** + * reference `inferOn` + */ + @Override + public Plan visitLogicalFilter(LogicalFilter<? extends Plan> filter, JobContext context) { + filter = (LogicalFilter<? extends Plan>) super.visit(filter, context); + Set<Expression> filterPredicates = filter.accept(pollUpPredicates, null); + Set<Expression> filterChildPredicates = filter.child(0).accept(pollUpPredicates, null); Review Comment: This code seem like every plan child has many repeated traverse the all child plan tree by the pollUpPredicates. I think we should support invoke poll up and infer predicate which just process one level ```java filter = (LogicalFilter<? extends Plan>) super.visit(filter, context); Set<Expression> childPredicates = pollUpPredicate(filter); // just pull up one level child Set<Expression> mergedPredicates = ImmutableSet.Builder() .addAll(filter.getPredicates()) .addAll(childPredicates); Set<Expression> inferredPredicates = propagation.infer(mergedPredicates); if (!inferredPredicates.equals(filter.getPredicates()) { return new LogicalFilter(inferredPredicates, filter.child()); } return filter; ``` -- 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