Skip to content

Commit

Permalink
modify from cache method
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Dec 4, 2023
1 parent 36fa988 commit 720ea15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
38 changes: 28 additions & 10 deletions fe/fe-core/src/main/java/org/apache/doris/mtmv/MVCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,13 +41,10 @@ public class MVCache {
// this should be shuttle expression with lineage
private final List<NamedExpression> mvOutputExpressions;
// the context when parse, analyze, optimize the mv logical plan
private final CascadesContext context;

public MVCache(MTMV materializedView, Plan logicalPlan, List<NamedExpression> mvOutputExpressions,
CascadesContext context) {
public MVCache(MTMV materializedView, Plan logicalPlan, List<NamedExpression> mvOutputExpressions) {
this.logicalPlan = logicalPlan;
this.mvOutputExpressions = mvOutputExpressions;
this.context = context;
}

public Plan getLogicalPlan() {
Expand All @@ -49,14 +55,26 @@ public List<NamedExpression> getMvOutputExpressions() {
return mvOutputExpressions;
}

public MVCache(Plan logicalPlan, List<NamedExpression> mvOutputExpressions, CascadesContext context) {
public MVCache(Plan logicalPlan, List<NamedExpression> mvOutputExpressions) {
this.logicalPlan = logicalPlan;
this.mvOutputExpressions = mvOutputExpressions;
this.context = context;
}

public static MVCache from(MTMV mtmv, Plan logicalPlan,
List<NamedExpression> 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<NamedExpression> mvOutputExpressions = mvAnalyzedPlan.getExpressions().stream()
.map(NamedExpression.class::cast)
.collect(Collectors.toList());
return new MVCache(mvPlan, mvOutputExpressions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<NamedExpression> mvOutputExpressions = mvCache.getMvOutputExpressions();
// mv output expression shuttle, this will be used to expression rewrite
Expand Down

0 comments on commit 720ea15

Please sign in to comment.