Skip to content

Commit cd828fb

Browse files
author
Alexander Barkov
committed
MDEV-9215 Detect cmp_type() and result_type() from field_type()
Part4: Deriving Item_temporal_hybrid_func from Type_handler_hybrid_field_type
1 parent 47a8c6c commit cd828fb

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

sql/item_timefunc.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,9 +1558,9 @@ String *Item_temporal_hybrid_func::val_str_ascii(String *str)
15581558
return (String *) 0;
15591559

15601560
/* Check that the returned timestamp type matches to the function type */
1561-
DBUG_ASSERT(cached_field_type == MYSQL_TYPE_STRING ||
1561+
DBUG_ASSERT(field_type() == MYSQL_TYPE_STRING ||
15621562
ltime.time_type == MYSQL_TIMESTAMP_NONE ||
1563-
mysql_type_to_time_type(cached_field_type) == ltime.time_type);
1563+
mysql_type_to_time_type(field_type()) == ltime.time_type);
15641564
return str;
15651565
}
15661566

@@ -2081,7 +2081,7 @@ void Item_date_add_interval::fix_length_and_dec()
20812081
(This is because you can't know if the string contains a DATE,
20822082
MYSQL_TIME or DATETIME argument)
20832083
*/
2084-
cached_field_type= MYSQL_TYPE_STRING;
2084+
set_handler_by_field_type(MYSQL_TYPE_STRING);
20852085
arg0_field_type= args[0]->field_type();
20862086
uint interval_dec= 0;
20872087
if (int_type == INTERVAL_MICROSECOND ||
@@ -2095,25 +2095,25 @@ void Item_date_add_interval::fix_length_and_dec()
20952095
arg0_field_type == MYSQL_TYPE_TIMESTAMP)
20962096
{
20972097
decimals= MY_MAX(args[0]->temporal_precision(MYSQL_TYPE_DATETIME), interval_dec);
2098-
cached_field_type= MYSQL_TYPE_DATETIME;
2098+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
20992099
}
21002100
else if (arg0_field_type == MYSQL_TYPE_DATE)
21012101
{
21022102
if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
2103-
cached_field_type= arg0_field_type;
2103+
set_handler_by_field_type(arg0_field_type);
21042104
else
21052105
{
21062106
decimals= interval_dec;
2107-
cached_field_type= MYSQL_TYPE_DATETIME;
2107+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
21082108
}
21092109
}
21102110
else if (arg0_field_type == MYSQL_TYPE_TIME)
21112111
{
21122112
decimals= MY_MAX(args[0]->temporal_precision(MYSQL_TYPE_TIME), interval_dec);
21132113
if (int_type >= INTERVAL_DAY && int_type != INTERVAL_YEAR_MONTH)
2114-
cached_field_type= arg0_field_type;
2114+
set_handler_by_field_type(arg0_field_type);
21152115
else
2116-
cached_field_type= MYSQL_TYPE_DATETIME;
2116+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
21172117
}
21182118
else
21192119
decimals= MY_MAX(args[0]->temporal_precision(MYSQL_TYPE_DATETIME), interval_dec);
@@ -2126,7 +2126,7 @@ bool Item_date_add_interval::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
21262126
INTERVAL interval;
21272127

21282128
if (args[0]->get_date(ltime,
2129-
cached_field_type == MYSQL_TYPE_TIME ?
2129+
field_type() == MYSQL_TYPE_TIME ?
21302130
TIME_TIME_ONLY : 0) ||
21312131
get_interval_value(args[1], int_type, &interval))
21322132
return (null_value=1);
@@ -2630,20 +2630,20 @@ void Item_func_add_time::fix_length_and_dec()
26302630
- Otherwise the result is MYSQL_TYPE_STRING
26312631
*/
26322632

