Commit b639fe2b authored by Alexander Barkov's avatar Alexander Barkov

MDEV-17351 Wrong results for GREATEST,TIMESTAMP,ADDTIME with an out-of-range TIME-alike argument

Problems:

Functions LEAST() and GREATEST() in TIME context, as well as functions
TIMESTAMP(a,b) and ADDTIME(a,b), returned confusing results when the
input TIME-alike value in a number or in a string was out of the TIME
supported range.

In case of TIMESTAMP(a,b) and ADDTIME(a,b), the second argument
value could get extra unexpected digits. For example, in:
    ADDTIME('2001-01-01 00:00:00', 10000000)  or
    ADDTIME('2001-01-01 00:00:00', '1000:00:00')
the second argument was converted to '838:59:59.999999'
with six fractional digits, which contradicted "decimals"
previously set to 0 in fix_length_and_dec().
These unexpected fractional digits led to confusing function results.

Changes:
1. GREATEST(), LEAST()

   - fixing Item_func_min_max::get_time_native()
   to respect "decimals" set by fix_length_and_dec().
   If a value of some numeric or string time-alike argument
   goes outside of the TIME range and gets limited to '838:59:59.999999',
   it's now right-truncated to the correct fractional precision.

   - fixing, Type_handler_temporal_result::Item_func_min_max_fix_attributes()
   to take into account arguments' time_precision() or datetime_precision(),
   rather than rely on "decimals" calculated by the generic implementation
   in Type_handler::Item_func_min_max_fix_attributes(). This makes
   GREATEST() and LEAST() return better data types, with the same
   fractional precision with what TIMESTAMP(a,b) and ADDTIME(a,b) return
   for the same arguments, and with DATE(a) and TIMESTAMP(a).

2. Item_func_add_time and Item_func_timestamp

   It was semantically wrong to apply the limit of the TIME data type
   to the argument "b", which plays the role of "INTERVAL DAY TO SECOND" here.
   Changing the code to fetch the argument "b" as INTERVAL rather than as TIME.

   The low level routine calc_time_diff() now gets the interval
   value without limiting to '838:59:59.999999', so in these examples:
     ADDTIME('2001-01-01 00:00:00', 10000000)
     ADDTIME('2001-01-01 00:00:00', '1000:00:00')
   calc_time_diff() gets '1000:00:00' as is.  The SQL function result
   now gets limited to the supported result data type range
   (datetime or time) inside calc_time_diff(), which now calculates
   the return value using the real fractional digits that
   came directly from the arguments (without the effect of limiting
   to the TIME range), so the result does not have any unexpected
   fractional digits any more.

   Detailed changes in TIMESTAMP() and ADDTIME():

   - Adding a new class Interval_DDhhmmssff. It's similar to Time, but:
     * does not try to parse datetime format, as it's not needed for
       functions TIMESTAMP() and ADDTIME().
     * does not cut values to '838:59:59.999999'

     The maximum supported Interval_DDhhmmssff's hard limit is
     'UINT_MAX32:59:59.999999'. The maximum used soft limit is:
     - '87649415:59:59.999999'   (in 'hh:mm:ss.ff' format)
     - '3652058 23:59:59.999999' (in 'DD hh:mm:ss.ff' format)
     which is a difference between:
     - TIMESTAMP'0001-01-01 00:00:00' and
     - TIMESTAMP'9999-12-31 23:59:59.999999'
     (the minimum datetime that supports arithmetic, and the
     maximum possible datetime value).

   - Fixing get_date() methods in the classes related to functions
     ADDTIME(a,b) and TIMESTAMP(a,b) to use the new class Interval_DDhhmmssff
     for fetching data from the second argument, instead of get_date().

   - Fixing fix_length_and_dec() methods in the classes related
     to functions ADDTIME(a,b) and TIMESTAMP(a,b) to use
     Interval_DDhhmmssff::fsp(item) instead of item->time_precision()
     to get the fractional precision of the second argument correctly.

   - Splitting the low level function str_to_time() into smaller pieces
     to reuse the code. Adding a new function str_to_DDhhmmssff(), to
     parse "INTERVAL DAY TO SECOND" values.

   After these changes, functions TIMESTAMP() and ADDTIME()
   return much more predictable results, in terms of fractional
   digits, and in terms of the overall result.

   The full ranges of DATETIME and TIME values are now covered by TIMESTAMP()
   and ADDTIME(), so the following can now be calculated:

    SELECT ADDTIME(TIMESTAMP'0001-01-01 00:00:00', '87649415:59:59.999999');
    -> '9999-12-31 23:59:59.999999'

    SELECT TIMESTAMP(DATE'0001-01-01', '87649415:59:59.999999')
    -> '9999-12-31 23:59:59.999999'

    SELECT ADDTIME(TIME'-838:59:59.999999', '1677:59:59.999998');
    -> '838:59:59.999999'
parent d03581bf
......@@ -110,6 +110,8 @@ static inline void my_time_status_init(MYSQL_TIME_STATUS *status)
my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date,
ulonglong flags, int *was_cut);
my_bool str_to_DDhhmmssff(const char *str, size_t length, MYSQL_TIME *l_time,
ulong max_hour, MYSQL_TIME_STATUS *status);
my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
ulonglong flag, MYSQL_TIME_STATUS *status);
my_bool str_to_datetime(const char *str, size_t length, MYSQL_TIME *l_time,
......
......@@ -3774,8 +3774,8 @@ LEAST(20010001,TIMESTAMP'2001-01-01 00:00:00') AS i4;
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
def s1 10 10 0 Y 128 0 63
def s2 10 10 0 Y 128 0 63
def s3 12 26 0 Y 128 6 63
def s4 12 26 0 Y 128 6 63
def s3 12 26 0 Y 128 0 63
def s4 12 26 0 Y 128 0 63
def i1 10 10 0 Y 128 0 63
def i2 10 10 0 Y 128 0 63
def i3 12 19 0 Y 128 0 63
......@@ -3818,8 +3818,8 @@ Table Create Table
t1 CREATE TABLE `t1` (
`s1` date DEFAULT NULL,
`s2` date DEFAULT NULL,
`s3` datetime(6) DEFAULT NULL,
`s4` datetime(6) DEFAULT NULL,
`s3` datetime DEFAULT NULL,
`s4` datetime DEFAULT NULL,
`i1` date DEFAULT NULL,
`i2` date DEFAULT NULL,
`i3` datetime DEFAULT NULL,
......
......@@ -107,9 +107,13 @@ subtime("1997-12-31 23:59:59.000001", "1 1:1:1.000002")
select addtime("1997-12-31 23:59:59.999999", "1998-01-01 01:01:01.999999");
addtime("1997-12-31 23:59:59.999999", "1998-01-01 01:01:01.999999")
NULL
Warnings:
Warning 1292 Incorrect INTERVAL DAY TO SECOND value: '1998-01-01 01:01:01.999999'
select subtime("1997-12-31 23:59:59.999999", "1998-01-01 01:01:01.999999");
subtime("1997-12-31 23:59:59.999999", "1998-01-01 01:01:01.999999")
NULL
Warnings:
Warning 1292 Incorrect INTERVAL DAY TO SECOND value: '1998-01-01 01:01:01.999999'
select subtime("01:00:00.999999", "02:00:00.999998");
subtime("01:00:00.999999", "02:00:00.999998")
-00:59:59.999999
......
This diff is collapsed.
This diff is collapsed.
......@@ -361,7 +361,7 @@ greatest(cast('01-01-01' as date), '01-01-02') + 0
20010102
select least(cast('01-01-01' as datetime), '01-01-02') + 0;
least(cast('01-01-01' as datetime), '01-01-02') + 0
20010101000000.000000
20010101000000
select cast(least(cast('01-01-01' as datetime), '01-01-02') as signed);
cast(least(cast('01-01-01' as datetime), '01-01-02') as signed)
20010101000000
......@@ -917,7 +917,7 @@ CREATE TABLE t1 (a DATETIME);
INSERT INTO t1 VALUES ('0000-00-00 10:20:30');
SELECT a, LEAST(a,'2001-01-01 10:20:30') FROM t1;
a LEAST(a,'2001-01-01 10:20:30')
0000-00-00 10:20:30 0000-00-00 10:20:30.000000
0000-00-00 10:20:30 0000-00-00 10:20:30
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME(6));
INSERT INTO t1 VALUES ('0000-00-00 00:00:00.000001');
......
......@@ -1463,9 +1463,9 @@ GREATEST('2010-01-01 10:10:10',TIME('-20:20:20')) AS gt_minus20_implicit,
GREATEST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('-20:20:20')) AS gt_minis20_explicit,
GREATEST('2010-01-01 10:10:10',TIME('20:20:20')) AS gt_plus20_implicit,
GREATEST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('20:20:20')) AS gt_plus20_explicit;
gt_minus20_implicit 10:10:10.000000
gt_minus20_implicit 10:10:10
gt_minis20_explicit 10:10:10.000000
gt_plus20_implicit 20:20:20.000000
gt_plus20_implicit 20:20:20
gt_plus20_explicit 20:20:20.000000
SELECT
HOUR(GREATEST('2010-01-01 10:10:10',TIME('-20:20:20'))) AS gt_minus20_implicit,
......@@ -1481,9 +1481,9 @@ LEAST('2010-01-01 10:10:10',TIME('-20:20:20')) AS lt_minus20_implicit,
LEAST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('-20:20:20')) AS lt_minus20_explicit,
LEAST('2010-01-01 10:10:10',TIME('20:20:20')) AS lt_plus20_implicit,
LEAST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('20:20:20')) AS lt_plus20_explicit;
lt_minus20_implicit -20:20:20.000000
lt_minus20_implicit -20:20:20
lt_minus20_explicit -20:20:20.000000
lt_plus20_implicit 10:10:10.000000
lt_plus20_implicit 10:10:10
lt_plus20_explicit 10:10:10.000000
SELECT
HOUR(LEAST('2010-01-01 10:10:10',TIME('-20:20:20'))) AS lt_minus20_implicit,
......@@ -1499,9 +1499,9 @@ GREATEST('2010-01-01 10:10:10',TIME('-200:20:20')) AS gt_minus200_implicit,
GREATEST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('-200:20:20')) AS gt_minus200_explictit,
GREATEST('2010-01-01 10:10:10',TIME('200:20:20')) AS gt_plus200_implicit,
GREATEST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('200:20:20')) AS gt_plus200_explicit;
gt_minus200_implicit 10:10:10.000000
gt_minus200_implicit 10:10:10
gt_minus200_explictit 10:10:10.000000
gt_plus200_implicit 200:20:20.000000
gt_plus200_implicit 200:20:20
gt_plus200_explicit 200:20:20.000000
SELECT
HOUR(GREATEST('2010-01-01 10:10:10',TIME('-200:20:20'))) AS gt_minus200_implicit,
......@@ -1517,9 +1517,9 @@ LEAST('2010-01-01 10:10:10',TIME('-200:20:20')) AS lt_minus200_implicit,
LEAST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('-200:20:20')) AS lt_minus200_explictit,
LEAST('2010-01-01 10:10:10',TIME('200:20:20')) AS lt_plus200_implicit,
LEAST(CAST('2010-01-01 10:10:10' AS TIME(6)),TIME('200:20:20')) AS lt_plus200_explicit;
lt_minus200_implicit -200:20:20.000000
lt_minus200_implicit -200:20:20
lt_minus200_explictit -200:20:20.000000
lt_plus200_implicit 10:10:10.000000
lt_plus200_implicit 10:10:10
lt_plus200_explicit 10:10:10.000000
SELECT
HOUR(LEAST('2010-01-01 10:10:10',TIME('-200:20:20'))) AS lt_minus200_implicit,
......
......@@ -261,6 +261,23 @@ static void get_microseconds(ulong *val, MYSQL_TIME_STATUS *status,
}
static int check_time_range_internal(MYSQL_TIME *ltime,
ulong max_hour, uint dec,
int *warning);
int check_time_range(MYSQL_TIME *ltime, uint dec, int *warning)
{
return check_time_range_internal(ltime, TIME_MAX_HOUR, dec, warning);
}
static my_bool
str_to_DDhhmmssff_internal(my_bool neg, const char *str, size_t length,
MYSQL_TIME *l_time, ulong max_hour,
MYSQL_TIME_STATUS *status,
const char **endptr);
/*
Convert a timestamp string to a MYSQL_TIME value.
......@@ -443,6 +460,20 @@ str_to_datetime(const char *str, size_t length, MYSQL_TIME *l_time,
}
static size_t get_prefix_and_sign(my_bool *neg, const char *str, size_t length)
{
const char *str0= str, *end= str + length;
for (; str < end && my_isspace(&my_charset_latin1, *str) ; str++)
{ }
if (str < end && *str == '-')
{
*neg= TRUE;
str++;
}
return str - str0;
}
/*
Convert a time string to a MYSQL_TIME struct.
......@@ -475,23 +506,19 @@ str_to_datetime(const char *str, size_t length, MYSQL_TIME *l_time,
my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
ulonglong fuzzydate, MYSQL_TIME_STATUS *status)
{
ulong date[5];
ulonglong value;
const char *end=str+length, *end_of_days;
my_bool found_days,found_hours, neg= 0;
uint UNINIT_VAR(state);
my_bool neg= 0;
size_t tmp_length;
const char *endptr;
DBUG_ASSERT(C_FLAGS_OK(fuzzydate));
my_time_status_init(status);
for (; str != end && my_isspace(&my_charset_latin1,*str) ; str++)
length--;
if (str != end && *str == '-')
if ((tmp_length= get_prefix_and_sign(&neg, str, length)))
{
neg=1;
str++;
length--;
str+= tmp_length;
length-= tmp_length;
}
if (str == end)
if (!length)
{
status->warnings|= MYSQL_TIME_WARN_TRUNCATED;
goto err;
......@@ -508,8 +535,80 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
my_time_status_init(status);
}
if (!str_to_DDhhmmssff_internal(neg, str, length, l_time, TIME_MAX_HOUR,
status, &endptr))
return FALSE;
err:
bzero((char*) l_time, sizeof(*l_time));
l_time->time_type= MYSQL_TIMESTAMP_ERROR;
return TRUE;
}
my_bool str_to_DDhhmmssff(const char *str, size_t length, MYSQL_TIME *ltime,
ulong max_hour, MYSQL_TIME_STATUS *status)
{
my_bool neg= 0;
size_t tmp_length;
const char *endptr;
my_time_status_init(status);
/* Remove trailing spaces */
for ( ; length > 0 && my_isspace(&my_charset_latin1, str[length - 1]) ; )
length--;
if ((tmp_length= get_prefix_and_sign(&neg, str, length)))
{
str+= tmp_length;
length-= tmp_length;
}
if (!length)
{
status->warnings|= MYSQL_TIME_WARN_TRUNCATED;
set_zero_time(ltime, MYSQL_TIMESTAMP_ERROR);
return TRUE;
}
/* Reject anything that might be parsed as a full TIMESTAMP */
if (length >= 12) /* The same condition with str_to_time() */
{
(void) str_to_datetime(str, length, ltime, C_TIME_DATETIME_ONLY, status);
if (ltime->time_type > MYSQL_TIMESTAMP_ERROR)
{
status->warnings|= MYSQL_TIME_WARN_TRUNCATED;
ltime->time_type= MYSQL_TIMESTAMP_NONE;
return TRUE;
}
my_time_status_init(status);
}
/*
Scan DDhhmmssff then reject anything that can remind date/datetime.
For example, in case of '2001-01-01', str_to_DDhhmmssff_internal()
will scan only '2001'.
*/
if (str_to_DDhhmmssff_internal(neg, str, length, ltime, max_hour,
status, &endptr) ||
(endptr < str + length && endptr[0] == '-'))
return TRUE;
return FALSE;
}
static my_bool
str_to_DDhhmmssff_internal(my_bool neg, const char *str, size_t length,
MYSQL_TIME *l_time, ulong max_hour,
MYSQL_TIME_STATUS *status, const char **endptr)
{
ulong date[5];
ulonglong value;
const char *end=str + length, *end_of_days;
my_bool found_days, found_hours;
uint UNINIT_VAR(state);
*endptr= str;
l_time->neg= neg;
/* Not a timestamp. Try to get this as a DAYS_TO_SECOND string */
/* Not a timestamp. Try to get this as a DAYS TO SECOND string */
for (value=0; str != end && my_isdigit(&my_charset_latin1,*str) ; str++)
value=value*10L + (long) (*str - '0');
......@@ -621,6 +720,11 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
goto err;
}
if ((ulonglong) date[0] * 24 + date[1] > (ulonglong) UINT_MAX32)
{
status->warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
goto err;
}
l_time->year= 0; /* For protocol::store_time */
l_time->month= 0;
l_time->day= 0;
......@@ -630,8 +734,10 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
l_time->second_part= date[4];
l_time->time_type= MYSQL_TIMESTAMP_TIME;
*endptr= str;
/* Check if the value is valid and fits into MYSQL_TIME range */
if (check_time_range(l_time, 6, &status->warnings))
if (check_time_range_internal(l_time, max_hour, 6, &status->warnings))
return TRUE;
/* Check if there is garbage at end of the MYSQL_TIME specification */
......@@ -649,8 +755,7 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
return FALSE;
err:
bzero((char*) l_time, sizeof(*l_time));
l_time->time_type= MYSQL_TIMESTAMP_ERROR;
*endptr= str;
return TRUE;
}
......@@ -659,8 +764,9 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
Check 'time' value to lie in the MYSQL_TIME range
SYNOPSIS:
check_time_range()
check_time_range_internal()
time pointer to MYSQL_TIME value
ulong max_hour - maximum allowed hour value
uint dec
warning set MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is out of range
......@@ -674,9 +780,10 @@ my_bool str_to_time(const char *str, size_t length, MYSQL_TIME *l_time,
1 time value is invalid
*/
int check_time_range(struct st_mysql_time *my_time, uint dec, int *warning)
int check_time_range_internal(struct st_mysql_time *my_time,
ulong max_hour, uint dec, int *warning)
{
longlong hour;
ulonglong hour;
static ulong max_sec_part[TIME_SECOND_PART_DIGITS+1]= {000000, 900000, 990000,
999000, 999900, 999990, 999999};
......@@ -691,14 +798,14 @@ int check_time_range(struct st_mysql_time *my_time, uint dec, int *warning)
if (dec == AUTO_SEC_PART_DIGITS)
dec= TIME_SECOND_PART_DIGITS;
if (hour <= TIME_MAX_HOUR &&
(hour != TIME_MAX_HOUR || my_time->minute != TIME_MAX_MINUTE ||
if (hour <= max_hour &&
(hour != max_hour || my_time->minute != TIME_MAX_MINUTE ||
my_time->second != TIME_MAX_SECOND ||
my_time->second_part <= max_sec_part[dec]))
return 0;
my_time->day= 0;
my_time->hour= TIME_MAX_HOUR;
my_time->hour= max_hour;
my_time->minute= TIME_MAX_MINUTE;
my_time->second= TIME_MAX_SECOND;
my_time->second_part= max_sec_part[dec];
......
......@@ -2649,13 +2649,13 @@ bool Item_func_min_max::get_time_native(THD *thd, MYSQL_TIME *ltime)
{
DBUG_ASSERT(fixed == 1);
Time value(thd, args[0]);
Time value(thd, args[0], Time::Options(), decimals);
if (!value.is_valid_time())
return (null_value= true);
for (uint i= 1; i < arg_count ; i++)
{
Time tmp(thd, args[i]);
Time tmp(thd, args[i], Time::Options(), decimals);
if (!tmp.is_valid_time())
return (null_value= true);
......
......@@ -1202,20 +1202,22 @@ class Item_func_timestamp :public Item_datetimefunc
bool fix_length_and_dec()
{
THD *thd= current_thd;
uint dec= MY_MAX(args[0]->datetime_precision(thd),
args[1]->time_precision(thd));
fix_attributes_datetime(dec);
uint dec0= args[0]->datetime_precision(thd);
uint dec1= Interval_DDhhmmssff::fsp(thd, args[1]);
fix_attributes_datetime(MY_MAX(dec0, dec1));
maybe_null= true;
return false;
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
{
Datetime dt(thd, args[0], date_mode_t(0));
MYSQL_TIME ltime2;
return (null_value= (!dt.is_valid_datetime() ||
args[1]->get_time(thd, &ltime2) ||
Sec6_add(dt.get_mysql_time(), &ltime2, 1).
to_datetime(ltime)));
if (!dt.is_valid_datetime())
return null_value= true;
Interval_DDhhmmssff it(thd, args[1]);
if (!it.is_valid_interval_DDhhmmssff())
return null_value= true;
return (null_value= Sec6_add(dt.get_mysql_time(), it.get_mysql_time(), 1).
to_datetime(ltime));
}
Item *get_copy(THD *thd)
{ return get_item_copy<Item_func_timestamp>(thd, this); }
......@@ -1558,20 +1560,23 @@ class Func_handler_add_time_datetime:
bool fix_length_and_dec(Item_handled_func *item) const
{
THD *thd= current_thd;
uint dec= MY_MAX(item->arguments()[0]->datetime_precision(thd),
item->arguments()[1]->time_precision(thd));
item->fix_attributes_datetime(dec);
uint dec0= item->arguments()[0]->datetime_precision(thd);
uint dec1= Interval_DDhhmmssff::fsp(thd, item->arguments()[1]);
item->fix_attributes_datetime(MY_MAX(dec0, dec1));
return false;
}
bool get_date(THD *thd, Item_handled_func *item,
MYSQL_TIME *to, date_mode_t fuzzy) const
{
DBUG_ASSERT(item->is_fixed());
MYSQL_TIME l_time2;
Datetime dt(thd, item->arguments()[0], date_mode_t(0));
return (item->null_value= (!dt.is_valid_datetime() ||
item->arguments()[1]->get_time(current_thd, &l_time2) ||
Sec6_add(dt.get_mysql_time(), &l_time2, m_sign).
if (!dt.is_valid_datetime())
return item->null_value= true;
Interval_DDhhmmssff it(thd, item->arguments()[1]);
if (!it.is_valid_interval_DDhhmmssff())
return item->null_value= true;
return (item->null_value= (Sec6_add(dt.get_mysql_time(),
it.get_mysql_time(), m_sign).
to_datetime(to)));
}
};
......@@ -1588,20 +1593,23 @@ class Func_handler_add_time_time:
bool fix_length_and_dec(Item_handled_func *item) const
{
THD *thd= current_thd;
uint dec= MY_MAX(item->arguments()[0]->time_precision(thd),
item->arguments()[1]->time_precision(thd));
item->fix_attributes_time(dec);
uint dec0= item->arguments()[0]->time_precision(thd);
uint dec1= Interval_DDhhmmssff::fsp(thd, item->arguments()[1]);
item->fix_attributes_time(MY_MAX(dec0, dec1));
return false;
}
bool get_date(THD *thd, Item_handled_func *item,
MYSQL_TIME *to, date_mode_t fuzzy) const
{
DBUG_ASSERT(item->is_fixed());
MYSQL_TIME l_time2;
Time t(thd, item->arguments()[0]);
return (item->null_value= (!t.is_valid_time() ||
item->arguments()[1]->get_time(current_thd, &l_time2) ||
Sec6_add(t.get_mysql_time(), &l_time2, m_sign).
if (!t.is_valid_time())
return item->null_value= true;
Interval_DDhhmmssff i(thd, item->arguments()[1]);
if (!i.is_valid_interval_DDhhmmssff())
return item->null_value= true;
return (item->null_value= (Sec6_add(t.get_mysql_time(),
i.get_mysql_time(), m_sign).
to_time(thd, to, item->decimals)));
}
};
......@@ -1617,8 +1625,9 @@ class Func_handler_add_time_string:
{ }
bool fix_length_and_dec(Item_handled_func *item) const
{
uint dec= MY_MAX(item->arguments()[0]->decimals,
item->arguments()[1]->decimals);
uint dec0= item->arguments()[0]->decimals;
uint dec1= Interval_DDhhmmssff::fsp(current_thd, item->arguments()[1]);
uint dec= MY_MAX(dec0, dec1);
item->collation.set(item->default_charset(),
DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
item->fix_char_length_temporal_not_fixed_dec(MAX_DATETIME_WIDTH, dec);
......@@ -1629,12 +1638,15 @@ class Func_handler_add_time_string:
{
DBUG_ASSERT(item->is_fixed());
// Detect a proper timestamp type based on the argument values
MYSQL_TIME l_time1, l_time2;
if (item->arguments()[0]->get_time(thd, &l_time1) ||
item->arguments()[1]->get_time(thd, &l_time2))
Temporal_hybrid l_time1(thd, item->arguments()[0], TIME_TIME_ONLY);
if (!l_time1.is_valid_temporal())
return (item->null_value= true);
Interval_DDhhmmssff l_time2(thd, item->arguments()[1]);
if (!l_time2.is_valid_interval_DDhhmmssff())
return (item->null_value= true);
Sec6_add add(&l_time1, &l_time2, m_sign);
return (item->null_value= (l_time1.time_type == MYSQL_TIMESTAMP_TIME ?
Sec6_add add(l_time1.get_mysql_time(), l_time2.get_mysql_time(), m_sign);
return (item->null_value= (l_time1.get_mysql_time()->time_type ==
MYSQL_TIMESTAMP_TIME ?
add.to_time(thd, to, item->decimals) :
add.to_datetime(to)));
}
......
......@@ -395,6 +395,16 @@ bool Temporal::str_to_datetime(MYSQL_TIME_STATUS *status,
}
/* Character set-aware version of str_to_DDhhmmssff() */
bool Interval_DDhhmmssff::str_to_DDhhmmssff(MYSQL_TIME_STATUS *status,
const char *str, size_t length,
CHARSET_INFO *cs, ulong max_hour)
{
TemporalAsciiBuffer tmp(str, length, cs);
return ::str_to_DDhhmmssff(tmp.str, tmp.length, this, UINT_MAX32, status);
}
/*
Convert a timestamp string to a MYSQL_TIME value and produce a warning
if string was truncated during conversion.
......
......@@ -154,6 +154,12 @@ VDec_op::VDec_op(Item_func_hybrid_field_type *item)
}
date_mode_t Temporal::sql_mode_for_dates(THD *thd)
{
return ::sql_mode_for_dates(thd);
}
bool Dec_ptr::to_datetime_with_warn(THD *thd, MYSQL_TIME *to,
date_mode_t fuzzydate, Item *item)
{
......@@ -176,9 +182,9 @@ my_decimal *Temporal::bad_to_decimal(my_decimal *to) const
}
Temporal_hybrid::Temporal_hybrid(THD *thd, Item *item)
Temporal_hybrid::Temporal_hybrid(THD *thd, Item *item, date_mode_t fuzzydate)
{
if (item->get_date(thd, this, sql_mode_for_dates(thd)))
if (item->get_date(thd, this, fuzzydate))
time_type= MYSQL_TIMESTAMP_NONE;
}
......@@ -331,6 +337,117 @@ VYear_op::VYear_op(Item_func_hybrid_field_type *item)
{ }
const LEX_CSTRING Interval_DDhhmmssff::m_type_name=
{STRING_WITH_LEN("INTERVAL DAY TO SECOND")};
Interval_DDhhmmssff::Interval_DDhhmmssff(THD *thd, MYSQL_TIME_STATUS *st,
bool push_warnings,
Item *item, ulong max_hour)
{
my_time_status_init(st);
switch (item->cmp_type()) {
case ROW_RESULT:
DBUG_ASSERT(0);
time_type= MYSQL_TIMESTAMP_NONE;
break;
case TIME_RESULT:
{
if (item->get_date(thd, this, TIME_TIME_ONLY))
time_type= MYSQL_TIMESTAMP_NONE;
else if (time_type != MYSQL_TIMESTAMP_TIME)
{
st->warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
push_warning_wrong_or_truncated_value(thd, ErrConvTime(this),
st->warnings);
time_type= MYSQL_TIMESTAMP_NONE;
}
break;
}
case INT_RESULT:
case REAL_RESULT:
case DECIMAL_RESULT:
case STRING_RESULT:
{
StringBuffer<STRING_BUFFER_USUAL_SIZE> tmp;
String *str= item->val_str(&tmp);
if (!str)
time_type= MYSQL_TIMESTAMP_NONE;
else if (str_to_DDhhmmssff(st, str->ptr(), str->length(), str->charset(),
UINT_MAX32))
{
if (push_warnings)
thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
m_type_name.str,
ErrConvString(str).ptr());
time_type= MYSQL_TIMESTAMP_NONE;
}
else
{
if (hour > max_hour)
{
st->warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
time_type= MYSQL_TIMESTAMP_NONE;
}
// Warn if hour or nanosecond truncation happened
if (push_warnings)
push_warning_wrong_or_truncated_value(thd, ErrConvString(str),
st->warnings);
}
}
break;
}
DBUG_ASSERT(is_valid_value_slow());
}
void
Interval_DDhhmmssff::push_warning_wrong_or_truncated_value(THD *thd,
const ErrConv &str,
int warnings)
{
if (warnings & MYSQL_TIME_WARN_OUT_OF_RANGE)
{
thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
m_type_name.str, str.ptr());
}
else if (MYSQL_TIME_WARN_HAVE_WARNINGS(warnings))
{
thd->push_warning_truncated_wrong_value(Sql_condition::WARN_LEVEL_WARN,
m_type_name.str, str.ptr());
}
else if (MYSQL_TIME_WARN_HAVE_NOTES(warnings))
{
thd->push_warning_truncated_wrong_value(Sql_condition::WARN_LEVEL_NOTE,
m_type_name.str, str.ptr());
}
}
uint Interval_DDhhmmssff::fsp(THD *thd, Item *item)
{
MYSQL_TIME_STATUS st;
switch (item->cmp_type()) {
case INT_RESULT:
case TIME_RESULT:
return item->decimals;
case REAL_RESULT:
case DECIMAL_RESULT:
return MY_MIN(item->decimals, TIME_SECOND_PART_DIGITS);
case ROW_RESULT:
DBUG_ASSERT(0);
return 0;
case STRING_RESULT:
break;
}
if (!item->const_item() || item->is_expensive())
return TIME_SECOND_PART_DIGITS;
Interval_DDhhmmssff it(thd, &st, false/*no warnings*/, item, UINT_MAX32);
return it.is_valid_interval_DDhhmmssff() ? st.precision :
TIME_SECOND_PART_DIGITS;
}
void Time::make_from_item(THD *thd, int *warn, Item *item, const Options opt)
{
*warn= 0;
......@@ -3487,6 +3604,15 @@ bool Type_handler_temporal_result::
{
bool rc= Type_handler::Item_func_min_max_fix_attributes(thd, func,
items, nitems);
bool is_time= func->field_type() == MYSQL_TYPE_TIME;
func->decimals= 0;
for (uint i= 0; i < nitems; i++)
{
uint deci= is_time ? items[i]->time_precision(thd) :
items[i]->datetime_precision(thd);
set_if_bigger(func->decimals, deci);
}
if (rc || func->maybe_null)
return rc;
/*
......
......@@ -446,6 +446,7 @@ class VYear_op: public Year_null
class Temporal: protected MYSQL_TIME
{
public:
static date_mode_t sql_mode_for_dates(THD *thd);
bool is_valid_temporal() const
{
DBUG_ASSERT(time_type != MYSQL_TIMESTAMP_ERROR);
......@@ -469,6 +470,16 @@ class Temporal: protected MYSQL_TIME
CHARSET_INFO *cs, date_mode_t fuzzydate);
bool str_to_datetime(MYSQL_TIME_STATUS *st, const char *str, size_t length,
CHARSET_INFO *cs, date_mode_t fuzzydate);
bool has_valid_mmssff() const
{
return minute <= TIME_MAX_MINUTE &&
second <= TIME_MAX_SECOND &&
second_part <= TIME_MAX_SECOND_PART;
}
bool has_zero_YYYYMMDD() const
{
return year == 0 && month == 0 && day == 0;
}
public:
static void *operator new(size_t size, MYSQL_TIME *ltime) throw()
{
......@@ -492,8 +503,13 @@ class Temporal: protected MYSQL_TIME
class Temporal_hybrid: public Temporal
{
public:
Temporal_hybrid(THD *thd, Item *item);
Temporal_hybrid(Item *item): Temporal_hybrid(current_thd, item) { }
Temporal_hybrid(THD *thd, Item *item, date_mode_t fuzzydate);
Temporal_hybrid(THD *thd, Item *item)
:Temporal_hybrid(thd, item, sql_mode_for_dates(thd))
{ }
Temporal_hybrid(Item *item)
:Temporal_hybrid(current_thd, item)
{ }
Temporal_hybrid(MYSQL_TIME_STATUS *st, const char *str, size_t length,
CHARSET_INFO *cs, date_mode_t fuzzydate)
{
......@@ -538,6 +554,63 @@ class Temporal_hybrid: public Temporal
};
class Interval_DDhhmmssff: public Temporal
{
static const LEX_CSTRING m_type_name;
bool str_to_DDhhmmssff(MYSQL_TIME_STATUS *status,
const char *str, size_t length, CHARSET_INFO *cs,
ulong max_hour);
void push_warning_wrong_or_truncated_value(THD *thd,
const ErrConv &str,
int warnings);
bool is_valid_interval_DDhhmmssff_slow() const
{
return time_type == MYSQL_TIMESTAMP_TIME &&
has_zero_YYYYMMDD() && has_valid_mmssff();
}
bool is_valid_value_slow() const
{
return time_type == MYSQL_TIMESTAMP_NONE ||
is_valid_interval_DDhhmmssff_slow();
}
public:
// Get fractional second precision from an Item
static uint fsp(THD *thd, Item *item);
/*
Maximum useful HOUR value:
TIMESTAMP'0001-01-01 00:00:00' + '87649415:59:59' = '9999-12-31 23:59:59'
This gives maximum possible interval values:
- '87649415:59:59.999999' (in 'hh:mm:ss.ff' format)
- '3652058 23:59:59.999999' (in 'DD hh:mm:ss.ff' format)
*/
static uint max_useful_hour()
{
return 87649415;
}
public:
Interval_DDhhmmssff(THD *thd, MYSQL_TIME_STATUS *st, bool push_warnings,
Item *item, ulong max_hour);
Interval_DDhhmmssff(THD *thd, Item *item)
{
MYSQL_TIME_STATUS st;
new(this) Interval_DDhhmmssff(thd, &st, true, item, max_useful_hour());
}
const MYSQL_TIME *get_mysql_time() const
{
DBUG_ASSERT(is_valid_interval_DDhhmmssff_slow());
return this;
}
bool is_valid_interval_DDhhmmssff() const
{
return time_type == MYSQL_TIMESTAMP_TIME;
}
bool is_valid_value() const
{
return time_type == MYSQL_TIMESTAMP_NONE || is_valid_interval_DDhhmmssff();
}
};
/**
Class Time is designed to store valid TIME values.
......@@ -609,10 +682,7 @@ class Time: public Temporal
bool is_valid_time_slow() const
{
return time_type == MYSQL_TIMESTAMP_TIME &&
year == 0 && month == 0 && day == 0 &&
minute <= TIME_MAX_MINUTE &&
second <= TIME_MAX_SECOND &&
second_part <= TIME_MAX_SECOND_PART;
has_zero_YYYYMMDD() && has_valid_mmssff();
}
void hhmmssff_copy(const MYSQL_TIME *from)
{
......
......@@ -363,7 +363,7 @@ greatest(cast('01-01-01' as date), '01-01-02') + 0
20010102
select least(cast('01-01-01' as datetime), '01-01-02') + 0;
least(cast('01-01-01' as datetime), '01-01-02') + 0
20010101000000.000000
20010101000000
select cast(least(cast('01-01-01' as datetime), '01-01-02') as signed);
cast(least(cast('01-01-01' as datetime), '01-01-02') as signed)
20010101000000
......
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