diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index cafaff591f815..9dca70c504340 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2115,7 +2115,6 @@ func TestDefaultColumnWithUUID(t *testing.T) { store := testkit.CreateMockStoreWithSchemaLease(t, testLease) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") -<<<<<<< HEAD:ddl/db_integration_test.go tk.MustExec("drop table if exists t") tk.MustExec("create table t (c int(10), c1 varchar(256) default (uuid()))") @@ -2136,51 +2135,13 @@ func TestDefaultColumnWithUUID(t *testing.T) { " `c` int(10) DEFAULT NULL,\n" + " `c1` varchar(256) DEFAULT uuid()\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) -======= - tk.MustExec("drop table if exists t, t1, t2") - - // date_format - tk.MustExec("create table t6 (c int(10), c1 int default (date_format(now(),'%Y-%m-%d %H:%i:%s')))") - tk.MustExec("create table t7 (c int(10), c1 date default (date_format(now(),'%Y-%m')))") - // Error message like: Error 1292 (22007): Truncated incorrect DOUBLE value: '2024-03-05 16:37:25'. - tk.MustGetErrCode("insert into t6(c) values (1)", errno.ErrTruncatedWrongValue) - tk.MustGetErrCode("insert into t7(c) values (1)", errno.ErrTruncatedWrongValue) - // user - tk.MustExec("create table t (c int(10), c1 varchar(256) default (upper(substring_index(user(),'@',1))));") - tk.Session().GetSessionVars().User = &auth.UserIdentity{Username: "root", Hostname: "localhost"} - tk.MustExec("insert into t(c) values (1),(2),(3)") - tk.Session().GetSessionVars().User = &auth.UserIdentity{Username: "xyz", Hostname: "localhost"} - tk.MustExec("insert into t(c) values (4),(5),(6)") - tk.MustExec("insert into t values (7, default)") - rows := tk.MustQuery("SELECT c1 from t order by c").Rows() - for i, row := range rows { - d, ok := row[0].(string) - require.True(t, ok) - if i < 3 { - require.Equal(t, "ROOT", d) - } else { - require.Equal(t, "XYZ", d) - } - } - - // replace - tk.MustExec("create table t1 (c int(10), c1 int default (REPLACE(UPPER(UUID()), '-', '')))") - // Different UUID values will result in different error code. - _, err := tk.Exec("insert into t1(c) values (1)") - originErr := errors.Cause(err) - tErr, ok := originErr.(*terror.Error) - require.Truef(t, ok, "expect type 'terror.Error', but obtain '%T': %v", originErr, originErr) - sqlErr := terror.ToSQLError(tErr) - if int(sqlErr.Code) != errno.ErrTruncatedWrongValue { - require.Equal(t, errno.ErrDataOutOfRange, int(sqlErr.Code)) - } // test modify column - // The error message has UUID, so put this test here. - tk.MustExec("create table t2(c int(10), c1 varchar(256) default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1));") - tk.MustExec("insert into t2(c) values (1),(2),(3);") - tk.MustGetErrCode("alter table t2 modify column c1 varchar(30) default 'xx';", errno.WarnDataTruncated) ->>>>>>> 1e5c179bef0 (ddl, tests: add expression default values feature relevant tests for some DDLs and fix a related bug (#51571)):pkg/ddl/db_integration_test.go + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (c int(10), c1 varchar(256) default rand());") + tk.MustExec("alter table t alter column c1 set default 'xx';") + tk.MustExec("insert into t values (1, default);") + tk.MustQuery("select c1 from t;").Check(testkit.Rows("xx")) } func TestChangingDBCharset(t *testing.T) { diff --git a/tests/integrationtest/r/ddl/default_as_expression.result b/tests/integrationtest/r/ddl/default_as_expression.result deleted file mode 100644 index e5779aa89a3f6..0000000000000 --- a/tests/integrationtest/r/ddl/default_as_expression.result +++ /dev/null @@ -1,738 +0,0 @@ -use test; -drop table if exists t0, t1, t2, t3, t4, t5, t6, t7; -create table t0 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m'))); -create table t1 (c int(10), c1 datetime default (date_format(now(),'%Y-%m-%d'))); -create table t2 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m-%d %H.%i.%s'))); -create table t3 (c int(10), c1 timestamp default (date_format(now(),'%Y-%m-%d %H.%i.%s'))); -create table t4 (c int(10), c1 date default (date_format(now(),'%Y-%m-%d %H:%i:%s'))); -create table t5 (c int(10), c1 date default (date_format(now(),_utf8mb4'%Y-%m-%d %H:%i:%s'))); -create table t6 (c int(10), c1 varchar(256) default (date_format(now(),'%b %d %Y %h:%i %p'))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `KindString %b %d %Y %h:%i %p`. -create table t7 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m-%d %H:%i:%s %p'))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `KindString %Y-%m-%d %H:%i:%s %p`. -alter table t0 add column c2 date default (date_format(now(),'%Y-%m')); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -SET @x := NOW(); -insert into t0(c) values (1); -insert into t0 values (2, default); -SELECT * FROM t0 WHERE c = date_format(@x,'%Y-%m') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m'); -c c1 -insert into t1(c) values (1); -insert into t1 values (2, default); -SELECT * FROM t1 WHERE c = date_format(@x,'%Y-%m-%d'); -c c1 -insert into t2(c) values (1); -insert into t2 values (2, default); -SELECT * FROM t2 WHERE c = date_format(@x,'%Y-%m-%d %H.%i.%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H.%i.%s'); -c c1 -SET @x := NOW(); -insert into t3(c) values (1); -insert into t3 values (2, default); -SELECT * FROM t3 WHERE c = date_format(@x,'%Y-%m-%d %H.%i.%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H.%i.%s'); -c c1 -insert into t4(c) values (1); -insert into t4 values (2, default); -SELECT * FROM t4 WHERE c = date_format(@x,'%Y-%m-%d %H:%i:%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H:%i:%s'); -c c1 -insert into t5(c) values (1); -insert into t5 values (2, default); -SELECT * FROM t5 WHERE c = date_format(@x,'%Y-%m-%d %H:%i:%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H:%i:%s'); -c c1 -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT date_format(now(), _utf8mb4'%Y-%m') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` datetime DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d %H.%i.%s') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 add index idx(c1); -alter table t1 add index idx(c1); -insert into t0 values (3, default); -insert into t1 values (3, default); -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT date_format(now(), _utf8mb4'%Y-%m'), - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` datetime DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d'), - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 modify column c1 varchar(30) default 'xx'; -alter table t1 modify column c1 varchar(30) default 'xx'; -insert into t0 values (4, default); -insert into t1 values (4, default); -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(30) DEFAULT 'xx', - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(30) DEFAULT 'xx', - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -Error 1292 (22007): Incorrect datetime value: '2024-03' -alter table t0 alter column c1 SET DEFAULT (date_format(now(), '%Y-%m-%d')); -insert into t0 values (5, default); -alter table t1 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -Error 1292 (22007): Incorrect datetime value: 'xx' -delete from t1 where c = 4; -alter table t1 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -insert into t1 values (5, default); -alter table t0 drop index idx; -alter table t1 drop index idx; -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(30) DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` datetime DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT count(1) FROM t0 WHERE c1 = date_format(@x,'%Y-%m') OR c1 = date_format(@x,'%Y-%m-%d') OR c1 = "xx"; -count(1) -5 -SELECT count(1) FROM t1 WHERE c1 = date_format(@x,'%Y-%m-%d'); -count(1) -4 -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -show columns from test.t1 where field='c1'; -Field Type Null Key Default Extra -c1 datetime YES date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -drop table if exists t, t1, t2; -create table t (c int(10), c1 varchar(256) default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1)); -create table t1 (c int(10), c1 int default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1)); -create table t2 (c int(10), c1 varchar(256) default (REPLACE(CONVERT(UPPER(UUID()) USING UTF8MB4), '-', '')), index idx(c1)); -create table t1 (c int(10), c1 varchar(256) default (REPLACE('xdfj-jfj', '-', ''))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `REPLACE`. -create table t1 (c int(10), c1 varchar(256) default (UPPER(UUID()))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `UPPER`. -create table t1 (c int(10), c1 varchar(256) default (REPLACE(UPPER('dfdkj-kjkl-d'), '-', ''))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `REPLACE`. -alter table t add column c2 varchar(32) default (REPLACE(UPPER(UUID()), '-', '')); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -alter table t add column c3 int default (UPPER(UUID())); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -alter table t add column c4 int default (REPLACE(UPPER('dfdkj-kjkl-d'), '-', '')); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -insert into t(c) values (1),(2),(3); -insert into t values (4, default); -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; -count(1) -4 -show create table t; -Table Create Table -t CREATE TABLE `t` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4''), - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` int(11) DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4''), - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT replace(convert(upper(uuid()) using 'utf8mb4'), _utf8mb4'-', _utf8mb4''), - KEY `idx` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t alter column c1 set default 'xx'; -alter table t drop index idx; -show create table t; -Table Create Table -t CREATE TABLE `t` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT 'xx' -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -insert into t values (5, default); -show create table t; -Table Create Table -t CREATE TABLE `t` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT 'xx' -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t add unique index idx(c, c1); -alter table t modify column c1 varchar(32) default (REPLACE(UPPER(UUID()), '-', '')); -insert into t values (6, default); -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; -count(1) -5 -show create table t; -Table Create Table -t CREATE TABLE `t` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(32) DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4''), - UNIQUE KEY `idx` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t' AND COLUMN_NAME='c1'; -column_default extra -replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') DEFAULT_GENERATED -alter table t alter column c1 set default null; -insert into t(c) values (7); -alter table t alter column c1 drop default; -insert into t(c) values (8); -Error 1364 (HY000): Field 'c1' doesn't have a default value -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; -count(1) -5 -drop table if exists t0, t1, t2, t3, t4, t5; -create table t0 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')), c2 date default (str_to_date('9999-01-01','%Y-%m-%d')), index idx(c, c1)); -create table t1 (c int(10), c1 int default (str_to_date('1980-01-01','%Y-%m-%d')), c2 int default (str_to_date('9999-01-01','%Y-%m-%d')), unique key idx(c, c1)); -create table t3 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01','%m-%d'))); -create table t4 (c int(10), c1 varchar(32) default (str_to_date('01-01','%Y-%m-%d'))); -set @sqlMode := @@session.sql_mode; -set @@sql_mode=''; -create table t2 (c int(10), c1 blob default (str_to_date('1980-01-01','%Y-%m-%d')), c2 blob default (str_to_date('9999-01-01','%m-%d'))); -create table t5 (c int(10), c1 json default (str_to_date('9999-01-01','%Y-%m-%d')), c2 timestamp default (str_to_date('1980-01-01','%Y-%m-%d'))); -set session sql_mode=@sqlMode; -create table t6 (c int(10), c1 varchar(32) default (str_to_date(upper('1980-01-01'),'%Y-%m-%d'))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `str_to_date with these args`. -create table t6 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01',upper('%Y-%m-%d')))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `str_to_date with these args`. -alter table t0 add column c3 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -alter table t0 add column c4 int default (str_to_date('1980-01-01','%Y-%m-%d')); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -insert into t0(c) values (1),(2),(3); -insert into t1(c) values (1),(2),(3); -insert into t0 values (4, default, default); -insert into t1 values (4, default, default); -insert into t3(c) values (1); -Error 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' -insert into t4(c) values (1); -Error 1292 (22007): Incorrect datetime value: '2001-01-00 00:00:00' -insert into t5(c) values (1); -set @@sql_mode=''; -insert into t2(c) values (1),(2),(3); -insert into t2 values (4, default, default); -set session sql_mode=@sqlMode; -insert into t2(c) values (5); -Error 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' -select * from t0; -c c1 c2 -1 1980-01-01 9999-01-01 -2 1980-01-01 9999-01-01 -3 1980-01-01 9999-01-01 -4 1980-01-01 9999-01-01 -select * from t1; -c c1 c2 -1 19800101 99990101 -2 19800101 99990101 -3 19800101 99990101 -4 19800101 99990101 -select * from t2; -c c1 c2 -1 1980-01-01 NULL -2 1980-01-01 NULL -3 1980-01-01 NULL -4 1980-01-01 NULL -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(32) DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` date DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%Y-%m-%d'), - KEY `idx` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` int(11) DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` int(11) DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%Y-%m-%d'), - UNIQUE KEY `idx` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` blob DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 add index idx1(c1); -alter table t1 add unique index idx1(c, c1); -insert into t0 values (5, default, default); -insert into t1 values (5, default, default); -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(32) DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` date DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%Y-%m-%d'), - KEY `idx` (`c`,`c1`), - KEY `idx1` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` int(11) DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` int(11) DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%Y-%m-%d'), - UNIQUE KEY `idx` (`c`,`c1`), - UNIQUE KEY `idx1` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 alter column c2 set default (current_date()); -alter table t1 modify column c1 varchar(30) default 'xx'; -insert into t0 values (6, default, default); -insert into t1 values (6, default, default); -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(32) DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d'), - `c2` date DEFAULT CURRENT_DATE, - KEY `idx` (`c`,`c1`), - KEY `idx1` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(30) DEFAULT 'xx', - `c2` int(11) DEFAULT str_to_date(_utf8mb4'9999-01-01', _utf8mb4'%Y-%m-%d'), - UNIQUE KEY `idx` (`c`,`c1`), - UNIQUE KEY `idx1` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 alter column c1 drop default; -alter table t1 modify column c1 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')); -insert into t0 values (7, default, default); -Error 1364 (HY000): Field 'c1' doesn't have a default value -insert into t1 values (7, default, default); -select * from t0 where c < 6; -c c1 c2 -1 1980-01-01 9999-01-01 -2 1980-01-01 9999-01-01 -3 1980-01-01 9999-01-01 -4 1980-01-01 9999-01-01 -5 1980-01-01 9999-01-01 -select c, c1 from t0 where c = 6 and c2 = date_format(now(),'%Y-%m-%d');; -c c1 -6 1980-01-01 -select * from t1; -c c1 c2 -1 19800101 99990101 -2 19800101 99990101 -3 19800101 99990101 -4 19800101 99990101 -5 19800101 99990101 -6 xx 99990101 -7 1980-01-01 99990101 -select * from t2; -c c1 c2 -1 1980-01-01 NULL -2 1980-01-01 NULL -3 1980-01-01 NULL -4 1980-01-01 NULL -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -drop table if exists t, t1, t2; -create table t (c int(10), c1 varchar(256) default (upper(substring_index(user(),'@',1))), unique index idx(c, c1)); -create table t1 (c int(10), c1 int default (upper(substring_index(user(),_utf8mb4'@',1)))); -create table t2 (c int(10), c1 varchar(256) default (substring_index(user(),'@',1))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `substring_index`. -create table t2 (c int(10), c1 varchar(256) default (upper(substring_index('fjks@jkkl','@',1)))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `upper`. -create table t2 (c int(10), c1 varchar(256) default (upper(substring_index(user(),'x',1)))); -Error 3770 (HY000): Default value expression of column 'c1' contains a disallowed function: `KindString x`. -alter table t add column c2 varchar(32) default (upper(substring_index(user(),'@',1))); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -alter table t add column c3 int default (upper(substring_index('fjks@jkkl','@',1))); -Error 1674 (HY000): Statement is unsafe because it uses a system function that may return a different value on the slave -insert into t1(c) values (1); -Error 1292 (22007): Truncated incorrect DOUBLE value: 'ROOT' -show create table t; -Table Create Table -t CREATE TABLE `t` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(256) DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)), - UNIQUE KEY `idx` (`c`,`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` int(11) DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t1 modify column c1 varchar(30) default 'xx'; -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(30) DEFAULT 'xx' -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t1 modify column c1 varchar(32) default (upper(substring_index(user(),'@',1))); -alter table t1 add index idx1(c1); -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` varchar(32) DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)), - KEY `idx1` (`c1`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -upper(substring_index(user(), _utf8mb4'@', 1)) DEFAULT_GENERATED -drop table if exists t0, t1, t2, t3; -create table t0 (c int(10), c1 BLOB default (date_format(now(),'%Y-%m-%d'))); -create table t1 (c int(10), c1 JSON default (date_format(now(),'%Y-%m-%d'))); -create table t2 (c int(10), c1 ENUM('y','n') default (date_format(now(),'%Y-%m-%d'))); -create table t3 (c int(10), c1 SET('y','n') default (date_format(now(),'%Y-%m-%d'))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -select count(1) from t0 where c1 = date_format(now(), '%Y-%m-%d'); -count(1) -2 -INSERT INTO t1 values (); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -INSERT INTO t1 values (1, DEFAULT); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -SELECT * from t1; -c c1 -INSERT INTO t2 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t2 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t2; -c c1 -INSERT INTO t3 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t3 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t3; -c c1 -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -column_default extra -date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -column_default extra -date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -column_default extra -date_format(now(), _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -alter table t0 alter column c1 set default "xx"; -Error 1101 (42000): BLOB/TEXT/JSON column 'c1' can't have a default value -alter table t1 alter column c1 set default "xx"; -Error 1101 (42000): BLOB/TEXT/JSON column 'c1' can't have a default value -alter table t2 alter column c1 set default 'y'; -alter table t3 alter column c1 set default 'n'; -INSERT INTO t0 values (2, DEFAULT); -INSERT INTO t2 values (2, DEFAULT); -INSERT INTO t3 values (2, DEFAULT); -alter table t0 modify column c1 BLOB default (date_format(now(),'%Y-%m-%d')); -alter table t1 modify column c1 JSON default (date_format(now(),'%Y-%m-%d')); -alter table t2 modify column c1 ENUM('y','n') default (date_format(now(),'%Y-%m-%d')); -alter table t3 modify column c1 SET('y','n') default (date_format(now(),'%Y-%m-%d')); -INSERT INTO t0 values (3, DEFAULT); -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') DEFAULT date_format(now(), _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -alter table t0 alter column c1 drop default; -alter table t1 alter column c1 drop default; -alter table t2 alter column c1 drop default; -alter table t3 alter column c1 drop default; -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -select count(1) from t0 where c1 = date_format(now(), '%Y-%m-%d'); -count(1) -4 -select * from t2; -c c1 -2 y -select * from t3; -c c1 -2 n -drop table t0, t1, t2, t3; -create table t0 (c int(10), c1 BLOB default (REPLACE(UPPER(UUID()), '-', ''))); -create table t1 (c int(10), c1 JSON default (REPLACE(UPPER(UUID()), '-', ''))); -create table t2 (c int(10), c1 ENUM('y','n') default (REPLACE(UPPER(UUID()), '-', ''))); -create table t3 (c int(10), c1 SET('y','n') default (REPLACE(UPPER(UUID()), '-', ''))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT count(1) FROM t0 WHERE c1 REGEXP '^[A-Z0-9]+$'; -count(1) -2 -INSERT INTO t1 values (); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -INSERT INTO t1 values (1, DEFAULT); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -SELECT * from t1; -c c1 -INSERT INTO t2 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t2 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t2; -c c1 -INSERT INTO t3 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t3 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t3; -c c1 -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') DEFAULT replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -column_default extra -replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -column_default extra -replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -column_default extra -replace(upper(uuid()), _utf8mb4'-', _utf8mb4'') DEFAULT_GENERATED -drop table t0, t1, t2, t3; -create table t0 (c int(10), c1 BLOB default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t1 (c int(10), c1 JSON default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t2 (c int(10), c1 ENUM('y','n') default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t3 (c int(10), c1 SET('y','n') default (str_to_date('1980-01-01','%Y-%m-%d'))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT * from t0; -c c1 -NULL 1980-01-01 -1 1980-01-01 -INSERT INTO t1 values (); -INSERT INTO t1 values (1, DEFAULT); -SELECT * from t1; -c c1 -NULL "1980-01-01" -1 "1980-01-01" -INSERT INTO t2 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t2 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t2; -c c1 -INSERT INTO t3 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t3 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t3; -c c1 -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') DEFAULT str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -column_default extra -str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -column_default extra -str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -column_default extra -str_to_date(_utf8mb4'1980-01-01', _utf8mb4'%Y-%m-%d') DEFAULT_GENERATED -drop table t0, t1, t2, t3; -create table t0 (c int(10), c1 BLOB default (upper(substring_index(user(),'@',1)))); -create table t1 (c int(10), c1 JSON default (upper(substring_index(user(),'@',1)))); -create table t2 (c int(10), c1 ENUM('y','n') default (upper(substring_index(user(),'@',1)))); -create table t3 (c int(10), c1 SET('y','n') default (upper(substring_index(user(),'@',1)))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT * from t0; -c c1 -NULL ROOT -1 ROOT -INSERT INTO t1 values (); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -INSERT INTO t1 values (1, DEFAULT); -Error 3140 (22032): Invalid JSON text: The document root must not be followed by other values. -SELECT * from t1; -c c1 -INSERT INTO t2 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t2 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t2; -c c1 -INSERT INTO t3 values (); -Error 1265 (01000): Data truncated for column '%s' at row %d -INSERT INTO t3 values (1, DEFAULT); -Error 1265 (01000): Data truncated for column '%s' at row %d -SELECT * from t3; -c c1 -show create table t0; -Table Create Table -t0 CREATE TABLE `t0` ( - `c` int(10) DEFAULT NULL, - `c1` blob DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `c` int(10) DEFAULT NULL, - `c1` json DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t2; -Table Create Table -t2 CREATE TABLE `t2` ( - `c` int(10) DEFAULT NULL, - `c1` enum('y','n') DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -show create table t3; -Table Create Table -t3 CREATE TABLE `t3` ( - `c` int(10) DEFAULT NULL, - `c1` set('y','n') DEFAULT upper(substring_index(user(), _utf8mb4'@', 1)) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin -drop table t0, t1, t2, t3; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -column_default extra -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -column_default extra -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -column_default extra -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -column_default extra diff --git a/tests/integrationtest/t/ddl/default_as_expression.test b/tests/integrationtest/t/ddl/default_as_expression.test deleted file mode 100644 index 09d81e1cf0e55..0000000000000 --- a/tests/integrationtest/t/ddl/default_as_expression.test +++ /dev/null @@ -1,385 +0,0 @@ -# TestDefaultColumnWithDateFormat -# date_format -use test; -drop table if exists t0, t1, t2, t3, t4, t5, t6, t7; -create table t0 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m'))); -create table t1 (c int(10), c1 datetime default (date_format(now(),'%Y-%m-%d'))); -create table t2 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m-%d %H.%i.%s'))); -create table t3 (c int(10), c1 timestamp default (date_format(now(),'%Y-%m-%d %H.%i.%s'))); -create table t4 (c int(10), c1 date default (date_format(now(),'%Y-%m-%d %H:%i:%s'))); -create table t5 (c int(10), c1 date default (date_format(now(),_utf8mb4'%Y-%m-%d %H:%i:%s'))); --- error 3770 -create table t6 (c int(10), c1 varchar(256) default (date_format(now(),'%b %d %Y %h:%i %p'))); --- error 3770 -create table t7 (c int(10), c1 varchar(256) default (date_format(now(),'%Y-%m-%d %H:%i:%s %p'))); --- error 1674 -alter table t0 add column c2 date default (date_format(now(),'%Y-%m')); -# insert records -SET @x := NOW(); -insert into t0(c) values (1); -insert into t0 values (2, default); -SELECT * FROM t0 WHERE c = date_format(@x,'%Y-%m') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m'); -insert into t1(c) values (1); -insert into t1 values (2, default); -SELECT * FROM t1 WHERE c = date_format(@x,'%Y-%m-%d'); -insert into t2(c) values (1); -insert into t2 values (2, default); -SELECT * FROM t2 WHERE c = date_format(@x,'%Y-%m-%d %H.%i.%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H.%i.%s'); -SET @x := NOW(); -insert into t3(c) values (1); -insert into t3 values (2, default); -SELECT * FROM t3 WHERE c = date_format(@x,'%Y-%m-%d %H.%i.%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H.%i.%s'); -insert into t4(c) values (1); -insert into t4 values (2, default); -SELECT * FROM t4 WHERE c = date_format(@x,'%Y-%m-%d %H:%i:%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H:%i:%s'); -insert into t5(c) values (1); -insert into t5 values (2, default); -SELECT * FROM t5 WHERE c = date_format(@x,'%Y-%m-%d %H:%i:%s') OR c = date_format(DATE_ADD(@x, INTERVAL 1 SECOND), '%Y-%m-%d %H:%i:%s'); - -show create table t0; -show create table t1; -show create table t2; - -# test modify column, set default value, add index -alter table t0 add index idx(c1); -alter table t1 add index idx(c1); -insert into t0 values (3, default); -insert into t1 values (3, default); -show create table t0; -show create table t1; -alter table t0 modify column c1 varchar(30) default 'xx'; -alter table t1 modify column c1 varchar(30) default 'xx'; -insert into t0 values (4, default); -insert into t1 values (4, default); -show create table t0; -show create table t1; --- error 1292 -alter table t0 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -alter table t0 alter column c1 SET DEFAULT (date_format(now(), '%Y-%m-%d')); -insert into t0 values (5, default); --- error 1292 -alter table t1 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -delete from t1 where c = 4; -alter table t1 modify column c1 datetime DEFAULT (date_format(now(), '%Y-%m-%d')); -insert into t1 values (5, default); -alter table t0 drop index idx; -alter table t1 drop index idx; -show create table t0; -show create table t1; -SELECT count(1) FROM t0 WHERE c1 = date_format(@x,'%Y-%m') OR c1 = date_format(@x,'%Y-%m-%d') OR c1 = "xx"; -SELECT count(1) FROM t1 WHERE c1 = date_format(@x,'%Y-%m-%d'); -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -show columns from test.t1 where field='c1'; - -# TestDefaultColumnWithReplace -# replace -drop table if exists t, t1, t2; -create table t (c int(10), c1 varchar(256) default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1)); -create table t1 (c int(10), c1 int default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1)); -create table t2 (c int(10), c1 varchar(256) default (REPLACE(CONVERT(UPPER(UUID()) USING UTF8MB4), '-', '')), index idx(c1)); --- error 3770 -create table t1 (c int(10), c1 varchar(256) default (REPLACE('xdfj-jfj', '-', ''))); --- error 3770 -create table t1 (c int(10), c1 varchar(256) default (UPPER(UUID()))); --- error 3770 -create table t1 (c int(10), c1 varchar(256) default (REPLACE(UPPER('dfdkj-kjkl-d'), '-', ''))); - -# add column --- error 1674 -alter table t add column c2 varchar(32) default (REPLACE(UPPER(UUID()), '-', '')); --- error 1674 -alter table t add column c3 int default (UPPER(UUID())); -# Alter support "REPLACE(UPPER('dfdkj-kjkl-d'), '-', '')", we need to support this DDL. --- error 1674 -alter table t add column c4 int default (REPLACE(UPPER('dfdkj-kjkl-d'), '-', '')); - -# insert records -insert into t(c) values (1),(2),(3); -insert into t values (4, default); -# It consists of uppercase letters or numbers. -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; - -# Some MySQL versions of "show create table" have different results. For example, MySQL 8.0.18 has the following results: -# `c1` varchar(16) DEFAULT (replace(convert(upper(uuid()) using utf8mb4),_utf8mb4'-',_utf8mb4'')) -show create table t; -show create table t1; -show create table t2; - -# test modify column, set default value, add index -alter table t alter column c1 set default 'xx'; -alter table t drop index idx; -show create table t; -insert into t values (5, default); -show create table t; -alter table t add unique index idx(c, c1); -alter table t modify column c1 varchar(32) default (REPLACE(UPPER(UUID()), '-', '')); -insert into t values (6, default); -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; -show create table t; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t' AND COLUMN_NAME='c1'; -alter table t alter column c1 set default null; -insert into t(c) values (7); -alter table t alter column c1 drop default; --- error 1364 -insert into t(c) values (8); -SELECT count(1) FROM t WHERE c1 REGEXP '^[A-Z0-9]+$'; - -# TestDefaultColumnWithStrToDate -# str_to_date -drop table if exists t0, t1, t2, t3, t4, t5; -# create table -create table t0 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')), c2 date default (str_to_date('9999-01-01','%Y-%m-%d')), index idx(c, c1)); -create table t1 (c int(10), c1 int default (str_to_date('1980-01-01','%Y-%m-%d')), c2 int default (str_to_date('9999-01-01','%Y-%m-%d')), unique key idx(c, c1)); -create table t3 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01','%m-%d'))); -create table t4 (c int(10), c1 varchar(32) default (str_to_date('01-01','%Y-%m-%d'))); -set @sqlMode := @@session.sql_mode; -set @@sql_mode=''; -create table t2 (c int(10), c1 blob default (str_to_date('1980-01-01','%Y-%m-%d')), c2 blob default (str_to_date('9999-01-01','%m-%d'))); -create table t5 (c int(10), c1 json default (str_to_date('9999-01-01','%Y-%m-%d')), c2 timestamp default (str_to_date('1980-01-01','%Y-%m-%d'))); -set session sql_mode=@sqlMode; --- error 3770 -create table t6 (c int(10), c1 varchar(32) default (str_to_date(upper('1980-01-01'),'%Y-%m-%d'))); --- error 3770 -create table t6 (c int(10), c1 varchar(32) default (str_to_date('1980-01-01',upper('%Y-%m-%d')))); -# TODO: We need to support it. --- error 1674 -alter table t0 add column c3 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')); --- error 1674 -alter table t0 add column c4 int default (str_to_date('1980-01-01','%Y-%m-%d')); - -# insert records -insert into t0(c) values (1),(2),(3); -insert into t1(c) values (1),(2),(3); -insert into t0 values (4, default, default); -insert into t1 values (4, default, default); --- error 1292 -insert into t3(c) values (1); --- error 1292 -insert into t4(c) values (1); -# MySQL will return an error. Related issue: https://github.com/pingcap/tidb/issues/51275. -insert into t5(c) values (1); -set @@sql_mode=''; -insert into t2(c) values (1),(2),(3); -insert into t2 values (4, default, default); -set session sql_mode=@sqlMode; --- error 1292 -insert into t2(c) values (5); -select * from t0; -select * from t1; -select * from t2; - -show create table t0; -show create table t1; -show create table t2; - -# test modify column, set default value, add index -alter table t0 add index idx1(c1); -alter table t1 add unique index idx1(c, c1); -insert into t0 values (5, default, default); -insert into t1 values (5, default, default); -show create table t0; -show create table t1; -alter table t0 alter column c2 set default (current_date()); -alter table t1 modify column c1 varchar(30) default 'xx'; -insert into t0 values (6, default, default); -insert into t1 values (6, default, default); -show create table t0; -show create table t1; -alter table t0 alter column c1 drop default; -alter table t1 modify column c1 varchar(32) default (str_to_date('1980-01-01','%Y-%m-%d')); --- error 1364 -insert into t0 values (7, default, default); -insert into t1 values (7, default, default); -select * from t0 where c < 6; -select c, c1 from t0 where c = 6 and c2 = date_format(now(),'%Y-%m-%d');; -select * from t1; -select * from t2; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; - -# TestDefaultColumnWithUpper -# upper -drop table if exists t, t1, t2; -# create table -create table t (c int(10), c1 varchar(256) default (upper(substring_index(user(),'@',1))), unique index idx(c, c1)); -create table t1 (c int(10), c1 int default (upper(substring_index(user(),_utf8mb4'@',1)))); --- error 3770 -create table t2 (c int(10), c1 varchar(256) default (substring_index(user(),'@',1))); --- error 3770 -create table t2 (c int(10), c1 varchar(256) default (upper(substring_index('fjks@jkkl','@',1)))); --- error 3770 -create table t2 (c int(10), c1 varchar(256) default (upper(substring_index(user(),'x',1)))); --- error 1674 -alter table t add column c2 varchar(32) default (upper(substring_index(user(),'@',1))); --- error 1674 -alter table t add column c3 int default (upper(substring_index('fjks@jkkl','@',1))); --- error 1292 -insert into t1(c) values (1); -show create table t; -show create table t1; - -# test modify column, set default value, add index -alter table t1 modify column c1 varchar(30) default 'xx'; -show create table t1; -alter table t1 modify column c1 varchar(32) default (upper(substring_index(user(),'@',1))); -alter table t1 add index idx1(c1); -show create table t1; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; - -# TestDefaultColumnWithDateFormatAndReplaceAndUpperAndStrToDate -# Different data types for data_format. -drop table if exists t0, t1, t2, t3; -create table t0 (c int(10), c1 BLOB default (date_format(now(),'%Y-%m-%d'))); -create table t1 (c int(10), c1 JSON default (date_format(now(),'%Y-%m-%d'))); -create table t2 (c int(10), c1 ENUM('y','n') default (date_format(now(),'%Y-%m-%d'))); -create table t3 (c int(10), c1 SET('y','n') default (date_format(now(),'%Y-%m-%d'))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -select count(1) from t0 where c1 = date_format(now(), '%Y-%m-%d'); --- error 3140 -INSERT INTO t1 values (); --- error 3140 -INSERT INTO t1 values (1, DEFAULT); -SELECT * from t1; --- error 1265 -INSERT INTO t2 values (); --- error 1265 -INSERT INTO t2 values (1, DEFAULT); -SELECT * from t2; --- error 1265 -INSERT INTO t3 values (); --- error 1265 -INSERT INTO t3 values (1, DEFAULT); -SELECT * from t3; -show create table t0; -show create table t1; -show create table t2; -show create table t3; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; --- error 1101 -alter table t0 alter column c1 set default "xx"; --- error 1101 -alter table t1 alter column c1 set default "xx"; -alter table t2 alter column c1 set default 'y'; -alter table t3 alter column c1 set default 'n'; -INSERT INTO t0 values (2, DEFAULT); -INSERT INTO t2 values (2, DEFAULT); -INSERT INTO t3 values (2, DEFAULT); -alter table t0 modify column c1 BLOB default (date_format(now(),'%Y-%m-%d')); -alter table t1 modify column c1 JSON default (date_format(now(),'%Y-%m-%d')); -alter table t2 modify column c1 ENUM('y','n') default (date_format(now(),'%Y-%m-%d')); -alter table t3 modify column c1 SET('y','n') default (date_format(now(),'%Y-%m-%d')); -INSERT INTO t0 values (3, DEFAULT); -show create table t0; -show create table t1; -show create table t2; -show create table t3; -alter table t0 alter column c1 drop default; -alter table t1 alter column c1 drop default; -alter table t2 alter column c1 drop default; -alter table t3 alter column c1 drop default; -show create table t0; -show create table t1; -show create table t2; -show create table t3; -select count(1) from t0 where c1 = date_format(now(), '%Y-%m-%d'); -select * from t2; -select * from t3; -drop table t0, t1, t2, t3; -# Different data types for replace. -create table t0 (c int(10), c1 BLOB default (REPLACE(UPPER(UUID()), '-', ''))); -create table t1 (c int(10), c1 JSON default (REPLACE(UPPER(UUID()), '-', ''))); -create table t2 (c int(10), c1 ENUM('y','n') default (REPLACE(UPPER(UUID()), '-', ''))); -create table t3 (c int(10), c1 SET('y','n') default (REPLACE(UPPER(UUID()), '-', ''))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT count(1) FROM t0 WHERE c1 REGEXP '^[A-Z0-9]+$'; --- error 3140 -INSERT INTO t1 values (); --- error 3140 -INSERT INTO t1 values (1, DEFAULT); -SELECT * from t1; --- error 1265 -INSERT INTO t2 values (); --- error 1265 -INSERT INTO t2 values (1, DEFAULT); -SELECT * from t2; --- error 1265 -INSERT INTO t3 values (); --- error 1265 -INSERT INTO t3 values (1, DEFAULT); -SELECT * from t3; -show create table t0; -show create table t1; -show create table t2; -show create table t3; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -drop table t0, t1, t2, t3; -# Different data types for str_to_date. -create table t0 (c int(10), c1 BLOB default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t1 (c int(10), c1 JSON default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t2 (c int(10), c1 ENUM('y','n') default (str_to_date('1980-01-01','%Y-%m-%d'))); -create table t3 (c int(10), c1 SET('y','n') default (str_to_date('1980-01-01','%Y-%m-%d'))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT * from t0; -# MySQL will return an error. Related issue: https://github.com/pingcap/tidb/issues/51275. -INSERT INTO t1 values (); -INSERT INTO t1 values (1, DEFAULT); -SELECT * from t1; --- error 1265 -INSERT INTO t2 values (); --- error 1265 -INSERT INTO t2 values (1, DEFAULT); -SELECT * from t2; --- error 1265 -INSERT INTO t3 values (); --- error 1265 -INSERT INTO t3 values (1, DEFAULT); -SELECT * from t3; -show create table t0; -show create table t1; -show create table t2; -show create table t3; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1'; -drop table t0, t1, t2, t3; -# Different data types for upper. -create table t0 (c int(10), c1 BLOB default (upper(substring_index(user(),'@',1)))); -create table t1 (c int(10), c1 JSON default (upper(substring_index(user(),'@',1)))); -create table t2 (c int(10), c1 ENUM('y','n') default (upper(substring_index(user(),'@',1)))); -create table t3 (c int(10), c1 SET('y','n') default (upper(substring_index(user(),'@',1)))); -INSERT INTO t0 values (); -INSERT INTO t0 values (1, DEFAULT); -SELECT * from t0; --- error 3140 -INSERT INTO t1 values (); --- error 3140 -INSERT INTO t1 values (1, DEFAULT); -SELECT * from t1; --- error 1265 -INSERT INTO t2 values (); --- error 1265 -INSERT INTO t2 values (1, DEFAULT); -SELECT * from t2; --- error 1265 -INSERT INTO t3 values (); --- error 1265 -INSERT INTO t3 values (1, DEFAULT); -SELECT * from t3; -show create table t0; -show create table t1; -show create table t2; -show create table t3; -drop table t0, t1, t2, t3; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t0' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t1' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t2' AND COLUMN_NAME='c1'; -SELECT column_default, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='test' AND TABLE_NAME='t3' AND COLUMN_NAME='c1';