revision-id: d64e60b83edddb4a4482df9c2bdf5bc53388cd3d (mariadb-10.2.29-1-gd64e60b83ed) parent(s): 90451a59811b415a443aab97230e694856392cb5 author: Varun Gupta committer: Varun Gupta timestamp: 2019-12-05 17:31:17 +0530 message: MDEV-16579: Wrong result of query using DISTINCT COUNT(*) OVER (*) The query requires 2 temporary tables for execution, the window function is always attached to the last temporary table, but in this case the result field of the window function points to the first temporary table rather than the last one. Fixed this by not changing window function items with temporary table items of the first temporary table. --- mysql-test/r/win.result | 10 ++++++++++ mysql-test/t/win.test | 10 ++++++++++ sql/sql_select.cc | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index 13d452f3ef2..42aedf49f8d 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3634,5 +3634,15 @@ rank() over (partition by 'abc' order by 'xyz') 1 drop table t1; # +# MDEV-16579: Wrong result of query using DISTINCT COUNT(*) OVER (*) +# +CREATE TABLE t1 (i int) ; +INSERT INTO t1 VALUES (1),(0),(1),(2),(0),(1),(2),(1),(2); +SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM t1 GROUP BY i ; +COUNT(*) OVER () MOD(MIN(i),2) +3 0 +3 1 +drop table t1; +# # End of 10.2 tests # diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index a4d42ce2b91..a2d2ebb7465 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -2341,6 +2341,16 @@ select rank() over (partition by 'abc' order by 'xyz') from t1; select rank() over (partition by 'abc' order by 'xyz') from t1; drop table t1; +--echo # +--echo # MDEV-16579: Wrong result of query using DISTINCT COUNT(*) OVER (*) +--echo # + +CREATE TABLE t1 (i int) ; +INSERT INTO t1 VALUES (1),(0),(1),(2),(0),(1),(2),(1),(2); + +SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM t1 GROUP BY i ; +drop table t1; + --echo # --echo # End of 10.2 tests --echo # diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c450572c9bd..c7929595d60 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -23628,7 +23628,8 @@ change_to_use_tmp_fields(THD *thd, Ref_ptr_array ref_pointer_array, for (uint i= 0; (item= it++); i++) { Field *field; - 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_field= item; else if (item->type() == Item::FIELD_ITEM) item_field= item->get_tmp_table_item(thd);