summaryrefslogtreecommitdiff
path: root/sql/sql_window.cc
diff options
context:
space:
mode:
authorSergei Petrunia <psergey@askmonty.org>2016-04-06 23:02:31 +0300
committerSergei Petrunia <psergey@askmonty.org>2016-04-06 23:02:31 +0300
commit306de8a927916db98c67fa338b5a275735f78240 (patch)
tree0073ea12fcbff7d3ad88068c93f6264699f69b50 /sql/sql_window.cc
parent2efabf81f1faae693ac381de688579283c6fad47 (diff)
downloadmariadb-git-306de8a927916db98c67fa338b5a275735f78240.tar.gz
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)."
Diffstat (limited to 'sql/sql_window.cc')
-rw-r--r--sql/sql_window.cc26
1 files changed, 17 insertions, 9 deletions
diff --git a/sql/sql_window.cc b/sql/sql_window.cc
index e479faa73d3..56f33937274 100644
--- a/sql/sql_window.cc
+++ b/sql/sql_window.cc
@@ -1829,16 +1829,9 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
List_iterator<Item_window_func> &it)
{
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,
- 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
+ do
{
Window_func_runner *runner;
if (!(runner= new Window_func_runner(win_func)) ||
@@ -1848,7 +1841,22 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
}
runners.push_back(runner);
it++;
+ prev_win_func= win_func;
} 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;
}