2633-
cached_field_type= MYSQL_TYPE_STRING;
2633+
set_handler_by_field_type(MYSQL_TYPE_STRING);
26342634
arg0_field_type= args[0]->field_type();
26352635
if (arg0_field_type == MYSQL_TYPE_DATE ||
26362636
arg0_field_type == MYSQL_TYPE_DATETIME ||
26372637
arg0_field_type == MYSQL_TYPE_TIMESTAMP ||
26382638
is_date)
26392639
{
2640-
cached_field_type= MYSQL_TYPE_DATETIME;
2640+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
26412641
decimals= MY_MAX(args[0]->temporal_precision(MYSQL_TYPE_DATETIME),
26422642
args[1]->temporal_precision(MYSQL_TYPE_TIME));
26432643
}
26442644
else if (arg0_field_type == MYSQL_TYPE_TIME)
26452645
{
2646-
cached_field_type= MYSQL_TYPE_TIME;
2646+
set_handler_by_field_type(MYSQL_TYPE_TIME);
26472647
decimals= MY_MAX(args[0]->temporal_precision(MYSQL_TYPE_TIME),
26482648
args[1]->temporal_precision(MYSQL_TYPE_TIME));
26492649
}
@@ -2669,7 +2669,7 @@ bool Item_func_add_time::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
26692669
longlong seconds;
26702670
int l_sign= sign;
26712671

2672-
if (cached_field_type == MYSQL_TYPE_DATETIME)
2672+
if (Item_func_add_time::field_type() == MYSQL_TYPE_DATETIME)
26732673
{
26742674
// TIMESTAMP function OR the first argument is DATE/DATETIME/TIMESTAMP
26752675
if (get_arg0_date(&l_time1, 0) ||
@@ -3149,7 +3149,7 @@ void Item_func_str_to_date::fix_length_and_dec()
31493149
#endif
31503150
}
31513151

3152-
cached_field_type= MYSQL_TYPE_DATETIME;
3152+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
31533153
decimals= TIME_SECOND_PART_DIGITS;
31543154
if ((const_item= args[1]->const_item()))
31553155
{
@@ -3164,24 +3164,24 @@ void Item_func_str_to_date::fix_length_and_dec()
31643164
get_date_time_result_type(format->ptr(), format->length());
31653165
switch (cached_format_type) {
31663166
case DATE_ONLY:
3167-
cached_field_type= MYSQL_TYPE_DATE;
3167+
set_handler_by_field_type(MYSQL_TYPE_DATE);
31683168
break;
31693169
case TIME_MICROSECOND:
31703170
decimals= 6;
31713171
/* fall through */
31723172
case TIME_ONLY:
3173-
cached_field_type= MYSQL_TYPE_TIME;
3173+
set_handler_by_field_type(MYSQL_TYPE_TIME);
31743174
break;
31753175
case DATE_TIME_MICROSECOND:
31763176
decimals= 6;
31773177
/* fall through */
31783178
case DATE_TIME:
3179-
cached_field_type= MYSQL_TYPE_DATETIME;
3179+
set_handler_by_field_type(MYSQL_TYPE_DATETIME);
31803180
break;
31813181
}
31823182
}
31833183
}
3184-
cached_timestamp_type= mysql_type_to_time_type(cached_field_type);
3184+
cached_timestamp_type= mysql_type_to_time_type(field_type());
31853185
Item_temporal_func::fix_length_and_dec();
31863186
}
31873187

