diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalDeferMaterializeTopNToPhysicalDeferMaterializeTopN.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalDeferMaterializeTopNToPhysicalDeferMaterializeTopN.java index 9ad6b73d1c85cf..2799ca30147bc7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalDeferMaterializeTopNToPhysicalDeferMaterializeTopN.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalDeferMaterializeTopNToPhysicalDeferMaterializeTopN.java @@ -20,6 +20,7 @@ import org.apache.doris.nereids.rules.Rule; import org.apache.doris.nereids.rules.RuleType; import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.SortPhase; import org.apache.doris.nereids.trees.plans.logical.LogicalDeferMaterializeTopN; import org.apache.doris.nereids.trees.plans.physical.PhysicalDeferMaterializeTopN; import org.apache.doris.nereids.trees.plans.physical.PhysicalTopN; @@ -38,8 +39,12 @@ public Rule build() { .build() .transform(topN.getLogicalTopN(), ctx.cascadesContext) .get(0); - return wrap(physicalTopN, topN, wrap((PhysicalTopN) physicalTopN.child(), topN, - ((PhysicalTopN) physicalTopN.child()).child())); + if (physicalTopN.getSortPhase() == SortPhase.MERGE_SORT) { + return wrap(physicalTopN, topN, wrap((PhysicalTopN) physicalTopN.child(), topN, + ((PhysicalTopN) physicalTopN.child()).child())); + } else { + return wrap(physicalTopN, topN, physicalTopN.child()); + } }).toRule(RuleType.LOGICAL_DEFER_MATERIALIZE_TOP_N_TO_PHYSICAL_DEFER_MATERIALIZE_TOP_N_RULE); } diff --git a/regression-test/data/nereids_rules_p0/defer_materialize_topn/one_phase.out b/regression-test/data/nereids_rules_p0/defer_materialize_topn/one_phase.out new file mode 100644 index 00000000000000..cb3f8c9b7ccdf4 --- /dev/null +++ b/regression-test/data/nereids_rules_p0/defer_materialize_topn/one_phase.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !1 -- +11113 + +-- !2 -- +11113 + diff --git a/regression-test/suites/nereids_rules_p0/cte/test_cte_name_reuse.groovy b/regression-test/suites/nereids_rules_p0/cte/test_cte_name_reuse.groovy index 5d472716f00b73..0f943db5b311d7 100644 --- a/regression-test/suites/nereids_rules_p0/cte/test_cte_name_reuse.groovy +++ b/regression-test/suites/nereids_rules_p0/cte/test_cte_name_reuse.groovy @@ -14,7 +14,7 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. -suite("test_cte_name_reuse)") { +suite("test_cte_name_reuse") { sql "SET enable_nereids_planner=true" sql "SET enable_pipeline_engine=true" sql "SET enable_fallback_to_original_planner=false" diff --git a/regression-test/suites/nereids_rules_p0/defer_materialize_topn/one_phase.groovy b/regression-test/suites/nereids_rules_p0/defer_materialize_topn/one_phase.groovy new file mode 100644 index 00000000000000..c7b106eff2a289 --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/defer_materialize_topn/one_phase.groovy @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +suite("one_phase") { + sql """ + drop table if exists users; + + CREATE TABLE `users` ( + `UserID` bigint NULL + ) ENGINE=OLAP + DUPLICATE KEY(`UserID`) + DISTRIBUTED BY HASH(`UserID`) BUCKETS 48 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "min_load_replica_num" = "-1", + "is_being_synced" = "false", + "storage_medium" = "hdd", + "storage_format" = "V2", + "inverted_index_storage_format" = "V2", + "light_schema_change" = "true", + "disable_auto_compaction" = "false", + "enable_single_replica_compaction" = "false", + "group_commit_interval_ms" = "10000", + "group_commit_data_bytes" = "134217728" + ); + + insert into users values (11111),(11112),(11113); + + """ + + sql "set sort_phase_num=1;" + qt_1 "select userid from users order by userid limit 2, 109000000;" + + sql "set sort_phase_num=2;" + qt_2 "select userid from users order by userid limit 2, 109000000;" + +} \ No newline at end of file