Commit 5606f878 authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-11594.

Using window functions over results of implicit groupings
required special handling in JOIN::make_aggr_tables_info.
The patch made sure that the result of implicit grouping
was written into a temporary table properly.
parent d123ed85
......@@ -2460,3 +2460,15 @@ username sum(amount) avg(sum(amount)) over (order by sum(amount) desc)
user1 9 34.5000
user2 60 60.0000
drop table t1;
#
# MDEV-11594: window function over implicit grouping
#
create table test.t1 (id int);
insert into test.t1 values (1), (2), (3), (2);
select sum(id) over (order by sum(id)) from t1;
sum(id) over (order by sum(id))
1
select sum(sum(id)) over (order by sum(id)) from t1;
sum(sum(id)) over (order by sum(id))
8
drop table t1;
......@@ -1491,4 +1491,16 @@ group by username;
drop table t1;
--echo #
--echo # MDEV-11594: window function over implicit grouping
--echo #
create table test.t1 (id int);
insert into test.t1 values (1), (2), (3), (2);
select sum(id) over (order by sum(id)) from t1;
select sum(sum(id)) over (order by sum(id)) from t1;
drop table t1;
......@@ -2177,6 +2177,9 @@ bool JOIN::make_aggr_tables_info()
const bool has_group_by= this->group;
sort_and_group_aggr_tab= NULL;
bool implicit_grouping_with_window_funcs= implicit_grouping &&
select_lex->have_window_funcs();
/*
......@@ -2338,9 +2341,11 @@ bool JOIN::make_aggr_tables_info()
distinct= select_distinct && !group_list &&
!select_lex->have_window_funcs();
keep_row_order= false;
bool save_sum_fields= (group_list && simple_group) ||
implicit_grouping_with_window_funcs;
if (create_postjoin_aggr_table(curr_tab,
&all_fields, tmp_group,
group_list && simple_group,
&all_fields, tmp_group,
save_sum_fields,
distinct, keep_row_order))
DBUG_RETURN(true);
exec_tmp_table= curr_tab->table;
......@@ -2540,7 +2545,9 @@ bool JOIN::make_aggr_tables_info()
count_field_types(select_lex, &tmp_table_param, *curr_all_fields, false);
}
if (group || implicit_grouping || tmp_table_param.sum_func_count)
if (group ||
(implicit_grouping && !implicit_grouping_with_window_funcs) ||
tmp_table_param.sum_func_count)
{
if (make_group_fields(this, this))
DBUG_RETURN(true);
......@@ -2772,13 +2779,15 @@ JOIN::create_postjoin_aggr_table(JOIN_TAB *tab, List<Item> *table_fields,
table->reginfo.join_tab= tab;
/* if group or order on first table, sort first */
if (group_list && simple_group)
if ((group_list && simple_group) ||
(implicit_grouping && select_lex->have_window_funcs()))
{
DBUG_PRINT("info",("Sorting for group"));
THD_STAGE_INFO(thd, stage_sorting_for_group);
if (ordered_index_usage != ordered_index_group_by &&
(join_tab + const_tables)->type != JT_CONST && // Don't sort 1 row
!implicit_grouping &&
add_sorting_to_table(join_tab + const_tables, group_list))
goto err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment