37
37
import org .apache .doris .nereids .rules .exploration .mv .mapping .ExpressionMapping ;
38
38
import org .apache .doris .nereids .rules .exploration .mv .mapping .RelationMapping ;
39
39
import org .apache .doris .nereids .rules .exploration .mv .mapping .SlotMapping ;
40
+ import org .apache .doris .nereids .rules .rewrite .MergeProjects ;
40
41
import org .apache .doris .nereids .trees .expressions .Alias ;
41
42
import org .apache .doris .nereids .trees .expressions .Expression ;
42
43
import org .apache .doris .nereids .trees .expressions .NamedExpression ;
@@ -261,6 +262,12 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
261
262
// Rewrite query by view
262
263
rewrittenPlan = rewriteQueryByView (matchMode , queryStructInfo , viewStructInfo , viewToQuerySlotMapping ,
263
264
rewrittenPlan , materializationContext , cascadesContext );
265
+ // If rewrite successfully, try to get mv read lock to avoid data inconsistent,
266
+ // try to get lock which should added before RBO
267
+ if (materializationContext instanceof AsyncMaterializationContext && !materializationContext .isSuccess ()) {
268
+ cascadesContext .getStatementContext ()
269
+ .addTableReadLock (((AsyncMaterializationContext ) materializationContext ).getMtmv ());
270
+ }
264
271
rewrittenPlan = MaterializedViewUtils .rewriteByRules (cascadesContext ,
265
272
childContext -> {
266
273
Rewriter .getWholeTreeRewriter (childContext ).execute ();
@@ -354,6 +361,13 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
354
361
rewrittenPlanOutput , queryPlan .getOutput ()));
355
362
continue ;
356
363
}
364
+ // Merge project
365
+ rewrittenPlan = MaterializedViewUtils .rewriteByRules (cascadesContext ,
366
+ childContext -> {
367
+ Rewriter .getCteChildrenRewriter (childContext ,
368
+ ImmutableList .of (Rewriter .bottomUp (new MergeProjects ()))).execute ();
369
+ return childContext .getRewritePlan ();
370
+ }, rewrittenPlan , queryPlan );
357
371
if (!isOutputValid (queryPlan , rewrittenPlan )) {
358
372
LogicalProperties logicalProperties = rewrittenPlan .getLogicalProperties ();
359
373
materializationContext .recordFailReason (queryStructInfo ,
@@ -363,11 +377,11 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
363
377
logicalProperties , queryPlan .getLogicalProperties ()));
364
378
continue ;
365
379
}
366
- recordIfRewritten (queryStructInfo .getOriginalPlan (), materializationContext );
367
380
trySetStatistics (materializationContext , cascadesContext );
368
381
rewriteResults .add (rewrittenPlan );
369
382
// if rewrite successfully, try to regenerate mv scan because it maybe used again
370
383
materializationContext .tryReGenerateScanPlan (cascadesContext );
384
+ recordIfRewritten (queryStructInfo .getOriginalPlan (), materializationContext , cascadesContext );
371
385
}
372
386
return rewriteResults ;
373
387
}
@@ -852,8 +866,9 @@ protected boolean checkMaterializationPattern(StructInfo structInfo, CascadesCon
852
866
return checkQueryPattern (structInfo , cascadesContext );
853
867
}
854
868
855
- protected void recordIfRewritten (Plan plan , MaterializationContext context ) {
869
+ protected void recordIfRewritten (Plan plan , MaterializationContext context , CascadesContext cascadesContext ) {
856
870
context .setSuccess (true );
871
+ cascadesContext .addMaterializationRewrittenSuccess (context .generateMaterializationIdentifier ());
857
872
if (plan .getGroupExpression ().isPresent ()) {
858
873
context .addMatchedGroup (plan .getGroupExpression ().get ().getOwnerGroup ().getGroupId (), true );
859
874
}
0 commit comments