revision-id: 09ca42e456b04901b85684525f67cd7a2882e0f0 (mariadb-10.2.5-624-g09ca42e456b) parent(s): 9bcd0f5fea8ca26742b10d37b95a966c69909ff1 author: Varun Gupta committer: Varun Gupta timestamp: 2018-05-08 12:49:23 +0530 message: MDEV-15853: Assertion `tab->filesort_result == 0' failed The issue here is that the window function execution is not called for the correct join tab, when we have GROUP BY where we create extra temporary tables then we need to call window function execution for the last join tab. For doing so the current code does not take into account the JOIN::aggr_tables. Fixed by making join_tab for the execution of window function to take in account these JOIN::aggr_tables. --- mysql-test/r/win.result | 15 +++++++++++++++ mysql-test/t/win.test | 14 ++++++++++++++ sql/sql_window.cc | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index e3cb40e8343..a921a5cbdbc 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3299,3 +3299,18 @@ ROW_NUMBER() OVER() i SELECT ROW_NUMBER() OVER(), i FROM t1 WHERE 0; ROW_NUMBER() OVER() i DROP TABLE t1; +# +# MDEV-15853: Assertion `tab->filesort_result == 0' failed +# +CREATE TABLE t1 ( a1 int); +insert into t1 values (1),(2),(3); +CREATE TABLE t2 (b1 int, a1 int, a2 int); +insert into t2 values (1,2,3),(2,3,4),(3,4,5); +SELECT COUNT(DISTINCT t2.a2), +rank() OVER (ORDER BY t2.b1) +FROM t2 ,t1 GROUP BY t1.a1 ORDER BY t2.a1; +COUNT(DISTINCT t2.a2) rank() OVER (ORDER BY t2.b1) +3 1 +3 1 +3 3 +DROP TABLE t1,t2; diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 95ffb6d9909..7a18c5d36b6 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -2067,3 +2067,17 @@ SELECT DISTINCT ROW_NUMBER() OVER(), i FROM t1 WHERE 0; SELECT ROW_NUMBER() OVER(), i FROM t1 WHERE 0; DROP TABLE t1; +--echo # +--echo # MDEV-15853: Assertion `tab->filesort_result == 0' failed +--echo # + +CREATE TABLE t1 ( a1 int); +insert into t1 values (1),(2),(3); + +CREATE TABLE t2 (b1 int, a1 int, a2 int); +insert into t2 values (1,2,3),(2,3,4),(3,4,5); + +SELECT COUNT(DISTINCT t2.a2), + rank() OVER (ORDER BY t2.b1) +FROM t2 ,t1 GROUP BY t1.a1 ORDER BY t2.a1; +DROP TABLE t1,t2; diff --git a/sql/sql_window.cc b/sql/sql_window.cc index 4e1e64365ae..562e7f10045 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -2754,7 +2754,8 @@ bool Window_func_runner::exec(THD *thd, TABLE *tbl, SORT_INFO *filesort_result) bool Window_funcs_sort::exec(JOIN *join) { THD *thd= join->thd; - JOIN_TAB *join_tab= join->join_tab + join->exec_join_tab_cnt(); + JOIN_TAB *join_tab= join->join_tab + join->exec_join_tab_cnt() + + join->aggr_tables -1; /* Sort the table based on the most specific sorting criteria of the window functions. */