From 0e9fb982f1f9b49b2e723bb5fdf5e340088521f1 Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Thu, 10 Mar 2016 17:46:47 +0300 Subject: [PATCH] MDEV-9695: Wrong window frame when using RANGE BETWEEN N FOLLOWING AND PRECEDING Part#1: Frame_n_rows::next_partition() should not assume that the current table->record[0] points to the first row in the partition. Since cursor supports move_to() operation, we dont need this. --- mysql-test/r/win.result | 26 ++++++++++++++++++++++++++ mysql-test/t/win.test | 19 +++++++++++++++++++ sql/sql_window.cc | 14 +++----------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index 3e3292589b4..72b4f6da3dc 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -658,3 +658,29 @@ part_id pk val CNT 5678 207 3 9 5678 208 3 9 drop table t4; +# +# MDEV-9695: Wrong window frame when using RANGE BETWEEN N FOLLOWING AND PRECEDING +# +create table t1 (pk int, a int, b int); +insert into t1 values +( 1 , 0, 1), +( 2 , 0, 2), +( 3 , 1, 4), +( 4 , 1, 8), +( 5 , 2, 32), +( 6 , 2, 64), +( 7 , 2, 128), +( 8 , 2, 16); +select pk, a, b, +bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or +from t1; +pk a b bit_or +1 0 1 3 +2 0 2 3 +3 1 4 12 +4 1 8 12 +5 2 32 96 +6 2 64 224 +7 2 128 208 +8 2 16 144 +drop table t1; diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 2eb6829539f..b56e43ba31c 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -422,3 +422,22 @@ from t4; drop table t4; +--echo # +--echo # MDEV-9695: Wrong window frame when using RANGE BETWEEN N FOLLOWING AND PRECEDING +--echo # +create table t1 (pk int, a int, b int); +insert into t1 values +( 1 , 0, 1), +( 2 , 0, 2), +( 3 , 1, 4), +( 4 , 1, 8), +( 5 , 2, 32), +( 6 , 2, 64), +( 7 , 2, 128), +( 8 , 2, 16); + +select pk, a, b, +bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or +from t1; + +drop table t1; diff --git a/sql/sql_window.cc b/sql/sql_window.cc index 3ccf35a2ed4..a5291079fd4 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -650,16 +650,8 @@ public: { if (rownum != 0) { - /* - The cursor in "ROWS n PRECEDING" lags behind by n_rows rows. - Catch up. - TODO: change this to be a jump forward. - */ - while (!(cursor_eof= (0 != cursor.get_next()))) - { - if (bound_tracker.check_if_next_group()) - break; - } + /* The cursor in "ROWS n PRECEDING" lags behind by n_rows rows. */ + cursor.move_to(rownum); } n_rows_to_skip= n_rows - (is_top_bound? 0:1); } @@ -717,7 +709,7 @@ private: { if (!(cursor_eof= (0 != cursor.get_next()))) { - bool new_group= bound_tracker.check_if_next_group(); + bool new_group= is_preceding? false: bound_tracker.check_if_next_group(); if (at_partition_start || !new_group) { if (is_top_bound) // this is frame start endpoint