Commit da7c5e3b authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-9895: Assertion `n_rows > 0' failed in Frame_cursor* get_frame_cursor

n=0 in "ROWS 0 PRECEDING" is valid, add handling for it:
- Adjust the assert
- Bottom bound of 'ROW 0 PRECEDING' is actually looking at the current
  row, that is, it needs to process partition's first row directly in
  Frame_n_rows_preceding::next_partition().
- Added testcases
parent 2905b2fe
......@@ -1841,3 +1841,64 @@ insert into t1 values (1),(2);
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
ERROR 42000: This version of MariaDB doesn't yet support 'This aggregate as window function'
drop table t1;
#
# Check the 0 in ROWS 0 PRECEDING
#
create table t1 (
part_id int,
pk int,
a int
);
insert into t1 values (1, 1, 1);
insert into t1 values (1, 2, 2);
insert into t1 values (1, 3, 4);
insert into t1 values (1, 4, 8);
select
pk, a,
sum(a) over (order by pk rows between 0 preceding and current row)
from t1;
pk a sum(a) over (order by pk rows between 0 preceding and current row)
1 1 1
2 2 2
3 4 4
4 8 8
select
pk, a,
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
from t1;
pk a sum(a) over (order by pk rows between 1 preceding and 0 preceding)
1 1 1
2 2 3
3 4 6
4 8 12
insert into t1 values (200, 1, 1);
insert into t1 values (200, 2, 2);
insert into t1 values (200, 3, 4);
insert into t1 values (200, 4, 8);
select
part_id, pk, a,
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
from t1;
part_id pk a sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
1 1 1 1
1 2 2 2
1 3 4 4
1 4 8 8
200 1 1 1
200 2 2 2
200 3 4 4
200 4 8 8
select
part_id, pk, a,
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
from t1;
part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
1 1 1 1
1 2 2 3
1 3 4 6
1 4 8 12
200 1 1 1
200 2 2 3
200 3 4 6
200 4 8 12
drop table t1;
......@@ -1124,3 +1124,44 @@ insert into t1 values (1),(2);
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
drop table t1;
--echo #
--echo # Check the 0 in ROWS 0 PRECEDING
--echo #
create table t1 (
part_id int,
pk int,
a int
);
insert into t1 values (1, 1, 1);
insert into t1 values (1, 2, 2);
insert into t1 values (1, 3, 4);
insert into t1 values (1, 4, 8);
select
pk, a,
sum(a) over (order by pk rows between 0 preceding and current row)
from t1;
select
pk, a,
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
from t1;
insert into t1 values (200, 1, 1);
insert into t1 values (200, 2, 2);
insert into t1 values (200, 3, 4);
insert into t1 values (200, 4, 8);
select
part_id, pk, a,
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
from t1;
select
part_id, pk, a,
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
from t1;
drop table t1;
......@@ -1279,6 +1279,14 @@ class Frame_n_rows_preceding : public Frame_cursor
- top bound should remove row (#n-3) from the window function.
*/
n_rows_to_skip= n_rows + (is_top_bound? 1:0) - 1;
/* Bottom bound "ROWS 0 PRECEDING" is a special case: */
if (n_rows_to_skip == -1)
{
cursor.get_next();
item->add();
n_rows_to_skip= 0;
}
}
void next_row(Item_sum* item)
......@@ -1496,7 +1504,7 @@ Frame_cursor *get_frame_cursor(Window_frame *frame, bool is_top_bound)
longlong n_rows= bound->offset->val_int();
/* These should be handled in the parser */
DBUG_ASSERT(!bound->offset->null_value);
DBUG_ASSERT(n_rows > 0);
DBUG_ASSERT(n_rows >= 0);
if (is_preceding)
return new Frame_n_rows_preceding(is_top_bound, n_rows);
else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment