summaryrefslogtreecommitdiff
path: root/sql/item_sum.cc
diff options
context:
space:
mode:
authorVicențiu Ciorbaru <vicentiu@mariadb.org>2016-03-14 19:11:03 +0200
committerVicențiu Ciorbaru <vicentiu@mariadb.org>2016-03-14 19:12:27 +0200
commit93f2371020039f943a28e314412ff6605315ff27 (patch)
treef88bfbf9553167dff5ab5aa1d5e5067e23796f19 /sql/item_sum.cc
parente261c14456fdc92844a76688c08e1f4a6cc2c8bb (diff)
downloadmariadb-git-93f2371020039f943a28e314412ff6605315ff27.tar.gz
Implemented a counter within Item_sum_sum
The counter keeps track of the number of items added to the sum function. It is increased when we add a value to the sum function and decreased when it is removed.
Diffstat (limited to 'sql/item_sum.cc')
-rw-r--r--sql/item_sum.cc26
1 files changed, 23 insertions, 3 deletions
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index 3f4853a4e1f..b7490d02233 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -1243,7 +1243,8 @@ Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
:Item_sum_num(thd, item),
Type_handler_hybrid_field_type(item),
- curr_dec_buff(item->curr_dec_buff)
+ curr_dec_buff(item->curr_dec_buff),
+ count(item->count)
{
/* TODO: check if the following assignments are really needed */
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
@@ -1265,6 +1266,7 @@ void Item_sum_sum::clear()
{
DBUG_ENTER("Item_sum_sum::clear");
null_value=1;
+ count= 0;
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
curr_dec_buff= 0;
@@ -1325,6 +1327,7 @@ bool Item_sum_sum::add()
void Item_sum_sum::add_helper(bool perform_removal)
{
DBUG_ENTER("Item_sum_sum::add_helper");
+
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
my_decimal value;
@@ -1332,13 +1335,20 @@ void Item_sum_sum::add_helper(bool perform_removal)
if (!aggr->arg_is_null(true))
{
if (perform_removal)
+ {
+ DBUG_ASSERT(count > 0);
my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
dec_buffs + curr_dec_buff, val);
+ count--;
+ }
else
+ {
+ count++;
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
val, dec_buffs + curr_dec_buff);
+ }
curr_dec_buff^= 1;
- null_value= 0;
+ null_value= (count > 0) ? 0 : 1;
}
}
else
@@ -1348,7 +1358,17 @@ void Item_sum_sum::add_helper(bool perform_removal)
else
sum+= aggr->arg_val_real();
if (!aggr->arg_is_null(true))
- null_value= 0;
+ {
+ if (perform_removal)
+ {
+ DBUG_ASSERT(count > 0);
+ count--;
+ }
+ else
+ count++;
+
+ null_value= (count > 0) ? 0 : 1;
+ }
}
DBUG_VOID_RETURN;
}