Skip to content

Commit 5da6ffe

Browse files
committed
MDEV-25032: Window functions without column references get removed from ORDER BY
row_number() over () window function can be used without any column in the OVER clause. Additionally, the item doesn't reference any tables, as it's not effectively referencing any table. Rather it is specifically built based on the end temporary table used for window function computation. This caused remove_const function to wrongly drop it from the ORDER list. Effectively, we shouldn't be dropping any window function from the ORDER clause, so adjust remove_const to account for that. Reviewed by: Sergei Petrunia sergey@mariadb.com
1 parent b044898 commit 5da6ffe

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

mysql-test/r/win.result

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,5 +3866,31 @@ NULL
38663866
DROP VIEW v1;
38673867
DROP TABLE t1,t2;
38683868
#
3869+
# MDEV-25032 Window functions without column references get removed from ORDER BY
3870+
#
3871+
create table t1 (id int, score double);
3872+
insert into t1 values
3873+
(1, 5),
3874+
(1, 6),
3875+
(1, 6),
3876+
(1, 6),
3877+
(1, 7),
3878+
(1, 8.1),
3879+
(1, 9),
3880+
(1, 10);
3881+
select id, row_number() over () rn
3882+
from t1
3883+
order by rn desc;
3884+
id rn
3885+
1 8
3886+
1 7
3887+
1 6
3888+
1 5
3889+
1 4
3890+
1 3
3891+
1 2
3892+
1 1
3893+
drop table t1;
3894+
#
38693895
# End of 10.2 tests
38703896
#

mysql-test/suite/encryption/r/tempfiles_encrypted.result

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,32 @@ NULL
38723872
DROP VIEW v1;
38733873
DROP TABLE t1,t2;
38743874
#
3875+
# MDEV-25032 Window functions without column references get removed from ORDER BY
3876+
#
3877+
create table t1 (id int, score double);
3878+
insert into t1 values
3879+
(1, 5),
3880+
(1, 6),
3881+
(1, 6),
3882+
(1, 6),
3883+
(1, 7),
3884+
(1, 8.1),
3885+
(1, 9),
3886+
(1, 10);
3887+
select id, row_number() over () rn
3888+
from t1
3889+
order by rn desc;
3890+
id rn
3891+
1 8
3892+
1 7
3893+
1 6
3894+
1 5
3895+
1 4
3896+
1 3
3897+
1 2
3898+
1 1
3899+
drop table t1;
3900+
#
38753901
# End of 10.2 tests
38763902
#
38773903
#

mysql-test/t/win.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,26 @@ SELECT NTH_VALUE(i1, i1) OVER (PARTITION BY i1) FROM v1;
25222522
DROP VIEW v1;
25232523
DROP TABLE t1,t2;
25242524

2525+
--echo #
2526+
--echo # MDEV-25032 Window functions without column references get removed from ORDER BY
2527+
--echo #
2528+
2529+
create table t1 (id int, score double);
2530+
insert into t1 values
2531+
(1, 5),
2532+
(1, 6),
2533+
(1, 6),
2534+
(1, 6),
2535+
(1, 7),
2536+
(1, 8.1),
2537+
(1, 9),
2538+
(1, 10);
2539+
select id, row_number() over () rn
2540+
from t1
2541+
order by rn desc;
2542+
2543+
drop table t1;
2544+
25252545
--echo #
25262546
--echo # End of 10.2 tests
25272547
--echo #

sql/sql_select.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12671,6 +12671,7 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond,
1267112671
{
1267212672
table_map order_tables=order->item[0]->used_tables();
1267312673
if (order->item[0]->with_sum_func ||
12674+
order->item[0]->with_window_func ||
1267412675
/*
1267512676
If the outer table of an outer join is const (either by itself or
1267612677
after applying WHERE condition), grouping on a field from such a

0 commit comments

Comments
 (0)