Skip to content

Commit

Permalink
MDEV-9215 Detect cmp_type() and result_type() from field_type()
Browse files Browse the repository at this point in the history
Part2: deriving Item_sum_sum from Type_handler_hybrid_field_type,
removing "Item_result Item_sum_sum::hybrid_type".
  • Loading branch information
Alexander Barkov committed Dec 1, 2015
1 parent 607ef78 commit 3e471bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
16 changes: 0 additions & 16 deletions sql/item.cc
Expand Up @@ -5350,22 +5350,6 @@ void Item_empty_string::make_field(THD *thd, Send_field *tmp_field)
}


enum_field_types Item::field_type_by_result_type() const
{
switch (result_type()) {
case STRING_RESULT: return string_field_type();
case INT_RESULT: return MYSQL_TYPE_LONGLONG;
case DECIMAL_RESULT: return MYSQL_TYPE_NEWDECIMAL;
case REAL_RESULT: return MYSQL_TYPE_DOUBLE;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0);
return MYSQL_TYPE_VARCHAR;
}
return MYSQL_TYPE_VARCHAR;
}


/**
Verifies that the input string is well-formed according to its character set.
@param send_error If true, call my_error if string is not well-formed.
Expand Down
1 change: 0 additions & 1 deletion sql/item.h
Expand Up @@ -768,7 +768,6 @@ class Item: public Value_source,
{
return Type_handler::string_type_handler(max_length)->field_type();
}
enum_field_types field_type_by_result_type() const;
virtual enum Type type() const =0;
/*
real_type() is the type of base item. This is same as type() for
Expand Down
51 changes: 26 additions & 25 deletions sql/item_sum.cc
Expand Up @@ -1318,11 +1318,12 @@ Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
check if the following assignments are really needed
*/
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
:Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
:Item_sum_num(thd, item),
Type_handler_hybrid_field_type(item),
curr_dec_buff(item->curr_dec_buff)
{
/* TODO: check if the following assignments are really needed */
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
my_decimal2decimal(item->dec_buffs, dec_buffs);
my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
Expand All @@ -1341,7 +1342,7 @@ void Item_sum_sum::clear()
{
DBUG_ENTER("Item_sum_sum::clear");
null_value=1;
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
curr_dec_buff= 0;
my_decimal_set_zero(dec_buffs);
Expand All @@ -1360,7 +1361,7 @@ void Item_sum_sum::fix_length_and_dec()
switch (args[0]->cast_to_int_type()) {
case REAL_RESULT:
case STRING_RESULT:
hybrid_type= REAL_RESULT;
set_handler_by_field_type(MYSQL_TYPE_DOUBLE);
sum= 0.0;
break;
case INT_RESULT:
Expand All @@ -1373,17 +1374,17 @@ void Item_sum_sum::fix_length_and_dec()
decimals,
unsigned_flag);
curr_dec_buff= 0;
hybrid_type= DECIMAL_RESULT;
set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL);
my_decimal_set_zero(dec_buffs);
break;
}
case ROW_RESULT:
DBUG_ASSERT(0);
}
DBUG_PRINT("info", ("Type: %s (%d, %d)",
(hybrid_type == REAL_RESULT ? "REAL_RESULT" :
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
hybrid_type == INT_RESULT ? "INT_RESULT" :
(result_type() == REAL_RESULT ? "REAL_RESULT" :
result_type() == DECIMAL_RESULT ? "DECIMAL_RESULT" :
result_type() == INT_RESULT ? "INT_RESULT" :
"--ILLEGAL!!!--"),
max_length,
(int)decimals));
Expand All @@ -1394,7 +1395,7 @@ void Item_sum_sum::fix_length_and_dec()
bool Item_sum_sum::add()
{
DBUG_ENTER("Item_sum_sum::add");
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
my_decimal value;
const my_decimal *val= aggr->arg_val_decimal(&value);
Expand All @@ -1421,7 +1422,7 @@ longlong Item_sum_sum::val_int()
DBUG_ASSERT(fixed == 1);
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
longlong result;
my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
Expand All @@ -1437,7 +1438,7 @@ double Item_sum_sum::val_real()
DBUG_ASSERT(fixed == 1);
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
return sum;
}
Expand All @@ -1447,7 +1448,7 @@ String *Item_sum_sum::val_str(String *str)
{
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
return val_string_from_decimal(str);
return val_string_from_real(str);
}
Expand All @@ -1457,7 +1458,7 @@ my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
{
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
return (dec_buffs + curr_dec_buff);
return val_decimal_from_real(val);
}
Expand Down Expand Up @@ -1633,7 +1634,7 @@ void Item_sum_avg::fix_length_and_dec()
Item_sum_sum::fix_length_and_dec();
maybe_null=null_value=1;
prec_increment= current_thd->variables.div_precincrement;
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_avg::result_type() == DECIMAL_RESULT)
{
int precision= args[0]->decimal_precision() + prec_increment;
decimals= MY_MIN(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
Expand Down Expand Up @@ -1672,11 +1673,11 @@ Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
and unpack on access.
*/
field= new (mem_root)
Field_string(((hybrid_type == DECIMAL_RESULT) ?
Field_string(((Item_sum_avg::result_type() == DECIMAL_RESULT) ?
dec_bin_size : sizeof(double)) + sizeof(longlong),
0, name, &my_charset_bin);
}
else if (hybrid_type == DECIMAL_RESULT)
else if (Item_sum_avg::result_type() == DECIMAL_RESULT)
field= Field_new_decimal::create_from_item(mem_root, this);
else
field= new (mem_root) Field_double(max_length, maybe_null, name, decimals,
Expand Down Expand Up @@ -1731,10 +1732,10 @@ my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
}

/*
For non-DECIMAL hybrid_type the division will be done in
For non-DECIMAL result_type() the division will be done in
Item_sum_avg::val_real().
*/
if (hybrid_type != DECIMAL_RESULT)
if (Item_sum_avg::result_type() != DECIMAL_RESULT)
return val_decimal_from_real(val);

sum_dec= dec_buffs + curr_dec_buff;
Expand All @@ -1748,7 +1749,7 @@ String *Item_sum_avg::val_str(String *str)
{
if (aggr)
aggr->endup();
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_avg::result_type() == DECIMAL_RESULT)
return val_string_from_decimal(str);
return val_string_from_real(str);
}
Expand Down Expand Up @@ -2316,7 +2317,7 @@ void Item_sum_hybrid::reset_field()
void Item_sum_sum::reset_field()
{
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!arg_val) // Null
Expand All @@ -2325,7 +2326,7 @@ void Item_sum_sum::reset_field()
}
else
{
DBUG_ASSERT(hybrid_type == REAL_RESULT);
DBUG_ASSERT(result_type() == REAL_RESULT);
double nr= args[0]->val_real(); // Nulls also return 0
float8store(result_field->ptr, nr);
}
Expand All @@ -2352,7 +2353,7 @@ void Item_sum_avg::reset_field()
{
uchar *res=result_field->ptr;
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_avg::result_type() == DECIMAL_RESULT)
{
longlong tmp;
my_decimal value, *arg_dec= args[0]->val_decimal(&value);
Expand Down Expand Up @@ -2406,7 +2407,7 @@ void Item_sum_bit::update_field()
void Item_sum_sum::update_field()
{
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{
my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!args[0]->null_value)
Expand Down Expand Up @@ -2461,7 +2462,7 @@ void Item_sum_avg::update_field()

DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);

if (hybrid_type == DECIMAL_RESULT)
if (Item_sum_avg::result_type() == DECIMAL_RESULT)
{
my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!args[0]->null_value)
Expand Down Expand Up @@ -2500,7 +2501,7 @@ void Item_sum_avg::update_field()
Item *Item_sum_avg::result_item(THD *thd, Field *field)
{
return
hybrid_type == DECIMAL_RESULT ?
Item_sum_avg::result_type() == DECIMAL_RESULT ?
(Item_avg_field*) new (thd->mem_root) Item_avg_field_decimal(thd, this) :
(Item_avg_field*) new (thd->mem_root) Item_avg_field_double(thd, this);
}
Expand Down
13 changes: 7 additions & 6 deletions sql/item_sum.h
Expand Up @@ -721,10 +721,10 @@ class Item_sum_int :public Item_sum_num
};


class Item_sum_sum :public Item_sum_num
class Item_sum_sum :public Item_sum_num,
public Type_handler_hybrid_field_type
{
protected:
Item_result hybrid_type;
double sum;
my_decimal dec_buffs[2];
uint curr_dec_buff;
Expand All @@ -747,11 +747,12 @@ class Item_sum_sum :public Item_sum_num
longlong val_int();
String *val_str(String*str);
my_decimal *val_decimal(my_decimal *);
enum Item_result result_type () const { return hybrid_type; }
enum_field_types field_type() const
{
return field_type_by_result_type();
}
{ return Type_handler_hybrid_field_type::field_type(); }
enum Item_result result_type () const
{ return Type_handler_hybrid_field_type::result_type(); }
enum Item_result cmp_type () const
{ return Type_handler_hybrid_field_type::cmp_type(); }
void reset_field();
void update_field();
void no_rows_in_result() {}
Expand Down

0 comments on commit 3e471bf

Please sign in to comment.