From 02ee5213c517b4e0f351a955cf6f5d73abdac959 Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Wed, 11 Oct 2023 14:16:15 +0800 Subject: [PATCH 1/6] update --- ddl/ddl_error_test.go | 28 - ddl/ddl_test.go | 2 - ddl/index_modify_test.go | 43 - ddl/integration_test.go | 84 -- ddl/modify_column_test.go | 352 +------ ddl/multi_schema_change_test.go | 479 ---------- ddl/partition_test.go | 100 -- ddl/primary_key_handle_test.go | 295 ------ ddl/reorg_partition_test.go | 793 ---------------- tests/integrationtest/r/ddl/ddl_error.result | 13 + .../integrationtest/r/ddl/index_modify.result | 26 + .../integrationtest/r/ddl/integration.result | 93 ++ .../r/ddl/modify_column.result | 887 ++++++++++++++++++ .../r/ddl/multi_schema_change.result | 443 +++++++++ tests/integrationtest/r/ddl/partition.result | 179 ++++ .../r/ddl/primary_key_handle.result | 316 +++++++ .../r/ddl/reorg_partition.result | 863 +++++++++++++++++ tests/integrationtest/t/ddl/ddl_error.test | 15 + tests/integrationtest/t/ddl/index_modify.test | 27 + tests/integrationtest/t/ddl/integration.test | 75 ++ .../integrationtest/t/ddl/modify_column.test | 757 +++++++++++++++ .../t/ddl/multi_schema_change.test | 366 ++++++++ tests/integrationtest/t/ddl/partition.test | 137 +++ .../t/ddl/primary_key_handle.test | 247 +++++ .../t/ddl/reorg_partition.test | 393 ++++++++ 25 files changed, 4852 insertions(+), 2161 deletions(-) create mode 100644 tests/integrationtest/r/ddl/ddl_error.result create mode 100644 tests/integrationtest/r/ddl/index_modify.result create mode 100644 tests/integrationtest/r/ddl/integration.result create mode 100644 tests/integrationtest/r/ddl/modify_column.result create mode 100644 tests/integrationtest/r/ddl/multi_schema_change.result create mode 100644 tests/integrationtest/r/ddl/partition.result create mode 100644 tests/integrationtest/r/ddl/primary_key_handle.result create mode 100644 tests/integrationtest/r/ddl/reorg_partition.result create mode 100644 tests/integrationtest/t/ddl/ddl_error.test create mode 100644 tests/integrationtest/t/ddl/index_modify.test create mode 100644 tests/integrationtest/t/ddl/integration.test create mode 100644 tests/integrationtest/t/ddl/modify_column.test create mode 100644 tests/integrationtest/t/ddl/multi_schema_change.test create mode 100644 tests/integrationtest/t/ddl/partition.test create mode 100644 tests/integrationtest/t/ddl/primary_key_handle.test create mode 100644 tests/integrationtest/t/ddl/reorg_partition.test diff --git a/ddl/ddl_error_test.go b/ddl/ddl_error_test.go index 521de5f0f0efb..180d20955001d 100644 --- a/ddl/ddl_error_test.go +++ b/ddl/ddl_error_test.go @@ -20,7 +20,6 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/errno" - "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/testkit" "github.com/stretchr/testify/require" ) @@ -181,30 +180,3 @@ func TestCreateDatabaseError(t *testing.T) { tk.MustExec("create database db1;") require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) } - -func TestRenameViewOverDifferentSchemaError(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - //init - tk.MustExec("use test") - tk.MustExec("drop database if exists test_2;") - tk.MustExec("drop table if exists table_1;") - tk.MustExec("drop view if exists view_1;") - - tk.MustExec("create database test_2;") - tk.MustExec("create table table_1 (a int);") - tk.MustExec("create view view_1 as select a from table_1;") - - //different schema - tk.MustGetErrCode("rename table test.view_1 to test_2.view_1;", errno.ErrForbidSchemaChange) - tk.MustGetErrMsg("rename table test.view_1 to test_2.view_1;", - infoschema.ErrForbidSchemaChange.GenWithStackByArgs("test", "test_2").Error(), - ) - tk.MustGetErrMsg("rename table test.view_1 to test_2.view_1;", - "[schema:1450]Changing schema from 'test' to 'test_2' is not allowed.", - ) - - //same schema - tk.MustExec("rename table test.view_1 to test.view_1000;") -} diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 224a473d0f1d7..73c28ffed3aa8 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -36,8 +36,6 @@ import ( "github.com/stretchr/testify/require" ) -const testLease = 5 * time.Millisecond - // DDLForTest exports for testing. type DDLForTest interface { // SetInterceptor sets the interceptor. diff --git a/ddl/index_modify_test.go b/ddl/index_modify_test.go index ef5787b4284e6..7b243e297b211 100644 --- a/ddl/index_modify_test.go +++ b/ddl/index_modify_test.go @@ -30,7 +30,6 @@ import ( "github.com/pingcap/tidb/ddl" testddlutil "github.com/pingcap/tidb/ddl/testutil" "github.com/pingcap/tidb/errno" - "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/sessionctx" @@ -985,48 +984,6 @@ LOOP: tk.MustExec("drop table test_drop_index") } -func TestAddMultiColumnsIndexClusterIndex(t *testing.T) { - store := testkit.CreateMockStoreWithSchemaLease(t, indexModifyLease) - tk := testkit.NewTestKit(t, store) - tk.MustExec("drop database if exists test_add_multi_col_index_clustered;") - tk.MustExec("create database test_add_multi_col_index_clustered;") - tk.MustExec("use test_add_multi_col_index_clustered;") - - tk.Session().GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeOn - tk.MustExec("create table t (a int, b varchar(10), c int, primary key (a, b));") - tk.MustExec("insert into t values (1, '1', 1), (2, '2', NULL), (3, '3', 3);") - tk.MustExec("create index idx on t (a, c);") - - tk.MustExec("admin check index t idx;") - tk.MustExec("admin check table t;") - - tk.MustExec("insert into t values (5, '5', 5), (6, '6', NULL);") - - tk.MustExec("admin check index t idx;") - tk.MustExec("admin check table t;") -} - -func TestAddIndexWithDupCols(t *testing.T) { - store := testkit.CreateMockStoreWithSchemaLease(t, indexModifyLease) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - err1 := infoschema.ErrColumnExists.GenWithStackByArgs("b") - err2 := infoschema.ErrColumnExists.GenWithStackByArgs("B") - - tk.MustExec("create table test_add_index_with_dup (a int, b int)") - err := tk.ExecToErr("create index c on test_add_index_with_dup(b, a, b)") - require.ErrorIs(t, err, errors.Cause(err1)) - err = tk.ExecToErr("create index c on test_add_index_with_dup(b, a, B)") - require.ErrorIs(t, err, errors.Cause(err2)) - err = tk.ExecToErr("alter table test_add_index_with_dup add index c (b, a, b)") - require.ErrorIs(t, err, errors.Cause(err1)) - err = tk.ExecToErr("alter table test_add_index_with_dup add index c (b, a, B)") - require.ErrorIs(t, err, errors.Cause(err2)) - - tk.MustExec("drop table test_add_index_with_dup") -} - func TestAnonymousIndex(t *testing.T) { store := testkit.CreateMockStoreWithSchemaLease(t, indexModifyLease, mockstore.WithDDLChecker()) diff --git a/ddl/integration_test.go b/ddl/integration_test.go index 46f26fa5e8237..adb348b65d658 100644 --- a/ddl/integration_test.go +++ b/ddl/integration_test.go @@ -15,7 +15,6 @@ package ddl_test import ( - "fmt" "testing" "github.com/pingcap/tidb/ddl/util/callback" @@ -24,63 +23,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestDefaultValueIsBinaryString(t *testing.T) { - store := testkit.CreateMockStore(t) - tests := []struct { - colTp string - defVal string - result string - }{ - {"char(10) charset gbk", "0xC4E3BAC3", "你好"}, - {"char(10) charset gbk", "'好'", "好"}, - {"varchar(10) charset gbk", "0xC4E3BAC3", "你好"}, - {"char(10) charset utf8mb4", "0xE4BDA0E5A5BD", "你好"}, - {"char(10) charset utf8mb4", "0b111001001011100010010110111001111001010110001100", "世界"}, - {"bit(48)", "0xE4BDA0E5A5BD", "你好"}, - {"enum('你好')", "0xE4BDA0E5A5BD", "你好"}, - {"set('你好')", "0xE4BDA0E5A5BD", "你好"}, - } - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - for _, tt := range tests { - tk.MustExec("drop table if exists t;") - template := "create table t (a %s default %s);" - tk.MustExec(fmt.Sprintf(template, tt.colTp, tt.defVal)) - tk.MustExec("insert into t values (default);") - tk.MustQuery("select a from t;").Check(testkit.Rows(tt.result)) - } - - // Test invalid default value. - tk.MustExec("drop table if exists t;") - // 0xE4BDA0E5A5BD81 is an invalid utf-8 string. - tk.MustGetErrMsg("create table t (a char(20) charset utf8mb4 default 0xE4BDA0E5A5BD81);", - "[ddl:1067]Invalid default value for 'a'") - tk.MustGetErrMsg("create table t (a blob default 0xE4BDA0E5A5BD81);", - "[ddl:1101]BLOB/TEXT/JSON column 'a' can't have a default value") -} - -// https://github.com/pingcap/tidb/issues/30740. -func TestDefaultValueInEnum(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - // The value 0x91 should not cause panic. - tk.MustExec("create table t(a enum('a', 0x91) charset gbk);") - tk.MustExec("insert into t values (1), (2);") // Use 1-base index to locate the value. - tk.MustQuery("select a from t;").Check(testkit.Rows("a", "?")) // 0x91 is replaced to '?'. - tk.MustExec("drop table t;") - tk.MustExec("create table t (a enum('a', 0x91)) charset gbk;") // Test for table charset. - tk.MustExec("insert into t values (1), (2);") - tk.MustQuery("select a from t;").Check(testkit.Rows("a", "?")) - tk.MustExec("drop table t;") - tk.MustGetErrMsg("create table t(a set('a', 0x91, '?') charset gbk);", - "[types:1291]Column 'a' has duplicated value '?' in SET") - // Test valid utf-8 string value in enum. - tk.MustExec("create table t (a enum('a', 0xE4BDA0E5A5BD) charset gbk);") - tk.MustExec("insert into t values (1), (2);") - tk.MustQuery("select a from t;").Check(testkit.Rows("a", "浣犲ソ")) -} - func TestDDLStatementsBackFill(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -116,29 +58,3 @@ func TestDDLStatementsBackFill(t *testing.T) { require.Equal(t, tc.expectedNeedReorg, needReorg, tc) } } - -func TestDDLOnCachedTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tests := []struct { - sql string - result string - }{ - {"drop table t", "[ddl:8242]'Drop Table' is unsupported on cache tables."}, - {"create index t_id on t (id)", "[ddl:8242]'Create Index' is unsupported on cache tables."}, - {"alter table t drop index c", "[ddl:8242]'Alter Table' is unsupported on cache tables."}, - {"alter table t add column (d int)", "[ddl:8242]'Alter Table' is unsupported on cache tables."}, - {"truncate table t", "[ddl:8242]'Truncate Table' is unsupported on cache tables."}, - {"rename table t to t1", "[ddl:8242]'Rename Table' is unsupported on cache tables."}, - } - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("create table t (id int, c int, index(c));") - tk.MustExec("alter table t cache;") - - for _, tt := range tests { - tk.MustGetErrMsg(tt.sql, tt.result) - } - - tk.MustExec("alter table t nocache;") - tk.MustExec("drop table if exists t;") -} diff --git a/ddl/modify_column_test.go b/ddl/modify_column_test.go index 3a48b883b38dc..47643e75e3dda 100644 --- a/ddl/modify_column_test.go +++ b/ddl/modify_column_test.go @@ -25,7 +25,6 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/ddl/util/callback" - "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/parser/ast" "github.com/pingcap/tidb/parser/model" @@ -436,33 +435,22 @@ func TestModifyColumnCharset(t *testing.T) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) } -func TestModifyColumnTime_TimeToYear(t *testing.T) { - outOfRangeCode := uint16(1264) - tests := []testModifyColumnTimeCase{ - // time to year, it's reasonable to return current year and discard the time (even if MySQL may get data out of range error). - {"time", `"30 20:00:12"`, "year", "", outOfRangeCode}, - {"time", `"30 20:00"`, "year", "", outOfRangeCode}, - {"time", `"30 20"`, "year", "", outOfRangeCode}, - {"time", `"20:00:12"`, "year", "", outOfRangeCode}, - {"time", `"20:00"`, "year", "", outOfRangeCode}, - {"time", `"12"`, "year", "2012", 0}, - {"time", `"200012"`, "year", "", outOfRangeCode}, - {"time", `200012`, "year", "", outOfRangeCode}, - {"time", `0012`, "year", "2012", 0}, - {"time", `12`, "year", "2012", 0}, - {"time", `"30 20:00:12.498"`, "year", "", outOfRangeCode}, - {"time", `"20:00:12.498"`, "year", "", outOfRangeCode}, - {"time", `"200012.498"`, "year", "", outOfRangeCode}, - {"time", `200012.498`, "year", "", outOfRangeCode}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimeToDate(t *testing.T) { +func TestModifyColumnTime(t *testing.T) { now := time.Now().UTC() now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) timeToDate1 := now.Format("2006-01-02") timeToDate2 := now.AddDate(0, 0, 30).Format("2006-01-02") + timeToDatetime1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToDatetime2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05") + timeToDatetime3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToDatetime4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToDatetime5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05") + timeToTimestamp1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToTimestamp2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05") + timeToTimestamp3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToTimestamp4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") + timeToTimestamp5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05") + tests := []testModifyColumnTimeCase{ // time to date {"time", `"30 20:00:12"`, "date", timeToDate2, 0}, @@ -479,19 +467,6 @@ func TestModifyColumnTime_TimeToDate(t *testing.T) { {"time", `"20:00:12.498"`, "date", timeToDate1, 0}, {"time", `"200012.498"`, "date", timeToDate1, 0}, {"time", `200012.498`, "date", timeToDate1, 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimeToDatetime(t *testing.T) { - now := time.Now().UTC() - now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) - timeToDatetime1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToDatetime2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05") - timeToDatetime3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToDatetime4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToDatetime5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05") - tests := []testModifyColumnTimeCase{ // time to datetime {"time", `"30 20:00:12"`, "datetime", timeToDatetime4, 0}, {"time", `"30 20:00"`, "datetime", timeToDatetime5, 0}, @@ -507,19 +482,6 @@ func TestModifyColumnTime_TimeToDatetime(t *testing.T) { {"time", `"20:00:12.498"`, "datetime", timeToDatetime1, 0}, {"time", `"200012.498"`, "datetime", timeToDatetime1, 0}, {"time", `200012.498`, "datetime", timeToDatetime1, 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimeToTimestamp(t *testing.T) { - now := time.Now().UTC() - now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) - timeToTimestamp1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToTimestamp2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05") - timeToTimestamp3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToTimestamp4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05") - timeToTimestamp5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05") - tests := []testModifyColumnTimeCase{ // time to timestamp {"time", `"30 20:00:12"`, "timestamp", timeToTimestamp4, 0}, {"time", `"30 20:00"`, "timestamp", timeToTimestamp5, 0}, @@ -539,247 +501,6 @@ func TestModifyColumnTime_TimeToTimestamp(t *testing.T) { testModifyColumnTime(t, tests) } -func TestModifyColumnTime_DateToTime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // date to time - {"date", `"2019-01-02"`, "time", "00:00:00", 0}, - {"date", `"19-01-02"`, "time", "00:00:00", 0}, - {"date", `"20190102"`, "time", "00:00:00", 0}, - {"date", `"190102"`, "time", "00:00:00", 0}, - {"date", `20190102`, "time", "00:00:00", 0}, - {"date", `190102`, "time", "00:00:00", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DateToYear(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // date to year - {"date", `"2019-01-02"`, "year", "2019", 0}, - {"date", `"19-01-02"`, "year", "2019", 0}, - {"date", `"20190102"`, "year", "2019", 0}, - {"date", `"190102"`, "year", "2019", 0}, - {"date", `20190102`, "year", "2019", 0}, - {"date", `190102`, "year", "2019", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DateToDatetime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // date to datetime - {"date", `"2019-01-02"`, "datetime", "2019-01-02 00:00:00", 0}, - {"date", `"19-01-02"`, "datetime", "2019-01-02 00:00:00", 0}, - {"date", `"20190102"`, "datetime", "2019-01-02 00:00:00", 0}, - {"date", `"190102"`, "datetime", "2019-01-02 00:00:00", 0}, - {"date", `20190102`, "datetime", "2019-01-02 00:00:00", 0}, - {"date", `190102`, "datetime", "2019-01-02 00:00:00", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DateToTimestamp(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // date to timestamp - {"date", `"2019-01-02"`, "timestamp", "2019-01-02 00:00:00", 0}, - {"date", `"19-01-02"`, "timestamp", "2019-01-02 00:00:00", 0}, - {"date", `"20190102"`, "timestamp", "2019-01-02 00:00:00", 0}, - {"date", `"190102"`, "timestamp", "2019-01-02 00:00:00", 0}, - {"date", `20190102`, "timestamp", "2019-01-02 00:00:00", 0}, - {"date", `190102`, "timestamp", "2019-01-02 00:00:00", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimestampToYear(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // timestamp to year - {"timestamp", `"2006-01-02 15:04:05"`, "year", "2006", 0}, - {"timestamp", `"06-01-02 15:04:05"`, "year", "2006", 0}, - {"timestamp", `"20060102150405"`, "year", "2006", 0}, - {"timestamp", `"060102150405"`, "year", "2006", 0}, - {"timestamp", `20060102150405`, "year", "2006", 0}, - {"timestamp", `060102150405`, "year", "2006", 0}, - {"timestamp", `"2006-01-02 23:59:59.506"`, "year", "2006", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimestampToTime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // timestamp to time - {"timestamp", `"2006-01-02 15:04:05"`, "time", "15:04:05", 0}, - {"timestamp", `"06-01-02 15:04:05"`, "time", "15:04:05", 0}, - {"timestamp", `"20060102150405"`, "time", "15:04:05", 0}, - {"timestamp", `"060102150405"`, "time", "15:04:05", 0}, - {"timestamp", `20060102150405`, "time", "15:04:05", 0}, - {"timestamp", `060102150405`, "time", "15:04:05", 0}, - {"timestamp", `"2006-01-02 23:59:59.506"`, "time", "00:00:00", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimestampToDate(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // timestamp to date - {"timestamp", `"2006-01-02 15:04:05"`, "date", "2006-01-02", 0}, - {"timestamp", `"06-01-02 15:04:05"`, "date", "2006-01-02", 0}, - {"timestamp", `"20060102150405"`, "date", "2006-01-02", 0}, - {"timestamp", `"060102150405"`, "date", "2006-01-02", 0}, - {"timestamp", `20060102150405`, "date", "2006-01-02", 0}, - {"timestamp", `060102150405`, "date", "2006-01-02", 0}, - {"timestamp", `"2006-01-02 23:59:59.506"`, "date", "2006-01-03", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_TimestampToDatetime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // timestamp to datetime - {"timestamp", `"2006-01-02 15:04:05"`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `"06-01-02 15:04:05"`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `"20060102150405"`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `"060102150405"`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `20060102150405`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `060102150405`, "datetime", "2006-01-02 15:04:05", 0}, - {"timestamp", `"2006-01-02 23:59:59.506"`, "datetime", "2006-01-03 00:00:00", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DatetimeToYear(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // datetime to year - {"datetime", `"2006-01-02 15:04:05"`, "year", "2006", 0}, - {"datetime", `"06-01-02 15:04:05"`, "year", "2006", 0}, - {"datetime", `"20060102150405"`, "year", "2006", 0}, - {"datetime", `"060102150405"`, "year", "2006", 0}, - {"datetime", `20060102150405`, "year", "2006", 0}, - {"datetime", `060102150405`, "year", "2006", 0}, - {"datetime", `"2006-01-02 23:59:59.506"`, "year", "2006", 0}, - {"datetime", `"1000-01-02 23:59:59"`, "year", "", errno.ErrWarnDataOutOfRange}, - {"datetime", `"9999-01-02 23:59:59"`, "year", "", errno.ErrWarnDataOutOfRange}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DatetimeToTime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // datetime to time - {"datetime", `"2006-01-02 15:04:05"`, "time", "15:04:05", 0}, - {"datetime", `"06-01-02 15:04:05"`, "time", "15:04:05", 0}, - {"datetime", `"20060102150405"`, "time", "15:04:05", 0}, - {"datetime", `"060102150405"`, "time", "15:04:05", 0}, - {"datetime", `20060102150405`, "time", "15:04:05", 0}, - {"datetime", `060102150405`, "time", "15:04:05", 0}, - {"datetime", `"2006-01-02 23:59:59.506"`, "time", "00:00:00", 0}, - {"datetime", `"1000-01-02 23:59:59"`, "time", "23:59:59", 0}, - {"datetime", `"9999-01-02 23:59:59"`, "time", "23:59:59", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DatetimeToDate(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // datetime to date - {"datetime", `"2006-01-02 15:04:05"`, "date", "2006-01-02", 0}, - {"datetime", `"06-01-02 15:04:05"`, "date", "2006-01-02", 0}, - {"datetime", `"20060102150405"`, "date", "2006-01-02", 0}, - {"datetime", `"060102150405"`, "date", "2006-01-02", 0}, - {"datetime", `20060102150405`, "date", "2006-01-02", 0}, - {"datetime", `060102150405`, "date", "2006-01-02", 0}, - {"datetime", `"2006-01-02 23:59:59.506"`, "date", "2006-01-03", 0}, - {"datetime", `"1000-01-02 23:59:59"`, "date", "1000-01-02", 0}, - {"datetime", `"9999-01-02 23:59:59"`, "date", "9999-01-02", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_DatetimeToTimestamp(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // datetime to timestamp - {"datetime", `"2006-01-02 15:04:05"`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `"06-01-02 15:04:05"`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `"20060102150405"`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `"060102150405"`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `20060102150405`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `060102150405`, "timestamp", "2006-01-02 15:04:05", 0}, - {"datetime", `"2006-01-02 23:59:59.506"`, "timestamp", "2006-01-03 00:00:00", 0}, - {"datetime", `"1971-01-02 23:59:59"`, "timestamp", "1971-01-02 23:59:59", 0}, - {"datetime", `"2009-01-02 23:59:59"`, "timestamp", "2009-01-02 23:59:59", 0}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_YearToTime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // year to time - // failed cases are not handled by TiDB - {"year", `"2019"`, "time", "00:20:19", 0}, - {"year", `2019`, "time", "00:20:19", 0}, - {"year", `"00"`, "time", "00:20:00", 0}, - {"year", `"69"`, "time", "", errno.ErrTruncatedWrongValue}, - {"year", `"70"`, "time", "", errno.ErrTruncatedWrongValue}, - {"year", `"99"`, "time", "", errno.ErrTruncatedWrongValue}, - {"year", `00`, "time", "00:00:00", 0}, - {"year", `69`, "time", "", errno.ErrTruncatedWrongValue}, - {"year", `70`, "time", "", errno.ErrTruncatedWrongValue}, - {"year", `99`, "time", "", errno.ErrTruncatedWrongValue}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_YearToDate(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // year to date - {"year", `"2019"`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `2019`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `"00"`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `"69"`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `"70"`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `"99"`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `00`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `69`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `70`, "date", "", errno.ErrTruncatedWrongValue}, - {"year", `99`, "date", "", errno.ErrTruncatedWrongValue}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_YearToDatetime(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // year to datetime - {"year", `"2019"`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `2019`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `"00"`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `"69"`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `"70"`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `"99"`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `00`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `69`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `70`, "datetime", "", errno.ErrTruncatedWrongValue}, - {"year", `99`, "datetime", "", errno.ErrTruncatedWrongValue}, - } - testModifyColumnTime(t, tests) -} - -func TestModifyColumnTime_YearToTimestamp(t *testing.T) { - tests := []testModifyColumnTimeCase{ - // year to timestamp - {"year", `"2019"`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `2019`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `"00"`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `"69"`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `"70"`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `"99"`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `00`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `69`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `70`, "timestamp", "", errno.ErrTruncatedWrongValue}, - {"year", `99`, "timestamp", "", errno.ErrTruncatedWrongValue}, - } - testModifyColumnTime(t, tests) -} - type testModifyColumnTimeCase struct { from string value string @@ -789,19 +510,15 @@ type testModifyColumnTimeCase struct { } func testModifyColumnTime(t *testing.T, tests []testModifyColumnTimeCase) { - limit := variable.GetDDLErrorCountLimit() - store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("set @@global.tidb_ddl_error_count_limit = 3") + tk.MustExec("set @@time_zone=UTC") - // Set time zone to UTC. - originalTz := tk.Session().GetSessionVars().TimeZone - tk.Session().GetSessionVars().TimeZone = time.UTC defer func() { - tk.MustExec(fmt.Sprintf("set @@global.tidb_ddl_error_count_limit = %v", limit)) - tk.Session().GetSessionVars().TimeZone = originalTz + tk.MustExec("set @@global.tidb_ddl_error_count_limit = default") + tk.MustExec("set @@time_zone=default") }() for _, test := range tests { @@ -820,35 +537,6 @@ func testModifyColumnTime(t *testing.T, tests []testModifyColumnTimeCase) { } } -func TestModifyColumnTypeWithWarnings(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test normal warnings. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a decimal(5,2))") - tk.MustExec("insert into t values(111.22),(111.22),(111.22),(111.22),(333.4)") - // 111.22 will be truncated the fraction .22 as .2 with truncated warning for each row. - tk.MustExec("alter table t modify column a decimal(4,1)") - // there should 4 rows of warnings corresponding to the origin rows. - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 4 warnings with this error code, first warning: Truncated incorrect DECIMAL value: '111.22'")) - - // Test the strict warnings is treated as errors under the strict mode. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a decimal(5,2))") - tk.MustExec("insert into t values(111.22),(111.22),(111.22),(33.4)") - // Since modify column a from decimal(5,2) to decimal(3,1), the first three rows with 111.22 will overflows the target types. - err := tk.ExecToErr("alter table t modify column a decimal(3,1)") - require.EqualError(t, err, "[types:1690]DECIMAL value is out of range in '(3, 1)'") - - // Test the strict warnings is treated as warnings under the non-strict mode. - tk.MustExec("set @@sql_mode=\"\"") - tk.MustExec("alter table t modify column a decimal(3,1)") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1690 3 warnings with this error code, first warning: DECIMAL value is out of range in '(3, 1)'")) -} - // TestModifyColumnTypeWhenInterception is to test modifying column type with warnings intercepted by // reorg timeout, not owner error and so on. func TestModifyColumnTypeWhenInterception(t *testing.T) { @@ -880,13 +568,3 @@ func TestModifyColumnTypeWhenInterception(t *testing.T) { tk.MustExec("alter table t modify column b decimal(3,1)") tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 4096 warnings with this error code, first warning: Truncated incorrect DECIMAL value: '11.22'")) } - -func TestModifyColumnAutoIncrementWithDefaultValue(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t (a bigint auto_increment primary key)") - - tk.MustGetErrMsg("alter table t modify column a bigint auto_increment default 3", "[ddl:1067]Invalid default value for 'a'") -} diff --git a/ddl/multi_schema_change_test.go b/ddl/multi_schema_change_test.go index 89a469c5c3cf9..38e1ddac016c8 100644 --- a/ddl/multi_schema_change_test.go +++ b/ddl/multi_schema_change_test.go @@ -32,107 +32,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestMultiSchemaChangeAddColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test add multiple columns in multiple specs. - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (1);") - tk.MustExec("alter table t add column b int default 2, add column c int default 3;") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3")) - - // Test add multiple columns and constraints in one spec. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (1);") - tk.MustExec("alter table t add column (b int default 2, c int default 3);") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t add column (d int default 4, e int default 5);") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4 5")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (1);") - tk.MustExec("alter table t add column (index i(a), index i1(a));") - tk.MustQuery("select * from t use index (i, i1);").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (1);") - tk.MustExec("alter table t add column (b int default 2, index i(a), primary key (a));") - tk.MustQuery("select * from t use index (i, primary)").Check(testkit.Rows("1 2")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (1);") - tk.MustExec("alter table t add column (index i(a));") - tk.MustQuery("select * from t use index (i)").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t add column (index i1(a, b, c), index i2(c, b, a), index i3((a + 1)), index i4((c - 1)));") - tk.MustQuery("select * from t use index (i1, i2);").Check(testkit.Rows("1 2 3")) - tk.MustExec("admin check table t;") - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t add column if not exists (b int default 2, c int default 3);") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3")) - tk.MustExec("alter table t add column if not exists (c int default 3, d int default 4);") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1060 Duplicate column name 'c'")) - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 4")) - - // Test referencing previous column in multi-schema change is not supported. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int);") - tk.MustGetErrCode("alter table t add column b int after a, add column c int after b", errno.ErrBadField) - tk.MustGetErrCode("alter table t add column c int after b, add column b int", errno.ErrBadField) - - // Test add multiple columns with different position. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec(`alter table t - add column d int default 4 first, - add column e int default 5 after b, - add column f int default 6 after b;`) - tk.MustQuery("select * from t;").Check(testkit.Rows("4 1 2 6 5 3")) - - // Test [if not exists] for adding columns. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t add column b int default 2, add column if not exists a int;") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1060 Duplicate column name 'a'")) - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2")) - - // Test add generate column - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustExec("alter table t add column c double default 3.0, add column d double as (a + b);") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3 3")) - - // Test add columns with same name - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, c int default 4);") - tk.MustGetErrCode("alter table t add column b int default 2, add column b int default 3", errno.ErrUnsupportedDDLOperation) - - // Test add generate column dependents on a modifying column - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustGetErrCode("alter table t modify column b double, add column c double as (a + b);", errno.ErrUnsupportedDDLOperation) -} - func TestMultiSchemaChangeAddColumnsCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -179,42 +78,6 @@ func TestMultiSchemaChangeAddColumnsParallel(t *testing.T) { }) } -func TestMultiSchemaChangeDropColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - // Test drop all columns - tk.MustExec("create table t (a int, b int);") - tk.MustGetErrCode("alter table t drop column a, drop column b;", errno.ErrCantRemoveAllFields) - - // Test drop multiple columns in multiple specs - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, d int, e int);") - tk.MustExec("insert into t values (1, 2, 3, 4, 5);") - tk.MustExec("alter table t drop column a, drop column d, drop column b;") - tk.MustQuery("select * from t;").Check(testkit.Rows("3 5")) - - // Test drop same column - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, c int default 4);") - tk.MustGetErrCode("alter table t drop column a, drop column a", errno.ErrUnsupportedDDLOperation) - - // Test drop if exists column. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t drop column if exists c, drop column a;") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1091 Can't DROP 'c'; check that column/key exists")) - tk.MustQuery("select * from t;").Check(testkit.Rows("2")) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2, c int default 3);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t drop column a, drop column if exists d, drop column c;") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1091 Can't DROP 'd'; check that column/key exists")) - tk.MustQuery("select * from t;").Check(testkit.Rows("2")) -} - func TestMultiSchemaChangeDropColumnsCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -300,31 +163,6 @@ func TestMultiSchemaChangeDropColumnsParallel(t *testing.T) { }) } -func TestMultiSchemaChangeAddDropColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - // [a, b] -> [+c, -a, +d, -b] -> [c, d] - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t add column c int default 3, drop column a, add column d int default 4, drop column b;") - tk.MustQuery("select * from t;").Check(testkit.Rows("3 4")) - - // [a, b] -> [-a, -b, +c, +d] -> [c, d] - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t drop column a, drop column b, add column c int default 3, add column d int default 4;") - tk.MustQuery("select * from t;").Check(testkit.Rows("3 4")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustGetErrCode("alter table t add column c int default 3 after a, add column d int default 4 first, drop column a, drop column b;", errno.ErrUnsupportedDDLOperation) -} - func TestMultiSchemaChangeRenameColumns(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) originHook := dom.DDL().GetHook() @@ -529,43 +367,6 @@ func TestMultiSchemaChangeChangeColumns(t *testing.T) { tk.MustGetErrCode("select d from t", errno.ErrBadField) } -func TestMultiSchemaChangeAddIndexes(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test add multiple indexes with same column. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t add index t(a, b), add index t1(a);") - tk.MustExec("alter table t add index t2(a), add index t3(a, b);") - tk.MustQuery("select * from t use index (t, t1, t2, t3);").Check(testkit.Rows("1 2 3")) - tk.MustExec("admin check table t;") - - // Test add multiple indexes with same name. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int)") - tk.MustGetErrCode("alter table t add index t(a), add index t(b)", errno.ErrUnsupportedDDLOperation) - tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ )) - - // Test add indexes with drop column. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int)") - tk.MustGetErrCode("alter table t add index t(a), drop column a", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add index t(a, b), drop column a", errno.ErrUnsupportedDDLOperation) - tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ )) - - // Test add index failed. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 1);") - tk.MustGetErrCode("alter table t add unique index i1(a), add unique index i2(a, b), add unique index i3(c);", - errno.ErrDupEntry) - tk.MustQuery("show index from t;").Check(testkit.Rows( /* no index */ )) - tk.MustExec("alter table t add index i1(a), add index i2(a, b), add index i3(c);") -} - func TestMultiSchemaChangeAddIndexesCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -615,34 +416,6 @@ func TestMultiSchemaChangeAddIndexesCancelled(t *testing.T) { tk.MustExec("admin check table t;") } -func TestMultiSchemaChangeDropIndexes(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - // Test drop same index. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, index t(a));") - tk.MustGetErrCode("alter table t drop index t, drop index t", errno.ErrUnsupportedDDLOperation) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (id int, c1 int, c2 int, primary key(id) nonclustered, key i1(c1), key i2(c2), key i3(c1, c2));") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t drop index i1, drop index i2;") - tk.MustGetErrCode("select * from t use index(i1);", errno.ErrKeyDoesNotExist) - tk.MustGetErrCode("select * from t use index(i2);", errno.ErrKeyDoesNotExist) - tk.MustExec("alter table t drop index i3, drop primary key;") - tk.MustGetErrCode("select * from t use index(primary);", errno.ErrKeyDoesNotExist) - tk.MustGetErrCode("select * from t use index(i3);", errno.ErrKeyDoesNotExist) - - // Test drop index with drop column. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int default 1, b int default 2, c int default 3, index t(a))") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t drop index t, drop column a") - tk.MustGetErrCode("select * from t force index(t)", errno.ErrKeyDoesNotExist) -} - func TestMultiSchemaChangeDropIndexesCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -703,32 +476,6 @@ func TestMultiSchemaChangeDropIndexesParallel(t *testing.T) { }) } -func TestMultiSchemaChangeAddDropIndexes(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test add and drop same index. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int, index t(a))") - tk.MustGetErrCode("alter table t drop index t, add index t(b)", errno.ErrDupKeyName) - - // Test add and drop same index. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int, index t(a))") - tk.MustGetErrCode("alter table t add index t1(b), drop index t1", errno.ErrCantDropFieldOrKey) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, index (a), index(b), index(c));") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t add index xa(a), drop index a, add index xc(c), drop index b, drop index c, add index xb(b);") - tk.MustQuery("select * from t use index(xa, xb, xc);").Check(testkit.Rows("1 2 3")) - tk.MustGetErrCode("select * from t use index(a);", errno.ErrKeyDoesNotExist) - tk.MustGetErrCode("select * from t use index(b);", errno.ErrKeyDoesNotExist) - tk.MustGetErrCode("select * from t use index(c);", errno.ErrKeyDoesNotExist) - tk.MustExec("admin check table t;") -} - func TestMultiSchemaChangeRenameIndexes(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -781,134 +528,6 @@ func TestMultiSchemaChangeRenameIndexes(t *testing.T) { tk.MustGetErrCode("select * from t use index (t1);", errno.ErrKeyDoesNotExist) } -func TestMultiSchemaChangeModifyColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - // unsupported ddl operations - { - // Test modify the same column twice. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustGetErrCode("alter table t modify column a int default 2, modify column a bigint;", errno.ErrUnsupportedDDLOperation) - - // Test modify and drop with same column - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustGetErrCode("alter table t modify column b double, drop column b", errno.ErrUnsupportedDDLOperation) - - // Test modify column related with dropped column - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2, c int default 3);") - tk.MustExec("insert into t values ();") - tk.MustGetErrCode("alter table t modify column b double after c, drop column c", errno.ErrUnsupportedDDLOperation) - - // Test modify column related with add index - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustGetErrCode("alter table t add index i(a), modify column a int null default 1 after a;", errno.ErrUnsupportedDDLOperation) - - // Test modify column related with add primary index - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustGetErrCode("alter table t add primary key(a), modify column a int null default 1 after a;", errno.ErrUnsupportedDDLOperation) - - // Test modify column related with expression index - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustGetErrCode("alter table t modify column b double, add index idx((a + b));", errno.ErrUnsupportedDDLOperation) - } - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int default 1, b int default 2);") - tk.MustExec("insert into t values ();") - tk.MustExec("alter table t modify column b double default 2 after a, add column c int default 3 after a;") - tk.MustQuery("select * from t").Check(testkit.Rows("1 3 2")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int);") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t modify column a bigint, modify column b bigint;") - tk.MustExec("insert into t values (9223372036854775807, 9223372036854775807, 1);") - tk.MustQuery("select * from t;").Check( - testkit.Rows("1 2 3", "9223372036854775807 9223372036854775807 1")) - - // Modify index-covered columns. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c));") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t modify column a tinyint, modify column b tinyint, modify column c tinyint;") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 2 3")) - tk.MustQuery("select * from t use index(i1, i2, i3, i4, i5);").Check(testkit.Rows("1 2 3")) - tk.MustExec("admin check table t;") - - // Modify index-covered columns with position change. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c));") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t modify column a tinyint after c, modify column b tinyint, modify column c tinyint first;") - tk.MustQuery("select * from t;").Check(testkit.Rows("3 2 1")) - tk.MustQuery("select * from t use index(i1, i2, i3, i4, i5);").Check(testkit.Rows("3 2 1")) - tk.MustExec("admin check table t;") - - // Modify columns that require and don't require reorganization of data. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int, c int, index i1(a), index i2(c, b));") - tk.MustExec("insert into t values (1, 2, 3), (11, 22, 33);") - tk.MustExec("alter table t modify column b char(255) after c, modify column a bigint;") - tk.MustQuery("select * from t;").Check(testkit.Rows("1 3 2", "11 33 22")) - tk.MustQuery("select * from t use index(i1, i2);").Check(testkit.Rows("1 3 2", "11 33 22")) - tk.MustExec("admin check table t;") - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a bigint null default '1761233443433596323', index t(a));") - tk.MustExec("insert into t set a = '-7184819032643664798';") - tk.MustGetErrCode("alter table t change column a b datetime null default '8972-12-24 10:56:03', rename index t to t1;", errno.ErrTruncatedWrongValue) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b double, index i(a, b));") - tk.MustExec("alter table t rename index i to i1, change column b c int;") - tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1';").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b double, index i(a, b), index _Idx$_i(a, b));") - tk.MustExec("alter table t rename index i to i1, change column b c int;") - tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1';").Check(testkit.Rows("1")) - tk.MustQuery("select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i';").Check(testkit.Rows("1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a));") - tk.MustExec("alter table t modify column a tinyint;") - tk.MustQuery("select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t';").Check(testkit.Rows("2")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a BIGINT NULL DEFAULT '-283977870758975838', b double);") - tk.MustExec("insert into t values (-283977870758975838, 0);") - tk.MustGetErrCode("alter table t change column a c tinyint null default '111' after b, modify column b time null default '13:51:02' FIRST;", errno.ErrDataOutOfRange) - rows := tk.MustQuery("admin show ddl jobs 1").Rows() - require.Equal(t, rows[0][11], "rollback done") - require.Equal(t, rows[1][11], "rollback done") - require.Equal(t, rows[2][11], "cancelled") - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int, b int);") - tk.MustExec("insert into t values (1, 2);") - tk.MustGetErrCode("alter table t add index i(b), modify column a int null default 1 after a;", errno.ErrBadField) - - // Test rolling back modify column with reorganization. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a char(3), b int, unique index i1(a), index i2(a, b));") - tk.MustExec("insert into t values ('aaa', 1), ('aa', 2);") - tk.MustExec("set @@sql_mode = '';") // Make it possible to truncate 'aaa' to 'aa'. - tk.MustGetErrCode("alter table t modify column b tinyint, modify column a char(2);", errno.ErrDupEntry) -} - func TestMultiSchemaChangeModifyColumnsCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -998,22 +617,6 @@ func TestMultiSchemaChangeAlterIndex(t *testing.T) { tk.MustExec("admin check table t;") } -func TestMultiSchemaChangeMix(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - tk.MustExec("create table t (a int, b int, c int, index i1(c), index i2(c));") - tk.MustExec("insert into t values (1, 2, 3);") - tk.MustExec("alter table t add column d int default 4, add index i3(c), " + - "drop column a, drop column if exists z, add column if not exists e int default 5, " + - "drop index i2, add column f int default 6, drop column b, drop index i1, add column if not exists c int;") - tk.MustQuery("select * from t;").Check(testkit.Rows("3 4 5 6")) - tk.MustGetErrCode("select * from t use index (i1);", errno.ErrKeyDoesNotExist) - tk.MustGetErrCode("select * from t use index (i2);", errno.ErrKeyDoesNotExist) - tk.MustQuery("select * from t use index (i3);").Check(testkit.Rows("3 4 5 6")) -} - func TestMultiSchemaChangeMixCancelled(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -1074,52 +677,6 @@ func TestMultiSchemaChangeAdminShowDDLJobs(t *testing.T) { dom.DDL().SetHook(originHook) } -func TestMultiSchemaChangeTableOption(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - tk.MustExec("create table t (a int auto_increment primary key, b int);") - tk.MustExec("alter table t modify column b tinyint, auto_increment = 100;") - tk.MustExec("insert into t (b) values (1);") - tk.MustQuery("select * from t;").Check(testkit.Rows("100 1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int auto_increment primary key, b int);") - tk.MustExec("alter table t auto_increment = 110, auto_increment = 90;") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 1105 Can't reset AUTO_INCREMENT to 90 without FORCE option, using 110 instead")) - tk.MustExec("insert into t (b) values (1);") - tk.MustQuery("select * from t;").Check(testkit.Rows("110 1")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int) charset = utf8 shard_row_id_bits=2;") - tk.MustExec("alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4;") - tk.MustQuery("select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't';"). - Check(testkit.Rows("SHARD_BITS=2 abc utf8mb4_bin")) -} - -func TestMultiSchemaChangeNonPublicDefaultValue(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("create table t (a tinyint);") - tk.MustExec("insert into t set a = 10;") - tk.MustExec("alter table t add column b int not null, change column a c char(5) first;") - tk.MustQuery("select * from t;").Check(testkit.Rows("10 0")) -} - -func TestMultiSchemaChangeAlterIndexVisibility(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("create table t (a int, b int, index idx(b));") - tk.MustExec("alter table t add index idx2(a), alter index idx visible;") - tk.MustQuery("select * from t use index (idx, idx2);").Check(testkit.Rows( /* no rows */ )) - tk.MustGetErrCode("alter table t drop column b, alter index idx invisible;", errno.ErrKeyDoesNotExist) - tk.MustQuery("select a, b from t;").Check(testkit.Rows( /* no rows */ )) -} - func TestMultiSchemaChangeWithExpressionIndex(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -1175,16 +732,6 @@ func TestMultiSchemaChangeNoSubJobs(t *testing.T) { require.Equal(t, "create table", rs[0][3]) } -func TestMultiSchemaChangeUnsupportedType(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - - tk.MustExec("create table t (a int, b int);") - tk.MustGetErrMsg("alter table t add column c int, auto_id_cache = 10;", - "[ddl:8200]Unsupported multi schema change for modify auto id cache") -} - func TestMultiSchemaChangeSchemaVersion(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -1212,32 +759,6 @@ func TestMultiSchemaChangeSchemaVersion(t *testing.T) { dom.DDL().SetHook(originHook) } -func TestMultiSchemaChangeAddIndexChangeColumn(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("CREATE TABLE t (a SMALLINT DEFAULT '30219', b TIME NULL DEFAULT '02:45:06', PRIMARY KEY (a));") - tk.MustExec("ALTER TABLE t ADD unique INDEX idx4 (b), change column a e MEDIUMINT DEFAULT '5280454' FIRST;") - tk.MustExec("insert ignore into t (e) values (5586359),(501788),(-5961048),(220083),(-4917129),(-7267211),(7750448);") - tk.MustQuery("select * from t;").Check(testkit.Rows("5586359 02:45:06")) - tk.MustExec("admin check table t;") -} - -func TestMultiSchemaChangeAddIndexOrder(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("create table t (a int);") - tk.MustExec("insert into t values (123);") - tk.MustExec("alter table t add index i(a), add primary key (a);") - result := "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " KEY `i` (`a`),\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" - tk.MustQuery("show create table t;").Check(testkit.Rows(result)) -} - func TestMultiSchemaChangeMixedWithUpdate(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) diff --git a/ddl/partition_test.go b/ddl/partition_test.go index d345454a14e80..325fe6a1795da 100644 --- a/ddl/partition_test.go +++ b/ddl/partition_test.go @@ -15,7 +15,6 @@ package ddl_test import ( - "fmt" "testing" "time" @@ -247,102 +246,3 @@ func TestReorganizePartitionRollback(t *testing.T) { // test then add index should success tk.MustExec("alter table t1 add index idx_kc (k, c)") } - -func TestAlterPartitionBy(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create schema AlterPartitionBy") - tk.MustExec("use AlterPartitionBy") - // Just for debug... - //tk.MustExec(`create table t (a int primary key, b varchar(255), key (b)) partition by range (a) (partition p0 values less than (10))`) - // First easy example non-partitioned -> partitioned - tk.MustExec(`create table t (a int primary key, b varchar(255), key (b))`) - for i := 0; i < 1000; i++ { - tk.MustExec(fmt.Sprintf(`insert into t values (%d,'filler%d')`, i, i/3)) - } - tk.MustExec(`alter table t partition by range (a) (partition p0 values less than (1000000), partition p1 values less than (2000000), partition pMax values less than (maxvalue))`) - tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 The statistics of new partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now")) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN (1000000),\n" + - " PARTITION `p1` VALUES LESS THAN (2000000),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`alter table t partition by hash(a) partitions 7`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`a`) PARTITIONS 7")) - tk.MustExec(`alter table t partition by key(a) partitions 5`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY KEY (`a`) PARTITIONS 5")) -} - -func TestReorgRangeTimestampMaxvalue(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create schema AlterPartitionBy") - tk.MustExec("use AlterPartitionBy") - tk.MustExec(`CREATE TABLE t1 ( -a timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -b varchar(10), -PRIMARY KEY (a) -) -PARTITION BY RANGE (UNIX_TIMESTAMP(a)) ( -PARTITION p1 VALUES LESS THAN (1199134800), -PARTITION pmax VALUES LESS THAN MAXVALUE -)`) - - tk.MustExec(`ALTER TABLE t1 REORGANIZE PARTITION pmax INTO ( -PARTITION p3 VALUES LESS THAN (1247688000), -PARTITION pmax VALUES LESS THAN MAXVALUE)`) -} - -func TestRemovePartitioningSinglePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - dbName := "RemovePartitioning" - tk.MustExec("create schema " + dbName) - tk.MustExec("use " + dbName) - tk.MustExec(`CREATE TABLE t ( -a int NOT NULL primary key , -b varchar(100), -key (b) -) -PARTITION BY hash (a) PARTITIONS 1`) - tk.MustExec(`insert into t values (1,"a"),(2,"bye"),(3,"Hi")`) - - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(100) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`a`) PARTITIONS 1")) - - tk.MustExec(`ALTER TABLE t REMOVE PARTITIONING`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(100) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows("1 a", "2 bye", "3 Hi")) -} diff --git a/ddl/primary_key_handle_test.go b/ddl/primary_key_handle_test.go index 92e43f2ce8f54..f49e3c3bba825 100644 --- a/ddl/primary_key_handle_test.go +++ b/ddl/primary_key_handle_test.go @@ -17,16 +17,13 @@ package ddl_test import ( "fmt" "math" - "strconv" "strings" "testing" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" - "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table" @@ -57,70 +54,6 @@ func checkTableMaxHandle(t *testing.T, d ddl.DDL, tbl table.Table, store kv.Stor } } -func TestPrimaryKey(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.Session().GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeIntOnly - - // Test add/drop primary key on a plain table. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b varchar(10));") - tk.MustGetErrCode("alter table t add primary key(a) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustExec("alter table t add primary key(a) nonclustered;") - tk.MustExec("alter table t drop primary key;") - tk.MustExec("alter table t add primary key(a) nonclustered;") - tk.MustExec("drop index `primary` on t;") - tk.MustExec("alter table t add primary key(a);") // implicit nonclustered - tk.MustExec("drop index `primary` on t;") - tk.MustGetErrCode("drop index `primary` on t;", errno.ErrCantDropFieldOrKey) - - // Test add/drop primary key on a PKIsHandle table. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b varchar(10), primary key(a) clustered);") - tk.MustGetErrCode("alter table t drop primary key;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(a) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(a) nonclustered;", mysql.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(a);", errno.ErrMultiplePriKey) // implicit nonclustered - tk.MustGetErrCode("alter table t add primary key(b) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(b) nonclustered;", errno.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(b);", errno.ErrMultiplePriKey) // implicit nonclustered - - // Test add/drop primary key on a nonclustered primary key table. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b varchar(10), primary key(a) nonclustered);") - tk.MustGetErrCode("alter table t add primary key(a) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(a) nonclustered;", errno.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(a);", errno.ErrMultiplePriKey) // implicit nonclustered - tk.MustGetErrCode("alter table t add primary key(b) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(b) nonclustered;", errno.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(b);", errno.ErrMultiplePriKey) // implicit nonclustered - tk.MustExec("alter table t drop primary key;") - - // Test add/drop primary key on a CommonHandle key table. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b varchar(10), primary key(b) clustered);") - tk.MustGetErrCode("alter table t drop primary key;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(a) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(a) nonclustered;", errno.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(a);", errno.ErrMultiplePriKey) // implicit nonclustered - tk.MustGetErrCode("alter table t add primary key(b) clustered;", errno.ErrUnsupportedDDLOperation) - tk.MustGetErrCode("alter table t add primary key(b) nonclustered;", errno.ErrMultiplePriKey) - tk.MustGetErrCode("alter table t add primary key(b);", errno.ErrMultiplePriKey) // implicit nonclustered - - // Test add/drop primary key when the column&index name is `primary`. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (`primary` int);") - tk.MustExec("alter table t add index (`primary`);") - tk.MustGetErrCode("drop index `primary` on t;", errno.ErrCantDropFieldOrKey) - - // The primary key cannot be invisible, for the case pk_is_handle. - tk.MustExec("drop table if exists t;") - tk.MustGetErrCode("create table t(c1 int not null, primary key(c1) invisible);", errno.ErrPKIndexCantBeInvisible) - tk.MustExec("create table t (a int, b int not null, primary key(a), unique(b) invisible);") - tk.MustExec("drop table t;") -} - func TestMultiRegionGetTableEndHandle(t *testing.T) { var cluster testutils.Cluster store := testkit.CreateMockStore(t, mockstore.WithClusterInspector(func(c testutils.Cluster) { @@ -325,234 +258,6 @@ func TestGetTableEndCommonHandle(t *testing.T) { checkTableMaxHandle(t, d, tbl, store, false, testutil.MustNewCommonHandle(t, "az", 1)) } -func TestAutoRandomChangeFromAutoInc(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("set @@tidb_allow_remove_auto_inc = 1;") - - // Basic usages. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_increment primary key);") - tk.MustExec("insert into t values (), (), ();") - tk.MustExec("alter table t modify column a bigint auto_random(3);") - tk.MustExec("insert into t values (), (), ();") - rows := tk.MustQuery("show table t next_row_id;").Rows() - require.Len(t, rows, 1, fmt.Sprintf("query result: %v", rows)) - require.Len(t, rows[0], 5, fmt.Sprintf("query result: %v", rows)) - require.Equal(t, "AUTO_RANDOM", rows[0][4]) - - // Changing from auto_inc unique key is not allowed. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_increment unique key);") - tk.MustGetErrCode("alter table t modify column a bigint auto_random;", errno.ErrInvalidAutoRandom) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_increment unique key, b bigint auto_random primary key);") - tk.MustGetErrCode("alter table t modify column a bigint auto_random;", errno.ErrInvalidAutoRandom) - - // Changing from non-auto-inc column is not allowed. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint);") - tk.MustGetErrCode("alter table t modify column a bigint auto_random;", errno.ErrInvalidAutoRandom) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint primary key);") - tk.MustGetErrCode("alter table t modify column a bigint auto_random;", errno.ErrInvalidAutoRandom) - - // Changing from non BIGINT auto_inc pk column is not allowed. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int auto_increment primary key);") - tk.MustGetErrCode("alter table t modify column a int auto_random;", errno.ErrInvalidAutoRandom) - tk.MustGetErrCode("alter table t modify column a bigint auto_random;", errno.ErrInvalidAutoRandom) - - // Changing from auto_random to auto_increment is not allowed. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_random primary key);") - // "Unsupported modify column: can't set auto_increment" - tk.MustGetErrCode("alter table t modify column a bigint auto_increment;", errno.ErrUnsupportedDDLOperation) - - // Large auto_increment number overflows auto_random. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_increment primary key);") - tk.MustExec("insert into t values (1<<(64-5));") - // "max allowed auto_random shard bits is 3, but got 4 on column `a`" - tk.MustGetErrCode("alter table t modify column a bigint auto_random(4);", errno.ErrInvalidAutoRandom) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint auto_increment primary key);") - tk.MustExec("insert into t values (1<<(64-6));") - tk.MustExec("alter table t modify column a bigint auto_random(4);") -} - -func TestAutoRandomExchangePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database if not exists auto_random_db") - defer tk.MustExec("drop database if exists auto_random_db") - - tk.MustExec("use auto_random_db") - - tk.MustExec("set @@tidb_enable_exchange_partition=1") - defer tk.MustExec("set @@tidb_enable_exchange_partition=0") - - tk.MustExec("drop table if exists e1, e2, e3, e4;") - - tk.MustExec("create table e1 (a bigint primary key clustered auto_random(3)) partition by hash(a) partitions 1;") - - tk.MustExec("create table e2 (a bigint primary key);") - tk.MustGetErrCode("alter table e1 exchange partition p0 with table e2;", errno.ErrTablesDifferentMetadata) - - tk.MustExec("create table e3 (a bigint primary key auto_random(2));") - tk.MustGetErrCode("alter table e1 exchange partition p0 with table e3;", errno.ErrTablesDifferentMetadata) - tk.MustExec("insert into e1 values (), (), ()") - - tk.MustExec("create table e4 (a bigint primary key auto_random(3));") - tk.MustExec("insert into e4 values ()") - tk.MustExec("alter table e1 exchange partition p0 with table e4;") - - tk.MustQuery("select count(*) from e1").Check(testkit.Rows("1")) - tk.MustExec("insert into e1 values ()") - tk.MustQuery("select count(*) from e1").Check(testkit.Rows("2")) - - tk.MustQuery("select count(*) from e4").Check(testkit.Rows("3")) - tk.MustExec("insert into e4 values ()") - tk.MustQuery("select count(*) from e4").Check(testkit.Rows("4")) -} - -func TestAutoRandomIncBitsIncrementAndOffset(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database if not exists auto_random_db") - defer tk.MustExec("drop database if exists auto_random_db") - tk.MustExec("use auto_random_db") - tk.MustExec("drop table if exists t") - - recreateTable := func() { - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a bigint auto_random(6) primary key clustered)") - } - truncateTable := func() { - _, _ = tk.Exec("delete from t") - } - insertTable := func() { - tk.MustExec("insert into t values ()") - } - assertIncBitsValues := func(values ...int) { - mask := strings.Repeat("1", 64-1-6) - sql := fmt.Sprintf(`select a & b'%s' from t order by a & b'%s' asc`, mask, mask) - vs := make([]string, len(values)) - for i, value := range values { - vs[i] = strconv.Itoa(value) - } - tk.MustQuery(sql).Check(testkit.Rows(vs...)) - } - - const truncate, recreate = true, false - expect := func(vs ...int) []int { return vs } - testCase := []struct { - setupAction bool // truncate or recreate - increment int // @@auto_increment_increment - offset int // @@auto_increment_offset - results []int // the implicit allocated auto_random incremental-bit part of values - }{ - {recreate, 5, 10, expect(10, 15, 20)}, - {recreate, 2, 10, expect(10, 12, 14)}, - {truncate, 5, 10, expect(15, 20, 25)}, - {truncate, 10, 10, expect(30, 40, 50)}, - {truncate, 5, 10, expect(55, 60, 65)}, - } - for _, tc := range testCase { - switch tc.setupAction { - case recreate: - recreateTable() - case truncate: - truncateTable() - } - tk.Session().GetSessionVars().AutoIncrementIncrement = tc.increment - tk.Session().GetSessionVars().AutoIncrementOffset = tc.offset - for range tc.results { - insertTable() - } - assertIncBitsValues(tc.results...) - } -} - -func TestInvisibleIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t,t1,t2,t3,t4,t5,t6") - - // The DDL statement related to invisible index. - showIndexes := "select index_name, is_visible from information_schema.statistics where table_schema = 'test' and table_name = 't'" - // 1. Create table with invisible index - tk.MustExec("create table t (a int, b int, unique (a) invisible)") - tk.MustQuery(showIndexes).Check(testkit.Rows("a NO")) - tk.MustExec("insert into t values (1, 2)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2")) - // 2. Drop invisible index - tk.MustExec("alter table t drop index a") - tk.MustQuery(showIndexes).Check(testkit.Rows()) - tk.MustExec("insert into t values (3, 4)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4")) - // 3. Add an invisible index - tk.MustExec("alter table t add index (b) invisible") - tk.MustQuery(showIndexes).Check(testkit.Rows("b NO")) - tk.MustExec("insert into t values (5, 6)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6")) - // 4. Drop it - tk.MustExec("alter table t drop index b") - tk.MustQuery(showIndexes).Check(testkit.Rows()) - tk.MustExec("insert into t values (7, 8)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8")) - // 5. Create a multiple-column invisible index - tk.MustExec("alter table t add index a_b(a, b) invisible") - tk.MustQuery(showIndexes).Check(testkit.Rows("a_b NO", "a_b NO")) - tk.MustExec("insert into t values (9, 10)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8", "9 10")) - // 6. Drop it - tk.MustExec("alter table t drop index a_b") - tk.MustQuery(showIndexes).Check(testkit.Rows()) - tk.MustExec("insert into t values (11, 12)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8", "9 10", "11 12")) - - // Limitation: Primary key cannot be invisible index - tk.MustGetErrCode("create table t1 (a int, primary key (a) nonclustered invisible)", errno.ErrPKIndexCantBeInvisible) - tk.MustGetErrCode("create table t1 (a int, b int, primary key (a, b) nonclustered invisible)", errno.ErrPKIndexCantBeInvisible) - tk.MustExec("create table t1 (a int, b int)") - tk.MustGetErrCode("alter table t1 add primary key(a) nonclustered invisible", errno.ErrPKIndexCantBeInvisible) - tk.MustGetErrCode("alter table t1 add primary key(a, b) nonclustered invisible", errno.ErrPKIndexCantBeInvisible) - - // Implicit primary key cannot be invisible index - // Create a implicit primary key - tk.MustGetErrCode("create table t2(a int not null, unique (a) invisible)", errno.ErrPKIndexCantBeInvisible) - tk.MustGetErrCode("create table t2(a int auto_increment, unique key (a) invisible);", errno.ErrPKIndexCantBeInvisible) - // Column `a` become implicit primary key after DDL statement on itself - tk.MustExec("create table t2(a int not null)") - tk.MustGetErrCode("alter table t2 add unique (a) invisible", errno.ErrPKIndexCantBeInvisible) - tk.MustExec("create table t3(a int, unique index (a) invisible)") - tk.MustGetErrCode("alter table t3 modify column a int not null", errno.ErrPKIndexCantBeInvisible) - // Only first unique column can be implicit primary - tk.MustExec("create table t4(a int not null, b int not null, unique (a), unique (b) invisible)") - showIndexes = "select index_name, is_visible from information_schema.statistics where table_schema = 'test' and table_name = 't4'" - tk.MustQuery(showIndexes).Check(testkit.Rows("a YES", "b NO")) - tk.MustExec("insert into t4 values (1, 2)") - tk.MustQuery("select * from t4").Check(testkit.Rows("1 2")) - tk.MustGetErrCode("create table t5(a int not null, b int not null, unique (b) invisible, unique (a))", errno.ErrPKIndexCantBeInvisible) - // Column `b` become implicit primary key after DDL statement on other columns - tk.MustExec("create table t5(a int not null, b int not null, unique (a), unique (b) invisible)") - tk.MustGetErrCode("alter table t5 drop index a", errno.ErrPKIndexCantBeInvisible) - tk.MustGetErrCode("alter table t5 modify column a int null", errno.ErrPKIndexCantBeInvisible) - // If these is a explicit primary key, no key will become implicit primary key - tk.MustExec("create table t6 (a int not null, b int, unique (a) invisible, primary key(b) nonclustered)") - showIndexes = "select index_name, is_visible from information_schema.statistics where table_schema = 'test' and table_name = 't6'" - tk.MustQuery(showIndexes).Check(testkit.Rows("a NO", "PRIMARY YES")) - tk.MustExec("insert into t6 values (1, 2)") - tk.MustQuery("select * from t6").Check(testkit.Rows("1 2")) - tk.MustGetErrCode("alter table t6 drop primary key", errno.ErrPKIndexCantBeInvisible) - res := tk.MustQuery("show index from t6 where Key_name='PRIMARY';") - require.Len(t, res.Rows(), 1) -} - func TestCreateClusteredIndex(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/ddl/reorg_partition_test.go b/ddl/reorg_partition_test.go index 970aa3fa16fec..2a1f5d2391419 100644 --- a/ddl/reorg_partition_test.go +++ b/ddl/reorg_partition_test.go @@ -140,780 +140,6 @@ func getAllDataForPhysicalTable(t *testing.T, ctx sessionctx.Context, physTable return all } -func TestReorganizeRangePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database ReorgPartition") - tk.MustExec("use ReorgPartition") - tk.MustExec(`create table t (a int unsigned PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (a) ` + - `(partition p0 values less than (10),` + - ` partition p1 values less than (20),` + - ` partition pMax values less than (MAXVALUE))`) - tk.MustExec(`insert into t values (1,"1",1), (12,"12",21),(23,"23",32),(34,"34",43),(45,"45",54),(56,"56",65)`) - tk.MustQuery(`select * from t where c < 40`).Sort().Check(testkit.Rows(""+ - "1 1 1", - "12 12 21", - "23 23 32")) - tk.MustExec(`alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN (10),\n" + - " PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (30),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows(""+ - "1 1 1", - "12 12 21", - "23 23 32", - "34 34 43", - "45 45 54", - "56 56 65")) - tk.MustQuery(`select * from t partition (p0)`).Sort().Check(testkit.Rows("" + - "1 1 1")) - tk.MustQuery(`select * from t partition (p1)`).Sort().Check(testkit.Rows("" + - "12 12 21")) - tk.MustQuery(`select * from t partition (p2)`).Sort().Check(testkit.Rows("" + - "23 23 32")) - tk.MustQuery(`select * from t partition (pMax)`).Sort().Check(testkit.Rows(""+ - "34 34 43", - "45 45 54", - "56 56 65")) - tk.MustQuery(`select * from t where b > "1"`).Sort().Check(testkit.Rows(""+ - "12 12 21", - "23 23 32", - "34 34 43", - "45 45 54", - "56 56 65")) - tk.MustQuery(`select * from t where c < 40`).Sort().Check(testkit.Rows(""+ - "1 1 1", - "12 12 21", - "23 23 32")) - tk.MustExec(`alter table t reorganize partition p2,pMax into (partition p2 values less than (35),partition p3 values less than (47), partition pMax values less than (MAXVALUE))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows(""+ - "1 1 1", - "12 12 21", - "23 23 32", - "34 34 43", - "45 45 54", - "56 56 65")) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN (10),\n" + - " PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustQuery(`select * from t partition (p0)`).Sort().Check(testkit.Rows("" + - "1 1 1")) - tk.MustQuery(`select * from t partition (p1)`).Sort().Check(testkit.Rows("" + - "12 12 21")) - tk.MustQuery(`select * from t partition (p2)`).Sort().Check(testkit.Rows(""+ - "23 23 32", - "34 34 43")) - tk.MustQuery(`select * from t partition (p3)`).Sort().Check(testkit.Rows("" + - "45 45 54")) - tk.MustQuery(`select * from t partition (pMax)`).Sort().Check(testkit.Rows("" + - "56 56 65")) - tk.MustExec(`alter table t reorganize partition p0,p1 into (partition p1 values less than (20))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows(""+ - "1 1 1", - "12 12 21", - "23 23 32", - "34 34 43", - "45 45 54", - "56 56 65")) - tk.MustExec(`alter table t drop index b`) - tk.MustExec(`alter table t drop index c`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`create table t2 (a int unsigned not null, b varchar(255), c int, key (b), key (c,b)) partition by range (a) ` + - "(PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))") - tk.MustExec(`insert into t2 select * from t`) - // Not allowed to change the start range! - tk.MustGetErrCode(`alter table t2 reorganize partition p2 into (partition p2a values less than (20), partition p2b values less than (36))`, - errno.ErrRangeNotIncreasing) - // Not allowed to change the end range! - tk.MustGetErrCode(`alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (36))`, errno.ErrRangeNotIncreasing) - tk.MustGetErrCode(`alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (34))`, errno.ErrRangeNotIncreasing) - // Also not allowed to change from MAXVALUE to something else IF there are values in the removed range! - tk.MustContainErrMsg(`alter table t2 reorganize partition pMax into (partition p2b values less than (50))`, "[table:1526]Table has no partition for value 56") - tk.MustQuery(`show create table t2`).Check(testkit.Rows("" + - "t2 CREATE TABLE `t2` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - // But allowed to change from MAXVALUE if no existing values is outside the new range! - tk.MustExec(`alter table t2 reorganize partition pMax into (partition p4 values less than (90))`) - tk.MustExec(`admin check table t2`) - tk.MustQuery(`show create table t2`).Check(testkit.Rows("" + - "t2 CREATE TABLE `t2` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (35),\n" + - " PARTITION `p3` VALUES LESS THAN (47),\n" + - " PARTITION `p4` VALUES LESS THAN (90))")) - - // Test reorganize range partitions works when a function defined as the key. - tk.MustExec("drop table t") - tk.MustExec(`create table t (a int PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (abs(a)) ` + - `(partition p0 values less than (10),` + - ` partition p1 values less than (20),` + - ` partition pMax values less than (MAXVALUE))`) - tk.MustExec(`insert into t values (0,"0",0),(1,"1",1),(2,"2",-2),(-12,"12",21),(23,"23",32),(-34,"34",43),(45,"45",54),(56,"56",65)`) - tk.MustExec(`alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (ABS(`a`))\n" + - "(PARTITION `p0` VALUES LESS THAN (10),\n" + - " PARTITION `p1` VALUES LESS THAN (20),\n" + - " PARTITION `p2` VALUES LESS THAN (30),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustQuery(`select * from t partition (p2)`).Sort().Check(testkit.Rows("" + - "23 23 32")) - tk.MustQuery(`select * from t partition (pMax)`).Sort().Check(testkit.Rows(""+ - "-34 34 43", "45 45 54", "56 56 65")) - tk.MustExec(`alter table t drop index b`) - tk.MustExec(`alter table t reorganize partition p0,p1,p2,pMax into (partition pAll values less than (maxvalue))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (ABS(`a`))\n" + - "(PARTITION `pAll` VALUES LESS THAN (MAXVALUE))")) - tk.MustQuery(`select * from t partition (pAll)`).Sort().Check(testkit.Rows(""+ - "-12 12 21", "-34 34 43", "0 0 0", "1 1 1", "2 2 -2", "23 23 32", "45 45 54", "56 56 65")) -} - -func TestReorganizeRangeColumnsPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - // Test reorganize range columns partitions works for integer and string defined as the key. - tk.MustExec("CREATE DATABASE ReorgPartition") - tk.MustExec("USE ReorgPartition") - tk.MustExec(` - CREATE TABLE t ( - a INT, - b CHAR(3), - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY RANGE COLUMNS(a,b) ( - PARTITION p0 VALUES LESS THAN (5,'ggg'), - PARTITION p1 VALUES LESS THAN (10,'mmm'), - PARTITION p2 VALUES LESS THAN (15,'sss'), - PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) - ); - `) - tk.MustExec(`INSERT INTO t VALUES (1,'abc',1), (3,'ggg',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19);`) - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows(""+ - "1 abc 1", - "3 ggg 3")) - tk.MustExec(`ALTER TABLE t DROP INDEX c`) - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN (2,'ggg'), PARTITION p01 VALUES LESS THAN (5,'ggg'));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` char(3) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`a`,`b`)\n" + - "(PARTITION `p00` VALUES LESS THAN (2,'ggg'),\n" + - " PARTITION `p01` VALUES LESS THAN (5,'ggg'),\n" + - " PARTITION `p1` VALUES LESS THAN (10,'mmm'),\n" + - " PARTITION `p2` VALUES LESS THAN (15,'sss'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Sort().Check(testkit.Rows("1 abc 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("3 ggg 3")) - - // Test reorganize range columns partitions works for string and integer defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a INT, - b CHAR(3), - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY RANGE COLUMNS(b,a) ( - PARTITION p0 VALUES LESS THAN ('ggg',5), - PARTITION p1 VALUES LESS THAN ('mmm',10), - PARTITION p2 VALUES LESS THAN ('sss',15), - PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) - ); - `) - tk.MustExec(`INSERT INTO t VALUES (1,'abc',1), (3,'ccc',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19);`) - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows(""+ - "1 abc 1", - "3 ccc 3")) - tk.MustExec("ALTER TABLE t DROP INDEX b") - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('ccc',2), PARTITION p01 VALUES LESS THAN ('ggg',5));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` char(3) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`b`,`a`)\n" + - "(PARTITION `p00` VALUES LESS THAN ('ccc',2),\n" + - " PARTITION `p01` VALUES LESS THAN ('ggg',5),\n" + - " PARTITION `p1` VALUES LESS THAN ('mmm',10),\n" + - " PARTITION `p2` VALUES LESS THAN ('sss',15),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Sort().Check(testkit.Rows("1 abc 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("3 ccc 3")) - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p00,p01,p1 into (PARTITION p1 VALUES LESS THAN ('mmm',10));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` char(3) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`b`,`a`)\n" + - "(PARTITION `p1` VALUES LESS THAN ('mmm',10),\n" + - " PARTITION `p2` VALUES LESS THAN ('sss',15),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p1)`).Sort().Check(testkit.Rows(""+ - "1 abc 1", - "3 ccc 3", - "5 ggg 5", - "9 ggg 9")) - - // Test reorganize range columns partitions works for DATE and DATETIME defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a DATE, - b DATETIME, - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY RANGE COLUMNS(a,b) ( - PARTITION p0 VALUES LESS THAN ('2020-05-05','2020-05-05 10:10:10'), - PARTITION p1 VALUES LESS THAN ('2021-05-05','2021-05-05 10:10:10'), - PARTITION p2 VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'), - PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) - ); - `) - tk.MustExec("INSERT INTO t VALUES" + - "('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2)," + - "('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4)," + - "('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6);") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10', '2020-04-10 10:10:10'), PARTITION p01 VALUES LESS THAN ('2020-05-05', '2020-05-05 10:10:10'));") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`a`,`b`)\n" + - "(PARTITION `p00` VALUES LESS THAN ('2020-04-10','2020-04-10 10:10:10'),\n" + - " PARTITION `p01` VALUES LESS THAN ('2020-05-05','2020-05-05 10:10:10'),\n" + - " PARTITION `p1` VALUES LESS THAN ('2021-05-05','2021-05-05 10:10:10'),\n" + - " PARTITION `p2` VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Check(testkit.Rows()) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2")) - //TODO(bb7133): different err message with MySQL - tk.MustContainErrMsg( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:11'))", - "VALUES LESS THAN value must be strictly increasing for each partition") - tk.MustExec("ALTER TABLE t DROP INDEX c") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:10'))") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`a`,`b`)\n" + - "(PARTITION `p0` VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-05 2020-05-05 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4")) - tk.MustQuery(`SELECT * FROM t PARTITION(pMax)`).Sort().Check(testkit.Rows("2022-05-05 2022-05-05 10:10:10 5", "2023-05-05 2023-05-05 10:10:10 6")) - - // Test reorganize range columns partitions works for DATETIME and DATE defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a DATE, - b DATETIME, - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY RANGE COLUMNS(b,a) ( - PARTITION p0 VALUES LESS THAN ('2020-05-05 10:10:10','2020-05-05'), - PARTITION p1 VALUES LESS THAN ('2021-05-05 10:10:10','2021-05-05'), - PARTITION p2 VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'), - PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) - ); - `) - tk.MustExec("INSERT INTO t VALUES" + - "('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2)," + - "('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4)," + - "('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6);") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10 10:10:10', '2020-04-10'), PARTITION p01 VALUES LESS THAN ('2020-05-05 10:10:10', '2020-05-05'));") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`b`,`a`)\n" + - "(PARTITION `p00` VALUES LESS THAN ('2020-04-10 10:10:10','2020-04-10'),\n" + - " PARTITION `p01` VALUES LESS THAN ('2020-05-05 10:10:10','2020-05-05'),\n" + - " PARTITION `p1` VALUES LESS THAN ('2021-05-05 10:10:10','2021-05-05'),\n" + - " PARTITION `p2` VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Check(testkit.Rows()) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2")) - tk.MustExec("ALTER TABLE t DROP INDEX b") - //TODO(bb7133): different err message with MySQL - tk.MustContainErrMsg( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:11', '2022-05-05'))", - "VALUES LESS THAN value must be strictly increasing for each partition") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:10', '2022-05-05'))") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`b`,`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE))")) - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-05 2020-05-05 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4")) - tk.MustQuery(`SELECT * FROM t PARTITION(pMax)`).Sort().Check(testkit.Rows("2022-05-05 2022-05-05 10:10:10 5", "2023-05-05 2023-05-05 10:10:10 6")) -} - -func TestReorganizeListPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database ReorgListPartition") - tk.MustExec("use ReorgListPartition") - tk.MustExec(`create table t (a int, b varchar(55), c int) partition by list (a)` + - ` (partition p1 values in (12,23,51,14), partition p2 values in (24,63), partition p3 values in (45))`) - tk.MustExec(`insert into t values (12,"12",21), (24,"24",42),(51,"51",15),(23,"23",32),(63,"63",36),(45,"45",54)`) - tk.MustExec(`alter table t reorganize partition p1 into (partition p0 values in (12,51,13), partition p1 values in (23))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` varchar(55) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (`a`)\n" + - "(PARTITION `p0` VALUES IN (12,51,13),\n" + - " PARTITION `p1` VALUES IN (23),\n" + - " PARTITION `p2` VALUES IN (24,63),\n" + - " PARTITION `p3` VALUES IN (45))")) - tk.MustExec(`alter table t add primary key (a), add key (b), add key (c,b)`) - - // Note: MySQL cannot reorganize two non-consecutive list partitions :) - // ERROR 1519 (HY000): When reorganizing a set of partitions they must be in consecutive order - // https://bugs.mysql.com/bug.php?id=106011 - // https://bugs.mysql.com/bug.php?id=109939 - tk.MustExec(`alter table t reorganize partition p1, p3 into (partition pa values in (45,23,15))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(55) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (`a`)\n" + - "(PARTITION `p0` VALUES IN (12,51,13),\n" + - " PARTITION `pa` VALUES IN (45,23,15),\n" + - " PARTITION `p2` VALUES IN (24,63))")) - tk.MustGetErrCode(`alter table t modify a varchar(20)`, errno.ErrUnsupportedDDLOperation) - - // Test reorganize list partitions works when a function defined as the key. - tk.MustExec("drop table t") - tk.MustExec(`create table t (a int, b varchar(55), c int) partition by list (abs(a)) - (partition p0 values in (-1,0,1), - partition p1 values in (12,23,51,14), - partition p2 values in (24,63), - partition p3 values in (45))`) - tk.MustExec(`insert into t values - (-1,"-1",11),(1,"1",11),(0,"0",0),(-12,"-12",21), - (-24,"-24",42),(51,"-51",15),(23,"23",32),(63,"63",36),(45,"45",54)`) - tk.MustExec(`alter table t reorganize partition p0, p1 into (partition p0 values in (0,1,2,12,51,13), partition p1 values in (23))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`select * from t partition (p0)`).Sort().Check(testkit.Rows(""+ - "-1 -1 11", "-12 -12 21", "0 0 0", "1 1 11", "51 -51 15")) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` varchar(55) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (ABS(`a`))\n" + - "(PARTITION `p0` VALUES IN (0,1,2,12,51,13),\n" + - " PARTITION `p1` VALUES IN (23),\n" + - " PARTITION `p2` VALUES IN (24,63),\n" + - " PARTITION `p3` VALUES IN (45))")) - tk.MustExec(`alter table t add primary key (a), add key (b), add key (c,b)`) - - tk.MustExec(`alter table t reorganize partition p0,p1,p2,p3 into (partition paa values in (0,1,2,12,13,23,24,45,51,63,64))`) - tk.MustExec(`admin check table t`) - tk.MustQuery(`select * from t partition (paa)`).Sort().Check(testkit.Rows(""+ - "-1 -1 11", "-12 -12 21", "-24 -24 42", "0 0 0", "1 1 11", "23 23 32", "45 45 54", "51 -51 15", "63 63 36")) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) NOT NULL,\n" + - " `b` varchar(55) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (ABS(`a`))\n" + - "(PARTITION `paa` VALUES IN (0,1,2,12,13,23,24,45,51,63,64))")) -} - -func TestReorganizeListColumnsPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("CREATE DATABASE ReorgPartition") - tk.MustExec("USE ReorgPartition") - // Test reorganize list columns partitions works for interger and string defined as the key. - tk.MustExec(` - CREATE TABLE t ( - a INT, - b CHAR(3), - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY LIST COLUMNS(a,b) ( - PARTITION p0 VALUES IN ((1,'aaa'),(2,'bbb'),(3,'ccc')), - PARTITION p1 VALUES IN ((4,'ddd'),(5,'eee'),(6,'fff')), - PARTITION p2 VALUES IN ((16,'lll'),(17,'mmm'),(18,'lll')) - ); - `) - tk.MustExec(`INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16);`) - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows(""+ - "1 aaa 1", - "3 ccc 3")) - //TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. - tk.MustContainErrMsg(`ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb')));`, "Table has no partition for value from column_list") - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb'),(3,'ccc')));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` char(3) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`a`,`b`)\n" + - "(PARTITION `p00` VALUES IN ((0,'uuu'),(1,'aaa')),\n" + - " PARTITION `p01` VALUES IN ((2,'bbb'),(3,'ccc')),\n" + - " PARTITION `p1` VALUES IN ((4,'ddd'),(5,'eee'),(6,'fff')),\n" + - " PARTITION `p2` VALUES IN ((16,'lll'),(17,'mmm'),(18,'lll')))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Sort().Check(testkit.Rows("1 aaa 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("3 ccc 3")) - tk.MustExec("ALTER TABLE t DROP INDEX b") - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN - ((0,'uuu'),(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd'),(5,'eee'),(6,'fff'),(16,'lll'),(17,'mmm'),(18,'lll')));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SELECT * FROM t PARTITION(pAll)`).Sort().Check(testkit.Rows("1 aaa 1", "16 lll 16", "3 ccc 3", "5 eee 5")) - tk.MustQuery(`SELECT * FROM t`).Sort().Check(testkit.Rows("1 aaa 1", "16 lll 16", "3 ccc 3", "5 eee 5")) - - // Test reorganize list columns partitions works for string and integer defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a INT, - b CHAR(3), - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY LIST COLUMNS(b,a) ( - PARTITION p0 VALUES IN (('aaa',1),('bbb',2),('ccc',3)), - PARTITION p1 VALUES IN (('ddd',4),('eee',5),('fff',6)), - PARTITION p2 VALUES IN (('lll',16),('mmm',17),('lll',18)) - ); - `) - tk.MustExec(`INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16);`) - tk.MustQuery(`SELECT * FROM t PARTITION(p0)`).Sort().Check(testkit.Rows(""+ - "1 aaa 1", - "3 ccc 3")) - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('uuu',-1),('aaa',1)), PARTITION p01 VALUES IN (('bbb',2),('ccc',3),('ccc',4)));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` char(3) DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`b`,`a`)\n" + - "(PARTITION `p00` VALUES IN (('uuu',-1),('aaa',1)),\n" + - " PARTITION `p01` VALUES IN (('bbb',2),('ccc',3),('ccc',4)),\n" + - " PARTITION `p1` VALUES IN (('ddd',4),('eee',5),('fff',6)),\n" + - " PARTITION `p2` VALUES IN (('lll',16),('mmm',17),('lll',18)))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Sort().Check(testkit.Rows("1 aaa 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("3 ccc 3")) - tk.MustExec("ALTER TABLE t DROP INDEX c") - tk.MustExec(`ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN - (('uuu',-1),('aaa',1),('bbb',2),('ccc',3),('ccc',4),('ddd',4),('eee',5),('fff',6),('lll',16),('mmm',17),('lll',18)));`) - tk.MustExec(`ADMIN CHECK TABLE t`) - tk.MustQuery(`SELECT * FROM t PARTITION(pAll)`).Sort().Check(testkit.Rows("1 aaa 1", "16 lll 16", "3 ccc 3", "5 eee 5")) - tk.MustQuery(`SELECT * FROM t`).Sort().Check(testkit.Rows("1 aaa 1", "16 lll 16", "3 ccc 3", "5 eee 5")) - - // Test reorganize list columns partitions works for DATE and DATETIME defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a DATE, - b DATETIME, - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY LIST COLUMNS(a,b) ( - PARTITION p0 VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10')), - PARTITION p1 VALUES IN (('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')), - PARTITION p2 VALUES IN (('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')) - ); - `) - tk.MustExec("INSERT INTO t VALUES" + - "('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2)," + - "('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4)," + - "('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6);") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10', '2020-04-10 10:10:10')), PARTITION p01 VALUES IN (('2020-05-04', '2020-05-04 10:10:10')));") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`a`,`b`)\n" + - "(PARTITION `p00` VALUES IN (('2020-04-10','2020-04-10 10:10:10')),\n" + - " PARTITION `p01` VALUES IN (('2020-05-04','2020-05-04 10:10:10')),\n" + - " PARTITION `p1` VALUES IN (('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')),\n" + - " PARTITION `p2` VALUES IN (('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Check(testkit.Rows("2020-04-10 2020-04-10 10:10:10 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("2020-05-04 2020-05-04 10:10:10 2", "2020-05-04 2020-05-04 10:10:10 3")) - tk.MustExec("ALTER TABLE t DROP INDEX b") - //TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. - tk.MustContainErrMsg( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN "+ - "(('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'),"+ - " ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'),"+ - " ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2023-05-05 11:11:11')))", - "Table has no partition for value from column_list") - tk.MustExec( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN " + - "(('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10')," + - " ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')," + - " ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')))") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`a`,`b`)\n" + - "(PARTITION `pAll` VALUES IN " + - "(('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10')," + - "('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')," + - "('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')))")) - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SELECT * FROM t PARTITION(pAll)`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-04 2020-05-04 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4", - "2022-05-04 2022-05-04 10:10:10 5", "2022-05-05 2022-05-06 11:11:11 6")) - tk.MustQuery(`SELECT * FROM t`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-04 2020-05-04 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4", - "2022-05-04 2022-05-04 10:10:10 5", "2022-05-05 2022-05-06 11:11:11 6")) - - // Test reorganize list columns partitions works for DATETIME and DATE defined as the key. - tk.MustExec("DROP TABLE t") - tk.MustExec(` - CREATE TABLE t ( - a DATE, - b DATETIME, - c INT, - KEY b(b), - KEY c(c,b) - ) - PARTITION BY LIST COLUMNS(b,a) ( - PARTITION p0 VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04')), - PARTITION p1 VALUES IN (('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')), - PARTITION p2 VALUES IN (('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')) - ); - `) - tk.MustExec("INSERT INTO t VALUES" + - "('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2)," + - "('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4)," + - "('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6);") - tk.MustExec("ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10 10:10:10','2020-04-10')), PARTITION p01 VALUES IN (('2020-05-04 10:10:10','2020-05-04')));") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `b` (`b`),\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`b`,`a`)\n" + - "(PARTITION `p00` VALUES IN (('2020-04-10 10:10:10','2020-04-10')),\n" + - " PARTITION `p01` VALUES IN (('2020-05-04 10:10:10','2020-05-04')),\n" + - " PARTITION `p1` VALUES IN (('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')),\n" + - " PARTITION `p2` VALUES IN (('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')))")) - tk.MustQuery(`SELECT * FROM t PARTITION(p00)`).Check(testkit.Rows("2020-04-10 2020-04-10 10:10:10 1")) - tk.MustQuery(`SELECT * FROM t PARTITION(p01)`).Sort().Check(testkit.Rows("2020-05-04 2020-05-04 10:10:10 2", "2020-05-04 2020-05-04 10:10:10 3")) - tk.MustExec("ALTER TABLE t DROP INDEX b") - //TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. - tk.MustContainErrMsg( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN "+ - "(('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'),"+ - " ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'),"+ - " ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2023-05-05')))", - "Table has no partition for value from column_list") - tk.MustExec( - "ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN " + - "(('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04')," + - " ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')," + - " ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')))") - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SHOW CREATE TABLE t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` date DEFAULT NULL,\n" + - " `b` datetime DEFAULT NULL,\n" + - " `c` int(11) DEFAULT NULL,\n" + - " KEY `c` (`c`,`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`b`,`a`)\n" + - "(PARTITION `pAll` VALUES IN " + - "(('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04')," + - "('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')," + - "('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')))")) - tk.MustExec("ADMIN CHECK TABLE t") - tk.MustQuery(`SELECT * FROM t PARTITION(pAll)`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-04 2020-05-04 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4", - "2022-05-04 2022-05-04 10:10:10 5", "2022-05-05 2022-05-06 11:11:11 6")) - tk.MustQuery(`SELECT * FROM t`).Sort().Check(testkit.Rows( - "2020-04-10 2020-04-10 10:10:10 1", "2020-05-04 2020-05-04 10:10:10 2", - "2020-05-04 2020-05-04 10:10:10 3", "2021-05-04 2021-05-04 10:10:10 4", - "2022-05-04 2022-05-04 10:10:10 5", "2022-05-05 2022-05-06 11:11:11 6")) -} - type TestReorgDDLCallback struct { *callback.TestDDLCallback syncChan chan bool @@ -1355,22 +581,3 @@ func TestReorgPartitionRollback(t *testing.T) { require.NoError(t, err) noNewTablesAfter(t, tk, ctx, tbl) } - -func TestReorgPartitionData(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - schemaName := "ReorgPartData" - tk.MustExec("create database " + schemaName) - tk.MustExec("use " + schemaName) - tk.MustExec(`SET @@session.sql_mode = default`) - tk.MustExec(`create table t (a int PRIMARY KEY AUTO_INCREMENT, b varchar(255), c int, d datetime, key (b), key (c,b)) partition by range (a) (partition p1 values less than (0), partition p1M values less than (1000000))`) - tk.MustContainErrMsg(`insert into t values (0, "Zero value!", 0, '2022-02-30')`, "[table:1292]Incorrect datetime value: '2022-02-30' for column 'd' at row 1") - tk.MustExec(`SET @@session.sql_mode = 'ALLOW_INVALID_DATES,NO_AUTO_VALUE_ON_ZERO'`) - tk.MustExec(`insert into t values (0, "Zero value!", 0, '2022-02-30')`) - tk.MustQuery(`show warnings`).Check(testkit.Rows()) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows("0 Zero value! 0 2022-02-30 00:00:00")) - tk.MustExec(`SET @@session.sql_mode = default`) - tk.MustExec(`alter table t reorganize partition p1M into (partition p0 values less than (1), partition p2M values less than (2000000))`) - tk.MustQuery(`select * from t`).Sort().Check(testkit.Rows("0 Zero value! 0 2022-02-30 00:00:00")) - tk.MustExec(`admin check table t`) -} diff --git a/tests/integrationtest/r/ddl/ddl_error.result b/tests/integrationtest/r/ddl/ddl_error.result new file mode 100644 index 0000000000000..47027d66d6845 --- /dev/null +++ b/tests/integrationtest/r/ddl/ddl_error.result @@ -0,0 +1,13 @@ +drop database if exists ddl__ddl_error2; +drop table if exists table_1; +drop view if exists view_1; +create database test_2; +create table table_1 (a int); +create view view_1 as select a from table_1; +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +Error 1450 (HY000): Changing schema from 'ddl__ddl_error' to 'ddl__ddl_error2' is not allowed. +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +Error 1450 (HY000): Changing schema from 'ddl__ddl_error' to 'ddl__ddl_error2' is not allowed. +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +Error 1450 (HY000): Changing schema from 'ddl__ddl_error' to 'ddl__ddl_error2' is not allowed. +rename table ddl__ddl_error.view_1 to ddl__ddl_error.view_1000; diff --git a/tests/integrationtest/r/ddl/index_modify.result b/tests/integrationtest/r/ddl/index_modify.result new file mode 100644 index 0000000000000..b0ce1ab6eb289 --- /dev/null +++ b/tests/integrationtest/r/ddl/index_modify.result @@ -0,0 +1,26 @@ +set tidb_enable_clustered_index = ON; +drop table if exists t; +create table t (a int, b varchar(10), c int, primary key (a, b)); +insert into t values (1, '1', 1), (2, '2', NULL), (3, '3', 3); +create index idx on t (a, c); +admin check index t idx; + +admin check table t; + +insert into t values (5, '5', 5), (6, '6', NULL); +admin check index t idx; + +admin check table t; + +set tidb_enable_clustered_index = default; +drop table if exists test_add_index_with_dup; +create table test_add_index_with_dup (a int, b int); +create index c on test_add_index_with_dup(b, a, b); +Error 1060 (42S21): Duplicate column name 'b' +create index c on test_add_index_with_dup(b, a, B); +Error 1060 (42S21): Duplicate column name 'B' +alter table test_add_index_with_dup add index c (b, a, b); +Error 1060 (42S21): Duplicate column name 'b' +alter table test_add_index_with_dup add index c (b, a, B); +Error 1060 (42S21): Duplicate column name 'B' +drop table test_add_index_with_dup; diff --git a/tests/integrationtest/r/ddl/integration.result b/tests/integrationtest/r/ddl/integration.result new file mode 100644 index 0000000000000..f4c64ac851ba6 --- /dev/null +++ b/tests/integrationtest/r/ddl/integration.result @@ -0,0 +1,93 @@ +drop table if exists t; +create table t (a char(10) charset gbk default 0xC4E3BAC3); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a char(10) charset gbk default '好'); +insert into t values (default); +select a from t; +a +好 +drop table if exists t; +create table t (a varchar(10) charset gbk default 0xC4E3BAC3); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a char(10) charset utf8mb4 default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a char(10) charset utf8mb4 default 0b111001001011100010010110111001111001010110001100); +insert into t values (default); +select a from t; +a +世界 +drop table if exists t; +create table t (a bit(48) default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a enum('你好') default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a set('你好') default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +a +你好 +drop table if exists t; +create table t (a char(20) charset utf8mb4 default 0xE4BDA0E5A5BD81); +Error 1067 (42000): Invalid default value for 'a' +create table t (a blob default 0xE4BDA0E5A5BD81); +Error 1101 (42000): BLOB/TEXT/JSON column 'a' can't have a default value +drop table if exists t; +create table t(a enum('a', 0x91) charset gbk); +insert into t values (1), (2); +select a from t; +a +a +? +drop table t; +create table t (a enum('a', 0x91)) charset gbk; +insert into t values (1), (2); +select a from t; +a +a +? +drop table t; +create table t(a set('a', 0x91, '?') charset gbk); +Error 1291 (HY000): Column 'a' has duplicated value '?' in SET +create table t (a enum('a', 0xE4BDA0E5A5BD) charset gbk); +insert into t values (1), (2); +select a from t; +a +a +浣犲ソ +drop table if exists t; +create table t (id int, c int, index(c)); +alter table t cache; +drop table t; +Error 8242 (HY000): 'Drop Table' is unsupported on cache tables. +create index t_id on t (id); +Error 8242 (HY000): 'Create Index' is unsupported on cache tables. +alter table t drop index c; +Error 8242 (HY000): 'Alter Table' is unsupported on cache tables. +alter table t add column (d int); +Error 8242 (HY000): 'Alter Table' is unsupported on cache tables. +truncate table t; +Error 8242 (HY000): 'Truncate Table' is unsupported on cache tables. +rename table t to t1; +Error 8242 (HY000): 'Rename Table' is unsupported on cache tables. +alter table t nocache; +drop table if exists t; diff --git a/tests/integrationtest/r/ddl/modify_column.result b/tests/integrationtest/r/ddl/modify_column.result new file mode 100644 index 0000000000000..de000627fff48 --- /dev/null +++ b/tests/integrationtest/r/ddl/modify_column.result @@ -0,0 +1,887 @@ +drop table if exists t; +create table t(a decimal(5,2)); +insert into t values(111.22),(111.22),(111.22),(111.22),(333.4); +alter table t modify column a decimal(4,1); +show warnings; +Level Code Message +Warning 1292 4 warnings with this error code, first warning: Truncated incorrect DECIMAL value: '111.22' +drop table if exists t; +create table t(a decimal(5,2)); +insert into t values(111.22),(111.22),(111.22),(33.4); +alter table t modify column a decimal(3,1); +Error 1690 (22003): DECIMAL value is out of range in '(3, 1)' +set @@sql_mode=""; +alter table t modify column a decimal(3,1); +show warnings; +Level Code Message +Warning 1690 3 warnings with this error code, first warning: DECIMAL value is out of range in '(3, 1)' +set @@sql_mode=default; +drop table if exists t; +create table t (a bigint auto_increment primary key); +alter table t modify column a bigint auto_increment default 3; +Error 1067 (42000): Invalid default value for 'a' +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00:12"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '740:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '740:00:00' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '740:00:00' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00:12"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:00' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("12"); +alter table t_mc modify a year; +select a from t_mc; +a +2012 +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("200012"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (200012); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (0012); +alter table t_mc modify a year; +select a from t_mc; +a +2012 +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (12); +alter table t_mc modify a year; +select a from t_mc; +a +2012 +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00:12.498"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '740:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00:12.498"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("200012.498"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (200012.498); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '20:00:12' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a year; +select a from t_mc; +a +2019 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a datetime; +select a from t_mc; +a +2019-01-02 00:00:00 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-03 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a year; +select a from t_mc; +a +2006 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '1000-01-02 23:59:59' +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +alter table t_mc modify a year; +Error 1264 (22003): Out of range value for column 'a', the value is '9999-01-02 23:59:59' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a time; +select a from t_mc; +a +15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +alter table t_mc modify a time; +select a from t_mc; +a +23:59:59 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +alter table t_mc modify a time; +select a from t_mc; +a +23:59:59 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a date; +select a from t_mc; +a +2006-01-03 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +alter table t_mc modify a date; +select a from t_mc; +a +1000-01-02 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +alter table t_mc modify a date; +select a from t_mc; +a +9999-01-02 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-02 15:04:05 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2006-01-03 00:00:00 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1971-01-02 23:59:59"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +1971-01-02 23:59:59 +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2009-01-02 23:59:59"); +alter table t_mc modify a timestamp; +select a from t_mc; +a +2009-01-02 23:59:59 +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +alter table t_mc modify a time; +select a from t_mc; +a +00:20:19 +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +alter table t_mc modify a time; +select a from t_mc; +a +00:20:19 +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +alter table t_mc modify a time; +select a from t_mc; +a +00:20:00 +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '1999' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +alter table t_mc modify a time; +select a from t_mc; +a +00:00:00 +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +alter table t_mc modify a time; +Error 1292 (22007): Truncated incorrect time value: '1999' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '2000' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '1999' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +alter table t_mc modify a date; +Error 1292 (22007): Truncated incorrect date value: '0' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +alter table t_mc modify a date; +Error 1292 (22007): Incorrect time value: '1999' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '2000' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '1999' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +alter table t_mc modify a datetime; +Error 1292 (22007): Truncated incorrect datetime value: '0' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +alter table t_mc modify a datetime; +Error 1292 (22007): Incorrect time value: '1999' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '2019' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '2000' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '1999' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +alter table t_mc modify a timestamp; +Error 1292 (22007): Truncated incorrect timestamp value: '0' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '2069' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '1970' +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +alter table t_mc modify a timestamp; +Error 1292 (22007): Incorrect time value: '1999' +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; diff --git a/tests/integrationtest/r/ddl/multi_schema_change.result b/tests/integrationtest/r/ddl/multi_schema_change.result new file mode 100644 index 0000000000000..f2b045c8fcfef --- /dev/null +++ b/tests/integrationtest/r/ddl/multi_schema_change.result @@ -0,0 +1,443 @@ +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column b int default 2, add column c int default 3; +select * from t; +a b c +1 2 3 +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (b int default 2, c int default 3); +select * from t; +a b c +1 2 3 +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add column (d int default 4, e int default 5); +select * from t; +a b c d e +1 2 3 4 5 +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (index i(a), index i1(a)); +select * from t use index (i, i1); +a +1 +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (b int default 2, index i(a), primary key (a)); +select * from t use index (i, primary); +a b +1 2 +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (index i(a)); +select * from t use index (i); +a +1 +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add column (index i1(a, b, c), index i2(c, b, a), index i3((a + 1)), index i4((c - 1))); +select * from t use index (i1, i2); +a b c +1 2 3 +admin check table t; + +drop table if exists t; +create table t (a int default 1); +insert into t values (); +alter table t add column if not exists (b int default 2, c int default 3); +select * from t; +a b c +1 2 3 +alter table t add column if not exists (c int default 3, d int default 4); +show warnings; +Level Code Message +Note 1060 Duplicate column name 'c' +select * from t; +a b c d +1 2 3 4 +drop table if exists t; +create table t (a int); +alter table t add column b int after a, add column c int after b; +Error 1054 (42S22): Unknown column 'b' in 't' +alter table t add column c int after b, add column b int; +Error 1054 (42S22): Unknown column 'b' in 't' +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t +add column d int default 4 first, +add column e int default 5 after b, +add column f int default 6 after b; +select * from t; +d a b f e c +4 1 2 6 5 3 +drop table if exists t; +create table t (a int default 1); +insert into t values (); +alter table t add column b int default 2, add column if not exists a int; +show warnings; +Level Code Message +Note 1060 Duplicate column name 'a' +select * from t; +a b +1 2 +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t add column c double default 3.0, add column d double as (a + b); +select * from t; +a b c d +1 2 3 3 +drop table if exists t; +create table t (a int default 1, c int default 4); +alter table t add column b int default 2, add column b int default 3; +Error 8200 (HY000): Unsupported operate same column 'b' +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t modify column b double, add column c double as (a + b); +Error 8200 (HY000): Unsupported operate same column 'b' +drop table if exists t; +create table t (a int, b int); +alter table t drop column a, drop column b; +Error 1090 (42000): You can't delete all columns with ALTER TABLE; use DROP TABLE instead +drop table if exists t; +create table t (a int, b int, c int, d int, e int); +insert into t values (1, 2, 3, 4, 5); +alter table t drop column a, drop column d, drop column b; +select * from t; +c e +3 5 +drop table if exists t; +create table t (a int default 1, c int default 4); +alter table t drop column a, drop column a; +Error 8200 (HY000): Unsupported operate same column 'a' +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t drop column if exists c, drop column a; +show warnings; +Level Code Message +Note 1091 Can't DROP 'c'; check that column/key exists +select * from t; +b +2 +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3); +insert into t values (); +alter table t drop column a, drop column if exists d, drop column c; +show warnings; +Level Code Message +Note 1091 Can't DROP 'd'; check that column/key exists +select * from t; +b +2 +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t add column c int default 3, drop column a, add column d int default 4, drop column b; +select * from t; +c d +3 4 +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t drop column a, drop column b, add column c int default 3, add column d int default 4; +select * from t; +c d +3 4 +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t add column c int default 3 after a, add column d int default 4 first, drop column a, drop column b; +Error 8200 (HY000): Unsupported operate same column 'a' +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add index t(a, b), add index t1(a); +alter table t add index t2(a), add index t3(a, b); +select * from t use index (t, t1, t2, t3); +a b c +1 2 3 +admin check table t; + +drop table if exists t; +create table t (a int, b int, c int); +alter table t add index t(a), add index t(b); +Error 8200 (HY000): Unsupported operate same index 't' +show index from t; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered +drop table if exists t; +create table t (a int, b int, c int); +alter table t add index t(a), drop column a; +Error 8200 (HY000): Unsupported operate same column 'a' +alter table t add index t(a, b), drop column a; +Error 8200 (HY000): Unsupported operate same column 'a' +show index from t; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 1); +alter table t add unique index i1(a), add unique index i2(a, b), add unique index i3(c); +Error 1062 (23000): Duplicate entry '1' for key 't.i3' +show index from t; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered +alter table t add index i1(a), add index i2(a, b), add index i3(c); +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +alter table t drop index t, drop index t; +Error 8200 (HY000): Unsupported operate same index 't' +drop table if exists t; +create table t (id int, c1 int, c2 int, primary key(id) nonclustered, key i1(c1), key i2(c2), key i3(c1, c2)); +insert into t values (1, 2, 3); +alter table t drop index i1, drop index i2; +select * from t use index(i1); +Error 1176 (42000): Key 'i1' doesn't exist in table 't' +select * from t use index(i2); +Error 1176 (42000): Key 'i2' doesn't exist in table 't' +alter table t drop index i3, drop primary key; +select * from t use index(primary); +Error 1176 (42000): Key 'primary' doesn't exist in table 't' +select * from t use index(i3); +Error 1176 (42000): Key 'i3' doesn't exist in table 't' +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3, index t(a)); +insert into t values (); +alter table t drop index t, drop column a; +select * from t force index(t); +Error 1176 (42000): Key 't' doesn't exist in table 't' +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +alter table t drop index t, add index t(b); +Error 1061 (42000): index already exist t +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +alter table t add index t1(b), drop index t1; +Error 1091 (42000): index t1 doesn't exist +drop table if exists t; +create table t (a int, b int, c int, index (a), index(b), index(c)); +insert into t values (1, 2, 3); +alter table t add index xa(a), drop index a, add index xc(c), drop index b, drop index c, add index xb(b); +select * from t use index(xa, xb, xc); +a b c +1 2 3 +select * from t use index(a); +Error 1176 (42000): Key 'a' doesn't exist in table 't' +select * from t use index(b); +Error 1176 (42000): Key 'b' doesn't exist in table 't' +select * from t use index(c); +Error 1176 (42000): Key 'c' doesn't exist in table 't' +admin check table t; + +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t modify column a int default 2, modify column a bigint; +Error 8200 (HY000): Unsupported operate same column 'a' +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t modify column b double, drop column b; +Error 8200 (HY000): Unsupported operate same column 'b' +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3); +insert into t values (); +alter table t modify column b double after c, drop column c; +Error 8200 (HY000): Unsupported operate same column 'c' +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t add index i(a), modify column a int null default 1 after a; +Error 8200 (HY000): Unsupported operate same column 'a' +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t add primary key(a), modify column a int null default 1 after a; +Error 8200 (HY000): Unsupported operate same column 'a' +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t modify column b double, add index idx((a + b)); +Error 8200 (HY000): Unsupported operate same column 'b' +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t modify column b double default 2 after a, add column c int default 3 after a; +select * from t; +a c b +1 3 2 +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t modify column a bigint, modify column b bigint; +insert into t values (9223372036854775807, 9223372036854775807, 1); +select * from t; +a b c +1 2 3 +9223372036854775807 9223372036854775807 1 +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c)); +insert into t values (1, 2, 3); +alter table t modify column a tinyint, modify column b tinyint, modify column c tinyint; +select * from t; +a b c +1 2 3 +select * from t use index(i1, i2, i3, i4, i5); +a b c +1 2 3 +admin check table t; + +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c)); +insert into t values (1, 2, 3); +alter table t modify column a tinyint after c, modify column b tinyint, modify column c tinyint first; +select * from t; +c b a +3 2 1 +select * from t use index(i1, i2, i3, i4, i5); +c b a +3 2 1 +admin check table t; + +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(c, b)); +insert into t values (1, 2, 3), (11, 22, 33); +alter table t modify column b char(255) after c, modify column a bigint; +select * from t; +a c b +1 3 2 +11 33 22 +select * from t use index(i1, i2); +a c b +1 3 2 +11 33 22 +admin check table t; + +drop table if exists t; +create table t(a bigint null default '1761233443433596323', index t(a)); +insert into t set a = '-7184819032643664798'; +alter table t change column a b datetime null default '8972-12-24 10:56:03', rename index t to t1; +Error 1292 (22007): Incorrect time value: '-7184819032643664798' +drop table if exists t; +create table t (a int, b double, index i(a, b)); +alter table t rename index i to i1, change column b c int; +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +count(*) +1 +drop table if exists t; +create table t (a int, b double, index i(a, b), index _Idx$_i(a, b)); +alter table t rename index i to i1, change column b c int; +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +count(*) +1 +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; +count(*) +1 +drop table if exists t; +create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a)); +alter table t modify column a tinyint; +select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t'; +count(distinct KEY_NAME) +2 +drop table if exists t; +create table t (a BIGINT NULL DEFAULT '-283977870758975838', b double); +insert into t values (-283977870758975838, 0); +alter table t change column a c tinyint null default '111' after b, modify column b time null default '13:51:02' FIRST; +Error 1690 (22003): constant -283977870758975838 overflows tinyint +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t add index i(b), modify column a int null default 1 after a; +Error 1054 (42S22): Unknown column 'a' in 't' +drop table if exists t; +create table t(a char(3), b int, unique index i1(a), index i2(a, b)); +insert into t values ('aaa', 1), ('aa', 2); +set @@sql_mode = ''; +alter table t modify column b tinyint, modify column a char(2); +Error 1062 (23000): Duplicate entry 'aa' for key 't._Idx$_i1_0' +set @@sql_mode = default; +drop table if exists t; +create table t (a int, b int, c int, index i1(c), index i2(c)); +insert into t values (1, 2, 3); +alter table t add column d int default 4, add index i3(c), drop column a, drop column if exists z, add column if not exists e int default 5, drop index i2, add column f int default 6, drop column b, drop index i1, add column if not exists c int; +select * from t; +c d e f +3 4 5 6 +select * from t use index (i1); +Error 1176 (42000): Key 'i1' doesn't exist in table 't' +select * from t use index (i2); +Error 1176 (42000): Key 'i2' doesn't exist in table 't' +select * from t use index (i3); +c d e f +3 4 5 6 +drop table if exists t; +create table t (a int auto_increment primary key, b int); +alter table t modify column b tinyint, auto_increment = 100; +insert into t (b) values (1); +select * from t; +a b +100 1 +drop table if exists t; +create table t (a int auto_increment primary key, b int); +alter table t auto_increment = 110, auto_increment = 90; +show warnings; +Level Code Message +Note 1105 Can't reset AUTO_INCREMENT to 90 without FORCE option, using 110 instead +insert into t (b) values (1); +select * from t; +a b +110 1 +drop table if exists t; +create table t (a int, b int) charset = utf8 shard_row_id_bits=2; +alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4; +select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't'; +TIDB_ROW_ID_SHARDING_INFO TABLE_COMMENT TABLE_COLLATION +SHARD_BITS=2 abc utf8mb4_bin +drop table if exists t; +create table t (a tinyint); +insert into t set a = 10; +alter table t add column b int not null, change column a c char(5) first; +select * from t; +c b +10 0 +drop table if exists t; +create table t (a int, b int, index idx(b)); +alter table t add index idx2(a), alter index idx visible; +select * from t use index (idx, idx2); +a b +alter table t drop column b, alter index idx invisible; +Error 1176 (42000): Key 'idx' doesn't exist in table 't' +select a, b from t; +a b +drop table if exists t; +create table t (a int, b int); +alter table t add column c int, auto_id_cache = 10; +Error 8200 (HY000): Unsupported multi schema change for modify auto id cache +drop table if exists t; +CREATE TABLE t (a SMALLINT DEFAULT '30219', b TIME NULL DEFAULT '02:45:06', PRIMARY KEY (a)); +ALTER TABLE t ADD unique INDEX idx4 (b), change column a e MEDIUMINT DEFAULT '5280454' FIRST; +insert ignore into t (e) values (5586359),(501788),(-5961048),(220083),(-4917129),(-7267211),(7750448); +select * from t; +e b +5586359 02:45:06 +admin check table t; + +drop table if exists t; +create table t (a int); +insert into t values (123); +alter table t add index i(a), add primary key (a); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + KEY `i` (`a`), + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin diff --git a/tests/integrationtest/r/ddl/partition.result b/tests/integrationtest/r/ddl/partition.result new file mode 100644 index 0000000000000..d037541f96ba1 --- /dev/null +++ b/tests/integrationtest/r/ddl/partition.result @@ -0,0 +1,179 @@ +drop table if exists t1; +CREATE TABLE t1 ( +a timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, +b varchar(10), +PRIMARY KEY (a) +) PARTITION BY RANGE (UNIX_TIMESTAMP(a)) ( +PARTITION p1 VALUES LESS THAN (1199134800), +PARTITION pmax VALUES LESS THAN MAXVALUE); +ALTER TABLE t1 REORGANIZE PARTITION pmax INTO ( +PARTITION p3 VALUES LESS THAN (1247688000), +PARTITION pmax VALUES LESS THAN MAXVALUE); +drop table if exists t; +CREATE TABLE t ( +a int NOT NULL primary key , +b varchar(100), +key (b) +) PARTITION BY hash (a) PARTITIONS 1; +insert into t values (1,"a"),(2,"bye"),(3,"Hi"); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(100) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`a`) PARTITIONS 1 +ALTER TABLE t REMOVE PARTITIONING; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(100) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +select * from t; +a b +1 a +2 bye +3 Hi +drop table if exists t; +create table t (a int primary key, b varchar(255), key (b)); +insert into t values (0,'filler0'); +insert into t values (1,'filler0'); +insert into t values (2,'filler0'); +insert into t values (3,'filler1'); +insert into t values (4,'filler1'); +insert into t values (5,'filler1'); +insert into t values (6,'filler2'); +insert into t values (7,'filler2'); +insert into t values (8,'filler2'); +insert into t values (9,'filler3'); +insert into t values (10,'filler3'); +insert into t values (11,'filler3'); +insert into t values (12,'filler4'); +insert into t values (13,'filler4'); +insert into t values (14,'filler4'); +insert into t values (15,'filler5'); +insert into t values (16,'filler5'); +insert into t values (17,'filler5'); +insert into t values (18,'filler6'); +insert into t values (19,'filler6'); +insert into t values (20,'filler6'); +insert into t values (21,'filler7'); +insert into t values (22,'filler7'); +insert into t values (23,'filler7'); +insert into t values (24,'filler8'); +insert into t values (25,'filler8'); +insert into t values (26,'filler8'); +insert into t values (27,'filler9'); +insert into t values (28,'filler9'); +insert into t values (29,'filler9'); +insert into t values (30,'filler10'); +insert into t values (31,'filler10'); +insert into t values (32,'filler10'); +insert into t values (33,'filler11'); +insert into t values (34,'filler11'); +insert into t values (35,'filler11'); +insert into t values (36,'filler12'); +insert into t values (37,'filler12'); +insert into t values (38,'filler12'); +insert into t values (39,'filler13'); +insert into t values (40,'filler13'); +insert into t values (41,'filler13'); +insert into t values (42,'filler14'); +insert into t values (43,'filler14'); +insert into t values (44,'filler14'); +insert into t values (45,'filler15'); +insert into t values (46,'filler15'); +insert into t values (47,'filler15'); +insert into t values (48,'filler16'); +insert into t values (49,'filler16'); +insert into t values (50,'filler16'); +insert into t values (51,'filler17'); +insert into t values (52,'filler17'); +insert into t values (53,'filler17'); +insert into t values (54,'filler18'); +insert into t values (55,'filler18'); +insert into t values (56,'filler18'); +insert into t values (57,'filler19'); +insert into t values (58,'filler19'); +insert into t values (59,'filler19'); +insert into t values (60,'filler20'); +insert into t values (61,'filler20'); +insert into t values (62,'filler20'); +insert into t values (63,'filler21'); +insert into t values (64,'filler21'); +insert into t values (65,'filler21'); +insert into t values (66,'filler22'); +insert into t values (67,'filler22'); +insert into t values (68,'filler22'); +insert into t values (69,'filler23'); +insert into t values (70,'filler23'); +insert into t values (71,'filler23'); +insert into t values (72,'filler24'); +insert into t values (73,'filler24'); +insert into t values (74,'filler24'); +insert into t values (75,'filler25'); +insert into t values (76,'filler25'); +insert into t values (77,'filler25'); +insert into t values (78,'filler26'); +insert into t values (79,'filler26'); +insert into t values (80,'filler26'); +insert into t values (81,'filler27'); +insert into t values (82,'filler27'); +insert into t values (83,'filler27'); +insert into t values (84,'filler28'); +insert into t values (85,'filler28'); +insert into t values (86,'filler28'); +insert into t values (87,'filler29'); +insert into t values (88,'filler29'); +insert into t values (89,'filler29'); +insert into t values (90,'filler30'); +insert into t values (91,'filler30'); +insert into t values (92,'filler30'); +insert into t values (93,'filler31'); +insert into t values (94,'filler31'); +insert into t values (95,'filler31'); +insert into t values (96,'filler32'); +insert into t values (97,'filler32'); +insert into t values (98,'filler32'); +insert into t values (99,'filler33'); +alter table t partition by range (a) (partition p0 values less than (1000000), partition p1 values less than (2000000), partition pMax values less than (maxvalue)); +show warnings; +Level Code Message +Warning 1105 The statistics of new partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (1000000), + PARTITION `p1` VALUES LESS THAN (2000000), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table t partition by hash(a) partitions 7; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`a`) PARTITIONS 7 +alter table t partition by key(a) partitions 5; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY KEY (`a`) PARTITIONS 5 diff --git a/tests/integrationtest/r/ddl/primary_key_handle.result b/tests/integrationtest/r/ddl/primary_key_handle.result new file mode 100644 index 0000000000000..296ff80f8ed57 --- /dev/null +++ b/tests/integrationtest/r/ddl/primary_key_handle.result @@ -0,0 +1,316 @@ +set tidb_enable_clustered_index = ON; +drop table if exists t; +create table t (a int, b varchar(10)); +alter table t add primary key(a) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(a) nonclustered; +alter table t drop primary key; +alter table t add primary key(a) nonclustered; +drop index `primary` on t; +alter table t add primary key(a); +drop index `primary` on t; +drop index `primary` on t; +Error 1091 (42000): Can't DROP 'PRIMARY'; check that column/key exists +drop table if exists t; +create table t (a int, b varchar(10), primary key(a) clustered); +alter table t drop primary key; +Error 8200 (HY000): Unsupported drop primary key when the table is using clustered index +alter table t add primary key(a) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(a) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(a); +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(b) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b); +Error 1068 (42000): Multiple primary key defined +drop table if exists t; +create table t (a int, b varchar(10), primary key(a) nonclustered); +alter table t add primary key(a) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(a) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(a); +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(b) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b); +Error 1068 (42000): Multiple primary key defined +alter table t drop primary key; +drop table if exists t; +create table t (a int, b varchar(10), primary key(b) clustered); +alter table t drop primary key; +Error 8200 (HY000): Unsupported drop primary key when the table is using clustered index +alter table t add primary key(a) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(a) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(a); +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b) clustered; +Error 8200 (HY000): Adding clustered primary key is not supported. Please consider adding NONCLUSTERED primary key instead +alter table t add primary key(b) nonclustered; +Error 1068 (42000): Multiple primary key defined +alter table t add primary key(b); +Error 1068 (42000): Multiple primary key defined +drop table if exists t; +create table t (`primary` int); +alter table t add index (`primary`); +drop index `primary` on t; +Error 1091 (42000): Can't DROP 'PRIMARY'; check that column/key exists +drop table if exists t; +create table t(c1 int not null, primary key(c1) invisible); +Error 3522 (HY000): A primary key index cannot be invisible +create table t (a int, b int not null, primary key(a), unique(b) invisible); +drop table t; +set tidb_enable_clustered_index = default; +set @@tidb_allow_remove_auto_inc = 1; +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (), (), (); +alter table t modify column a bigint auto_random(3); +insert into t values (), (), (); +show table t next_row_id; +DB_NAME TABLE_NAME COLUMN_NAME NEXT_GLOBAL_ROW_ID ID_TYPE +ddl__primary_key_handle t a 60002 AUTO_RANDOM +drop table if exists t; +create table t (a bigint auto_increment unique key); +alter table t modify column a bigint auto_random; +Error 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key +drop table if exists t; +create table t (a bigint auto_increment unique key, b bigint auto_random primary key); +alter table t modify column a bigint auto_random; +Error 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key +drop table if exists t; +create table t (a bigint); +alter table t modify column a bigint auto_random; +Error 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key +drop table if exists t; +create table t (a bigint primary key); +alter table t modify column a bigint auto_random; +Error 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key +drop table if exists t; +create table t (a int auto_increment primary key); +alter table t modify column a int auto_random; +Error 8216 (HY000): Invalid auto random: auto_random option must be defined on `bigint` column, but not on `int` column +alter table t modify column a bigint auto_random; +Error 8216 (HY000): Invalid auto random: modifying the auto_random column type is not supported +drop table if exists t; +create table t (a bigint auto_random primary key); +alter table t modify column a bigint auto_increment; +Error 8200 (HY000): Unsupported modify column: can't set auto_increment +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (1<<(64-5)); +alter table t modify column a bigint auto_random(4); +Error 8216 (HY000): Invalid auto random: max allowed auto_random shard bits is 3, but got 4 on column `a` +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (1<<(64-6)); +alter table t modify column a bigint auto_random(4); +set @@tidb_allow_remove_auto_inc = default; +set @@tidb_enable_exchange_partition=1; +drop table if exists e1, e2, e3, e4; +create table e1 (a bigint primary key clustered auto_random(3)) partition by hash(a) partitions 1; +create table e2 (a bigint primary key); +alter table e1 exchange partition p0 with table e2; +Error 1736 (HY000): Tables have different definitions +create table e3 (a bigint primary key auto_random(2)); +alter table e1 exchange partition p0 with table e3; +Error 1736 (HY000): Tables have different definitions +insert into e1 values (), (), (); +create table e4 (a bigint primary key auto_random(3)); +insert into e4 values (); +alter table e1 exchange partition p0 with table e4; +select count(*) from e1; +count(*) +1 +insert into e1 values (); +select count(*) from e1; +count(*) +2 +select count(*) from e4; +count(*) +3 +insert into e4 values (); +select count(*) from e4; +count(*) +4 +set @@tidb_enable_exchange_partition=default; +drop database if exists auto_random_db; +drop table if exists t; +create table t (a bigint auto_random(6) primary key clustered); +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +a & b'111111111111111111111111111111111111111111111111111111111' +10 +15 +20 +drop table if exists t; +create table t (a bigint auto_random(6) primary key clustered); +set @@auto_increment_increment=2; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +a & b'111111111111111111111111111111111111111111111111111111111' +10 +12 +14 +delete from t; +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +a & b'111111111111111111111111111111111111111111111111111111111' +15 +20 +25 +delete from t; +set @@auto_increment_increment=10; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +a & b'111111111111111111111111111111111111111111111111111111111' +30 +40 +50 +delete from t; +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +a & b'111111111111111111111111111111111111111111111111111111111' +55 +60 +65 +drop database if exists auto_random_db; +set @@auto_increment_increment=default; +set @@auto_increment_offset=default; +drop table if exists t,t1,t2,t3,t4,t5,t6; +create table t (a int, b int, unique (a) invisible); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +a NO +insert into t values (1, 2); +select * from t; +a b +1 2 +alter table t drop index a; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +insert into t values (3, 4); +select * from t; +a b +1 2 +3 4 +alter table t add index (b) invisible; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +b NO +insert into t values (5, 6); +select * from t; +a b +1 2 +3 4 +5 6 +alter table t drop index b; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +insert into t values (7, 8); +select * from t; +a b +1 2 +3 4 +5 6 +7 8 +alter table t add index a_b(a, b) invisible; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +a_b NO +a_b NO +insert into t values (9, 10); +select * from t; +a b +1 2 +3 4 +5 6 +7 8 +9 10 +alter table t drop index a_b; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +index_name is_visible +insert into t values (11, 12); +select * from t; +a b +1 2 +3 4 +5 6 +7 8 +9 10 +11 12 +create table t1 (a int, primary key (a) nonclustered invisible); +Error 3522 (HY000): A primary key index cannot be invisible +create table t1 (a int, b int, primary key (a, b) nonclustered invisible); +Error 3522 (HY000): A primary key index cannot be invisible +create table t1 (a int, b int); +alter table t1 add primary key(a) nonclustered invisible; +Error 3522 (HY000): A primary key index cannot be invisible +alter table t1 add primary key(a, b) nonclustered invisible; +Error 3522 (HY000): A primary key index cannot be invisible +create table t2(a int not null, unique (a) invisible); +Error 3522 (HY000): A primary key index cannot be invisible +create table t2(a int auto_increment, unique key (a) invisible); +Error 3522 (HY000): A primary key index cannot be invisible +create table t2(a int not null); +alter table t2 add unique (a) invisible; +Error 3522 (HY000): A primary key index cannot be invisible +create table t3(a int, unique index (a) invisible); +alter table t3 modify column a int not null; +Error 3522 (HY000): A primary key index cannot be invisible +create table t4(a int not null, b int not null, unique (a), unique (b) invisible); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't4'; +index_name is_visible +a YES +b NO +insert into t4 values (1, 2); +select * from t4; +a b +1 2 +create table t5(a int not null, b int not null, unique (b) invisible, unique (a)); +Error 3522 (HY000): A primary key index cannot be invisible +create table t5(a int not null, b int not null, unique (a), unique (b) invisible); +alter table t5 drop index a; +Error 3522 (HY000): A primary key index cannot be invisible +alter table t5 modify column a int null; +Error 3522 (HY000): A primary key index cannot be invisible +create table t6 (a int not null, b int, unique (a) invisible, primary key(b) nonclustered); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't6'; +index_name is_visible +a NO +PRIMARY YES +insert into t6 values (1, 2); +select * from t6; +a b +1 2 +alter table t6 drop primary key; +Error 3522 (HY000): A primary key index cannot be invisible +show index from t6 where Key_name='PRIMARY'; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression Clustered +t6 0 PRIMARY 1 b A 0 NULL NULL BTREE YES NULL NO diff --git a/tests/integrationtest/r/ddl/reorg_partition.result b/tests/integrationtest/r/ddl/reorg_partition.result new file mode 100644 index 0000000000000..375ea80cf34c2 --- /dev/null +++ b/tests/integrationtest/r/ddl/reorg_partition.result @@ -0,0 +1,863 @@ +drop table if exists t; +create table t (a int unsigned PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (20), partition pMax values less than (MAXVALUE)); +insert into t values (1,"1",1), (12,"12",21),(23,"23",32),(34,"34",43),(45,"45",54),(56,"56",65); +select * from t where c < 40; +a b c +1 1 1 +12 12 21 +23 23 32 +alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (10), + PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (30), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +select * from t; +a b c +1 1 1 +12 12 21 +23 23 32 +34 34 43 +45 45 54 +56 56 65 +select * from t partition (p0); +a b c +1 1 1 +select * from t partition (p1); +a b c +12 12 21 +select * from t partition (p2); +a b c +23 23 32 +select * from t partition (pMax); +a b c +34 34 43 +45 45 54 +56 56 65 +select * from t where b > "1"; +a b c +12 12 21 +23 23 32 +34 34 43 +45 45 54 +56 56 65 +select * from t where c < 40; +a b c +1 1 1 +12 12 21 +23 23 32 +alter table t reorganize partition p2,pMax into (partition p2 values less than (35),partition p3 values less than (47), partition pMax values less than (MAXVALUE)); +admin check table t; + +select * from t; +a b c +1 1 1 +12 12 21 +23 23 32 +34 34 43 +45 45 54 +56 56 65 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (10), + PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +select * from t partition (p0); +a b c +1 1 1 +select * from t partition (p1); +a b c +12 12 21 +select * from t partition (p2); +a b c +23 23 32 +34 34 43 +select * from t partition (p3); +a b c +45 45 54 +select * from t partition (pMax); +a b c +56 56 65 +alter table t reorganize partition p0,p1 into (partition p1 values less than (20)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +select * from t; +a b c +1 1 1 +12 12 21 +23 23 32 +34 34 43 +45 45 54 +56 56 65 +alter table t drop index b; +alter table t drop index c; +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +create table t2 (a int unsigned not null, b varchar(255), c int, key (b), key (c,b)) partition by range (a) (PARTITION `p1` VALUES LESS THAN (20), +PARTITION `p2` VALUES LESS THAN (35), +PARTITION `p3` VALUES LESS THAN (47), +PARTITION `pMax` VALUES LESS THAN (MAXVALUE)); +insert into t2 select * from t; +alter table t2 reorganize partition p2 into (partition p2a values less than (20), partition p2b values less than (36)); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (36)); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (34)); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table t2 reorganize partition pMax into (partition p2b values less than (50)); +Error 1526 (HY000): Table has no partition for value 56 +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table t2 reorganize partition pMax into (partition p4 values less than (90)); +admin check table t2; + +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `p4` VALUES LESS THAN (90)) +drop table t; +create table t (a int PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (abs(a)) (partition p0 values less than (10), partition p1 values less than (20), partition pMax values less than (MAXVALUE)); +insert into t values (0,"0",0),(1,"1",1),(2,"2",-2),(-12,"12",21),(23,"23",32),(-34,"34",43),(45,"45",54),(56,"56",65); +alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (ABS(`a`)) +(PARTITION `p0` VALUES LESS THAN (10), + PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (30), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +select * from t partition (p2); +a b c +23 23 32 +select * from t partition (pMax); +a b c +-34 34 43 +45 45 54 +56 56 65 +alter table t drop index b; +alter table t reorganize partition p0,p1,p2,pMax into (partition pAll values less than (maxvalue)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(255) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (ABS(`a`)) +(PARTITION `pAll` VALUES LESS THAN (MAXVALUE)) +select * from t partition (pAll); +a b c +-12 12 21 +-34 34 43 +0 0 0 +1 1 1 +2 2 -2 +23 23 32 +45 45 54 +56 56 65 +drop table if exists t; +CREATE TABLE t ( +a INT, +b CHAR(3), +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(a,b) ( +PARTITION p0 VALUES LESS THAN (5,'ggg'), +PARTITION p1 VALUES LESS THAN (10,'mmm'), +PARTITION p2 VALUES LESS THAN (15,'sss'), +PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES (1,'abc',1), (3,'ggg',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19); +SELECT * FROM t PARTITION(p0); +a b c +1 abc 1 +3 ggg 3 +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN (2,'ggg'), PARTITION p01 VALUES LESS THAN (5,'ggg')); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` char(3) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`a`,`b`) +(PARTITION `p00` VALUES LESS THAN (2,'ggg'), + PARTITION `p01` VALUES LESS THAN (5,'ggg'), + PARTITION `p1` VALUES LESS THAN (10,'mmm'), + PARTITION `p2` VALUES LESS THAN (15,'sss'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SELECT * FROM t PARTITION(p00); +a b c +1 abc 1 +SELECT * FROM t PARTITION(p01); +a b c +3 ggg 3 +DROP TABLE t; +CREATE TABLE t ( +a INT, +b CHAR(3), +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(b,a) ( +PARTITION p0 VALUES LESS THAN ('ggg',5), +PARTITION p1 VALUES LESS THAN ('mmm',10), +PARTITION p2 VALUES LESS THAN ('sss',15), +PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES (1,'abc',1), (3,'ccc',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19); +SELECT * FROM t PARTITION(p0); +a b c +1 abc 1 +3 ccc 3 +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('ccc',2), PARTITION p01 VALUES LESS THAN ('ggg',5)); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` char(3) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`b`,`a`) +(PARTITION `p00` VALUES LESS THAN ('ccc',2), + PARTITION `p01` VALUES LESS THAN ('ggg',5), + PARTITION `p1` VALUES LESS THAN ('mmm',10), + PARTITION `p2` VALUES LESS THAN ('sss',15), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SELECT * FROM t PARTITION(p00); +a b c +1 abc 1 +SELECT * FROM t PARTITION(p01); +a b c +3 ccc 3 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1 into (PARTITION p1 VALUES LESS THAN ('mmm',10)); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` char(3) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`b`,`a`) +(PARTITION `p1` VALUES LESS THAN ('mmm',10), + PARTITION `p2` VALUES LESS THAN ('sss',15), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SELECT * FROM t PARTITION(p1); +a b c +1 abc 1 +3 ccc 3 +5 ggg 5 +9 ggg 9 +DROP TABLE t; +CREATE TABLE t ( +a DATE, +b DATETIME, +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(a,b) ( +PARTITION p0 VALUES LESS THAN ('2020-05-05','2020-05-05 10:10:10'), +PARTITION p1 VALUES LESS THAN ('2021-05-05','2021-05-05 10:10:10'), +PARTITION p2 VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'), +PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10', '2020-04-10 10:10:10'), PARTITION p01 VALUES LESS THAN ('2020-05-05', '2020-05-05 10:10:10')); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`a`,`b`) +(PARTITION `p00` VALUES LESS THAN ('2020-04-10','2020-04-10 10:10:10'), + PARTITION `p01` VALUES LESS THAN ('2020-05-05','2020-05-05 10:10:10'), + PARTITION `p1` VALUES LESS THAN ('2021-05-05','2021-05-05 10:10:10'), + PARTITION `p2` VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SELECT * FROM t PARTITION(p00); +a b c +SELECT * FROM t PARTITION(p01); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:11')); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:10')); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`a`,`b`) +(PARTITION `p0` VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(p0); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-05 2020-05-05 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +SELECT * FROM t PARTITION(pMax); +a b c +2022-05-05 2022-05-05 10:10:10 5 +2023-05-05 2023-05-05 10:10:10 6 +DROP TABLE t; +CREATE TABLE t ( +a DATE, +b DATETIME, +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(b,a) ( +PARTITION p0 VALUES LESS THAN ('2020-05-05 10:10:10','2020-05-05'), +PARTITION p1 VALUES LESS THAN ('2021-05-05 10:10:10','2021-05-05'), +PARTITION p2 VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'), +PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10 10:10:10', '2020-04-10'), PARTITION p01 VALUES LESS THAN ('2020-05-05 10:10:10', '2020-05-05')); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`b`,`a`) +(PARTITION `p00` VALUES LESS THAN ('2020-04-10 10:10:10','2020-04-10'), + PARTITION `p01` VALUES LESS THAN ('2020-05-05 10:10:10','2020-05-05'), + PARTITION `p1` VALUES LESS THAN ('2021-05-05 10:10:10','2021-05-05'), + PARTITION `p2` VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SELECT * FROM t PARTITION(p00); +a b c +SELECT * FROM t PARTITION(p01); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:11', '2022-05-05')); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:10', '2022-05-05')); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`b`,`a`) +(PARTITION `p0` VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,MAXVALUE)) +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(p0); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-05 2020-05-05 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +SELECT * FROM t PARTITION(pMax); +a b c +2022-05-05 2022-05-05 10:10:10 5 +2023-05-05 2023-05-05 10:10:10 6 +drop table if exists t; +create table t (a int, b varchar(55), c int) partition by list (a) (partition p1 values in (12,23,51,14), partition p2 values in (24,63), partition p3 values in (45)); +insert into t values (12,"12",21), (24,"24",42),(51,"51",15),(23,"23",32),(63,"63",36),(45,"45",54); +alter table t reorganize partition p1 into (partition p0 values in (12,51,13), partition p1 values in (23)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` varchar(55) DEFAULT NULL, + `c` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`a`) +(PARTITION `p0` VALUES IN (12,51,13), + PARTITION `p1` VALUES IN (23), + PARTITION `p2` VALUES IN (24,63), + PARTITION `p3` VALUES IN (45)) +alter table t add primary key (a), add key (b), add key (c,b); +alter table t reorganize partition p1, p3 into (partition pa values in (45,23,15)); +admin check table t; + +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(55) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`a`) +(PARTITION `p0` VALUES IN (12,51,13), + PARTITION `pa` VALUES IN (45,23,15), + PARTITION `p2` VALUES IN (24,63)) +alter table t modify a varchar(20); +Error 8200 (HY000): Unsupported modify column: this column has primary key flag +drop table t; +create table t (a int, b varchar(55), c int) partition by list (abs(a)) +(partition p0 values in (-1,0,1), +partition p1 values in (12,23,51,14), +partition p2 values in (24,63), +partition p3 values in (45)); +insert into t values +(-1,"-1",11),(1,"1",11),(0,"0",0),(-12,"-12",21), +(-24,"-24",42),(51,"-51",15),(23,"23",32),(63,"63",36),(45,"45",54); +alter table t reorganize partition p0, p1 into (partition p0 values in (0,1,2,12,51,13), partition p1 values in (23)); +admin check table t; + +select * from t partition (p0); +a b c +-1 -1 11 +-12 -12 21 +0 0 0 +1 1 11 +51 -51 15 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` varchar(55) DEFAULT NULL, + `c` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (ABS(`a`)) +(PARTITION `p0` VALUES IN (0,1,2,12,51,13), + PARTITION `p1` VALUES IN (23), + PARTITION `p2` VALUES IN (24,63), + PARTITION `p3` VALUES IN (45)) +alter table t add primary key (a), add key (b), add key (c,b); +alter table t reorganize partition p0,p1,p2,p3 into (partition paa values in (0,1,2,12,13,23,24,45,51,63,64)); +admin check table t; + +select * from t partition (paa); +a b c +-1 -1 11 +-12 -12 21 +-24 -24 42 +0 0 0 +1 1 11 +23 23 32 +45 45 54 +51 -51 15 +63 63 36 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) NOT NULL, + `b` varchar(55) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (ABS(`a`)) +(PARTITION `paa` VALUES IN (0,1,2,12,13,23,24,45,51,63,64)) +drop table if exists t; +create table t (a int PRIMARY KEY AUTO_INCREMENT, b varchar(255), c int, d datetime, key (b), key (c,b)) partition by range (a) (partition p1 values less than (0), partition p1M values less than (1000000)); +insert into t values (0, "Zero value!", 0, '2022-02-30'); +Error 1292 (22007): Incorrect datetime value: '2022-02-30' for column 'd' at row 1 +SET @@session.sql_mode = 'ALLOW_INVALID_DATES,NO_AUTO_VALUE_ON_ZERO'; +insert into t values (0, "Zero value!", 0, '2022-02-30'); +show warnings; +Level Code Message +select * from t; +a b c d +0 Zero value! 0 2022-02-30 00:00:00 +SET @@session.sql_mode = default; +alter table t reorganize partition p1M into (partition p0 values less than (1), partition p2M values less than (2000000)); +select * from t; +a b c d +0 Zero value! 0 2022-02-30 00:00:00 +admin check table t; + +drop table if exists t; +CREATE TABLE t ( +a INT, +b CHAR(3), +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY LIST COLUMNS(a,b) ( +PARTITION p0 VALUES IN ((1,'aaa'),(2,'bbb'),(3,'ccc')), +PARTITION p1 VALUES IN ((4,'ddd'),(5,'eee'),(6,'fff')), +PARTITION p2 VALUES IN ((16,'lll'),(17,'mmm'),(18,'lll')) +); +INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16); +SELECT * FROM t PARTITION(p0); +a b c +1 aaa 1 +3 ccc 3 +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb'))); +Error 1526 (HY000): Table has no partition for value from column_list +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb'),(3,'ccc'))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` char(3) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`a`,`b`) +(PARTITION `p00` VALUES IN ((0,'uuu'),(1,'aaa')), + PARTITION `p01` VALUES IN ((2,'bbb'),(3,'ccc')), + PARTITION `p1` VALUES IN ((4,'ddd'),(5,'eee'),(6,'fff')), + PARTITION `p2` VALUES IN ((16,'lll'),(17,'mmm'),(18,'lll'))) +SELECT * FROM t PARTITION(p00); +a b c +1 aaa 1 +SELECT * FROM t PARTITION(p01); +a b c +3 ccc 3 +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN +((0,'uuu'),(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd'),(5,'eee'),(6,'fff'),(16,'lll'),(17,'mmm'),(18,'lll'))); +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(pAll); +a b c +1 aaa 1 +16 lll 16 +3 ccc 3 +5 eee 5 +SELECT * FROM t; +a b c +1 aaa 1 +16 lll 16 +3 ccc 3 +5 eee 5 +DROP TABLE t; +CREATE TABLE t ( +a INT, +b CHAR(3), +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY LIST COLUMNS(b,a) ( +PARTITION p0 VALUES IN (('aaa',1),('bbb',2),('ccc',3)), +PARTITION p1 VALUES IN (('ddd',4),('eee',5),('fff',6)), +PARTITION p2 VALUES IN (('lll',16),('mmm',17),('lll',18)) +); +INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16); +SELECT * FROM t PARTITION(p0); +a b c +1 aaa 1 +3 ccc 3 +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('uuu',-1),('aaa',1)), PARTITION p01 VALUES IN (('bbb',2),('ccc',3),('ccc',4))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` char(3) DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`b`,`a`) +(PARTITION `p00` VALUES IN (('uuu',-1),('aaa',1)), + PARTITION `p01` VALUES IN (('bbb',2),('ccc',3),('ccc',4)), + PARTITION `p1` VALUES IN (('ddd',4),('eee',5),('fff',6)), + PARTITION `p2` VALUES IN (('lll',16),('mmm',17),('lll',18))) +SELECT * FROM t PARTITION(p00); +a b c +1 aaa 1 +SELECT * FROM t PARTITION(p01); +a b c +3 ccc 3 +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN +(('uuu',-1),('aaa',1),('bbb',2),('ccc',3),('ccc',4),('ddd',4),('eee',5),('fff',6),('lll',16),('mmm',17),('lll',18))); +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(pAll); +a b c +1 aaa 1 +16 lll 16 +3 ccc 3 +5 eee 5 +SELECT * FROM t; +a b c +1 aaa 1 +16 lll 16 +3 ccc 3 +5 eee 5 +DROP TABLE t; +CREATE TABLE t ( +a DATE, +b DATETIME, +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY LIST COLUMNS(a,b) ( +PARTITION p0 VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10')), +PARTITION p1 VALUES IN (('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')), +PARTITION p2 VALUES IN (('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10', '2020-04-10 10:10:10')), PARTITION p01 VALUES IN (('2020-05-04', '2020-05-04 10:10:10'))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`a`,`b`) +(PARTITION `p00` VALUES IN (('2020-04-10','2020-04-10 10:10:10')), + PARTITION `p01` VALUES IN (('2020-05-04','2020-05-04 10:10:10')), + PARTITION `p1` VALUES IN (('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')), + PARTITION `p2` VALUES IN (('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11'))) +SELECT * FROM t PARTITION(p00); +a b c +2020-04-10 2020-04-10 10:10:10 1 +SELECT * FROM t PARTITION(p01); +a b c +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'), ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'), ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2023-05-05 11:11:11'))); +Error 1526 (HY000): Table has no partition for value from column_list +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'), ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'), ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11'))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`a`,`b`) +(PARTITION `pAll` VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'),('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'),('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11'))) +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(pAll); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +2022-05-04 2022-05-04 10:10:10 5 +2022-05-05 2022-05-06 11:11:11 6 +SELECT * FROM t; +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +2022-05-04 2022-05-04 10:10:10 5 +2022-05-05 2022-05-06 11:11:11 6 +DROP TABLE t; +CREATE TABLE t ( +a DATE, +b DATETIME, +c INT, +KEY b(b), +KEY c(c,b) +) +PARTITION BY LIST COLUMNS(b,a) ( +PARTITION p0 VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04')), +PARTITION p1 VALUES IN (('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')), +PARTITION p2 VALUES IN (('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10 10:10:10','2020-04-10')), PARTITION p01 VALUES IN (('2020-05-04 10:10:10','2020-05-04'))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `b` (`b`), + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`b`,`a`) +(PARTITION `p00` VALUES IN (('2020-04-10 10:10:10','2020-04-10')), + PARTITION `p01` VALUES IN (('2020-05-04 10:10:10','2020-05-04')), + PARTITION `p1` VALUES IN (('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')), + PARTITION `p2` VALUES IN (('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05'))) +SELECT * FROM t PARTITION(p00); +a b c +2020-04-10 2020-04-10 10:10:10 1 +SELECT * FROM t PARTITION(p01); +a b c +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'), ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'), ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2023-05-05'))); +Error 1526 (HY000): Table has no partition for value from column_list +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'), ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'), ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05'))); +ADMIN CHECK TABLE t; + +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` date DEFAULT NULL, + `b` datetime DEFAULT NULL, + `c` int(11) DEFAULT NULL, + KEY `c` (`c`,`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`b`,`a`) +(PARTITION `pAll` VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'),('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'),('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05'))) +ADMIN CHECK TABLE t; + +SELECT * FROM t PARTITION(pAll); +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +2022-05-04 2022-05-04 10:10:10 5 +2022-05-05 2022-05-06 11:11:11 6 +SELECT * FROM t; +a b c +2020-04-10 2020-04-10 10:10:10 1 +2020-05-04 2020-05-04 10:10:10 2 +2020-05-04 2020-05-04 10:10:10 3 +2021-05-04 2021-05-04 10:10:10 4 +2022-05-04 2022-05-04 10:10:10 5 +2022-05-05 2022-05-06 11:11:11 6 diff --git a/tests/integrationtest/t/ddl/ddl_error.test b/tests/integrationtest/t/ddl/ddl_error.test new file mode 100644 index 0000000000000..4f1ee2cf2a8ac --- /dev/null +++ b/tests/integrationtest/t/ddl/ddl_error.test @@ -0,0 +1,15 @@ +# TestRenameViewOverDifferentSchemaError +drop database if exists ddl__ddl_error2; +drop table if exists table_1; +drop view if exists view_1; +create database test_2; +create table table_1 (a int); +create view view_1 as select a from table_1; +-- error 1450 +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +-- error 1450 +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +-- error 1450 +rename table ddl__ddl_error.view_1 to ddl__ddl_error2.view_1; +rename table ddl__ddl_error.view_1 to ddl__ddl_error.view_1000; + diff --git a/tests/integrationtest/t/ddl/index_modify.test b/tests/integrationtest/t/ddl/index_modify.test new file mode 100644 index 0000000000000..d4eeddd1d43e2 --- /dev/null +++ b/tests/integrationtest/t/ddl/index_modify.test @@ -0,0 +1,27 @@ +# TestAddMultiColumnsIndexClusterIndex +set tidb_enable_clustered_index = ON; +drop table if exists t; +create table t (a int, b varchar(10), c int, primary key (a, b)); +insert into t values (1, '1', 1), (2, '2', NULL), (3, '3', 3); +create index idx on t (a, c); +admin check index t idx; +admin check table t; +insert into t values (5, '5', 5), (6, '6', NULL); +admin check index t idx; +admin check table t; +set tidb_enable_clustered_index = default; + +# TestAddIndexWithDupCols +drop table if exists test_add_index_with_dup; +create table test_add_index_with_dup (a int, b int); +-- error 1060 +create index c on test_add_index_with_dup(b, a, b); +-- error 1060 +create index c on test_add_index_with_dup(b, a, B); +-- error 1060 +alter table test_add_index_with_dup add index c (b, a, b); +-- error 1060 +alter table test_add_index_with_dup add index c (b, a, B); +drop table test_add_index_with_dup; + + diff --git a/tests/integrationtest/t/ddl/integration.test b/tests/integrationtest/t/ddl/integration.test new file mode 100644 index 0000000000000..36d87b0e74f5f --- /dev/null +++ b/tests/integrationtest/t/ddl/integration.test @@ -0,0 +1,75 @@ +# TestDefaultValueIsBinaryString +drop table if exists t; +create table t (a char(10) charset gbk default 0xC4E3BAC3); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a char(10) charset gbk default '好'); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a varchar(10) charset gbk default 0xC4E3BAC3); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a char(10) charset utf8mb4 default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a char(10) charset utf8mb4 default 0b111001001011100010010110111001111001010110001100); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a bit(48) default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a enum('你好') default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +drop table if exists t; +create table t (a set('你好') default 0xE4BDA0E5A5BD); +insert into t values (default); +select a from t; +drop table if exists t; +-- error 1067 +create table t (a char(20) charset utf8mb4 default 0xE4BDA0E5A5BD81); +-- error 1101 +create table t (a blob default 0xE4BDA0E5A5BD81); + +# TestDefaultValueInEnum +# https://github.com/pingcap/tidb/issues/30740 +drop table if exists t; +create table t(a enum('a', 0x91) charset gbk); +insert into t values (1), (2); +select a from t; +drop table t; +create table t (a enum('a', 0x91)) charset gbk; +insert into t values (1), (2); +select a from t; +drop table t; +-- error 1291 +create table t(a set('a', 0x91, '?') charset gbk); +create table t (a enum('a', 0xE4BDA0E5A5BD) charset gbk); +insert into t values (1), (2); +select a from t; + +# TestDDLOnCachedTable +drop table if exists t; +create table t (id int, c int, index(c)); +alter table t cache; +-- error 8242 +drop table t; +-- error 8242 +create index t_id on t (id); +-- error 8242 +alter table t drop index c; +-- error 8242 +alter table t add column (d int); +-- error 8242 +truncate table t; +-- error 8242 +rename table t to t1; +alter table t nocache; +drop table if exists t; + diff --git a/tests/integrationtest/t/ddl/modify_column.test b/tests/integrationtest/t/ddl/modify_column.test new file mode 100644 index 0000000000000..54728bb32855d --- /dev/null +++ b/tests/integrationtest/t/ddl/modify_column.test @@ -0,0 +1,757 @@ +# TestModifyColumnTypeWithWarnings +drop table if exists t; +create table t(a decimal(5,2)); +insert into t values(111.22),(111.22),(111.22),(111.22),(333.4); +alter table t modify column a decimal(4,1); +show warnings; +drop table if exists t; +create table t(a decimal(5,2)); +insert into t values(111.22),(111.22),(111.22),(33.4); +-- error 1690 +alter table t modify column a decimal(3,1); +set @@sql_mode=""; +alter table t modify column a decimal(3,1); +show warnings; +set @@sql_mode=default; + +# TestModifyColumnAutoIncrementWithDefaultValue +drop table if exists t; +create table t (a bigint auto_increment primary key); +-- error 1067 +alter table t modify column a bigint auto_increment default 3; + +# TestModifyColumnTime_TimeToYear +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00:12"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00:12"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("12"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("200012"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (200012); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (0012); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (12); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("30 20:00:12.498"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("20:00:12.498"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values ("200012.498"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a time); +insert into t_mc (a) values (200012.498); +-- error 1264 +alter table t_mc modify a year; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DateToTime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a time; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DateToYear +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a year; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DateToDatetime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("2019-01-02"); +alter table t_mc modify a datetime; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("19-01-02"); +alter table t_mc modify a datetime; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("20190102"); +alter table t_mc modify a datetime; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values ("190102"); +alter table t_mc modify a datetime; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (20190102); +alter table t_mc modify a datetime; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a date); +insert into t_mc (a) values (190102); +alter table t_mc modify a datetime; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_TimestampToYear +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a year; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_TimestampToTime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a time; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_TimestampToDate +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a timestamp); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a date; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DatetimeToYear +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a year; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +-- error 1264 +alter table t_mc modify a year; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +-- error 1264 +alter table t_mc modify a year; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DatetimeToTime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +alter table t_mc modify a time; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DatetimeToDate +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1000-01-02 23:59:59"); +alter table t_mc modify a date; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("9999-01-02 23:59:59"); +alter table t_mc modify a date; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_DatetimeToTimestamp +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 15:04:05"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("06-01-02 15:04:05"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("20060102150405"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("060102150405"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (20060102150405); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values (060102150405); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2006-01-02 23:59:59.506"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("1971-01-02 23:59:59"); +alter table t_mc modify a timestamp; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a datetime); +insert into t_mc (a) values ("2009-01-02 23:59:59"); +alter table t_mc modify a timestamp; +select a from t_mc; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_YearToTime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +-- error 1292 +alter table t_mc modify a time; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +-- error 1292 +alter table t_mc modify a time; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +-- error 1292 +alter table t_mc modify a time; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +alter table t_mc modify a time; +select a from t_mc; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +-- error 1292 +alter table t_mc modify a time; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +-- error 1292 +alter table t_mc modify a time; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +-- error 1292 +alter table t_mc modify a time; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_YearToDate +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +-- error 1292 +alter table t_mc modify a date; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +-- error 1292 +alter table t_mc modify a date; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_YearToDatetime +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +-- error 1292 +alter table t_mc modify a datetime; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +-- error 1292 +alter table t_mc modify a datetime; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + +# TestModifyColumnTime_YearToTimestamp +set @@global.tidb_ddl_error_count_limit = 3; +set @@time_zone=UTC; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("2019"); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (2019); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("00"); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("69"); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("70"); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values ("99"); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (00); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (69); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (70); +-- error 1292 +alter table t_mc modify a timestamp; +drop table if exists t_mc; +create table t_mc(a year); +insert into t_mc (a) values (99); +-- error 1292 +alter table t_mc modify a timestamp; +set @@global.tidb_ddl_error_count_limit = default; +set @@time_zone=default; + diff --git a/tests/integrationtest/t/ddl/multi_schema_change.test b/tests/integrationtest/t/ddl/multi_schema_change.test new file mode 100644 index 0000000000000..e518ebcd349ae --- /dev/null +++ b/tests/integrationtest/t/ddl/multi_schema_change.test @@ -0,0 +1,366 @@ +# TestMultiSchemaChangeAddColumns +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column b int default 2, add column c int default 3; +select * from t; +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (b int default 2, c int default 3); +select * from t; +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add column (d int default 4, e int default 5); +select * from t; +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (index i(a), index i1(a)); +select * from t use index (i, i1); +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (b int default 2, index i(a), primary key (a)); +select * from t use index (i, primary); +drop table if exists t; +create table t (a int); +insert into t values (1); +alter table t add column (index i(a)); +select * from t use index (i); +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add column (index i1(a, b, c), index i2(c, b, a), index i3((a + 1)), index i4((c - 1))); +select * from t use index (i1, i2); +admin check table t; +drop table if exists t; +create table t (a int default 1); +insert into t values (); +alter table t add column if not exists (b int default 2, c int default 3); +select * from t; +alter table t add column if not exists (c int default 3, d int default 4); +show warnings; +select * from t; +drop table if exists t; +create table t (a int); +-- error 1054 +alter table t add column b int after a, add column c int after b; +-- error 1054 +alter table t add column c int after b, add column b int; +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t + add column d int default 4 first, + add column e int default 5 after b, + add column f int default 6 after b; +select * from t; +drop table if exists t; +create table t (a int default 1); +insert into t values (); +alter table t add column b int default 2, add column if not exists a int; +show warnings; +select * from t; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +alter table t add column c double default 3.0, add column d double as (a + b); +select * from t; +drop table if exists t; +create table t (a int default 1, c int default 4); +-- error 8200 +alter table t add column b int default 2, add column b int default 3; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +-- error 8200 +alter table t modify column b double, add column c double as (a + b); + +# TestMultiSchemaChangeDropColumns +drop table if exists t; +create table t (a int, b int); +-- error 1090 +alter table t drop column a, drop column b; +drop table if exists t; +create table t (a int, b int, c int, d int, e int); +insert into t values (1, 2, 3, 4, 5); +alter table t drop column a, drop column d, drop column b; +select * from t; +drop table if exists t; +create table t (a int default 1, c int default 4); +-- error 8200 +alter table t drop column a, drop column a; +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t drop column if exists c, drop column a; +show warnings; +select * from t; +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3); +insert into t values (); +alter table t drop column a, drop column if exists d, drop column c; +show warnings; +select * from t; + +# TestMultiSchemaChangeAddDropColumns +# [a, b] -> [+c, -a, +d, -b] -> [c, d] +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t add column c int default 3, drop column a, add column d int default 4, drop column b; +select * from t; +# [a, b] -> [-a, -b, +c, +d] -> [c, d] +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t drop column a, drop column b, add column c int default 3, add column d int default 4; +select * from t; +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +-- error 8200 +alter table t add column c int default 3 after a, add column d int default 4 first, drop column a, drop column b; + +# TestMultiSchemaChangeAddIndexes +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t add index t(a, b), add index t1(a); +alter table t add index t2(a), add index t3(a, b); +select * from t use index (t, t1, t2, t3); +admin check table t; +drop table if exists t; +create table t (a int, b int, c int); +-- error 8200 +alter table t add index t(a), add index t(b); +show index from t; +drop table if exists t; +create table t (a int, b int, c int); +-- error 8200 +alter table t add index t(a), drop column a; +-- error 8200 +alter table t add index t(a, b), drop column a; +show index from t; +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 1); +-- error 1062 +alter table t add unique index i1(a), add unique index i2(a, b), add unique index i3(c); +show index from t; +alter table t add index i1(a), add index i2(a, b), add index i3(c); + +# TestMultiSchemaChangeDropIndexes +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +-- error 8200 +alter table t drop index t, drop index t; +drop table if exists t; +create table t (id int, c1 int, c2 int, primary key(id) nonclustered, key i1(c1), key i2(c2), key i3(c1, c2)); +insert into t values (1, 2, 3); +alter table t drop index i1, drop index i2; +-- error 1176 +select * from t use index(i1); +-- error 1176 +select * from t use index(i2); +alter table t drop index i3, drop primary key; +-- error 1176 +select * from t use index(primary); +-- error 1176 +select * from t use index(i3); +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3, index t(a)); +insert into t values (); +alter table t drop index t, drop column a; +-- error 1176 +select * from t force index(t); + +# TestMultiSchemaChangeAddDropIndexes +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +-- error 1061 +alter table t drop index t, add index t(b); +drop table if exists t; +create table t (a int, b int, c int, index t(a)); +-- error 1091 +alter table t add index t1(b), drop index t1; +drop table if exists t; +create table t (a int, b int, c int, index (a), index(b), index(c)); +insert into t values (1, 2, 3); +alter table t add index xa(a), drop index a, add index xc(c), drop index b, drop index c, add index xb(b); +select * from t use index(xa, xb, xc); +-- error 1176 +select * from t use index(a); +-- error 1176 +select * from t use index(b); +-- error 1176 +select * from t use index(c); +admin check table t; + +# TestMultiSchemaChangeModifyColumns +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +-- error 8200 +alter table t modify column a int default 2, modify column a bigint; +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +-- error 8200 +alter table t modify column b double, drop column b; +drop table if exists t; +create table t (a int default 1, b int default 2, c int default 3); +insert into t values (); +-- error 8200 +alter table t modify column b double after c, drop column c; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +-- error 8200 +alter table t add index i(a), modify column a int null default 1 after a; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +-- error 8200 +alter table t add primary key(a), modify column a int null default 1 after a; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +-- error 8200 +alter table t modify column b double, add index idx((a + b)); +drop table if exists t; +create table t (a int default 1, b int default 2); +insert into t values (); +alter table t modify column b double default 2 after a, add column c int default 3 after a; +select * from t; +drop table if exists t; +create table t (a int, b int, c int); +insert into t values (1, 2, 3); +alter table t modify column a bigint, modify column b bigint; +insert into t values (9223372036854775807, 9223372036854775807, 1); +select * from t; +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c)); +insert into t values (1, 2, 3); +alter table t modify column a tinyint, modify column b tinyint, modify column c tinyint; +select * from t; +select * from t use index(i1, i2, i3, i4, i5); +admin check table t; +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(b), index i3(c), index i4(a, b), index i5(a, b, c)); +insert into t values (1, 2, 3); +alter table t modify column a tinyint after c, modify column b tinyint, modify column c tinyint first; +select * from t; +select * from t use index(i1, i2, i3, i4, i5); +admin check table t; +drop table if exists t; +create table t (a int, b int, c int, index i1(a), index i2(c, b)); +insert into t values (1, 2, 3), (11, 22, 33); +alter table t modify column b char(255) after c, modify column a bigint; +select * from t; +select * from t use index(i1, i2); +admin check table t; +drop table if exists t; +create table t(a bigint null default '1761233443433596323', index t(a)); +insert into t set a = '-7184819032643664798'; +-- error 1292 +alter table t change column a b datetime null default '8972-12-24 10:56:03', rename index t to t1; +drop table if exists t; +create table t (a int, b double, index i(a, b)); +alter table t rename index i to i1, change column b c int; +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +drop table if exists t; +create table t (a int, b double, index i(a, b), index _Idx$_i(a, b)); +alter table t rename index i to i1, change column b c int; +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; +drop table if exists t; +create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a)); +alter table t modify column a tinyint; +select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t'; +drop table if exists t; +create table t (a BIGINT NULL DEFAULT '-283977870758975838', b double); +insert into t values (-283977870758975838, 0); +-- error 1690 +alter table t change column a c tinyint null default '111' after b, modify column b time null default '13:51:02' FIRST; +drop table if exists t; +create table t(a int, b int); +insert into t values (1, 2); +-- error 1054 +alter table t add index i(b), modify column a int null default 1 after a; +drop table if exists t; +create table t(a char(3), b int, unique index i1(a), index i2(a, b)); +insert into t values ('aaa', 1), ('aa', 2); +set @@sql_mode = ''; +-- error 1062 +alter table t modify column b tinyint, modify column a char(2); +set @@sql_mode = default; + +# TestMultiSchemaChangeMix +drop table if exists t; +create table t (a int, b int, c int, index i1(c), index i2(c)); +insert into t values (1, 2, 3); +alter table t add column d int default 4, add index i3(c), drop column a, drop column if exists z, add column if not exists e int default 5, drop index i2, add column f int default 6, drop column b, drop index i1, add column if not exists c int; +select * from t; +-- error 1176 +select * from t use index (i1); +-- error 1176 +select * from t use index (i2); +select * from t use index (i3); + +# TestMultiSchemaChangeTableOption +drop table if exists t; +create table t (a int auto_increment primary key, b int); +alter table t modify column b tinyint, auto_increment = 100; +insert into t (b) values (1); +select * from t; +drop table if exists t; +create table t (a int auto_increment primary key, b int); +alter table t auto_increment = 110, auto_increment = 90; +show warnings; +insert into t (b) values (1); +select * from t; +drop table if exists t; +create table t (a int, b int) charset = utf8 shard_row_id_bits=2; +alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4; +select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't'; + +# TestMultiSchemaChangeNonPublicDefaultValue +drop table if exists t; +create table t (a tinyint); +insert into t set a = 10; +alter table t add column b int not null, change column a c char(5) first; +select * from t; + +# TestMultiSchemaChangeAlterIndexVisibility +drop table if exists t; +create table t (a int, b int, index idx(b)); +alter table t add index idx2(a), alter index idx visible; +select * from t use index (idx, idx2); +-- error 1176 +alter table t drop column b, alter index idx invisible; +select a, b from t; + +# TestMultiSchemaChangeUnsupportedType +drop table if exists t; +create table t (a int, b int); +-- error 8200 +alter table t add column c int, auto_id_cache = 10; + +# TestMultiSchemaChangeAddIndexChangeColumn +drop table if exists t; +CREATE TABLE t (a SMALLINT DEFAULT '30219', b TIME NULL DEFAULT '02:45:06', PRIMARY KEY (a)); +ALTER TABLE t ADD unique INDEX idx4 (b), change column a e MEDIUMINT DEFAULT '5280454' FIRST; +insert ignore into t (e) values (5586359),(501788),(-5961048),(220083),(-4917129),(-7267211),(7750448); +select * from t; +admin check table t; + +# TestMultiSchemaChangeAddIndexOrder +drop table if exists t; +create table t (a int); +insert into t values (123); +alter table t add index i(a), add primary key (a); +show create table t; + diff --git a/tests/integrationtest/t/ddl/partition.test b/tests/integrationtest/t/ddl/partition.test new file mode 100644 index 0000000000000..4622da78575ad --- /dev/null +++ b/tests/integrationtest/t/ddl/partition.test @@ -0,0 +1,137 @@ +# TestReorgRangeTimestampMaxvalue +drop table if exists t1; +CREATE TABLE t1 ( + a timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + b varchar(10), + PRIMARY KEY (a) +) PARTITION BY RANGE (UNIX_TIMESTAMP(a)) ( + PARTITION p1 VALUES LESS THAN (1199134800), + PARTITION pmax VALUES LESS THAN MAXVALUE); +ALTER TABLE t1 REORGANIZE PARTITION pmax INTO ( +PARTITION p3 VALUES LESS THAN (1247688000), +PARTITION pmax VALUES LESS THAN MAXVALUE); + +# TestRemovePartitioningSinglePartition +drop table if exists t; +CREATE TABLE t ( + a int NOT NULL primary key , + b varchar(100), + key (b) +) PARTITION BY hash (a) PARTITIONS 1; +insert into t values (1,"a"),(2,"bye"),(3,"Hi"); +show create table t; +ALTER TABLE t REMOVE PARTITIONING; +show create table t; +select * from t; + +# TestAlterPartitionBy +drop table if exists t; +create table t (a int primary key, b varchar(255), key (b)); +insert into t values (0,'filler0'); +insert into t values (1,'filler0'); +insert into t values (2,'filler0'); +insert into t values (3,'filler1'); +insert into t values (4,'filler1'); +insert into t values (5,'filler1'); +insert into t values (6,'filler2'); +insert into t values (7,'filler2'); +insert into t values (8,'filler2'); +insert into t values (9,'filler3'); +insert into t values (10,'filler3'); +insert into t values (11,'filler3'); +insert into t values (12,'filler4'); +insert into t values (13,'filler4'); +insert into t values (14,'filler4'); +insert into t values (15,'filler5'); +insert into t values (16,'filler5'); +insert into t values (17,'filler5'); +insert into t values (18,'filler6'); +insert into t values (19,'filler6'); +insert into t values (20,'filler6'); +insert into t values (21,'filler7'); +insert into t values (22,'filler7'); +insert into t values (23,'filler7'); +insert into t values (24,'filler8'); +insert into t values (25,'filler8'); +insert into t values (26,'filler8'); +insert into t values (27,'filler9'); +insert into t values (28,'filler9'); +insert into t values (29,'filler9'); +insert into t values (30,'filler10'); +insert into t values (31,'filler10'); +insert into t values (32,'filler10'); +insert into t values (33,'filler11'); +insert into t values (34,'filler11'); +insert into t values (35,'filler11'); +insert into t values (36,'filler12'); +insert into t values (37,'filler12'); +insert into t values (38,'filler12'); +insert into t values (39,'filler13'); +insert into t values (40,'filler13'); +insert into t values (41,'filler13'); +insert into t values (42,'filler14'); +insert into t values (43,'filler14'); +insert into t values (44,'filler14'); +insert into t values (45,'filler15'); +insert into t values (46,'filler15'); +insert into t values (47,'filler15'); +insert into t values (48,'filler16'); +insert into t values (49,'filler16'); +insert into t values (50,'filler16'); +insert into t values (51,'filler17'); +insert into t values (52,'filler17'); +insert into t values (53,'filler17'); +insert into t values (54,'filler18'); +insert into t values (55,'filler18'); +insert into t values (56,'filler18'); +insert into t values (57,'filler19'); +insert into t values (58,'filler19'); +insert into t values (59,'filler19'); +insert into t values (60,'filler20'); +insert into t values (61,'filler20'); +insert into t values (62,'filler20'); +insert into t values (63,'filler21'); +insert into t values (64,'filler21'); +insert into t values (65,'filler21'); +insert into t values (66,'filler22'); +insert into t values (67,'filler22'); +insert into t values (68,'filler22'); +insert into t values (69,'filler23'); +insert into t values (70,'filler23'); +insert into t values (71,'filler23'); +insert into t values (72,'filler24'); +insert into t values (73,'filler24'); +insert into t values (74,'filler24'); +insert into t values (75,'filler25'); +insert into t values (76,'filler25'); +insert into t values (77,'filler25'); +insert into t values (78,'filler26'); +insert into t values (79,'filler26'); +insert into t values (80,'filler26'); +insert into t values (81,'filler27'); +insert into t values (82,'filler27'); +insert into t values (83,'filler27'); +insert into t values (84,'filler28'); +insert into t values (85,'filler28'); +insert into t values (86,'filler28'); +insert into t values (87,'filler29'); +insert into t values (88,'filler29'); +insert into t values (89,'filler29'); +insert into t values (90,'filler30'); +insert into t values (91,'filler30'); +insert into t values (92,'filler30'); +insert into t values (93,'filler31'); +insert into t values (94,'filler31'); +insert into t values (95,'filler31'); +insert into t values (96,'filler32'); +insert into t values (97,'filler32'); +insert into t values (98,'filler32'); +insert into t values (99,'filler33'); +alter table t partition by range (a) (partition p0 values less than (1000000), partition p1 values less than (2000000), partition pMax values less than (maxvalue)); +show warnings; +show create table t; +alter table t partition by hash(a) partitions 7; +show create table t; +alter table t partition by key(a) partitions 5; +show create table t; + diff --git a/tests/integrationtest/t/ddl/primary_key_handle.test b/tests/integrationtest/t/ddl/primary_key_handle.test new file mode 100644 index 0000000000000..707af5f7bd1af --- /dev/null +++ b/tests/integrationtest/t/ddl/primary_key_handle.test @@ -0,0 +1,247 @@ +# TestPrimaryKey +set tidb_enable_clustered_index = ON; +drop table if exists t; +create table t (a int, b varchar(10)); +-- error 8200 +alter table t add primary key(a) clustered; +alter table t add primary key(a) nonclustered; +alter table t drop primary key; +alter table t add primary key(a) nonclustered; +drop index `primary` on t; +alter table t add primary key(a); +drop index `primary` on t; +-- error 1091 +drop index `primary` on t; +drop table if exists t; +create table t (a int, b varchar(10), primary key(a) clustered); +-- error 8200 +alter table t drop primary key; +-- error 8200 +alter table t add primary key(a) clustered; +-- error 1068 +alter table t add primary key(a) nonclustered; +-- error 1068 +alter table t add primary key(a); +-- error 8200 +alter table t add primary key(b) clustered; +-- error 1068 +alter table t add primary key(b) nonclustered; +-- error 1068 +alter table t add primary key(b); +drop table if exists t; +create table t (a int, b varchar(10), primary key(a) nonclustered); +-- error 8200 +alter table t add primary key(a) clustered; +-- error 1068 +alter table t add primary key(a) nonclustered; +-- error 1068 +alter table t add primary key(a); +-- error 8200 +alter table t add primary key(b) clustered; +-- error 1068 +alter table t add primary key(b) nonclustered; +-- error 1068 +alter table t add primary key(b); +alter table t drop primary key; +drop table if exists t; +create table t (a int, b varchar(10), primary key(b) clustered); +-- error 8200 +alter table t drop primary key; +-- error 8200 +alter table t add primary key(a) clustered; +-- error 1068 +alter table t add primary key(a) nonclustered; +-- error 1068 +alter table t add primary key(a); +-- error 8200 +alter table t add primary key(b) clustered; +-- error 1068 +alter table t add primary key(b) nonclustered; +-- error 1068 +alter table t add primary key(b); +drop table if exists t; +create table t (`primary` int); +alter table t add index (`primary`); +-- error 1091 +drop index `primary` on t; +drop table if exists t; +-- error 3522 +create table t(c1 int not null, primary key(c1) invisible); +create table t (a int, b int not null, primary key(a), unique(b) invisible); +drop table t; +set tidb_enable_clustered_index = default; + +# TestAutoRandomChangeFromAutoInc +set @@tidb_allow_remove_auto_inc = 1; +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (), (), (); +alter table t modify column a bigint auto_random(3); +insert into t values (), (), (); +show table t next_row_id; +drop table if exists t; +create table t (a bigint auto_increment unique key); +-- error 8216 +alter table t modify column a bigint auto_random; +drop table if exists t; +create table t (a bigint auto_increment unique key, b bigint auto_random primary key); +-- error 8216 +alter table t modify column a bigint auto_random; +drop table if exists t; +create table t (a bigint); +-- error 8216 +alter table t modify column a bigint auto_random; +drop table if exists t; +create table t (a bigint primary key); +-- error 8216 +alter table t modify column a bigint auto_random; +drop table if exists t; +create table t (a int auto_increment primary key); +-- error 8216 +alter table t modify column a int auto_random; +-- error 8216 +alter table t modify column a bigint auto_random; +drop table if exists t; +create table t (a bigint auto_random primary key); +-- error 8200 +alter table t modify column a bigint auto_increment; +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (1<<(64-5)); +-- error 8216 +alter table t modify column a bigint auto_random(4); +drop table if exists t; +create table t (a bigint auto_increment primary key); +insert into t values (1<<(64-6)); +alter table t modify column a bigint auto_random(4); +set @@tidb_allow_remove_auto_inc = default; + +# TestAutoRandomExchangePartition +set @@tidb_enable_exchange_partition=1; +drop table if exists e1, e2, e3, e4; +create table e1 (a bigint primary key clustered auto_random(3)) partition by hash(a) partitions 1; +create table e2 (a bigint primary key); +-- error 1736 +alter table e1 exchange partition p0 with table e2; +create table e3 (a bigint primary key auto_random(2)); +-- error 1736 +alter table e1 exchange partition p0 with table e3; +insert into e1 values (), (), (); +create table e4 (a bigint primary key auto_random(3)); +insert into e4 values (); +alter table e1 exchange partition p0 with table e4; +select count(*) from e1; +insert into e1 values (); +select count(*) from e1; +select count(*) from e4; +insert into e4 values (); +select count(*) from e4; +set @@tidb_enable_exchange_partition=default; +drop database if exists auto_random_db; + +# TestAutoRandomIncBitsIncrementAndOffset +drop table if exists t; +create table t (a bigint auto_random(6) primary key clustered); +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +drop table if exists t; +create table t (a bigint auto_random(6) primary key clustered); +set @@auto_increment_increment=2; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +delete from t; +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +delete from t; +set @@auto_increment_increment=10; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +delete from t; +set @@auto_increment_increment=5; +set @@auto_increment_offset=10; +insert into t values (); +insert into t values (); +insert into t values (); +select a & b'111111111111111111111111111111111111111111111111111111111' from t order by a & b'111111111111111111111111111111111111111111111111111111111' asc; +drop database if exists auto_random_db; +set @@auto_increment_increment=default; +set @@auto_increment_offset=default; + +# TestInvisibleIndex +drop table if exists t,t1,t2,t3,t4,t5,t6; +create table t (a int, b int, unique (a) invisible); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (1, 2); +select * from t; +alter table t drop index a; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (3, 4); +select * from t; +alter table t add index (b) invisible; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (5, 6); +select * from t; +alter table t drop index b; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (7, 8); +select * from t; +alter table t add index a_b(a, b) invisible; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (9, 10); +select * from t; +alter table t drop index a_b; +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't'; +insert into t values (11, 12); +select * from t; +-- error 3522 +create table t1 (a int, primary key (a) nonclustered invisible); +-- error 3522 +create table t1 (a int, b int, primary key (a, b) nonclustered invisible); +create table t1 (a int, b int); +-- error 3522 +alter table t1 add primary key(a) nonclustered invisible; +-- error 3522 +alter table t1 add primary key(a, b) nonclustered invisible; +-- error 3522 +create table t2(a int not null, unique (a) invisible); +-- error 3522 +create table t2(a int auto_increment, unique key (a) invisible); +create table t2(a int not null); +-- error 3522 +alter table t2 add unique (a) invisible; +create table t3(a int, unique index (a) invisible); +-- error 3522 +alter table t3 modify column a int not null; +create table t4(a int not null, b int not null, unique (a), unique (b) invisible); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't4'; +insert into t4 values (1, 2); +select * from t4; +-- error 3522 +create table t5(a int not null, b int not null, unique (b) invisible, unique (a)); +create table t5(a int not null, b int not null, unique (a), unique (b) invisible); +-- error 3522 +alter table t5 drop index a; +-- error 3522 +alter table t5 modify column a int null; +create table t6 (a int not null, b int, unique (a) invisible, primary key(b) nonclustered); +select index_name, is_visible from information_schema.statistics where table_schema = 'ddl__primary_key_handle' and table_name = 't6'; +insert into t6 values (1, 2); +select * from t6; +-- error 3522 +alter table t6 drop primary key; +show index from t6 where Key_name='PRIMARY'; + diff --git a/tests/integrationtest/t/ddl/reorg_partition.test b/tests/integrationtest/t/ddl/reorg_partition.test new file mode 100644 index 0000000000000..8a546f0a787d7 --- /dev/null +++ b/tests/integrationtest/t/ddl/reorg_partition.test @@ -0,0 +1,393 @@ +# TestReorganizeRangePartition +drop table if exists t; +create table t (a int unsigned PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (20), partition pMax values less than (MAXVALUE)); +insert into t values (1,"1",1), (12,"12",21),(23,"23",32),(34,"34",43),(45,"45",54),(56,"56",65); +--sorted_result +select * from t where c < 40; +alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE)); +admin check table t; +show create table t; +--sorted_result +select * from t; +--sorted_result +select * from t partition (p0); +--sorted_result +select * from t partition (p1); +--sorted_result +select * from t partition (p2); +--sorted_result +select * from t partition (pMax); +--sorted_result +select * from t where b > "1"; +--sorted_result +select * from t where c < 40; +alter table t reorganize partition p2,pMax into (partition p2 values less than (35),partition p3 values less than (47), partition pMax values less than (MAXVALUE)); +admin check table t; +--sorted_result +select * from t; +show create table t; +--sorted_result +select * from t partition (p0); +--sorted_result +select * from t partition (p1); +--sorted_result +select * from t partition (p2); +--sorted_result +select * from t partition (p3); +--sorted_result +select * from t partition (pMax); +alter table t reorganize partition p0,p1 into (partition p1 values less than (20)); +admin check table t; +show create table t; +--sorted_result +select * from t; +alter table t drop index b; +alter table t drop index c; +admin check table t; +show create table t; +create table t2 (a int unsigned not null, b varchar(255), c int, key (b), key (c,b)) partition by range (a) (PARTITION `p1` VALUES LESS THAN (20), + PARTITION `p2` VALUES LESS THAN (35), + PARTITION `p3` VALUES LESS THAN (47), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)); +insert into t2 select * from t; +-- error 1493 +alter table t2 reorganize partition p2 into (partition p2a values less than (20), partition p2b values less than (36)); +-- error 1493 +alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (36)); +-- error 1493 +alter table t2 reorganize partition p2 into (partition p2a values less than (30), partition p2b values less than (34)); +-- error 1526 +alter table t2 reorganize partition pMax into (partition p2b values less than (50)); +show create table t2; +alter table t2 reorganize partition pMax into (partition p4 values less than (90)); +admin check table t2; +show create table t2; +drop table t; +create table t (a int PRIMARY KEY, b varchar(255), c int, key (b), key (c,b)) partition by range (abs(a)) (partition p0 values less than (10), partition p1 values less than (20), partition pMax values less than (MAXVALUE)); +insert into t values (0,"0",0),(1,"1",1),(2,"2",-2),(-12,"12",21),(23,"23",32),(-34,"34",43),(45,"45",54),(56,"56",65); +alter table t reorganize partition pMax into (partition p2 values less than (30), partition pMax values less than (MAXVALUE)); +admin check table t; +show create table t; +--sorted_result +select * from t partition (p2); +--sorted_result +select * from t partition (pMax); +alter table t drop index b; +alter table t reorganize partition p0,p1,p2,pMax into (partition pAll values less than (maxvalue)); +admin check table t; +show create table t; +--sorted_result +select * from t partition (pAll); + +# TestReorganizeRangeColumnsPartition +drop table if exists t; +CREATE TABLE t ( + a INT, + b CHAR(3), + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(a,b) ( + PARTITION p0 VALUES LESS THAN (5,'ggg'), + PARTITION p1 VALUES LESS THAN (10,'mmm'), + PARTITION p2 VALUES LESS THAN (15,'sss'), + PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES (1,'abc',1), (3,'ggg',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19); +--sorted_result +SELECT * FROM t PARTITION(p0); +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN (2,'ggg'), PARTITION p01 VALUES LESS THAN (5,'ggg')); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +DROP TABLE t; +CREATE TABLE t ( + a INT, + b CHAR(3), + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(b,a) ( + PARTITION p0 VALUES LESS THAN ('ggg',5), + PARTITION p1 VALUES LESS THAN ('mmm',10), + PARTITION p2 VALUES LESS THAN ('sss',15), + PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES (1,'abc',1), (3,'ccc',3),(5,'ggg',5), (9,'ggg',9),(10,'mmm',10),(19,'xxx',19); +--sorted_result +SELECT * FROM t PARTITION(p0); +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('ccc',2), PARTITION p01 VALUES LESS THAN ('ggg',5)); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1 into (PARTITION p1 VALUES LESS THAN ('mmm',10)); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p1); +DROP TABLE t; +CREATE TABLE t ( + a DATE, + b DATETIME, + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(a,b) ( + PARTITION p0 VALUES LESS THAN ('2020-05-05','2020-05-05 10:10:10'), + PARTITION p1 VALUES LESS THAN ('2021-05-05','2021-05-05 10:10:10'), + PARTITION p2 VALUES LESS THAN ('2022-05-05','2022-05-05 10:10:10'), + PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10', '2020-04-10 10:10:10'), PARTITION p01 VALUES LESS THAN ('2020-05-05', '2020-05-05 10:10:10')); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +# TODO(bb7133): different err message with MySQL +-- error 1493 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:11')); +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05', '2022-05-05 10:10:10')); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p0); +--sorted_result +SELECT * FROM t PARTITION(pMax); +DROP TABLE t; +CREATE TABLE t ( + a DATE, + b DATETIME, + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY RANGE COLUMNS(b,a) ( + PARTITION p0 VALUES LESS THAN ('2020-05-05 10:10:10','2020-05-05'), + PARTITION p1 VALUES LESS THAN ('2021-05-05 10:10:10','2021-05-05'), + PARTITION p2 VALUES LESS THAN ('2022-05-05 10:10:10','2022-05-05'), + PARTITION pMax VALUES LESS THAN (MAXVALUE,MAXVALUE) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-05', '2020-05-05 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-05', '2022-05-05 10:10:10', 5), ('2023-05-05', '2023-05-05 10:10:10', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES LESS THAN ('2020-04-10 10:10:10', '2020-04-10'), PARTITION p01 VALUES LESS THAN ('2020-05-05 10:10:10', '2020-05-05')); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t DROP INDEX b; +# TODO(bb7133): different err message with MySQL +-- error 1493 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:11', '2022-05-05')); +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION p0 VALUES LESS THAN ('2022-05-05 10:10:10', '2022-05-05')); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p0); +--sorted_result +SELECT * FROM t PARTITION(pMax); + +# TestReorganizeListPartition +drop table if exists t; +create table t (a int, b varchar(55), c int) partition by list (a) (partition p1 values in (12,23,51,14), partition p2 values in (24,63), partition p3 values in (45)); +insert into t values (12,"12",21), (24,"24",42),(51,"51",15),(23,"23",32),(63,"63",36),(45,"45",54); +alter table t reorganize partition p1 into (partition p0 values in (12,51,13), partition p1 values in (23)); +admin check table t; +show create table t; +alter table t add primary key (a), add key (b), add key (c,b); +# Note: MySQL cannot reorganize two non-consecutive list partitions :) +# ERROR 1519 (HY000): When reorganizing a set of partitions they must be in consecutive order +# https://bugs.mysql.com/bug.php?id=106011 +# https://bugs.mysql.com/bug.php?id=109939 +alter table t reorganize partition p1, p3 into (partition pa values in (45,23,15)); +admin check table t; +show create table t; +-- error 8200 +alter table t modify a varchar(20); +drop table t; +create table t (a int, b varchar(55), c int) partition by list (abs(a)) + (partition p0 values in (-1,0,1), + partition p1 values in (12,23,51,14), + partition p2 values in (24,63), + partition p3 values in (45)); +insert into t values + (-1,"-1",11),(1,"1",11),(0,"0",0),(-12,"-12",21), + (-24,"-24",42),(51,"-51",15),(23,"23",32),(63,"63",36),(45,"45",54); +alter table t reorganize partition p0, p1 into (partition p0 values in (0,1,2,12,51,13), partition p1 values in (23)); +admin check table t; +--sorted_result +select * from t partition (p0); +show create table t; +alter table t add primary key (a), add key (b), add key (c,b); +alter table t reorganize partition p0,p1,p2,p3 into (partition paa values in (0,1,2,12,13,23,24,45,51,63,64)); +admin check table t; +--sorted_result +select * from t partition (paa); +show create table t; + +# TestReorgPartitionData +drop table if exists t; +create table t (a int PRIMARY KEY AUTO_INCREMENT, b varchar(255), c int, d datetime, key (b), key (c,b)) partition by range (a) (partition p1 values less than (0), partition p1M values less than (1000000)); +-- error 1292 +insert into t values (0, "Zero value!", 0, '2022-02-30'); +SET @@session.sql_mode = 'ALLOW_INVALID_DATES,NO_AUTO_VALUE_ON_ZERO'; +insert into t values (0, "Zero value!", 0, '2022-02-30'); +show warnings; +--sorted_result +select * from t; +SET @@session.sql_mode = default; +alter table t reorganize partition p1M into (partition p0 values less than (1), partition p2M values less than (2000000)); +--sorted_result +select * from t; +admin check table t; + +# TestReorganizeListColumnsPartition +drop table if exists t; +CREATE TABLE t ( + a INT, + b CHAR(3), + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY LIST COLUMNS(a,b) ( + PARTITION p0 VALUES IN ((1,'aaa'),(2,'bbb'),(3,'ccc')), + PARTITION p1 VALUES IN ((4,'ddd'),(5,'eee'),(6,'fff')), + PARTITION p2 VALUES IN ((16,'lll'),(17,'mmm'),(18,'lll')) +); +INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16); +--sorted_result +SELECT * FROM t PARTITION(p0); +# TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. +-- error 1526 +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb'))); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN ((0,'uuu'),(1,'aaa')), PARTITION p01 VALUES IN ((2,'bbb'),(3,'ccc'))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t DROP INDEX b; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN + ((0,'uuu'),(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd'),(5,'eee'),(6,'fff'),(16,'lll'),(17,'mmm'),(18,'lll'))); +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(pAll); +--sorted_result +SELECT * FROM t; +DROP TABLE t; +CREATE TABLE t ( + a INT, + b CHAR(3), + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY LIST COLUMNS(b,a) ( + PARTITION p0 VALUES IN (('aaa',1),('bbb',2),('ccc',3)), + PARTITION p1 VALUES IN (('ddd',4),('eee',5),('fff',6)), + PARTITION p2 VALUES IN (('lll',16),('mmm',17),('lll',18)) +); +INSERT INTO t VALUES (1,'aaa',1), (3,'ccc',3),(5,'eee',5), (16,'lll',16); +--sorted_result +SELECT * FROM t PARTITION(p0); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('uuu',-1),('aaa',1)), PARTITION p01 VALUES IN (('bbb',2),('ccc',3),('ccc',4))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t DROP INDEX c; +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN + (('uuu',-1),('aaa',1),('bbb',2),('ccc',3),('ccc',4),('ddd',4),('eee',5),('fff',6),('lll',16),('mmm',17),('lll',18))); +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(pAll); +--sorted_result +SELECT * FROM t; +DROP TABLE t; +CREATE TABLE t ( + a DATE, + b DATETIME, + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY LIST COLUMNS(a,b) ( + PARTITION p0 VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10')), + PARTITION p1 VALUES IN (('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10')), + PARTITION p2 VALUES IN (('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11')) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10', '2020-04-10 10:10:10')), PARTITION p01 VALUES IN (('2020-05-04', '2020-05-04 10:10:10'))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t DROP INDEX b; +# TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. +-- error 1526 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'), ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'), ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2023-05-05 11:11:11'))); +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10','2020-04-10 10:10:10'),('2020-05-04','2020-05-04 10:10:10'), ('2021-05-04','2021-05-04 10:10:10'),('2021-05-05','2021-05-05 10:10:10'), ('2022-05-04','2022-05-04 10:10:10'),('2022-05-05','2022-05-06 11:11:11'))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(pAll); +--sorted_result +SELECT * FROM t; +DROP TABLE t; +CREATE TABLE t ( + a DATE, + b DATETIME, + c INT, + KEY b(b), + KEY c(c,b) +) +PARTITION BY LIST COLUMNS(b,a) ( + PARTITION p0 VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04')), + PARTITION p1 VALUES IN (('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05')), + PARTITION p2 VALUES IN (('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05')) +); +INSERT INTO t VALUES('2020-04-10', '2020-04-10 10:10:10', 1), ('2020-05-04', '2020-05-04 10:10:10', 2),('2020-05-04', '2020-05-04 10:10:10', 3), ('2021-05-04', '2021-05-04 10:10:10', 4),('2022-05-04', '2022-05-04 10:10:10', 5), ('2022-05-05', '2022-05-06 11:11:11', 6); +ALTER TABLE t REORGANIZE PARTITION p0 into (PARTITION p00 VALUES IN (('2020-04-10 10:10:10','2020-04-10')), PARTITION p01 VALUES IN (('2020-05-04 10:10:10','2020-05-04'))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +--sorted_result +SELECT * FROM t PARTITION(p00); +--sorted_result +SELECT * FROM t PARTITION(p01); +ALTER TABLE t DROP INDEX b; +# TODO(bb7133) MySQL 8 does not report an error if there's any row does not fit the new partitions, instead the row will be removed. +-- error 1526 +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'), ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'), ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2023-05-05'))); +ALTER TABLE t REORGANIZE PARTITION p00,p01,p1,p2 into (PARTITION pAll VALUES IN (('2020-04-10 10:10:10','2020-04-10'),('2020-05-04 10:10:10','2020-05-04'), ('2021-05-04 10:10:10','2021-05-04'),('2021-05-05 10:10:10','2021-05-05'), ('2022-05-04 10:10:10','2022-05-04'),('2022-05-06 11:11:11','2022-05-05'))); +ADMIN CHECK TABLE t; +SHOW CREATE TABLE t; +ADMIN CHECK TABLE t; +--sorted_result +SELECT * FROM t PARTITION(pAll); +--sorted_result +SELECT * FROM t; + From 1d0a1a47a92962deaf65d76ad828a10b9fcfa8d6 Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Wed, 11 Oct 2023 18:08:21 +0800 Subject: [PATCH 2/6] fix --- tests/integrationtest/r/ddl/multi_schema_change.result | 10 +++++----- tests/integrationtest/t/ddl/multi_schema_change.test | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/integrationtest/r/ddl/multi_schema_change.result b/tests/integrationtest/r/ddl/multi_schema_change.result index f2b045c8fcfef..d76c1638e6fc4 100644 --- a/tests/integrationtest/r/ddl/multi_schema_change.result +++ b/tests/integrationtest/r/ddl/multi_schema_change.result @@ -329,22 +329,22 @@ Error 1292 (22007): Incorrect time value: '-7184819032643664798' drop table if exists t; create table t (a int, b double, index i(a, b)); alter table t rename index i to i1, change column b c int; -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; count(*) 1 drop table if exists t; create table t (a int, b double, index i(a, b), index _Idx$_i(a, b)); alter table t rename index i to i1, change column b c int; -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; count(*) 1 -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; count(*) 1 drop table if exists t; create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a)); alter table t modify column a tinyint; -select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t'; +select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t'; count(distinct KEY_NAME) 2 drop table if exists t; @@ -398,7 +398,7 @@ a b drop table if exists t; create table t (a int, b int) charset = utf8 shard_row_id_bits=2; alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4; -select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't'; +select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where TABLE_SCHEMA='ddl__multi_schema_change' and table_name = 't'; TIDB_ROW_ID_SHARDING_INFO TABLE_COMMENT TABLE_COLLATION SHARD_BITS=2 abc utf8mb4_bin drop table if exists t; diff --git a/tests/integrationtest/t/ddl/multi_schema_change.test b/tests/integrationtest/t/ddl/multi_schema_change.test index e518ebcd349ae..29e8a6fe2403c 100644 --- a/tests/integrationtest/t/ddl/multi_schema_change.test +++ b/tests/integrationtest/t/ddl/multi_schema_change.test @@ -270,16 +270,16 @@ alter table t change column a b datetime null default '8972-12-24 10:56:03', ren drop table if exists t; create table t (a int, b double, index i(a, b)); alter table t rename index i to i1, change column b c int; -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; drop table if exists t; create table t (a int, b double, index i(a, b), index _Idx$_i(a, b)); alter table t rename index i to i1, change column b c int; -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; -select count(*) from information_schema.TIDB_INDEXES where TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='i1'; +select count(*) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t' and COLUMN_NAME='c' and KEY_NAME='_Idx$_i'; drop table if exists t; create table t (a int, _Col$_a double, index _Idx$_i(a, _Col$_a), index i(a, _Col$_a)); alter table t modify column a tinyint; -select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_NAME='t'; +select count(distinct KEY_NAME) from information_schema.TIDB_INDEXES where TABLE_SCHEMA='ddl__multi_schema_change' and TABLE_NAME='t'; drop table if exists t; create table t (a BIGINT NULL DEFAULT '-283977870758975838', b double); insert into t values (-283977870758975838, 0); @@ -325,7 +325,7 @@ select * from t; drop table if exists t; create table t (a int, b int) charset = utf8 shard_row_id_bits=2; alter table t modify column a tinyint, comment = 'abc', charset = utf8mb4; -select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where table_name = 't'; +select TIDB_ROW_ID_SHARDING_INFO, TABLE_COMMENT, TABLE_COLLATION from information_schema.tables where TABLE_SCHEMA='ddl__multi_schema_change' and table_name = 't'; # TestMultiSchemaChangeNonPublicDefaultValue drop table if exists t; From 7e67db2df25bfe09916c86bbb06d12c51cb3c40a Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Thu, 12 Oct 2023 11:46:10 +0800 Subject: [PATCH 3/6] update sequence --- ddl/sequence_test.go | 586 -------------------- tests/integrationtest/r/ddl/sequence.result | 502 +++++++++++++++++ tests/integrationtest/t/ddl/sequence.test | 369 ++++++++++++ 3 files changed, 871 insertions(+), 586 deletions(-) create mode 100644 tests/integrationtest/r/ddl/sequence.result create mode 100644 tests/integrationtest/t/ddl/sequence.test diff --git a/ddl/sequence_test.go b/ddl/sequence_test.go index d704626792c0c..942bf23f0ee1f 100644 --- a/ddl/sequence_test.go +++ b/ddl/sequence_test.go @@ -15,19 +15,16 @@ package ddl_test import ( - "strconv" "testing" "time" mysql "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/model" - "github.com/pingcap/tidb/parser/terror" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/external" - "github.com/pingcap/tidb/util/dbterror" "github.com/stretchr/testify/require" ) @@ -94,215 +91,6 @@ func TestCreateSequence(t *testing.T) { require.EqualError(t, err, "[planner:1142]CREATE command denied to user 'myuser'@'localhost' for table 'my_seq'") } -// Test for sequence still works with a infoschema attached by temporary table -func TestIssue28881(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop sequence if exists s") - tk.MustExec("create sequence s") - defer tk.MustExec("drop sequence s") - tk.MustExec("create temporary table tmp1 (id int)") - - tk.MustQuery("select nextval(s)").Check(testkit.Rows("1")) - tk.MustQuery("select lastval(s)").Check(testkit.Rows("1")) -} - -func TestDropSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop sequence if exists seq") - - // Test sequence is unknown. - tk.MustGetErrCode("drop sequence seq", mysql.ErrUnknownSequence) - - // Test non-existed sequence can't drop successfully. - tk.MustExec("create sequence seq") - _, err := tk.Exec("drop sequence seq, seq2") - require.Error(t, err) - require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'test.seq2'") - - // Test the specified object is not sequence. - tk.MustExec("create table seq3 (a int)") - err = tk.ExecToErr("drop sequence seq3") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject)) - - // Test schema is not exist. - err = tk.ExecToErr("drop sequence unknown.seq") - require.Error(t, err) - require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'unknown.seq'") - - // Test drop sequence successfully. - tk.MustExec("create sequence seq") - tk.MustExec("drop sequence seq") - err = tk.ExecToErr("drop sequence seq") - require.Error(t, err) - require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'test.seq'") - - // Test drop table when the object is a sequence. - tk.MustExec("create sequence seq") - err = tk.ExecToErr("drop table seq") - require.Error(t, err) - require.EqualError(t, err, "[schema:1051]Unknown table 'test.seq'") - - // Test drop view when the object is a sequence. - err = tk.ExecToErr("drop view seq") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject)) - tk.MustExec("drop sequence seq") - - // Test drop privilege. - tk.MustExec("drop user if exists myuser@localhost") - tk.MustExec("create user myuser@localhost") - - tk1 := testkit.NewTestKit(t, store) - se, err := session.CreateSession4Test(store) - require.NoError(t, err) - require.NoError(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil, nil)) - tk1.SetSession(se) - - // grant the myuser the access to database test. - tk.MustExec("create sequence my_seq") - tk.MustExec("grant select on test.* to 'myuser'@'localhost'") - - tk1.MustExec("use test") - _, err = tk1.Exec("drop sequence my_seq") - require.Error(t, err) - require.EqualError(t, err, "[planner:1142]DROP command denied to user 'myuser'@'localhost' for table 'my_seq'") - - // Test for `drop sequence if exists`. - tk.MustExec("drop sequence if exists seq_if_exists") - tk.MustQuery("show warnings;").Check(testkit.Rows("Note 4139 Unknown SEQUENCE: 'test.seq_if_exists'")) -} - -func TestShowCreateSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create table t(a int)") - tk.MustExec("create sequence seq") - - // Test show privilege. - tk.MustExec("drop user if exists myuser@localhost") - tk.MustExec("create user myuser@localhost") - - tk1 := testkit.NewTestKit(t, store) - se, err := session.CreateSession4Test(store) - require.NoError(t, err) - require.NoError(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil, nil)) - tk1.SetSession(se) - - // Grant the myuser the access to table t in database test, but sequence seq. - tk.MustExec("grant select on test.t to 'myuser'@'localhost'") - - tk1.MustExec("use test") - tk1.MustExec("show create table t") - _, err = tk1.Exec("show create sequence seq") - require.Error(t, err) - require.EqualError(t, err, "[planner:1142]SHOW command denied to user 'myuser'@'localhost' for table 'seq'") - - // Grant the myuser the access to sequence seq in database test. - tk.MustExec("grant select on test.seq to 'myuser'@'localhost'") - - tk1.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB")) - - // Test show sequence detail. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq start 10") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 10 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq minvalue 0") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 0 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq maxvalue 100") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 100 increment by 1 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq increment = -2") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with -1 minvalue -9223372036854775807 maxvalue -1 increment by -2 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq nocache") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq cycle") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 cycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq comment=\"ccc\"") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB COMMENT='ccc'")) - - // Test show create sequence with a normal table. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create table seq (a int)") - err = tk.QueryToErr("show create sequence seq") - require.Error(t, err) - require.EqualError(t, err, "[executor:1347]'test.seq' is not SEQUENCE") - tk.MustExec("drop table if exists seq") - - // Test use the show create sequence result to create sequence. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - showString := tk.MustQuery("show create sequence seq").Rows()[0][1].(string) - tk.MustExec("drop sequence if exists seq") - tk.MustExec(showString) -} - -func TestSequenceAsDefaultValue(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - - // test the use sequence's nextval as default. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int not null default next value for seq key)") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int not null default nextval(seq), b int, primary key(a))") - - tk.MustExec("create table t1 (a int default next value for seq)") - tk.MustGetErrMsg("create table t2 (a char(1) default next value for seq)", "[ddl:8228]Unsupported sequence default value for column type 'a'") - - tk.MustExec("create table t3 (a int default nextval(seq))") - - tk.MustExec("create table t4 (a int)") - tk.MustExec("alter table t4 alter column a set default (next value for seq)") - tk.MustExec("alter table t4 alter column a set default (nextval(seq))") - - tk.MustExec("create table t5 (a char(1))") - tk.MustGetErrMsg("alter table t5 alter column a set default (next value for seq)", "[ddl:8228]Unsupported sequence default value for column type 'a'") - - tk.MustGetErrMsg("alter table t5 alter column a set default (nextval(seq))", "[ddl:8228]Unsupported sequence default value for column type 'a'") - - // Specially, the new added column with sequence as it's default value is forbade. - // But alter table column with sequence as it's default value is allowed. - tk.MustGetErrMsg("alter table t5 add column c int default next value for seq", "[ddl:8230]Unsupported using sequence as default value in add column 'c'") - - tk.MustExec("alter table t5 add column c int default -1") - // Alter with modify. - tk.MustExec("alter table t5 modify column c int default next value for seq") - // Alter with alter. - tk.MustExec("alter table t5 alter column c set default (next value for seq)") - // Alter with change. - tk.MustExec("alter table t5 change column c c int default next value for seq") -} - func TestSequenceFunction(t *testing.T) { store := testkit.CreateMockStore(t) session.SetSchemaLease(600 * time.Millisecond) @@ -731,140 +519,6 @@ func TestSequenceFunction(t *testing.T) { tk.MustExec("drop table t") } -func TestInsertSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop sequence if exists seq") - tk.MustExec("drop table if exists t") - - // test insert with sequence default value. - tk.MustExec("create sequence seq") - tk.MustExec("create table t (a int default next value for seq)") - tk.MustExec("insert into t values()") - tk.MustQuery("select * from t").Check(testkit.Rows("1")) - tk.MustExec("insert into t values(),(),()") - tk.MustQuery("select * from t").Check(testkit.Rows("1", "2", "3", "4")) - tk.MustExec("delete from t") - tk.MustExec("insert into t values(-1),(default),(-1)") - tk.MustQuery("select * from t").Check(testkit.Rows("-1", "5", "-1")) - - // test insert with specified sequence value rather than default. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int)") - tk.MustExec("insert into t values(next value for seq)") - tk.MustQuery("select * from t").Check(testkit.Rows("6")) - tk.MustExec("insert into t values(next value for seq),(nextval(seq))") - tk.MustQuery("select * from t").Check(testkit.Rows("6", "7", "8")) - - // test insert with sequence expression. - tk.MustExec("delete from t") - tk.MustExec("insert into t values(next value for seq + 1),(nextval(seq) * 2)") - tk.MustQuery("select * from t").Check(testkit.Rows("10", "20")) - tk.MustExec("delete from t") - tk.MustExec("insert into t values((next value for seq - 1) / 2)") - tk.MustQuery("select * from t").Check(testkit.Rows("5")) - - // test insert with user specified value. - tk.MustExec("delete from t") - tk.MustExec("insert into t values(-1),(next value for seq),(-1),(nextval(seq))") - tk.MustQuery("select * from t").Check(testkit.Rows("-1", "12", "-1", "13")) - - // test insert with lastval & setval. - tk.MustExec("delete from t") - tk.MustExec("insert into t values(lastval(seq)),(-1),(nextval(seq))") - tk.MustQuery("select * from t").Check(testkit.Rows("13", "-1", "14")) - tk.MustExec("delete from t") - tk.MustQuery("select setval(seq, 100)").Check(testkit.Rows("100")) - tk.MustExec("insert into t values(lastval(seq)),(-1),(nextval(seq))") - tk.MustQuery("select * from t").Check(testkit.Rows("14", "-1", "101")) - - // test insert with generated column. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int default next value for seq, col1 int generated always as (id + 1))") - - tk.MustExec("insert into t values()") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2")) - tk.MustExec("insert into t values(),()") - tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "2 3", "3 4")) - tk.MustExec("delete from t") - tk.MustExec("insert into t (id) values(-1),(default)") - tk.MustQuery("select * from t").Check(testkit.Rows("-1 0", "4 5")) - - // test sequence run out (overflows MaxInt64). - setSQL := "select setval(seq," + strconv.FormatInt(model.DefaultPositiveSequenceMaxValue+1, 10) + ")" - tk.MustQuery(setSQL).Check(testkit.Rows("9223372036854775807")) - err := tk.QueryToErr("select nextval(seq)") - require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out") -} - -func TestUnflodSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - // test insert into select from. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("drop table if exists t1,t2,t3") - tk.MustExec("create sequence seq") - tk.MustExec("create table t1 (a int)") - tk.MustExec("create table t2 (a int, b int)") - tk.MustExec("create table t3 (a int, b int, c int)") - tk.MustExec("insert into t1 values(-1),(-1),(-1)") - // test sequence function unfold. - tk.MustQuery("select nextval(seq), a from t1").Check(testkit.Rows("1 -1", "2 -1", "3 -1")) - tk.MustExec("insert into t2 select nextval(seq), a from t1") - tk.MustQuery("select * from t2").Check(testkit.Rows("4 -1", "5 -1", "6 -1")) - tk.MustExec("delete from t2") - - // if lastval is folded, the first result should be always 6. - tk.MustQuery("select lastval(seq), nextval(seq), a from t1").Check(testkit.Rows("6 7 -1", "7 8 -1", "8 9 -1")) - tk.MustExec("insert into t3 select lastval(seq), nextval(seq), a from t1") - tk.MustQuery("select * from t3").Check(testkit.Rows("9 10 -1", "10 11 -1", "11 12 -1")) - tk.MustExec("delete from t3") - - // if setval is folded, the result should be "101 100 -1"... - tk.MustQuery("select nextval(seq), setval(seq,100), a from t1").Check(testkit.Rows("13 100 -1", "101 -1", "102 -1")) - tk.MustExec("insert into t3 select nextval(seq), setval(seq,200), a from t1") - tk.MustQuery("select * from t3").Check(testkit.Rows("103 200 -1", "201 -1", "202 -1")) - tk.MustExec("delete from t3") - - // lastval should be evaluated after nextval in each row. - tk.MustQuery("select nextval(seq), lastval(seq), a from t1").Check(testkit.Rows("203 203 -1", "204 204 -1", "205 205 -1")) - tk.MustExec("insert into t3 select nextval(seq), lastval(seq), a from t1") - tk.MustQuery("select * from t3").Check(testkit.Rows("206 206 -1", "207 207 -1", "208 208 -1")) - tk.MustExec("delete from t3") - - // double nextval should be also evaluated in each row. - tk.MustQuery("select nextval(seq), nextval(seq), a from t1").Check(testkit.Rows("209 210 -1", "211 212 -1", "213 214 -1")) - tk.MustExec("insert into t3 select nextval(seq), nextval(seq), a from t1") - tk.MustQuery("select * from t3").Check(testkit.Rows("215 216 -1", "217 218 -1", "219 220 -1")) - tk.MustExec("delete from t3") - - tk.MustQuery("select nextval(seq)+lastval(seq), a from t1").Check(testkit.Rows("442 -1", "444 -1", "446 -1")) - tk.MustExec("insert into t2 select nextval(seq)+lastval(seq), a from t1") - tk.MustQuery("select * from t2").Check(testkit.Rows("448 -1", "450 -1", "452 -1")) - tk.MustExec("delete from t2") - - // sub-query contain sequence function. - tk.MustQuery("select nextval(seq), b from (select nextval(seq) as b, a from t1) t2").Check(testkit.Rows("227 228", "229 230", "231 232")) - tk.MustExec("insert into t2 select nextval(seq), b from (select nextval(seq) as b, a from t1) t2") - tk.MustQuery("select * from t2").Check(testkit.Rows("233 234", "235 236", "237 238")) - //nolint:all_revive,revive - tk.MustExec("delete from t2") - - // For union operator like select1 union select2, select1 and select2 will be executed parallelly, - // so sequence function in both select are evaluated without order. Besides, the upper union operator - // will gather results through multi worker goroutine parallelly leading the results unordered. - // Cases like: - // `select nextval(seq), a from t1 union select lastval(seq), a from t2` - // `select nextval(seq), a from t1 union select nextval(seq), a from t2` - // The executing order of nextval and lastval is implicit, don't make any assumptions on it. -} - // before this PR: // single insert consume: 50.498672ms // after this PR: @@ -891,243 +545,3 @@ func BenchmarkInsertCacheDefaultExpr(b *testing.B) { tk.MustExec(sql) } } - -func TestSequenceFunctionPrivilege(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test sequence function privilege. - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int default next value for seq)") - tk.MustExec("drop user if exists myuser@localhost") - tk.MustExec("create user myuser@localhost") - - tk1 := testkit.NewTestKit(t, store) - se, err := session.CreateSession4Test(store) - require.NoError(t, err) - require.NoError(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil, nil)) - tk1.SetSession(se) - - // grant the myuser the create access to the sequence. - tk.MustExec("grant insert on test.t to 'myuser'@'localhost'") - - // INSERT privilege required to use nextval. - tk1.MustExec("use test") - err = tk1.QueryToErr("select nextval(seq)") - require.Error(t, err) - require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'") - - _, err = tk1.Exec("insert into t values()") - require.Error(t, err) - require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'") - - // SELECT privilege required to use lastval. - err = tk1.QueryToErr("select lastval(seq)") - require.Error(t, err) - require.EqualError(t, err, "[expression:1142]SELECT command denied to user 'myuser'@'localhost' for table 'seq'") - - // INSERT privilege required to use setval. - err = tk1.QueryToErr("select setval(seq, 10)") - require.Error(t, err) - require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'") - - // grant the myuser the SELECT & UPDATE access to sequence seq. - tk.MustExec("grant SELECT, INSERT on test.seq to 'myuser'@'localhost'") - - // SELECT privilege required to use nextval. - tk1.MustQuery("select nextval(seq)").Check(testkit.Rows("1")) - tk1.MustQuery("select lastval(seq)").Check(testkit.Rows("1")) - tk1.MustQuery("select setval(seq, 10)").Check(testkit.Rows("10")) - tk1.MustExec("insert into t values()") - - tk.MustExec("drop table t") - tk.MustExec("drop sequence seq") - tk.MustExec("drop user myuser@localhost") -} - -// Background: the newly added column in TiDB won't fill the known rows with specific -// sequence next value immediately. Every time TiDB select the data from storage, kvDB -// will fill the originDefaultValue to these incomplete rows (but not store). -// -// In sequence case, every time filling these rows, kvDB should eval the sequence -// expr for len(incomplete rows) times, and combine these row data together. That -// means the select result is not always the same. -// -// However, the altered column with sequence as it's default value can work well. -// Because this column has already been added before the alter action, which also -// means originDefaultValue should be something but nil, so the back filling in kvDB -// can work well. -// -// The new altered sequence default value for this column only take effect on the -// subsequent inserted rows. -// -// So under current situation, TiDB will -// [1]: forbid the new added column has sequence as it's default value. -// [2]: allow the altered column with sequence as default value. -func TestSequenceDefaultLogic(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("drop table if exists t") - tk.MustExec("create sequence seq") - tk.MustExec("create table t(a int)") - - // Alter table to use sequence as default value is ok. - tk.MustExec("insert into t values(-1),(-1),(-1)") - tk.MustExec("alter table t add column b int default -1") - tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1")) - tk.MustExec("alter table t modify column b int default next value for seq") - tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1")) - tk.MustExec("insert into t(a) values(-1),(-1)") - tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1", "-1 1", "-1 2")) - - // Add column to set sequence as default value is forbade. - tk.MustExec("drop sequence seq") - tk.MustExec("drop table t") - tk.MustExec("create sequence seq") - tk.MustExec("create table t(a int)") - tk.MustExec("insert into t values(-1),(-1),(-1)") - tk.MustGetErrMsg("alter table t add column b int default next value for seq", "[ddl:8230]Unsupported using sequence as default value in add column 'b'") - tk.MustQuery("select * from t").Check(testkit.Rows("-1", "-1", "-1")) -} - -// Close issue #17945, sequence cache shouldn't be negative. -func TestSequenceCacheShouldNotBeNegative(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop sequence if exists seq") - err := tk.ExecToErr("create sequence seq cache -1") - require.Error(t, err) - require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") - - err = tk.ExecToErr("create sequence seq cache 0") - require.Error(t, err) - require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") - - // This will error because - // 1: maxvalue = -1 by default - // 2: minvalue = -9223372036854775807 by default - // 3: increment = -9223372036854775807 by user - // `seqInfo.CacheValue < (math.MaxInt64-absIncrement)/absIncrement` will - // ensure there is enough value for one cache allocation at least. - err = tk.ExecToErr("create sequence seq INCREMENT -9223372036854775807 cache 1") - require.Error(t, err) - require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") - - tk.MustExec("create sequence seq cache 1") -} - -func TestAlterSequence(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - tk.MustExec("alter sequence seq increment by 2 start with 2") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` " + - "start with 2 minvalue 1 maxvalue 9223372036854775806 increment by 2 cache 1000 nocycle ENGINE=InnoDB")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq") - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3")) - // Alter sequence will invalidate the sequence cache in memory. - tk.MustExec("alter sequence seq increment by 2") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` " + - "start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 2 cache 1000 nocycle ENGINE=InnoDB")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1001")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1003")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1005")) - - // Restart with value will reset the sequence value in kv. - tk.MustExec("alter sequence seq restart with 9") - // Like setval does, the sequence current value change won't affect the increment frequency. - // By now the step frequency is: 1, 3, 5, 7, 9, 11, 13, 15... - // After restart with 9, the current value rebased to 8, the next valid value will be 9, coincidentally equal to what we restarted. - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("9")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("11")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("13")) - - tk.MustExec("alter sequence seq restart with 10") - // After restart with 10, the current value rebased to 9, the next valid value will be 11, rather than what we restart. - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("11")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("13")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("15")) - - // Restart will reset the sequence value to start value by default. - tk.MustExec("alter sequence seq restart") - // After restart, the base will be pointed to 0, the first value will be 1 here, then plus the increment 2, the second value will be 3. - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("5")) - - tk.MustExec("drop sequence if exists seq") - tk.MustExec("create sequence seq increment by 3") - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("4")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("7")) - - tk.MustExec("alter sequence seq increment by 4") - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3001")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3005")) - tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3009")) - tk.MustExec("drop sequence if exists seq") -} - -func TestAlterSequencePrivilege(t *testing.T) { - store := testkit.CreateMockStore(t) - session.SetSchemaLease(600 * time.Millisecond) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop sequence if exists my_seq") - tk.MustExec("create sequence my_seq") - - // Test create privilege. - tk.MustExec("drop user if exists myuser@localhost") - tk.MustExec("create user myuser@localhost") - - tk1 := testkit.NewTestKit(t, store) - se, err := session.CreateSession4Test(store) - require.NoError(t, err) - require.NoError(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil, nil)) - tk1.SetSession(se) - - // grant the myuser the access to database test. - tk.MustExec("grant select on test.* to 'myuser'@'localhost'") - - tk1.MustExec("use test") - err = tk1.ExecToErr("alter sequence my_seq increment = 2") - require.Error(t, err) - require.EqualError(t, err, "[planner:1142]ALTER command denied to user 'myuser'@'localhost' for table 'my_seq'") - tk.MustExec("drop sequence if exists my_seq") -} - -func TestDdl_AlterSequenceIssue31265(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create sequence seq cache=1 nocache") - tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB")) - - tk.MustExec("create sequence cache_to_nocache_seq;") - tk.MustExec("alter sequence cache_to_nocache_seq nocache;") - tk.MustQuery("show create sequence cache_to_nocache_seq;").Check(testkit.Rows("cache_to_nocache_seq CREATE SEQUENCE `cache_to_nocache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB")) - - tk.MustExec("create sequence nocache_to_cache_seq nocache;") - tk.MustExec("alter sequence nocache_to_cache_seq cache 10;") - tk.MustQuery("show create sequence nocache_to_cache_seq;").Check(testkit.Rows("nocache_to_cache_seq CREATE SEQUENCE `nocache_to_cache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 10 nocycle ENGINE=InnoDB")) -} diff --git a/tests/integrationtest/r/ddl/sequence.result b/tests/integrationtest/r/ddl/sequence.result new file mode 100644 index 0000000000000..a887999d17fdd --- /dev/null +++ b/tests/integrationtest/r/ddl/sequence.result @@ -0,0 +1,502 @@ +drop sequence if exists s; +create sequence s; +create temporary table tmp1 (id int); +select nextval(s); +nextval(s) +1 +select lastval(s); +lastval(s) +1 +drop sequence s; +drop sequence if exists seq; +drop sequence seq; +Error 4139 (HY000): Unknown SEQUENCE: 'ddl__sequence.seq' +create sequence seq; +drop sequence seq, seq2; +Error 4139 (HY000): Unknown SEQUENCE: 'ddl__sequence.seq2' +create table seq3 (a int); +drop sequence seq3; +Error 1347 (HY000): 'ddl__sequence.seq3' is not SEQUENCE +drop sequence unknown.seq; +Error 4139 (HY000): Unknown SEQUENCE: 'unknown.seq' +create sequence seq; +drop sequence seq; +drop sequence seq; +Error 4139 (HY000): Unknown SEQUENCE: 'ddl__sequence.seq' +create sequence seq; +drop table seq; +Error 1051 (42S02): Unknown table 'ddl__sequence.seq' +drop view seq; +Error 1347 (HY000): 'ddl__sequence.seq' is not VIEW +drop sequence seq; +drop user if exists myuser@localhost; +create user myuser@localhost; +create sequence my_seq; +grant select on ddl__sequence.* to 'myuser'@'localhost'; +drop sequence my_seq; +Error 1142 (42000): DROP command denied to user 'myuser'@'localhost' for table 'my_seq' +drop sequence my_seq; +drop sequence if exists seq_if_exists; +show warnings; +Level Code Message +Note 4139 Unknown SEQUENCE: 'ddl__sequence.seq_if_exists' +drop table if exists t; +drop sequence if exists seq; +create table t(a int); +create sequence seq; +drop user if exists myuser@localhost; +create user myuser@localhost; +grant select on ddl__sequence.t to 'myuser'@'localhost'; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +show create sequence seq; +Error 1142 (42000): SHOW command denied to user 'myuser'@'localhost' for table 'seq' +grant select on ddl__sequence.seq to 'myuser'@'localhost'; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq start 10; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 10 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq minvalue 0; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 0 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq maxvalue 100; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 100 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq increment = -2; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with -1 minvalue -9223372036854775807 maxvalue -1 increment by -2 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq nocache; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq cycle; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 cycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq comment="ccc"; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB COMMENT='ccc' +drop sequence if exists seq; +create table seq (a int); +show create sequence seq; +Error 1347 (HY000): 'ddl__sequence.seq' is not SEQUENCE +drop table if exists seq; +drop sequence if exists seq; +create sequence seq; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB; +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t(a int not null default next value for seq key); +drop table if exists t; +create table t(a int not null default nextval(seq), b int, primary key(a)); +create table t1 (a int default next value for seq); +create table t2 (a char(1) default next value for seq); +Error 8228 (HY000): Unsupported sequence default value for column type 'a' +create table t3 (a int default nextval(seq)); +create table t4 (a int); +alter table t4 alter column a set default (next value for seq); +alter table t4 alter column a set default (nextval(seq)); +create table t5 (a char(1)); +alter table t5 alter column a set default (next value for seq); +Error 8228 (HY000): Unsupported sequence default value for column type 'a' +alter table t5 alter column a set default (nextval(seq)); +Error 8228 (HY000): Unsupported sequence default value for column type 'a' +alter table t5 add column c int default next value for seq; +Error 8230 (HY000): Unsupported using sequence as default value in add column 'c' +alter table t5 add column c int default -1; +alter table t5 modify column c int default next value for seq; +alter table t5 alter column c set default (next value for seq); +alter table t5 change column c c int default next value for seq; +drop sequence if exists seq; +drop table if exists t; +create sequence seq; +create table t (a int default next value for seq); +insert into t values(); +select * from t; +a +1 +insert into t values(),(),(); +select * from t; +a +1 +2 +3 +4 +delete from t; +insert into t values(-1),(default),(-1); +select * from t; +a +-1 +5 +-1 +drop table if exists t; +create table t (a int); +insert into t values(next value for seq); +select * from t; +a +6 +insert into t values(next value for seq),(nextval(seq)); +select * from t; +a +6 +7 +8 +delete from t; +insert into t values(next value for seq + 1),(nextval(seq) * 2); +select * from t; +a +10 +20 +delete from t; +insert into t values((next value for seq - 1) / 2); +select * from t; +a +5 +delete from t; +insert into t values(-1),(next value for seq),(-1),(nextval(seq)); +select * from t; +a +-1 +12 +-1 +13 +delete from t; +insert into t values(lastval(seq)),(-1),(nextval(seq)); +select * from t; +a +13 +-1 +14 +delete from t; +select setval(seq, 100); +setval(seq, 100) +100 +insert into t values(lastval(seq)),(-1),(nextval(seq)); +select * from t; +a +14 +-1 +101 +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t (id int default next value for seq, col1 int generated always as (id + 1)); +insert into t values(); +select * from t; +id col1 +1 2 +insert into t values(),(); +select * from t; +id col1 +1 2 +2 3 +3 4 +delete from t; +insert into t (id) values(-1),(default); +select * from t; +id col1 +-1 0 +4 5 +select setval(seq,9223372036854775807); +setval(seq,9223372036854775807) +9223372036854775807 +select nextval(seq); +Error 4135 (HY000): Sequence 'ddl__sequence.seq' has run out +drop sequence if exists seq; +drop table if exists t1,t2,t3; +create sequence seq; +create table t1 (a int); +create table t2 (a int, b int); +create table t3 (a int, b int, c int); +insert into t1 values(-1),(-1),(-1); +select nextval(seq), a from t1; +nextval(seq) a +1 -1 +2 -1 +3 -1 +insert into t2 select nextval(seq), a from t1; +select * from t2; +a b +4 -1 +5 -1 +6 -1 +delete from t2; +select lastval(seq), nextval(seq), a from t1; +lastval(seq) nextval(seq) a +6 7 -1 +7 8 -1 +8 9 -1 +insert into t3 select lastval(seq), nextval(seq), a from t1; +select * from t3; +a b c +9 10 -1 +10 11 -1 +11 12 -1 +delete from t3; +select nextval(seq), setval(seq,100), a from t1; +nextval(seq) setval(seq,100) a +13 100 -1 +101 NULL -1 +102 NULL -1 +insert into t3 select nextval(seq), setval(seq,200), a from t1; +select * from t3; +a b c +103 200 -1 +201 NULL -1 +202 NULL -1 +delete from t3; +select nextval(seq), lastval(seq), a from t1; +nextval(seq) lastval(seq) a +203 203 -1 +204 204 -1 +205 205 -1 +insert into t3 select nextval(seq), lastval(seq), a from t1; +select * from t3; +a b c +206 206 -1 +207 207 -1 +208 208 -1 +delete from t3; +select nextval(seq), nextval(seq), a from t1; +nextval(seq) nextval(seq) a +209 210 -1 +211 212 -1 +213 214 -1 +insert into t3 select nextval(seq), nextval(seq), a from t1; +select * from t3; +a b c +215 216 -1 +217 218 -1 +219 220 -1 +delete from t3; +select nextval(seq)+lastval(seq), a from t1; +nextval(seq)+lastval(seq) a +442 -1 +444 -1 +446 -1 +insert into t2 select nextval(seq)+lastval(seq), a from t1; +select * from t2; +a b +448 -1 +450 -1 +452 -1 +delete from t2; +select nextval(seq), b from (select nextval(seq) as b, a from t1) t2; +nextval(seq) b +227 228 +229 230 +231 232 +insert into t2 select nextval(seq), b from (select nextval(seq) as b, a from t1) t2; +select * from t2; +a b +233 234 +235 236 +237 238 +delete from t2; +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t(a int default next value for seq); +drop user if exists myuser@localhost; +create user myuser@localhost; +grant insert on ddl__sequence.t to 'myuser'@'localhost'; +select nextval(seq); +Error 1142 (42000): INSERT command denied to user 'myuser'@'localhost' for table 'seq' +insert into t values(); +Error 1142 (42000): INSERT command denied to user 'myuser'@'localhost' for table 'seq' +select lastval(seq); +Error 1142 (42000): SELECT command denied to user 'myuser'@'localhost' for table 'seq' +select setval(seq, 10); +Error 1142 (42000): INSERT command denied to user 'myuser'@'localhost' for table 'seq' +grant SELECT, INSERT on ddl__sequence.seq to 'myuser'@'localhost'; +select nextval(seq); +nextval(seq) +1 +select lastval(seq); +lastval(seq) +1 +select setval(seq, 10); +setval(seq, 10) +10 +insert into t values(); +drop table t; +drop sequence seq; +drop user myuser@localhost; +drop sequence if exists seq; +drop table if exists t; +create sequence seq; +create table t(a int); +insert into t values(-1),(-1),(-1); +alter table t add column b int default -1; +select * from t; +a b +-1 -1 +-1 -1 +-1 -1 +alter table t modify column b int default next value for seq; +select * from t; +a b +-1 -1 +-1 -1 +-1 -1 +insert into t(a) values(-1),(-1); +select * from t; +a b +-1 -1 +-1 -1 +-1 -1 +-1 1 +-1 2 +drop sequence seq; +drop table t; +create sequence seq; +create table t(a int); +insert into t values(-1),(-1),(-1); +alter table t add column b int default next value for seq; +Error 8230 (HY000): Unsupported using sequence as default value in add column 'b' +select * from t; +a +-1 +-1 +-1 +drop sequence if exists seq; +create sequence seq cache -1; +Error 4136 (HY000): Sequence 'ddl__sequence.seq' values are conflicting +create sequence seq cache 0; +Error 4136 (HY000): Sequence 'ddl__sequence.seq' values are conflicting +create sequence seq INCREMENT -9223372036854775807 cache 1; +Error 4136 (HY000): Sequence 'ddl__sequence.seq' values are conflicting +create sequence seq cache 1; +drop sequence if exists seq; +create sequence seq; +alter sequence seq increment by 2 start with 2; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 2 minvalue 1 maxvalue 9223372036854775806 increment by 2 cache 1000 nocycle ENGINE=InnoDB +drop sequence if exists seq; +create sequence seq; +select nextval(seq); +nextval(seq) +1 +select nextval(seq); +nextval(seq) +2 +select nextval(seq); +nextval(seq) +3 +alter sequence seq increment by 2; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 2 cache 1000 nocycle ENGINE=InnoDB +select nextval(seq); +nextval(seq) +1001 +select nextval(seq); +nextval(seq) +1003 +select nextval(seq); +nextval(seq) +1005 +alter sequence seq restart with 9; +select nextval(seq); +nextval(seq) +9 +select nextval(seq); +nextval(seq) +11 +select nextval(seq); +nextval(seq) +13 +alter sequence seq restart with 10; +select nextval(seq); +nextval(seq) +11 +select nextval(seq); +nextval(seq) +13 +select nextval(seq); +nextval(seq) +15 +alter sequence seq restart; +select nextval(seq); +nextval(seq) +1 +select nextval(seq); +nextval(seq) +3 +select nextval(seq); +nextval(seq) +5 +drop sequence if exists seq; +create sequence seq increment by 3; +select nextval(seq); +nextval(seq) +1 +select nextval(seq); +nextval(seq) +4 +select nextval(seq); +nextval(seq) +7 +alter sequence seq increment by 4; +select nextval(seq); +nextval(seq) +3001 +select nextval(seq); +nextval(seq) +3005 +select nextval(seq); +nextval(seq) +3009 +drop sequence if exists seq; +drop sequence if exists my_seq; +create sequence my_seq; +drop user if exists myuser@localhost; +create user myuser@localhost; +grant select on ddl__sequence.* to 'myuser'@'localhost'; +alter sequence my_seq increment = 2; +Error 1142 (42000): ALTER command denied to user 'myuser'@'localhost' for table 'my_seq' +drop sequence if exists my_seq; +drop sequence if exists seq, cache_to_nocache_seq, nocache_to_cache_seq; +create sequence seq cache=1 nocache; +show create sequence seq; +Sequence Create Sequence +seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB +create sequence cache_to_nocache_seq; +alter sequence cache_to_nocache_seq nocache; +show create sequence cache_to_nocache_seq; +Sequence Create Sequence +cache_to_nocache_seq CREATE SEQUENCE `cache_to_nocache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB +create sequence nocache_to_cache_seq nocache; +alter sequence nocache_to_cache_seq cache 10; +show create sequence nocache_to_cache_seq; +Sequence Create Sequence +nocache_to_cache_seq CREATE SEQUENCE `nocache_to_cache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 10 nocycle ENGINE=InnoDB +drop sequence if exists seq, cache_to_nocache_seq, nocache_to_cache_seq; diff --git a/tests/integrationtest/t/ddl/sequence.test b/tests/integrationtest/t/ddl/sequence.test new file mode 100644 index 0000000000000..4055f5dc55ed7 --- /dev/null +++ b/tests/integrationtest/t/ddl/sequence.test @@ -0,0 +1,369 @@ +# TestIssue28881 +drop sequence if exists s; +create sequence s; +create temporary table tmp1 (id int); +select nextval(s); +select lastval(s); +drop sequence s; + +# TestDropSequence +drop sequence if exists seq; +-- error 4139 +drop sequence seq; +create sequence seq; +-- error 4139 +drop sequence seq, seq2; +create table seq3 (a int); +-- error 1347 +drop sequence seq3; +-- error 4139 +drop sequence unknown.seq; +create sequence seq; +drop sequence seq; +-- error 4139 +drop sequence seq; +create sequence seq; +-- error 1051 +drop table seq; +-- error 1347 +drop view seq; +drop sequence seq; + +# grant the myuser the access to database test. +drop user if exists myuser@localhost; +create user myuser@localhost; +create sequence my_seq; +grant select on ddl__sequence.* to 'myuser'@'localhost'; +connect (conn1, localhost, myuser,, ddl__sequence); +-- error 1142 +drop sequence my_seq; +disconnect conn1; +drop sequence my_seq; + +drop sequence if exists seq_if_exists; +show warnings; + +# TestShowCreateSequence +drop table if exists t; +drop sequence if exists seq; +create table t(a int); +create sequence seq; +drop user if exists myuser@localhost; +create user myuser@localhost; +# Test show privilege. +grant select on ddl__sequence.t to 'myuser'@'localhost'; +connect (conn1, localhost, myuser,, ddl__sequence); +connection conn1; +show create table t; +-- error 1142 +show create sequence seq; +connection default; +grant select on ddl__sequence.seq to 'myuser'@'localhost'; +connection conn1; +show create sequence seq; +connection default; + +drop sequence if exists seq; +create sequence seq; +show create sequence seq; +drop sequence if exists seq; +create sequence seq start 10; +show create sequence seq; +drop sequence if exists seq; +create sequence seq minvalue 0; +show create sequence seq; +drop sequence if exists seq; +create sequence seq maxvalue 100; +show create sequence seq; +drop sequence if exists seq; +create sequence seq increment = -2; +show create sequence seq; +drop sequence if exists seq; +create sequence seq nocache; +show create sequence seq; +drop sequence if exists seq; +create sequence seq cycle; +show create sequence seq; +drop sequence if exists seq; +create sequence seq comment="ccc"; +show create sequence seq; +drop sequence if exists seq; +create table seq (a int); +-- error 1347 +show create sequence seq; +drop table if exists seq; +drop sequence if exists seq; +create sequence seq; +show create sequence seq; +drop sequence if exists seq; +CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB; + +# TestSequenceAsDefaultValue +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t(a int not null default next value for seq key); +drop table if exists t; +create table t(a int not null default nextval(seq), b int, primary key(a)); +create table t1 (a int default next value for seq); +-- error 8228 +create table t2 (a char(1) default next value for seq); +create table t3 (a int default nextval(seq)); +create table t4 (a int); +alter table t4 alter column a set default (next value for seq); +alter table t4 alter column a set default (nextval(seq)); +create table t5 (a char(1)); +-- error 8228 +alter table t5 alter column a set default (next value for seq); +-- error 8228 +alter table t5 alter column a set default (nextval(seq)); +-- error 8230 +alter table t5 add column c int default next value for seq; +alter table t5 add column c int default -1; +alter table t5 modify column c int default next value for seq; +alter table t5 alter column c set default (next value for seq); +alter table t5 change column c c int default next value for seq; + +# TestInsertSequence +drop sequence if exists seq; +drop table if exists t; +create sequence seq; +create table t (a int default next value for seq); +insert into t values(); +select * from t; +insert into t values(),(),(); +select * from t; +delete from t; +insert into t values(-1),(default),(-1); +select * from t; +drop table if exists t; +create table t (a int); +insert into t values(next value for seq); +select * from t; +insert into t values(next value for seq),(nextval(seq)); +select * from t; +delete from t; +insert into t values(next value for seq + 1),(nextval(seq) * 2); +select * from t; +delete from t; +insert into t values((next value for seq - 1) / 2); +select * from t; +delete from t; +insert into t values(-1),(next value for seq),(-1),(nextval(seq)); +select * from t; +delete from t; +insert into t values(lastval(seq)),(-1),(nextval(seq)); +select * from t; +delete from t; +select setval(seq, 100); +insert into t values(lastval(seq)),(-1),(nextval(seq)); +select * from t; +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t (id int default next value for seq, col1 int generated always as (id + 1)); +insert into t values(); +select * from t; +insert into t values(),(); +select * from t; +delete from t; +insert into t (id) values(-1),(default); +select * from t; +select setval(seq,9223372036854775807); +-- error 4135 +select nextval(seq); + +# TestUnflodSequence +drop sequence if exists seq; +drop table if exists t1,t2,t3; +create sequence seq; +create table t1 (a int); +create table t2 (a int, b int); +create table t3 (a int, b int, c int); +insert into t1 values(-1),(-1),(-1); +select nextval(seq), a from t1; +insert into t2 select nextval(seq), a from t1; +select * from t2; +delete from t2; +select lastval(seq), nextval(seq), a from t1; +insert into t3 select lastval(seq), nextval(seq), a from t1; +select * from t3; +delete from t3; +select nextval(seq), setval(seq,100), a from t1; +insert into t3 select nextval(seq), setval(seq,200), a from t1; +select * from t3; +delete from t3; +select nextval(seq), lastval(seq), a from t1; +insert into t3 select nextval(seq), lastval(seq), a from t1; +select * from t3; +delete from t3; +select nextval(seq), nextval(seq), a from t1; +insert into t3 select nextval(seq), nextval(seq), a from t1; +select * from t3; +delete from t3; +select nextval(seq)+lastval(seq), a from t1; +insert into t2 select nextval(seq)+lastval(seq), a from t1; +select * from t2; +delete from t2; +select nextval(seq), b from (select nextval(seq) as b, a from t1) t2; +insert into t2 select nextval(seq), b from (select nextval(seq) as b, a from t1) t2; +select * from t2; +delete from t2; +# For union operator like select1 union select2, select1 and select2 will be executed parallelly, +# so sequence function in both select are evaluated without order. Besides, the upper union operator +# will gather results through multi worker goroutine parallelly leading the results unordered. +# Cases like: +# `select nextval(seq), a from t1 union select lastval(seq), a from t2` +# `select nextval(seq), a from t1 union select nextval(seq), a from t2` +# The executing order of nextval and lastval is implicit, don't make any assumptions on it. + +# TestSequenceFunctionPrivilege +drop sequence if exists seq; +create sequence seq; +drop table if exists t; +create table t(a int default next value for seq); +# Test sequence function privilege. +drop user if exists myuser@localhost; +create user myuser@localhost; +grant insert on ddl__sequence.t to 'myuser'@'localhost'; +connect (conn1, localhost, myuser,, ddl__sequence); +connection conn1; +-- error 1142 +select nextval(seq); +-- error 1142 +insert into t values(); +-- error 1142 +select lastval(seq); +-- error 1142 +select setval(seq, 10); +connection default; +grant SELECT, INSERT on ddl__sequence.seq to 'myuser'@'localhost'; +connection conn1; +select nextval(seq); +select lastval(seq); +select setval(seq, 10); +insert into t values(); +disconnect conn1; +drop table t; +drop sequence seq; +drop user myuser@localhost; + +# Background: the newly added column in TiDB won't fill the known rows with specific +# sequence next value immediately. Every time TiDB select the data from storage, kvDB +# will fill the originDefaultValue to these incomplete rows (but not store). +# +# In sequence case, every time filling these rows, kvDB should eval the sequence +# expr for len(incomplete rows) times, and combine these row data together. That +# means the select result is not always the same. +# +# However, the altered column with sequence as it's default value can work well. +# Because this column has already been added before the alter action, which also +# means originDefaultValue should be something but nil, so the back filling in kvDB +# can work well. +# +# The new altered sequence default value for this column only take effect on the +# subsequent inserted rows. +# +# So under current situation, TiDB will +# [0]: forbid the new added column has sequence as it's default value. +# [1]: allow the altered column with sequence as default value. +# TestSequenceDefaultLogic +drop sequence if exists seq; +drop table if exists t; +create sequence seq; +create table t(a int); +insert into t values(-1),(-1),(-1); +alter table t add column b int default -1; +select * from t; +alter table t modify column b int default next value for seq; +select * from t; +insert into t(a) values(-1),(-1); +select * from t; +drop sequence seq; +drop table t; +create sequence seq; +create table t(a int); +insert into t values(-1),(-1),(-1); +-- error 8230 +alter table t add column b int default next value for seq; +select * from t; + +# Close issue #17945, sequence cache shouldn't be negative. +# TestSequenceCacheShouldNotBeNegative +drop sequence if exists seq; +-- error 4136 +create sequence seq cache -1; +-- error 4136 +create sequence seq cache 0; +# This will error because +# 1: maxvalue = -1 by default +# 2: minvalue = -9223372036854775807 by default +# 3: increment = -9223372036854775807 by user +# `seqInfo.CacheValue < (math.MaxInt64-absIncrement)/absIncrement` will +# ensure there is enough value for one cache allocation at least. +-- error 4136 +create sequence seq INCREMENT -9223372036854775807 cache 1; +create sequence seq cache 1; + +# TestAlterSequence +drop sequence if exists seq; +create sequence seq; +alter sequence seq increment by 2 start with 2; +show create sequence seq; +drop sequence if exists seq; +create sequence seq; +select nextval(seq); +select nextval(seq); +select nextval(seq); +alter sequence seq increment by 2; +show create sequence seq; +select nextval(seq); +select nextval(seq); +select nextval(seq); +alter sequence seq restart with 9; +select nextval(seq); +select nextval(seq); +select nextval(seq); +alter sequence seq restart with 10; +select nextval(seq); +select nextval(seq); +select nextval(seq); +alter sequence seq restart; +select nextval(seq); +select nextval(seq); +select nextval(seq); +drop sequence if exists seq; +create sequence seq increment by 3; +select nextval(seq); +select nextval(seq); +select nextval(seq); +alter sequence seq increment by 4; +select nextval(seq); +select nextval(seq); +select nextval(seq); +drop sequence if exists seq; + +# TestAlterSequencePrivilege +drop sequence if exists my_seq; +create sequence my_seq; +drop user if exists myuser@localhost; +create user myuser@localhost; +grant select on ddl__sequence.* to 'myuser'@'localhost'; +connect (conn1, localhost, myuser,, ddl__sequence); +-- error 1142 +alter sequence my_seq increment = 2; +connection default; +drop sequence if exists my_seq; + +# TestDdl_AlterSequenceIssue31265 +drop sequence if exists seq, cache_to_nocache_seq, nocache_to_cache_seq; +create sequence seq cache=1 nocache; +show create sequence seq; +create sequence cache_to_nocache_seq; +alter sequence cache_to_nocache_seq nocache; +show create sequence cache_to_nocache_seq; +create sequence nocache_to_cache_seq nocache; +alter sequence nocache_to_cache_seq cache 10; +show create sequence nocache_to_cache_seq; +drop sequence if exists seq, cache_to_nocache_seq, nocache_to_cache_seq; + From d77fec9d12e3d0da773081304dee93be67b79a6e Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Thu, 12 Oct 2023 12:25:04 +0800 Subject: [PATCH 4/6] update --- .../r/planner/cascades/integration.result | 11 ++++++----- tests/integrationtest/t/ddl/sequence.test | 1 + .../t/planner/cascades/integration.test | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/integrationtest/r/planner/cascades/integration.result b/tests/integrationtest/r/planner/cascades/integration.result index d133602987722..8b3e15e1845bb 100644 --- a/tests/integrationtest/r/planner/cascades/integration.result +++ b/tests/integrationtest/r/planner/cascades/integration.result @@ -890,13 +890,14 @@ a 2 3 set session tidb_enable_cascades_planner = 1; -explain select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist; +explain select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist where DB = "planner__cascades__integration"; id estRows task access object operator info -Projection_3 10000.00 root Column#2, Column#4, Column#5, Column#6, Column#7, Column#8, Column#9 -└─MemTableScan_4 10000.00 root table:PROCESSLIST -select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist; +Projection_4 8000.00 root Column#2, Column#4, Column#5, Column#6, Column#7, Column#8, Column#9 +└─Selection_5 8000.00 root eq(Column#4, "planner__cascades__integration") + └─MemTableScan_6 10000.00 root table:PROCESSLIST +select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist where DB = "planner__cascades__integration"; USER DB COMMAND TIME STATE INFO DIGEST -root planner__cascades__integration Query 0 autocommit select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist 78f1c0b2fcd082b6504b8ba48706391f13aad730a9efb7ec295688010bfec477 +root planner__cascades__integration Query 0 autocommit select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist where DB = "planner__cascades__integration" 769773ab56dd2977858c3c36e76e503323bbbc62257bb2e18e0e10cd9df43973 drop table if exists t; create table t(a int primary key, b int); insert into t values (1, 11), (4, 44), (2, 22), (3, 33); diff --git a/tests/integrationtest/t/ddl/sequence.test b/tests/integrationtest/t/ddl/sequence.test index 4055f5dc55ed7..d6f231456f8f1 100644 --- a/tests/integrationtest/t/ddl/sequence.test +++ b/tests/integrationtest/t/ddl/sequence.test @@ -353,6 +353,7 @@ connect (conn1, localhost, myuser,, ddl__sequence); -- error 1142 alter sequence my_seq increment = 2; connection default; +disconnect conn1; drop sequence if exists my_seq; # TestDdl_AlterSequenceIssue31265 diff --git a/tests/integrationtest/t/planner/cascades/integration.test b/tests/integrationtest/t/planner/cascades/integration.test index 5c3ca19d145da..ff7b05aef8db7 100644 --- a/tests/integrationtest/t/planner/cascades/integration.test +++ b/tests/integrationtest/t/planner/cascades/integration.test @@ -234,8 +234,8 @@ select a from t1 where exists(select 1 from t2 where t1.a = t2.a); # TestMemTableScan set session tidb_enable_cascades_planner = 1; -explain select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist; -select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist; +explain select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist where DB = "planner__cascades__integration"; +select USER, DB, COMMAND, TIME, STATE, INFO, `DIGEST` from information_schema.processlist where DB = "planner__cascades__integration"; # TestTopN drop table if exists t; From 771b32876cd446d5d0783df3fd2df3b458f6ac0a Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Thu, 12 Oct 2023 13:40:26 +0800 Subject: [PATCH 5/6] update table_modify --- ddl/table_modify_test.go | 87 ------------------- .../integrationtest/r/ddl/table_modify.result | 86 ++++++++++++++++++ tests/integrationtest/t/ddl/table_modify.test | 72 +++++++++++++++ 3 files changed, 158 insertions(+), 87 deletions(-) create mode 100644 tests/integrationtest/r/ddl/table_modify.result create mode 100644 tests/integrationtest/t/ddl/table_modify.test diff --git a/ddl/table_modify_test.go b/ddl/table_modify_test.go index 5203078629d79..b7781a5fef4a1 100644 --- a/ddl/table_modify_test.go +++ b/ddl/table_modify_test.go @@ -22,7 +22,6 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/ddl/util/callback" "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/model" @@ -36,84 +35,6 @@ import ( const tableModifyLease = 600 * time.Millisecond -func TestCreateTable(t *testing.T) { - store := testkit.CreateMockStoreWithSchemaLease(t, tableModifyLease) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 );") - tk.MustExec("CREATE TABLE IF NOT EXISTS `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 );") - is := domain.GetDomain(tk.Session()).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) - require.NoError(t, err) - - cols := tbl.Cols() - require.Len(t, cols, 1) - col := cols[0] - require.Equal(t, "a", col.Name.L) - d, ok := col.DefaultValue.(string) - require.True(t, ok) - require.Equal(t, "2", d) - - tk.MustExec("drop table t") - tk.MustGetErrCode("CREATE TABLE `t` (`a` int) DEFAULT CHARSET=abcdefg", errno.ErrUnknownCharacterSet) - - tk.MustExec("CREATE TABLE `collateTest` (`a` int, `b` varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci") - expects := "collateTest CREATE TABLE `collateTest` (\n `a` int(11) DEFAULT NULL,\n `b` varchar(10) COLLATE utf8_general_ci DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci" - tk.MustQuery("show create table collateTest").Check(testkit.Rows(expects)) - - tk.MustGetErrCode("CREATE TABLE `collateTest2` (`a` int) CHARSET utf8 COLLATE utf8mb4_unicode_ci", errno.ErrCollationCharsetMismatch) - tk.MustGetErrCode("CREATE TABLE `collateTest3` (`a` int) COLLATE utf8mb4_unicode_ci CHARSET utf8", errno.ErrConflictingDeclarations) - - tk.MustExec("CREATE TABLE `collateTest4` (`a` int) COLLATE utf8_uniCOde_ci") - expects = "collateTest4 CREATE TABLE `collateTest4` (\n `a` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" - tk.MustQuery("show create table collateTest4").Check(testkit.Rows(expects)) - - tk.MustExec("create database test2 default charset utf8 collate utf8_general_ci") - tk.MustExec("use test2") - tk.MustExec("create table dbCollateTest (a varchar(10))") - expects = "dbCollateTest CREATE TABLE `dbCollateTest` (\n `a` varchar(10) COLLATE utf8_general_ci DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci" - tk.MustQuery("show create table dbCollateTest").Check(testkit.Rows(expects)) - - // test for enum column - tk.MustExec("use test") - tk.MustGetErrCode("create table t_enum (a enum('e','e'));", errno.ErrDuplicatedValueInType) - - tk = testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustGetErrCode("create table t_enum (a enum('e','E')) charset=utf8 collate=utf8_general_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a enum('abc','Abc')) charset=utf8 collate=utf8_general_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a enum('e','E')) charset=utf8 collate=utf8_unicode_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a enum('ss','ß')) charset=utf8 collate=utf8_unicode_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a enum('æ','ae')) charset=utf8mb4 collate=utf8mb4_0900_ai_ci;", errno.ErrDuplicatedValueInType) - // test for set column - tk.MustGetErrCode("create table t_enum (a set('e','e'));", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a set('e','E')) charset=utf8 collate=utf8_general_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a set('abc','Abc')) charset=utf8 collate=utf8_general_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrMsg("create table t_enum (a enum('B','b')) charset=utf8 collate=utf8_general_ci;", "[types:1291]Column 'a' has duplicated value 'b' in ENUM") - tk.MustGetErrCode("create table t_enum (a set('e','E')) charset=utf8 collate=utf8_unicode_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrCode("create table t_enum (a set('ss','ß')) charset=utf8 collate=utf8_unicode_ci;", errno.ErrDuplicatedValueInType) - tk.MustGetErrMsg("create table t_enum (a enum('ss','ß')) charset=utf8 collate=utf8_unicode_ci;", "[types:1291]Column 'a' has duplicated value 'ß' in ENUM") - tk.MustGetErrCode("create table t_enum (a set('æ','ae')) charset=utf8mb4 collate=utf8mb4_0900_ai_ci;", errno.ErrDuplicatedValueInType) - - // test for table option "union" not supported - tk.MustExec("use test") - tk.MustExec("CREATE TABLE x (a INT) ENGINE = MyISAM;") - tk.MustExec("CREATE TABLE y (a INT) ENGINE = MyISAM;") - tk.MustGetErrCode("CREATE TABLE z (a INT) ENGINE = MERGE UNION = (x, y);", errno.ErrTableOptionUnionUnsupported) - tk.MustGetErrCode("ALTER TABLE x UNION = (y);", errno.ErrTableOptionUnionUnsupported) - tk.MustExec("drop table x;") - tk.MustExec("drop table y;") - - // test for table option "insert method" not supported - tk.MustExec("use test") - tk.MustExec("CREATE TABLE x (a INT) ENGINE = MyISAM;") - tk.MustExec("CREATE TABLE y (a INT) ENGINE = MyISAM;") - tk.MustGetErrCode("CREATE TABLE z (a INT) ENGINE = MERGE INSERT_METHOD=LAST;", errno.ErrTableOptionInsertMethodUnsupported) - tk.MustGetErrCode("ALTER TABLE x INSERT_METHOD=LAST;", errno.ErrTableOptionInsertMethodUnsupported) - tk.MustExec("drop table x;") - tk.MustExec("drop table y;") -} - func TestLockTableReadOnly(t *testing.T) { store := testkit.CreateMockStoreWithSchemaLease(t, tableModifyLease) tk1 := testkit.NewTestKit(t, store) @@ -266,11 +187,3 @@ func testParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain, sql wg.Wait() f(t, err1, err2) } - -func TestUnsupportedAlterTableOption(t *testing.T) { - store := testkit.CreateMockStoreWithSchemaLease(t, tableModifyLease) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t(a char(10) not null,b char(20)) shard_row_id_bits=6") - tk.MustGetErrCode("alter table t pre_split_regions=6", errno.ErrUnsupportedDDLOperation) -} diff --git a/tests/integrationtest/r/ddl/table_modify.result b/tests/integrationtest/r/ddl/table_modify.result new file mode 100644 index 0000000000000..334215de3b24c --- /dev/null +++ b/tests/integrationtest/r/ddl/table_modify.result @@ -0,0 +1,86 @@ +drop table if exists t; +CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 ); +CREATE TABLE IF NOT EXISTS `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 ); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` double DEFAULT '2' +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table t; +CREATE TABLE `t` (`a` int) DEFAULT CHARSET=abcdefg; +[parser:1115]Unknown character set: 'abcdefg' +CREATE TABLE `collateTest` (`a` int, `b` varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +show create table collateTest; +Table Create Table +collateTest CREATE TABLE `collateTest` ( + `a` int(11) DEFAULT NULL, + `b` varchar(10) COLLATE utf8_general_ci DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci +CREATE TABLE `collateTest2` (`a` int) CHARSET utf8 COLLATE utf8mb4_unicode_ci; +Error 1253 (42000): COLLATION 'utf8mb4_unicode_ci' is not valid for CHARACTER SET 'utf8' +CREATE TABLE `collateTest3` (`a` int) COLLATE utf8mb4_unicode_ci CHARSET utf8; +Error 1302 (HY000): Conflicting declarations: 'CHARACTER SET utf8mb4' and 'CHARACTER SET utf8' +CREATE TABLE `collateTest4` (`a` int) COLLATE utf8_uniCOde_ci; +show create table collateTest4; +Table Create Table +collateTest4 CREATE TABLE `collateTest4` ( + `a` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci +create database test2 default charset utf8 collate utf8_general_ci; +use test2; +create table dbCollateTest (a varchar(10)); +show create table dbCollateTest; +Table Create Table +dbCollateTest CREATE TABLE `dbCollateTest` ( + `a` varchar(10) COLLATE utf8_general_ci DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci +create table t_enum (a enum('e','e')); +Error 1291 (HY000): Column 'a' has duplicated value 'e' in ENUM +create table t_enum (a enum('e','E')) charset=utf7 collate=utf8_general_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('abc','Abc')) charset=utf7 collate=utf8_general_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('e','E')) charset=utf7 collate=utf8_unicode_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('æ','ae')) charset=utf7mb4 collate=utf8mb4_0900_ai_ci; +[parser:1115]Unknown character set: 'utf7mb4' +create table t_enum (a set('e','e')); +Error 1291 (HY000): Column 'a' has duplicated value 'e' in SET +create table t_enum (a set('e','E')) charset=utf7 collate=utf8_general_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a set('abc','Abc')) charset=utf7 collate=utf8_general_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('B','b')) charset=utf7 collate=utf8_general_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a set('e','E')) charset=utf7 collate=utf8_unicode_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a set('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a enum('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +[parser:1115]Unknown character set: 'utf7' +create table t_enum (a set('æ','ae')) charset=utf7mb4 collate=utf8mb4_0900_ai_ci; +[parser:1115]Unknown character set: 'utf7mb4' +CREATE TABLE x (a INT) ENGINE = MyISAM; +CREATE TABLE y (a INT) ENGINE = MyISAM; +CREATE TABLE z (a INT) ENGINE = MERGE UNION = (x, y); +Error 8232 (HY000): CREATE/ALTER table with union option is not supported +ALTER TABLE x UNION = (y); +Error 8232 (HY000): CREATE/ALTER table with union option is not supported +drop table x; +drop table y; +CREATE TABLE x (a INT) ENGINE = MyISAM; +CREATE TABLE y (a INT) ENGINE = MyISAM; +CREATE TABLE z (a INT) ENGINE = MERGE INSERT_METHOD=LAST; +Error 8233 (HY000): CREATE/ALTER table with insert method option is not supported +ALTER TABLE x INSERT_METHOD=LAST; +Error 8233 (HY000): CREATE/ALTER table with insert method option is not supported +drop table x; +drop table y; +drop database test2; +use ddl__table_modify; +drop table if exists t; +create table t(a char(10) not null,b char(20)) shard_row_id_bits=6; +alter table t pre_split_regions=6; +Error 8200 (HY000): This type of ALTER TABLE is currently unsupported diff --git a/tests/integrationtest/t/ddl/table_modify.test b/tests/integrationtest/t/ddl/table_modify.test new file mode 100644 index 0000000000000..3426116ceb2b5 --- /dev/null +++ b/tests/integrationtest/t/ddl/table_modify.test @@ -0,0 +1,72 @@ +# TestCreateTable +drop table if exists t; +CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 ); +CREATE TABLE IF NOT EXISTS `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 ); +show create table t; +drop table t; +-- error 1115 +CREATE TABLE `t` (`a` int) DEFAULT CHARSET=abcdefg; +CREATE TABLE `collateTest` (`a` int, `b` varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; +show create table collateTest; +-- error 1253 +CREATE TABLE `collateTest2` (`a` int) CHARSET utf8 COLLATE utf8mb4_unicode_ci; +-- error 1302 +CREATE TABLE `collateTest3` (`a` int) COLLATE utf8mb4_unicode_ci CHARSET utf8; +CREATE TABLE `collateTest4` (`a` int) COLLATE utf8_uniCOde_ci; +show create table collateTest4; +create database test2 default charset utf8 collate utf8_general_ci; +use test2; +create table dbCollateTest (a varchar(10)); +show create table dbCollateTest; +-- error 1291 +create table t_enum (a enum('e','e')); +-- error 1290 +create table t_enum (a enum('e','E')) charset=utf7 collate=utf8_general_ci; +-- error 1290 +create table t_enum (a enum('abc','Abc')) charset=utf7 collate=utf8_general_ci; +-- error 1290 +create table t_enum (a enum('e','E')) charset=utf7 collate=utf8_unicode_ci; +-- error 1290 +create table t_enum (a enum('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +-- error 1290 +create table t_enum (a enum('æ','ae')) charset=utf7mb4 collate=utf8mb4_0900_ai_ci; +-- error 1290 +create table t_enum (a set('e','e')); +-- error 1290 +create table t_enum (a set('e','E')) charset=utf7 collate=utf8_general_ci; +-- error 1290 +create table t_enum (a set('abc','Abc')) charset=utf7 collate=utf8_general_ci; +-- error 1290 +create table t_enum (a enum('B','b')) charset=utf7 collate=utf8_general_ci; +-- error 1290 +create table t_enum (a set('e','E')) charset=utf7 collate=utf8_unicode_ci; +-- error 1290 +create table t_enum (a set('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +-- error 1290 +create table t_enum (a enum('ss','ß')) charset=utf7 collate=utf8_unicode_ci; +-- error 1290 +create table t_enum (a set('æ','ae')) charset=utf7mb4 collate=utf8mb4_0900_ai_ci; +CREATE TABLE x (a INT) ENGINE = MyISAM; +CREATE TABLE y (a INT) ENGINE = MyISAM; +-- error 8231 +CREATE TABLE z (a INT) ENGINE = MERGE UNION = (x, y); +-- error 8231 +ALTER TABLE x UNION = (y); +drop table x; +drop table y; +CREATE TABLE x (a INT) ENGINE = MyISAM; +CREATE TABLE y (a INT) ENGINE = MyISAM; +-- error 8232 +CREATE TABLE z (a INT) ENGINE = MERGE INSERT_METHOD=LAST; +-- error 8232 +ALTER TABLE x INSERT_METHOD=LAST; +drop table x; +drop table y; +drop database test2; +use ddl__table_modify; + +# TestUnsupportedAlterTableOption +drop table if exists t; +create table t(a char(10) not null,b char(20)) shard_row_id_bits=6; +-- error 8200 +alter table t pre_split_regions=6; From 10947e1779d2f9cf143842c36dd415213dbcb5e4 Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Thu, 12 Oct 2023 14:19:48 +0800 Subject: [PATCH 6/6] update fail_db --- ddl/tests/fail/BUILD.bazel | 2 +- ddl/tests/fail/fail_db_test.go | 29 ---------------------- tests/integrationtest/r/ddl/fail_db.result | 16 ++++++++++++ tests/integrationtest/t/ddl/fail_db.test | 14 +++++++++++ 4 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 tests/integrationtest/r/ddl/fail_db.result create mode 100644 tests/integrationtest/t/ddl/fail_db.test diff --git a/ddl/tests/fail/BUILD.bazel b/ddl/tests/fail/BUILD.bazel index 48258d48b7179..d5cfbf757e9aa 100644 --- a/ddl/tests/fail/BUILD.bazel +++ b/ddl/tests/fail/BUILD.bazel @@ -9,7 +9,7 @@ go_test( ], flaky = True, race = "on", - shard_count = 13, + shard_count = 12, deps = [ "//config", "//ddl", diff --git a/ddl/tests/fail/fail_db_test.go b/ddl/tests/fail/fail_db_test.go index 3fdcda0dddc75..d9b32487e754e 100644 --- a/ddl/tests/fail/fail_db_test.go +++ b/ddl/tests/fail/fail_db_test.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "math/rand" - "strings" "sync/atomic" "testing" "time" @@ -537,34 +536,6 @@ func TestModifyColumn(t *testing.T) { tk.MustExec("drop table t, t1, t2, t3, t4, t5") } -func TestIssue38699(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - //Test multi records - tk.MustExec("USE test") - tk.MustExec("set sql_mode=''") - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t (a int)") - tk.MustExec("insert into t values (1000000000), (2000000)") - tk.MustExec("alter table t modify a tinyint") - result := tk.MustQuery("show warnings") - require.Len(t, result.Rows(), 1) - result.CheckWithFunc(testkit.Rows("Warning 1690 2 warnings with this error code"), func(actual []string, expected []interface{}) bool { - //Check if it starts with x warning(s) - return strings.EqualFold(actual[0], expected[0].(string)) && strings.EqualFold(actual[1], expected[1].(string)) && strings.HasPrefix(actual[2], expected[2].(string)) - }) - - //Test single record - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t (a int)") - tk.MustExec("insert into t values (1000000000)") - tk.MustExec("alter table t modify a tinyint") - result = tk.MustQuery("show warnings") - require.Len(t, result.Rows(), 1) - result.Check(testkit.Rows("Warning 1690 constant 1000000000 overflows tinyint")) -} - func TestPartitionAddPanic(t *testing.T) { s := createFailDBSuite(t) tk := testkit.NewTestKit(t, s.store) diff --git a/tests/integrationtest/r/ddl/fail_db.result b/tests/integrationtest/r/ddl/fail_db.result new file mode 100644 index 0000000000000..068ec32b6e47a --- /dev/null +++ b/tests/integrationtest/r/ddl/fail_db.result @@ -0,0 +1,16 @@ +set sql_mode=''; +DROP TABLE IF EXISTS t; +CREATE TABLE t (a int); +insert into t values (1000000000), (2000000); +alter table t modify a tinyint; +show warnings; +Level Code Message +Warning 1690 2 warnings with this error code, first warning: constant 1000000000 overflows tinyint +DROP TABLE IF EXISTS t; +CREATE TABLE t (a int); +insert into t values (1000000000); +alter table t modify a tinyint; +show warnings; +Level Code Message +Warning 1690 constant 1000000000 overflows tinyint +set sql_mode=default; diff --git a/tests/integrationtest/t/ddl/fail_db.test b/tests/integrationtest/t/ddl/fail_db.test new file mode 100644 index 0000000000000..e15663ca4c759 --- /dev/null +++ b/tests/integrationtest/t/ddl/fail_db.test @@ -0,0 +1,14 @@ +# TestIssue38699 +set sql_mode=''; +DROP TABLE IF EXISTS t; +CREATE TABLE t (a int); +insert into t values (1000000000), (2000000); +alter table t modify a tinyint; +show warnings; +DROP TABLE IF EXISTS t; +CREATE TABLE t (a int); +insert into t values (1000000000); +alter table t modify a tinyint; +show warnings; +set sql_mode=default; +