seawinde commented on code in PR #66370:
URL: https://github.com/apache/doris/pull/66370#discussion_r3749127897


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExecuteCommand.java:
##########
@@ -91,6 +91,10 @@ public void run(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
         }
         PrepareCommand prepareCommand = preparedStmtCtx.command;
         StatementContext statementContext = 
preparedStmtCtx.getStatementContext();
+        // Prepared statements reuse StatementContext across executions. 
Discard partition

Review Comment:
   Current head b78a96f80a5 keeps this PR intentionally MV-specific. The 
external-EXECUTE reset covers materializedViewRewriteDuration, stale UseMvHint 
entries, and the other MV partition/discovery/rewrite fields listed in the 
updated PR body. It does not reset joinFilters, disableRules, 
queryStatsRecorded, or unresolved state. Those are generic 
prepared-StatementContext lifecycle concerns and need a separate change with 
dedicated behavior tests. I am leaving this thread unresolved to make that 
scope boundary explicit.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExecuteCommand.java:
##########
@@ -91,6 +91,10 @@ public void run(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
         }
         PrepareCommand prepareCommand = preparedStmtCtx.command;
         StatementContext statementContext = 
preparedStmtCtx.getStatementContext();
+        // Prepared statements reuse StatementContext across executions. 
Discard partition
+        // information collected by the previous execution before planning the 
current one.
+        statementContext.getTableUsedPartitionNameMap().clear();

Review Comment:
   Behavior coverage is now added in 353fd8b0b1b. 
testMvValidPartitionsAreRefreshedForEveryExecute drives two external EXECUTEs 
through the real MTMVRelationManager.isMVPartitionValid path: the first 
validity computation returns {p1,p2}, the second returns only {p1}, and the 
reused StatementContext must contain only the second result. The test fails 
without clearing mvCanRewritePartitionsMap because putIfAbsent retains {p1,p2}. 
The focused FE UT passes on current head.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExecuteCommand.java:
##########
@@ -91,6 +91,10 @@ public void run(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
         }
         PrepareCommand prepareCommand = preparedStmtCtx.command;
         StatementContext statementContext = 
preparedStmtCtx.getStatementContext();
+        // Prepared statements reuse StatementContext across executions. 
Discard partition
+        // information collected by the previous execution before planning the 
current one.
+        statementContext.getTableUsedPartitionNameMap().clear();
+        statementContext.getCommonTableIdToRelationIdMap().clear();

Review Comment:
   The current reset clears all listed MV pre-rewrite plans, flags, masks, 
successes, and relation statistics. 
testMaterializedViewStateIsResetForEveryExecute runs a real NereidsPlanner.plan 
on both external EXECUTEs and verifies the previous phase state is absent 
before each pass. Current head also adds independent behavior tests for 
validity-cache refresh, candidate replacement, and rewrite-hook settings; 
ExecuteCommandTest passes 12/12.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/StatementContext.java:
##########
@@ -1443,6 +1443,22 @@ public Map<BaseTableInfo, Collection<Partition>> 
getMvCanRewritePartitionsMap()
         return mvCanRewritePartitionsMap;
     }
 
+    /** Clear materialized-view planning state retained by a prepared 
statement between executions. */
+    public void resetMaterializedViewStateForPreparedExecution() {
+        tableUsedPartitionNameMap.clear();
+        commonTableIdToRelationIdToMap.clear();
+        mvCanRewritePartitionsMap.clear();
+        materializedViewRewriteDuration = 0;
+        hints.removeIf(UseMvHint.class::isInstance);
+        tmpPlanForMvRewrite.clear();
+        rewrittenPlansByMv.clear();
+        needPreMvRewriteRuleMasks.clear();
+        needPreMvRewrite = false;
+        preMvRewritten = false;
+        materializationRewrittenSuccessSet.clear();
+        relationIdToStatisticsMap.clear();

Review Comment:
   Current head b78a96f80a5 adds 
testMvCandidateIsRebuiltAfterSameNameReplacement. It uses the real 
MTMVRelationManager relation index and candidate lookup across two external 
EXECUTEs, refreshes the same-name relation entry between them, returns the old 
catalog object and then its replacement, and verifies that the reused 
StatementContext contains only the replacement. Without clearing 
candidateMTMVs, the second result contains both objects. ExecuteCommandTest 
passes 12/12.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/StatementContext.java:
##########
@@ -1443,6 +1443,22 @@ public Map<BaseTableInfo, Collection<Partition>> 
getMvCanRewritePartitionsMap()
         return mvCanRewritePartitionsMap;
     }
 
+    /** Clear materialized-view planning state retained by a prepared 
statement between executions. */
+    public void resetMaterializedViewStateForPreparedExecution() {
+        tableUsedPartitionNameMap.clear();
+        commonTableIdToRelationIdToMap.clear();
+        mvCanRewritePartitionsMap.clear();
+        materializedViewRewriteDuration = 0;
+        hints.removeIf(UseMvHint.class::isInstance);

Review Comment:
   Behavior coverage is now added in 353fd8b0b1b. 
testMaterializationHookFollowsRewriteSettingForEveryExecute runs the real 
Nereids planner twice on one prepared StatementContext: the first EXECUTE has 
MV rewrite enabled and installs InitMaterializationContextHook; the second 
disables rewrite and verifies that no materialization hook remains. 
ExecuteCommandTest passes 12/12 on current head.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to