Skip to content

Commit

Permalink
add some comment
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Dec 7, 2023
1 parent 76c3b22 commit be85ee2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class MVCache {
private final Plan logicalPlan;
// this should be shuttle expression with lineage
private final List<NamedExpression> mvOutputExpressions;
// the context when parse, analyze, optimize the mv logical plan

public MVCache(MTMV materializedView, Plan logicalPlan, List<NamedExpression> mvOutputExpressions) {
this.logicalPlan = logicalPlan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected List<Plan> rewrite(Plan queryPlan, CascadesContext cascadesContext) {
}
List<RelationMapping> queryToViewTableMappings =
RelationMapping.generate(queryStructInfo.getRelations(), viewStructInfo.getRelations());
// if any relation in query and view can not map, bail out.
if (queryToViewTableMappings == null) {
return rewriteResults;
}
Expand Down Expand Up @@ -161,7 +162,7 @@ protected Plan rewriteQueryByView(MatchMode matchMode,
*/
protected List<Expression> rewriteExpression(
List<? extends Expression> 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
Expand All @@ -175,13 +176,14 @@ protected List<Expression> 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<Map<? extends Expression, ? extends Expression>> flattenExpressionMap =
expressionMappingKeySourceBased.flattenMap();
// generate mvSql to mvScan mvExprToMvScanExprMapping, and change mv sql expression to query based
ExpressionMapping mvExprToMvScanExprMappingKeySourceBased =
mvExprToMvScanExprMapping.keyPermute(sourceToTargetMapping.inverse());
List<Map<? extends Expression, ? extends Expression>> flattenExpressionMapping =
mvExprToMvScanExprMappingKeySourceBased.flattenMap();
// view to view scan expression is 1:1 so get first element
Map<? extends Expression, ? extends Expression> mvSqlToMvScanMappingQueryBased = flattenExpressionMap.get(0);
Map<? extends Expression, ? extends Expression> mvSqlToMvScanMappingQueryBased =
flattenExpressionMapping.get(0);

List<Expression> rewrittenExpressions = new ArrayList<>();
for (Expression expressionToRewrite : sourceExpressionsToWrite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -42,7 +44,7 @@ public static EquivalenceClassSetMapping of(Map<Set<SlotReference>, Set<SlotRefe
}

/**
* source equivalence set map to target equivalence set
* Generate source equivalence set map to target equivalence set
*/
public static EquivalenceClassSetMapping generate(EquivalenceClass source, EquivalenceClass target) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Map;

/**
* Expression and it's index mapping
* Expression mapping, maybe one expression map to multi expression
*/
public class ExpressionMapping extends Mapping {
private final Multimap<? extends Expression, ? extends Expression> expressionMapping;
Expand Down

0 comments on commit be85ee2

Please sign in to comment.