diff options
author | Sergei Golubchik <serg@mariadb.org> | 2016-07-22 13:44:58 +0200 |
---|---|---|
committer | Sergei Golubchik <serg@mariadb.org> | 2016-08-27 16:59:12 +0200 |
commit | 266563ad775fca38dbb5c76a5100fef49515ffda (patch) | |
tree | e94b6621fbdde62b2ab9dd1d3af48db866468a9a | |
parent | 73a220aac3842da0ab8d51fa9961a18b03c45001 (diff) | |
download | mariadb-git-266563ad775fca38dbb5c76a5100fef49515ffda.tar.gz |
fix: CREATE TABLE (col TIMESTAMP(6) DEFAULT NOW(2))
That is, when the precision of DEFAULT NOW() is less than
the precision of the column, do not convert it to unireg_check,
use the new approach where DEFAULT is tryly an expression.
-rw-r--r-- | mysql-test/r/default.result | 2 | ||||
-rw-r--r-- | mysql-test/r/func_default.result | 4 | ||||
-rw-r--r-- | mysql-test/r/type_timestamp_hires.result | 67 | ||||
-rw-r--r-- | mysql-test/t/type_timestamp_hires.test | 32 | ||||
-rw-r--r-- | sql/field.cc | 58 |
5 files changed, 93 insertions, 70 deletions
diff --git a/mysql-test/r/default.result b/mysql-test/r/default.result index 1f60e66e64e..62dee045604 100644 --- a/mysql-test/r/default.result +++ b/mysql-test/r/default.result @@ -253,7 +253,7 @@ CREATE or replace TABLE t1 (event_time TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIM SHOW CREATE TABLE t1; Table Create Table t1 CREATE TABLE `t1` ( - `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) + `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(6) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 CREATE or replace TABLE t1 (event_time TIMESTAMP(6) NOT NULL DEFAULT SYSDATE(2) ON UPDATE CURRENT_TIMESTAMP); SHOW CREATE TABLE t1; diff --git a/mysql-test/r/func_default.result b/mysql-test/r/func_default.result index aac5f4942a7..535be10da86 100644 --- a/mysql-test/r/func_default.result +++ b/mysql-test/r/func_default.result @@ -29,6 +29,6 @@ insert t1 () values (); insert t1 (a) values (10); select default(a),default(b),default(c),default(d),default(e),default(f) from t1; default(a) default(b) default(c) default(d) default(e) default(f) -1 2 foo bar 2001-01-01 10:20:30 2001-01-01 10:20:30.123456 -1 11 foo bar 2001-01-01 10:20:30 2001-01-01 10:20:30.123456 +1 2 foo bar 2001-01-01 10:20:30 2001-01-01 10:20:30.120000 +1 11 foo bar 2001-01-01 10:20:30 2001-01-01 10:20:30.120000 drop table t1; diff --git a/mysql-test/r/type_timestamp_hires.result b/mysql-test/r/type_timestamp_hires.result index 89f6754ec9a..3b88454ba00 100644 --- a/mysql-test/r/type_timestamp_hires.result +++ b/mysql-test/r/type_timestamp_hires.result @@ -272,33 +272,64 @@ select * from t1; a 2011-01-01 01:01:01.12345 drop table t1; -create table t1 (a timestamp(5) default current_timestamp); +create or replace table t1 (a timestamp(5) default current_timestamp); show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `a` timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP(5) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 -drop table t1; -create table t1 (a timestamp(5) default current_timestamp()); -drop table t1; -create table t1 (a timestamp(5) default current_timestamp(2)); +create or replace table t1 (a timestamp(5) default current_timestamp()); show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `a` timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP(5) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 -drop table t1; -create table t1 (a timestamp(5) default current_timestamp(5)); -drop table t1; -create table t1 (a timestamp(5) default current_timestamp(6)); -drop table t1; -create table t1 (a timestamp(5) on update current_timestamp); -drop table t1; -create table t1 (a timestamp(5) on update current_timestamp()); -drop table t1; -create table t1 (a timestamp(5) on update current_timestamp(3)); +create or replace table t1 (a timestamp(5) default current_timestamp(2)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT current_timestamp(2) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +insert t1 () values (); +select * from t1; +a +2011-01-01 01:01:01.12000 +create or replace table t1 (a timestamp(5) default current_timestamp(5)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create or replace table t1 (a timestamp(5) default current_timestamp(6)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create or replace table t1 (a timestamp(5) on update current_timestamp); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT '0000-00-00 00:00:00.00000' ON UPDATE CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create or replace table t1 (a timestamp(5) on update current_timestamp()); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT '0000-00-00 00:00:00.00000' ON UPDATE CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create or replace table t1 (a timestamp(5) on update current_timestamp(3)); ERROR HY000: Invalid ON UPDATE clause for 'a' column -create table t1 (a timestamp(5) on update current_timestamp(5)); -drop table t1; -create table t1 (a timestamp(5) on update current_timestamp(6)); +create or replace table t1 (a timestamp(5) on update current_timestamp(5)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT '0000-00-00 00:00:00.00000' ON UPDATE CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create or replace table t1 (a timestamp(5) on update current_timestamp(6)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` timestamp(5) NOT NULL DEFAULT '0000-00-00 00:00:00.00000' ON UPDATE CURRENT_TIMESTAMP(5) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; diff --git a/mysql-test/t/type_timestamp_hires.test b/mysql-test/t/type_timestamp_hires.test index c51fb2fb247..0b05f81ef42 100644 --- a/mysql-test/t/type_timestamp_hires.test +++ b/mysql-test/t/type_timestamp_hires.test @@ -17,18 +17,26 @@ drop table t1; # # MDEV-438 Microseconds: Precision is ignored in CURRENT_TIMESTAMP(N) when it is given as a default column value # -create table t1 (a timestamp(5) default current_timestamp); +create or replace table t1 (a timestamp(5) default current_timestamp); show create table t1; -drop table t1; -create table t1 (a timestamp(5) default current_timestamp()); drop table t1; -create table t1 (a timestamp(5) default current_timestamp(2)); +create or replace table t1 (a timestamp(5) default current_timestamp()); +show create table t1; +create or replace table t1 (a timestamp(5) default current_timestamp(2)); +show create table t1; +insert t1 () values (); +select * from t1; +create or replace table t1 (a timestamp(5) default current_timestamp(5)); +show create table t1; +create or replace table t1 (a timestamp(5) default current_timestamp(6)); +show create table t1; +create or replace table t1 (a timestamp(5) on update current_timestamp); +show create table t1; +create or replace table t1 (a timestamp(5) on update current_timestamp()); show create table t1; -drop table t1; -create table t1 (a timestamp(5) default current_timestamp(5)); drop table t1; -create table t1 (a timestamp(5) default current_timestamp(6)); drop table t1; -create table t1 (a timestamp(5) on update current_timestamp); drop table t1; -create table t1 (a timestamp(5) on update current_timestamp()); drop table t1; --error ER_INVALID_ON_UPDATE -create table t1 (a timestamp(5) on update current_timestamp(3)); -create table t1 (a timestamp(5) on update current_timestamp(5)); drop table t1; -create table t1 (a timestamp(5) on update current_timestamp(6)); drop table t1; +create or replace table t1 (a timestamp(5) on update current_timestamp(3)); +create or replace table t1 (a timestamp(5) on update current_timestamp(5)); +show create table t1; +create or replace table t1 (a timestamp(5) on update current_timestamp(6)); +show create table t1; +drop table t1; diff --git a/sql/field.cc b/sql/field.cc index b8816c59d9f..73e6b4edc3c 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -9863,59 +9863,43 @@ bool Column_definition::check(THD *thd) } } } + if (default_value && (flags & AUTO_INCREMENT_FLAG)) { my_error(ER_INVALID_DEFAULT, MYF(0), field_name); DBUG_RETURN(1); } - if (default_value && !default_value->expr_item->basic_const_item()) + if (default_value && !default_value->expr_item->basic_const_item() && + mysql_type_to_time_type(sql_type) == MYSQL_TIMESTAMP_DATETIME && + default_value->expr_item->type() == Item::FUNC_ITEM) { - Item *def_expr= default_value->expr_item; - - unireg_check= Field::NONE; /* - NOW() for TIMESTAMP and DATETIME fields are handled as in MariaDB 10.1 - by marking them in unireg_check. + Special case: NOW() for TIMESTAMP and DATETIME fields are handled + as in MariaDB 10.1 by marking them in unireg_check. */ - if (def_expr->type() == Item::FUNC_ITEM && - (static_cast<Item_func*>(def_expr)->functype() == - Item_func::NOW_FUNC && - (mysql_type_to_time_type(sql_type) == MYSQL_TIMESTAMP_DATETIME))) + Item_func *fn= static_cast<Item_func*>(default_value->expr_item); + if (fn->functype() == Item_func::NOW_FUNC && + (fn->decimals == 0 || fn->decimals >= length)) { - /* - We are not checking the number of decimals for timestamps - to allow one to write (for historical reasons) - TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP - Instead we are going to use the number of decimals specifed by the - column. - */ default_value= 0; - unireg_check= (on_update ? - Field::TIMESTAMP_DNUN_FIELD : // for insertions and for updates. - Field::TIMESTAMP_DN_FIELD); // only for insertions. + unireg_check= Field::TIMESTAMP_DN_FIELD; } - else if (on_update) - unireg_check= Field::TIMESTAMP_UN_FIELD; // function default for updates - } - else - { - /* No function default for insertions. Either NULL or a constant. */ - if (on_update) - unireg_check= Field::TIMESTAMP_UN_FIELD; // function default for updates - else - unireg_check= ((flags & AUTO_INCREMENT_FLAG) ? - Field::NEXT_NUMBER : // Automatic increment. - Field::NONE); } - if (on_update && - (mysql_type_to_time_type(sql_type) != MYSQL_TIMESTAMP_DATETIME || - on_update->decimals < length)) + if (on_update) { - my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name); - DBUG_RETURN(TRUE); + if (mysql_type_to_time_type(sql_type) != MYSQL_TIMESTAMP_DATETIME || + on_update->decimals < length) + { + my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name); + DBUG_RETURN(TRUE); + } + unireg_check= unireg_check == Field::NONE ? Field::TIMESTAMP_UN_FIELD + : Field::TIMESTAMP_DNUN_FIELD; } + else if (flags & AUTO_INCREMENT_FLAG) + unireg_check= Field::NEXT_NUMBER; sign_len= flags & UNSIGNED_FLAG ? 0 : 1; |