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


##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java:
##########
@@ -123,35 +125,46 @@ public static MTMVCache from(String defSql,
                 // No need cost for performance
                 planner.planWithLock(unboundMvPlan, PhysicalProperties.ANY, 
ExplainLevel.REWRITTEN_PLAN);
             }
-            originPlan = planner.getCascadesContext().getRewritePlan();
-            // Eliminate result sink because sink operator is useless in query 
rewrite by materialized view
-            // and the top sort can also be removed
-            mvPlan = originPlan.rewriteDownShortCircuit(p -> {
-                if (p instanceof LogicalResultSink) {
-                    return p.child(0);
-                }
-                return p;
-            });
-            // Optimize by rules to remove top sort
-            CascadesContext parentCascadesContext = 
CascadesContext.initContext(mvSqlStatementContext, mvPlan,
-                    PhysicalProperties.ANY);
-            mvPlan = 
MaterializedViewUtils.rewriteByRules(parentCascadesContext, childContext -> {
-                Rewriter.getCteChildrenRewriter(childContext,
-                        
ImmutableList.of(Rewriter.custom(RuleType.ELIMINATE_SORT, 
EliminateSort::new))).execute();
-                return childContext.getRewritePlan();
-            }, mvPlan, originPlan);
-            // Construct structInfo once for use later
-            structInfoOptional = 
MaterializationContext.constructStructInfo(mvPlan, originPlan,
-                    planner.getCascadesContext(),
-                    new BitSet());
+            CascadesContext cascadesContext = planner.getCascadesContext();
+            Pair<Plan, StructInfo> finalPlanStructInfoPair = 
constructPlanAndStructInfo(
+                    cascadesContext.getRewritePlan(), cascadesContext);
+            List<Pair<Plan, StructInfo>> tmpPlanUsedForRewrite = new 
ArrayList<>();
+            for (Plan plan : 
cascadesContext.getStatementContext().getTmpPlanForMvRewrite()) {
+                tmpPlanUsedForRewrite.add(constructPlanAndStructInfo(plan, 
cascadesContext));
+            }
+            return new MTMVCache(finalPlanStructInfoPair, 
cascadesContext.getRewritePlan(), needCost
+                    ? cascadesContext.getMemo().getRoot().getStatistics() : 
null, tmpPlanUsedForRewrite);
         } finally {
+            
createCacheContext.getStatementContext().setForceRecordTmpPlan(false);
+            mvSqlStatementContext.setForceRecordTmpPlan(false);
             
createCacheContext.getSessionVariable().enableMaterializedViewRewrite = 
originalRewriteFlag;
             if (currentContext != null) {
                 currentContext.setThreadLocalInfo();
             }
         }
-        return new MTMVCache(mvPlan, originPlan, planner.getAnalyzedPlan(), 
needCost
-                ? 
planner.getCascadesContext().getMemo().getRoot().getStatistics() : null,
-                structInfoOptional.orElse(null));
+    }
+
+    // Eliminate result sink because sink operator is useless in query rewrite 
by materialized view
+    // and the top sort can also be removed
+    private static Pair<Plan, StructInfo> constructPlanAndStructInfo(Plan 
plan, CascadesContext cascadesContext) {
+        Plan mvPlan = plan.accept(new DefaultPlanRewriter<Object>() {
+            @Override
+            public Plan visitLogicalResultSink(LogicalResultSink<? extends 
Plan> logicalResultSink,
+                    Object context) {
+                return new LogicalProject(logicalResultSink.getOutput(),
+                        false, logicalResultSink.children());
+            }
+        }, null);
+        // Optimize by rules to remove top sort
+        mvPlan = MaterializedViewUtils.rewriteByRules(cascadesContext, 
childContext -> {
+            Rewriter.getCteChildrenRewriter(childContext, ImmutableList.of(
+                    Rewriter.custom(RuleType.ELIMINATE_SORT, 
EliminateSort::new),
+                    Rewriter.bottomUp(new MergeProjects()))).execute();

Review Comment:
   have fixed



-- 
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