From 1a0e3a285818f0a71cdc7f387a63f7cc72c72972 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 4 May 2007 00:53:37 +0400 Subject: [PATCH] Bug#23656: Wrong conversion result of a DATETIME to integer using CAST function. The generic string to int conversion was used by the Item_func_signed and the Item_func_unsigned classes to convert DATE/DATETIME values to the SIGNED/UNSIGNED type. But this conversion produces wrong results for such values. Now if the item which result has to be converted can return its result as longlong then the item->val_int() method is used to allow the item to carry out the conversion itself and return the correct result. This condition is checked in the Item_func_signed::val_int() and the Item_func_unsigned::val_int() functions. mysql-test/t/cast.test: Added a test case for the bug#23656: Wrong conversion result of a DATETIME to integer using CAST function. mysql-test/r/cast.result: Added a test case for the bug#23656: Wrong conversion result of a DATETIME to integer using CAST function. sql/item_func.cc: Bug#23656: Wrong conversion result of a DATETIME to integer using CAST function. Now if the item which result has to be converted can return its result as longlong then the item->val_int() method is used to allow the item to carry out the conversion itself and return the correct result. This condition is checked in the Item_func_signed::val_int() and the Item_func_unsigned::val_int() functions. --- mysql-test/r/cast.result | 6 ++++++ mysql-test/t/cast.test | 6 ++++++ sql/item_func.cc | 6 ++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/cast.result b/mysql-test/r/cast.result index 23c38bb792c..f6f46bd4079 100644 --- a/mysql-test/r/cast.result +++ b/mysql-test/r/cast.result @@ -281,4 +281,10 @@ DROP TABLE t1; select isnull(date(NULL)), isnull(cast(NULL as DATE)); isnull(date(NULL)) isnull(cast(NULL as DATE)) 1 1 +SELECT CAST(cast('01-01-01' as date) AS UNSIGNED); +CAST(cast('01-01-01' as date) AS UNSIGNED) +20010101 +SELECT CAST(cast('01-01-01' as date) AS SIGNED); +CAST(cast('01-01-01' as date) AS SIGNED) +20010101 End of 4.1 tests diff --git a/mysql-test/t/cast.test b/mysql-test/t/cast.test index 7e8ef031e6b..8eef66f9e1b 100644 --- a/mysql-test/t/cast.test +++ b/mysql-test/t/cast.test @@ -173,4 +173,10 @@ DROP TABLE t1; select isnull(date(NULL)), isnull(cast(NULL as DATE)); +# +# Bug#23656: Wrong result of CAST from DATE to int +# +SELECT CAST(cast('01-01-01' as date) AS UNSIGNED); +SELECT CAST(cast('01-01-01' as date) AS SIGNED); + --echo End of 4.1 tests diff --git a/sql/item_func.cc b/sql/item_func.cc index c6b2fa5cc3e..12bb6571369 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -477,7 +477,8 @@ longlong Item_func_signed::val_int() longlong value; int error; - if (args[0]->cast_to_int_type() != STRING_RESULT) + if (args[0]->cast_to_int_type() != STRING_RESULT || + args[0]->result_as_longlong()) { value= args[0]->val_int(); null_value= args[0]->null_value; @@ -529,7 +530,8 @@ longlong Item_func_unsigned::val_int() return (longlong) (dvalue + (dvalue > 0 ? 0.5 : -0.5)); } - if (args[0]->cast_to_int_type() != STRING_RESULT) + if (args[0]->cast_to_int_type() != STRING_RESULT || + args[0]->result_as_longlong()) { value= args[0]->val_int(); null_value= args[0]->null_value;