[Commits] 8e0e3968c37: MDEV-15208: server crashed, when using ORDER BY with window function and UNION
revision-id: 8e0e3968c37345d799982eefbb956d5e492fdc73 (mariadb-10.2.43-14-g8e0e3968c37) parent(s): d140d276244508d7f2e69a3c1a23e0b09201f71a author: Varun Gupta committer: Sergei Petrunia timestamp: 2022-02-21 20:23:39 +0300 message: MDEV-15208: server crashed, when using ORDER BY with window function and UNION (Edits by SergeiP: fix encryption.tempfiles_encrypted, re-word comment) Global ORDER BY clause of a UNION may not refer to 1) aggregate functions or 2) window functions. setup_order() checked for #1 but not for #2. --- mysql-test/r/win.result | 8 ++++++++ mysql-test/suite/encryption/r/tempfiles_encrypted.result | 8 ++++++++ mysql-test/t/win.test | 10 ++++++++++ sql/sql_select.cc | 4 +++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index 30650f29555..1ddafbd4f8f 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -4230,5 +4230,13 @@ i LAST_VALUE(COUNT(i)) OVER (PARTITION BY i ORDER BY j) 4 2 DROP TABLE t1; # +# MDEV-15208: server crashed, when using ORDER BY with window function and UNION +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(2),(2),(2),(2),(2); +SELECT 1 UNION SELECT a FROM t1 ORDER BY (row_number() over ()); +ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to a UNION +DROP TABLE t1; +# # End of 10.2 tests # diff --git a/mysql-test/suite/encryption/r/tempfiles_encrypted.result b/mysql-test/suite/encryption/r/tempfiles_encrypted.result index aaba6bb044a..f7b5081f625 100644 --- a/mysql-test/suite/encryption/r/tempfiles_encrypted.result +++ b/mysql-test/suite/encryption/r/tempfiles_encrypted.result @@ -4236,6 +4236,14 @@ i LAST_VALUE(COUNT(i)) OVER (PARTITION BY i ORDER BY j) 4 2 DROP TABLE t1; # +# MDEV-15208: server crashed, when using ORDER BY with window function and UNION +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(2),(2),(2),(2),(2); +SELECT 1 UNION SELECT a FROM t1 ORDER BY (row_number() over ()); +ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to a UNION +DROP TABLE t1; +# # End of 10.2 tests # # diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 126ed735c88..37f09a6e850 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -2730,6 +2730,16 @@ INSERT INTO t1 VALUES (1,1), (1,5),(1,4), (2,2),(2,5), (3,3),(4,4); SELECT i, LAST_VALUE(COUNT(i)) OVER (PARTITION BY i ORDER BY j) FROM t1 GROUP BY i; DROP TABLE t1; +--echo # +--echo # MDEV-15208: server crashed, when using ORDER BY with window function and UNION +--echo # + +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(2),(2),(2),(2),(2); +--error ER_AGGREGATE_ORDER_FOR_UNION +SELECT 1 UNION SELECT a FROM t1 ORDER BY (row_number() over ()); +DROP TABLE t1; + --echo # --echo # End of 10.2 tests --echo # diff --git a/sql/sql_select.cc b/sql/sql_select.cc index db8a63eeb48..a3e8b453897 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -22796,7 +22796,9 @@ int setup_order(THD *thd, Ref_ptr_array ref_pointer_array, TABLE_LIST *tables, an ORDER BY clause */ - if (for_union && (*order->item)->with_sum_func) + if (for_union && + ((*order->item)->with_sum_func || + (*order->item)->with_window_func)) { my_error(ER_AGGREGATE_ORDER_FOR_UNION, MYF(0), number); return 1;
participants (1)
-
psergey