diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index d48bb34d982..26194bc5990 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -3029,3 +3029,29 @@ show warnings; Level Code Message set sql_mode=@sql_mode_save; drop table t1; +# +# MDEV-12336: several functions over a window function +# +create table t1 (name varchar(10), cnt int); +insert into t1 values ('Fred', 23), ('Fred', 35), ('Joe', 10); +select q.name, q.row_cnt, +round( 100 * ( q.row_cnt / +sum(q.row_cnt) over +( +order by q.name +rows between +unbounded preceding and +unbounded following +) +),2 +) pct_of_total +from +( +select name, count(*) row_cnt, sum(cnt) sum_cnt +from t1 +group by 1 +) q; +name row_cnt pct_of_total +Fred 2 66.67 +Joe 1 33.33 +drop table t1; diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 3cd17ae179d..82c1dadf5ad 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -1830,3 +1830,29 @@ set sql_mode=@sql_mode_save; drop table t1; +--echo # +--echo # MDEV-12336: several functions over a window function +--echo # + +create table t1 (name varchar(10), cnt int); +insert into t1 values ('Fred', 23), ('Fred', 35), ('Joe', 10); + +select q.name, q.row_cnt, + round( 100 * ( q.row_cnt / + sum(q.row_cnt) over + ( + order by q.name + rows between + unbounded preceding and + unbounded following + ) + ),2 + ) pct_of_total +from +( + select name, count(*) row_cnt, sum(cnt) sum_cnt + from t1 + group by 1 +) q; + +drop table t1; diff --git a/sql/item.cc b/sql/item.cc index c34d27fa63b..2ff9d4e4a44 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1986,6 +1986,9 @@ void Item::split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array, &ref_pointer_array[el], 0, name)))) return; // fatal_error is set } + else if (type() == FUNC_ITEM && + ((Item_func *) this)->with_window_func) + return; else { if (!(item_ref= (new (thd->mem_root)