Skip to content

Commit fbf0364

Browse files
committed
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.
1 parent da7c5e3 commit fbf0364

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

mysql-test/r/win.result

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,3 +1902,61 @@ part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preced
19021902
200 3 4 6
19031903
200 4 8 12
19041904
drop table t1;
1905+
#
1906+
# MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
1907+
# window functions are present" part
1908+
#
1909+
create table t1 (part_id int, a int);
1910+
insert into t1 values
1911+
(100, 1),
1912+
(100, 2),
1913+
(100, 2),
1914+
(100, 3),
1915+
(2000, 1),
1916+
(2000, 2),
1917+
(2000, 3),
1918+
(2000, 3),
1919+
(2000, 3);
1920+
select rank() over (partition by part_id order by a) from t1;
1921+
rank() over (partition by part_id order by a)
1922+
1
1923+
2
1924+
2
1925+
4
1926+
1
1927+
2
1928+
3
1929+
3
1930+
3
1931+
select distinct rank() over (partition by part_id order by a) from t1;
1932+
rank() over (partition by part_id order by a)
1933+
1
1934+
2
1935+
4
1936+
3
1937+
explain format=json
1938+
select distinct rank() over (partition by part_id order by a) from t1;
1939+
EXPLAIN
1940+
{
1941+
"query_block": {
1942+
"select_id": 1,
1943+
"duplicate_removal": {
1944+
"window_functions_computation": {
1945+
"sorts": {
1946+
"filesort": {
1947+
"sort_key": "t1.part_id, t1.a"
1948+
}
1949+
},
1950+
"temporary_table": {
1951+
"table": {
1952+
"table_name": "t1",
1953+
"access_type": "ALL",
1954+
"rows": 9,
1955+
"filtered": 100
1956+
}
1957+
}
1958+
}
1959+
}
1960+
}
1961+
}
1962+
drop table t1;

mysql-test/t/win.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,4 +1164,27 @@ select
11641164
from t1;
11651165

11661166
drop table t1;
1167+
--echo #
1168+
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
1169+
--echo # window functions are present" part
1170+
--echo #
1171+
1172+
create table t1 (part_id int, a int);
1173+
insert into t1 values
1174+
(100, 1),
1175+
(100, 2),
1176+
(100, 2),
1177+
(100, 3),
1178+
(2000, 1),
1179+
(2000, 2),
1180+
(2000, 3),
1181+
(2000, 3),
1182+
(2000, 3);
1183+
1184+
select rank() over (partition by part_id order by a) from t1;
1185+
select distinct rank() over (partition by part_id order by a) from t1;
1186+
explain format=json
1187+
select distinct rank() over (partition by part_id order by a) from t1;
1188+
1189+
drop table t1;
11671190

sql/sql_lex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,8 @@ class st_select_lex: public st_select_lex_node
11131113
return window_funcs.push_back(win_func);
11141114
}
11151115

1116+
bool have_window_funcs() const { return (window_funcs.elements !=0); }
1117+
11161118
private:
11171119
bool m_non_agg_field_used;
11181120
bool m_agg_func_used;

sql/sql_select.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,8 @@ JOIN::optimize_inner()
16591659
(!join_tab[const_tables].select ||
16601660
!join_tab[const_tables].select->quick ||
16611661
join_tab[const_tables].select->quick->get_type() !=
1662-
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1662+
QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) &&
1663+
!select_lex->have_window_funcs())
16631664
{
16641665
if (group && rollup.state == ROLLUP::STATE_NONE &&
16651666
list_contains_unique_index(join_tab[const_tables].table,
@@ -1709,11 +1710,13 @@ JOIN::optimize_inner()
17091710
}
17101711
if (group || tmp_table_param.sum_func_count)
17111712
{
1712-
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1713+
if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE
1714+
&& !select_lex->have_window_funcs())
17131715
select_distinct=0;
17141716
}
17151717
else if (select_distinct && table_count - const_tables == 1 &&
1716-
rollup.state == ROLLUP::STATE_NONE)
1718+
rollup.state == ROLLUP::STATE_NONE &&
1719+
!select_lex->have_window_funcs())
17171720
{
17181721
/*
17191722
We are only using one table. In this case we change DISTINCT to a
@@ -2283,7 +2286,8 @@ bool JOIN::make_aggr_tables_info()
22832286
tmp_table_param.hidden_field_count=
22842287
all_fields.elements - fields_list.elements;
22852288

2286-
distinct= select_distinct && !group_list;
2289+
distinct= select_distinct && !group_list &&
2290+
!select_lex->have_window_funcs();
22872291
keep_row_order= false;
22882292
if (create_postjoin_aggr_table(curr_tab,
22892293
&all_fields, tmp_group,

0 commit comments

Comments
 (0)