Commit 93f23710 authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru

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.
parent e261c144
......@@ -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;
}
......
......@@ -777,6 +777,7 @@ public:
private:
void add_helper(bool perform_removal);
ulonglong count;
};
......
......@@ -1261,6 +1261,13 @@ bool compute_window_func_with_frames(Item_window_func *item_win,
*/
tbl->file->ha_rnd_pos(tbl->record[0], rowid_buf);
store_record(tbl,record[1]);
// TODO-cvicentiu
// Save in field does not make use of the null value for the sum function.
// If we do however set the null value to be the same as the one that
// the current sum function has, via say
// item_win->null_value = sum_func->null_value; we get an assertion failure
// during the sending of data within Item::send, in the case of DECIMAL_RESULT.
// How do we force saving of NULL values in the table?
item_win->save_in_field(item_win->result_field, true);
err= tbl->file->ha_update_row(tbl->record[1], tbl->record[0]);
if (err && err != HA_ERR_RECORD_IS_THE_SAME)
......
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