Skip to content

Commit

Permalink
MDEV-15208: server crashed, when using ORDER BY with window function …
Browse files Browse the repository at this point in the history
…and UNION

SELECTs inside a UNION can have window function but not the global ORDER BY clause of the UNION.
  • Loading branch information
Varun Gupta authored and spetrunia committed Feb 21, 2022
1 parent d140d27 commit 942a979
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions mysql-test/r/win.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
10 changes: 10 additions & 0 deletions mysql-test/t/win.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
4 changes: 3 additions & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 942a979

Please sign in to comment.