Skip to content

Commit b30856b

Browse files
BiteTheDDDDtYour Name
authored and
Your Name
committed
[Bug](sort) fix wrong result coz push down sort with null last (#51472)
### What problem does this PR solve? ```sql drop table if exists d_table; create table d_table ( k1 int null, k2 int not null, k3 bigint null, k4 varchar(100) null ) duplicate key (k1,k2,k3) distributed BY hash(k1) buckets 3 properties("replication_num" = "1"); insert into d_table select 1,1,1,'a'; insert into d_table select 2,2,2,'b'; insert into d_table select 3,-3,null,'c'; insert into d_table select 3,3,null,'c'; insert into d_table select null,3,null,'c'; select * from d_table order by k1 nulls last limit 1; mysql> select * from d_table order by k1 nulls last limit 1; +------+------+------+------+ | k1 | k2 | k3 | k4 | +------+------+------+------+ | 2 | 2 | 2 | b | +------+------+------+------+ 1 row in set (0.01 sec) mysql> select * from d_table order by k1 nulls last limit 2; +------+------+------+------+ | k1 | k2 | k3 | k4 | +------+------+------+------+ | 1 | 1 | 1 | a | | 2 | 2 | 2 | b | +------+------+------+------+ 2 rows in set (0.02 sec) ```
1 parent 14cb4c9 commit b30856b

File tree

4 files changed

+199
-6
lines changed

4 files changed

+199
-6
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,9 +2753,9 @@ private boolean checkPushSort(SortNode sortNode, OlapTable olapTable) {
27532753
if (sortExpr instanceof SlotRef) {
27542754
SlotRef slotRef = (SlotRef) sortExpr;
27552755
if (sortColumn.equals(slotRef.getColumn())) {
2756-
// ORDER BY DESC NULLS FIRST can not be optimized to only read file tail,
2757-
// since NULLS is at file head but data is at tail
2758-
if (sortColumn.isAllowNull() && nullsFirsts.get(i) && !isAscOrders.get(i)) {
2756+
// [ORDER BY DESC NULLS FIRST] or [ORDER BY ASC NULLS LAST] can not be optimized
2757+
// to only read file tail, since NULLS is at file head but data is at tail
2758+
if (sortColumn.isAllowNull() && nullsFirsts.get(i) != isAscOrders.get(i)) {
27592759
return false;
27602760
}
27612761
} else {

fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,9 @@ public boolean checkPushSort(SortNode sortNode) {
12351235
if (sortExpr instanceof SlotRef) {
12361236
SlotRef slotRef = (SlotRef) sortExpr;
12371237
if (tableKey.equals(slotRef.getColumn())) {
1238-
// ORDER BY DESC NULLS FIRST can not be optimized to only read file tail,
1239-
// since NULLS is at file head but data is at tail
1240-
if (tableKey.isAllowNull() && nullsFirsts.get(i) && !isAscOrders.get(i)) {
1238+
// [ORDER BY DESC NULLS FIRST] or [ORDER BY ASC NULLS LAST] can not be optimized
1239+
// to only read file tail, since NULLS is at file head but data is at tail
1240+
if (tableKey.isAllowNull() && (nullsFirsts.get(i) != isAscOrders.get(i))) {
12411241
return false;
12421242
}
12431243
} else {
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !select --
3+
3 3 \N c
4+
5+
-- !select --
6+
3 3 \N c
7+
2 2 2 b
8+
9+
-- !select --
10+
3 3 \N c
11+
2 2 2 b
12+
1 1 1 a
13+
14+
-- !select --
15+
3 3 \N c
16+
2 2 2 b
17+
1 1 1 a
18+
-3 -3 \N c
19+
20+
-- !select --
21+
3 3 \N c
22+
2 2 2 b
23+
1 1 1 a
24+
-3 -3 \N c
25+
\N 3 \N c
26+
27+
-- !select --
28+
3 3 \N c
29+
2 2 2 b
30+
1 1 1 a
31+
-3 -3 \N c
32+
\N 3 \N c
33+
34+
-- !select --
35+
\N 3 \N c
36+
37+
-- !select --
38+
\N 3 \N c
39+
3 3 \N c
40+
41+
-- !select --
42+
\N 3 \N c
43+
3 3 \N c
44+
2 2 2 b
45+
46+
-- !select --
47+
\N 3 \N c
48+
3 3 \N c
49+
2 2 2 b
50+
1 1 1 a
51+
52+
-- !select --
53+
\N 3 \N c
54+
3 3 \N c
55+
2 2 2 b
56+
1 1 1 a
57+
-3 -3 \N c
58+
59+
-- !select --
60+
\N 3 \N c
61+
3 3 \N c
62+
2 2 2 b
63+
1 1 1 a
64+
-3 -3 \N c
65+
66+
-- !select --
67+
-3 -3 \N c
68+
69+
-- !select --
70+
-3 -3 \N c
71+
1 1 1 a
72+
73+
-- !select --
74+
-3 -3 \N c
75+
1 1 1 a
76+
2 2 2 b
77+
78+
-- !select --
79+
-3 -3 \N c
80+
1 1 1 a
81+
2 2 2 b
82+
3 3 \N c
83+
84+
-- !select --
85+
-3 -3 \N c
86+
1 1 1 a
87+
2 2 2 b
88+
3 3 \N c
89+
\N 3 \N c
90+
91+
-- !select --
92+
-3 -3 \N c
93+
1 1 1 a
94+
2 2 2 b
95+
3 3 \N c
96+
\N 3 \N c
97+
98+
-- !select --
99+
\N 3 \N c
100+
101+
-- !select --
102+
\N 3 \N c
103+
-3 -3 \N c
104+
105+
-- !select --
106+
\N 3 \N c
107+
-3 -3 \N c
108+
1 1 1 a
109+
110+
-- !select --
111+
\N 3 \N c
112+
-3 -3 \N c
113+
1 1 1 a
114+
2 2 2 b
115+
116+
-- !select --
117+
\N 3 \N c
118+
-3 -3 \N c
119+
1 1 1 a
120+
2 2 2 b
121+
3 3 \N c
122+
123+
-- !select --
124+
\N 3 \N c
125+
-3 -3 \N c
126+
1 1 1 a
127+
2 2 2 b
128+
3 3 \N c
129+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_orderby_limit") {
19+
sql "drop table if exists d_table;"
20+
sql """
21+
create table d_table (
22+
k1 int null,
23+
k2 int not null,
24+
k3 bigint null,
25+
k4 varchar(100) null
26+
)
27+
duplicate key (k1,k2,k3)
28+
distributed BY hash(k1) buckets 3
29+
properties("replication_num" = "1");
30+
"""
31+
sql "insert into d_table select 1,1,1,'a';"
32+
sql "insert into d_table select 2,2,2,'b';"
33+
sql "insert into d_table select -3,-3,null,'c';"
34+
sql "insert into d_table select 3,3,null,'c';"
35+
sql "insert into d_table select null,3,null,'c';"
36+
37+
qt_select "select * from d_table order by k1 desc nulls last limit 1;"
38+
qt_select "select * from d_table order by k1 desc nulls last limit 2;"
39+
qt_select "select * from d_table order by k1 desc nulls last limit 3;"
40+
qt_select "select * from d_table order by k1 desc nulls last limit 4;"
41+
qt_select "select * from d_table order by k1 desc nulls last limit 5;"
42+
qt_select "select * from d_table order by k1 desc nulls last limit 6;"
43+
44+
qt_select "select * from d_table order by k1 desc nulls first limit 1;"
45+
qt_select "select * from d_table order by k1 desc nulls first limit 2;"
46+
qt_select "select * from d_table order by k1 desc nulls first limit 3;"
47+
qt_select "select * from d_table order by k1 desc nulls first limit 4;"
48+
qt_select "select * from d_table order by k1 desc nulls first limit 5;"
49+
qt_select "select * from d_table order by k1 desc nulls first limit 6;"
50+
51+
qt_select "select * from d_table order by k1 asc nulls last limit 1;"
52+
qt_select "select * from d_table order by k1 asc nulls last limit 2;"
53+
qt_select "select * from d_table order by k1 asc nulls last limit 3;"
54+
qt_select "select * from d_table order by k1 asc nulls last limit 4;"
55+
qt_select "select * from d_table order by k1 asc nulls last limit 5;"
56+
qt_select "select * from d_table order by k1 asc nulls last limit 6;"
57+
58+
qt_select "select * from d_table order by k1 asc nulls first limit 1;"
59+
qt_select "select * from d_table order by k1 asc nulls first limit 2;"
60+
qt_select "select * from d_table order by k1 asc nulls first limit 3;"
61+
qt_select "select * from d_table order by k1 asc nulls first limit 4;"
62+
qt_select "select * from d_table order by k1 asc nulls first limit 5;"
63+
qt_select "select * from d_table order by k1 asc nulls first limit 6;"
64+
}

0 commit comments

Comments
 (0)