1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-07 00:04:31 +03:00

Fix for bug #6914 "Problems using time()/date() output in expressions".

When we cast datetime value to DATE (TIME) type we should throw away its
time (date) part. This was not done properly if CAST() function was used
in datetime expressions.


mysql-test/r/cast.result:
  Added test for bug #6914 "Problems using time()/date() output in
  expressions".
mysql-test/t/cast.test:
  Added test for bug #6914 "Problems using time()/date() output in
  expressions".
sql/item_timefunc.cc:
  Item_time_typecast::get_time()/Item_date_typecast::get_date():
    When we cast datetime value to DATE we should throw away its time part.
    When we cast such value to TIME type we should throw away its date part.
This commit is contained in:
unknown
2004-12-30 13:39:01 +03:00
parent 49501611aa
commit 1382df5aff
3 changed files with 26 additions and 0 deletions

View File

@@ -178,3 +178,12 @@ aaa aa
aab aa aab aa
aac aa aac aa
DROP TABLE t1; DROP TABLE t1;
select date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour);
date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour)
2004-12-30 00:00:00
select timediff(cast('2004-12-30 12:00:00' as time), '12:00:00');
timediff(cast('2004-12-30 12:00:00' as time), '12:00:00')
00:00:00
select timediff(cast('1 12:00:00' as time), '12:00:00');
timediff(cast('1 12:00:00' as time), '12:00:00')
24:00:00

View File

@@ -108,3 +108,13 @@ SELECT a, CAST(a AS CHAR(3)) FROM t1 ORDER BY CAST(a AS CHAR(2)), a;
SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY CAST(a AS CHAR) ; SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY CAST(a AS CHAR) ;
SELECT a, CAST(a AS CHAR(2)) FROM t1 ORDER BY CAST(a AS CHAR(3)), a; SELECT a, CAST(a AS CHAR(2)) FROM t1 ORDER BY CAST(a AS CHAR(3)), a;
DROP TABLE t1; DROP TABLE t1;
#
# Test for bug #6914 "Problems using time()/date() output in expressions".
# When we are casting datetime value to DATE/TIME we should throw away
# time/date parts (correspondingly).
#
select date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour);
select timediff(cast('2004-12-30 12:00:00' as time), '12:00:00');
# Still we should not throw away "days" part of time value
select timediff(cast('1 12:00:00' as time), '12:00:00');

View File

@@ -2183,6 +2183,12 @@ String *Item_datetime_typecast::val_str(String *str)
bool Item_time_typecast::get_time(TIME *ltime) bool Item_time_typecast::get_time(TIME *ltime)
{ {
bool res= get_arg0_time(ltime); bool res= get_arg0_time(ltime);
/*
For MYSQL_TIMESTAMP_TIME value we can have non-zero day part,
which we should not lose.
*/
if (ltime->time_type == MYSQL_TIMESTAMP_DATETIME)
ltime->year= ltime->month= ltime->day= 0;
ltime->time_type= MYSQL_TIMESTAMP_TIME; ltime->time_type= MYSQL_TIMESTAMP_TIME;
return res; return res;
} }
@@ -2206,6 +2212,7 @@ String *Item_time_typecast::val_str(String *str)
bool Item_date_typecast::get_date(TIME *ltime, uint fuzzy_date) bool Item_date_typecast::get_date(TIME *ltime, uint fuzzy_date)
{ {
bool res= get_arg0_date(ltime,1); bool res= get_arg0_date(ltime,1);
ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
ltime->time_type= MYSQL_TIMESTAMP_DATE; ltime->time_type= MYSQL_TIMESTAMP_DATE;
return res; return res;
} }