diff --git a/mysql-test/r/type_datetime.result b/mysql-test/r/type_datetime.result index f7bfba0accd..958285d6a23 100644 --- a/mysql-test/r/type_datetime.result +++ b/mysql-test/r/type_datetime.result @@ -680,5 +680,17 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 const PRIMARY PRIMARY 8 const 1 Using index DROP TABLE t1,t2; # +# Bug#57095: Wrongly chosen expression cache type led to a wrong +# result. +# +CREATE TABLE t1 (`b` datetime ); +INSERT INTO t1 VALUES ('2010-01-01 00:00:00'), ('2010-01-01 00:00:00'); +SELECT * FROM t1 WHERE b <= coalesce(NULL, now()); +b +2010-01-01 00:00:00 +2010-01-01 00:00:00 +DROP TABLE t1; +# +# # End of 5.5 tests # diff --git a/mysql-test/t/type_datetime.test b/mysql-test/t/type_datetime.test index 6e40c9ccfa0..ca2a7804907 100644 --- a/mysql-test/t/type_datetime.test +++ b/mysql-test/t/type_datetime.test @@ -484,6 +484,16 @@ explain select * from t2 where f1=STR_TO_DATE('4/1/2010', '%m/%d/%Y'); DROP TABLE t1,t2; +--echo # +--echo # Bug#57095: Wrongly chosen expression cache type led to a wrong +--echo # result. +--echo # +CREATE TABLE t1 (`b` datetime ); +INSERT INTO t1 VALUES ('2010-01-01 00:00:00'), ('2010-01-01 00:00:00'); +SELECT * FROM t1 WHERE b <= coalesce(NULL, now()); +DROP TABLE t1; +--echo # + --echo # --echo # End of 5.5 tests --echo # diff --git a/sql/item.cc b/sql/item.cc index e782e90b874..fff7511015f 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -7356,9 +7356,11 @@ Item_cache* Item_cache::get_cache(const Item *item, const Item_result type) case DECIMAL_RESULT: return new Item_cache_decimal(); case STRING_RESULT: - if (item->field_type() == MYSQL_TYPE_DATE || - item->field_type() == MYSQL_TYPE_DATETIME || - item->field_type() == MYSQL_TYPE_TIME) + /* Not all functions that return DATE/TIME are actually DATE/TIME funcs. */ + if ((item->field_type() == MYSQL_TYPE_DATE || + item->field_type() == MYSQL_TYPE_DATETIME || + item->field_type() == MYSQL_TYPE_TIME) && + (const_cast(item))->result_as_longlong()) return new Item_cache_datetime(item->field_type()); return new Item_cache_str(item); case ROW_RESULT: