Commit be2b833c authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru

It is now possible to remove values multiple times from window functions

This can happen with degenerate frame definitions that are always empty.
parent a37f24b9
...@@ -1189,6 +1189,44 @@ pk c cnt ...@@ -1189,6 +1189,44 @@ pk c cnt
10 2 0 10 2 0
select select
pk, c, pk, c,
sum(c) over (partition by c
order by pk
rows between 1 preceding
and 2 preceding)
as sum
from t1;
pk c sum
1 1 NULL
2 1 NULL
3 1 NULL
4 1 NULL
5 2 NULL
6 2 NULL
7 2 NULL
8 2 NULL
9 2 NULL
10 2 NULL
select
pk, c,
sum(c) over (partition by c
order by pk
rows between 2 following
and 1 following)
as sum
from t1;
pk c sum
1 1 NULL
2 1 NULL
3 1 NULL
4 1 NULL
5 2 NULL
6 2 NULL
7 2 NULL
8 2 NULL
9 2 NULL
10 2 NULL
select
pk, c,
count(*) over (partition by c count(*) over (partition by c
order by pk order by pk
range between 1 preceding range between 1 preceding
...@@ -1788,15 +1826,12 @@ rank() over (order by b) ...@@ -1788,15 +1826,12 @@ rank() over (order by b)
0 0
0 0
drop table t1; drop table t1;
#
# MDEV-9894: Assertion `0' failed in Window_func_runner::setup
# return ER_NOT_SUPPORTED_YET for aggregates that are not yet supported
# as window functions.
#
create table t1 (i int); create table t1 (i int);
insert into t1 values (1),(2); insert into t1 values (1),(2);
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1; SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
ERROR 42000: This version of MariaDB doesn't yet support 'This aggregate as window function' MAX(i) OVER (PARTITION BY (i))
1
2
drop table t1; drop table t1;
# #
# Check the 0 in ROWS 0 PRECEDING # Check the 0 in ROWS 0 PRECEDING
......
...@@ -745,6 +745,24 @@ select ...@@ -745,6 +745,24 @@ select
as cnt as cnt
from t1; from t1;
select
pk, c,
sum(c) over (partition by c
order by pk
rows between 1 preceding
and 2 preceding)
as sum
from t1;
select
pk, c,
sum(c) over (partition by c
order by pk
rows between 2 following
and 1 following)
as sum
from t1;
select select
pk, c, pk, c,
...@@ -1089,14 +1107,8 @@ from ...@@ -1089,14 +1107,8 @@ from
drop table t1; drop table t1;
--echo #
--echo # MDEV-9894: Assertion `0' failed in Window_func_runner::setup
--echo # return ER_NOT_SUPPORTED_YET for aggregates that are not yet supported
--echo # as window functions.
--echo #
create table t1 (i int); create table t1 (i int);
insert into t1 values (1),(2); insert into t1 values (1),(2);
--error ER_NOT_SUPPORTED_YET
SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1; SELECT MAX(i) OVER (PARTITION BY (i)) FROM t1;
drop table t1; drop table t1;
......
...@@ -1352,11 +1352,15 @@ void Item_sum_sum::add_helper(bool perform_removal) ...@@ -1352,11 +1352,15 @@ void Item_sum_sum::add_helper(bool perform_removal)
{ {
if (perform_removal) if (perform_removal)
{ {
DBUG_ASSERT(count > 0); if (count > 0)
{
my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1), my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
dec_buffs + curr_dec_buff, val); dec_buffs + curr_dec_buff, val);
count--; count--;
} }
else
DBUG_VOID_RETURN;
}
else else
{ {
count++; count++;
...@@ -1369,7 +1373,7 @@ void Item_sum_sum::add_helper(bool perform_removal) ...@@ -1369,7 +1373,7 @@ void Item_sum_sum::add_helper(bool perform_removal)
} }
else else
{ {
if (perform_removal) if (perform_removal && count > 0)
sum-= aggr->arg_val_real(); sum-= aggr->arg_val_real();
else else
sum+= aggr->arg_val_real(); sum+= aggr->arg_val_real();
...@@ -1377,9 +1381,11 @@ void Item_sum_sum::add_helper(bool perform_removal) ...@@ -1377,9 +1381,11 @@ void Item_sum_sum::add_helper(bool perform_removal)
{ {
if (perform_removal) if (perform_removal)
{ {
DBUG_ASSERT(count > 0); if (count > 0)
{
count--; count--;
} }
}
else else
count++; count++;
...@@ -1598,6 +1604,7 @@ void Item_sum_count::remove() ...@@ -1598,6 +1604,7 @@ void Item_sum_count::remove()
DBUG_ASSERT(aggr->Aggrtype() == Aggregator::SIMPLE_AGGREGATOR); DBUG_ASSERT(aggr->Aggrtype() == Aggregator::SIMPLE_AGGREGATOR);
if (aggr->arg_is_null(false)) if (aggr->arg_is_null(false))
return; return;
if (count > 0)
count--; count--;
} }
...@@ -1702,7 +1709,7 @@ void Item_sum_avg::remove() ...@@ -1702,7 +1709,7 @@ void Item_sum_avg::remove()
Item_sum_sum::remove(); Item_sum_sum::remove();
if (!aggr->arg_is_null(true)) if (!aggr->arg_is_null(true))
{ {
DBUG_ASSERT(count > 0); if (count > 0)
count--; count--;
} }
} }
...@@ -2190,6 +2197,9 @@ bool Item_sum_bit::clear_as_window() ...@@ -2190,6 +2197,9 @@ bool Item_sum_bit::clear_as_window()
bool Item_sum_bit::remove_as_window(ulonglong value) bool Item_sum_bit::remove_as_window(ulonglong value)
{ {
DBUG_ASSERT(as_window_function); DBUG_ASSERT(as_window_function);
if (num_values_added == 0)
return 0; // Nothing to remove.
for (int i= 0; i < NUM_BIT_COUNTERS; i++) for (int i= 0; i < NUM_BIT_COUNTERS; i++)
{ {
if (!bit_counters[i]) if (!bit_counters[i])
...@@ -2200,7 +2210,7 @@ bool Item_sum_bit::remove_as_window(ulonglong value) ...@@ -2200,7 +2210,7 @@ bool Item_sum_bit::remove_as_window(ulonglong value)
} }
bit_counters[i]-= (value & (1 << i)) ? 1 : 0; bit_counters[i]-= (value & (1 << i)) ? 1 : 0;
} }
DBUG_ASSERT(num_values_added > 0);
// Prevent overflow; // Prevent overflow;
num_values_added = std::min(num_values_added, num_values_added - 1); num_values_added = std::min(num_values_added, num_values_added - 1);
set_bits_from_counters(); set_bits_from_counters();
......
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