From 1d10132aa1dbb84b07b37195cc650aa469b9e701 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:44:51 +0800 Subject: [PATCH] [fix](Nereids) should distribute first when do sort enforce on must shuffle (#30948) --- .../EnforceMissingPropertiesHelper.java | 8 +++- .../cte/test_cte_reuse_with_window.groovy | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 regression-test/suites/nereids_p0/cte/test_cte_reuse_with_window.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/EnforceMissingPropertiesHelper.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/EnforceMissingPropertiesHelper.java index 31d99765be..b08db2aeba 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/EnforceMissingPropertiesHelper.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/EnforceMissingPropertiesHelper.java @@ -130,9 +130,13 @@ public class EnforceMissingPropertiesHelper { private PhysicalProperties enforceSortAndDistribution(PhysicalProperties outputProperty, PhysicalProperties requiredProperty) { - PhysicalProperties enforcedProperty; + PhysicalProperties enforcedProperty = outputProperty; if (requiredProperty.getDistributionSpec().equals(new DistributionSpecGather())) { - enforcedProperty = enforceLocalSort(outputProperty, requiredProperty); + // NOTICE: if output is must shuffle, we must do distribution first. so add a random shuffle here. + if (outputProperty.getDistributionSpec() instanceof DistributionSpecMustShuffle) { + enforcedProperty = enforceDistribution(enforcedProperty, PhysicalProperties.EXECUTION_ANY); + } + enforcedProperty = enforceLocalSort(enforcedProperty, requiredProperty); enforcedProperty = enforceDistribution(enforcedProperty, requiredProperty); enforcedProperty = enforceGlobalSort(enforcedProperty, requiredProperty); } else { diff --git a/regression-test/suites/nereids_p0/cte/test_cte_reuse_with_window.groovy b/regression-test/suites/nereids_p0/cte/test_cte_reuse_with_window.groovy new file mode 100644 index 0000000000..431aeda754 --- /dev/null +++ b/regression-test/suites/nereids_p0/cte/test_cte_reuse_with_window.groovy @@ -0,0 +1,39 @@ +// 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("test_cte_reuse_with_window") { + sql "SET enable_nereids_planner=true" + sql "SET enable_pipeline_engine=true" + sql "SET enable_fallback_to_original_planner=false" + + sql """ + drop table if exists test_cte_reuse_with_window; + """ + + sql """ + create table test_cte_reuse_with_window (id int default '10', c1 int default '10') distributed by hash(id) properties('replication_num'="1"); + """ + + sql """ + with temp as (select * from test_cte_reuse_with_window) + select * from + (select t.id, row_number() over(order by t.id) as num from temp t limit 20) a + left join + (select t.id, row_number() over(order by t.id desc) as num from temp t where t.id = 5) b + on a.id = b.id + """ +}