From bc7c439e6b496937f6862f1397e881bf639f71f7 Mon Sep 17 00:00:00 2001 From: Gleb Shchepa Date: Tue, 13 Oct 2009 21:50:08 +0500 Subject: [PATCH] Backport of fix to bug #33629 into mysql-next-mr-bugfixing. Bug #33629: last_day function can return null, but has 'not null' flag set for result LAST_DAY and MAKEDATE functions are documented as returning NULL value, but actually they was implemented as returning NOT NULL typed values. That caused a confusing error "ERROR 1048 (23000): Column '...' cannot be null" on queries like: SELECT 1 FROM (SELECT LAST_DAY('0')) a; --- mysql-test/r/func_sapdb.result | 2 +- mysql-test/r/func_time.result | 2 +- mysql-test/r/type_date.result | 22 ++++++++++++++++++++++ mysql-test/t/type_date.test | 18 ++++++++++++++++++ sql/item_timefunc.h | 6 ++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/func_sapdb.result b/mysql-test/r/func_sapdb.result index bbc5390895b..87b88692a34 100644 --- a/mysql-test/r/func_sapdb.result +++ b/mysql-test/r/func_sapdb.result @@ -194,7 +194,7 @@ date("1997-12-31 23:59:59.000001") as f8, time("1997-12-31 23:59:59.000001") as f9; describe t1; Field Type Null Key Default Extra -f1 date NO 0000-00-00 +f1 date YES NULL f2 datetime YES NULL f3 time YES NULL f4 time YES NULL diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result index 0fa143d95bc..5e72b69c41d 100644 --- a/mysql-test/r/func_time.result +++ b/mysql-test/r/func_time.result @@ -814,7 +814,7 @@ create table t1 select last_day('2000-02-05') as a, from_days(to_days("960101")) as b; describe t1; Field Type Null Key Default Extra -a date NO 0000-00-00 +a date YES NULL b date YES NULL select * from t1; a b diff --git a/mysql-test/r/type_date.result b/mysql-test/r/type_date.result index f96e07b0c5e..92be736ff9a 100644 --- a/mysql-test/r/type_date.result +++ b/mysql-test/r/type_date.result @@ -276,3 +276,25 @@ a 0000-00-01 drop table t1; End of 5.1 tests +# +# Bug #33629: last_day function can return null, but has 'not null' +# flag set for result +# +SELECT 1 FROM (SELECT LAST_DAY('0')) a; +1 +1 +Warnings: +Warning 1292 Incorrect datetime value: '0' +SELECT 1 FROM (SELECT MAKEDATE(2011,0)) a; +1 +1 +CREATE TABLE t1 AS +SELECT LAST_DAY('1970-01-01') AS f1, +MAKEDATE(1970, 1) AS f2; +DESCRIBE t1; +Field Type Null Key Default Extra +f1 date YES NULL +f2 date YES NULL +DROP TABLE t1; +# +End of 6.0 tests diff --git a/mysql-test/t/type_date.test b/mysql-test/t/type_date.test index aec60bc2dee..db273f90425 100644 --- a/mysql-test/t/type_date.test +++ b/mysql-test/t/type_date.test @@ -247,3 +247,21 @@ select * from t1 where a between '0000-00-01' and '0000-00-02'; drop table t1; --echo End of 5.1 tests + +--echo # +--echo # Bug #33629: last_day function can return null, but has 'not null' +--echo # flag set for result +--echo # + +SELECT 1 FROM (SELECT LAST_DAY('0')) a; +SELECT 1 FROM (SELECT MAKEDATE(2011,0)) a; + +CREATE TABLE t1 AS + SELECT LAST_DAY('1970-01-01') AS f1, + MAKEDATE(1970, 1) AS f2; +DESCRIBE t1; +DROP TABLE t1; + +--echo # + +--echo End of 6.0 tests diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h index 9e3c2e8c89f..5997bb96efe 100644 --- a/sql/item_timefunc.h +++ b/sql/item_timefunc.h @@ -871,6 +871,7 @@ public: { decimals=0; max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN; + maybe_null= 1; } longlong val_int(); }; @@ -1025,4 +1026,9 @@ public: Item_func_last_day(Item *a) :Item_date(a) {} const char *func_name() const { return "last_day"; } bool get_date(MYSQL_TIME *res, uint fuzzy_date); + void fix_length_and_dec() + { + Item_date::fix_length_and_dec(); + maybe_null= 1; + } };