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 ;
@@ -256,6 +257,12 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
256
257
// Rewrite query by view
257
258
rewrittenPlan = rewriteQueryByView (matchMode , queryStructInfo , viewStructInfo , viewToQuerySlotMapping ,
258
259
rewrittenPlan , materializationContext , cascadesContext );
260
+ // If rewrite successfully, try to get mv read lock to avoid data inconsistent,
261
+ // try to get lock which should added before RBO
262
+ if (materializationContext instanceof AsyncMaterializationContext && !materializationContext .isSuccess ()) {
263
+ cascadesContext .getStatementContext ()
264
+ .addTableReadLock (((AsyncMaterializationContext ) materializationContext ).getMtmv ());
265
+ }
259
266
rewrittenPlan = MaterializedViewUtils .rewriteByRules (cascadesContext ,
260
267
childContext -> {
261
268
Rewriter .getWholeTreeRewriter (childContext ).execute ();
@@ -349,6 +356,13 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
349
356
rewrittenPlanOutput , queryPlan .getOutput ()));
350
357
continue ;
351
358
}
359
+ // Merge project
360
+ rewrittenPlan = MaterializedViewUtils .rewriteByRules (cascadesContext ,
361
+ childContext -> {
362
+ Rewriter .getCteChildrenRewriter (childContext ,
363
+ ImmutableList .of (Rewriter .bottomUp (new MergeProjects ()))).execute ();
364
+ return childContext .getRewritePlan ();
365
+ }, rewrittenPlan , queryPlan );
352
366
if (!isOutputValid (queryPlan , rewrittenPlan )) {
353
367
LogicalProperties logicalProperties = rewrittenPlan .getLogicalProperties ();
354
368
materializationContext .recordFailReason (queryStructInfo ,
@@ -358,11 +372,11 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
358
372
logicalProperties , queryPlan .getLogicalProperties ()));
359
373
continue ;
360
374
}
361
- recordIfRewritten (queryStructInfo .getOriginalPlan (), materializationContext );
362
375
trySetStatistics (materializationContext , cascadesContext );
363
376
rewriteResults .add (rewrittenPlan );
364
377
// if rewrite successfully, try to regenerate mv scan because it maybe used again
365
378
materializationContext .tryReGenerateScanPlan (cascadesContext );
379
+ recordIfRewritten (queryStructInfo .getOriginalPlan (), materializationContext , cascadesContext );
366
380
}
367
381
return rewriteResults ;
368
382
}
@@ -830,8 +844,9 @@ protected boolean checkMaterializationPattern(StructInfo structInfo, CascadesCon
830
844
return checkQueryPattern (structInfo , cascadesContext );
831
845
}
832
846
833
- protected void recordIfRewritten (Plan plan , MaterializationContext context ) {
847
+ protected void recordIfRewritten (Plan plan , MaterializationContext context , CascadesContext cascadesContext ) {
834
848
context .setSuccess (true );
849
+ cascadesContext .addMaterializationRewrittenSuccess (context .generateMaterializationIdentifier ());
835
850
if (plan .getGroupExpression ().isPresent ()) {
836
851
context .addMatchedGroup (plan .getGroupExpression ().get ().getOwnerGroup ().getGroupId (), true );
837
852
}
0 commit comments