From 5bf338435aae358b2661ce2dc04b5a70d3d0c783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Tue, 7 Feb 2017 17:16:20 +0200 Subject: MDEV-11746: Wrong result upon using FIRST_VALUE with a window frame The same approach is needed for LAST_VALUE, otherwise the LAST_VALUE sum functions are not cleared correctly. Now LAST_VALUE behaves as NTH_VALUE with 0 offset, only that the frame that it is examining is the bottom bound, not the top bound. --- sql/sql_window.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'sql/sql_window.cc') diff --git a/sql/sql_window.cc b/sql/sql_window.cc index 668c6a41e08..37095ad78ca 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -2311,11 +2311,6 @@ void add_special_frame_cursors(THD *thd, Cursor_manager *cursor_manager, fc->add_sum_func(item_sum); cursor_manager->add_cursor(fc); break; - case Item_sum::LAST_VALUE_FUNC: - fc= get_frame_cursor(thd, spec, false); - fc->add_sum_func(item_sum); - cursor_manager->add_cursor(fc); - break; case Item_sum::LEAD_FUNC: case Item_sum::LAG_FUNC: { @@ -2355,6 +2350,22 @@ void add_special_frame_cursors(THD *thd, Cursor_manager *cursor_manager, cursor_manager->add_cursor(fc); break; } + case Item_sum::LAST_VALUE_FUNC: + { + Frame_cursor *bottom_bound= get_frame_cursor(thd, spec, false); + Frame_cursor *top_bound= get_frame_cursor(thd, spec, true); + cursor_manager->add_cursor(bottom_bound); + cursor_manager->add_cursor(top_bound); + DBUG_ASSERT(item_sum->fixed); + Item *offset_item= new (thd->mem_root) Item_int(thd, 0); + offset_item->fix_fields(thd, &offset_item); + fc= new Frame_positional_cursor(*bottom_bound, + *top_bound, *bottom_bound, + *offset_item, false); + fc->add_sum_func(item_sum); + cursor_manager->add_cursor(fc); + break; + } case Item_sum::NTH_VALUE_FUNC: { Frame_cursor *bottom_bound= get_frame_cursor(thd, spec, false); -- cgit v1.2.1