diff --git a/executor/test/ddl/BUILD.bazel b/executor/test/ddl/BUILD.bazel index 9ecaaa0b63035..b4e59f0f2bbb3 100644 --- a/executor/test/ddl/BUILD.bazel +++ b/executor/test/ddl/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 42, + shard_count = 27, deps = [ "//config", "//ddl/schematracker", diff --git a/executor/test/ddl/ddl_test.go b/executor/test/ddl/ddl_test.go index e0244ee57657d..7ac08a2600aba 100644 --- a/executor/test/ddl/ddl_test.go +++ b/executor/test/ddl/ddl_test.go @@ -52,20 +52,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestTruncateTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists truncate_test;`) - tk.MustExec(`create table truncate_test (a int)`) - tk.MustExec(`insert truncate_test values (1),(2),(3)`) - result := tk.MustQuery("select * from truncate_test") - result.Check(testkit.Rows("1", "2", "3")) - tk.MustExec("truncate table truncate_test") - result = tk.MustQuery("select * from truncate_test") - result.Check(nil) -} - // TestInTxnExecDDLFail tests the following case: // 1. Execute the SQL of "begin"; // 2. A SQL that will fail to execute; @@ -302,80 +288,6 @@ func TestCreateView(t *testing.T) { require.Truef(t, terror.ErrorEqual(err, exeerrors.ErrWrongStringLength), "ERROR 1470 (HY000): String 'host_1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij12345' is too long for host name (should be no longer than 255)") } -func TestViewRecursion(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table if not exists t(a int)") - tk.MustExec("create definer='root'@'localhost' view recursive_view1 as select * from t") - tk.MustExec("create definer='root'@'localhost' view recursive_view2 as select * from recursive_view1") - tk.MustExec("drop table t") - tk.MustExec("rename table recursive_view2 to t") - tk.MustGetDBError("select * from recursive_view1", plannercore.ErrViewRecursive) - tk.MustExec("drop view recursive_view1, t") -} - -func TestIssue16250(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table if not exists t(a int)") - tk.MustExec("create view view_issue16250 as select * from t") - tk.MustGetErrMsg("truncate table view_issue16250", - "[schema:1146]Table 'test.view_issue16250' doesn't exist") -} - -func TestIssue24771(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists zy_tab;`) - tk.MustExec(`create table if not exists zy_tab ( - zy_code int, - zy_name varchar(100) - );`) - tk.MustExec(`drop table if exists bj_tab;`) - tk.MustExec(`create table if not exists bj_tab ( - bj_code int, - bj_name varchar(100), - bj_addr varchar(100), - bj_person_count int, - zy_code int - );`) - tk.MustExec(`drop table if exists st_tab;`) - tk.MustExec(`create table if not exists st_tab ( - st_code int, - st_name varchar(100), - bj_code int - );`) - tk.MustExec(`drop view if exists v_st_2;`) - tk.MustExec(`create definer='root'@'localhost' view v_st_2 as - select st.st_name,bj.bj_name,zy.zy_name - from ( - select bj_code, - bj_name, - zy_code - from bj_tab as b - where b.bj_code = 1 - ) as bj - left join zy_tab as zy on zy.zy_code = bj.zy_code - left join st_tab as st on bj.bj_code = st.bj_code;`) - tk.MustQuery(`show create view v_st_2`) - tk.MustQuery(`select * from v_st_2`) -} - -func TestTruncateSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create sequence if not exists seq") - tk.MustGetErrMsg("truncate table seq", "[schema:1146]Table 'test.seq' doesn't exist") - tk.MustExec("create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle") - tk.MustGetErrMsg("truncate table seq1", "[schema:1146]Table 'test.seq1' doesn't exist") - tk.MustExec("drop sequence if exists seq") - tk.MustExec("drop sequence if exists seq1") -} - func TestCreateViewWithOverlongColName(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -521,16 +433,6 @@ func TestCreateDropView(t *testing.T) { testkit.Rows("def test v SELECT `test`.`t_v2`.`a` AS `a`,`test`.`t_v2`.`b` AS `b` FROM `test`.`t_v2` CASCADED NO @ DEFINER utf8mb4 utf8mb4_bin")) } -func TestCreateDropIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table if not exists drop_test (a int)") - tk.MustExec("create index idx_a on drop_test (a)") - tk.MustExec("drop index idx_a on drop_test") - tk.MustExec("drop table drop_test") -} - func TestAlterTableAddColumn(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1108,43 +1010,6 @@ func TestAutoRandomTableOption(t *testing.T) { require.Contains(t, err.Error(), autoid.AutoRandomRebaseNotApplicable) } -func TestAutoRandomClusteredPrimaryKey(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t (a bigint auto_random(5), b int, primary key (a, b) clustered);") - tk.MustExec("insert into t (b) values (1);") - tk.MustExec("set @@allow_auto_random_explicit_insert = 0;") - tk.MustGetErrCode("insert into t values (100, 2);", errno.ErrInvalidAutoRandom) - tk.MustExec("set @@allow_auto_random_explicit_insert = 1;") - tk.MustExec("insert into t values (100, 2);") - tk.MustQuery("select b from t order by b;").Check(testkit.Rows("1", "2")) - tk.MustExec("alter table t modify column a bigint auto_random(6);") - - tk.MustExec("drop table t;") - tk.MustExec("create table t (a bigint, b bigint auto_random(4, 32), primary key (b, a) clustered)") - tk.MustExec("insert into t (a) values (1);") - tk.MustQuery("select a from t;").Check(testkit.Rows("1")) -} - -func TestMaxHandleAddIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("create table t(a bigint PRIMARY KEY, b int)") - tk.MustExec(fmt.Sprintf("insert into t values(%v, 1)", math.MaxInt64)) - tk.MustExec(fmt.Sprintf("insert into t values(%v, 1)", math.MinInt64)) - tk.MustExec("alter table t add index idx_b(b)") - tk.MustExec("admin check table t") - - tk.MustExec("create table t1(a bigint UNSIGNED PRIMARY KEY, b int)") - tk.MustExec(fmt.Sprintf("insert into t1 values(%v, 1)", uint64(math.MaxUint64))) - tk.MustExec(fmt.Sprintf("insert into t1 values(%v, 1)", 0)) - tk.MustExec("alter table t1 add index idx_b(b)") - tk.MustExec("admin check table t1") -} - func TestSetDDLReorgWorkerCnt(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1332,80 +1197,6 @@ func TestLoadDDLDistributeVars(t *testing.T) { require.Equal(t, disttask.TiDBEnableDistTask, variable.EnableDistTask.Load()) } -// Test issue #9205, fix the precision problem for time type default values -// See https://github.com/pingcap/tidb/issues/9205 for details -func TestIssue9205(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(c time DEFAULT '12:12:12.8');`) - tk.MustQuery("show create table `t`").Check(testkit.RowsWithSep("|", - ""+ - "t CREATE TABLE `t` (\n"+ - " `c` time DEFAULT '12:12:13'\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", - )) - tk.MustExec(`alter table t add column c1 time default '12:12:12.000000';`) - tk.MustQuery("show create table `t`").Check(testkit.RowsWithSep("|", - ""+ - "t CREATE TABLE `t` (\n"+ - " `c` time DEFAULT '12:12:13',\n"+ - " `c1` time DEFAULT '12:12:12'\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", - )) - - tk.MustExec(`alter table t alter column c1 set default '2019-02-01 12:12:10.4';`) - tk.MustQuery("show create table `t`").Check(testkit.RowsWithSep("|", - ""+ - "t CREATE TABLE `t` (\n"+ - " `c` time DEFAULT '12:12:13',\n"+ - " `c1` time DEFAULT '12:12:10'\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", - )) - - tk.MustExec(`alter table t modify c1 time DEFAULT '770:12:12.000000';`) - tk.MustQuery("show create table `t`").Check(testkit.RowsWithSep("|", - ""+ - "t CREATE TABLE `t` (\n"+ - " `c` time DEFAULT '12:12:13',\n"+ - " `c1` time DEFAULT '770:12:12'\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", - )) -} - -func TestCheckDefaultFsp(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - - tk.MustGetErrMsg("create table t ( tt timestamp default now(1));", "[ddl:1067]Invalid default value for 'tt'") - tk.MustGetErrMsg("create table t ( tt timestamp(1) default current_timestamp);", "[ddl:1067]Invalid default value for 'tt'") - tk.MustGetErrMsg("create table t ( tt timestamp(1) default now(2));", "[ddl:1067]Invalid default value for 'tt'") - - tk.MustExec("create table t ( tt timestamp(1) default now(1));") - tk.MustExec("create table t2 ( tt timestamp default current_timestamp());") - tk.MustExec("create table t3 ( tt timestamp default current_timestamp(0));") - - tk.MustGetErrMsg("alter table t add column ttt timestamp default now(2);", "[ddl:1067]Invalid default value for 'ttt'") - tk.MustGetErrMsg("alter table t add column ttt timestamp(5) default current_timestamp;", "[ddl:1067]Invalid default value for 'ttt'") - tk.MustGetErrMsg("alter table t add column ttt timestamp(5) default now(2);", "[ddl:1067]Invalid default value for 'ttt'") - tk.MustGetErrMsg("alter table t modify column tt timestamp(1) default now();", "[ddl:1067]Invalid default value for 'tt'") - tk.MustGetErrMsg("alter table t modify column tt timestamp(4) default now(5);", "[ddl:1067]Invalid default value for 'tt'") - tk.MustGetErrMsg("alter table t change column tt tttt timestamp(4) default now(5);", "[ddl:1067]Invalid default value for 'tttt'") - tk.MustGetErrMsg("alter table t change column tt tttt timestamp(1) default now();", "[ddl:1067]Invalid default value for 'tttt'") -} - -func TestTimestampMinDefaultValue(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists tdv;") - tk.MustExec("create table tdv(a int);") - tk.MustExec("ALTER TABLE tdv ADD COLUMN ts timestamp DEFAULT '1970-01-01 08:00:01';") -} - // this test will change the fail-point `mockAutoIDChange`, so we move it to the `testRecoverTable` suite func TestRenameTable(t *testing.T) { require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange", `return(true)`)) @@ -1549,104 +1340,6 @@ func TestRenameMultiTables(t *testing.T) { tk.MustExec("drop database rename3") } -func TestCreateTableWithTTL(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("CREATE TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 5 DAY */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */")) - tk.MustExec("DROP TABLE t") - - tk.MustGetErrMsg("CREATE TABLE t (id int) TTL = `id` + INTERVAL 5 DAY", "[ddl:8148]Field 'id' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP") - - tk.MustGetErrMsg("CREATE TABLE t (id int) TTL_ENABLE = 'ON'", "[ddl:8150]Cannot set TTL_ENABLE on a table without TTL config") - - tk.MustGetErrMsg("CREATE TABLE t (id int) TTL_JOB_INTERVAL = '1h'", "[ddl:8150]Cannot set TTL_JOB_INTERVAL on a table without TTL config") - - tk.MustExec("CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL_ENABLE = 'OFF' TTL_JOB_INTERVAL = '1d'") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */")) - tk.MustExec("DROP TABLE t") - - // when multiple ttl and ttl_enable configs are submitted, only the last one will be handled - tk.MustExec("CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL = `created_at` + INTERVAL 2 DAY TTL = `created_at` + INTERVAL 3 DAY TTL_ENABLE = 'OFF'") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */")) - tk.MustExec("DROP TABLE t") -} - -func TestAlterTTLInfo(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("CREATE TABLE t (created_at datetime, updated_at datetime, wrong_type int) TTL = `created_at` + INTERVAL 5 DAY") - tk.MustExec("ALTER TABLE t TTL = `updated_at` + INTERVAL 2 YEAR") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at` datetime DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */")) - - tk.MustExec("ALTER TABLE t TTL_ENABLE = 'OFF'") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at` datetime DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */")) - - tk.MustExec("ALTER TABLE t TTL_JOB_INTERVAL = '1d'") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at` datetime DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */")) - - tk.MustGetErrMsg("ALTER TABLE t TTL = `not_exist` + INTERVAL 2 YEAR", "[ddl:1054]Unknown column 'not_exist' in 'TTL config'") - - tk.MustGetErrMsg("ALTER TABLE t TTL = `wrong_type` + INTERVAL 2 YEAR", "[ddl:8148]Field 'wrong_type' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP") - - tk.MustGetErrMsg("ALTER TABLE t DROP COLUMN updated_at", "[ddl:8149]Cannot drop column 'updated_at': needed in TTL config") - tk.MustGetErrMsg("ALTER TABLE t CHANGE updated_at updated_at_new INT", "[ddl:8148]Field 'updated_at_new' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP") - - tk.MustExec("ALTER TABLE t RENAME COLUMN `updated_at` TO `updated_at_2`") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at_2` datetime DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_2` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */")) - - tk.MustExec("ALTER TABLE t CHANGE `updated_at_2` `updated_at_3` date") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at_3` date DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_3` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */")) - - tk.MustExec("ALTER TABLE t TTL = `updated_at_3` + INTERVAL 3 YEAR") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at_3` date DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_3` + INTERVAL 3 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */")) - - tk.MustGetErrMsg("ALTER TABLE t TTL_ENABLE = 'OFF' REMOVE TTL", "[ddl:8200]Unsupported multi schema change for alter table ttl") - - tk.MustExec("ALTER TABLE t REMOVE TTL") - tk.MustQuery("SHOW CREATE TABLE t").Check(testkit.Rows("t CREATE TABLE `t` (\n `created_at` datetime DEFAULT NULL,\n `updated_at_3` date DEFAULT NULL,\n `wrong_type` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - - tk.MustGetErrMsg("ALTER TABLE t TTL_ENABLE = 'OFF'", "[ddl:8150]Cannot set TTL_ENABLE on a table without TTL config") - - tk.MustGetErrMsg("ALTER TABLE t TTL_JOB_INTERVAL = '1h'", "[ddl:8150]Cannot set TTL_JOB_INTERVAL on a table without TTL config") -} - -func TestDisableTTLForTempTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustGetDBError("CREATE TEMPORARY TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY", dbterror.ErrTempTableNotAllowedWithTTL) -} - -func TestDisableTTLForFKParentTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // alter ttl for a FK parent table is not allowed - tk.MustExec("set global tidb_enable_foreign_key='ON'") - tk.MustExec("CREATE TABLE t (id int primary key, created_at datetime)") - tk.MustExec("CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id))") - tk.MustGetDBError("ALTER TABLE t TTL = created_at + INTERVAL 5 YEAR", dbterror.ErrUnsupportedTTLReferencedByFK) - tk.MustExec("drop table t,t_1") - - // refuse to reference TTL key when create table - tk.MustExec("CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR") - tk.MustGetDBError("CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id))", dbterror.ErrUnsupportedTTLReferencedByFK) - tk.MustExec("drop table t") - - // refuse to add foreign key reference TTL table - tk.MustExec("CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR") - tk.MustExec("CREATE TABLE t_1 (t_id int)") - tk.MustGetDBError("ALTER TABLE t_1 ADD FOREIGN KEY fk_t_id(t_id) references t(id)", dbterror.ErrUnsupportedTTLReferencedByFK) - tk.MustExec("drop table t,t_1") -} - func TestCheckPrimaryKeyForTTLTable(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/executor/test/splittest/BUILD.bazel b/executor/test/splittest/BUILD.bazel index 86bfff2e02849..9991491295297 100644 --- a/executor/test/splittest/BUILD.bazel +++ b/executor/test/splittest/BUILD.bazel @@ -9,7 +9,7 @@ go_test( ], flaky = True, race = "on", - shard_count = 6, + shard_count = 5, deps = [ "//ddl", "//domain/infosync", diff --git a/executor/test/splittest/split_table_test.go b/executor/test/splittest/split_table_test.go index cec687ec40fad..3f86a84daf967 100644 --- a/executor/test/splittest/split_table_test.go +++ b/executor/test/splittest/split_table_test.go @@ -111,20 +111,6 @@ func TestSplitTableRegion(t *testing.T) { tk.MustQuery("split region for partition table t partition (p3,p4) between (100000000) and (1000000000) regions 5;").Check(testkit.Rows("8 1")) } -func TestSplitRegionEdgeCase(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a bigint(20) auto_increment primary key);") - tk.MustExec("split table t between (-9223372036854775808) and (9223372036854775807) regions 16;") - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int(20) auto_increment primary key);") - tk.MustGetErrCode("split table t between (-9223372036854775808) and (9223372036854775807) regions 16;", errno.ErrDataOutOfRange) -} - func TestClusterIndexSplitTableIntegration(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/executor/test/writetest/BUILD.bazel b/executor/test/writetest/BUILD.bazel index ecd4cde42a260..260a9a05c4e8a 100644 --- a/executor/test/writetest/BUILD.bazel +++ b/executor/test/writetest/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "write_test.go", ], flaky = True, - shard_count = 50, + shard_count = 44, deps = [ "//br/pkg/lightning/mydump", "//config", diff --git a/executor/test/writetest/write_test.go b/executor/test/writetest/write_test.go index ab0069f356d8a..06d7065ef7107 100644 --- a/executor/test/writetest/write_test.go +++ b/executor/test/writetest/write_test.go @@ -326,19 +326,6 @@ func TestInsert(t *testing.T) { require.EqualError(t, err, "[kv:1062]Duplicate entry '\xe6\xb5' for key 't.PRIMARY'") } -func TestMultiBatch(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t,t0") - tk.MustExec("create table t0 (i int)") - tk.MustExec("insert into t0 values (1), (1)") - tk.MustExec("create table t (i int unique key)") - tk.MustExec("set @@tidb_dml_batch_size = 1") - tk.MustExec("insert ignore into t select * from t0") - tk.MustExec("admin check table t") -} - func TestInsertAutoInc(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -831,143 +818,6 @@ func TestInsertIgnoreOnDup(t *testing.T) { tk.MustQuery("select * from t6").Check(testkit.Rows("100 10 1000")) } -func TestInsertSetWithDefault(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - // Assign `DEFAULT` in `INSERT ... SET ...` statement - tk.MustExec("drop table if exists t1, t2;") - tk.MustExec("create table t1 (a int default 10, b int default 20);") - tk.MustExec("insert into t1 set a=default;") - tk.MustQuery("select * from t1;").Check(testkit.Rows("10 20")) - tk.MustExec("delete from t1;") - tk.MustExec("insert into t1 set b=default;") - tk.MustQuery("select * from t1;").Check(testkit.Rows("10 20")) - tk.MustExec("delete from t1;") - tk.MustExec("insert into t1 set b=default, a=1;") - tk.MustQuery("select * from t1;").Check(testkit.Rows("1 20")) - tk.MustExec("delete from t1;") - tk.MustExec("insert into t1 set a=default(a);") - tk.MustQuery("select * from t1;").Check(testkit.Rows("10 20")) - tk.MustExec("delete from t1;") - tk.MustExec("insert into t1 set a=default(b), b=default(a)") - tk.MustQuery("select * from t1;").Check(testkit.Rows("20 10")) - tk.MustExec("delete from t1;") - tk.MustExec("insert into t1 set a=default(b)+default(a);") - tk.MustQuery("select * from t1;").Check(testkit.Rows("30 20")) - // With generated columns - tk.MustExec("create table t2 (a int default 10 primary key, b int generated always as (-a) virtual, c int generated always as (-a) stored);") - tk.MustExec("insert into t2 set a=default;") - tk.MustQuery("select * from t2;").Check(testkit.Rows("10 -10 -10")) - tk.MustExec("delete from t2;") - tk.MustExec("insert into t2 set a=2, b=default;") - tk.MustQuery("select * from t2;").Check(testkit.Rows("2 -2 -2")) - tk.MustExec("delete from t2;") - tk.MustExec("insert into t2 set c=default, a=3;") - tk.MustQuery("select * from t2;").Check(testkit.Rows("3 -3 -3")) - tk.MustExec("delete from t2;") - tk.MustExec("insert into t2 set a=default, b=default, c=default;") - tk.MustQuery("select * from t2;").Check(testkit.Rows("10 -10 -10")) - tk.MustExec("delete from t2;") - tk.MustExec("insert into t2 set a=default(a), b=default, c=default;") - tk.MustQuery("select * from t2;").Check(testkit.Rows("10 -10 -10")) - tk.MustExec("delete from t2;") - // Looks like MySQL accepts this, but still the inserted value would be default(b) i.e. ignored - tk.MustGetErrCode("insert into t2 set b=default(a);", mysql.ErrBadGeneratedColumn) - // Looks like MySQL accepts this, but inserted values are all NULL - tk.MustGetErrCode("insert into t2 set a=default(b), b=default(b);", mysql.ErrBadGeneratedColumn) - tk.MustExec("insert into t2 set a=default(a), c=default(c)") - tk.MustGetErrCode("insert into t2 set a=default(a), c=default(a);", mysql.ErrBadGeneratedColumn) - tk.MustExec("insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b)") - // This fails most likely due only the generated column is updated -> no change -> duplicate key? - // Too odd to create a bug, better to have it documented by this test instead... - tk.MustGetErrCode("insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b)", mysql.ErrDupEntry) - tk.MustGetErrCode("insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(a)", mysql.ErrBadGeneratedColumn) - tk.MustQuery("select * from t2").Sort().Check(testkit.Rows("10 -10 -10", "3 -3 -3")) - tk.MustExec("drop table t1, t2") - // Issue 29926 - tk.MustExec("create table t1 (a int not null auto_increment,primary key(a), t timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)") - defer tk.MustExec("drop table if exists t1") - tk.MustExec("set @@timestamp = 1637541064") - defer tk.MustExec("set @@timestamp = DEFAULT") - tk.MustExec("insert into t1 set a=default,t=default") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustExec("set @@timestamp = 1637541082") - tk.MustExec("insert into t1 VALUES (default,default)") - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows( - "1 2021-11-22 08:31:04", - "2 2021-11-22 08:31:22")) - tk.MustExec("set @@timestamp = 1637541332") - tk.MustExec("insert into t1 set a=1,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustExec("insert into t1 set a=2,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default(t)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows( - "1 2021-11-22 08:35:32", - "2 2021-11-22 08:35:32")) - tk.MustExec(`DROP TABLE t1`) - tk.MustExec(`CREATE TABLE t1 (a int default 1 PRIMARY KEY, b int default 2)`) - tk.MustExec(`INSERT INTO t1 VALUES (2,2), (3,3)`) - tk.MustExec(`INSERT INTO t1 VALUES (3,2) ON DUPLICATE KEY UPDATE b = DEFAULT(a)`) - tk.MustExec(`INSERT INTO t1 SET a = 2, b = 3 ON DUPLICATE KEY UPDATE b = DEFAULT(a)`) - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows( - "2 1", - "3 1")) -} - -func TestInsertOnDupUpdateDefault(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - // Assign `DEFAULT` in `INSERT ... ON DUPLICATE KEY UPDATE ...` statement - tk.MustExec("drop table if exists t1, t2;") - tk.MustExec("create table t1 (a int unique, b int default 20, c int default 30);") - tk.MustExec("insert into t1 values (1,default,default);") - tk.MustExec("insert into t1 values (1,default,default) on duplicate key update b=default;") - tk.MustQuery("select * from t1;").Check(testkit.Rows("1 20 30")) - tk.MustExec("insert into t1 values (1,default,default) on duplicate key update c=default, b=default;") - tk.MustQuery("select * from t1;").Check(testkit.Rows("1 20 30")) - tk.MustExec("insert into t1 values (1,default,default) on duplicate key update c=default, a=2") - tk.MustQuery("select * from t1;").Check(testkit.Rows("2 20 30")) - tk.MustExec("insert into t1 values (2,default,default) on duplicate key update c=default(b)") - tk.MustQuery("select * from t1;").Check(testkit.Rows("2 20 20")) - tk.MustExec("insert into t1 values (2,default,default) on duplicate key update a=default(b)+default(c)") - tk.MustQuery("select * from t1;").Check(testkit.Rows("50 20 20")) - // With generated columns - tk.MustExec("create table t2 (a int unique, b int generated always as (-a) virtual, c int generated always as (-a) stored);") - tk.MustExec("insert into t2 values (1,default,default);") - tk.MustExec("insert into t2 values (1,default,default) on duplicate key update a=2, b=default;") - tk.MustQuery("select * from t2").Check(testkit.Rows("2 -2 -2")) - tk.MustExec("insert into t2 values (2,default,default) on duplicate key update a=3, c=default;") - tk.MustQuery("select * from t2").Check(testkit.Rows("3 -3 -3")) - tk.MustExec("insert into t2 values (3,default,default) on duplicate key update c=default, b=default, a=4;") - tk.MustQuery("select * from t2").Check(testkit.Rows("4 -4 -4")) - tk.MustExec("insert into t2 values (4,default,default) on duplicate key update b=default, a=5, c=default;") - tk.MustQuery("select * from t2").Check(testkit.Rows("5 -5 -5")) - tk.MustGetErrCode("insert into t2 values (5,default,default) on duplicate key update b=default(a);", mysql.ErrBadGeneratedColumn) - tk.MustExec("insert into t2 values (5,default,default) on duplicate key update a=default(a), c=default(c)") - tk.MustQuery("select * from t2").Check(testkit.Rows(" ")) - tk.MustExec("delete from t2") - tk.MustExec("insert into t2 (a) values (1);") - tk.MustExec("insert into t2 values (1,default,default) on duplicate key update a=default(b), b=default(b);") - tk.MustQuery("select * from t2").Check(testkit.Rows(" ")) - tk.MustExec("delete from t2") - tk.MustExec("insert into t2 (a) values (1);") - tk.MustGetErrCode("insert into t2 values (1,default,default) on duplicate key update a=default(a), c=default(a);", mysql.ErrBadGeneratedColumn) - tk.MustExec("drop table t1, t2") - - tk.MustExec("set @@tidb_txn_mode = 'pessimistic'") - tk.MustExec("create table t ( c_int int, c_string varchar(40) collate utf8mb4_bin , primary key (c_string), unique key (c_int));") - tk.MustExec("insert into t values (22, 'gold witch'), (24, 'gray singer'), (21, 'silver sight');") - tk.MustExec("begin;") - err := tk.ExecToErr("insert into t values (21,'black warlock'), (22, 'dark sloth'), (21, 'cyan song') on duplicate key update c_int = c_int + 1, c_string = concat(c_int, ':', c_string);") - require.True(t, kv.ErrKeyExists.Equal(err)) - tk.MustExec("commit;") - tk.MustQuery("select * from t order by c_int;").Check(testkit.RowsWithSep("|", "21|silver sight", "22|gold witch", "24|gray singer")) - tk.MustExec("drop table t;") -} - func TestReplace(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1150,67 +1000,6 @@ func TestReplace(t *testing.T) { tk.MustExec("drop table t1, t2") } -func TestReplaceWithCICollation(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("create table t (a varchar(20) charset utf8mb4 collate utf8mb4_general_ci primary key);") - tk.MustExec("replace into t(a) values (_binary'A '),(_binary'A');") - tk.MustQuery("select a from t use index(primary);").Check(testkit.Rows("A")) - tk.MustQuery("select a from t ignore index(primary);").Check(testkit.Rows("A")) -} - -func TestGeneratedColumnForInsert(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // test cases for default behavior - tk.MustExec(`drop table if exists t1;`) - tk.MustExec(`create table t1(id int, id_gen int as(id + 42), b int, unique key id_gen(id_gen));`) - tk.MustExec(`insert into t1 (id, b) values(1,1),(2,2),(3,3),(4,4),(5,5);`) - tk.MustExec(`replace into t1 (id, b) values(1,1);`) - tk.MustExec(`replace into t1 (id, b) values(1,1),(2,2);`) - tk.MustExec(`replace into t1 (id, b) values(6,16),(7,17),(8,18);`) - tk.MustQuery("select * from t1;").Check(testkit.Rows( - "1 43 1", "2 44 2", "3 45 3", "4 46 4", "5 47 5", "6 48 16", "7 49 17", "8 50 18")) - tk.MustExec(`insert into t1 (id, b) values (6,18) on duplicate key update id = -id;`) - tk.MustExec(`insert into t1 (id, b) values (7,28) on duplicate key update b = -values(b);`) - tk.MustQuery("select * from t1;").Check(testkit.Rows( - "1 43 1", "2 44 2", "3 45 3", "4 46 4", "5 47 5", "-6 36 16", "7 49 -28", "8 50 18")) - - // test cases for virtual and stored columns in the same table - tk.MustExec(`drop table if exists t`) - tk.MustExec(`create table t - (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i), unique key idx_j(j))`) - tk.MustExec(`insert into t (k) values (1), (2)`) - tk.MustExec(`replace into t (k) values (1), (2)`) - tk.MustQuery(`select * from t`).Check(testkit.Rows("2 3 1", "3 4 2")) - - tk.MustExec(`drop table if exists t`) - tk.MustExec(`create table t - (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_j(j))`) - tk.MustExec(`insert into t (k) values (1), (2)`) - tk.MustExec(`replace into t (k) values (1), (2)`) - tk.MustQuery(`select * from t`).Check(testkit.Rows("2 3 1", "3 4 2")) - - tk.MustExec(`drop table if exists t`) - tk.MustExec(`create table t - (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i))`) - tk.MustExec(`insert into t (k) values (1), (2)`) - tk.MustExec(`replace into t (k) values (1), (2)`) - tk.MustQuery(`select * from t`).Check(testkit.Rows("2 3 1", "3 4 2")) - - // For issue 14340 - tk.MustExec(`drop table if exists t1`) - tk.MustExec(`create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1))))`) - tk.MustGetErrMsg(`INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON))`, "[types:1690]DECIMAL value is out of range in '(2, 1)'") - tk.MustExec(`set @@sql_mode = ''`) - tk.MustExec(`INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON))`) - tk.MustQuery(`select * from t1`).Check(testkit.Rows("1000 9.9")) -} - // TestUpdateCastOnlyModifiedValues for issue #4514. func TestUpdateCastOnlyModifiedValues(t *testing.T) { store := testkit.CreateMockStore(t) @@ -1561,29 +1350,6 @@ func TestIssue34358(t *testing.T) { }, ld, t, tk, ctx, "select * from load_data_test", "delete from load_data_test") } -func TestNullDefault(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test; drop table if exists test_null_default;") - tk.MustExec("set timestamp = 1234") - tk.MustExec("set time_zone = '+08:00'") - tk.MustExec("create table test_null_default (ts timestamp null default current_timestamp)") - tk.MustExec("insert into test_null_default values (null)") - tk.MustQuery("select * from test_null_default").Check(testkit.Rows("")) - tk.MustExec("insert into test_null_default values ()") - tk.MustQuery("select * from test_null_default").Check(testkit.Rows("", "1970-01-01 08:20:34")) -} - -func TestNotNullDefault(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test; drop table if exists t1,t2;") - defer tk.MustExec("drop table t1,t2") - tk.MustExec("create table t1 (a int not null default null default 1);") - tk.MustExec("create table t2 (a int);") - tk.MustExec("alter table t2 change column a a int not null default null default 1;") -} - func TestLatch(t *testing.T) { store, err := mockstore.NewMockStore( // Small latch slot size to make conflicts. @@ -1644,158 +1410,6 @@ func TestLatch(t *testing.T) { tk1.MustExec("commit") } -// TestIssue4067 Test issue https://github.com/pingcap/tidb/issues/4067 -func TestIssue4067(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1, t2") - tk.MustExec("create table t1(id int)") - tk.MustExec("create table t2(id int)") - tk.MustExec("insert into t1 values(123)") - tk.MustExec("insert into t2 values(123)") - tk.MustExec("delete from t1 where id not in (select id from t2)") - tk.MustQuery("select * from t1").Check(testkit.Rows("123")) - tk.MustExec("delete from t1 where id in (select id from t2)") - tk.MustQuery("select * from t1").Check(nil) -} - -func TestInsertCalculatedValue(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b int)") - tk.MustExec("insert into t set a=1, b=a+1") - tk.MustQuery("select a, b from t").Check(testkit.Rows("1 2")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int default 100, b int)") - tk.MustExec("insert into t set b=a+1, a=1") - tk.MustQuery("select a, b from t").Check(testkit.Rows("1 101")) - tk.MustExec("insert into t (b) value (a)") - tk.MustQuery("select * from t where b = 100").Check(testkit.Rows("100 100")) - tk.MustExec("insert into t set a=2, b=a+1") - tk.MustQuery("select * from t where a = 2").Check(testkit.Rows("2 3")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (c int)") - tk.MustExec("insert into test.t set test.t.c = '1'") - tk.MustQuery("select * from t").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int default 1)") - tk.MustExec("insert into t values (a)") - tk.MustQuery("select * from t").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int, d int)") - tk.MustExec("insert into t value (1, 2, a+1, b+1)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2 2 3")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int not null)") - tk.MustExec("insert into t values (a+2)") - tk.MustExec("insert into t values (a)") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("0", "2")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a bigint not null, b bigint not null)") - tk.MustExec("insert into t value(b + 1, a)") - tk.MustExec("insert into t set a = b + a, b = a + 1") - tk.MustExec("insert into t value(1000, a)") - tk.MustExec("insert t set b = sqrt(a + 4), a = 10") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("0 1", "1 1", "10 2", "1000 1000")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int)") - tk.MustExec("insert into t values(a)") - tk.MustQuery("select * from t").Check(testkit.Rows("")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a enum('a', 'b'))") - tk.MustExec("insert into t values(a)") - tk.MustQuery("select * from t").Check(testkit.Rows("")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a enum('a', 'b') default 'a')") - tk.MustExec("insert into t values(a)") - tk.MustExec("insert into t values(a+1)") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("a", "b")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a blob)") - tk.MustExec("insert into t values(a)") - tk.MustQuery("select * from t").Check(testkit.Rows("")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a varchar(20) default 'a')") - tk.MustExec("insert into t values(a)") - tk.MustExec("insert into t values(upper(a))") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("A", "a")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a varchar(20) not null, b varchar(20))") - tk.MustExec("insert into t value (a, b)") - tk.MustQuery("select * from t").Check(testkit.Rows(" ")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b int)") - tk.MustExec("insert into t values(a*b, b*b)") - tk.MustQuery("select * from t").Check(testkit.Rows(" ")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a json not null, b int)") - // TODO: MySQL reports 3156 instead of ErrTruncatedWrongValueForField. - tk.MustGetErrCode("insert into t value (a,a->'$')", mysql.ErrTruncatedWrongValueForField) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a json not null, b varchar(10))") - tk.MustExec("insert into t value (a,a->'$')") - tk.MustQuery("select * from t").Check(testkit.Rows("null null")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a json, b int, c int as (a->'$.a'))") - tk.MustExec("insert into t (a, b) value (a, a->'$.a'+1)") - tk.MustExec("insert into t (b) value (a->'$.a'+1)") - tk.MustQuery("select * from t").Check(testkit.Rows(" ", " ")) - tk.MustExec(`insert into t (a, b) value ('{"a": 1}', a->'$.a'+1)`) - tk.MustQuery("select * from t where c = 1").Check(testkit.Rows(`{"a": 1} 2 1`)) - tk.MustExec("truncate table t") - tk.MustExec("insert t set b = c + 1") - tk.MustQuery("select * from t").Check(testkit.Rows(" ")) - tk.MustExec("truncate table t") - tk.MustExec(`insert t set a = '{"a": 1}', b = c`) - tk.MustQuery("select * from t").Check(testkit.Rows(`{"a": 1} 1`)) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int auto_increment key, b int)") - tk.MustExec("insert into t (b) value (a)") - tk.MustExec("insert into t value (a, a+1)") - tk.MustExec("set SQL_MODE=NO_AUTO_VALUE_ON_ZERO") - tk.MustExec("insert into t (b) value (a+1)") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("1 0", "2 1", "3 1")) - - tk.MustExec("set SQL_MODE=STRICT_ALL_TABLES") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int not null, b int, c int as (sqrt(a)))") - tk.MustExec("insert t set b = a, a = 4") - tk.MustQuery("select * from t").Check(testkit.Rows("4 0 2")) -} - -func TestDataTooLongErrMsg(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t(a varchar(2));") - err := tk.ExecToErr("insert into t values('123');") - require.True(t, types.ErrDataTooLong.Equal(err)) - require.EqualError(t, err, "[types:1406]Data too long for column 'a' at row 1") - tk.MustExec("insert into t values('12')") - err = tk.ExecToErr("update t set a = '123' where a = '12';") - require.True(t, types.ErrDataTooLong.Equal(err)) - require.EqualError(t, err, "[types:1406]Data too long for column 'a' at row 1") -} - func TestUpdateSelect(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1917,55 +1531,6 @@ func TestRebaseIfNeeded(t *testing.T) { tk.MustQuery(`select a from t where b = 6;`).Check(testkit.Rows("30003")) } -func TestDeferConstraintCheckForDelete(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("set tidb_constraint_check_in_place = 0") - tk.MustExec("set @@tidb_txn_mode = 'optimistic'") - tk.MustExec("use test") - - tk.MustExec("drop table if exists t1, t2, t3, t4, t5") - tk.MustExec("create table t1(i int primary key, j int)") - tk.MustExec("insert into t1 values(1, 2)") - tk.MustExec("begin") - tk.MustExec("insert into t1 values(1, 3)") - tk.MustExec("delete from t1 where j = 3") - tk.MustGetErrMsg("commit", "previous statement: delete from t1 where j = 3: [kv:1062]Duplicate entry '1' for key 't1.PRIMARY'") - tk.MustExec("rollback") - - tk.MustExec("create table t2(i int, j int, unique index idx(i))") - tk.MustExec("insert into t2 values(1, 2)") - tk.MustExec("begin") - tk.MustExec("insert into t2 values(1, 3)") - tk.MustExec("delete from t2 where j = 3") - tk.MustGetErrMsg("commit", "previous statement: delete from t2 where j = 3: [kv:1062]Duplicate entry '1' for key 't2.idx'") - tk.MustExec("admin check table t2") - - tk.MustExec("create table t3(i int, j int, primary key(i))") - tk.MustExec("begin") - tk.MustExec("insert into t3 values(1, 3)") - tk.MustExec("delete from t3 where j = 3") - tk.MustExec("commit") - - tk.MustExec("create table t4(i int, j int, primary key(i))") - tk.MustExec("begin") - tk.MustExec("insert into t4 values(1, 3)") - tk.MustExec("delete from t4 where j = 3") - tk.MustExec("insert into t4 values(2, 3)") - tk.MustExec("commit") - tk.MustExec("admin check table t4") - tk.MustQuery("select * from t4").Check(testkit.Rows("2 3")) - - tk.MustExec("create table t5(i int, j int, primary key(i))") - tk.MustExec("begin") - tk.MustExec("insert into t5 values(1, 3)") - tk.MustExec("delete from t5 where j = 3") - tk.MustExec("insert into t5 values(1, 4)") - tk.MustExec("commit") - tk.MustExec("admin check table t5") - tk.MustQuery("select * from t5").Check(testkit.Rows("1 4")) -} - func TestDeferConstraintCheckForInsert(t *testing.T) { store := testkit.CreateMockStore(t) setTxnTk := testkit.NewTestKit(t, store) @@ -2121,221 +1686,6 @@ func TestPessimisticDeleteYourWrites(t *testing.T) { session2.MustQuery("select * from x").Check(testkit.Rows("1 2")) } -func TestDefEnumInsert(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table test (id int, prescription_type enum('a','b','c','d','e','f') NOT NULL, primary key(id));") - tk.MustExec("insert into test (id) values (1)") - tk.MustQuery("select prescription_type from test").Check(testkit.Rows("a")) -} - -func TestIssue11059(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t (pk int primary key, uk int unique, v int)") - tk.MustExec("insert into t values (2, 11, 215)") - tk.MustExec("insert into t values (3, 7, 2111)") - err := tk.ExecToErr("update t set pk = 2 where uk = 7") - require.Error(t, err) -} - -func TestSetWithRefGenCol(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`create table t (i int, j int as (i+1) not null);`) - tk.MustExec(`insert into t set i = j + 1;`) - tk.MustQuery("select * from t").Check(testkit.Rows("1 2")) - tk.MustExec(`insert into t set i = j + 100;`) - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "100 101")) - - tk.MustExec(`create table te (i int)`) - tk.MustExec(`insert into te set i = i + 10;`) - tk.MustQuery("select * from te").Check(testkit.Rows("")) - tk.MustExec(`insert into te set i = i;`) - tk.MustQuery("select * from te").Check(testkit.Rows("", "")) - - tk.MustExec(`create table tn (i int not null)`) - tk.MustExec(`insert into tn set i = i;`) - tk.MustQuery("select * from tn").Check(testkit.Rows("0")) - tk.MustExec(`insert into tn set i = i + 10;`) - tk.MustQuery("select * from tn").Check(testkit.Rows("0", "10")) - - // - tk.MustExec(`create table t1 (j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '10');`) - tk.MustExec(`insert into t1 values()`) - tk.MustQuery("select * from t1").Check(testkit.Rows("11 10")) - tk.MustExec(`insert into t1 values()`) - tk.MustQuery("select * from t1").Check(testkit.Rows("11 10", "11 10")) - - tk.MustExec(`create table t2 (j int(11) GENERATED ALWAYS AS (i + 1) stored not null, i int(11) DEFAULT '5');`) - tk.MustExec(`insert into t2 set i = j + 9`) - tk.MustQuery("select * from t2").Check(testkit.Rows("10 9")) - err := tk.ExecToErr(`insert into t2 set j = i + 1`) - require.Error(t, err) - tk.MustExec(`insert into t2 set i = j + 100`) - tk.MustQuery("select * from t2").Check(testkit.Rows("10 9", "101 100")) - - tk.MustExec(`create table t3(j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '5');`) - tk.MustExec(`insert into t3 set i = j + 100`) - tk.MustQuery("select * from t3").Check(testkit.Rows(" ")) - err = tk.ExecToErr(`insert into t3 set j = i + 1`) - require.Error(t, err) -} - -func TestSetWithCurrentTimestampAndNow(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists tbl;`) - tk.MustExec(`create table t1(c1 timestamp default current_timestamp, c2 int, c3 timestamp default current_timestamp);`) - // c1 insert using now() function result, c3 using default value calculation, should be same - tk.MustExec(`insert into t1 set c1 = current_timestamp, c2 = sleep(2);`) - tk.MustQuery("select c1 = c3 from t1").Check(testkit.Rows("1")) - tk.MustExec(`insert into t1 set c1 = current_timestamp, c2 = sleep(1);`) - tk.MustQuery("select c1 = c3 from t1").Check(testkit.Rows("1", "1")) -} - -func TestApplyWithPointAndBatchPointGet(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t ( c_int int, c_str varchar(40),c_datetime datetime, c_timestamp timestamp, -c_double double, c_decimal decimal(12, 6) , primary key(c_int, c_str) , unique key(c_int) , unique key(c_str) , -unique key(c_decimal) , unique key(c_datetime) , key(c_timestamp) );`) - tk.MustExec(`insert into t values (1, 'zen ardinghelli', '2020-02-03 18:15:17', '2020-03-11 05:47:11', 36.226534, 3.763), -(2, 'suspicious joliot', '2020-01-01 22:56:37', '2020-04-07 06:19:07', 62.756537, 5.567), -(3, 'keen zhukovsky', '2020-01-21 04:09:20', '2020-06-06 08:32:14', 33.115065, 1.381), -(4, 'crazy newton', '2020-02-14 21:37:56', '2020-04-28 08:33:48', 44.146318, 4.249), -(5, 'happy black', '2020-03-12 16:04:14', '2020-01-18 09:17:37', 41.962653, 5.959);`) - tk.MustExec(`insert into t values (6, 'vigilant swartz', '2020-06-01 07:37:44', '2020-05-25 01:26:43', 56.352233, 2.202), -(7, 'suspicious germain', '2020-04-16 23:25:23', '2020-03-17 05:06:57', 55.897698, 3.460), -(8, 'festive chandrasekhar', '2020-02-11 23:40:29', '2020-04-08 10:13:04', 77.565691, 0.540), -(9, 'vigorous meninsky', '2020-02-17 10:03:17', '2020-01-02 15:02:02', 6.484815, 6.292), -(10, 'heuristic moser', '2020-04-20 12:18:49', '2020-06-20 20:20:18', 28.023822, 2.765);`) - tk.MustExec(`insert into t values (11, 'sharp carver', '2020-03-01 11:23:41', '2020-03-23 17:59:05', 40.842442, 6.345), -(12, 'trusting noether', '2020-03-28 06:42:34', '2020-01-27 15:33:40', 49.544658, 4.811), -(13, 'objective ishizaka', '2020-01-28 17:30:55', '2020-04-02 17:45:39', 59.523930, 5.015), -(14, 'sad rhodes', '2020-03-30 21:43:37', '2020-06-09 06:53:53', 87.295753, 2.413), -(15, 'wonderful shockley', '2020-04-29 09:17:11', '2020-03-14 04:36:51', 6.778588, 8.497);`) - tk.MustExec("begin pessimistic") - tk.MustExec(`insert into t values (13, 'vibrant yalow', '2020-05-15 06:59:05', '2020-05-03 05:58:45', 43.721929, 8.066), -(14, 'xenodochial spence', '2020-02-13 17:28:07', '2020-04-01 12:18:30', 19.981331, 5.774), -(22, 'eloquent neumann', '2020-02-10 16:00:20', '2020-03-28 00:24:42', 10.702532, 7.618) -on duplicate key update c_int=values(c_int), c_str=values(c_str), c_double=values(c_double), c_timestamp=values(c_timestamp);`) - // Test pointGet. - tk.MustQuery(`select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls -from t order by c_str;`).Check(testkit.Rows("10")) - // Test batchPointGet - tk.MustQuery(`select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls -from t order by c_str;`).Check(testkit.Rows("10")) - tk.MustExec("commit") - tk.MustQuery(`select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls -from t order by c_str;`).Check(testkit.Rows("10")) - // Test batchPointGet - tk.MustQuery(`select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls -from t order by c_str;`).Check(testkit.Rows("10")) -} - -func TestWriteListPartitionTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec("drop table if exists t") - tk.MustExec(`create table t (id int, name varchar(10), unique index idx (id)) partition by list (id) ( - partition p0 values in (3,5,6,9,17), - partition p1 values in (1,2,10,11,19,20), - partition p2 values in (4,12,13,14,18), - partition p3 values in (7,8,15,16,null) - );`) - - // Test insert,update,delete - tk.MustExec("insert into t values (1, 'a')") - tk.MustExec("update t set name='b' where id=2;") - tk.MustQuery("select * from t").Check(testkit.Rows("1 a")) - tk.MustExec("update t set name='b' where id=1;") - tk.MustQuery("select * from t").Check(testkit.Rows("1 b")) - tk.MustExec("replace into t values (1, 'c')") - tk.MustQuery("select * from t").Check(testkit.Rows("1 c")) - tk.MustExec("insert into t values (1, 'd') on duplicate key update name='e'") - tk.MustQuery("select * from t").Check(testkit.Rows("1 e")) - tk.MustExec("delete from t where id=1") - tk.MustQuery("select * from t").Check(testkit.Rows()) - tk.MustExec("insert into t values (2, 'f')") - tk.MustExec("delete from t where name='f'") - tk.MustQuery("select * from t").Check(testkit.Rows()) - - // Test insert error - tk.MustExec("insert into t values (1, 'a')") - tk.MustGetErrMsg("insert into t values (1, 'd')", "[kv:1062]Duplicate entry '1' for key 't.idx'") - tk.MustGetErrMsg("insert into t values (100, 'd')", "[table:1526]Table has no partition for value 100") - tk.MustExec("admin check table t;") - - // Test select partition - tk.MustExec("insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null)") - tk.MustQuery("select * from t partition (p0) order by id").Check(testkit.Rows("3 c")) - tk.MustQuery("select * from t partition (p1,p3) order by id").Check(testkit.Rows(" ", "1 a", "2 b", "7 f")) - tk.MustQuery("select * from t partition (p1,p3,p0,p2) order by id").Check(testkit.Rows(" ", "1 a", "2 b", "3 c", "4 d", "7 f")) - tk.MustQuery("select * from t order by id").Check(testkit.Rows(" ", "1 a", "2 b", "3 c", "4 d", "7 f")) - tk.MustExec("delete from t partition (p0)") - tk.MustQuery("select * from t order by id").Check(testkit.Rows(" ", "1 a", "2 b", "4 d", "7 f")) - tk.MustExec("delete from t partition (p3,p2)") - tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 a", "2 b")) -} - -func TestWriteListColumnsPartitionTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec("drop table if exists t") - tk.MustExec(`create table t (id int, name varchar(10), unique index idx (id)) partition by list columns (id) ( - partition p0 values in (3,5,6,9,17), - partition p1 values in (1,2,10,11,19,20), - partition p2 values in (4,12,13,14,18), - partition p3 values in (7,8,15,16,null) - );`) - - // Test insert,update,delete - tk.MustExec("insert into t values (1, 'a')") - tk.MustExec("update t set name='b' where id=2;") - tk.MustQuery("select * from t").Check(testkit.Rows("1 a")) - tk.MustExec("update t set name='b' where id=1;") - tk.MustQuery("select * from t").Check(testkit.Rows("1 b")) - tk.MustExec("replace into t values (1, 'c')") - tk.MustQuery("select * from t").Check(testkit.Rows("1 c")) - tk.MustExec("insert into t values (1, 'd') on duplicate key update name='e'") - tk.MustQuery("select * from t").Check(testkit.Rows("1 e")) - tk.MustExec("delete from t where id=1") - tk.MustQuery("select * from t").Check(testkit.Rows()) - tk.MustExec("insert into t values (2, 'f')") - tk.MustExec("delete from t where name='f'") - tk.MustQuery("select * from t").Check(testkit.Rows()) - - // Test insert error - tk.MustExec("insert into t values (1, 'a')") - err := tk.ExecToErr("insert into t values (1, 'd')") - require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 't.idx'") - err = tk.ExecToErr("insert into t values (100, 'd')") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - tk.MustExec("admin check table t;") - - // Test select partition - tk.MustExec("insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null)") - tk.MustQuery("select * from t partition (p0) order by id").Check(testkit.Rows("3 c")) - tk.MustQuery("select * from t partition (p1,p3) order by id").Check(testkit.Rows(" ", "1 a", "2 b", "7 f")) - tk.MustQuery("select * from t partition (p1,p3,p0,p2) order by id").Check(testkit.Rows(" ", "1 a", "2 b", "3 c", "4 d", "7 f")) - tk.MustQuery("select * from t order by id").Check(testkit.Rows(" ", "1 a", "2 b", "3 c", "4 d", "7 f")) - tk.MustExec("delete from t partition (p0)") - tk.MustQuery("select * from t order by id").Check(testkit.Rows(" ", "1 a", "2 b", "4 d", "7 f")) - tk.MustExec("delete from t partition (p3,p2)") - tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 a", "2 b")) -} - // TestWriteListPartitionTable1 test for write list partition when the partition expression is simple. func TestWriteListPartitionTable1(t *testing.T) { store := testkit.CreateMockStore(t) diff --git a/tests/integrationtest/r/executor/ddl.result b/tests/integrationtest/r/executor/ddl.result new file mode 100644 index 0000000000000..2aa60d6014c39 --- /dev/null +++ b/tests/integrationtest/r/executor/ddl.result @@ -0,0 +1,298 @@ +drop table if exists truncate_test; +create table truncate_test (a int); +insert truncate_test values (1),(2),(3); +select * from truncate_test; +a +1 +2 +3 +truncate table truncate_test; +select * from truncate_test; +a +drop table if exists t; +drop view if exists recursive_view1, recursive_view2; +create table if not exists t(a int); +create definer='root'@'localhost' view recursive_view1 as select * from t; +create definer='root'@'localhost' view recursive_view2 as select * from recursive_view1; +drop table t; +rename table recursive_view2 to t; +select * from recursive_view1; +Error 1462 (HY000): `executor__ddl`.`recursive_view1` contains view recursion +drop view recursive_view1, t; +drop table if exists t; +drop view if exists recursive_view1, recursive_view2; +create table if not exists t(a int); +create view view_issue16250 as select * from t; +truncate table view_issue16250; +Error 1146 (42S02): Table 'executor__ddl.view_issue16250' doesn't exist +drop table if exists t; +drop view if exists view_issue16250; +drop table if exists zy_tab; +create table if not exists zy_tab ( +zy_code int, +zy_name varchar(100) +); +drop table if exists bj_tab; +create table if not exists bj_tab ( +bj_code int, +bj_name varchar(100), +bj_addr varchar(100), +bj_person_count int, +zy_code int +); +drop table if exists st_tab; +create table if not exists st_tab ( +st_code int, +st_name varchar(100), +bj_code int +); +drop view if exists v_st_2; +create definer='root'@'localhost' view v_st_2 as +select st.st_name,bj.bj_name,zy.zy_name +from ( +select bj_code, +bj_name, +zy_code +from bj_tab as b +where b.bj_code = 1 +) as bj +left join zy_tab as zy on zy.zy_code = bj.zy_code +left join st_tab as st on bj.bj_code = st.bj_code; +show create view v_st_2; +View Create View character_set_client collation_connection +v_st_2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_st_2` (`st_name`, `bj_name`, `zy_name`) AS SELECT `st`.`st_name` AS `st_name`,`bj`.`bj_name` AS `bj_name`,`zy`.`zy_name` AS `zy_name` FROM ((SELECT `bj_code` AS `bj_code`,`bj_name` AS `bj_name`,`zy_code` AS `zy_code` FROM `executor__ddl`.`bj_tab` AS `b` WHERE `b`.`bj_code`=1) AS `bj` LEFT JOIN `executor__ddl`.`zy_tab` AS `zy` ON `zy`.`zy_code`=`bj`.`zy_code`) LEFT JOIN `executor__ddl`.`st_tab` AS `st` ON `bj`.`bj_code`=`st`.`bj_code` utf8mb4 utf8mb4_general_ci +select * from v_st_2; +st_name bj_name zy_name +drop view if exists v_st_2; +drop table if exists zy_tab; +drop table if exists bj_tab; +drop table if exists st_tab; +drop sequence if exists seq; +drop sequence if exists seq1; +create sequence if not exists seq; +truncate table seq; +Error 1146 (42S02): Table 'executor__ddl.seq' doesn't exist +create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle; +truncate table seq1; +Error 1146 (42S02): Table 'executor__ddl.seq1' doesn't exist +drop sequence if exists seq; +drop sequence if exists seq1; +drop table if exists drop_test; +create table if not exists drop_test (a int); +create index idx_a on drop_test (a); +drop index idx_a on drop_test; +drop table drop_test; +drop table if exists t; +create table t (a bigint auto_random(5), b int, primary key (a, b) clustered); +insert into t (b) values (1); +set @@allow_auto_random_explicit_insert = 0; +insert into t values (100, 2); +Error 8216 (HY000): Invalid auto random: Explicit insertion on auto_random column is disabled. Try to set @@allow_auto_random_explicit_insert = true. +set @@allow_auto_random_explicit_insert = 1; +insert into t values (100, 2); +select b from t order by b; +b +1 +2 +alter table t modify column a bigint auto_random(6); +drop table t; +create table t (a bigint, b bigint auto_random(4, 32), primary key (b, a) clustered); +insert into t (a) values (1); +select a from t; +a +1 +drop table if exists t; +set @@allow_auto_random_explicit_insert = default; +drop table if exists t; +create table t(a bigint PRIMARY KEY, b int); +insert into t values(9223372036854775807, 1); +insert into t values(-9223372036854775808, 1); +alter table t add index idx_b(b); +admin check table t; + +create table t1(a bigint UNSIGNED PRIMARY KEY, b int); +insert into t1 values(18446744073709551615, 1); +insert into t1 values(0, 1); +alter table t1 add index idx_b(b); +admin check table t1; + +drop table if exists t; +drop table if exists t; +create table t(c time DEFAULT '12:12:12.8'); +show create table `t`; +Table Create Table +t CREATE TABLE `t` ( + `c` time DEFAULT '12:12:13' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add column c1 time default '12:12:12.000000'; +show create table `t`; +Table Create Table +t CREATE TABLE `t` ( + `c` time DEFAULT '12:12:13', + `c1` time DEFAULT '12:12:12' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t alter column c1 set default '2019-02-01 12:12:10.4'; +show create table `t`; +Table Create Table +t CREATE TABLE `t` ( + `c` time DEFAULT '12:12:13', + `c1` time DEFAULT '12:12:10' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t modify c1 time DEFAULT '770:12:12.000000'; +show create table `t`; +Table Create Table +t CREATE TABLE `t` ( + `c` time DEFAULT '12:12:13', + `c1` time DEFAULT '770:12:12' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table if exists t; +drop table if exists t, t2, t3; +create table t ( tt timestamp default now(1)); +Error 1067 (42000): Invalid default value for 'tt' +create table t ( tt timestamp(1) default current_timestamp); +Error 1067 (42000): Invalid default value for 'tt' +create table t ( tt timestamp(1) default now(2)); +Error 1067 (42000): Invalid default value for 'tt' +create table t ( tt timestamp(1) default now(1)); +create table t2 ( tt timestamp default current_timestamp()); +create table t3 ( tt timestamp default current_timestamp(0)); +alter table t add column ttt timestamp default now(2); +Error 1067 (42000): Invalid default value for 'ttt' +alter table t add column ttt timestamp(5) default current_timestamp; +Error 1067 (42000): Invalid default value for 'ttt' +alter table t add column ttt timestamp(5) default now(2); +Error 1067 (42000): Invalid default value for 'ttt' +alter table t modify column tt timestamp(1) default now(); +Error 1067 (42000): Invalid default value for 'tt' +alter table t modify column tt timestamp(4) default now(5); +Error 1067 (42000): Invalid default value for 'tt' +alter table t change column tt tttt timestamp(4) default now(5); +Error 1067 (42000): Invalid default value for 'tttt' +alter table t change column tt tttt timestamp(1) default now(); +Error 1067 (42000): Invalid default value for 'tttt' +drop table if exists t, t2, t3; +drop table if exists tdv; +create table tdv(a int); +ALTER TABLE tdv ADD COLUMN ts timestamp DEFAULT '1970-01-01 08:00:01'; +drop table if exists tdv; +drop table if exists t; +CREATE TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 5 DAY */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */ +DROP TABLE t; +CREATE TABLE t (id int) TTL = `id` + INTERVAL 5 DAY; +Error 8148 (HY000): Field 'id' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP +CREATE TABLE t (id int) TTL_ENABLE = 'ON'; +Error 8150 (HY000): Cannot set TTL_ENABLE on a table without TTL config +CREATE TABLE t (id int) TTL_JOB_INTERVAL = '1h'; +Error 8150 (HY000): Cannot set TTL_JOB_INTERVAL on a table without TTL config +CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL_ENABLE = 'OFF' TTL_JOB_INTERVAL = '1d'; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 1 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */ +DROP TABLE t; +CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL = `created_at` + INTERVAL 2 DAY TTL = `created_at` + INTERVAL 3 DAY TTL_ENABLE = 'OFF'; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 3 DAY */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */ +DROP TABLE t; +drop table if exists t; +CREATE TABLE t (created_at datetime, updated_at datetime, wrong_type int) TTL = `created_at` + INTERVAL 5 DAY; +ALTER TABLE t TTL = `updated_at` + INTERVAL 2 YEAR; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */ +ALTER TABLE t TTL_ENABLE = 'OFF'; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1h' */ +ALTER TABLE t TTL_JOB_INTERVAL = '1d'; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */ +ALTER TABLE t TTL = `not_exist` + INTERVAL 2 YEAR; +Error 1054 (42S22): Unknown column 'not_exist' in 'TTL config' +ALTER TABLE t TTL = `wrong_type` + INTERVAL 2 YEAR; +Error 8148 (HY000): Field 'wrong_type' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP +ALTER TABLE t DROP COLUMN updated_at; +Error 8149 (HY000): Cannot drop column 'updated_at': needed in TTL config +ALTER TABLE t CHANGE updated_at updated_at_new INT; +Error 8148 (HY000): Field 'updated_at_new' is of a not supported type for TTL config, expect DATETIME, DATE or TIMESTAMP +ALTER TABLE t RENAME COLUMN `updated_at` TO `updated_at_2`; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at_2` datetime DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_2` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */ +ALTER TABLE t CHANGE `updated_at_2` `updated_at_3` date; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at_3` date DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_3` + INTERVAL 2 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */ +ALTER TABLE t TTL = `updated_at_3` + INTERVAL 3 YEAR; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at_3` date DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`updated_at_3` + INTERVAL 3 YEAR */ /*T![ttl] TTL_ENABLE='OFF' */ /*T![ttl] TTL_JOB_INTERVAL='1d' */ +ALTER TABLE t TTL_ENABLE = 'OFF' REMOVE TTL; +Error 8200 (HY000): Unsupported multi schema change for alter table ttl +ALTER TABLE t REMOVE TTL; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `created_at` datetime DEFAULT NULL, + `updated_at_3` date DEFAULT NULL, + `wrong_type` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +ALTER TABLE t TTL_ENABLE = 'OFF'; +Error 8150 (HY000): Cannot set TTL_ENABLE on a table without TTL config +ALTER TABLE t TTL_JOB_INTERVAL = '1h'; +Error 8150 (HY000): Cannot set TTL_JOB_INTERVAL on a table without TTL config +drop table if exists t; +drop table if exists t; +CREATE TEMPORARY TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY; +Error 8151 (HY000): Set TTL for temporary table is not allowed +set global tidb_enable_foreign_key='ON'; +drop table if exists t, t_1; +CREATE TABLE t (id int primary key, created_at datetime); +CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id)); +ALTER TABLE t TTL = created_at + INTERVAL 5 YEAR; +Error 8152 (HY000): Set TTL for a table referenced by foreign key is not allowed +drop table t,t_1; +CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR; +CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id)); +Error 8152 (HY000): Set TTL for a table referenced by foreign key is not allowed +drop table t; +CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR; +CREATE TABLE t_1 (t_id int); +ALTER TABLE t_1 ADD FOREIGN KEY fk_t_id(t_id) references t(id); +Error 8152 (HY000): Set TTL for a table referenced by foreign key is not allowed +drop table t,t_1; +set global tidb_enable_foreign_key=default; diff --git a/tests/integrationtest/r/executor/split_table.result b/tests/integrationtest/r/executor/split_table.result new file mode 100644 index 0000000000000..482c65b167396 --- /dev/null +++ b/tests/integrationtest/r/executor/split_table.result @@ -0,0 +1,8 @@ +drop table if exists t; +create table t(a bigint(20) auto_increment primary key); +split table t between (-9223372036854775808) and (9223372036854775807) regions 16; +drop table if exists t; +create table t(a int(20) auto_increment primary key); +split table t between (-9223372036854775808) and (9223372036854775807) regions 16; +Error 1690 (22003): constant -9223372036854775808 overflows int +drop table if exists t; diff --git a/tests/integrationtest/r/executor/write.result b/tests/integrationtest/r/executor/write.result new file mode 100644 index 0000000000000..ee8179fb3b6d6 --- /dev/null +++ b/tests/integrationtest/r/executor/write.result @@ -0,0 +1,791 @@ +drop table if exists t,t0; +create table t0 (i int); +insert into t0 values (1), (1); +create table t (i int unique key); +set @@tidb_dml_batch_size = 1; +insert ignore into t select * from t0; +admin check table t; + +drop table if exists t,t0; +set @@tidb_dml_batch_size = default; +drop table if exists t1, t2; +create table t1 (a int default 10, b int default 20); +insert into t1 set a=default; +select * from t1; +a b +10 20 +delete from t1; +insert into t1 set b=default; +select * from t1; +a b +10 20 +delete from t1; +insert into t1 set b=default, a=1; +select * from t1; +a b +1 20 +delete from t1; +insert into t1 set a=default(a); +select * from t1; +a b +10 20 +delete from t1; +insert into t1 set a=default(b), b=default(a); +select * from t1; +a b +20 10 +delete from t1; +insert into t1 set a=default(b)+default(a); +select * from t1; +a b +30 20 +create table t2 (a int default 10 primary key, b int generated always as (-a) virtual, c int generated always as (-a) stored); +insert into t2 set a=default; +select * from t2; +a b c +10 -10 -10 +delete from t2; +insert into t2 set a=2, b=default; +select * from t2; +a b c +2 -2 -2 +delete from t2; +insert into t2 set c=default, a=3; +select * from t2; +a b c +3 -3 -3 +delete from t2; +insert into t2 set a=default, b=default, c=default; +select * from t2; +a b c +10 -10 -10 +delete from t2; +insert into t2 set a=default(a), b=default, c=default; +select * from t2; +a b c +10 -10 -10 +delete from t2; +insert into t2 set b=default(a); +Error 3105 (HY000): The value specified for generated column 'b' in table 't2' is not allowed. +insert into t2 set a=default(b), b=default(b); +Error 3105 (HY000): The value specified for generated column 'a' in table 't2' is not allowed. +insert into t2 set a=default(a), c=default(c); +insert into t2 set a=default(a), c=default(a); +Error 3105 (HY000): The value specified for generated column 'c' in table 't2' is not allowed. +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b); +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b); +Error 1062 (23000): Duplicate entry '3' for key 't2.PRIMARY' +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(a); +Error 3105 (HY000): The value specified for generated column 'b' in table 't2' is not allowed. +select * from t2; +a b c +10 -10 -10 +3 -3 -3 +drop table t1, t2; +create table t1 (a int not null auto_increment,primary key(a), t timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP); +set @@timestamp = 1637541064; +insert into t1 set a=default,t=default; +set @@timestamp = 1637541082; +insert into t1 VALUES (default,default); +select * from t1; +a t +1 2021-11-22 08:31:04 +2 2021-11-22 08:31:22 +set @@timestamp = 1637541332; +insert into t1 set a=1,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default; +insert into t1 set a=2,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default(t); +select * from t1; +a t +1 2021-11-22 08:35:32 +2 2021-11-22 08:35:32 +DROP TABLE t1; +CREATE TABLE t1 (a int default 1 PRIMARY KEY, b int default 2); +INSERT INTO t1 VALUES (2,2), (3,3); +INSERT INTO t1 VALUES (3,2) ON DUPLICATE KEY UPDATE b = DEFAULT(a); +INSERT INTO t1 SET a = 2, b = 3 ON DUPLICATE KEY UPDATE b = DEFAULT(a); +select * from t1; +a b +2 1 +3 1 +drop table if exists t1; +set @@timestamp = DEFAULT; +drop table if exists t1, t2; +create table t1 (a int unique, b int default 20, c int default 30); +insert into t1 values (1,default,default); +insert into t1 values (1,default,default) on duplicate key update b=default; +select * from t1; +a b c +1 20 30 +insert into t1 values (1,default,default) on duplicate key update c=default, b=default; +select * from t1; +a b c +1 20 30 +insert into t1 values (1,default,default) on duplicate key update c=default, a=2; +select * from t1; +a b c +2 20 30 +insert into t1 values (2,default,default) on duplicate key update c=default(b); +select * from t1; +a b c +2 20 20 +insert into t1 values (2,default,default) on duplicate key update a=default(b)+default(c); +select * from t1; +a b c +50 20 20 +create table t2 (a int unique, b int generated always as (-a) virtual, c int generated always as (-a) stored); +insert into t2 values (1,default,default); +insert into t2 values (1,default,default) on duplicate key update a=2, b=default; +select * from t2; +a b c +2 -2 -2 +insert into t2 values (2,default,default) on duplicate key update a=3, c=default; +select * from t2; +a b c +3 -3 -3 +insert into t2 values (3,default,default) on duplicate key update c=default, b=default, a=4; +select * from t2; +a b c +4 -4 -4 +insert into t2 values (4,default,default) on duplicate key update b=default, a=5, c=default; +select * from t2; +a b c +5 -5 -5 +insert into t2 values (5,default,default) on duplicate key update b=default(a); +Error 3105 (HY000): The value specified for generated column 'b' in table 't2' is not allowed. +insert into t2 values (5,default,default) on duplicate key update a=default(a), c=default(c); +select * from t2; +a b c +NULL NULL NULL +delete from t2; +insert into t2 (a) values (1); +insert into t2 values (1,default,default) on duplicate key update a=default(b), b=default(b); +select * from t2; +a b c +NULL NULL NULL +delete from t2; +insert into t2 (a) values (1); +insert into t2 values (1,default,default) on duplicate key update a=default(a), c=default(a); +Error 3105 (HY000): The value specified for generated column 'c' in table 't2' is not allowed. +drop table t1, t2; +set @@tidb_txn_mode = 'pessimistic'; +create table t ( c_int int, c_string varchar(40) collate utf8mb4_bin , primary key (c_string), unique key (c_int)); +insert into t values (22, 'gold witch'), (24, 'gray singer'), (21, 'silver sight'); +begin; +insert into t values (21,'black warlock'), (22, 'dark sloth'), (21, 'cyan song') on duplicate key update c_int = c_int + 1, c_string = concat(c_int, ':', c_string); +Error 1062 (23000): Duplicate entry '22' for key 't.c_int' +commit; +select * from t order by c_int; +c_int c_string +21 silver sight +22 gold witch +24 gray singer +drop table t; +set @@tidb_txn_mode = default; +drop table if exists t; +create table t (a varchar(20) charset utf8mb4 collate utf8mb4_general_ci primary key); +replace into t(a) values (_binary'A '),(_binary'A'); +select a from t use index(primary); +a +A +select a from t ignore index(primary); +a +A +drop table if exists t; +drop table if exists t1; +create table t1(id int, id_gen int as(id + 42), b int, unique key id_gen(id_gen)); +insert into t1 (id, b) values(1,1),(2,2),(3,3),(4,4),(5,5); +replace into t1 (id, b) values(1,1); +replace into t1 (id, b) values(1,1),(2,2); +replace into t1 (id, b) values(6,16),(7,17),(8,18); +select * from t1; +id id_gen b +1 43 1 +2 44 2 +3 45 3 +4 46 4 +5 47 5 +6 48 16 +7 49 17 +8 50 18 +insert into t1 (id, b) values (6,18) on duplicate key update id = -id; +insert into t1 (id, b) values (7,28) on duplicate key update b = -values(b); +select * from t1; +id id_gen b +1 43 1 +2 44 2 +3 45 3 +4 46 4 +5 47 5 +-6 36 16 +7 49 -28 +8 50 18 +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i), unique key idx_j(j)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +i j k +2 3 1 +3 4 2 +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_j(j)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +i j k +2 3 1 +3 4 2 +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +i j k +2 3 1 +3 4 2 +drop table if exists t1; +create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1)))); +INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON)); +Error 1690 (22003): DECIMAL value is out of range in '(2, 1)' +set @@sql_mode = ''; +INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON)); +select * from t1; +f1 f2 +1000 9.9 +set @@sql_mode = default; +drop table if exists t1; +drop table if exists test_null_default; +set timestamp = 1234; +set time_zone = '+08:00'; +create table test_null_default (ts timestamp null default current_timestamp); +insert into test_null_default values (null); +select * from test_null_default; +ts +NULL +insert into test_null_default values (); +select * from test_null_default; +ts +NULL +1970-01-01 08:20:34 +set timestamp = default; +drop table if exists test_null_default; +drop table if exists t1,t2; +create table t1 (a int not null default null default 1); +create table t2 (a int); +alter table t2 change column a a int not null default null default 1; +drop table t1,t2; +drop table if exists t1, t2; +create table t1(id int); +create table t2(id int); +insert into t1 values(123); +insert into t2 values(123); +delete from t1 where id not in (select id from t2); +select * from t1; +id +123 +delete from t1 where id in (select id from t2); +select * from t1; +id +drop table if exists t1, t2; +drop table if exists t; +create table t(a int, b int); +insert into t set a=1, b=a+1; +select a, b from t; +a b +1 2 +drop table if exists t; +create table t(a int default 100, b int); +insert into t set b=a+1, a=1; +select a, b from t; +a b +1 101 +insert into t (b) value (a); +select * from t where b = 100; +a b +100 100 +insert into t set a=2, b=a+1; +select * from t where a = 2; +a b +2 3 +drop table if exists t; +create table t (c int); +insert into t set t.c = '1'; +select * from t; +c +1 +drop table if exists t; +create table t(a int default 1); +insert into t values (a); +select * from t; +a +1 +drop table if exists t; +create table t (a int, b int, c int, d int); +insert into t value (1, 2, a+1, b+1); +select * from t; +a b c d +1 2 2 3 +drop table if exists t; +create table t (a int not null); +insert into t values (a+2); +insert into t values (a); +select * from t order by a; +a +0 +2 +drop table if exists t; +create table t (a bigint not null, b bigint not null); +insert into t value(b + 1, a); +insert into t set a = b + a, b = a + 1; +insert into t value(1000, a); +insert t set b = sqrt(a + 4), a = 10; +select * from t order by a; +a b +0 1 +1 1 +10 2 +1000 1000 +drop table if exists t; +create table t(a int); +insert into t values(a); +select * from t; +a +NULL +drop table if exists t; +create table t(a enum('a', 'b')); +insert into t values(a); +select * from t; +a +NULL +drop table if exists t; +create table t(a enum('a', 'b') default 'a'); +insert into t values(a); +insert into t values(a+1); +select * from t order by a; +a +a +b +drop table if exists t; +create table t(a blob); +insert into t values(a); +select * from t; +a +NULL +drop table if exists t; +create table t(a varchar(20) default 'a'); +insert into t values(a); +insert into t values(upper(a)); +select * from t order by a; +a +A +a +drop table if exists t; +create table t(a varchar(20) not null, b varchar(20)); +insert into t value (a, b); +select * from t; +a b + NULL +drop table if exists t; +create table t(a int, b int); +insert into t values(a*b, b*b); +select * from t; +a b +NULL NULL +drop table if exists t; +create table t (a json not null, b int); +insert into t value (a,a->'$'); +Error 1366 (HY000): Incorrect int value: 'null' for column 'b' at row 1 +drop table if exists t; +create table t (a json not null, b varchar(10)); +insert into t value (a,a->'$'); +select * from t; +a b +null null +drop table if exists t; +create table t(a json, b int, c int as (a->'$.a')); +insert into t (a, b) value (a, a->'$.a'+1); +insert into t (b) value (a->'$.a'+1); +select * from t; +a b c +NULL NULL NULL +NULL NULL NULL +insert into t (a, b) value ('{"a": 1}', a->'$.a'+1); +select * from t where c = 1; +a b c +{"a": 1} 2 1 +truncate table t; +insert t set b = c + 1; +select * from t; +a b c +NULL NULL NULL +truncate table t; +insert t set a = '{"a": 1}', b = c; +select * from t; +a b c +{"a": 1} NULL 1 +drop table if exists t; +create table t(a int auto_increment key, b int); +insert into t (b) value (a); +insert into t value (a, a+1); +set SQL_MODE=NO_AUTO_VALUE_ON_ZERO; +insert into t (b) value (a+1); +select * from t order by a; +a b +1 0 +2 1 +3 1 +set SQL_MODE=STRICT_ALL_TABLES; +drop table if exists t; +create table t(a int not null, b int, c int as (sqrt(a))); +insert t set b = a, a = 4; +select * from t; +a b c +4 0 2 +set SQL_MODE=default; +drop table t; +drop table if exists t; +create table t(a varchar(2)); +insert into t values('123'); +Error 1406 (22001): Data too long for column 'a' at row 1 +insert into t values('12'); +update t set a = '123' where a = '12'; +Error 1406 (22001): Data too long for column 'a' at row 1 +drop table t; +set tidb_constraint_check_in_place = 0; +set @@tidb_txn_mode = 'optimistic'; +drop table if exists t1, t2, t3, t4, t5; +create table t1(i int primary key, j int); +insert into t1 values(1, 2); +begin; +insert into t1 values(1, 3); +delete from t1 where j = 3; +commit; +Error 1062 (23000): Duplicate entry '1' for key 't1.PRIMARY' +rollback; +create table t2(i int, j int, unique index idx(i)); +insert into t2 values(1, 2); +begin; +insert into t2 values(1, 3); +delete from t2 where j = 3; +commit; +Error 1062 (23000): Duplicate entry '1' for key 't2.idx' +admin check table t2; + +create table t3(i int, j int, primary key(i)); +begin; +insert into t3 values(1, 3); +delete from t3 where j = 3; +commit; +create table t4(i int, j int, primary key(i)); +begin; +insert into t4 values(1, 3); +delete from t4 where j = 3; +insert into t4 values(2, 3); +commit; +admin check table t4; + +select * from t4; +i j +2 3 +create table t5(i int, j int, primary key(i)); +begin; +insert into t5 values(1, 3); +delete from t5 where j = 3; +insert into t5 values(1, 4); +commit; +admin check table t5; + +select * from t5; +i j +1 4 +set tidb_constraint_check_in_place = default; +set @@tidb_txn_mode = default; +drop table if exists test; +create table test (id int, prescription_type enum('a','b','c','d','e','f') NOT NULL, primary key(id)); +insert into test (id) values (1); +select prescription_type from test; +prescription_type +a +drop table if exists test; +drop table if exists t; +create table t (pk int primary key, uk int unique, v int); +insert into t values (2, 11, 215); +insert into t values (3, 7, 2111); +update t set pk = 2 where uk = 7; +Error 1062 (23000): Duplicate entry '2' for key 't.PRIMARY' +drop table if exists t; +drop table if exists t, te, tn, t1, t2, t3; +create table t (i int, j int as (i+1) not null); +insert into t set i = j + 1; +select * from t; +i j +1 2 +insert into t set i = j + 100; +select * from t; +i j +1 2 +100 101 +create table te (i int); +insert into te set i = i + 10; +select * from te; +i +NULL +insert into te set i = i; +select * from te; +i +NULL +NULL +create table tn (i int not null); +insert into tn set i = i; +select * from tn; +i +0 +insert into tn set i = i + 10; +select * from tn; +i +0 +10 +create table t1 (j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '10'); +insert into t1 values(); +select * from t1; +j i +11 10 +insert into t1 values(); +select * from t1; +j i +11 10 +11 10 +create table t2 (j int(11) GENERATED ALWAYS AS (i + 1) stored not null, i int(11) DEFAULT '5'); +insert into t2 set i = j + 9; +select * from t2; +j i +10 9 +insert into t2 set j = i + 1; +Error 3105 (HY000): The value specified for generated column 'j' in table 't2' is not allowed. +insert into t2 set i = j + 100; +select * from t2; +j i +10 9 +101 100 +create table t3(j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '5'); +insert into t3 set i = j + 100; +select * from t3; +j i +NULL NULL +insert into t3 set j = i + 1; +Error 3105 (HY000): The value specified for generated column 'j' in table 't3' is not allowed. +drop table if exists t, te, t1, t2, t3; +drop table if exists t1; +create table t1(c1 timestamp default current_timestamp, c2 int, c3 timestamp default current_timestamp); +insert into t1 set c1 = current_timestamp, c2 = sleep(2); +select c1 = c3 from t1; +c1 = c3 +1 +insert into t1 set c1 = current_timestamp, c2 = sleep(1); +select c1 = c3 from t1; +c1 = c3 +1 +1 +drop table if exists t1; +drop table if exists t; +create table t ( c_int int, c_str varchar(40),c_datetime datetime, c_timestamp timestamp, +c_double double, c_decimal decimal(12, 6) , primary key(c_int, c_str) , unique key(c_int) , unique key(c_str) , +unique key(c_decimal) , unique key(c_datetime) , key(c_timestamp) ); +insert into t values (1, 'zen ardinghelli', '2020-02-03 18:15:17', '2020-03-11 05:47:11', 36.226534, 3.763), +(2, 'suspicious joliot', '2020-01-01 22:56:37', '2020-04-07 06:19:07', 62.756537, 5.567), +(3, 'keen zhukovsky', '2020-01-21 04:09:20', '2020-06-06 08:32:14', 33.115065, 1.381), +(4, 'crazy newton', '2020-02-14 21:37:56', '2020-04-28 08:33:48', 44.146318, 4.249), +(5, 'happy black', '2020-03-12 16:04:14', '2020-01-18 09:17:37', 41.962653, 5.959); +insert into t values (6, 'vigilant swartz', '2020-06-01 07:37:44', '2020-05-25 01:26:43', 56.352233, 2.202), +(7, 'suspicious germain', '2020-04-16 23:25:23', '2020-03-17 05:06:57', 55.897698, 3.460), +(8, 'festive chandrasekhar', '2020-02-11 23:40:29', '2020-04-08 10:13:04', 77.565691, 0.540), +(9, 'vigorous meninsky', '2020-02-17 10:03:17', '2020-01-02 15:02:02', 6.484815, 6.292), +(10, 'heuristic moser', '2020-04-20 12:18:49', '2020-06-20 20:20:18', 28.023822, 2.765); +insert into t values (11, 'sharp carver', '2020-03-01 11:23:41', '2020-03-23 17:59:05', 40.842442, 6.345), +(12, 'trusting noether', '2020-03-28 06:42:34', '2020-01-27 15:33:40', 49.544658, 4.811), +(13, 'objective ishizaka', '2020-01-28 17:30:55', '2020-04-02 17:45:39', 59.523930, 5.015), +(14, 'sad rhodes', '2020-03-30 21:43:37', '2020-06-09 06:53:53', 87.295753, 2.413), +(15, 'wonderful shockley', '2020-04-29 09:17:11', '2020-03-14 04:36:51', 6.778588, 8.497); +begin pessimistic; +insert into t values (13, 'vibrant yalow', '2020-05-15 06:59:05', '2020-05-03 05:58:45', 43.721929, 8.066), +(14, 'xenodochial spence', '2020-02-13 17:28:07', '2020-04-01 12:18:30', 19.981331, 5.774), +(22, 'eloquent neumann', '2020-02-10 16:00:20', '2020-03-28 00:24:42', 10.702532, 7.618) +on duplicate key update c_int=values(c_int), c_str=values(c_str), c_double=values(c_double), c_timestamp=values(c_timestamp); +select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +nulls +10 +select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +nulls +10 +commit; +select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +nulls +10 +select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +nulls +10 +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +drop table if exists t; +create table t (id int, name varchar(10), unique index idx (id)) partition by list (id) ( +partition p0 values in (3,5,6,9,17), +partition p1 values in (1,2,10,11,19,20), +partition p2 values in (4,12,13,14,18), +partition p3 values in (7,8,15,16,null) +); +insert into t values (1, 'a'); +update t set name='b' where id=2; +select * from t; +id name +1 a +update t set name='b' where id=1; +select * from t; +id name +1 b +replace into t values (1, 'c'); +select * from t; +id name +1 c +insert into t values (1, 'd') on duplicate key update name='e'; +select * from t; +id name +1 e +delete from t where id=1; +select * from t; +id name +insert into t values (2, 'f'); +delete from t where name='f'; +select * from t; +id name +insert into t values (1, 'a'); +insert into t values (1, 'd'); +Error 1062 (23000): Duplicate entry '1' for key 't.idx' +insert into t values (100, 'd'); +Error 1526 (HY000): Table has no partition for value 100 +admin check table t; + +insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null); +select * from t partition (p0) order by id; +id name +3 c +select * from t partition (p1,p3) order by id; +id name +NULL NULL +1 a +2 b +7 f +select * from t partition (p1,p3,p0,p2) order by id; +id name +NULL NULL +1 a +2 b +3 c +4 d +7 f +select * from t order by id; +id name +NULL NULL +1 a +2 b +3 c +4 d +7 f +delete from t partition (p0); +select * from t order by id; +id name +NULL NULL +1 a +2 b +4 d +7 f +delete from t partition (p3,p2); +select * from t order by id; +id name +1 a +2 b +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +drop table if exists t; +create table t (id int, name varchar(10), unique index idx (id)) partition by list columns (id) ( +partition p0 values in (3,5,6,9,17), +partition p1 values in (1,2,10,11,19,20), +partition p2 values in (4,12,13,14,18), +partition p3 values in (7,8,15,16,null) +); +insert into t values (1, 'a'); +update t set name='b' where id=2; +select * from t; +id name +1 a +update t set name='b' where id=1; +select * from t; +id name +1 b +replace into t values (1, 'c'); +select * from t; +id name +1 c +insert into t values (1, 'd') on duplicate key update name='e'; +select * from t; +id name +1 e +delete from t where id=1; +select * from t; +id name +insert into t values (2, 'f'); +delete from t where name='f'; +select * from t; +id name +insert into t values (1, 'a'); +insert into t values (1, 'd'); +Error 1062 (23000): Duplicate entry '1' for key 't.idx' +insert into t values (100, 'd'); +Error 1526 (HY000): Table has no partition for value from column_list +admin check table t; + +insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null); +select * from t partition (p0) order by id; +id name +3 c +select * from t partition (p1,p3) order by id; +id name +NULL NULL +1 a +2 b +7 f +select * from t partition (p1,p3,p0,p2) order by id; +id name +NULL NULL +1 a +2 b +3 c +4 d +7 f +select * from t order by id; +id name +NULL NULL +1 a +2 b +3 c +4 d +7 f +delete from t partition (p0); +select * from t order by id; +id name +NULL NULL +1 a +2 b +4 d +7 f +delete from t partition (p3,p2); +select * from t order by id; +id name +1 a +2 b +set @@session.tidb_enable_list_partition = default; +drop table if exists t; diff --git a/tests/integrationtest/t/executor/ddl.test b/tests/integrationtest/t/executor/ddl.test new file mode 100644 index 0000000000000..eaf74e6c96767 --- /dev/null +++ b/tests/integrationtest/t/executor/ddl.test @@ -0,0 +1,239 @@ +# TestTruncateTable +drop table if exists truncate_test; +create table truncate_test (a int); +insert truncate_test values (1),(2),(3); +select * from truncate_test; +truncate table truncate_test; +select * from truncate_test; + +# TestViewRecursion +drop table if exists t; +drop view if exists recursive_view1, recursive_view2; +create table if not exists t(a int); +create definer='root'@'localhost' view recursive_view1 as select * from t; +create definer='root'@'localhost' view recursive_view2 as select * from recursive_view1; +drop table t; +rename table recursive_view2 to t; +--error 1462 +select * from recursive_view1; +drop view recursive_view1, t; +drop table if exists t; +drop view if exists recursive_view1, recursive_view2; + +# TestIssue16250 +create table if not exists t(a int); +create view view_issue16250 as select * from t; +-- error 1146 +truncate table view_issue16250; +drop table if exists t; +drop view if exists view_issue16250; + +# TestIssue24771 +drop table if exists zy_tab; +create table if not exists zy_tab ( + zy_code int, + zy_name varchar(100) + ); +drop table if exists bj_tab; +create table if not exists bj_tab ( + bj_code int, + bj_name varchar(100), + bj_addr varchar(100), + bj_person_count int, + zy_code int + ); +drop table if exists st_tab; +create table if not exists st_tab ( + st_code int, + st_name varchar(100), + bj_code int + ); +drop view if exists v_st_2; +create definer='root'@'localhost' view v_st_2 as + select st.st_name,bj.bj_name,zy.zy_name + from ( + select bj_code, + bj_name, + zy_code + from bj_tab as b + where b.bj_code = 1 + ) as bj + left join zy_tab as zy on zy.zy_code = bj.zy_code + left join st_tab as st on bj.bj_code = st.bj_code; +show create view v_st_2; +select * from v_st_2; +drop view if exists v_st_2; +drop table if exists zy_tab; +drop table if exists bj_tab; +drop table if exists st_tab; + +# TestTruncateSequence +drop sequence if exists seq; +drop sequence if exists seq1; +create sequence if not exists seq; +-- error 1146 +truncate table seq; +create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle; +-- error 1146 +truncate table seq1; +drop sequence if exists seq; +drop sequence if exists seq1; + +# TestCreateDropIndex +drop table if exists drop_test; +create table if not exists drop_test (a int); +create index idx_a on drop_test (a); +drop index idx_a on drop_test; +drop table drop_test; + +# TestAutoRandomClusteredPrimaryKey +drop table if exists t; +create table t (a bigint auto_random(5), b int, primary key (a, b) clustered); +insert into t (b) values (1); +set @@allow_auto_random_explicit_insert = 0; +-- error 8216 +insert into t values (100, 2); +set @@allow_auto_random_explicit_insert = 1; +insert into t values (100, 2); +select b from t order by b; +alter table t modify column a bigint auto_random(6); +drop table t; +create table t (a bigint, b bigint auto_random(4, 32), primary key (b, a) clustered); +insert into t (a) values (1); +select a from t; +drop table if exists t; +set @@allow_auto_random_explicit_insert = default; + +# TestMaxHandleAddIndex +drop table if exists t; +create table t(a bigint PRIMARY KEY, b int); +insert into t values(9223372036854775807, 1); +insert into t values(-9223372036854775808, 1); +alter table t add index idx_b(b); +admin check table t; +create table t1(a bigint UNSIGNED PRIMARY KEY, b int); +insert into t1 values(18446744073709551615, 1); +insert into t1 values(0, 1); +alter table t1 add index idx_b(b); +admin check table t1; +drop table if exists t; + +# TestIssue9205 +drop table if exists t; +create table t(c time DEFAULT '12:12:12.8'); +show create table `t`; +alter table t add column c1 time default '12:12:12.000000'; +show create table `t`; +alter table t alter column c1 set default '2019-02-01 12:12:10.4'; +show create table `t`; +alter table t modify c1 time DEFAULT '770:12:12.000000'; +show create table `t`; +drop table if exists t; + +# TestCheckDefaultFsp +drop table if exists t, t2, t3; +-- error 1067 +create table t ( tt timestamp default now(1)); +-- error 1067 +create table t ( tt timestamp(1) default current_timestamp); +-- error 1067 +create table t ( tt timestamp(1) default now(2)); +create table t ( tt timestamp(1) default now(1)); +create table t2 ( tt timestamp default current_timestamp()); +create table t3 ( tt timestamp default current_timestamp(0)); +-- error 1067 +alter table t add column ttt timestamp default now(2); +-- error 1067 +alter table t add column ttt timestamp(5) default current_timestamp; +-- error 1067 +alter table t add column ttt timestamp(5) default now(2); +-- error 1067 +alter table t modify column tt timestamp(1) default now(); +-- error 1067 +alter table t modify column tt timestamp(4) default now(5); +-- error 1067 +alter table t change column tt tttt timestamp(4) default now(5); +-- error 1067 +alter table t change column tt tttt timestamp(1) default now(); +drop table if exists t, t2, t3; + +# TestTimestampMinDefaultValue +drop table if exists tdv; +create table tdv(a int); +ALTER TABLE tdv ADD COLUMN ts timestamp DEFAULT '1970-01-01 08:00:01'; +drop table if exists tdv; + +# TestCreateTableWithTTL +drop table if exists t; +CREATE TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY; +SHOW CREATE TABLE t; +DROP TABLE t; +-- error 8148 +CREATE TABLE t (id int) TTL = `id` + INTERVAL 5 DAY; +-- error 8150 +CREATE TABLE t (id int) TTL_ENABLE = 'ON'; +-- error 8150 +CREATE TABLE t (id int) TTL_JOB_INTERVAL = '1h'; +CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL_ENABLE = 'OFF' TTL_JOB_INTERVAL = '1d'; +SHOW CREATE TABLE t; +DROP TABLE t; +CREATE TABLE t (created_at datetime) TTL_ENABLE = 'ON' TTL = `created_at` + INTERVAL 1 DAY TTL = `created_at` + INTERVAL 2 DAY TTL = `created_at` + INTERVAL 3 DAY TTL_ENABLE = 'OFF'; +SHOW CREATE TABLE t; +DROP TABLE t; + +# TestAlterTTLInfo +drop table if exists t; +CREATE TABLE t (created_at datetime, updated_at datetime, wrong_type int) TTL = `created_at` + INTERVAL 5 DAY; +ALTER TABLE t TTL = `updated_at` + INTERVAL 2 YEAR; +SHOW CREATE TABLE t; +ALTER TABLE t TTL_ENABLE = 'OFF'; +SHOW CREATE TABLE t; +ALTER TABLE t TTL_JOB_INTERVAL = '1d'; +SHOW CREATE TABLE t; +-- error 1054 +ALTER TABLE t TTL = `not_exist` + INTERVAL 2 YEAR; +-- error 8148 +ALTER TABLE t TTL = `wrong_type` + INTERVAL 2 YEAR; +-- error 8149 +ALTER TABLE t DROP COLUMN updated_at; +-- error 8148 +ALTER TABLE t CHANGE updated_at updated_at_new INT; +ALTER TABLE t RENAME COLUMN `updated_at` TO `updated_at_2`; +SHOW CREATE TABLE t; +ALTER TABLE t CHANGE `updated_at_2` `updated_at_3` date; +SHOW CREATE TABLE t; +ALTER TABLE t TTL = `updated_at_3` + INTERVAL 3 YEAR; +SHOW CREATE TABLE t; +-- error 8200 +ALTER TABLE t TTL_ENABLE = 'OFF' REMOVE TTL; +ALTER TABLE t REMOVE TTL; +SHOW CREATE TABLE t; +-- error 8150 +ALTER TABLE t TTL_ENABLE = 'OFF'; +-- error 8150 +ALTER TABLE t TTL_JOB_INTERVAL = '1h'; +drop table if exists t; + +# TestDisableTTLForTempTable +drop table if exists t; +--error 8151 +CREATE TEMPORARY TABLE t (created_at datetime) TTL = `created_at` + INTERVAL 5 DAY; + +# TestDisableTTLForFKParentTable +set global tidb_enable_foreign_key='ON'; +drop table if exists t, t_1; +CREATE TABLE t (id int primary key, created_at datetime); +CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id)); +--error 8152 +ALTER TABLE t TTL = created_at + INTERVAL 5 YEAR; +drop table t,t_1; +CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR; +--error 8152 +CREATE TABLE t_1 (t_id int, foreign key fk_t_id(t_id) references t(id)); +drop table t; +CREATE TABLE t (id int primary key, created_at datetime) TTL = created_at + INTERVAL 5 YEAR; +CREATE TABLE t_1 (t_id int); +--error 8152 +ALTER TABLE t_1 ADD FOREIGN KEY fk_t_id(t_id) references t(id); +drop table t,t_1; +set global tidb_enable_foreign_key=default; diff --git a/tests/integrationtest/t/executor/split_table.test b/tests/integrationtest/t/executor/split_table.test new file mode 100644 index 0000000000000..b21af7384674d --- /dev/null +++ b/tests/integrationtest/t/executor/split_table.test @@ -0,0 +1,9 @@ +# TestSplitRegionEdgeCase +drop table if exists t; +create table t(a bigint(20) auto_increment primary key); +split table t between (-9223372036854775808) and (9223372036854775807) regions 16; +drop table if exists t; +create table t(a int(20) auto_increment primary key); +-- error 1690 +split table t between (-9223372036854775808) and (9223372036854775807) regions 16; +drop table if exists t; diff --git a/tests/integrationtest/t/executor/write.test b/tests/integrationtest/t/executor/write.test new file mode 100644 index 0000000000000..77e949d34bfbf --- /dev/null +++ b/tests/integrationtest/t/executor/write.test @@ -0,0 +1,548 @@ +# TestMultiBatch +drop table if exists t,t0; +create table t0 (i int); +insert into t0 values (1), (1); +create table t (i int unique key); +set @@tidb_dml_batch_size = 1; +insert ignore into t select * from t0; +admin check table t; +drop table if exists t,t0; +set @@tidb_dml_batch_size = default; + +# TestInsertSetWithDefault +drop table if exists t1, t2; +create table t1 (a int default 10, b int default 20); +insert into t1 set a=default; +select * from t1; +delete from t1; +insert into t1 set b=default; +select * from t1; +delete from t1; +insert into t1 set b=default, a=1; +select * from t1; +delete from t1; +insert into t1 set a=default(a); +select * from t1; +delete from t1; +insert into t1 set a=default(b), b=default(a); +select * from t1; +delete from t1; +insert into t1 set a=default(b)+default(a); +select * from t1; +create table t2 (a int default 10 primary key, b int generated always as (-a) virtual, c int generated always as (-a) stored); +insert into t2 set a=default; +select * from t2; +delete from t2; +insert into t2 set a=2, b=default; +select * from t2; +delete from t2; +insert into t2 set c=default, a=3; +select * from t2; +delete from t2; +insert into t2 set a=default, b=default, c=default; +select * from t2; +delete from t2; +insert into t2 set a=default(a), b=default, c=default; +select * from t2; +delete from t2; +-- error 3105 +insert into t2 set b=default(a); +-- error 3105 +insert into t2 set a=default(b), b=default(b); +insert into t2 set a=default(a), c=default(c); +-- error 3105 +insert into t2 set a=default(a), c=default(a); +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b); +-- error 1062 +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(b); +-- error 3105 +insert into t2 set a=3, b=default, c=default(c) ON DUPLICATE KEY UPDATE b = default(a); +--sorted_result +select * from t2; +drop table t1, t2; +create table t1 (a int not null auto_increment,primary key(a), t timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP); +set @@timestamp = 1637541064; +--enable_warnings; +insert into t1 set a=default,t=default; +--disable_warnings; +set @@timestamp = 1637541082; +insert into t1 VALUES (default,default); +--sorted_result +select * from t1; +set @@timestamp = 1637541332; +--enable_warnings; +insert into t1 set a=1,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default; +insert into t1 set a=2,t='2001-02-03 04:05:06' ON DUPLICATE KEY UPDATE t = default(t); +--disable_warnings; +--sorted_result +select * from t1; +DROP TABLE t1; +CREATE TABLE t1 (a int default 1 PRIMARY KEY, b int default 2); +INSERT INTO t1 VALUES (2,2), (3,3); +INSERT INTO t1 VALUES (3,2) ON DUPLICATE KEY UPDATE b = DEFAULT(a); +--enable_warnings; +INSERT INTO t1 SET a = 2, b = 3 ON DUPLICATE KEY UPDATE b = DEFAULT(a); +--disable_warnings; +--sorted_result +select * from t1; +drop table if exists t1; +set @@timestamp = DEFAULT; + +# TestInsertOnDupUpdateDefault +drop table if exists t1, t2; +create table t1 (a int unique, b int default 20, c int default 30); +insert into t1 values (1,default,default); +insert into t1 values (1,default,default) on duplicate key update b=default; +select * from t1; +insert into t1 values (1,default,default) on duplicate key update c=default, b=default; +select * from t1; +insert into t1 values (1,default,default) on duplicate key update c=default, a=2; +select * from t1; +insert into t1 values (2,default,default) on duplicate key update c=default(b); +select * from t1; +insert into t1 values (2,default,default) on duplicate key update a=default(b)+default(c); +select * from t1; +create table t2 (a int unique, b int generated always as (-a) virtual, c int generated always as (-a) stored); +insert into t2 values (1,default,default); +insert into t2 values (1,default,default) on duplicate key update a=2, b=default; +select * from t2; +insert into t2 values (2,default,default) on duplicate key update a=3, c=default; +select * from t2; +insert into t2 values (3,default,default) on duplicate key update c=default, b=default, a=4; +select * from t2; +insert into t2 values (4,default,default) on duplicate key update b=default, a=5, c=default; +select * from t2; +-- error 3105 +insert into t2 values (5,default,default) on duplicate key update b=default(a); +insert into t2 values (5,default,default) on duplicate key update a=default(a), c=default(c); +select * from t2; +delete from t2; +insert into t2 (a) values (1); +insert into t2 values (1,default,default) on duplicate key update a=default(b), b=default(b); +select * from t2; +delete from t2; +insert into t2 (a) values (1); +-- error 3105 +insert into t2 values (1,default,default) on duplicate key update a=default(a), c=default(a); +drop table t1, t2; +set @@tidb_txn_mode = 'pessimistic'; +create table t ( c_int int, c_string varchar(40) collate utf8mb4_bin , primary key (c_string), unique key (c_int)); +insert into t values (22, 'gold witch'), (24, 'gray singer'), (21, 'silver sight'); +begin; +--error 1062 +insert into t values (21,'black warlock'), (22, 'dark sloth'), (21, 'cyan song') on duplicate key update c_int = c_int + 1, c_string = concat(c_int, ':', c_string); +commit; +select * from t order by c_int; +drop table t; +set @@tidb_txn_mode = default; + +# TestReplaceWithCICollation +drop table if exists t; +create table t (a varchar(20) charset utf8mb4 collate utf8mb4_general_ci primary key); +replace into t(a) values (_binary'A '),(_binary'A'); +select a from t use index(primary); +select a from t ignore index(primary); +drop table if exists t; + +# TestGeneratedColumnForInsert +drop table if exists t1; +create table t1(id int, id_gen int as(id + 42), b int, unique key id_gen(id_gen)); +insert into t1 (id, b) values(1,1),(2,2),(3,3),(4,4),(5,5); +replace into t1 (id, b) values(1,1); +replace into t1 (id, b) values(1,1),(2,2); +replace into t1 (id, b) values(6,16),(7,17),(8,18); +select * from t1; +insert into t1 (id, b) values (6,18) on duplicate key update id = -id; +insert into t1 (id, b) values (7,28) on duplicate key update b = -values(b); +select * from t1; +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i), unique key idx_j(j)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_j(j)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +drop table if exists t; +create table t (i int as(k+1) stored, j int as(k+2) virtual, k int, unique key idx_i(i)); +insert into t (k) values (1), (2); +replace into t (k) values (1), (2); +select * from t; +drop table if exists t1; +create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1)))); +-- error 1690 +INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON)); +set @@sql_mode = ''; +INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON)); +select * from t1; +set @@sql_mode = default; +drop table if exists t1; + +# TestNullDefault +drop table if exists test_null_default; +set timestamp = 1234; +set time_zone = '+08:00'; +create table test_null_default (ts timestamp null default current_timestamp); +insert into test_null_default values (null); +select * from test_null_default; +insert into test_null_default values (); +select * from test_null_default; +set timestamp = default; +drop table if exists test_null_default; + +# TestNotNullDefault +drop table if exists t1,t2; +create table t1 (a int not null default null default 1); +create table t2 (a int); +alter table t2 change column a a int not null default null default 1; +drop table t1,t2; + +# TestIssue4067 +drop table if exists t1, t2; +create table t1(id int); +create table t2(id int); +insert into t1 values(123); +insert into t2 values(123); +delete from t1 where id not in (select id from t2); +select * from t1; +delete from t1 where id in (select id from t2); +select * from t1; +drop table if exists t1, t2; + +# TestInsertCalculatedValue +drop table if exists t; +create table t(a int, b int); +insert into t set a=1, b=a+1; +select a, b from t; +drop table if exists t; +create table t(a int default 100, b int); +insert into t set b=a+1, a=1; +select a, b from t; +insert into t (b) value (a); +select * from t where b = 100; +insert into t set a=2, b=a+1; +select * from t where a = 2; +drop table if exists t; +create table t (c int); +insert into t set t.c = '1'; +select * from t; +drop table if exists t; +create table t(a int default 1); +insert into t values (a); +select * from t; +drop table if exists t; +create table t (a int, b int, c int, d int); +insert into t value (1, 2, a+1, b+1); +select * from t; +drop table if exists t; +create table t (a int not null); +insert into t values (a+2); +insert into t values (a); +select * from t order by a; +drop table if exists t; +create table t (a bigint not null, b bigint not null); +insert into t value(b + 1, a); +insert into t set a = b + a, b = a + 1; +insert into t value(1000, a); +insert t set b = sqrt(a + 4), a = 10; +select * from t order by a; +drop table if exists t; +create table t(a int); +insert into t values(a); +select * from t; +drop table if exists t; +create table t(a enum('a', 'b')); +insert into t values(a); +select * from t; +drop table if exists t; +create table t(a enum('a', 'b') default 'a'); +insert into t values(a); +insert into t values(a+1); +select * from t order by a; +drop table if exists t; +create table t(a blob); +insert into t values(a); +select * from t; +drop table if exists t; +create table t(a varchar(20) default 'a'); +insert into t values(a); +insert into t values(upper(a)); +select * from t order by a; +drop table if exists t; +create table t(a varchar(20) not null, b varchar(20)); +insert into t value (a, b); +select * from t; +drop table if exists t; +create table t(a int, b int); +insert into t values(a*b, b*b); +select * from t; +drop table if exists t; +create table t (a json not null, b int); +-- error 1366 +insert into t value (a,a->'$'); +drop table if exists t; +create table t (a json not null, b varchar(10)); +insert into t value (a,a->'$'); +select * from t; +drop table if exists t; +create table t(a json, b int, c int as (a->'$.a')); +insert into t (a, b) value (a, a->'$.a'+1); +insert into t (b) value (a->'$.a'+1); +select * from t; +insert into t (a, b) value ('{"a": 1}', a->'$.a'+1); +select * from t where c = 1; +truncate table t; +insert t set b = c + 1; +select * from t; +truncate table t; +insert t set a = '{"a": 1}', b = c; +select * from t; +drop table if exists t; +create table t(a int auto_increment key, b int); +insert into t (b) value (a); +insert into t value (a, a+1); +set SQL_MODE=NO_AUTO_VALUE_ON_ZERO; +insert into t (b) value (a+1); +select * from t order by a; +set SQL_MODE=STRICT_ALL_TABLES; +drop table if exists t; +create table t(a int not null, b int, c int as (sqrt(a))); +insert t set b = a, a = 4; +select * from t; +set SQL_MODE=default; +drop table t; + +# TestDataTooLongErrMsg +drop table if exists t; +create table t(a varchar(2)); +--error 1406 +insert into t values('123'); +insert into t values('12'); +--error 1406 +update t set a = '123' where a = '12'; +drop table t; + +# TestDeferConstraintCheckForDelete +set tidb_constraint_check_in_place = 0; +set @@tidb_txn_mode = 'optimistic'; +drop table if exists t1, t2, t3, t4, t5; +create table t1(i int primary key, j int); +insert into t1 values(1, 2); +begin; +insert into t1 values(1, 3); +delete from t1 where j = 3; +-- error 1062 +commit; +rollback; +create table t2(i int, j int, unique index idx(i)); +insert into t2 values(1, 2); +begin; +insert into t2 values(1, 3); +delete from t2 where j = 3; +-- error 1062 +commit; +admin check table t2; +create table t3(i int, j int, primary key(i)); +begin; +insert into t3 values(1, 3); +delete from t3 where j = 3; +commit; +create table t4(i int, j int, primary key(i)); +begin; +insert into t4 values(1, 3); +delete from t4 where j = 3; +insert into t4 values(2, 3); +commit; +admin check table t4; +select * from t4; +create table t5(i int, j int, primary key(i)); +begin; +insert into t5 values(1, 3); +delete from t5 where j = 3; +insert into t5 values(1, 4); +commit; +admin check table t5; +select * from t5; +set tidb_constraint_check_in_place = default; +set @@tidb_txn_mode = default; + +# TestDefEnumInsert +drop table if exists test; +create table test (id int, prescription_type enum('a','b','c','d','e','f') NOT NULL, primary key(id)); +insert into test (id) values (1); +select prescription_type from test; +drop table if exists test; + +# TestIssue11059 +drop table if exists t; +create table t (pk int primary key, uk int unique, v int); +insert into t values (2, 11, 215); +insert into t values (3, 7, 2111); +--error 1062 +update t set pk = 2 where uk = 7; +drop table if exists t; + +# TestSetWithRefGenCol +drop table if exists t, te, tn, t1, t2, t3; +create table t (i int, j int as (i+1) not null); +insert into t set i = j + 1; +select * from t; +insert into t set i = j + 100; +select * from t; +create table te (i int); +insert into te set i = i + 10; +select * from te; +insert into te set i = i; +select * from te; +create table tn (i int not null); +insert into tn set i = i; +select * from tn; +insert into tn set i = i + 10; +select * from tn; +create table t1 (j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '10'); +insert into t1 values(); +select * from t1; +insert into t1 values(); +select * from t1; +create table t2 (j int(11) GENERATED ALWAYS AS (i + 1) stored not null, i int(11) DEFAULT '5'); +insert into t2 set i = j + 9; +select * from t2; +--error 3105 +insert into t2 set j = i + 1; +insert into t2 set i = j + 100; +select * from t2; +create table t3(j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '5'); +insert into t3 set i = j + 100; +select * from t3; +--error 3105 +insert into t3 set j = i + 1; +drop table if exists t, te, t1, t2, t3; + +# TestSetWithCurrentTimestampAndNow +drop table if exists t1; +create table t1(c1 timestamp default current_timestamp, c2 int, c3 timestamp default current_timestamp); +insert into t1 set c1 = current_timestamp, c2 = sleep(2); +select c1 = c3 from t1; +insert into t1 set c1 = current_timestamp, c2 = sleep(1); +select c1 = c3 from t1; +drop table if exists t1; + +# TestApplyWithPointAndBatchPointGet +drop table if exists t; +create table t ( c_int int, c_str varchar(40),c_datetime datetime, c_timestamp timestamp, +c_double double, c_decimal decimal(12, 6) , primary key(c_int, c_str) , unique key(c_int) , unique key(c_str) , +unique key(c_decimal) , unique key(c_datetime) , key(c_timestamp) ); +insert into t values (1, 'zen ardinghelli', '2020-02-03 18:15:17', '2020-03-11 05:47:11', 36.226534, 3.763), +(2, 'suspicious joliot', '2020-01-01 22:56:37', '2020-04-07 06:19:07', 62.756537, 5.567), +(3, 'keen zhukovsky', '2020-01-21 04:09:20', '2020-06-06 08:32:14', 33.115065, 1.381), +(4, 'crazy newton', '2020-02-14 21:37:56', '2020-04-28 08:33:48', 44.146318, 4.249), +(5, 'happy black', '2020-03-12 16:04:14', '2020-01-18 09:17:37', 41.962653, 5.959); +insert into t values (6, 'vigilant swartz', '2020-06-01 07:37:44', '2020-05-25 01:26:43', 56.352233, 2.202), +(7, 'suspicious germain', '2020-04-16 23:25:23', '2020-03-17 05:06:57', 55.897698, 3.460), +(8, 'festive chandrasekhar', '2020-02-11 23:40:29', '2020-04-08 10:13:04', 77.565691, 0.540), +(9, 'vigorous meninsky', '2020-02-17 10:03:17', '2020-01-02 15:02:02', 6.484815, 6.292), +(10, 'heuristic moser', '2020-04-20 12:18:49', '2020-06-20 20:20:18', 28.023822, 2.765); +insert into t values (11, 'sharp carver', '2020-03-01 11:23:41', '2020-03-23 17:59:05', 40.842442, 6.345), +(12, 'trusting noether', '2020-03-28 06:42:34', '2020-01-27 15:33:40', 49.544658, 4.811), +(13, 'objective ishizaka', '2020-01-28 17:30:55', '2020-04-02 17:45:39', 59.523930, 5.015), +(14, 'sad rhodes', '2020-03-30 21:43:37', '2020-06-09 06:53:53', 87.295753, 2.413), +(15, 'wonderful shockley', '2020-04-29 09:17:11', '2020-03-14 04:36:51', 6.778588, 8.497); +begin pessimistic; +insert into t values (13, 'vibrant yalow', '2020-05-15 06:59:05', '2020-05-03 05:58:45', 43.721929, 8.066), +(14, 'xenodochial spence', '2020-02-13 17:28:07', '2020-04-01 12:18:30', 19.981331, 5.774), +(22, 'eloquent neumann', '2020-02-10 16:00:20', '2020-03-28 00:24:42', 10.702532, 7.618) +on duplicate key update c_int=values(c_int), c_str=values(c_str), c_double=values(c_double), c_timestamp=values(c_timestamp); +select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +commit; +select sum((select t1.c_str from t t1 where t1.c_int = 11 and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +select sum((select t1.c_str from t t1 where t1.c_int in (11, 10086) and t1.c_str > t.c_str order by t1.c_decimal limit 1) is null) nulls +from t order by c_str; +drop table if exists t; + +# TestWriteListPartitionTable +set @@session.tidb_enable_list_partition = ON; +drop table if exists t; +create table t (id int, name varchar(10), unique index idx (id)) partition by list (id) ( + partition p0 values in (3,5,6,9,17), + partition p1 values in (1,2,10,11,19,20), + partition p2 values in (4,12,13,14,18), + partition p3 values in (7,8,15,16,null) + ); +insert into t values (1, 'a'); +update t set name='b' where id=2; +select * from t; +update t set name='b' where id=1; +select * from t; +replace into t values (1, 'c'); +select * from t; +insert into t values (1, 'd') on duplicate key update name='e'; +select * from t; +delete from t where id=1; +select * from t; +insert into t values (2, 'f'); +delete from t where name='f'; +select * from t; +insert into t values (1, 'a'); +-- error 1062 +insert into t values (1, 'd'); +-- error 1526 +insert into t values (100, 'd'); +admin check table t; +insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null); +select * from t partition (p0) order by id; +select * from t partition (p1,p3) order by id; +select * from t partition (p1,p3,p0,p2) order by id; +select * from t order by id; +delete from t partition (p0); +select * from t order by id; +delete from t partition (p3,p2); +select * from t order by id; +set @@session.tidb_enable_list_partition = default; +drop table if exists t; + +# TestWriteListColumnsPartitionTable +set @@session.tidb_enable_list_partition = ON; +drop table if exists t; +create table t (id int, name varchar(10), unique index idx (id)) partition by list columns (id) ( + partition p0 values in (3,5,6,9,17), + partition p1 values in (1,2,10,11,19,20), + partition p2 values in (4,12,13,14,18), + partition p3 values in (7,8,15,16,null) + ); +insert into t values (1, 'a'); +update t set name='b' where id=2; +select * from t; +update t set name='b' where id=1; +select * from t; +replace into t values (1, 'c'); +select * from t; +insert into t values (1, 'd') on duplicate key update name='e'; +select * from t; +delete from t where id=1; +select * from t; +insert into t values (2, 'f'); +delete from t where name='f'; +select * from t; +insert into t values (1, 'a'); +--error 1062 +insert into t values (1, 'd'); +--error 1526 +insert into t values (100, 'd'); +admin check table t; +insert into t values (2,'b'),(3,'c'),(4,'d'),(7,'f'), (null,null); +select * from t partition (p0) order by id; +select * from t partition (p1,p3) order by id; +select * from t partition (p1,p3,p0,p2) order by id; +select * from t order by id; +delete from t partition (p0); +select * from t order by id; +delete from t partition (p3,p2); +select * from t order by id; +set @@session.tidb_enable_list_partition = default; +drop table if exists t;