summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorVicențiu Ciorbaru <vicentiu@mariadb.org>2018-11-01 08:55:16 +0200
committerVicențiu Ciorbaru <vicentiu@mariadb.org>2018-11-01 21:15:55 +0200
commit1c6b982e02eeaa75bb6c2f2a3c2b64491dd6d3c8 (patch)
treedd639542380856235c0d38ede1c184f148f7dff9 /sql
parentdd6e74c62a2aa44d9d5e1790bcd36d3ff1c7d98b (diff)
downloadmariadb-git-1c6b982e02eeaa75bb6c2f2a3c2b64491dd6d3c8.tar.gz
MDEV-12779 Oracle/DB2 Compatibility Implicit Ordering for ROW_NUMBER OVER
Users expect window functions to produce a certain ordering of rows in the final result set. Although the standard does not require this, we already have the filesort result done for when we computed the window function. If there is no ORDER BY attached to the query, just keep it till the SELECT is completely evaluated and use that to print the result. Update test cases as many did not take care to guarantee a stable result.
Diffstat (limited to 'sql')
-rw-r--r--sql/sql_select.cc9
-rw-r--r--sql/sql_window.cc17
-rw-r--r--sql/sql_window.h4
3 files changed, 22 insertions, 8 deletions
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 1fa80da85a6..0cdecf1bf2e 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -26649,9 +26649,10 @@ AGGR_OP::end_send()
// Update ref array
join_tab->join->set_items_ref_array(*join_tab->ref_array);
+ bool keep_last_filesort_result = join_tab->filesort ? false : true;
if (join_tab->window_funcs_step)
{
- if (join_tab->window_funcs_step->exec(join))
+ if (join_tab->window_funcs_step->exec(join, keep_last_filesort_result))
return NESTED_LOOP_ERROR;
}
@@ -26705,6 +26706,12 @@ AGGR_OP::end_send()
}
}
+ if (keep_last_filesort_result)
+ {
+ delete join_tab->filesort_result;
+ join_tab->filesort_result= NULL;
+ }
+
// Finish rnd scn after sending records
if (join_tab->table->file->inited)
join_tab->table->file->ha_rnd_end();
diff --git a/sql/sql_window.cc b/sql/sql_window.cc
index cd09e174808..465c6ae032c 100644
--- a/sql/sql_window.cc
+++ b/sql/sql_window.cc
@@ -2751,7 +2751,7 @@ bool Window_func_runner::exec(THD *thd, TABLE *tbl, SORT_INFO *filesort_result)
}
-bool Window_funcs_sort::exec(JOIN *join)
+bool Window_funcs_sort::exec(JOIN *join, bool keep_filesort_result)
{
THD *thd= join->thd;
JOIN_TAB *join_tab= join->join_tab + join->total_join_tab_cnt();
@@ -2766,8 +2766,11 @@ bool Window_funcs_sort::exec(JOIN *join)
bool is_error= runner.exec(thd, tbl, filesort_result);
- delete join_tab->filesort_result;
- join_tab->filesort_result= NULL;
+ if (!keep_filesort_result)
+ {
+ delete join_tab->filesort_result;
+ join_tab->filesort_result= NULL;
+ }
return is_error;
}
@@ -2876,14 +2879,18 @@ bool Window_funcs_computation::setup(THD *thd,
}
-bool Window_funcs_computation::exec(JOIN *join)
+bool Window_funcs_computation::exec(JOIN *join, bool keep_last_filesort_result)
{
List_iterator<Window_funcs_sort> it(win_func_sorts);
Window_funcs_sort *srt;
+ uint counter= 0; /* Count how many sorts we've executed. */
/* Execute each sort */
while ((srt = it++))
{
- if (srt->exec(join))
+ counter++;
+ bool keep_filesort_result= keep_last_filesort_result &&
+ counter == win_func_sorts.elements;
+ if (srt->exec(join, keep_filesort_result))
return true;
}
return false;
diff --git a/sql/sql_window.h b/sql/sql_window.h
index 6a56fc84392..e0c1563e5bb 100644
--- a/sql/sql_window.h
+++ b/sql/sql_window.h
@@ -195,7 +195,7 @@ class Window_funcs_sort : public Sql_alloc
public:
bool setup(THD *thd, SQL_SELECT *sel, List_iterator<Item_window_func> &it,
st_join_table *join_tab);
- bool exec(JOIN *join);
+ bool exec(JOIN *join, bool keep_filesort_result);
void cleanup() { delete filesort; }
friend class Window_funcs_computation;
@@ -225,7 +225,7 @@ class Window_funcs_computation : public Sql_alloc
List<Window_funcs_sort> win_func_sorts;
public:
bool setup(THD *thd, List<Item_window_func> *window_funcs, st_join_table *tab);
- bool exec(JOIN *join);
+ bool exec(JOIN *join, bool keep_last_filesort_result);
Explain_aggr_window_funcs *save_explain_plan(MEM_ROOT *mem_root, bool is_analyze);
void cleanup();