diff --git a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MVCache.java b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MVCache.java index 7490566a7f475c..d45fa70cf33d54 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MVCache.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MVCache.java @@ -18,11 +18,20 @@ package org.apache.doris.mtmv; import org.apache.doris.catalog.MTMV; -import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.NereidsPlanner; +import org.apache.doris.nereids.StatementContext; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.properties.PhysicalProperties; import org.apache.doris.nereids.trees.expressions.NamedExpression; import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalResultSink; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.OriginStatement; import java.util.List; +import java.util.stream.Collectors; /**The cache for materialized view cache */ public class MVCache { @@ -32,13 +41,10 @@ public class MVCache { // this should be shuttle expression with lineage private final List mvOutputExpressions; // the context when parse, analyze, optimize the mv logical plan - private final CascadesContext context; - public MVCache(MTMV materializedView, Plan logicalPlan, List mvOutputExpressions, - CascadesContext context) { + public MVCache(MTMV materializedView, Plan logicalPlan, List mvOutputExpressions) { this.logicalPlan = logicalPlan; this.mvOutputExpressions = mvOutputExpressions; - this.context = context; } public Plan getLogicalPlan() { @@ -49,14 +55,26 @@ public List getMvOutputExpressions() { return mvOutputExpressions; } - public MVCache(Plan logicalPlan, List mvOutputExpressions, CascadesContext context) { + public MVCache(Plan logicalPlan, List mvOutputExpressions) { this.logicalPlan = logicalPlan; this.mvOutputExpressions = mvOutputExpressions; - this.context = context; } - public static MVCache from(MTMV mtmv, Plan logicalPlan, - List mvOutputExpressions, CascadesContext context) { - return new MVCache(mtmv, logicalPlan, mvOutputExpressions, context); + public static MVCache from(MTMV mtmv, ConnectContext connectContext) { + LogicalPlan unboundMvPlan = new NereidsParser().parseSingle(mtmv.getQuerySql()); + // TODO: connect context set current db when create mv by use database + StatementContext mvSqlStatementContext = new StatementContext(connectContext, + new OriginStatement(mtmv.getQuerySql(), 0)); + NereidsPlanner planner = new NereidsPlanner(mvSqlStatementContext); + + planner.plan(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.ALL_PLAN); + Plan mvAnalyzedPlan = planner.getAnalyzedPlan(); + Plan mvRewrittenPlan = planner.getRewrittenPlan(); + Plan mvPlan = mvRewrittenPlan instanceof LogicalResultSink + ? (Plan) ((LogicalResultSink) mvRewrittenPlan).child() : mvRewrittenPlan; + List mvOutputExpressions = mvAnalyzedPlan.getExpressions().stream() + .map(NamedExpression.class::cast) + .collect(Collectors.toList()); + return new MVCache(mvPlan, mvOutputExpressions); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializationContext.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializationContext.java index 8b1e75017caaaf..39e759a148700b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializationContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializationContext.java @@ -21,19 +21,11 @@ import org.apache.doris.catalog.Table; import org.apache.doris.mtmv.MVCache; import org.apache.doris.nereids.CascadesContext; -import org.apache.doris.nereids.NereidsPlanner; -import org.apache.doris.nereids.StatementContext; import org.apache.doris.nereids.memo.GroupId; -import org.apache.doris.nereids.parser.NereidsParser; -import org.apache.doris.nereids.properties.PhysicalProperties; import org.apache.doris.nereids.rules.exploration.mv.mapping.ExpressionMapping; import org.apache.doris.nereids.trees.expressions.NamedExpression; import org.apache.doris.nereids.trees.plans.Plan; -import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel; -import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; -import org.apache.doris.nereids.trees.plans.logical.LogicalResultSink; import org.apache.doris.nereids.util.ExpressionUtils; -import org.apache.doris.qe.OriginStatement; import com.google.common.collect.ImmutableList; @@ -72,21 +64,7 @@ public MaterializationContext(MTMV mtmv, MVCache mvCache = mtmv.getMvCache(); // TODO This logic should move to materialized view cache manager if (mvCache == null) { - LogicalPlan unboundMvPlan = new NereidsParser().parseSingle(mtmv.getQuerySql()); - StatementContext mvSqlStatementContext = new StatementContext( - cascadesContext.getConnectContext(), - new OriginStatement(mtmv.getQuerySql(), 0)); - NereidsPlanner planner = new NereidsPlanner(mvSqlStatementContext); - - planner.plan(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.ALL_PLAN); - Plan mvAnalyzedPlan = planner.getAnalyzedPlan(); - Plan mvRewrittenPlan = planner.getRewrittenPlan(); - mvCache = MVCache.from(mtmv, mvRewrittenPlan instanceof LogicalResultSink - ? (Plan) ((LogicalResultSink) mvRewrittenPlan).child() : mvRewrittenPlan, - mvAnalyzedPlan.getExpressions().stream().map(NamedExpression.class::cast) - .collect(Collectors.toList()), - planner.getCascadesContext()); - mtmv.setMvCache(mvCache); + mtmv.setMvCache(MVCache.from(mtmv, cascadesContext.getConnectContext())); } List mvOutputExpressions = mvCache.getMvOutputExpressions(); // mv output expression shuttle, this will be used to expression rewrite