From baa0524e377e8589b8def88a44d753b14b23eaa2 Mon Sep 17 00:00:00 2001 From: 924060929 <924060929@qq.com> Date: Mon, 1 Jul 2024 21:18:03 +0800 Subject: [PATCH] refactor analyze view --- .../apache/doris/nereids/CascadesContext.java | 12 +- .../executor/AbstractBatchJobExecutor.java | 39 ++++- .../doris/nereids/jobs/executor/Analyzer.java | 165 ++++++++---------- .../rewrite/PlanTreeRewriteBottomUpJob.java | 22 ++- .../jobs/rewrite/PlanTreeRewriteJob.java | 6 +- .../rewrite/PlanTreeRewriteTopDownJob.java | 22 ++- .../jobs/rewrite/RootPlanTreeRewriteJob.java | 14 +- .../nereids/rules/analysis/BindRelation.java | 2 +- 8 files changed, 158 insertions(+), 124 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java index 3b9ba912383e2f1..a0d748c08c72d0e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java @@ -240,19 +240,11 @@ public void toMemo() { } public Analyzer newAnalyzer() { - return newAnalyzer(false); - } - - public Analyzer newAnalyzer(boolean analyzeView) { - return new Analyzer(this, analyzeView); - } - - public Analyzer newAnalyzer(boolean analyzeView, Optional customTableResolver) { - return new Analyzer(this, analyzeView, customTableResolver); + return newAnalyzer(Optional.empty()); } public Analyzer newAnalyzer(Optional customTableResolver) { - return newAnalyzer(false, customTableResolver); + return new Analyzer(this, customTableResolver); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/AbstractBatchJobExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/AbstractBatchJobExecutor.java index bec86debc9ecac1..98ca1a16e8c6373 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/AbstractBatchJobExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/AbstractBatchJobExecutor.java @@ -29,6 +29,7 @@ import org.apache.doris.nereids.rules.Rule; import org.apache.doris.nereids.rules.RuleFactory; import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter; import com.google.common.collect.ImmutableList; @@ -36,6 +37,8 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -46,6 +49,8 @@ * Each batch of rules will be uniformly executed. */ public abstract class AbstractBatchJobExecutor { + private static final ThreadLocal>> NOT_TRAVERSE_CHILDREN = new ThreadLocal(); + private static final Predicate TRAVERSE_ALL_PLANS = plan -> true; protected CascadesContext cascadesContext; @@ -65,6 +70,16 @@ public static List jobs(RewriteJob... jobs) { ).collect(ImmutableList.toImmutableList()); } + public static List notTraverseChildrenOf( + Set> notTraverseClasses, Supplier> jobs) { + try { + NOT_TRAVERSE_CHILDREN.set((Set) notTraverseClasses); + return jobs.get(); + } finally { + NOT_TRAVERSE_CHILDREN.remove(); + } + } + public static TopicRewriteJob topic(String topicName, RewriteJob... jobs) { return new TopicRewriteJob(topicName, Arrays.asList(jobs)); } @@ -82,7 +97,7 @@ public static RewriteJob bottomUp(List ruleFactories) { .map(RuleFactory::buildRules) .flatMap(List::stream) .collect(ImmutableList.toImmutableList()); - return new RootPlanTreeRewriteJob(rules, PlanTreeRewriteBottomUpJob::new, true); + return new RootPlanTreeRewriteJob(rules, PlanTreeRewriteBottomUpJob::new, getTraversePredicate(), true); } public static RewriteJob topDown(RuleFactory... ruleFactories) { @@ -98,7 +113,7 @@ public static RewriteJob topDown(List ruleFactories, boolean once) .map(RuleFactory::buildRules) .flatMap(List::stream) .collect(ImmutableList.toImmutableList()); - return new RootPlanTreeRewriteJob(rules, PlanTreeRewriteTopDownJob::new, once); + return new RootPlanTreeRewriteJob(rules, PlanTreeRewriteTopDownJob::new, getTraversePredicate(), once); } public static RewriteJob custom(RuleType ruleType, Supplier planRewriter) { @@ -126,4 +141,24 @@ public void execute() { } public abstract List getJobs(); + + private static Predicate getTraversePredicate() { + Set> notTraverseChildren = NOT_TRAVERSE_CHILDREN.get(); + return notTraverseChildren == null + ? TRAVERSE_ALL_PLANS + : new NotTraverseChildren(notTraverseChildren); + } + + private static class NotTraverseChildren implements Predicate { + private final Set> notTraverseChildren; + + public NotTraverseChildren(Set> notTraverseChildren) { + this.notTraverseChildren = Objects.requireNonNull(notTraverseChildren, "notTraversePlans can not be null"); + } + + @Override + public boolean test(Plan plan) { + return !notTraverseChildren.contains(plan.getClass()); + } + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java index b72240cb8e503e0..4ba85ec813d9ac3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java @@ -48,6 +48,9 @@ import org.apache.doris.nereids.rules.rewrite.MergeProjects; import org.apache.doris.nereids.rules.rewrite.SemiJoinCommute; import org.apache.doris.nereids.rules.rewrite.SimplifyAggGroupBy; +import org.apache.doris.nereids.trees.plans.logical.LogicalView; + +import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Objects; @@ -59,8 +62,7 @@ */ public class Analyzer extends AbstractBatchJobExecutor { - public static final List DEFAULT_ANALYZE_JOBS = buildAnalyzeJobs(Optional.empty()); - public static final List DEFAULT_ANALYZE_VIEW_JOBS = buildAnalyzeViewJobs(Optional.empty()); + public static final List ANALYZE_JOBS = buildAnalyzeJobs(Optional.empty()); private final List jobs; @@ -69,36 +71,23 @@ public class Analyzer extends AbstractBatchJobExecutor { * @param cascadesContext planner context for execute job */ public Analyzer(CascadesContext cascadesContext) { - this(cascadesContext, false); - } - - public Analyzer(CascadesContext cascadesContext, boolean analyzeView) { - this(cascadesContext, analyzeView, Optional.empty()); + this(cascadesContext, Optional.empty()); } /** * constructor of Analyzer. For view, we only do bind relation since other analyze step will do by outer Analyzer. * * @param cascadesContext current context for analyzer - * @param analyzeView analyze view or user sql. If true, analyzer is used for view. * @param customTableResolver custom resolver for outer catalog. */ - public Analyzer(CascadesContext cascadesContext, boolean analyzeView, - Optional customTableResolver) { + public Analyzer(CascadesContext cascadesContext, Optional customTableResolver) { super(cascadesContext); Objects.requireNonNull(customTableResolver, "customTableResolver cannot be null"); - if (analyzeView) { - if (customTableResolver.isPresent()) { - this.jobs = buildAnalyzeViewJobs(customTableResolver); - } else { - this.jobs = DEFAULT_ANALYZE_VIEW_JOBS; - } + + if (customTableResolver.isPresent()) { + this.jobs = buildAnalyzeJobs(customTableResolver); } else { - if (customTableResolver.isPresent()) { - this.jobs = buildAnalyzeJobs(customTableResolver); - } else { - this.jobs = DEFAULT_ANALYZE_JOBS; - } + this.jobs = ANALYZE_JOBS; } } @@ -114,79 +103,71 @@ public void analyze() { execute(); } - private static List buildAnalyzeViewJobs(Optional customTableResolver) { - return jobs( - topDown(new AnalyzeCTE()), - topDown(new EliminateLogicalSelectHint()), - bottomUp( - new BindRelation(customTableResolver), - new CheckPolicy() - ) - ); - } - private static List buildAnalyzeJobs(Optional customTableResolver) { - return jobs( - // we should eliminate hint before "Subquery unnesting". - topDown(new AnalyzeCTE()), - topDown(new EliminateLogicalSelectHint()), - bottomUp( - new BindRelation(customTableResolver), - new CheckPolicy() - ), - bottomUp(new BindExpression()), - topDown(new BindSink()), - bottomUp(new CheckAfterBind()), - bottomUp( - new ProjectToGlobalAggregate(), - // this rule check's the logicalProject node's isDistinct property - // and replace the logicalProject node with a LogicalAggregate node - // so any rule before this, if create a new logicalProject node - // should make sure isDistinct property is correctly passed around. - // please see rule BindSlotReference or BindFunction for example - new EliminateDistinctConstant(), - new ProjectWithDistinctToAggregate(), - new ReplaceExpressionByChildOutput(), - new OneRowRelationExtractAggregate() - ), - topDown( - new FillUpMissingSlots(), - // We should use NormalizeRepeat to compute nullable properties for LogicalRepeat in the analysis - // stage. NormalizeRepeat will compute nullable property, add virtual slot, LogicalAggregate and - // LogicalProject for normalize. This rule depends on FillUpMissingSlots to fill up slots. - new NormalizeRepeat() - ), - bottomUp(new AdjustAggregateNullableForEmptySet()), - // consider sql with user defined var @t_zone - // set @t_zone='GMT'; - // SELECT - // DATE_FORMAT(convert_tz(dt, time_zone, @t_zone),'%Y-%m-%d') day - // FROM - // t - // GROUP BY - // 1; - // @t_zone must be replaced as 'GMT' before EliminateGroupByConstant and NormalizeAggregate rule. - // So need run VariableToLiteral rule before the two rules. - topDown(new VariableToLiteral()), - // run CheckAnalysis before EliminateGroupByConstant in order to report error message correctly like bellow - // select SUM(lo_tax) FROM lineorder group by 1; - // errCode = 2, detailMessage = GROUP BY expression must not contain aggregate functions: sum(lo_tax) - bottomUp(new CheckAnalysis()), - topDown(new EliminateGroupByConstant()), + return notTraverseChildrenOf( + ImmutableSet.of(LogicalView.class), + () -> jobs( + // we should eliminate hint before "Subquery unnesting". + topDown(new AnalyzeCTE()), + topDown(new EliminateLogicalSelectHint()), + bottomUp( + new BindRelation(customTableResolver), + new CheckPolicy() + ), + bottomUp(new BindExpression()), + topDown(new BindSink()), + bottomUp(new CheckAfterBind()), + bottomUp( + new ProjectToGlobalAggregate(), + // this rule check's the logicalProject node's isDistinct property + // and replace the logicalProject node with a LogicalAggregate node + // so any rule before this, if create a new logicalProject node + // should make sure isDistinct property is correctly passed around. + // please see rule BindSlotReference or BindFunction for example + new EliminateDistinctConstant(), + new ProjectWithDistinctToAggregate(), + new ReplaceExpressionByChildOutput(), + new OneRowRelationExtractAggregate() + ), + topDown( + new FillUpMissingSlots(), + // We should use NormalizeRepeat to compute nullable properties for LogicalRepeat in the analysis + // stage. NormalizeRepeat will compute nullable property, add virtual slot, LogicalAggregate and + // LogicalProject for normalize. This rule depends on FillUpMissingSlots to fill up slots. + new NormalizeRepeat() + ), + bottomUp(new AdjustAggregateNullableForEmptySet()), + // consider sql with user defined var @t_zone + // set @t_zone='GMT'; + // SELECT + // DATE_FORMAT(convert_tz(dt, time_zone, @t_zone),'%Y-%m-%d') day + // FROM + // t + // GROUP BY + // 1; + // @t_zone must be replaced as 'GMT' before EliminateGroupByConstant and NormalizeAggregate rule. + // So need run VariableToLiteral rule before the two rules. + topDown(new VariableToLiteral()), + // run CheckAnalysis before EliminateGroupByConstant in order to report error message correctly like bellow + // select SUM(lo_tax) FROM lineorder group by 1; + // errCode = 2, detailMessage = GROUP BY expression must not contain aggregate functions: sum(lo_tax) + bottomUp(new CheckAnalysis()), + topDown(new EliminateGroupByConstant()), - topDown(new SimplifyAggGroupBy()), - // run BuildAggForRandomDistributedTable before NormalizeAggregate in order to optimize the agg plan - topDown(new BuildAggForRandomDistributedTable()), - topDown(new NormalizeAggregate()), - topDown(new HavingToFilter()), - bottomUp(new SemiJoinCommute()), - bottomUp( - new CollectSubQueryAlias(), - new CollectJoinConstraint() - ), - topDown(new LeadingJoin()), - bottomUp(new SubqueryToApply()), - topDown(new MergeProjects()) + topDown(new SimplifyAggGroupBy()), + // run BuildAggForRandomDistributedTable before NormalizeAggregate in order to optimize the agg plan + topDown(new BuildAggForRandomDistributedTable()), + topDown(new NormalizeAggregate()), + topDown(new HavingToFilter()), + bottomUp(new SemiJoinCommute()), + bottomUp( + new CollectSubQueryAlias(), + new CollectJoinConstraint() + ), + topDown(new LeadingJoin()), + bottomUp(new SubqueryToApply()), + topDown(new MergeProjects()) + ) ); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteBottomUpJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteBottomUpJob.java index 60555a9cc04ad69..e4adc27b0f67d67 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteBottomUpJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteBottomUpJob.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.function.Predicate; /** * PlanTreeRewriteBottomUpJob @@ -55,8 +56,10 @@ enum RewriteState { ENSURE_CHILDREN_REWRITTEN } - public PlanTreeRewriteBottomUpJob(RewriteJobContext rewriteJobContext, JobContext context, List rules) { - super(JobType.BOTTOM_UP_REWRITE, context); + public PlanTreeRewriteBottomUpJob( + RewriteJobContext rewriteJobContext, JobContext context, + Predicate isTraverseChildren, List rules) { + super(JobType.BOTTOM_UP_REWRITE, context, isTraverseChildren); this.rewriteJobContext = Objects.requireNonNull(rewriteJobContext, "rewriteContext cannot be null"); this.rules = Objects.requireNonNull(rules, "rules cannot be null"); this.batchId = rewriteJobContext.batchId; @@ -97,7 +100,7 @@ private void rewriteThis() { return; } // After the rewrite take effect, we should handle the children part again. - pushJob(new PlanTreeRewriteBottomUpJob(newJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(newJobContext, context, isTraverseChildren, rules)); setState(rewriteResult.plan, RewriteState.ENSURE_CHILDREN_REWRITTEN, batchId); } else { // No new plan is generated, so just set the state of the current plan to 'REWRITTEN'. @@ -110,7 +113,7 @@ private void ensureChildrenRewritten() { Plan plan = rewriteJobContext.plan; int batchId = rewriteJobContext.batchId; setState(plan, RewriteState.REWRITE_THIS, batchId); - pushJob(new PlanTreeRewriteBottomUpJob(rewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(rewriteJobContext, context, isTraverseChildren, rules)); // some rule return new plan tree, which the number of new plan node > 1, // we should transform this new plan nodes too. @@ -122,31 +125,34 @@ private void ensureChildrenRewritten() { private void pushChildrenJobs(Plan plan) { List children = plan.children(); + if (!isTraverseChildren.test(plan)) { + return; + } switch (children.size()) { case 0: return; case 1: Plan child = children.get(0); RewriteJobContext childRewriteJobContext = new RewriteJobContext( child, rewriteJobContext, 0, false, batchId); - pushJob(new PlanTreeRewriteBottomUpJob(childRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(childRewriteJobContext, context, isTraverseChildren, rules)); return; case 2: Plan right = children.get(1); RewriteJobContext rightRewriteJobContext = new RewriteJobContext( right, rewriteJobContext, 1, false, batchId); - pushJob(new PlanTreeRewriteBottomUpJob(rightRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(rightRewriteJobContext, context, isTraverseChildren, rules)); Plan left = children.get(0); RewriteJobContext leftRewriteJobContext = new RewriteJobContext( left, rewriteJobContext, 0, false, batchId); - pushJob(new PlanTreeRewriteBottomUpJob(leftRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(leftRewriteJobContext, context, isTraverseChildren, rules)); return; default: for (int i = children.size() - 1; i >= 0; i--) { child = children.get(i); childRewriteJobContext = new RewriteJobContext( child, rewriteJobContext, i, false, batchId); - pushJob(new PlanTreeRewriteBottomUpJob(childRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteBottomUpJob(childRewriteJobContext, context, isTraverseChildren, rules)); } } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteJob.java index c2b136c40fad785..0f87a745b5e43f1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteJob.java @@ -31,12 +31,16 @@ import com.google.common.collect.ImmutableList; import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; /** PlanTreeRewriteJob */ public abstract class PlanTreeRewriteJob extends Job { + protected final Predicate isTraverseChildren; - public PlanTreeRewriteJob(JobType type, JobContext context) { + public PlanTreeRewriteJob(JobType type, JobContext context, Predicate isTraverseChildren) { super(type, context); + this.isTraverseChildren = Objects.requireNonNull(isTraverseChildren, "isTraverseChildren can not be null"); } protected final RewriteResult rewrite(Plan plan, List rules, RewriteJobContext rewriteJobContext) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteTopDownJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteTopDownJob.java index 14019bc885e0d05..ff863a878872bdb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteTopDownJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/PlanTreeRewriteTopDownJob.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Objects; +import java.util.function.Predicate; /** * PlanTreeRewriteTopDownJob @@ -36,8 +37,10 @@ public class PlanTreeRewriteTopDownJob extends PlanTreeRewriteJob { private final RewriteJobContext rewriteJobContext; private final List rules; - public PlanTreeRewriteTopDownJob(RewriteJobContext rewriteJobContext, JobContext context, List rules) { - super(JobType.TOP_DOWN_REWRITE, context); + public PlanTreeRewriteTopDownJob( + RewriteJobContext rewriteJobContext, JobContext context, + Predicate isTraverseChildren, List rules) { + super(JobType.TOP_DOWN_REWRITE, context, isTraverseChildren); this.rewriteJobContext = Objects.requireNonNull(rewriteJobContext, "rewriteContext cannot be null"); this.rules = Objects.requireNonNull(rules, "rules cannot be null"); } @@ -49,12 +52,12 @@ public void execute() { if (rewriteResult.hasNewPlan) { RewriteJobContext newContext = rewriteJobContext .withPlanAndChildrenVisited(rewriteResult.plan, false); - pushJob(new PlanTreeRewriteTopDownJob(newContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(newContext, context, isTraverseChildren, rules)); return; } RewriteJobContext newRewriteJobContext = rewriteJobContext.withChildrenVisited(true); - pushJob(new PlanTreeRewriteTopDownJob(newRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(newRewriteJobContext, context, isTraverseChildren, rules)); // NOTICE: this relay on pull up cte anchor if (!(this.rewriteJobContext.plan instanceof LogicalCTEAnchor)) { @@ -71,28 +74,31 @@ public void execute() { } private void pushChildrenJobs(RewriteJobContext rewriteJobContext) { + if (!isTraverseChildren.test(rewriteJobContext.plan)) { + return; + } List children = rewriteJobContext.plan.children(); switch (children.size()) { case 0: return; case 1: RewriteJobContext childRewriteJobContext = new RewriteJobContext( children.get(0), rewriteJobContext, 0, false, this.rewriteJobContext.batchId); - pushJob(new PlanTreeRewriteTopDownJob(childRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(childRewriteJobContext, context, isTraverseChildren, rules)); return; case 2: RewriteJobContext rightRewriteJobContext = new RewriteJobContext( children.get(1), rewriteJobContext, 1, false, this.rewriteJobContext.batchId); - pushJob(new PlanTreeRewriteTopDownJob(rightRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(rightRewriteJobContext, context, isTraverseChildren, rules)); RewriteJobContext leftRewriteJobContext = new RewriteJobContext( children.get(0), rewriteJobContext, 0, false, this.rewriteJobContext.batchId); - pushJob(new PlanTreeRewriteTopDownJob(leftRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(leftRewriteJobContext, context, isTraverseChildren, rules)); return; default: for (int i = children.size() - 1; i >= 0; i--) { childRewriteJobContext = new RewriteJobContext( children.get(i), rewriteJobContext, i, false, this.rewriteJobContext.batchId); - pushJob(new PlanTreeRewriteTopDownJob(childRewriteJobContext, context, rules)); + pushJob(new PlanTreeRewriteTopDownJob(childRewriteJobContext, context, isTraverseChildren, rules)); } } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RootPlanTreeRewriteJob.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RootPlanTreeRewriteJob.java index 4949d422613df04..13d0b5f875d1636 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RootPlanTreeRewriteJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/rewrite/RootPlanTreeRewriteJob.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; /** RootPlanTreeRewriteJob */ public class RootPlanTreeRewriteJob implements RewriteJob { @@ -36,11 +37,19 @@ public class RootPlanTreeRewriteJob implements RewriteJob { private final List rules; private final RewriteJobBuilder rewriteJobBuilder; private final boolean once; + private final Predicate isTraverseChildren; + public RootPlanTreeRewriteJob(List rules, RewriteJobBuilder rewriteJobBuilder, boolean once) { + this(rules, rewriteJobBuilder, plan -> true, once); + } + + public RootPlanTreeRewriteJob( + List rules, RewriteJobBuilder rewriteJobBuilder, Predicate isTraverseChildren, boolean once) { this.rules = Objects.requireNonNull(rules, "rules cannot be null"); this.rewriteJobBuilder = Objects.requireNonNull(rewriteJobBuilder, "rewriteJobBuilder cannot be null"); this.once = once; + this.isTraverseChildren = isTraverseChildren; } @Override @@ -52,7 +61,7 @@ public void execute(JobContext context) { int batchId = BATCH_ID.incrementAndGet(); RootRewriteJobContext rewriteJobContext = new RootRewriteJobContext( root, false, context, batchId); - Job rewriteJob = rewriteJobBuilder.build(rewriteJobContext, context, rules); + Job rewriteJob = rewriteJobBuilder.build(rewriteJobContext, context, isTraverseChildren, rules); context.getScheduleContext().pushJob(rewriteJob); cascadesContext.getJobScheduler().executeJobPool(cascadesContext); @@ -67,7 +76,8 @@ public boolean isOnce() { /** RewriteJobBuilder */ public interface RewriteJobBuilder { - Job build(RewriteJobContext rewriteJobContext, JobContext jobContext, List rules); + Job build(RewriteJobContext rewriteJobContext, JobContext jobContext, + Predicate isTraverseChildren, List rules); } /** RootRewriteJobContext */ diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java index f92a302bd02338d..ddb0e199e8cebd8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java @@ -347,7 +347,7 @@ private Plan parseAndAnalyzeView(TableIf view, String ddlSql, CascadesContext pa CascadesContext viewContext = CascadesContext.initContext( parentContext.getStatementContext(), parsedViewPlan, PhysicalProperties.ANY); viewContext.keepOrShowPlanProcess(parentContext.showPlanProcess(), () -> { - viewContext.newAnalyzer(true, customTableResolver).analyze(); + viewContext.newAnalyzer(customTableResolver).analyze(); }); parentContext.addPlanProcesses(viewContext.getPlanProcesses()); // we should remove all group expression of the plan which in other memo, so the groupId would not conflict