diff options
author | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2016-09-16 20:38:22 +0200 |
---|---|---|
committer | Vicențiu Ciorbaru <vicentiu@mariadb.org> | 2016-09-24 15:12:34 +0200 |
commit | 2857ff3c98cd2720d28aaf452f2c63ca7f98604b (patch) | |
tree | 370b53ce83ae1bc581827126bc5a1875abf45932 /sql | |
parent | 1c72441364157f6856406bbfc3a753868d33fbef (diff) | |
download | mariadb-git-2857ff3c98cd2720d28aaf452f2c63ca7f98604b.tar.gz |
MDEV-10815: Window Function Expressions Wrong Results
Fix window function expressions such as win_func() <operator> expr.
The problem was found in 2 places.
First, when we have complex expressions containing window functions, we
can only compute their final value _after_ we have computed the window
function's values. These values must be stored within the temporary
table that we are using, before sending them off.
This is done by performing an extra copy_funcs call before the final
end_send() call.
Second, such expressions need to have their inner arguments,
changed such that the references within those arguments point to fields within
the temporary table.
Ex: sum(t.a) over (order by t.b) + sum(t.a) over (order by t.b)
Before this fix, t.a pointed to the original table's a field. In order
to compute the sum function's value correctly, it needs to point to the
copy of this field inside the temp table.
This is done by calling split_sum_func for each argument in the
expression in turn.
The win.test results have also been updated as they contained wrong
values for such a use case.
Diffstat (limited to 'sql')
-rw-r--r-- | sql/item.cc | 4 | ||||
-rw-r--r-- | sql/item_windowfunc.h | 1 | ||||
-rw-r--r-- | sql/sql_select.cc | 15 |
3 files changed, 20 insertions, 0 deletions
diff --git a/sql/item.cc b/sql/item.cc index d461386a696..ec4eae2a46a 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -1876,7 +1876,11 @@ void Item::split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array, Skip the else part, window functions are very special functions: they need to have their own fields in the temp. table, but they need to be proceessed differently than regular aggregate functions + + Call split_sum_func here so that each argument gets its fields to + point to the temporary table. */ + split_sum_func(thd, ref_pointer_array, fields, split_flags); } else { diff --git a/sql/item_windowfunc.h b/sql/item_windowfunc.h index 433b3f6c4fd..3d4c310fa74 100644 --- a/sql/item_windowfunc.h +++ b/sql/item_windowfunc.h @@ -771,6 +771,7 @@ public: void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array, List<Item> &fields, uint flags); + void fix_length_and_dec() { decimals = window_func()->decimals; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c70e0d5b7e2..1eaa1ced0b8 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -26228,7 +26228,22 @@ AGGR_OP::end_send() rc= NESTED_LOOP_KILLED; } else + { + /* + 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. + */ + if (join_tab->window_funcs_step) + copy_funcs(join_tab->tmp_table_param->items_to_copy, join->thd); rc= evaluate_join_record(join, join_tab, 0); + } } // Finish rnd scn after sending records |