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 f301973d649e555..09d194759229939 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 @@ -261,6 +261,12 @@ protected List doRewrite(StructInfo queryStructInfo, CascadesContext casca // Rewrite query by view rewrittenPlan = rewriteQueryByView(matchMode, queryStructInfo, viewStructInfo, viewToQuerySlotMapping, rewrittenPlan, materializationContext, cascadesContext); + // If rewrite successfully, try to get mv read lock to avoid data inconsistent, + // try to get lock which should added before RBO + if (materializationContext instanceof AsyncMaterializationContext && !materializationContext.isSuccess()) { + cascadesContext.getStatementContext() + .addTableReadLock(((AsyncMaterializationContext) materializationContext).getMtmv()); + } rewrittenPlan = MaterializedViewUtils.rewriteByRules(cascadesContext, childContext -> { Rewriter.getWholeTreeRewriter(childContext).execute(); @@ -356,12 +362,6 @@ protected List doRewrite(StructInfo queryStructInfo, CascadesContext casca rewriteResults.add(rewrittenPlan); // if rewrite successfully, try to regenerate mv scan because it maybe used again materializationContext.tryReGenerateScanPlan(cascadesContext); - // if rewrite successfully, try to get mv read lock to avoid data inconsistent - if (materializationContext instanceof AsyncMaterializationContext && !materializationContext.isSuccess()) { - // Need get mtmv read lock, to avoid the data of plan which use mv is wrong - cascadesContext.getStatementContext() - .addTableReadLock(((AsyncMaterializationContext) materializationContext).getMtmv()); - } recordIfRewritten(queryStructInfo.getOriginalPlan(), materializationContext); } return rewriteResults;