Skip to content

Commit

Permalink
MDEV-9395 Add Type_handler::Item_decimal_scale() and Item_divisor_pre…
Browse files Browse the repository at this point in the history
…cision_increment()
  • Loading branch information
Alexander Barkov committed Apr 29, 2017
1 parent ea18b11 commit 7a19c59
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
21 changes: 0 additions & 21 deletions sql/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,27 +525,6 @@ inline bool is_temporal_type(enum_field_types type)
}


/**
Tests if field type is temporal and has time part,
i.e. represents TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal type with time part.
@retval false If field type is not temporal type with time part.
*/
inline bool is_temporal_type_with_time(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return true;
default:
return false;
}
}

enum enum_vcol_info_type
{
VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED,
Expand Down
10 changes: 2 additions & 8 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,7 @@ class Item: public Value_source,
*/
uint decimal_scale() const
{
return decimals < NOT_FIXED_DEC ? decimals :
is_temporal_type_with_time(field_type()) ?
TIME_SECOND_PART_DIGITS :
MY_MIN(max_length, DECIMAL_MAX_SCALE);
return type_handler()->Item_decimal_scale(this);
}
/*
Returns how many digits a divisor adds into a division result.
Expand All @@ -1186,10 +1183,7 @@ class Item: public Value_source,
*/
uint divisor_precision_increment() const
{
return decimals < NOT_FIXED_DEC ? decimals :
is_temporal_type_with_time(field_type()) ?
TIME_SECOND_PART_DIGITS :
decimals;
return type_handler()->Item_divisor_precision_increment(this);
}
/**
TIME or DATETIME precision of the item: 0..6
Expand Down
30 changes: 30 additions & 0 deletions sql/sql_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3696,6 +3696,36 @@ uint Type_handler_string_result::Item_temporal_precision(Item *item,

/***************************************************************************/

uint Type_handler::Item_decimal_scale(const Item *item) const
{
return item->decimals < NOT_FIXED_DEC ?
item->decimals :
MY_MIN(item->max_length, DECIMAL_MAX_SCALE);
}

uint Type_handler_temporal_result::
Item_decimal_scale_with_seconds(const Item *item) const
{
return item->decimals < NOT_FIXED_DEC ?
item->decimals :
TIME_SECOND_PART_DIGITS;
}

uint Type_handler::Item_divisor_precision_increment(const Item *item) const
{
return item->decimals;
}

uint Type_handler_temporal_result::
Item_divisor_precision_increment_with_seconds(const Item *item) const
{
return item->decimals < NOT_FIXED_DEC ?
item->decimals :
TIME_SECOND_PART_DIGITS;
}

/***************************************************************************/

bool Type_handler_real_result::
subquery_type_allows_materialization(const Item *inner,
const Item *outer) const
Expand Down
33 changes: 33 additions & 0 deletions sql/sql_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,12 @@ class Type_handler
}
virtual uint Item_time_precision(Item *item) const;
virtual uint Item_datetime_precision(Item *item) const;
virtual uint Item_decimal_scale(const Item *item) const;
/*
Returns how many digits a divisor adds into a division result.
See Item::divisor_precision_increment() in item.h for more comments.
*/
virtual uint Item_divisor_precision_increment(const Item *) const;
/**
Makes a temporary table Field to handle numeric aggregate functions,
e.g. SUM(DISTINCT expr), AVG(DISTINCT expr), etc.
Expand Down Expand Up @@ -1110,6 +1116,9 @@ class Type_handler_int_result: public Type_handler_numeric

class Type_handler_temporal_result: public Type_handler
{
protected:
uint Item_decimal_scale_with_seconds(const Item *item) const;
uint Item_divisor_precision_increment_with_seconds(const Item *) const;
public:
Item_result result_type() const { return STRING_RESULT; }
Item_result cmp_type() const { return TIME_RESULT; }
Expand Down Expand Up @@ -1441,6 +1450,14 @@ class Type_handler_time_common: public Type_handler_temporal_result
{
return MYSQL_TIMESTAMP_TIME;
}
uint Item_decimal_scale(const Item *item) const
{
return Item_decimal_scale_with_seconds(item);
}
uint Item_divisor_precision_increment(const Item *item) const
{
return Item_divisor_precision_increment_with_seconds(item);
}
const Type_handler *type_handler_for_comparison() const;
int Item_save_in_field(Item *item, Field *field, bool no_conversions) const;
String *print_item_value(THD *thd, Item *item, String *str) const;
Expand Down Expand Up @@ -1543,6 +1560,14 @@ class Type_handler_datetime_common: public Type_handler_temporal_with_date
{
return MYSQL_TIMESTAMP_DATETIME;
}
uint Item_decimal_scale(const Item *item) const
{
return Item_decimal_scale_with_seconds(item);
}
uint Item_divisor_precision_increment(const Item *item) const
{
return Item_divisor_precision_increment_with_seconds(item);
}
String *print_item_value(THD *thd, Item *item, String *str) const;
bool Item_hybrid_func_fix_attributes(THD *thd, Item_hybrid_func *func,
Item **items, uint nitems) const;
Expand Down Expand Up @@ -1587,6 +1612,14 @@ class Type_handler_timestamp_common: public Type_handler_temporal_with_date
{
return MYSQL_TIMESTAMP_DATETIME;
}
uint Item_decimal_scale(const Item *item) const
{
return Item_decimal_scale_with_seconds(item);
}
uint Item_divisor_precision_increment(const Item *item) const
{
return Item_divisor_precision_increment_with_seconds(item);
}
String *print_item_value(THD *thd, Item *item, String *str) const;
bool Item_hybrid_func_fix_attributes(THD *thd, Item_hybrid_func *func,
Item **items, uint nitems) const;
Expand Down

0 comments on commit 7a19c59

Please sign in to comment.