seawinde commented on code in PR #30870: URL: https://github.com/apache/doris/pull/30870#discussion_r1495093937
########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java: ########## @@ -68,13 +77,28 @@ public static MTMVCache from(MTMV mtmv, ConnectContext connectContext) { } Plan mvRewrittenPlan = planner.plan(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.REWRITTEN_PLAN); - Plan mvPlan = mvRewrittenPlan instanceof LogicalResultSink - ? (Plan) ((LogicalResultSink) mvRewrittenPlan).child() : mvRewrittenPlan; + // remove top sort and result sink node above materialized view plan + if (mvRewrittenPlan instanceof LogicalResultSink) { + // replace result sink to table sink for sort eliminate + mvRewrittenPlan = new LogicalOlapTableSink<>(new Database(), mtmv, mtmv.getBaseSchema(), + mtmv.getPartitionIds(), + mvRewrittenPlan.getOutput().stream().map(NamedExpression.class::cast).collect(Collectors.toList()), + mtmv.isPartitionedTable(), DMLCommandType.NONE, mvRewrittenPlan.child(0)); + // eliminate sort under sink because sort is useless for materialized view + planner.getCascadesContext().setRewritePlan(mvRewrittenPlan); + Rewriter rewriter = Rewriter.getCteChildrenRewriter(planner.getCascadesContext(), + Lists.newArrayList(AbstractBatchJobExecutor.topDown(new EliminateSortUnderSink()))); + rewriter.execute(); + } + if (planner.getCascadesContext().getRewritePlan() instanceof Sink) { + // remove the sink + mvRewrittenPlan = planner.getCascadesContext().getRewritePlan().child(0); + } Review Comment: Have fixed it -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org