Commit fbf0364c authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-9780: Window functions: interplay between window function and other constructs

Implement the "DISTINCT must not be converted into GROUP BY when window
functions are present" part.
parent da7c5e3b
......@@ -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;
......@@ -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;
......@@ -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;
......
......@@ -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,
......@@ -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
......@@ -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,
......
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