summaryrefslogtreecommitdiff
path: root/sql/item_timefunc.h
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2018-10-08 13:38:01 +0400
committerAlexander Barkov <bar@mariadb.com>2018-10-08 13:38:01 +0400
commitb639fe2be11a8b4904da081d70881b5122e8c718 (patch)
tree901761ebe821ed0f82eb869a9cf2d363fe2b5ea4 /sql/item_timefunc.h
parentd03581bf3cc4afa397f9780eb63e568c8241dd0c (diff)
downloadmariadb-git-b639fe2be11a8b4904da081d70881b5122e8c718.tar.gz
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'
Diffstat (limited to 'sql/item_timefunc.h')
-rw-r--r--sql/item_timefunc.h70
1 files changed, 41 insertions, 29 deletions
diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h
index ad261635cc0..c1e77eae5bc 100644
--- a/sql/item_timefunc.h
+++ b/sql/item_timefunc.h
@@ -1202,20 +1202,22 @@ public:
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 @@ public:
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 @@ public:
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 @@ public:
{ }
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 @@ public:
{
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)));
}