revision-id: ea3a92c3f7a7ba6a4957f305e85ee6fcb49c1ff4 (mariadb-10.2.16-128-gea3a92c3f7a) parent(s): fbe35f63afb537f5f4eba10375c4b0b0e613d438 author: Varun Gupta committer: Varun Gupta timestamp: 2018-09-20 17:57:44 +0530 message: MDEV-14791: Crash with order by expression containing window functions The issue here is that for a window function in the ORDER BY clause, we were not creating an extra field in the temporary table for the window function (which is contained in an expression). So a call to split_sum_func is added to handle this case --- mysql-test/r/win.result | 14 ++++++++++++++ mysql-test/t/win.test | 11 +++++++++++ sql/sql_select.cc | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index e6802075266..525eb4e2ab0 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3357,3 +3357,17 @@ GROUP BY i ) AS sq; ROW_NUMBER() OVER() i sum(i) drop table t1; +# +# MDEV-14791: Crash with order by expression containing window functions +# +CREATE TABLE t1 (b1 int, b2 int, b3 int, b4 int , b5 int, b6 int); +INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0); +explain +SELECT b1 from t1 order by row_number() over (ORDER BY b6) + 1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort +SELECT b1 from t1 order by row_number() over (ORDER BY b6) + 1; +b1 +1 +0 +DROP TABLE t1; diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 46345b65831..e3e4e992fa6 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -2125,3 +2125,14 @@ SELECT * FROM ( ) AS sq; drop table t1; +--echo # +--echo # MDEV-14791: Crash with order by expression containing window functions +--echo # + +CREATE TABLE t1 (b1 int, b2 int, b3 int, b4 int , b5 int, b6 int); +INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0); + +explain +SELECT b1 from t1 order by row_number() over (ORDER BY b6) + 1; +SELECT b1 from t1 order by row_number() over (ORDER BY b6) + 1; +DROP TABLE t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 9343fbd1bb1..cca23504230 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -933,7 +933,8 @@ JOIN::prepare(TABLE_LIST *tables_init, item->max_length))) real_order= TRUE; - if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) + if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) || + item->with_window_func) item->split_sum_func(thd, ref_ptrs, all_fields, 0); } if (!real_order)