Skip to content

Commit 93f2371

Browse files
committed
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.
1 parent e261c14 commit 93f2371

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

sql/item_sum.cc

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,8 @@ Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
12431243
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
12441244
:Item_sum_num(thd, item),
12451245
Type_handler_hybrid_field_type(item),
1246-
curr_dec_buff(item->curr_dec_buff)
1246+
curr_dec_buff(item->curr_dec_buff),
1247+
count(item->count)
12471248
{
12481249
/* TODO: check if the following assignments are really needed */
12491250
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
@@ -1265,6 +1266,7 @@ void Item_sum_sum::clear()
12651266
{
12661267
DBUG_ENTER("Item_sum_sum::clear");
12671268
null_value=1;
1269+
count= 0;
12681270
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
12691271
{
12701272
curr_dec_buff= 0;
@@ -1325,20 +1327,28 @@ bool Item_sum_sum::add()
13251327
void Item_sum_sum::add_helper(bool perform_removal)
13261328
{
13271329
DBUG_ENTER("Item_sum_sum::add_helper");
1330+
13281331
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
13291332
{
13301333
my_decimal value;
13311334
const my_decimal *val= aggr->arg_val_decimal(&value);
13321335
if (!aggr->arg_is_null(true))
13331336
{
13341337
if (perform_removal)
1338+
{
1339+
DBUG_ASSERT(count > 0);
13351340
my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
13361341
dec_buffs + curr_dec_buff, val);
1342+
count--;
1343+
}
13371344
else
1345+
{
1346+
count++;
13381347
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
13391348
val, dec_buffs + curr_dec_buff);
1349+
}
13401350
curr_dec_buff^= 1;
1341-
null_value= 0;
1351+
null_value= (count > 0) ? 0 : 1;
13421352
}
13431353
}
13441354
else
@@ -1348,7 +1358,17 @@ void Item_sum_sum::add_helper(bool perform_removal)
13481358
else
13491359
sum+= aggr->arg_val_real();
13501360
if (!aggr->arg_is_null(true))
1351-
null_value= 0;
1361+
{
1362+
if (perform_removal)
1363+
{
1364+
DBUG_ASSERT(count > 0);
1365+
count--;
1366+
}
1367+
else
1368+
count++;
1369+
1370+
null_value= (count > 0) ? 0 : 1;
1371+
}
13521372
}
13531373
DBUG_VOID_RETURN;
13541374
}

sql/item_sum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ class Item_sum_sum :public Item_sum_num,
777777

778778
private:
779779
void add_helper(bool perform_removal);
780+
ulonglong count;
780781
};
781782

782783

sql/sql_window.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,13 @@ bool compute_window_func_with_frames(Item_window_func *item_win,
12611261
*/
12621262
tbl->file->ha_rnd_pos(tbl->record[0], rowid_buf);
12631263
store_record(tbl,record[1]);
1264+
// TODO-cvicentiu
1265+
// Save in field does not make use of the null value for the sum function.
1266+
// If we do however set the null value to be the same as the one that
1267+
// the current sum function has, via say
1268+
// item_win->null_value = sum_func->null_value; we get an assertion failure
1269+
// during the sending of data within Item::send, in the case of DECIMAL_RESULT.
1270+
// How do we force saving of NULL values in the table?
12641271
item_win->save_in_field(item_win->result_field, true);
12651272
err= tbl->file->ha_update_row(tbl->record[1], tbl->record[0]);
12661273
if (err && err != HA_ERR_RECORD_IS_THE_SAME)

0 commit comments

Comments
 (0)