From a6ea7996513be944473e137b36df5353fd3025f7 Mon Sep 17 00:00:00 2001 From: Varun Gupta Date: Fri, 3 May 2019 17:10:51 +0530 Subject: MDEV-14791: Crash with order by expression containing window functions The issue here is that for a window function in the ORDER BY clause, we were not creating an extra field in the temporary table for the window function (which is contained in an expression). So a call to split_sum_func is added to handle this case Also we need to update all items that contain a window function in the temp table during window function computation as filesort would need these values to be updated to calculate the ORDER BY clause of the select. --- sql/sql_window.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'sql/sql_window.cc') diff --git a/sql/sql_window.cc b/sql/sql_window.cc index 310cf5bfd91..b258b8f56c9 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -2527,11 +2527,38 @@ bool save_window_function_values(List& window_functions, TABLE *tbl, uchar *rowid_buf) { List_iterator_fast iter(window_functions); + JOIN_TAB *join_tab= tbl->reginfo.join_tab; tbl->file->ha_rnd_pos(tbl->record[0], rowid_buf); store_record(tbl, record[1]); while (Item_window_func *item_win= iter++) item_win->save_in_field(item_win->result_field, true); + /* + In case we have window functions present, an extra step is required + to compute all the fields from the temporary table. + In case we have a compound expression such as: expr + expr, + where one of the terms has a window function inside it, only + after computing window function values we actually know the true + final result of the compounded expression. + + Go through all the func items and save their values once again in the + corresponding temp table fields. Do this for each row in the table. + + This needs to be done earlier because ORDER BY clause can also have + a window function, so we need to make sure all the fields of the temp.table + are updated before we do the filesort. So is best to update the other fields + that contain the window functions along with the computation of window + functions. + */ + + Item **func_ptr= join_tab->tmp_table_param->items_to_copy; + Item *func; + for (; (func = *func_ptr) ; func_ptr++) + { + if (func->with_window_func && func->type() != Item::WINDOW_FUNC_ITEM) + func->save_in_result_field(true); + } + int err= tbl->file->ha_update_row(tbl->record[1], tbl->record[0]); if (err && err != HA_ERR_RECORD_IS_THE_SAME) return true; -- cgit v1.2.1