Skip to content

Commit

Permalink
MDEV-25032: Window functions without column references get removed fr…
Browse files Browse the repository at this point in the history
…om 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
  • Loading branch information
cvicentiu committed Mar 4, 2021
1 parent b044898 commit 5da6ffe
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions mysql-test/r/win.result
Original file line number Diff line number Diff line change
Expand Up @@ -3866,5 +3866,31 @@ NULL
DROP VIEW v1;
DROP TABLE t1,t2;
#
# MDEV-25032 Window functions without column references get removed from ORDER BY
#
create table t1 (id int, score double);
insert into t1 values
(1, 5),
(1, 6),
(1, 6),
(1, 6),
(1, 7),
(1, 8.1),
(1, 9),
(1, 10);
select id, row_number() over () rn
from t1
order by rn desc;
id rn
1 8
1 7
1 6
1 5
1 4
1 3
1 2
1 1
drop table t1;
#
# End of 10.2 tests
#
26 changes: 26 additions & 0 deletions mysql-test/suite/encryption/r/tempfiles_encrypted.result
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,32 @@ NULL
DROP VIEW v1;
DROP TABLE t1,t2;
#
# MDEV-25032 Window functions without column references get removed from ORDER BY
#
create table t1 (id int, score double);
insert into t1 values
(1, 5),
(1, 6),
(1, 6),
(1, 6),
(1, 7),
(1, 8.1),
(1, 9),
(1, 10);
select id, row_number() over () rn
from t1
order by rn desc;
id rn
1 8
1 7
1 6
1 5
1 4
1 3
1 2
1 1
drop table t1;
#
# End of 10.2 tests
#
#
Expand Down
20 changes: 20 additions & 0 deletions mysql-test/t/win.test
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,26 @@ SELECT NTH_VALUE(i1, i1) OVER (PARTITION BY i1) FROM v1;
DROP VIEW v1;
DROP TABLE t1,t2;

--echo #
--echo # MDEV-25032 Window functions without column references get removed from ORDER BY
--echo #

create table t1 (id int, score double);
insert into t1 values
(1, 5),
(1, 6),
(1, 6),
(1, 6),
(1, 7),
(1, 8.1),
(1, 9),
(1, 10);
select id, row_number() over () rn
from t1
order by rn desc;

drop table t1;

--echo #
--echo # End of 10.2 tests
--echo #
1 change: 1 addition & 0 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12671,6 +12671,7 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond,
{
table_map order_tables=order->item[0]->used_tables();
if (order->item[0]->with_sum_func ||
order->item[0]->with_window_func ||
/*
If the outer table of an outer join is const (either by itself or
after applying WHERE condition), grouping on a field from such a
Expand Down

0 comments on commit 5da6ffe

Please sign in to comment.