Skip to content

Commit 5606f87

Browse files
committed
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.
1 parent d123ed8 commit 5606f87

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

mysql-test/r/win.result

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,3 +2460,15 @@ username sum(amount) avg(sum(amount)) over (order by sum(amount) desc)
24602460
user1 9 34.5000
24612461
user2 60 60.0000
24622462
drop table t1;
2463+
#
2464+
# MDEV-11594: window function over implicit grouping
2465+
#
2466+
create table test.t1 (id int);
2467+
insert into test.t1 values (1), (2), (3), (2);
2468+
select sum(id) over (order by sum(id)) from t1;
2469+
sum(id) over (order by sum(id))
2470+
1
2471+
select sum(sum(id)) over (order by sum(id)) from t1;
2472+
sum(sum(id)) over (order by sum(id))
2473+
8
2474+
drop table t1;

mysql-test/t/win.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,4 +1491,16 @@ group by username;
14911491

14921492
drop table t1;
14931493

1494+
--echo #
1495+
--echo # MDEV-11594: window function over implicit grouping
1496+
--echo #
1497+
1498+
create table test.t1 (id int);
1499+
insert into test.t1 values (1), (2), (3), (2);
1500+
1501+
select sum(id) over (order by sum(id)) from t1;
1502+
1503+
select sum(sum(id)) over (order by sum(id)) from t1;
1504+
1505+
drop table t1;
14941506

sql/sql_select.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,9 @@ bool JOIN::make_aggr_tables_info()
21772177
const bool has_group_by= this->group;
21782178

21792179
sort_and_group_aggr_tab= NULL;
2180+
2181+
bool implicit_grouping_with_window_funcs= implicit_grouping &&
2182+
select_lex->have_window_funcs();
21802183

21812184

21822185
/*
@@ -2338,9 +2341,11 @@ bool JOIN::make_aggr_tables_info()
23382341
distinct= select_distinct && !group_list &&
23392342
!select_lex->have_window_funcs();
23402343
keep_row_order= false;
2344+
bool save_sum_fields= (group_list && simple_group) ||
2345+
implicit_grouping_with_window_funcs;
23412346
if (create_postjoin_aggr_table(curr_tab,
2342-
&all_fields, tmp_group,
2343-
group_list && simple_group,
2347+
&all_fields, tmp_group,
2348+
save_sum_fields,
23442349
distinct, keep_row_order))
23452350
DBUG_RETURN(true);
23462351
exec_tmp_table= curr_tab->table;
@@ -2540,7 +2545,9 @@ bool JOIN::make_aggr_tables_info()
25402545
count_field_types(select_lex, &tmp_table_param, *curr_all_fields, false);
25412546
}
25422547

2543-
if (group || implicit_grouping || tmp_table_param.sum_func_count)
2548+
if (group ||
2549+
(implicit_grouping && !implicit_grouping_with_window_funcs) ||
2550+
tmp_table_param.sum_func_count)
25442551
{
25452552
if (make_group_fields(this, this))
25462553
DBUG_RETURN(true);
@@ -2772,13 +2779,15 @@ JOIN::create_postjoin_aggr_table(JOIN_TAB *tab, List<Item> *table_fields,
27722779
table->reginfo.join_tab= tab;
27732780

27742781
/* if group or order on first table, sort first */
2775-
if (group_list && simple_group)
2782+
if ((group_list && simple_group) ||
2783+
(implicit_grouping && select_lex->have_window_funcs()))
27762784
{
27772785
DBUG_PRINT("info",("Sorting for group"));
27782786
THD_STAGE_INFO(thd, stage_sorting_for_group);
27792787

27802788
if (ordered_index_usage != ordered_index_group_by &&
27812789
(join_tab + const_tables)->type != JT_CONST && // Don't sort 1 row
2790+
!implicit_grouping &&
27822791
add_sorting_to_table(join_tab + const_tables, group_list))
27832792
goto err;
27842793

0 commit comments

Comments
 (0)