From 4a22b202440050e20d7a579a95825f6319718ab4 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Mon, 15 Jan 2024 14:48:25 +0800 Subject: [PATCH] [fix](Nereids) top-n with top project should hit top-n opt (#29971) in PR #29312, we limit top-n opt only enable in simplest cases. in this PR, we let go of some restrictions. pick from master #29971 commit id ce89a9f07c561317fefe8023047269ea3b264038 --- .../apache/doris/nereids/processor/post/TopNScanOpt.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopNScanOpt.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopNScanOpt.java index 31a2a7aff561a3..1fb078fa643267 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopNScanOpt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopNScanOpt.java @@ -52,6 +52,14 @@ public Plan visit(Plan plan, CascadesContext context) { public Plan visitPhysicalSink(PhysicalSink physicalSink, CascadesContext context) { if (physicalSink.child() instanceof TopN) { return super.visit(physicalSink, context); + } else if (physicalSink.child() instanceof Project && physicalSink.child().child(0) instanceof TopN) { + PhysicalTopN oldTopN = (PhysicalTopN) physicalSink.child().child(0); + PhysicalTopN newTopN = (PhysicalTopN) oldTopN.accept(this, context); + if (newTopN == oldTopN) { + return physicalSink; + } else { + return physicalSink.withChildren(physicalSink.child().withChildren(newTopN)); + } } return physicalSink; }