diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateOrderByConstant.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateOrderByConstant.java index 021cae2d6533f5..05e71089416483 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateOrderByConstant.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateOrderByConstant.java @@ -17,9 +17,11 @@ package org.apache.doris.nereids.rules.rewrite; +import org.apache.doris.nereids.properties.DataTrait; import org.apache.doris.nereids.properties.OrderKey; import org.apache.doris.nereids.rules.Rule; import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Slot; import com.google.common.collect.ImmutableList; @@ -36,8 +38,10 @@ public Rule build() { List orderKeys = sort.getOrderKeys(); ImmutableList.Builder orderKeysWithoutConstBuilder = ImmutableList.builderWithExpectedSize(orderKeys.size()); + DataTrait dataTrait = sort.child().getLogicalProperties().getTrait(); for (OrderKey orderKey : orderKeys) { - if (!orderKey.getExpr().isConstant()) { + if (!orderKey.getExpr().isConstant() && !(orderKey.getExpr() instanceof Slot + && dataTrait.isUniformAndNotNull((Slot) orderKey.getExpr()))) { orderKeysWithoutConstBuilder.add(orderKey); } } diff --git a/regression-test/data/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.out b/regression-test/data/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.out new file mode 100644 index 00000000000000..06356becf99710 --- /dev/null +++ b/regression-test/data/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.out @@ -0,0 +1,34 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !predicate -- +1 1 +1 1 + +-- !predicate_order_by_two -- +1 1 +1 1 + +-- !with_group_by -- +1 1 + +-- !predicate_multi_other -- +1 1 100 apple +1 1 100 apple + +-- !predicate_shape -- +PhysicalResultSink +--filter((eliminate_order_by_constant_t.a = 1)) +----PhysicalOlapScan[eliminate_order_by_constant_t] + +-- !with_group_by_shape -- +PhysicalResultSink +--PhysicalLimit[GLOBAL] +----filter((eliminate_order_by_constant_t.a = 1)) +------PhysicalOlapScan[eliminate_order_by_constant_t] + +-- !predicate_multi_other_shape -- +PhysicalResultSink +--PhysicalQuickSort[MERGE_SORT] +----PhysicalQuickSort[LOCAL_SORT] +------filter((eliminate_order_by_constant_t.a = 1)) +--------PhysicalOlapScan[eliminate_order_by_constant_t] + diff --git a/regression-test/suites/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.groovy b/regression-test/suites/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.groovy new file mode 100644 index 00000000000000..707481f13286ba --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/eliminate_order_by_constant/eliminate_order_by_constant.groovy @@ -0,0 +1,45 @@ +// 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("eliminate_order_by_constant") { + sql "SET ignore_shape_nodes='PhysicalDistribute,PhysicalProject'" + sql "drop table if exists eliminate_order_by_constant_t" + sql """create table eliminate_order_by_constant_t(a int null, b int not null, c varchar(10) null, d date, dt datetime) + distributed by hash(a) properties("replication_num"="1"); + """ + sql """ + INSERT INTO eliminate_order_by_constant_t (a, b, c, d, dt) VALUES + (1, 100, 'apple', '2023-01-01', '2023-01-01 10:00:00'), + (1, 100, 'apple', '2023-01-01', '2023-01-01 10:00:00'), + (2, 101, 'banana', '2023-01-02', '2023-01-02 11:00:00'), + (3, 102, 'cherry', '2023-01-03', '2023-01-03 12:00:00'), + (3, 102, 'cherry', '2023-01-03', '2023-01-03 12:00:00'), + (NULL, 103, 'date', '2023-01-04', '2023-01-04 13:00:00'), + (4, 104, 'elderberry', '2023-01-05', '2023-01-05 14:00:00'), + (5, 105, NULL, '2023-01-06', '2023-01-06 15:00:00'), + (5, 105, NULL, '2023-01-06', '2023-01-06 15:00:00'), + (6, 106, 'fig', '2023-01-07', '2023-01-07 16:00:00'), + (NULL, 107, 'grape', '2023-01-08', '2023-01-08 17:00:00'); + """ + qt_predicate "select 1 as c1,a from eliminate_order_by_constant_t where a=1 order by a" + qt_predicate_order_by_two "select 1 as c1,a from eliminate_order_by_constant_t where a=1 order by a,c1" + qt_with_group_by """select 1 as c1,a from eliminate_order_by_constant_t where a=1 group by c1,a order by a""" + qt_predicate_multi_other """select 1 as c1,a,b,c from eliminate_order_by_constant_t where a=1 order by a,'abc',b,c""" + qt_predicate_shape "explain shape plan select 1 as c1,a from eliminate_order_by_constant_t where a=1 order by a" + qt_with_group_by_shape """explain shape plan select 1 as c1,a from eliminate_order_by_constant_t where a=1 group by c1,a order by a""" + qt_predicate_multi_other_shape """explain shape plan select 1 as c1,a,b,c from eliminate_order_by_constant_t where a=1 order by a,'abc',b,c""" +} \ No newline at end of file