From be85ee2b1c5b5a5c7aa5c1c96c0f960fd54747c6 Mon Sep 17 00:00:00 2001 From: seawinde Date: Thu, 7 Dec 2023 11:22:50 +0800 Subject: [PATCH] add some comment --- .../main/java/org/apache/doris/mtmv/MVCache.java | 1 - .../org/apache/doris/nereids/PlannerHook.java | 7 +++++++ .../mv/AbstractMaterializedViewJoinRule.java | 11 +++++++---- .../mv/AbstractMaterializedViewRule.java | 16 +++++++++------- .../mv/mapping/EquivalenceClassSetMapping.java | 4 +++- .../mv/mapping/ExpressionMapping.java | 2 +- 6 files changed, 27 insertions(+), 14 deletions(-) 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 231ccbf0f67f538..b5cf92f87e4e73d 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 @@ -42,7 +42,6 @@ public class MVCache { private final Plan logicalPlan; // this should be shuttle expression with lineage private final List mvOutputExpressions; - // the context when parse, analyze, optimize the mv logical plan public MVCache(MTMV materializedView, Plan logicalPlan, List mvOutputExpressions) { this.logicalPlan = logicalPlan; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/PlannerHook.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/PlannerHook.java index 0fb95a28e5582c8..18d71b539a94b32 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/PlannerHook.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/PlannerHook.java @@ -23,9 +23,16 @@ * For example: after analyze plan when query or explain, we should generate materialization context. */ public interface PlannerHook { + + /** + * the hook before analyze + */ default void beforeAnalyze(NereidsPlanner planner) { } + /** + * the hook after analyze + */ default void afterAnalyze(NereidsPlanner planner) { } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewJoinRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewJoinRule.java index acc6c271ca29d2e..384522c0f679c9e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewJoinRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewJoinRule.java @@ -65,6 +65,8 @@ protected Plan rewriteQueryByView(MatchMode matchMode, || expressionsRewritten.stream().anyMatch(expr -> !(expr instanceof NamedExpression))) { return null; } + // record the group id in materializationContext, and when rewrite again in + // the same group, bail out quickly. if (queryStructInfo.getOriginalPlan().getGroupExpression().isPresent()) { materializationContext.addMatchedGroup( queryStructInfo.getOriginalPlan().getGroupExpression().get().getOwnerGroup().getGroupId()); @@ -88,10 +90,11 @@ protected boolean checkPattern(StructInfo structInfo) { SUPPORTED_JOIN_TYPE_SET)) { return false; } - for (Edge edge : hyperGraph.getEdges()) { - if (!edge.getJoin().accept(StructInfo.JOIN_PATTERN_CHECKER, SUPPORTED_JOIN_TYPE_SET)) { - return false; - } + } + for (Edge edge : hyperGraph.getEdges()) { + if (!edge.getJoin().accept(StructInfo.JOIN_PATTERN_CHECKER, + SUPPORTED_JOIN_TYPE_SET)) { + return false; } } return true; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java index 5eff40bd4bd8967..09b2ea776150daf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java @@ -92,6 +92,7 @@ protected List rewrite(Plan queryPlan, CascadesContext cascadesContext) { } List queryToViewTableMappings = RelationMapping.generate(queryStructInfo.getRelations(), viewStructInfo.getRelations()); + // if any relation in query and view can not map, bail out. if (queryToViewTableMappings == null) { return rewriteResults; } @@ -161,7 +162,7 @@ protected Plan rewriteQueryByView(MatchMode matchMode, */ protected List rewriteExpression( List sourceExpressionsToWrite, - ExpressionMapping mvExpressionToMvScanExpressionMapping, + ExpressionMapping mvExprToMvScanExprMapping, SlotMapping sourceToTargetMapping) { // Firstly, rewrite the target plan output expression using query with inverse mapping // then try to use the mv expression to represent the query. if any of source expressions @@ -175,13 +176,14 @@ protected List rewriteExpression( // transform source to: // project(slot 2, 1) // target - // generate mvSql to mvScan mvExpressionToMvScanExpressionMapping, and change mv sql expression to query based - ExpressionMapping expressionMappingKeySourceBased = - mvExpressionToMvScanExpressionMapping.keyPermute(sourceToTargetMapping.inverse()); - List> flattenExpressionMap = - expressionMappingKeySourceBased.flattenMap(); + // generate mvSql to mvScan mvExprToMvScanExprMapping, and change mv sql expression to query based + ExpressionMapping mvExprToMvScanExprMappingKeySourceBased = + mvExprToMvScanExprMapping.keyPermute(sourceToTargetMapping.inverse()); + List> flattenExpressionMapping = + mvExprToMvScanExprMappingKeySourceBased.flattenMap(); // view to view scan expression is 1:1 so get first element - Map mvSqlToMvScanMappingQueryBased = flattenExpressionMap.get(0); + Map mvSqlToMvScanMappingQueryBased = + flattenExpressionMapping.get(0); List rewrittenExpressions = new ArrayList<>(); for (Expression expressionToRewrite : sourceExpressionsToWrite) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/mapping/EquivalenceClassSetMapping.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/mapping/EquivalenceClassSetMapping.java index f5153a20d9307fd..d4ec09c24a4b8c2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/mapping/EquivalenceClassSetMapping.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/mapping/EquivalenceClassSetMapping.java @@ -27,6 +27,8 @@ /** * EquivalenceClassSetMapping + * This will extract the equivalence class set in EquivalenceClass and mapping set in + * two different EquivalenceClass. */ public class EquivalenceClassSetMapping extends Mapping { @@ -42,7 +44,7 @@ public static EquivalenceClassSetMapping of(Map, Set expressionMapping;