Skip to content

Commit

Permalink
MDEV-9780: Window functions: interplay between window function and ot…
Browse files Browse the repository at this point in the history
…her constructs

Implement the "DISTINCT must not be converted into GROUP BY when window
functions are present" part.
  • Loading branch information
spetrunia committed Apr 10, 2016
1 parent da7c5e3 commit fbf0364
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
58 changes: 58 additions & 0 deletions mysql-test/r/win.result
Original file line number Diff line number Diff line change
Expand Up @@ -1902,3 +1902,61 @@ part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preced
200 3 4 6
200 4 8 12
drop table t1;
#
# MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
# window functions are present" part
#
create table t1 (part_id int, a int);
insert into t1 values
(100, 1),
(100, 2),
(100, 2),
(100, 3),
(2000, 1),
(2000, 2),
(2000, 3),
(2000, 3),
(2000, 3);
select rank() over (partition by part_id order by a) from t1;
rank() over (partition by part_id order by a)
1
2
2
4
1
2
3
3
3
select distinct rank() over (partition by part_id order by a) from t1;
rank() over (partition by part_id order by a)
1
2
4
3
explain format=json
select distinct rank() over (partition by part_id order by a) from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"duplicate_removal": {
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "t1.part_id, t1.a"
}
},
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 9,
"filtered": 100
}
}
}
}
}
}
drop table t1;
23 changes: 23 additions & 0 deletions mysql-test/t/win.test
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,27 @@ select
from t1;

drop table t1;
--echo #
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
--echo # window functions are present" part
--echo #

create table t1 (part_id int, a int);
insert into t1 values
(100, 1),
(100, 2),
(100, 2),
(100, 3),
(2000, 1),
(2000, 2),
(2000, 3),
(2000, 3),
(2000, 3);

select rank() over (partition by part_id order by a) from t1;
select distinct rank() over (partition by part_id order by a) from t1;
explain format=json
select distinct rank() over (partition by part_id order by a) from t1;

drop table t1;

2 changes: 2 additions & 0 deletions sql/sql_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,8 @@ class st_select_lex: public st_select_lex_node
return window_funcs.push_back(win_func);
}

bool have_window_funcs() const { return (window_funcs.elements !=0); }

private:
bool m_non_agg_field_used;
bool m_agg_func_used;
Expand Down
12 changes: 8 additions & 4 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,8 @@ JOIN::optimize_inner()
(!join_tab[const_tables].select ||
!join_tab[const_tables].select->quick ||
join_tab[const_tables].select->quick->get_type() !=
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) &&
!select_lex->have_window_funcs())
{
if (group && rollup.state == ROLLUP::STATE_NONE &&
list_contains_unique_index(join_tab[const_tables].table,
Expand Down Expand Up @@ -1709,11 +1710,13 @@ JOIN::optimize_inner()
}
if (group || tmp_table_param.sum_func_count)
{
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE
&& !select_lex->have_window_funcs())
select_distinct=0;
}
else if (select_distinct && table_count - const_tables == 1 &&
rollup.state == ROLLUP::STATE_NONE)
rollup.state == ROLLUP::STATE_NONE &&
!select_lex->have_window_funcs())
{
/*
We are only using one table. In this case we change DISTINCT to a
Expand Down Expand Up @@ -2283,7 +2286,8 @@ bool JOIN::make_aggr_tables_info()
tmp_table_param.hidden_field_count=
all_fields.elements - fields_list.elements;

distinct= select_distinct && !group_list;
distinct= select_distinct && !group_list &&
!select_lex->have_window_funcs();
keep_row_order= false;
if (create_postjoin_aggr_table(curr_tab,
&all_fields, tmp_group,
Expand Down

0 comments on commit fbf0364

Please sign in to comment.