sql/item_timefunc.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ class Item_temporal_func: public Item_func
495495
Item_temporal_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) {}
496496
Item_temporal_func(THD *thd, Item *a, Item *b, Item *c): Item_func(thd, a, b, c) {}
497497
enum Item_result result_type () const { return STRING_RESULT; }
498-
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
499498
Item_result cmp_type() const { return TIME_RESULT; }
500499
String *val_str(String *str);
501500
longlong val_int() { return val_int_from_date(); }
@@ -515,20 +514,20 @@ class Item_temporal_func: public Item_func
515514
Abstract class for functions returning TIME, DATE, DATETIME or string values,
516515
whose data type depends on parameters and is set at fix_fields time.
517516
*/
518-
class Item_temporal_hybrid_func: public Item_temporal_func
517+
class Item_temporal_hybrid_func: public Item_temporal_func,
518+
public Type_handler_hybrid_field_type
519519
{
520520
protected:
521-
enum_field_types cached_field_type; // TIME, DATE, DATETIME or STRING
522521
String ascii_buf; // Conversion buffer
523522
public:
524523
Item_temporal_hybrid_func(THD *thd, Item *a, Item *b):
525524
Item_temporal_func(thd, a, b) {}
526-
enum_field_types field_type() const { return cached_field_type; }
527-
Item_result cmp_type() const
528-
{
529-
return cached_field_type == MYSQL_TYPE_STRING ?
530-
STRING_RESULT : TIME_RESULT;
531-
}
525+
enum_field_types field_type() const
526+
{ return Type_handler_hybrid_field_type::field_type(); }
527+
enum Item_result result_type () const
528+
{ return Type_handler_hybrid_field_type::result_type(); }
529+
enum Item_result cmp_type () const
530+
{ return Type_handler_hybrid_field_type::cmp_type(); }
532531
CHARSET_INFO *charset_for_protocol() const
533532
{
534533
/*
@@ -538,7 +537,7 @@ class Item_temporal_hybrid_func: public Item_temporal_func
538537
(which is fixed from @@collation_connection in fix_length_and_dec).
539538
*/
540539
DBUG_ASSERT(fixed == 1);
541-
return cached_field_type == MYSQL_TYPE_STRING ?
540+
return Item_temporal_hybrid_func::field_type() == MYSQL_TYPE_STRING ?
542541
collation.collation : &my_charset_bin;
543542
}
544543
/**
@@ -581,6 +580,17 @@ class Item_timefunc :public Item_temporal_func
581580
};
582581

583582

583+
class Item_datetimefunc :public Item_temporal_func
584+
{
585+
public:
586+
Item_datetimefunc(THD *thd): Item_temporal_func(thd) {}
587+
Item_datetimefunc(THD *thd, Item *a): Item_temporal_func(thd, a) {}
588+
Item_datetimefunc(THD *thd, Item *a, Item *b, Item *c):
589+
Item_temporal_func(thd, a, b ,c) {}
590+
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
591+
};
592+
593+
584594
/* Abstract CURTIME function. Children should define what time zone is used */
585595

586596
class Item_func_curtime :public Item_timefunc
@@ -665,11 +675,11 @@ class Item_func_curdate_utc :public Item_func_curdate
665675
/* Abstract CURRENT_TIMESTAMP function. See also Item_func_curtime */
666676

667677

668-
class Item_func_now :public Item_temporal_func
678+
class Item_func_now :public Item_datetimefunc
669679
{
670680
MYSQL_TIME ltime;
671681
public:
672-
Item_func_now(THD *thd, uint dec): Item_temporal_func(thd) { decimals= dec; }
682+
Item_func_now(THD *thd, uint dec): Item_datetimefunc(thd) { decimals= dec; }
673683
bool fix_fields(THD *, Item **);
674684
void fix_length_and_dec()
675685
{
@@ -759,11 +769,11 @@ class Item_func_date_format :public Item_str_func
759769
};
760770

761771

762-
class Item_func_from_unixtime :public Item_temporal_func
772+
class Item_func_from_unixtime :public Item_datetimefunc
763773
{
764774
Time_zone *tz;
765775
public:
766-
Item_func_from_unixtime(THD *thd, Item *a): Item_temporal_func(thd, a) {}
776+
Item_func_from_unixtime(THD *thd, Item *a): Item_datetimefunc(thd, a) {}
767777
const char *func_name() const { return "from_unixtime"; }
768778
void fix_length_and_dec();
769779
bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
@@ -784,7 +794,7 @@ class Time_zone;
784794
tables can be used during this function calculation for loading time zone
785795
descriptions.
786796
*/
787-
class Item_func_convert_tz :public Item_temporal_func
797+
class Item_func_convert_tz :public Item_datetimefunc
788798
{
789799
/*
790800
If time zone parameters are constants we are caching objects that
@@ -796,7 +806,7 @@ class Item_func_convert_tz :public Item_temporal_func
796806
Time_zone *from_tz, *to_tz;
797807
public:
798808
Item_func_convert_tz(THD *thd, Item *a, Item *b, Item *c):
799-
Item_temporal_func(thd, a, b, c), from_tz_cached(0), to_tz_cached(0) {}
809+
Item_datetimefunc(thd, a, b, c), from_tz_cached(0), to_tz_cached(0) {}
800810
const char *func_name() const { return "convert_tz"; }
801811
void fix_length_and_dec();
802812
bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);

0 commit comments

Comments
 (0)