Commit 306de8a9 authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-9877: Window functions: wrong sort criteria is used

" The sort order for the sub-sequence of window functions starting
from the element marked by SORTORDER_CHANGE_FLAG up to the next
element marked by SORTORDER_CHANGE_FLAG must be taken from the
last element of the sub-sequence (not from the first one)."
parent 2efabf81
...@@ -1693,6 +1693,58 @@ EXPLAIN ...@@ -1693,6 +1693,58 @@ EXPLAIN
} }
} }
} }
explain format=json
select
rank() over (partition by c order by a),
count(*) over (partition by c)
from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "t1.c, t1.a"
}
},
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 3,
"filtered": 100
}
}
}
}
}
explain format=json
select
count(*) over (partition by c),
rank() over (partition by c order by a)
from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "t1.c, t1.a"
}
},
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 3,
"filtered": 100
}
}
}
}
}
drop table t1; drop table t1;
# #
# MDEV-9847: Window functions: crash with big_tables=1 # MDEV-9847: Window functions: crash with big_tables=1
......
...@@ -1053,13 +1053,17 @@ select ...@@ -1053,13 +1053,17 @@ select
row_number() over (order by a) row_number() over (order by a)
from t1; from t1;
--disable_parsing
explain format=json explain format=json
select select
rank() over (partition by c order by a), rank() over (partition by c order by a),
count(*) over (partition by c) count(*) over (partition by c)
from t1; from t1;
--enable_parsing
explain format=json
select
count(*) over (partition by c),
rank() over (partition by c order by a)
from t1;
drop table t1; drop table t1;
......
...@@ -1829,16 +1829,9 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel, ...@@ -1829,16 +1829,9 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
List_iterator<Item_window_func> &it) List_iterator<Item_window_func> &it)
{ {
Item_window_func *win_func= it.peek(); Item_window_func *win_func= it.peek();
Window_spec *spec = win_func->window_spec; Item_window_func *prev_win_func;
ORDER* sort_order= concat_order_lists(thd->mem_root, do
spec->partition_list->first,
spec->order_list->first);
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL);
/* Apply the same condition that the subsequent sort has. */
filesort->select= sel;
do
{ {
Window_func_runner *runner; Window_func_runner *runner;
if (!(runner= new Window_func_runner(win_func)) || if (!(runner= new Window_func_runner(win_func)) ||
...@@ -1848,7 +1841,22 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel, ...@@ -1848,7 +1841,22 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
} }
runners.push_back(runner); runners.push_back(runner);
it++; it++;
prev_win_func= win_func;
} while ((win_func= it.peek()) && !(win_func->marker & SORTORDER_CHANGE_FLAG)); } while ((win_func= it.peek()) && !(win_func->marker & SORTORDER_CHANGE_FLAG));
/*
The sort criteria must be taken from the last win_func in the group of
adjacent win_funcs that do not have SORTORDER_CHANGE_FLAG.
*/
Window_spec *spec = prev_win_func->window_spec;
ORDER* sort_order= concat_order_lists(thd->mem_root,
spec->partition_list->first,
spec->order_list->first);
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL);
/* Apply the same condition that the subsequent sort has. */
filesort->select= sel;
return false; return false;
} }
......
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