Commit 3e471bf3 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-9215 Detect cmp_type() and result_type() from field_type()

Part2: deriving Item_sum_sum from Type_handler_hybrid_field_type,
removing "Item_result Item_sum_sum::hybrid_type".
parent 607ef786
...@@ -5350,22 +5350,6 @@ void Item_empty_string::make_field(THD *thd, Send_field *tmp_field) ...@@ -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. 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. @param send_error If true, call my_error if string is not well-formed.
......
...@@ -768,7 +768,6 @@ class Item: public Value_source, ...@@ -768,7 +768,6 @@ class Item: public Value_source,
{ {
return Type_handler::string_type_handler(max_length)->field_type(); 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; virtual enum Type type() const =0;
/* /*
real_type() is the type of base item. This is same as type() for real_type() is the type of base item. This is same as type() for
......
...@@ -1318,11 +1318,12 @@ Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table, ...@@ -1318,11 +1318,12 @@ Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
check if the following assignments are really needed check if the following assignments are really needed
*/ */
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 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) curr_dec_buff(item->curr_dec_buff)
{ {
/* TODO: check if the following assignments are really needed */ /* 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, dec_buffs);
my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1); my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
...@@ -1341,7 +1342,7 @@ void Item_sum_sum::clear() ...@@ -1341,7 +1342,7 @@ void Item_sum_sum::clear()
{ {
DBUG_ENTER("Item_sum_sum::clear"); DBUG_ENTER("Item_sum_sum::clear");
null_value=1; null_value=1;
if (hybrid_type == DECIMAL_RESULT) if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{ {
curr_dec_buff= 0; curr_dec_buff= 0;
my_decimal_set_zero(dec_buffs); my_decimal_set_zero(dec_buffs);
...@@ -1360,7 +1361,7 @@ void Item_sum_sum::fix_length_and_dec() ...@@ -1360,7 +1361,7 @@ void Item_sum_sum::fix_length_and_dec()
switch (args[0]->cast_to_int_type()) { switch (args[0]->cast_to_int_type()) {
case REAL_RESULT: case REAL_RESULT:
case STRING_RESULT: case STRING_RESULT:
hybrid_type= REAL_RESULT; set_handler_by_field_type(MYSQL_TYPE_DOUBLE);
sum= 0.0; sum= 0.0;
break; break;
case INT_RESULT: case INT_RESULT:
...@@ -1373,7 +1374,7 @@ void Item_sum_sum::fix_length_and_dec() ...@@ -1373,7 +1374,7 @@ void Item_sum_sum::fix_length_and_dec()
decimals, decimals,
unsigned_flag); unsigned_flag);
curr_dec_buff= 0; curr_dec_buff= 0;
hybrid_type= DECIMAL_RESULT; set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL);
my_decimal_set_zero(dec_buffs); my_decimal_set_zero(dec_buffs);
break; break;
} }
...@@ -1381,9 +1382,9 @@ void Item_sum_sum::fix_length_and_dec() ...@@ -1381,9 +1382,9 @@ void Item_sum_sum::fix_length_and_dec()
DBUG_ASSERT(0); DBUG_ASSERT(0);
} }
DBUG_PRINT("info", ("Type: %s (%d, %d)", DBUG_PRINT("info", ("Type: %s (%d, %d)",
(hybrid_type == REAL_RESULT ? "REAL_RESULT" : (result_type() == REAL_RESULT ? "REAL_RESULT" :
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" : result_type() == DECIMAL_RESULT ? "DECIMAL_RESULT" :
hybrid_type == INT_RESULT ? "INT_RESULT" : result_type() == INT_RESULT ? "INT_RESULT" :
"--ILLEGAL!!!--"), "--ILLEGAL!!!--"),
max_length, max_length,
(int)decimals)); (int)decimals));
...@@ -1394,7 +1395,7 @@ void Item_sum_sum::fix_length_and_dec() ...@@ -1394,7 +1395,7 @@ void Item_sum_sum::fix_length_and_dec()
bool Item_sum_sum::add() bool Item_sum_sum::add()
{ {
DBUG_ENTER("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; my_decimal value;
const my_decimal *val= aggr->arg_val_decimal(&value); const my_decimal *val= aggr->arg_val_decimal(&value);
...@@ -1421,7 +1422,7 @@ longlong Item_sum_sum::val_int() ...@@ -1421,7 +1422,7 @@ longlong Item_sum_sum::val_int()
DBUG_ASSERT(fixed == 1); DBUG_ASSERT(fixed == 1);
if (aggr) if (aggr)
aggr->endup(); aggr->endup();
if (hybrid_type == DECIMAL_RESULT) if (Item_sum_sum::result_type() == DECIMAL_RESULT)
{ {
longlong result; longlong result;
my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag, my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
...@@ -1437,7 +1438,7 @@ double Item_sum_sum::val_real() ...@@ -1437,7 +1438,7 @@ double Item_sum_sum::val_real()
DBUG_ASSERT(fixed == 1); DBUG_ASSERT(fixed == 1);
if (aggr) if (aggr)
aggr->endup(); 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); my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
return sum; return sum;
} }
...@@ -1447,7 +1448,7 @@ String *Item_sum_sum::val_str(String *str) ...@@ -1447,7 +1448,7 @@ String *Item_sum_sum::val_str(String *str)
{ {
if (aggr) if (aggr)
aggr->endup(); 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_decimal(str);
return val_string_from_real(str); return val_string_from_real(str);
} }
...@@ -1457,7 +1458,7 @@ my_decimal *Item_sum_sum::val_decimal(my_decimal *val) ...@@ -1457,7 +1458,7 @@ my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
{ {
if (aggr) if (aggr)
aggr->endup(); aggr->endup();
if (hybrid_type == DECIMAL_RESULT) if (Item_sum_sum::result_type() == DECIMAL_RESULT)
return (dec_buffs + curr_dec_buff); return (dec_buffs + curr_dec_buff);
return val_decimal_from_real(val); return val_decimal_from_real(val);
} }
...@@ -1633,7 +1634,7 @@ void Item_sum_avg::fix_length_and_dec() ...@@ -1633,7 +1634,7 @@ void Item_sum_avg::fix_length_and_dec()
Item_sum_sum::fix_length_and_dec(); Item_sum_sum::fix_length_and_dec();
maybe_null=null_value=1; maybe_null=null_value=1;
prec_increment= current_thd->variables.div_precincrement; 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; int precision= args[0]->decimal_precision() + prec_increment;
decimals= MY_MIN(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE); decimals= MY_MIN(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
...@@ -1672,11 +1673,11 @@ Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table, ...@@ -1672,11 +1673,11 @@ Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
and unpack on access. and unpack on access.
*/ */
field= new (mem_root) 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), dec_bin_size : sizeof(double)) + sizeof(longlong),
0, name, &my_charset_bin); 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); field= Field_new_decimal::create_from_item(mem_root, this);
else else
field= new (mem_root) Field_double(max_length, maybe_null, name, decimals, field= new (mem_root) Field_double(max_length, maybe_null, name, decimals,
...@@ -1731,10 +1732,10 @@ my_decimal *Item_sum_avg::val_decimal(my_decimal *val) ...@@ -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(). Item_sum_avg::val_real().
*/ */
if (hybrid_type != DECIMAL_RESULT) if (Item_sum_avg::result_type() != DECIMAL_RESULT)
return val_decimal_from_real(val); return val_decimal_from_real(val);
sum_dec= dec_buffs + curr_dec_buff; sum_dec= dec_buffs + curr_dec_buff;
...@@ -1748,7 +1749,7 @@ String *Item_sum_avg::val_str(String *str) ...@@ -1748,7 +1749,7 @@ String *Item_sum_avg::val_str(String *str)
{ {
if (aggr) if (aggr)
aggr->endup(); 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_decimal(str);
return val_string_from_real(str); return val_string_from_real(str);
} }
...@@ -2316,7 +2317,7 @@ void Item_sum_hybrid::reset_field() ...@@ -2316,7 +2317,7 @@ void Item_sum_hybrid::reset_field()
void Item_sum_sum::reset_field() void Item_sum_sum::reset_field()
{ {
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR); 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); my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!arg_val) // Null if (!arg_val) // Null
...@@ -2325,7 +2326,7 @@ void Item_sum_sum::reset_field() ...@@ -2325,7 +2326,7 @@ void Item_sum_sum::reset_field()
} }
else else
{ {
DBUG_ASSERT(hybrid_type == REAL_RESULT); DBUG_ASSERT(result_type() == REAL_RESULT);
double nr= args[0]->val_real(); // Nulls also return 0 double nr= args[0]->val_real(); // Nulls also return 0
float8store(result_field->ptr, nr); float8store(result_field->ptr, nr);
} }
...@@ -2352,7 +2353,7 @@ void Item_sum_avg::reset_field() ...@@ -2352,7 +2353,7 @@ void Item_sum_avg::reset_field()
{ {
uchar *res=result_field->ptr; uchar *res=result_field->ptr;
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR); DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
if (hybrid_type == DECIMAL_RESULT) if (Item_sum_avg::result_type() == DECIMAL_RESULT)
{ {
longlong tmp; longlong tmp;
my_decimal value, *arg_dec= args[0]->val_decimal(&value); my_decimal value, *arg_dec= args[0]->val_decimal(&value);
...@@ -2406,7 +2407,7 @@ void Item_sum_bit::update_field() ...@@ -2406,7 +2407,7 @@ void Item_sum_bit::update_field()
void Item_sum_sum::update_field() void Item_sum_sum::update_field()
{ {
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR); 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); my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!args[0]->null_value) if (!args[0]->null_value)
...@@ -2461,7 +2462,7 @@ void Item_sum_avg::update_field() ...@@ -2461,7 +2462,7 @@ void Item_sum_avg::update_field()
DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR); 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); my_decimal value, *arg_val= args[0]->val_decimal(&value);
if (!args[0]->null_value) if (!args[0]->null_value)
...@@ -2500,7 +2501,7 @@ void Item_sum_avg::update_field() ...@@ -2500,7 +2501,7 @@ void Item_sum_avg::update_field()
Item *Item_sum_avg::result_item(THD *thd, Field *field) Item *Item_sum_avg::result_item(THD *thd, Field *field)
{ {
return 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_decimal(thd, this) :
(Item_avg_field*) new (thd->mem_root) Item_avg_field_double(thd, this); (Item_avg_field*) new (thd->mem_root) Item_avg_field_double(thd, this);
} }
......
...@@ -721,10 +721,10 @@ class Item_sum_int :public Item_sum_num ...@@ -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: protected:
Item_result hybrid_type;
double sum; double sum;
my_decimal dec_buffs[2]; my_decimal dec_buffs[2];
uint curr_dec_buff; uint curr_dec_buff;
...@@ -747,11 +747,12 @@ class Item_sum_sum :public Item_sum_num ...@@ -747,11 +747,12 @@ class Item_sum_sum :public Item_sum_num
longlong val_int(); longlong val_int();
String *val_str(String*str); String *val_str(String*str);
my_decimal *val_decimal(my_decimal *); my_decimal *val_decimal(my_decimal *);
enum Item_result result_type () const { return hybrid_type; }
enum_field_types field_type() const enum_field_types field_type() const
{ { return Type_handler_hybrid_field_type::field_type(); }
return field_type_by_result_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 reset_field();
void update_field(); void update_field();
void no_rows_in_result() {} void no_rows_in_result() {}
......
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