diff --git a/ddl/tests/partition/BUILD.bazel b/ddl/tests/partition/BUILD.bazel index b0ee39b9898ad..8116f6070ae85 100644 --- a/ddl/tests/partition/BUILD.bazel +++ b/ddl/tests/partition/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 50, + shard_count = 47, deps = [ "//config", "//ddl", diff --git a/ddl/tests/partition/db_partition_test.go b/ddl/tests/partition/db_partition_test.go index 914467fc809d5..4bb911bca79d3 100644 --- a/ddl/tests/partition/db_partition_test.go +++ b/ddl/tests/partition/db_partition_test.go @@ -762,135 +762,6 @@ create table log_message_1 ( " PARTITION `p2` VALUES LESS THAN ('20:20:10','20:20:10'))")) } -func TestPartitionRangeColumnsCollate(t *testing.T) { - failpoint.Enable("github.com/pingcap/tidb/planner/core/forceDynamicPrune", `return(true)`) - defer failpoint.Disable("github.com/pingcap/tidb/planner/core/forceDynamicPrune") - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create schema PartitionRangeColumnsCollate") - tk.MustExec("use PartitionRangeColumnsCollate") - tk.MustExec(`create table t (a varchar(255) charset utf8mb4 collate utf8mb4_bin) partition by range columns (a) - (partition p0A values less than ("A"), - partition p1AA values less than ("AA"), - partition p2Aa values less than ("Aa"), - partition p3BB values less than ("BB"), - partition p4Bb values less than ("Bb"), - partition p5aA values less than ("aA"), - partition p6aa values less than ("aa"), - partition p7bB values less than ("bB"), - partition p8bb values less than ("bb"), - partition pMax values less than (MAXVALUE))`) - tk.MustExec(`insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba")`) - tk.MustQuery(`explain select * from t where a = "AA" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "AA")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "AA" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows("AA", "Aa", "aA", "aa")) - tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows("AA", "Aa", "aA", "aa")) - tk.MustQuery(`explain select * from t where a >= "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] ge(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a >= "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows( - "AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "aA", "aB", "aa", "ab", "b", "bA", "bB", "ba", "bb")) - tk.MustQuery(`explain select * from t where a > "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] gt(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a > "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows( - "AB", "Ab", "B", "BA", "BB", "Ba", "Bb", "aB", "ab", "b", "bA", "bB", "ba", "bb")) - tk.MustQuery(`explain select * from t where a <= "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] le(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a <= "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows( - "A", "AA", "Aa", "a", "aA", "aa")) - tk.MustQuery(`explain select * from t where a < "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] lt(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a < "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows( - "A", "a")) - - tk.MustExec("drop table t") - tk.MustExec(` create table t (a varchar(255) charset utf8mb4 collate utf8mb4_general_ci) partition by range columns (a) -(partition p0 values less than ("A"), - partition p1 values less than ("aa"), - partition p2 values less than ("AAA"), - partition p3 values less than ("aaaa"), - partition p4 values less than ("B"), - partition p5 values less than ("bb"), - partition pMax values less than (MAXVALUE))`) - tk.MustExec(`insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"),("ä"),("ÄÄÄ")`) - tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_general_ci`).Check(testkit.Rows( - `TableReader_7 10.00 root partition:p2 data:Selection_6`, - `└─Selection_6 10.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_general_ci`).Sort().Check(testkit.Rows( - "AA", "Aa", "aA", "aa")) - tk.MustQuery(`explain select * from t where a = "aa" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:p2 data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "aa")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "aa" collate utf8mb4_bin`).Sort().Check(testkit.Rows("aa")) - // 'a' < 'b' < 'ä' in _bin - tk.MustQuery(`explain select * from t where a = "ä" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:p1 data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "ä")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "ä" collate utf8mb4_bin`).Sort().Check(testkit.Rows("ä")) - tk.MustQuery(`explain select * from t where a = "b" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:p5 data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] eq(partitionrangecolumnscollate.t.a, "b")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a = "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("b")) - tk.MustQuery(`explain select * from t where a <= "b" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] le(partitionrangecolumnscollate.t.a, "b")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a <= "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("A", "AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "a", "aA", "aB", "aa", "ab", "b")) - tk.MustQuery(`explain select * from t where a < "b" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] lt(partitionrangecolumnscollate.t.a, "b")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - // Missing upper case B if not p5 is included! - tk.MustQuery(`select * from t where a < "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("A", "AA", "AB", "Aa", "Ab", "B", "BA", "BB", "Ba", "Bb", "a", "aA", "aB", "aa", "ab")) - tk.MustQuery(`explain select * from t where a >= "b" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] ge(partitionrangecolumnscollate.t.a, "b")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a >= "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("b", "bA", "bB", "ba", "bb", "ÄÄÄ", "ä")) - tk.MustQuery(`explain select * from t where a > "b" collate utf8mb4_bin`).Check(testkit.Rows( - `TableReader_7 8000.00 root partition:all data:Selection_6`, - `└─Selection_6 8000.00 cop[tikv] gt(partitionrangecolumnscollate.t.a, "b")`, - ` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`)) - tk.MustQuery(`select * from t where a > "b" collate utf8mb4_bin`).Sort().Check(testkit.Rows("bA", "bB", "ba", "bb", "ÄÄÄ", "ä")) -} - -func TestDisableTablePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - for _, v := range []string{"'AUTO'", "'OFF'", "0", "'ON'"} { - tk.MustExec("set @@session.tidb_enable_table_partition = " + v) - tk.MustExec("set @@session.tidb_enable_list_partition = OFF") - tk.MustExec("drop table if exists t") - tk.MustExec(`create table t (id int) partition by list (id) ( - partition p0 values in (1,2),partition p1 values in (3,4));`) - tbl := external.GetTableByName(t, tk, "test", "t") - require.Nil(t, tbl.Meta().Partition) - _, err := tk.Exec(`alter table t add partition ( - partition p4 values in (7), - partition p5 values in (8,9));`) - require.True(t, dbterror.ErrPartitionMgmtOnNonpartitioned.Equal(err)) - tk.MustExec("insert into t values (1),(3),(5),(100),(null)") - } -} - func generatePartitionTableByNum(num int) string { buf := bytes.NewBuffer(make([]byte, 0, 1024*1024)) buf.WriteString("create table gen_t (id int) partition by list (id) (") @@ -1277,589 +1148,6 @@ func TestCreateTableWithListColumnsPartition(t *testing.T) { } } -func TestAlterTableAddPartitionByList(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustContainErrMsg(`create table t (a int) partition by list (a) (partition p1 values in (null, 6), partition p2 values in (7,null))`, "[ddl:1495]Multiple definition of same constant in list partitioning") - tk.MustExec(`create table t (id int) partition by list (id) ( - partition p0 values in (1,2), - partition p1 values in (3,4), - partition p3 values in (5,null) - );`) - tk.MustExec(`alter table t add partition ( - partition p4 values in (7), - partition p5 values in (8,9));`) - - tk.MustContainErrMsg(`alter table t add partition (partition p6 values in (null, 6))`, - "[ddl:1495]Multiple definition of same constant in list partitioning") - tk.MustExec(`alter table t add partition (partition pDef values in (default, 6))`) - tk.MustContainErrMsg(`alter table t add partition (partition pDef2 values in (10, default))`, `[ddl:8200]Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead`) - tk.MustContainErrMsg(`alter table t add partition (partition pDef2 values in (10))`, `[ddl:8200]Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead`) - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().Partition) - part := tbl.Meta().Partition - require.True(t, part.Type == model.PartitionTypeList) - - require.Equal(t, "`id`", part.Expr) - require.Len(t, part.Definitions, 6) - require.Equal(t, [][]string{{"1"}, {"2"}}, part.Definitions[0].InValues) - require.Equal(t, model.NewCIStr("p0"), part.Definitions[0].Name) - require.Equal(t, [][]string{{"3"}, {"4"}}, part.Definitions[1].InValues) - require.Equal(t, model.NewCIStr("p1"), part.Definitions[1].Name) - require.Equal(t, [][]string{{"5"}, {"NULL"}}, part.Definitions[2].InValues) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[2].Name) - require.Equal(t, [][]string{{"7"}}, part.Definitions[3].InValues) - require.Equal(t, model.NewCIStr("p4"), part.Definitions[3].Name) - require.Equal(t, [][]string{{"8"}, {"9"}}, part.Definitions[4].InValues) - require.Equal(t, model.NewCIStr("p5"), part.Definitions[4].Name) - require.Equal(t, [][]string{{"DEFAULT"}, {"6"}}, part.Definitions[5].InValues) - require.Equal(t, model.NewCIStr("pDef"), part.Definitions[5].Name) - - tk.MustQuery(`select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='test'`).Sort().Check(testkit.Rows(""+ - "p0 1 LIST `id` 1,2 ", - "p1 2 LIST `id` 3,4 ", - "p3 3 LIST `id` 5,NULL ", - "p4 4 LIST `id` 7 ", - "p5 5 LIST `id` 8,9 ", - "pDef 6 LIST `id` DEFAULT,6 ")) - - tk.MustExec(`alter table t reorganize partition pDef into (partition p_6 values in (6))`) - errorCases := []struct { - sql string - err *terror.Error - }{ - {"alter table t add partition (partition p4 values in (7))", - dbterror.ErrSameNamePartition, - }, - {"alter table t add partition (partition p6 values less than (7))", - ast.ErrPartitionWrongValues, - }, - {"alter table t add partition (partition p6 values in (null))", - dbterror.ErrMultipleDefConstInListPart, - }, - {"alter table t add partition (partition p6 values in (7))", - dbterror.ErrMultipleDefConstInListPart, - }, - {"alter table t add partition (partition p6 values in ('a'))", - dbterror.ErrValuesIsNotIntType, - }, - {"alter table t add partition (partition p5 values in (10),partition p6 values in (7))", - dbterror.ErrSameNamePartition, - }, - } - - for i, tt := range errorCases { - _, err := tk.Exec(tt.sql) - require.Truef(t, tt.err.Equal(err), - "case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`", - i, tt.sql, tt.err, err, - ) - } - - errorCases2 := []struct { - create string - alter string - err *terror.Error - }{ - { - "create table t (a bigint unsigned) partition by list columns (a) (partition p0 values in (1));", - "alter table t add partition (partition p1 values in (-1))", - dbterror.ErrWrongTypeColumnValue, - }, - { - "create table t (a varchar(2)) partition by list columns (a) (partition p0 values in ('a','b'));", - "alter table t add partition (partition p1 values in ('abc'))", - dbterror.ErrWrongTypeColumnValue, - }, - { - "create table t (a tinyint) partition by list columns (a) (partition p0 values in (1,2,3));", - "alter table t add partition (partition p1 values in (65536))", - dbterror.ErrWrongTypeColumnValue, - }, - { - "create table t (a bigint) partition by list columns (a) (partition p0 values in (1,2,3));", - "alter table t add partition (partition p1 values in (18446744073709551615))", - dbterror.ErrWrongTypeColumnValue, - }, - { - "create table t (a char) partition by list columns (a) (partition p0 values in ('a','b'));", - "alter table t add partition (partition p1 values in ('abc'))", - dbterror.ErrWrongTypeColumnValue, - }, - { - "create table t (a datetime) partition by list columns (a) (partition p0 values in ('2020-11-30 12:00:00'));", - "alter table t add partition (partition p1 values in ('2020-11-31 12:00:00'))", - dbterror.ErrWrongTypeColumnValue, - }, - } - - for i, tt := range errorCases2 { - tk.MustExec("drop table if exists t;") - tk.MustExec(tt.create) - _, err := tk.Exec(tt.alter) - require.Truef(t, tt.err.Equal(err), - "case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`", - i, tt.alter, tt.err, err, - ) - } -} - -func TestAlterTableAddPartitionByListColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec(`create table t (id int, name varchar(10)) partition by list columns (id,name) ( - partition p0 values in ((1,'a'),(2,'b')), - partition p1 values in ((3,'a'),(4,'b')), - partition p3 values in ((5,null)) - )`) - tk.MustGetErrCode(`create table t (a int) partition by list (a) (partition p0 values in (default), partition p1 values in (maxvalue))`, - errno.ErrParse) - tk.MustExec(`alter table t add partition ( - partition p4 values in ((7,'a')), - partition p5 values in ((8,'a')));`) - // We only support a single DEFAULT (catch-all), - // Not a DEFAULT per column in LIST COLUMNS! - tk.MustGetErrMsg(`alter table t add partition ( - partition pDef values in (10, default))`, `[ddl:1653]Inconsistency in usage of column lists for partitioning`) - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((10, default)))`, errno.ErrParse) - tk.MustGetErrMsg(`alter table t add partition ( - partition pDef values in (default, 10))`, `[ddl:1653]Inconsistency in usage of column lists for partitioning`) - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((default, 10)))`, errno.ErrParse) - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((9,'a'), (default, 10, 'q'))`, errno.ErrParse) - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((9,'a'), (10, default, 'q'))`, errno.ErrParse) - tk.MustExec(`alter table t add partition ( - partition pDef values in (default))`) - tk.MustGetErrMsg(`alter table t add partition ( - partition pDef2 values in (default))`, - "[ddl:8200]Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead") - tk.MustGetErrMsg("alter table t add partition (partition p6 values in ((9,'d')))", - "[ddl:8200]Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead") - tk.MustGetErrMsg("alter table t add partition (partition p6 values in (default))", - "[ddl:8200]Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead") - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition (partition pDef default)`) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition ( - partition pDef values in ((9, 'c'), default))`) - // DEFAULT cannot be within parentheses (i.e. like a list). - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((9, 'c'), (default)))`, errno.ErrParse) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition ( - partition pDef values in (default, (9,'c')))`) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition ( - partition pDef values in ((9,'d'), default))`) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition ( - partition pDef values in (default, (9,'c'), (10, 'd')))`) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustExec(`alter table t add partition ( - partition pDef values in ((9,'d'), default, (10, 'd')))`) - // After a default value is included in a partition, - // it can no longer support ADD PARTITION, but should do REORGANIZE instead. - tk.MustGetErrCode(`alter table t add partition ( - partition pDef values in ((9,'a'), (10, default, 'q'))`, errno.ErrParse) - - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL,\n" + - " `name` varchar(10) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`id`,`name`)\n" + - "(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')),\n" + - " PARTITION `p1` VALUES IN ((3,'a'),(4,'b')),\n" + - " PARTITION `p3` VALUES IN ((5,NULL)),\n" + - " PARTITION `p4` VALUES IN ((7,'a')),\n" + - " PARTITION `p5` VALUES IN ((8,'a')),\n" + - " PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d')))")) - tk.MustQuery(`select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='test'`).Sort().Check(testkit.Rows(""+ - "p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') ", - "p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') ", - "p3 3 LIST COLUMNS `id`,`name` (5,NULL) ", - "p4 4 LIST COLUMNS `id`,`name` (7,'a') ", - "p5 5 LIST COLUMNS `id`,`name` (8,'a') ", - "pDef 6 LIST COLUMNS `id`,`name` (9,'d'),DEFAULT,(10,'d') ")) - - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().Partition) - part := tbl.Meta().Partition - require.True(t, part.Type == model.PartitionTypeList) - - require.Equal(t, "", part.Expr) - require.Equal(t, "id", part.Columns[0].O) - require.Equal(t, "name", part.Columns[1].O) - require.Len(t, part.Definitions, 6) - require.Equal(t, [][]string{{"1", `'a'`}, {"2", `'b'`}}, part.Definitions[0].InValues) - require.Equal(t, model.NewCIStr("p0"), part.Definitions[0].Name) - require.Equal(t, [][]string{{"3", `'a'`}, {"4", `'b'`}}, part.Definitions[1].InValues) - require.Equal(t, model.NewCIStr("p1"), part.Definitions[1].Name) - require.Equal(t, [][]string{{"5", `NULL`}}, part.Definitions[2].InValues) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[2].Name) - require.Equal(t, [][]string{{"7", `'a'`}}, part.Definitions[3].InValues) - require.Equal(t, model.NewCIStr("p4"), part.Definitions[3].Name) - require.Equal(t, [][]string{{"8", `'a'`}}, part.Definitions[4].InValues) - require.Equal(t, model.NewCIStr("p5"), part.Definitions[4].Name) - require.Equal(t, [][]string{{"9", `'d'`}, {`DEFAULT`}, {`10`, `'d'`}}, part.Definitions[5].InValues) - require.Equal(t, model.NewCIStr("pDef"), part.Definitions[5].Name) - - tk.MustExec(`alter table t reorganize partition pDef into (partition pd VALUES IN ((9,'d'),(10,'d')))`) - errorCases := []struct { - sql string - err *terror.Error - }{ - {"alter table t add partition (partition p4 values in ((7,'b')))", - dbterror.ErrSameNamePartition, - }, - {"alter table t add partition (partition p6 values less than ((7,'a')))", - ast.ErrPartitionWrongValues, - }, - {"alter table t add partition (partition p6 values in ((5,null)))", - dbterror.ErrMultipleDefConstInListPart, - }, - {"alter table t add partition (partition p6 values in (('a','a')))", - dbterror.ErrWrongTypeColumnValue, - }, - } - - for i, tt := range errorCases { - _, err := tk.Exec(tt.sql) - require.Truef(t, tt.err.Equal(err), - "case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`", - i, tt.sql, tt.err, err, - ) - } - tk.MustExec(`drop table t`) - tk.MustExec( - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL,\n" + - " `name` varchar(10) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`id`,`name`)\n" + - "(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')),\n" + - " PARTITION `p1` VALUES IN ((3,'a'),(4,'b')),\n" + - " PARTITION `p3` VALUES IN ((5,NULL)),\n" + - " PARTITION `p4` VALUES IN ((7,'a')),\n" + - " PARTITION `p5` VALUES IN ((8,'a')),\n" + - " PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d')))") - tk.MustQuery(`select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='test'`).Sort().Check(testkit.Rows(""+ - "p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') ", - "p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') ", - "p3 3 LIST COLUMNS `id`,`name` (5,NULL) ", - "p4 4 LIST COLUMNS `id`,`name` (7,'a') ", - "p5 5 LIST COLUMNS `id`,`name` (8,'a') ", - "pDef 6 LIST COLUMNS `id`,`name` (9,'d'),DEFAULT,(10,'d') ")) - tk.MustExec("set @@session.tidb_enable_list_partition = OFF") - tk.MustExec(`alter table t drop partition pDef`) - tk.MustContainErrMsg(`alter table t add partition (partition pDef)`, - "[ddl:1479]Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition") - tk.MustExec(`alter table t add partition (partition pDef default)`) - tk.MustQuery(`select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='test'`).Sort().Check(testkit.Rows(""+ - "p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') ", - "p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') ", - "p3 3 LIST COLUMNS `id`,`name` (5,NULL) ", - "p4 4 LIST COLUMNS `id`,`name` (7,'a') ", - "p5 5 LIST COLUMNS `id`,`name` (8,'a') ", - "pDef 6 LIST COLUMNS `id`,`name` DEFAULT ")) - tk.MustExec(`alter table t reorganize partition pDef into (partition pDef VALUES IN (DEFAULT,(9,'c')))`) - tk.MustQuery(`select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='test'`).Sort().Check(testkit.Rows(""+ - "p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') ", - "p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') ", - "p3 3 LIST COLUMNS `id`,`name` (5,NULL) ", - "p4 4 LIST COLUMNS `id`,`name` (7,'a') ", - "p5 5 LIST COLUMNS `id`,`name` (8,'a') ", - "pDef 6 LIST COLUMNS `id`,`name` DEFAULT,(9,'c') ")) -} - -func TestDefaultListPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database defaultPartition") - tk.MustExec("use defaultPartition") - - tk.MustExec(`create table t (a int, b varchar(255), unique key (a), key (b)) partition by list (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10))`) - tk.MustExec(`insert into t values (1, "1"), (2, "2"), (3,'3'), (null, "null"), (4, "4"), (11, "11")`) - tk.MustExec(`analyze table t`) - - tk.MustQuery(`select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'defaultPartition'`).Sort().Check(testkit.Rows(""+ - "p0 LIST `a` 0,4 1", - "p1 LIST `a` 1,NULL,DEFAULT 4", - "p2 LIST `a` 2,7,10 1")) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " UNIQUE KEY `a` (`a`),\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (`a`)\n" + - "(PARTITION `p0` VALUES IN (0,4),\n" + - " PARTITION `p1` VALUES IN (1,NULL,DEFAULT),\n" + - " PARTITION `p2` VALUES IN (2,7,10))")) - tk.MustExec(`set @@tidb_partition_prune_mode = 'dynamic'`) - tk.MustQuery(`select * from t where a = 4`).Check(testkit.Rows("4 4")) - tk.MustQuery(`select * from t where a is null`).Check(testkit.Rows(" null")) - tk.MustQuery(`select * from t where a = 3`).Check(testkit.Rows("3 3")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 4`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p0, index:a(a) ")) - tk.MustQuery(`explain format = 'brief' select * from t where a is null`).Check(testkit.Rows(""+ - "TableReader 1.00 root partition:p1 data:Selection", - "└─Selection 1.00 cop[tikv] isnull(defaultpartition.t.a)", - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 3`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p1, index:a(a) ")) - tk.MustExec(`set @@tidb_partition_prune_mode = 'static'`) - tk.MustQuery(`select * from t where a = 4`).Check(testkit.Rows("4 4")) - tk.MustQuery(`select * from t where a is null`).Check(testkit.Rows(" null")) - tk.MustQuery(`select * from t where a = 3`).Check(testkit.Rows("3 3")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 4`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p0, index:a(a) ")) - tk.MustQuery(`explain format = 'brief' select * from t where a is null`).Check(testkit.Rows(""+ - "TableReader 1.00 root data:Selection", - "└─Selection 1.00 cop[tikv] isnull(defaultpartition.t.a)", - " └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 3`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p1, index:a(a) ")) - - // TODO: check ranges with 1,2 and 3 matching values, including 'holes' matching DEFAULT - // DEFAULT partition will not be used in reorganize partition if not included in the source/destination - tk.MustExecToErr(`alter table t reorganize partition p0 into (partition p0 values in (0))`, "[table:1526]Table has no partition for value 4") - - tk.MustExec(`drop table t`) - tk.MustExec(`CREATE TABLE t (a VARCHAR(100),b INT) PARTITION BY LIST COLUMNS (a) (PARTITION p1 VALUES IN ('a', 'b', 'DEFAULT'),PARTITION pDef DEFAULT)`) -} - -func TestDefaultListColumnPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database defaultPartition") - tk.MustExec("use defaultPartition") - - tk.MustExec(`create table t (b int, a varchar(255)) partition by list columns (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10))`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `b` int(11) DEFAULT NULL,\n" + - " `a` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`a`)\n" + - "(PARTITION `p0` VALUES IN (0,4),\n" + - " PARTITION `p1` VALUES IN (1,NULL,DEFAULT),\n" + - " PARTITION `p2` VALUES IN (2,7,10))")) - tk.MustExec(`insert into t values (1, "1"), (2, "2"), (3,'3'), (null, null), (4, "4"), (11, "11")`) - tk.MustExec(`analyze table t`) - - tk.MustQuery(`select * from t partition(p0)`).Sort().Check(testkit.Rows("4 4")) - tk.MustQuery(`select * from t partition(p1)`).Sort().Check(testkit.Rows("1 1", "11 11", "3 3", " ")) - tk.MustQuery(`select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'defaultPartition'`).Sort().Check(testkit.Rows(""+ - "p0 LIST COLUMNS `a` 0,4 1", - "p1 LIST COLUMNS `a` 1,NULL,DEFAULT 4", - "p2 LIST COLUMNS `a` 2,7,10 1")) - tk.MustExec(`set @@tidb_partition_prune_mode = 'dynamic'`) - tk.MustQuery(`explain format = 'brief' select * from t where a is null`).Check(testkit.Rows(""+ - "TableReader 1.00 root partition:p1 data:Selection", - "└─Selection 1.00 cop[tikv] isnull(defaultpartition.t.a)", - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - tk.MustQuery(`select * from t where a is null`).Check(testkit.Rows(" ")) - tk.MustQuery(`select * from t where a = 4`).Check(testkit.Rows("4 4")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 4`).Check(testkit.Rows( - "TableReader 4.80 root partition:p0,p1,p2 data:Selection", - "└─Selection 4.80 cop[tikv] eq(cast(defaultpartition.t.a, double BINARY), 4)", - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - tk.MustQuery(`select * from t where a = 3`).Check(testkit.Rows("3 3")) - tk.MustQuery(`explain format = 'brief' select * from t where a = 3`).Check(testkit.Rows( - "TableReader 4.80 root partition:p0,p1,p2 data:Selection", - "└─Selection 4.80 cop[tikv] eq(cast(defaultpartition.t.a, double BINARY), 3)", - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - tk.MustQuery(`explain format = 'brief' select * from t where a = "3"`).Check(testkit.Rows( - "TableReader 1.00 root partition:p1 data:Selection", - `└─Selection 1.00 cop[tikv] eq(defaultpartition.t.a, "3")`, - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - tk.MustQuery(`select * from t where a = "3"`).Check(testkit.Rows("3 3")) - tk.MustQuery(`select * from t where a = "4"`).Check(testkit.Rows("4 4")) - tk.MustQuery(`explain format = 'brief' select * from t where a = "4"`).Check(testkit.Rows( - "TableReader 1.00 root partition:p0 data:Selection", - `└─Selection 1.00 cop[tikv] eq(defaultpartition.t.a, "4")`, - " └─TableFullScan 6.00 cop[tikv] table:t keep order:false")) - - // Notice in string context that "35" is between "3" and "4" - tk.MustQuery(`explain format = 'brief' select * from t where a between "3" and "4"`).Check(testkit.Rows(""+ - `TableReader 2.00 root partition:p0,p1 data:Selection`, - `└─Selection 2.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - ` └─TableFullScan 6.00 cop[tikv] table:t keep order:false`)) - tk.MustQuery(`select * from t where a between "3" and "4"`).Sort().Check(testkit.Rows("3 3", "4 4")) - tk.MustQuery(`explain format = 'brief' select * from t where a >= "3" and a <= "4"`).Check(testkit.Rows(""+ - `TableReader 2.00 root partition:p0,p1 data:Selection`, - `└─Selection 2.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - ` └─TableFullScan 6.00 cop[tikv] table:t keep order:false`)) - tk.MustQuery(`select * from t where a >= "3" and a <= "4"`).Sort().Check(testkit.Rows("3 3", "4 4")) - - tk.MustExec(`set @@tidb_partition_prune_mode = 'static'`) - tk.MustQuery(`select * from t where a is null`).Check(testkit.Rows(" ")) - tk.MustQuery(`explain format = 'brief' select * from t where a is null`).Check(testkit.Rows(""+ - `TableReader 1.00 root data:Selection`, - `└─Selection 1.00 cop[tikv] isnull(defaultpartition.t.a)`, - ` └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false`)) - tk.MustQuery(`select * from t where a = 4`).Check(testkit.Rows("4 4")) - tk.MustQuery(`select * from t where a = "4"`).Check(testkit.Rows("4 4")) - tk.MustQuery(`explain format = 'brief' select * from t where a = "4"`).Check(testkit.Rows(""+ - `TableReader 1.00 root data:Selection`, - `└─Selection 1.00 cop[tikv] eq(defaultpartition.t.a, "4")`, - ` └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false`)) - tk.MustQuery(`select * from t where a = 3`).Check(testkit.Rows("3 3")) - tk.MustQuery(`select * from t where a = "3"`).Check(testkit.Rows("3 3")) - tk.MustQuery(`explain format = 'brief' select * from t where a = "3"`).Check(testkit.Rows(""+ - `TableReader 1.00 root data:Selection`, - `└─Selection 1.00 cop[tikv] eq(defaultpartition.t.a, "3")`, - ` └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false`)) - tk.MustQuery(`explain format = 'brief' select * from t where a between "3" and "4"`).Check(testkit.Rows(""+ - `PartitionUnion 2.00 root `, - `├─TableReader 1.00 root data:Selection`, - `│ └─Selection 1.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - `│ └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false`, - `└─TableReader 1.00 root data:Selection`, - ` └─Selection 1.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - ` └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false`)) - tk.MustQuery(`select * from t where a between "3" and "4"`).Sort().Check(testkit.Rows("3 3", "4 4")) - tk.MustQuery(`explain format = 'brief' select * from t where a >= "3" and a <= "4"`).Check(testkit.Rows(""+ - `PartitionUnion 2.00 root `, - `├─TableReader 1.00 root data:Selection`, - `│ └─Selection 1.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - `│ └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false`, - `└─TableReader 1.00 root data:Selection`, - ` └─Selection 1.00 cop[tikv] ge(defaultpartition.t.a, "3"), le(defaultpartition.t.a, "4")`, - ` └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false`)) - tk.MustQuery(`select * from t where a >= "3" and a <= "4"`).Sort().Check(testkit.Rows("3 3", "4 4")) - - // DEFAULT partition will not be used in reorganize partition if not included in the source/destination - tk.MustExecToErr(`alter table t reorganize partition p0 into (partition p0 values in (0))`, "[table:1526]Table has no partition for value 4") -} - -func TestDefaultListErrors(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create schema defaultListErrors") - tk.MustExec("use defaultListErrors") - tk.MustContainErrMsg(`create table t (a int) partition by range (a) (partition p0 values less than (default))`, - "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 86 near ") - tk.MustContainErrMsg(`create table t (a int) partition by range (a) (partition p0 values less than ("default"))`, - "[ddl:1697]VALUES value for partition 'p0' must have type INT") - tk.MustExec(`create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than ("default"))`) - tk.MustExec(`drop table t`) - tk.MustContainErrMsg(`create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than (default))`, - "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 102 near ") - tk.MustContainErrMsg(`create table t (a varchar(55)) partition by range columns (a) (partition p0 default)`, - "[ddl:1480]Only LIST PARTITIONING can use VALUES IN in partition definition") - tk.MustExec(`create table t (a varchar(55)) partition by list columns (a) (partition p0 default)`) - tk.MustExec(`insert into t values ('Hi')`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` varchar(55) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(`a`)\n" + - "(PARTITION `p0` DEFAULT)")) - tk.MustExec(`drop table t`) - tk.MustExec(`create table t (a int) partition by list (a) (partition p0 default)`) - tk.MustExec(`insert into t values (2)`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (`a`)\n" + - "(PARTITION `p0` DEFAULT)")) - tk.MustExec(`drop table t`) - tk.MustContainErrMsg(`create table t (a int) partition by list (a) (partition p0 values in ())`, - "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 71 near ") -} - -func TestAlterTableDropPartitionByList(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec(`create table t (id int) partition by list (id) ( - partition p0 values in (1,2), - partition p1 values in (3,4), - partition p3 values in (5,null) - );`) - // TODO: Add cases for dropping DEFAULT partition - tk.MustExec(`insert into t values (1),(3),(5),(null)`) - tk.MustExec(`alter table t drop partition p1`) - tk.MustQuery("select * from t order by id").Check(testkit.Rows("", "1", "5")) - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().Partition) - part := tbl.Meta().Partition - require.True(t, part.Type == model.PartitionTypeList) - require.Equal(t, "`id`", part.Expr) - require.Len(t, part.Definitions, 2) - require.Equal(t, [][]string{{"1"}, {"2"}}, part.Definitions[0].InValues) - require.Equal(t, model.NewCIStr("p0"), part.Definitions[0].Name) - require.Equal(t, [][]string{{"5"}, {"NULL"}}, part.Definitions[1].InValues) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[1].Name) - - sql := "alter table t drop partition p10;" - tk.MustGetErrCode(sql, errno.ErrDropPartitionNonExistent) - tk.MustExec(`alter table t drop partition p3`) - tk.MustQuery("select * from t").Check(testkit.Rows("1")) - sql = "alter table t drop partition p0;" - tk.MustGetErrCode(sql, errno.ErrDropLastPartition) -} - -func TestAlterTableDropPartitionByListColumns(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec(`create table t (id int, name varchar(10)) partition by list columns (id,name) ( - partition p0 values in ((1,'a'),(2,'b')), - partition p1 values in ((3,'a'),(4,'b')), - partition p3 values in ((5,'a'),(null,null)), - partition pDef values in (default) - )`) - tk.MustExec(`insert into t values (1,'a'),(3,'a'),(5,'a'),(null,null),(9,9)`) - tk.MustExec(`alter table t drop partition p1`) - tk.MustExec(`alter table t drop partition pDef`) - tk.MustQuery("select * from t").Sort().Check(testkit.Rows("1 a", "5 a", " ")) - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().Partition) - part := tbl.Meta().Partition - require.True(t, part.Type == model.PartitionTypeList) - require.Equal(t, "", part.Expr) - require.Equal(t, "id", part.Columns[0].O) - require.Equal(t, "name", part.Columns[1].O) - require.Len(t, part.Definitions, 2) - require.Equal(t, [][]string{{"1", `'a'`}, {"2", `'b'`}}, part.Definitions[0].InValues) - require.Equal(t, model.NewCIStr("p0"), part.Definitions[0].Name) - require.Equal(t, [][]string{{"5", `'a'`}, {"NULL", "NULL"}}, part.Definitions[1].InValues) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[1].Name) - - sql := "alter table t drop partition p10;" - tk.MustGetErrCode(sql, errno.ErrDropPartitionNonExistent) - tk.MustExec(`alter table t drop partition p3`) - tk.MustQuery("select * from t").Check(testkit.Rows("1 a")) - sql = "alter table t drop partition p0;" - tk.MustGetErrCode(sql, errno.ErrDropLastPartition) -} - func TestAlterTableTruncatePartitionByList(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1973,320 +1261,6 @@ func TestCreateTableWithKeyPartition(t *testing.T) { tk.MustExec(`create table tm2 (a char(5), unique key(a(5))) partition by key() partitions 5;`) } -func TestAlterTableAddPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists employees;") - tk.MustExec(`create table employees ( - id int not null, - hired date not null - ) - partition by range( year(hired) ) ( - partition p1 values less than (1991), - partition p2 values less than (1996), - partition p3 values less than (2001) - );`) - tk.MustExec(`alter table employees add partition ( - partition p4 values less than (2010), - partition p5 values less than MAXVALUE - );`) - - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().Partition) - part := tbl.Meta().Partition - require.Equal(t, model.PartitionTypeRange, part.Type) - - require.Equal(t, "YEAR(`hired`)", part.Expr) - require.Len(t, part.Definitions, 5) - require.Equal(t, "1991", part.Definitions[0].LessThan[0]) - require.Equal(t, model.NewCIStr("p1"), part.Definitions[0].Name) - require.Equal(t, "1996", part.Definitions[1].LessThan[0]) - require.Equal(t, model.NewCIStr("p2"), part.Definitions[1].Name) - require.Equal(t, "2001", part.Definitions[2].LessThan[0]) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[2].Name) - require.Equal(t, "2010", part.Definitions[3].LessThan[0]) - require.Equal(t, model.NewCIStr("p4"), part.Definitions[3].Name) - require.Equal(t, "MAXVALUE", part.Definitions[4].LessThan[0]) - require.Equal(t, model.NewCIStr("p5"), part.Definitions[4].Name) - - tk.MustExec("drop table if exists table1;") - tk.MustExec("create table table1(a int)") - sql1 := `alter table table1 add partition ( - partition p1 values less than (2010), - partition p2 values less than maxvalue - );` - tk.MustGetErrCode(sql1, errno.ErrPartitionMgmtOnNonpartitioned) - tk.MustExec(`create table table_MustBeDefined ( - id int not null, - hired date not null - ) - partition by range( year(hired) ) ( - partition p1 values less than (1991), - partition p2 values less than (1996), - partition p3 values less than (2001) - );`) - sql2 := "alter table table_MustBeDefined add partition" - tk.MustGetErrCode(sql2, errno.ErrPartitionsMustBeDefined) - tk.MustExec("drop table if exists table2;") - tk.MustExec(`create table table2 ( - - id int not null, - hired date not null - ) - partition by range( year(hired) ) ( - partition p1 values less than (1991), - partition p2 values less than maxvalue - );`) - - sql3 := `alter table table2 add partition ( - partition p3 values less than (2010) - );` - tk.MustGetErrCode(sql3, errno.ErrPartitionMaxvalue) - - tk.MustExec("drop table if exists table3;") - tk.MustExec(`create table table3 ( - id int not null, - hired date not null - ) - partition by range( year(hired) ) ( - partition p1 values less than (1991), - partition p2 values less than (2001) - );`) - - sql4 := `alter table table3 add partition ( - partition p3 values less than (1993) - );` - tk.MustGetErrCode(sql4, errno.ErrRangeNotIncreasing) - - sql5 := `alter table table3 add partition ( - partition p1 values less than (1993) - );` - tk.MustGetErrCode(sql5, errno.ErrSameNamePartition) - - sql6 := `alter table table3 add partition ( - partition p1 values less than (1993), - partition p1 values less than (1995) - );` - tk.MustGetErrCode(sql6, errno.ErrSameNamePartition) - - sql7 := `alter table table3 add partition ( - partition p4 values less than (1993), - partition p1 values less than (1995), - partition p5 values less than maxvalue - );` - tk.MustGetErrCode(sql7, errno.ErrSameNamePartition) - - sql8 := "alter table table3 add partition (partition p6);" - tk.MustGetErrCode(sql8, errno.ErrPartitionRequiresValues) - - sql9 := "alter table table3 add partition (partition p7 values in (2018));" - tk.MustGetErrCode(sql9, errno.ErrPartitionWrongValues) - - sql10 := "alter table table3 add partition partitions 4;" - tk.MustGetErrCode(sql10, errno.ErrPartitionsMustBeDefined) - - tk.MustExec("alter table table3 add partition (partition p3 values less than (2001 + 10))") - - // less than value can be negative or expression. - tk.MustExec(`CREATE TABLE tt5 ( - c3 bigint(20) NOT NULL - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin - PARTITION BY RANGE ( c3 ) ( - PARTITION p0 VALUES LESS THAN (-3), - PARTITION p1 VALUES LESS THAN (-2) - );`) - tk.MustExec(`ALTER TABLE tt5 add partition ( partition p2 values less than (-1) );`) - tk.MustExec(`ALTER TABLE tt5 add partition ( partition p3 values less than (5-1) );`) - - // Test add partition for the table partition by range columns. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a datetime) partition by range columns (a) (partition p1 values less than ('2019-06-01'), partition p2 values less than ('2019-07-01'));") - sql := "alter table t add partition ( partition p3 values less than ('2019-07-01'));" - tk.MustGetErrCode(sql, errno.ErrRangeNotIncreasing) - tk.MustExec("alter table t add partition ( partition p3 values less than ('2019-08-01'));") - - // Add partition value's type should be the same with the column's type. - tk.MustExec("drop table if exists t;") - tk.MustExec(`create table t ( - col date not null default '2000-01-01') - partition by range columns (col) ( - PARTITION p0 VALUES LESS THAN ('20190905'), - PARTITION p1 VALUES LESS THAN ('20190906'));`) - sql = "alter table t add partition (partition p2 values less than (20190907));" - tk.MustGetErrCode(sql, errno.ErrWrongTypeColumnValue) -} - -func TestAlterTableDropPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists employees") - tk.MustExec(`create table employees ( - id int not null, - hired int not null - ) - partition by range( hired ) ( - partition p1 values less than (1991), - partition p2 values less than (1996), - partition p3 values less than (2001) - );`) - - tk.MustExec("alter table employees drop partition p3;") - ctx := tk.Session() - is := domain.GetDomain(ctx).InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().GetPartitionInfo()) - part := tbl.Meta().Partition - require.Equal(t, model.PartitionTypeRange, part.Type) - require.Equal(t, "`hired`", part.Expr) - require.Len(t, part.Definitions, 2) - require.Equal(t, "1991", part.Definitions[0].LessThan[0]) - require.Equal(t, model.NewCIStr("p1"), part.Definitions[0].Name) - require.Equal(t, "1996", part.Definitions[1].LessThan[0]) - require.Equal(t, model.NewCIStr("p2"), part.Definitions[1].Name) - - tk.MustExec("drop table if exists table1;") - tk.MustExec("create table table1 (a int);") - sql1 := "alter table table1 drop partition p10;" - tk.MustGetErrCode(sql1, errno.ErrPartitionMgmtOnNonpartitioned) - - tk.MustExec("drop table if exists table2;") - tk.MustExec(`create table table2 ( - id int not null, - hired date not null - ) - partition by range( year(hired) ) ( - partition p1 values less than (1991), - partition p2 values less than (1996), - partition p3 values less than (2001) - );`) - sql2 := "alter table table2 drop partition p10;" - tk.MustGetErrCode(sql2, errno.ErrDropPartitionNonExistent) - - tk.MustExec("drop table if exists table3;") - tk.MustExec(`create table table3 ( - id int not null - ) - partition by range( id ) ( - partition p1 values less than (1991) - );`) - sql3 := "alter table table3 drop partition p1;" - tk.MustGetErrCode(sql3, errno.ErrDropLastPartition) - - tk.MustExec("drop table if exists table4;") - tk.MustExec(`create table table4 ( - id int not null - ) - partition by range( id ) ( - partition p1 values less than (10), - partition p2 values less than (20), - partition p3 values less than MAXVALUE - );`) - - tk.MustExec("alter table table4 drop partition p2;") - is = domain.GetDomain(ctx).InfoSchema() - tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("table4")) - require.NoError(t, err) - require.NotNil(t, tbl.Meta().GetPartitionInfo()) - part = tbl.Meta().Partition - require.Equal(t, model.PartitionTypeRange, part.Type) - require.Equal(t, "`id`", part.Expr) - require.Len(t, part.Definitions, 2) - require.Equal(t, "10", part.Definitions[0].LessThan[0]) - require.Equal(t, model.NewCIStr("p1"), part.Definitions[0].Name) - require.Equal(t, "MAXVALUE", part.Definitions[1].LessThan[0]) - require.Equal(t, model.NewCIStr("p3"), part.Definitions[1].Name) - - tk.MustExec("drop table if exists tr;") - tk.MustExec(` create table tr( - id int, name varchar(50), - purchased date - ) - partition by range( year(purchased) ) ( - partition p0 values less than (1990), - partition p1 values less than (1995), - partition p2 values less than (2000), - partition p3 values less than (2005), - partition p4 values less than (2010), - partition p5 values less than (2015) - );`) - tk.MustExec(`INSERT INTO tr VALUES - (1, 'desk organiser', '2003-10-15'), - (2, 'alarm clock', '1997-11-05'), - (3, 'chair', '2009-03-10'), - (4, 'bookcase', '1989-01-10'), - (5, 'exercise bike', '2014-05-09'), - (6, 'sofa', '1987-06-05'), - (7, 'espresso maker', '2011-11-22'), - (8, 'aquarium', '1992-08-04'), - (9, 'study desk', '2006-09-16'), - (10, 'lava lamp', '1998-12-25');`) - result := tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';") - result.Check(testkit.Rows(`2 alarm clock 1997-11-05`, `10 lava lamp 1998-12-25`)) - tk.MustExec("alter table tr drop partition p2;") - result = tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';") - result.Check(testkit.Rows()) - - result = tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';") - result.Check(testkit.Rows(`5 exercise bike 2014-05-09`, `7 espresso maker 2011-11-22`)) - tk.MustExec("alter table tr drop partition p5;") - result = tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';") - result.Check(testkit.Rows()) - - tk.MustExec("alter table tr drop partition p4;") - result = tk.MustQuery("select * from tr where purchased between '2005-01-01' and '2009-12-31';") - result.Check(testkit.Rows()) - - tk.MustExec("drop table if exists table4;") - tk.MustExec(`create table table4 ( - id int not null - ) - partition by range( id ) ( - partition Par1 values less than (1991), - partition pAR2 values less than (1992), - partition Par3 values less than (1995), - partition PaR5 values less than (1996) - );`) - tk.MustExec("alter table table4 drop partition Par2;") - tk.MustExec("alter table table4 drop partition PAR5;") - sql4 := "alter table table4 drop partition PAR0;" - tk.MustGetErrCode(sql4, errno.ErrDropPartitionNonExistent) - - tk.MustExec("CREATE TABLE t1 (a int(11), b varchar(64)) PARTITION BY HASH(a) PARTITIONS 3") - tk.MustGetErrCode("alter table t1 drop partition p2", errno.ErrOnlyOnRangeListPartition) -} - -func TestMultiPartitionDropAndTruncate(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists employees") - tk.MustExec(`create table employees ( - hired int not null - ) - partition by range( hired ) ( - partition p1 values less than (1991), - partition p2 values less than (1996), - partition p3 values less than (2001), - partition p4 values less than (2006), - partition p5 values less than (2011) - );`) - tk.MustExec(`INSERT INTO employees VALUES (1990), (1995), (2000), (2005), (2010)`) - - tk.MustExec("alter table employees drop partition p1, p2;") - result := tk.MustQuery("select * from employees;") - result.Sort().Check(testkit.Rows(`2000`, `2005`, `2010`)) - - tk.MustExec("alter table employees truncate partition p3, p4") - result = tk.MustQuery("select * from employees;") - result.Check(testkit.Rows(`2010`)) -} - func TestCreatePartitionTableWithGlobalIndex(t *testing.T) { defer config.RestoreFunc()() config.UpdateGlobal(func(conf *config.Config) { @@ -3373,223 +2347,46 @@ func TestExchangePartitionMultiTable(t *testing.T) { tk1.MustExec(`CREATE TABLE t2 (a int)`) tk1.MustExec(`CREATE TABLE tp (a int) partition by hash(a) partitions 3`) tk1.MustExec(`insert into t1 values (0)`) - tk1.MustExec(`insert into t2 values (3)`) - tk1.MustExec(`insert into tp values (6)`) - - tk2 := testkit.NewTestKit(t, store) - tk2.MustExec(`use ` + dbName) - tk3 := testkit.NewTestKit(t, store) - tk3.MustExec(`use ` + dbName) - tk4 := testkit.NewTestKit(t, store) - tk4.MustExec(`use ` + dbName) - waitFor := func(col int, tableName, s string) { - for { - tk4 := testkit.NewTestKit(t, store) - tk4.MustExec(`use test`) - sql := `admin show ddl jobs where db_name = '` + strings.ToLower(dbName) + `' and table_name = '` + tableName + `' and job_type = 'exchange partition'` - res := tk4.MustQuery(sql).Rows() - if len(res) == 1 && res[0][col] == s { - break - } - time.Sleep(10 * time.Millisecond) - } - } - alterChan1 := make(chan error) - alterChan2 := make(chan error) - tk3.MustExec(`BEGIN`) - tk3.MustExec(`insert into tp values (1)`) - go func() { - alterChan1 <- tk1.ExecToErr(`alter table tp exchange partition p0 with table t1`) - }() - waitFor(11, "t1", "running") - go func() { - alterChan2 <- tk2.ExecToErr(`alter table tp exchange partition p0 with table t2`) - }() - waitFor(11, "t2", "queueing") - tk3.MustExec(`rollback`) - require.NoError(t, <-alterChan1) - err := <-alterChan2 - tk3.MustQuery(`select * from t1`).Check(testkit.Rows("6")) - tk3.MustQuery(`select * from t2`).Check(testkit.Rows("0")) - tk3.MustQuery(`select * from tp`).Check(testkit.Rows("3")) - require.NoError(t, err) -} - -func TestExchangePartitionValidation(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - dbName := "ExchangeValidation" - tk.MustExec(`create schema ` + dbName) - tk.MustExec(`use ` + dbName) - tk.MustExec(`CREATE TABLE t1 ( - d date NOT NULL , - name varchar(10) NOT NULL, - UNIQUE KEY (d,name))`) - - tk.MustExec(`CREATE TABLE t1p ( - d date NOT NULL , - name varchar(10) NOT NULL, - UNIQUE KEY (d,name) - ) - PARTITION BY RANGE COLUMNS(d) - (PARTITION p202307 VALUES LESS THAN ('2023-08-01'), - PARTITION p202308 VALUES LESS THAN ('2023-09-01'), - PARTITION p202309 VALUES LESS THAN ('2023-10-01'), - PARTITION p202310 VALUES LESS THAN ('2023-11-01'), - PARTITION p202311 VALUES LESS THAN ('2023-12-01'), - PARTITION p202312 VALUES LESS THAN ('2024-01-01'), - PARTITION pfuture VALUES LESS THAN (MAXVALUE))`) - - tk.MustExec(`insert into t1 values ("2023-08-06","0000")`) - tk.MustContainErrMsg(`alter table t1p exchange partition p202307 with table t1 with validation`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`insert into t1 values ("2023-08-06","0001")`) -} - -func TestExchangePartitionCheckConstraint(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec(`create database db_one`) - tk.MustExec(`create database db_two`) - - ntSQL := "create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED)" - ptSQL := "create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) )" - alterSQL := "alter table db_two.pt exchange partition p1 with table db_one.nt" - dropSQL := "drop table db_one.nt, db_two.pt" - errMsg := "[ddl:1737]Found a row that does not match the partition" - - type record struct { - a int - b int - } - inputs := []struct { - t []record - pt []record - ok bool - }{ - { - t: []record{{60, 60}}, - ok: true, - }, - { - t: []record{{80, 60}}, - ok: false, - }, - { - t: []record{{60, 80}}, - ok: false, - }, - { - t: []record{{80, 80}}, - ok: false, - }, - { - t: []record{{60, 120}}, - ok: true, - }, - { - t: []record{{80, 120}}, - ok: false, - }, - { - pt: []record{{60, 60}}, - ok: true, - }, - { - pt: []record{{60, 50}}, - ok: false, - }, - // Record in partition p0(less than (50)). - { - pt: []record{{30, 50}}, - ok: true, - }, - { - t: []record{{60, 60}}, - pt: []record{{70, 70}}, - ok: true, - }, - { - t: []record{{60, 60}}, - pt: []record{{70, 70}, {30, 50}}, - ok: true, - }, - { - t: []record{{60, 60}, {60, 120}}, - pt: []record{{70, 70}, {30, 50}}, - ok: true, - }, - { - t: []record{{60, 60}, {80, 120}}, - pt: []record{{70, 70}, {30, 50}}, - ok: false, - }, - { - t: []record{{60, 60}}, - pt: []record{{70, 70}, {30, 50}, {60, 50}}, - ok: false, - }, - { - t: []record{{60, 60}, {60, 80}}, - pt: []record{{70, 70}, {30, 50}}, - ok: false, - }, - } - for _, input := range inputs { - tk.MustExec(`set @@global.tidb_enable_check_constraint = 1`) - tk.MustExec(ntSQL) - for _, r := range input.t { - tk.MustExec(fmt.Sprintf("insert into db_one.nt values (%d, %d)", r.a, r.b)) - } - tk.MustExec(ptSQL) - for _, r := range input.pt { - tk.MustExec(fmt.Sprintf("insert into db_two.pt values (%d, %d)", r.a, r.b)) - } - if input.ok { - tk.MustExec(alterSQL) - tk.MustExec(dropSQL) - continue + tk1.MustExec(`insert into t2 values (3)`) + tk1.MustExec(`insert into tp values (6)`) + + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec(`use ` + dbName) + tk3 := testkit.NewTestKit(t, store) + tk3.MustExec(`use ` + dbName) + tk4 := testkit.NewTestKit(t, store) + tk4.MustExec(`use ` + dbName) + waitFor := func(col int, tableName, s string) { + for { + tk4 := testkit.NewTestKit(t, store) + tk4.MustExec(`use test`) + sql := `admin show ddl jobs where db_name = '` + strings.ToLower(dbName) + `' and table_name = '` + tableName + `' and job_type = 'exchange partition'` + res := tk4.MustQuery(sql).Rows() + if len(res) == 1 && res[0][col] == s { + break + } + time.Sleep(10 * time.Millisecond) } - tk.MustContainErrMsg(alterSQL, errMsg) - tk.MustExec(`set @@global.tidb_enable_check_constraint = 0`) - tk.MustExec(alterSQL) - tk.MustExec(dropSQL) } -} - -func TestExchangePartitionPlacementPolicy(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec(`create schema ExchangePartWithPolicy`) - tk.MustExec(`use ExchangePartWithPolicy`) - tk.MustExec(`CREATE PLACEMENT POLICY rule1 FOLLOWERS=1`) - tk.MustExec(`CREATE PLACEMENT POLICY rule2 FOLLOWERS=2`) - tk.MustExec(`CREATE TABLE t1 ( - d date NOT NULL , - name varchar(10) NOT NULL, - UNIQUE KEY (d,name) - ) PLACEMENT POLICY="rule1"`) - - tk.MustExec(`CREATE TABLE t1p ( - d date NOT NULL , - name varchar(10) NOT NULL, - UNIQUE KEY (d,name) - ) PLACEMENT POLICY="rule2" - PARTITION BY RANGE COLUMNS(d) - (PARTITION p202307 VALUES LESS THAN ('2023-08-01'), - PARTITION p202308 VALUES LESS THAN ('2023-09-01'), - PARTITION p202309 VALUES LESS THAN ('2023-10-01'), - PARTITION p202310 VALUES LESS THAN ('2023-11-01'), - PARTITION p202311 VALUES LESS THAN ('2023-12-01'), - PARTITION p202312 VALUES LESS THAN ('2024-01-01'), - PARTITION pfuture VALUES LESS THAN (MAXVALUE))`) - - tk.MustContainErrMsg(`alter table t1p exchange partition p202307 with table t1`, - "[ddl:1736]Tables have different definitions") - tk.MustExec(`insert into t1 values ("2023-08-06","0000")`) + alterChan1 := make(chan error) + alterChan2 := make(chan error) + tk3.MustExec(`BEGIN`) + tk3.MustExec(`insert into tp values (1)`) + go func() { + alterChan1 <- tk1.ExecToErr(`alter table tp exchange partition p0 with table t1`) + }() + waitFor(11, "t1", "running") + go func() { + alterChan2 <- tk2.ExecToErr(`alter table tp exchange partition p0 with table t2`) + }() + waitFor(11, "t2", "queueing") + tk3.MustExec(`rollback`) + require.NoError(t, <-alterChan1) + err := <-alterChan2 + tk3.MustQuery(`select * from t1`).Check(testkit.Rows("6")) + tk3.MustQuery(`select * from t2`).Check(testkit.Rows("0")) + tk3.MustQuery(`select * from tp`).Check(testkit.Rows("3")) + require.NoError(t, err) } func TestExchangePartitionHook(t *testing.T) { @@ -3657,38 +2454,6 @@ func TestExchangePartitionAutoID(t *testing.T) { tk.MustQuery("select count(*) from pt where a >= 4000000").Check(testkit.Rows("1")) } -func TestTiDBEnableExchangePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec(`create table pt (a int primary key auto_increment) partition by range(a) ( - partition p0 values less than (3), - partition p1 values less than (6), - PARTITION p2 values less than (9) - );`) - // default - tk.MustQuery("select @@tidb_enable_exchange_partition").Check(testkit.Rows("1")) - tk.MustExec(`create table nt(a int primary key auto_increment);`) - tk.MustExec("alter table pt exchange partition p0 with table nt") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 after the exchange, please analyze related table of the exchange to update statistics")) - - // set tidb_enable_exchange_partition = 0 - tk.MustExec("set @@tidb_enable_exchange_partition=0") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 tidb_enable_exchange_partition is always turned on. This variable has been deprecated and will be removed in the future releases")) - tk.MustQuery("select @@tidb_enable_exchange_partition").Check(testkit.Rows("1")) - tk.MustExec("alter table pt exchange partition p0 with table nt") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 after the exchange, please analyze related table of the exchange to update statistics")) - - // set tidb_enable_exchange_partition = 1 - tk.MustExec("set @@tidb_enable_exchange_partition=1") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select @@tidb_enable_exchange_partition").Check(testkit.Rows("1")) - tk.MustExec("alter table pt exchange partition p0 with table nt") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 after the exchange, please analyze related table of the exchange to update statistics")) - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 after the exchange, please analyze related table of the exchange to update statistics")) -} - func TestExchangePartitionExpressIndex(t *testing.T) { restore := config.RestoreFunc() defer restore() @@ -3967,271 +2732,18 @@ func TestTruncatePartitionAndDropTable(t *testing.T) { } } -func TestPartitionUniqueKeyNeedAllFieldsInPf(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists part1;") - tk.MustExec(`create table part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1, col2) - ) - partition by range( col1 + col2 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );`) - - tk.MustExec("drop table if exists part2;") - tk.MustExec(`create table part2 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1, col2, col3), - unique key (col3) - ) - partition by range( col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );`) - - tk.MustExec("drop table if exists part3;") - tk.MustExec(`create table part3 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col2) - ) - partition by range( col1 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );`) - - tk.MustExec("drop table if exists part4;") - tk.MustExec(`create table part4 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col2), - unique key(col2) - ) - partition by range( year(col2) ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );`) - - tk.MustExec("drop table if exists part5;") - tk.MustExec(`create table part5 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col2, col4), - unique key(col2, col1) - ) - partition by range( col1 + col2 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );`) - - tk.MustExec("drop table if exists Part1;") - sql1 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1, col2) - ) - partition by range( col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql1, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql2 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1), - unique key (col3) - ) - partition by range( col1 + col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql2, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql3 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1), - unique key (col3) - ) - partition by range( col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql3, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql4 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - unique key (col1, col2, col3), - unique key (col3) - ) - partition by range( col1 + col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql4, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql5 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col2) - ) - partition by range( col3 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql5, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql6 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col3), - unique key(col2) - ) - partition by range( year(col2) ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql6, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists Part1;") - sql7 := `create table Part1 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - primary key(col1, col3, col4), - unique key(col2, col1) - ) - partition by range( col1 + col2 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql7, errno.ErrUniqueKeyNeedAllFieldsInPf) - - tk.MustExec("drop table if exists part6;") - sql8 := `create table part6 ( - col1 int not null, - col2 date not null, - col3 int not null, - col4 int not null, - col5 int not null, - unique key(col1, col2), - unique key(col1, col3) - ) - partition by range( col1 + col2 ) ( - partition p1 values less than (11), - partition p2 values less than (15) - );` - tk.MustGetErrCode(sql8, errno.ErrUniqueKeyNeedAllFieldsInPf) - - sql9 := `create table part7 ( - col1 int not null, - col2 int not null, - col3 int not null unique, - unique key(col1, col2) - ) - partition by range (col1 + col2) ( - partition p1 values less than (11), - partition p2 values less than (15) - )` - tk.MustGetErrCode(sql9, errno.ErrUniqueKeyNeedAllFieldsInPf) - - sql10 := `create table part8 ( - a int not null, - b int not null, - c int default null, - d int default null, - e int default null, - primary key (a, b), - unique key (c, d) - ) - partition by range columns (b) ( - partition p0 values less than (4), - partition p1 values less than (7), - partition p2 values less than (11) - )` - tk.MustGetErrCode(sql10, errno.ErrUniqueKeyNeedAllFieldsInPf) - - sql11 := `create table part9 ( - a int not null, - b int not null, - c int default null, - d int default null, - e int default null, - primary key (a, b), - unique key (b, c, d) - ) - partition by range columns (b, c) ( - partition p0 values less than (4, 5), - partition p1 values less than (7, 9), - partition p2 values less than (11, 22) - )` - tk.MustGetErrCode(sql11, errno.ErrUniqueKeyNeedAllFieldsInPf) - - sql12 := `create table part12 (a varchar(20), b binary, unique index (a(5))) partition by range columns (a) ( - partition p0 values less than ('aaaaa'), - partition p1 values less than ('bbbbb'), - partition p2 values less than ('ccccc'))` - tk.MustGetErrCode(sql12, errno.ErrUniqueKeyNeedAllFieldsInPf) - tk.MustExec(`create table part12 (a varchar(20), b binary) partition by range columns (a) ( - partition p0 values less than ('aaaaa'), - partition p1 values less than ('bbbbb'), - partition p2 values less than ('ccccc'))`) - tk.MustGetErrCode("alter table part12 add unique index (a(5))", errno.ErrUniqueKeyNeedAllFieldsInPf) - sql13 := `create table part13 (a varchar(20), b varchar(10), unique index (a(5),b)) partition by range columns (b) ( - partition p0 values less than ('aaaaa'), - partition p1 values less than ('bbbbb'), - partition p2 values less than ('ccccc'))` - tk.MustExec(sql13) -} - -func TestPartitionDropPrimaryKey(t *testing.T) { +func TestPartitionDropPrimaryKeyAndDropIndex(t *testing.T) { store := testkit.CreateMockStore(t) + // Drop Primary Key idxName := "primary" addIdxSQL := "alter table partition_drop_idx add primary key idx1 (c1);" dropIdxSQL := "alter table partition_drop_idx drop primary key;" testPartitionDropIndex(t, store, 50*time.Millisecond, idxName, addIdxSQL, dropIdxSQL) -} -func TestPartitionDropIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - idxName := "idx1" - addIdxSQL := "alter table partition_drop_idx add index idx1 (c1);" - dropIdxSQL := "alter table partition_drop_idx drop index idx1;" + // Drop Index + idxName = "idx1" + addIdxSQL = "alter table partition_drop_idx add index idx1 (c1);" + dropIdxSQL = "alter table partition_drop_idx drop index idx1;" testPartitionDropIndex(t, store, 50*time.Millisecond, idxName, addIdxSQL, dropIdxSQL) } @@ -4272,7 +2784,6 @@ LOOP: require.NoError(t, err) case <-ticker.C: step := 10 - rand.Seed(time.Now().Unix()) for i := num; i < num+step; i++ { n := rand.Intn(num) tk.MustExec("update partition_drop_idx set c2 = 1 where c1 = ?", n) @@ -4284,20 +2795,18 @@ LOOP: tk.MustExec("drop table partition_drop_idx;") } -func TestPartitionAddPrimaryKey(t *testing.T) { +func TestPartitionAddPrimaryKeyAndAddIndex(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) + // Add Primary Key testPartitionAddIndexOrPK(t, tk, "primary key") -} - -func TestPartitionAddIndex(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) + // Add Index testPartitionAddIndexOrPK(t, tk, "index") } func testPartitionAddIndexOrPK(t *testing.T, tk *testkit.TestKit, key string) { tk.MustExec("use test") + tk.MustExec("drop table if exists partition_add_idx") tk.MustExec(`create table partition_add_idx ( id int not null, hired date not null @@ -4526,503 +3035,6 @@ func TestPartitionErrorCode(t *testing.T) { tk1.MustExec("commit") } -func TestCoalescePartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - // coalesce partition - tk.MustExec(`create schema coalescePart`) - tk.MustExec(`use coalescePart`) - tk.MustExec(`create table t ( - id int, - fname varchar(30), - lname varchar(30), - signed date - ) - partition by hash( month(signed) ) - partitions 12`) - for i := 0; i < 200; i++ { - tk.MustExec(`insert into t values (?, "Joe", "Doe", from_days(738974 + ?))`, i, i*3) - } - tk.MustExec("alter table t coalesce partition 4") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `signed` date DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (MONTH(`signed`)) PARTITIONS 8")) - tk.MustExec(`analyze table t`) - tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'coalescePart'`).Sort().Check(testkit.Rows(""+ - "p0 20", - "p1 30", - "p2 30", - "p3 27", - "p4 31", - "p5 20", - "p6 20", - "p7 22")) - tk.MustQuery("" + - `(select 'p0', count(*) from t partition (p0) UNION` + - ` select 'p1', count(*) from t partition (p1) UNION` + - ` select 'p2', count(*) from t partition (p2) UNION` + - ` select 'p3', count(*) from t partition (p3) UNION` + - ` select 'p4', count(*) from t partition (p4) UNION` + - ` select 'p5', count(*) from t partition (p5) UNION` + - ` select 'p6', count(*) from t partition (p6) UNION` + - ` select 'p7', count(*) from t partition (p7)` + - `) ORDER BY 1`).Check(testkit.Rows(""+ - "p0 20", - "p1 30", - "p2 30", - "p3 27", - "p4 31", - "p5 20", - "p6 20", - "p7 22")) - tk.MustExec(`drop table t`) - tk.MustExec(`create table t ( - id int, - fname varchar(30), - lname varchar(30), - signed date - ) - partition by key(signed,fname) - partitions 12`) - for i := 0; i < 200; i++ { - tk.MustExec(`insert into t values (?, "Joe", "Doe", from_days(738974 + ?))`, i, i*3) - } - tk.MustExec("alter table t coalesce partition 4") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `signed` date DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY KEY (`signed`,`fname`) PARTITIONS 8")) - tk.MustExec(`analyze table t`) - tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'coalescePart'`).Sort().Check(testkit.Rows(""+ - "p0 26", - "p1 28", - "p2 22", - "p3 24", - "p4 30", - "p5 27", - "p6 22", - "p7 21")) - tk.MustQuery("" + - `(select 'p0', count(*) from t partition (p0) UNION` + - ` select 'p1', count(*) from t partition (p1) UNION` + - ` select 'p2', count(*) from t partition (p2) UNION` + - ` select 'p3', count(*) from t partition (p3) UNION` + - ` select 'p4', count(*) from t partition (p4) UNION` + - ` select 'p5', count(*) from t partition (p5) UNION` + - ` select 'p6', count(*) from t partition (p6) UNION` + - ` select 'p7', count(*) from t partition (p7)` + - `) ORDER BY 1`).Check(testkit.Rows(""+ - "p0 26", - "p1 28", - "p2 22", - "p3 24", - "p4 30", - "p5 27", - "p6 22", - "p7 21")) -} - -func TestAddHashPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - // add partition - tk.MustExec("create database addHash") - tk.MustExec("use addHash") - tk.MustExec(`create table t ( - id int not null, - fname varchar(30), - lname varchar(30), - hired date not null default '1970-01-01', - separated date, - job_code int, - store_id int - ) - partition by hash(store_id) - partitions 4`) - tk.MustExec(`insert into t values (1,"one", "two","1999-01-04", null, 1, 1), (2, "Who", "Else", '2020-12-02', '2022-12-31', 2, 2)`) - tk.MustExec(`insert into t select id + 2, fname, lname, hired, separated, job_code, store_id + 2 from t`) - tk.MustExec(`insert into t select id + 4, fname, lname, hired, separated, job_code, store_id + 4 from t`) - tk.MustExec(`insert into t select id + 8, fname, lname, hired, separated, job_code, store_id + 8 from t`) - tk.MustExec(`insert into t select id + 16, fname, lname, hired, separated, job_code, store_id + 16 from t`) - tk.MustExec(`insert into t select id + 32, fname, lname, hired, separated, job_code, store_id + 32 from t`) - tk.MustExec(`insert into t select id + 64, fname, lname, hired, separated, job_code, store_id + 64 from t`) - tk.MustExec(`insert into t select id + 128, fname, lname, hired, separated, job_code, store_id + 128 from t`) - tk.MustExec(`insert into t select id + 256, fname, lname, hired, separated, job_code, store_id + 256 from t`) - tk.MustExec(`insert into t select id + 512, fname, lname, hired, separated, job_code, store_id + 512 from t`) - tk.MustQuery(`select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t`).Check(testkit.Rows("524800 512.5000 1024 1 524800")) - // TiDB does not support system versioned tables / SYSTEM_TIME - // also the error is slightly wrong with 'VALUES HISTORY' - // instead of just 'HISTORY' - tk.MustContainErrMsg(`alter table t add partition (partition pHist history)`, "[ddl:1480]Only SYSTEM_TIME PARTITIONING can use VALUES HISTORY in partition definition") - tk.MustContainErrMsg(`alter table t add partition (partition pList values in (22))`, "[ddl:1480]Only LIST PARTITIONING can use VALUES IN in partition definition") - tk.MustContainErrMsg(`alter table t add partition (partition pRange values less than (22))`, "[ddl:1480]Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition") - tk.MustExec(`insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1)`) - tk.MustQuery(`select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t`).Check(testkit.Rows("524802 511.5029 1024 1 524841")) - tk.MustExec("alter table t add partition partitions 8") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`store_id`) PARTITIONS 12")) - tk.MustContainErrMsg(`alter table t coalesce partition 0`, - "[ddl:1515]At least one partition must be coalesced") - tk.MustContainErrMsg(`alter table t coalesce partition 12`, - "[ddl:1508]Cannot remove all partitions, use DROP TABLE instead") - tk.MustExec(`create placement policy tworeplicas followers=1`) - tk.MustExec(`create placement policy threereplicas followers=2`) - tk.MustExec(`create placement policy fourreplicas followers=3`) - tk.MustExec("alter table t add partition (partition pp13 comment 'p13' placement policy tworeplicas, partition pp14 comment 'p14' placement policy threereplicas, partition pp15 comment 'p15' placement policy fourreplicas)") - tk.MustExec(`alter table t add partition partitions 1`) - tk.MustExec(`alter table t coalesce partition 1`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`store_id`)\n" + - "(PARTITION `p0`,\n" + - " PARTITION `p1`,\n" + - " PARTITION `p2`,\n" + - " PARTITION `p3`,\n" + - " PARTITION `p4`,\n" + - " PARTITION `p5`,\n" + - " PARTITION `p6`,\n" + - " PARTITION `p7`,\n" + - " PARTITION `p8`,\n" + - " PARTITION `p9`,\n" + - " PARTITION `p10`,\n" + - " PARTITION `p11`,\n" + - " PARTITION `pp13` COMMENT 'p13' /*T![placement] PLACEMENT POLICY=`tworeplicas` */,\n" + - " PARTITION `pp14` COMMENT 'p14' /*T![placement] PLACEMENT POLICY=`threereplicas` */,\n" + - " PARTITION `pp15` COMMENT 'p15' /*T![placement] PLACEMENT POLICY=`fourreplicas` */)")) - // MySQL keeps the comments, so we should keep all options connected to the remaining partitions too! - // So once you added any partition option, - // it will never go back to just a number of partitions in MySQL! - tk.MustExec("alter table t coalesce partition 2") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`store_id`)\n" + - "(PARTITION `p0`,\n" + - " PARTITION `p1`,\n" + - " PARTITION `p2`,\n" + - " PARTITION `p3`,\n" + - " PARTITION `p4`,\n" + - " PARTITION `p5`,\n" + - " PARTITION `p6`,\n" + - " PARTITION `p7`,\n" + - " PARTITION `p8`,\n" + - " PARTITION `p9`,\n" + - " PARTITION `p10`,\n" + - " PARTITION `p11`,\n" + - " PARTITION `pp13` COMMENT 'p13' /*T![placement] PLACEMENT POLICY=`tworeplicas` */)")) - // If no extra options, it will go back to just numbers in TiDB: - tk.MustExec("alter table t coalesce partition 1") - tk.MustExec(`alter table t add partition (partition p13 comment 'p13')`) - tk.MustContainErrMsg(`alter table t add partition partitions 1`, - "[ddl:1517]Duplicate partition name p13") - tk.MustExec("alter table t coalesce partition 1") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`store_id`) PARTITIONS 12")) - tk.MustExec(`alter table t add partition (partition p12 comment 'p12' placement policy tworeplicas)`) - tk.MustExec("alter table t placement policy fourreplicas") - tk.MustExec(`alter table t add partition partitions 1`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`fourreplicas` */\n" + - "PARTITION BY HASH (`store_id`)\n" + - "(PARTITION `p0`,\n" + - " PARTITION `p1`,\n" + - " PARTITION `p2`,\n" + - " PARTITION `p3`,\n" + - " PARTITION `p4`,\n" + - " PARTITION `p5`,\n" + - " PARTITION `p6`,\n" + - " PARTITION `p7`,\n" + - " PARTITION `p8`,\n" + - " PARTITION `p9`,\n" + - " PARTITION `p10`,\n" + - " PARTITION `p11`,\n" + - " PARTITION `p12` COMMENT 'p12' /*T![placement] PLACEMENT POLICY=`tworeplicas` */,\n" + - " PARTITION `p13`)")) - tk.MustExec(`alter table t remove partitioning`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`fourreplicas` */")) - tk.MustQuery(`select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t`).Check(testkit.Rows("524802 511.5029 1024 1 524841")) - tk.MustExec("alter table t placement policy default") - tk.MustExec(`drop placement policy tworeplicas`) - tk.MustExec(`drop placement policy threereplicas`) - tk.MustExec(`drop placement policy fourreplicas`) - tk.MustExec(`drop table t`) - - tk.MustExec(`create table t ( - id int not null, - fname varchar(30), - lname varchar(30), - hired date not null default '1970-01-01', - separated date, - job_code int, - store_id int - ) - partition by key (hired) - partitions 4`) - tk.MustExec(`insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1)`) - tk.MustExec("alter table t add partition partitions 8") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY KEY (`hired`) PARTITIONS 12")) - tk.MustExec("alter table t add partition (partition p13)") - tk.MustContainErrMsg(`alter table t add partition partitions 1`, - "[ddl:1517]Duplicate partition name p13") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY KEY (`hired`)\n" + - "(PARTITION `p0`,\n" + - " PARTITION `p1`,\n" + - " PARTITION `p2`,\n" + - " PARTITION `p3`,\n" + - " PARTITION `p4`,\n" + - " PARTITION `p5`,\n" + - " PARTITION `p6`,\n" + - " PARTITION `p7`,\n" + - " PARTITION `p8`,\n" + - " PARTITION `p9`,\n" + - " PARTITION `p10`,\n" + - " PARTITION `p11`,\n" + - " PARTITION `p13`)")) - // Note: MySQL does not remove all names when coalesce partitions is back to defaults - tk.MustExec("alter table t coalesce partition 2") - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `id` int(11) NOT NULL,\n" + - " `fname` varchar(30) DEFAULT NULL,\n" + - " `lname` varchar(30) DEFAULT NULL,\n" + - " `hired` date NOT NULL DEFAULT '1970-01-01',\n" + - " `separated` date DEFAULT NULL,\n" + - " `job_code` int(11) DEFAULT NULL,\n" + - " `store_id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY KEY (`hired`) PARTITIONS 11")) -} - -func TestConstAndTimezoneDepent(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - // add partition - tk.MustExec("set @@session.tidb_enable_table_partition = 1") - tk.MustExec("drop database if exists test_db_with_partition_const") - tk.MustExec("create database test_db_with_partition_const") - tk.MustExec("use test_db_with_partition_const") - - sql1 := `create table t1 ( id int ) - partition by range(4) ( - partition p1 values less than (10) - );` - tk.MustGetErrCode(sql1, errno.ErrWrongExprInPartitionFunc) - - sql2 := `create table t2 ( time_recorded timestamp ) - partition by range(TO_DAYS(time_recorded)) ( - partition p1 values less than (1559192604) - );` - tk.MustGetErrCode(sql2, errno.ErrWrongExprInPartitionFunc) - - sql3 := `create table t3 ( id int ) - partition by range(DAY(id)) ( - partition p1 values less than (1) - );` - tk.MustGetErrCode(sql3, errno.ErrWrongExprInPartitionFunc) - - sql4 := `create table t4 ( id int ) - partition by hash(4) partitions 4 - ;` - tk.MustGetErrCode(sql4, errno.ErrWrongExprInPartitionFunc) - - sql5 := `create table t5 ( time_recorded timestamp ) - partition by range(to_seconds(time_recorded)) ( - partition p1 values less than (1559192604) - );` - tk.MustGetErrCode(sql5, errno.ErrWrongExprInPartitionFunc) - - sql6 := `create table t6 ( id int ) - partition by range(to_seconds(id)) ( - partition p1 values less than (1559192604) - );` - tk.MustGetErrCode(sql6, errno.ErrWrongExprInPartitionFunc) - - sql7 := `create table t7 ( time_recorded timestamp ) - partition by range(abs(time_recorded)) ( - partition p1 values less than (1559192604) - );` - tk.MustGetErrCode(sql7, errno.ErrWrongExprInPartitionFunc) - - sql8 := `create table t2332 ( time_recorded time ) - partition by range(TO_DAYS(time_recorded)) ( - partition p0 values less than (1) - );` - tk.MustGetErrCode(sql8, errno.ErrWrongExprInPartitionFunc) - - sql9 := `create table t1 ( id int ) - partition by hash(4) partitions 4;` - tk.MustGetErrCode(sql9, errno.ErrWrongExprInPartitionFunc) - - sql10 := `create table t1 ( id int ) - partition by hash(ed) partitions 4;` - tk.MustGetErrCode(sql10, errno.ErrBadField) - - sql11 := `create table t2332 ( time_recorded time ) - partition by range(TO_SECONDS(time_recorded)) ( - partition p0 values less than (1) - );` - tk.MustGetErrCode(sql11, errno.ErrWrongExprInPartitionFunc) - - sql12 := `create table t2332 ( time_recorded time ) - partition by range(TO_SECONDS(time_recorded)) ( - partition p0 values less than (1) - );` - tk.MustGetErrCode(sql12, errno.ErrWrongExprInPartitionFunc) - - sql13 := `create table t2332 ( time_recorded time ) - partition by range(day(time_recorded)) ( - partition p0 values less than (1) - );` - tk.MustGetErrCode(sql13, errno.ErrWrongExprInPartitionFunc) - - sql14 := `create table t2332 ( time_recorded timestamp ) - partition by range(day(time_recorded)) ( - partition p0 values less than (1) - );` - tk.MustGetErrCode(sql14, errno.ErrWrongExprInPartitionFunc) -} - -func TestConstAndTimezoneDepent2(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - // add partition - tk.MustExec("set @@session.tidb_enable_table_partition = 1") - tk.MustExec("drop database if exists test_db_with_partition_const") - tk.MustExec("create database test_db_with_partition_const") - tk.MustExec("use test_db_with_partition_const") - - tk.MustExec(`create table t1 ( time_recorded datetime ) - partition by range(TO_DAYS(time_recorded)) ( - partition p0 values less than (1));`) - - tk.MustExec(`create table t2 ( time_recorded date ) - partition by range(TO_DAYS(time_recorded)) ( - partition p0 values less than (1));`) - - tk.MustExec(`create table t3 ( time_recorded date ) - partition by range(TO_SECONDS(time_recorded)) ( - partition p0 values less than (1));`) - - tk.MustExec(`create table t4 ( time_recorded date ) - partition by range(TO_SECONDS(time_recorded)) ( - partition p0 values less than (1));`) - - tk.MustExec(`create table t5 ( time_recorded timestamp ) - partition by range(unix_timestamp(time_recorded)) ( - partition p1 values less than (1559192604) - );`) -} - -func TestUnsupportedPartitionManagementDDLs(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists test_1465;") - tk.MustExec(` - create table test_1465 (a int) - partition by range(a) ( - partition p1 values less than (10), - partition p2 values less than (20), - partition p3 values less than (30) - ); - `) - - tk.MustExec(`alter table test_1465 truncate partition p1`) - tk.MustContainErrMsg(`alter table test_1465 check partition p1`, "[ddl:8200]Unsupported check partition") - tk.MustContainErrMsg(`alter table test_1465 optimize partition p1`, "[ddl:8200]Unsupported optimize partition") - tk.MustContainErrMsg(`alter table test_1465 repair partition p1`, "[ddl:8200]Unsupported repair partition") - tk.MustContainErrMsg(`alter table test_1465 import partition p1 tablespace`, "[ddl:8200]Unsupported Unsupported/unknown ALTER TABLE specification") - tk.MustContainErrMsg(`alter table test_1465 discard partition p1 tablespace`, "[ddl:8200]Unsupported Unsupported/unknown ALTER TABLE specification") - tk.MustContainErrMsg(`alter table test_1465 rebuild partition p1`, "[ddl:8200]Unsupported rebuild partition") - tk.MustContainErrMsg(`alter table test_1465 coalesce partition 1`, "[ddl:1509]COALESCE PARTITION can only be used on HASH/KEY partitions") - tk.MustExec("alter table test_1465 partition by hash(a)") - tk.MustQuery(`show create table test_1465`).Check(testkit.Rows("" + - "test_1465 CREATE TABLE `test_1465` (\n" + - " `a` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY HASH (`a`) PARTITIONS 1")) - tk.MustContainErrMsg(`alter table test_1465 drop partition p0`, "[ddl:1512]DROP PARTITION can only be used on RANGE/LIST partitions") -} - func TestCommitWhenSchemaChange(t *testing.T) { restore := config.RestoreFunc() defer restore() @@ -5082,169 +3094,20 @@ func TestCommitWhenSchemaChange(t *testing.T) { tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) tk.MustExec("admin check table pt") - tk.MustQuery("select * from pt").Check(testkit.Rows()) - tk.MustExec("admin check table nt") - tk.MustQuery("select * from nt").Check(testkit.Rows()) - - tk.MustExec("begin") - tk.MustExec("insert into pt values (1), (3), (5);") - tk2.MustExec("alter table pt exchange partition p1 with table nt;") - tk.MustExec("insert into pt values (7), (9);") - tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) - - tk.MustExec("admin check table pt") - tk.MustQuery("select * from pt").Check(testkit.Rows()) - tk.MustExec("admin check table nt") - tk.MustQuery("select * from nt").Check(testkit.Rows()) -} - -func TestCreatePartitionTableWithWrongType(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - var err error - tk.MustGetDBError(`create table t( - b int(10) - ) partition by range columns (b) ( - partition p0 values less than (0x10), - partition p3 values less than (0x20) - )`, dbterror.ErrWrongTypeColumnValue) - - tk.MustGetDBError(`create table t( - b int(10) - ) partition by range columns (b) ( - partition p0 values less than ('g'), - partition p3 values less than ('k') - )`, dbterror.ErrWrongTypeColumnValue) - - tk.MustGetDBError(`create table t( - b char(10) - ) partition by range columns (b) ( - partition p0 values less than (30), - partition p3 values less than (60) - )`, dbterror.ErrWrongTypeColumnValue) - - err = tk.ExecToErr(`create table t( - b datetime - ) partition by range columns (b) ( - partition p0 values less than ('g'), - partition p3 values less than ('m') - )`) - require.Error(t, err) -} - -func TestAddPartitionForTableWithWrongType(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop tables if exists t_int, t_char, t_date") - tk.MustExec(`create table t_int(b int(10)) - partition by range columns (b) ( - partition p0 values less than (10) - )`) - - tk.MustExec(`create table t_char(b char(10)) - partition by range columns (b) ( - partition p0 values less than ('a') - )`) - - tk.MustExec(`create table t_date(b datetime) - partition by range columns (b) ( - partition p0 values less than ('2020-09-01') - )`) - - tk.MustGetDBError("alter table t_int add partition (partition p1 values less than ('g'))", dbterror.ErrWrongTypeColumnValue) - tk.MustGetDBError("alter table t_int add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) - tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (0x20))", dbterror.ErrRangeNotIncreasing) - tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (10))", dbterror.ErrWrongTypeColumnValue) - tk.MustGetDBError("alter table t_date add partition (partition p1 values less than ('m'))", dbterror.ErrWrongTypeColumnValue) - tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) - tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (20))", dbterror.ErrWrongTypeColumnValue) -} - -func TestPartitionListWithTimeType(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustExec("create table t_list1(a date) partition by list columns (a) (partition p0 values in ('2010-02-02', '20180203'), partition p1 values in ('20200202'));") - tk.MustExec("insert into t_list1(a) values (20180203);") - tk.MustQuery(`select * from t_list1 partition (p0);`).Check(testkit.Rows("2018-02-03")) -} - -func TestPartitionListWithNewCollation(t *testing.T) { - failpoint.Enable("github.com/pingcap/tidb/planner/core/forceDynamicPrune", `return(true)`) - defer failpoint.Disable("github.com/pingcap/tidb/planner/core/forceDynamicPrune") - - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustGetErrCode(`create table t (a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'A'));`, mysql.ErrMultipleDefConstInListPart) - tk.MustExec("create table t11(a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'b'), partition p1 values in ('C', 'D'));") - tk.MustExec("insert into t11(a) values ('A'), ('c'), ('C'), ('d'), ('B');") - tk.MustQuery(`select * from t11 order by a;`).Check(testkit.Rows("A", "B", "c", "C", "d")) - tk.MustQuery(`select * from t11 partition (p0);`).Check(testkit.Rows("A", "B")) - tk.MustQuery(`select * from t11 partition (p1);`).Check(testkit.Rows("c", "C", "d")) - str := tk.MustQuery(`desc select * from t11 where a = 'b';`).Rows()[0][3].(string) - require.True(t, strings.Contains(str, "partition:p0")) -} + tk.MustQuery("select * from pt").Check(testkit.Rows()) + tk.MustExec("admin check table nt") + tk.MustQuery("select * from nt").Check(testkit.Rows()) -func TestAddTableWithPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - // for global temporary table - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists global_partition_table;") - tk.MustGetErrCode("create global temporary table global_partition_table (a int, b int) partition by hash(a) partitions 3 ON COMMIT DELETE ROWS;", errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists global_partition_table;") - tk.MustExec("drop table if exists partition_table;") - _, err := tk.Exec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") - require.NoError(t, err) - tk.MustExec("drop table if exists partition_table;") - tk.MustExec("drop table if exists partition_range_table;") - tk.MustGetErrCode(`create global temporary table partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( - partition p0 values less than (10), - partition p1 values less than (20), - partition p2 values less than (30), - partition p3 values less than (MAXVALUE) - ) ON COMMIT DELETE ROWS;`, errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists partition_range_table;") - tk.MustExec("drop table if exists partition_list_table;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustGetErrCode(`create global temporary table partition_list_table (id int) partition by list (id) ( - partition p0 values in (1,2), - partition p1 values in (3,4), - partition p3 values in (5,null) - ) ON COMMIT DELETE ROWS;`, errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists partition_list_table;") + tk.MustExec("begin") + tk.MustExec("insert into pt values (1), (3), (5);") + tk2.MustExec("alter table pt exchange partition p1 with table nt;") + tk.MustExec("insert into pt values (7), (9);") + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) - // for local temporary table - tk.MustExec("use test;") - tk.MustExec("drop table if exists local_partition_table;") - tk.MustGetErrCode("create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3;", errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists local_partition_table;") - tk.MustExec("drop table if exists partition_table;") - tk.MustExec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") - tk.MustExec("drop table if exists partition_table;") - tk.MustExec("drop table if exists local_partition_range_table;") - tk.MustGetErrCode(`create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( - partition p0 values less than (10), - partition p1 values less than (20), - partition p2 values less than (30), - partition p3 values less than (MAXVALUE) - );`, errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists local_partition_range_table;") - tk.MustExec("drop table if exists local_partition_list_table;") - tk.MustExec("set @@session.tidb_enable_list_partition = ON") - tk.MustGetErrCode(`create temporary table local_partition_list_table (id int) partition by list (id) ( - partition p0 values in (1,2), - partition p1 values in (3,4), - partition p3 values in (5,null) - );`, errno.ErrPartitionNoTemporary) - tk.MustExec("drop table if exists local_partition_list_table;") + tk.MustExec("admin check table pt") + tk.MustQuery("select * from pt").Check(testkit.Rows()) + tk.MustExec("admin check table nt") + tk.MustQuery("select * from nt").Check(testkit.Rows()) } func TestTruncatePartitionMultipleTimes(t *testing.T) { @@ -5442,733 +3305,6 @@ func TestReorgPartitionTiFlash(t *testing.T) { } } -func TestDuplicatePartitionNames(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("create database DuplicatePartitionNames") - defer tk.MustExec("drop database DuplicatePartitionNames") - tk.MustExec("use DuplicatePartitionNames") - - tk.MustExec("set @@tidb_enable_list_partition=on") - tk.MustExec("create table t1 (a int) partition by list (a) (partition p1 values in (1), partition p2 values in (2), partition p3 values in (3))") - tk.MustExec("insert into t1 values (1),(2),(3)") - tk.MustExec("alter table t1 truncate partition p1,p1") - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows("2", "3")) - tk.MustExec("insert into t1 values (1)") - err := tk.ExecToErr("alter table t1 drop partition p1,p1") - require.Error(t, err) - require.Equal(t, "[ddl:1507]Error in list of partitions to DROP", err.Error()) - err = tk.ExecToErr("alter table t1 drop partition p1,p9") - require.Error(t, err) - require.Equal(t, "[ddl:1507]Error in list of partitions to DROP", err.Error()) - err = tk.ExecToErr("alter table t1 drop partition p1,p1,p1") - require.Error(t, err) - require.Equal(t, "[ddl:1508]Cannot remove all partitions, use DROP TABLE instead", err.Error()) - err = tk.ExecToErr("alter table t1 drop partition p1,p9,p1") - require.Error(t, err) - require.Equal(t, "[ddl:1508]Cannot remove all partitions, use DROP TABLE instead", err.Error()) - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows("1", "2", "3")) - tk.MustExec("alter table t1 drop partition p1") - tk.MustQuery("select * from t1").Sort().Check(testkit.Rows("2", "3")) - tk.MustQuery("Show create table t1").Check(testkit.Rows("" + - "t1 CREATE TABLE `t1` (\n" + - " `a` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST (`a`)\n" + - "(PARTITION `p2` VALUES IN (2),\n" + - " PARTITION `p3` VALUES IN (3))")) -} - -func checkCreateSyntax(t *testing.T, tk *testkit.TestKit, ok bool, sql, showCreate string) { - for i, sqlStmt := range []string{sql, showCreate} { - _, err := tk.Exec(sqlStmt) - // ignore warnings for now - if !ok { - require.Error(t, err, "sql: %s", sql) - // If not ok, no need to check anything else - return - } - require.NoError(t, err, "%d sql: %s", i, sql) - res := tk.MustQuery("show create table t") - require.Equal(t, showCreate, res.Rows()[0][1], "Compatible! (%d) sql: %s", i, sqlStmt) - tk.MustExec("drop table t") - } -} - -func TestCreateIntervalPartitionSyntax(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("create database IntervalPartitionSyntax") - defer tk.MustExec("drop database IntervalPartitionSyntax") - tk.MustExec("use IntervalPartitionSyntax") - - type testCase struct { - sql string - ok bool - showCreate string - } - - cases := []testCase{ - - { - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808),\n" + - " PARTITION `p_0` VALUES LESS THAN (0),\n" + - " PARTITION `p_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `p_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `p_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `p_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `p_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `p_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `p_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `p_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `p_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE))", - true, - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808),\n" + - " PARTITION `p_0` VALUES LESS THAN (0),\n" + - " PARTITION `p_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `p_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `p_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `p_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `p_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `p_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `p_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `p_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `p_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE))", - }, - - { - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `pNull` VALUES LESS THAN (-2147483648),\n" + - " PARTITION `p_0` VALUES LESS THAN (0),\n" + - " PARTITION `p_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `p_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `p_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `p_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `p_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `p_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `p_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `p_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `p_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE))", - true, - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `pNull` VALUES LESS THAN (-2147483648),\n" + - " PARTITION `p_0` VALUES LESS THAN (0),\n" + - " PARTITION `p_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `p_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `p_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `p_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `p_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `p_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `p_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `p_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `p_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE))", - }, - - { - "create table t (id int) partition by range (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition", - true, - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_NULL` VALUES LESS THAN (-9223372036854775808),\n" + - " PARTITION `P_LT_0` VALUES LESS THAN (0),\n" + - " PARTITION `P_LT_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `P_LT_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `P_LT_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `P_LT_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `P_LT_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `P_LT_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `P_LT_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `P_LT_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `P_LT_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE))", - }, - { - "create table t (id int) partition by range columns (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition", - true, - "CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `P_NULL` VALUES LESS THAN (-2147483648),\n" + - " PARTITION `P_LT_0` VALUES LESS THAN (0),\n" + - " PARTITION `P_LT_10000000` VALUES LESS THAN (10000000),\n" + - " PARTITION `P_LT_20000000` VALUES LESS THAN (20000000),\n" + - " PARTITION `P_LT_30000000` VALUES LESS THAN (30000000),\n" + - " PARTITION `P_LT_40000000` VALUES LESS THAN (40000000),\n" + - " PARTITION `P_LT_50000000` VALUES LESS THAN (50000000),\n" + - " PARTITION `P_LT_60000000` VALUES LESS THAN (60000000),\n" + - " PARTITION `P_LT_70000000` VALUES LESS THAN (70000000),\n" + - " PARTITION `P_LT_80000000` VALUES LESS THAN (80000000),\n" + - " PARTITION `P_LT_90000000` VALUES LESS THAN (90000000),\n" + - " PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE))", - }, - } - for _, tt := range cases { - checkCreateSyntax(t, tk, tt.ok, tt.sql, tt.showCreate) - } -} - -func TestCreateAndAlterIntervalPartition(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("create database IntervalPartition") - defer tk.MustExec("drop database IntervalPartition") - tk.MustExec("use IntervalPartition") - tk.MustExec("create table ipt (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90) MAXVALUE PARTITION") - tk.MustExec("insert into ipt values (0, '0'), (1, '1'), (2, '2')") - tk.MustExec("insert into ipt select id + 10, concat('1', val) FROM ipt") - tk.MustExec("insert into ipt select id + 20, concat('2', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 30, concat('3', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 40, concat('4', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 50, concat('5', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 60, concat('6', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 70, concat('7', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 80, concat('8', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 90, concat('9', val) FROM ipt where id < 10") - tk.MustExec("insert into ipt select id + 100, concat('10', val) FROM ipt where id < 10") - tk.MustQuery("SHOW CREATE TABLE ipt").Check(testkit.Rows( - "ipt CREATE TABLE `ipt` (\n" + - " `id` bigint(20) unsigned NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `val` (`val`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_LT_10` VALUES LESS THAN (10),\n" + - " PARTITION `P_LT_20` VALUES LESS THAN (20),\n" + - " PARTITION `P_LT_30` VALUES LESS THAN (30),\n" + - " PARTITION `P_LT_40` VALUES LESS THAN (40),\n" + - " PARTITION `P_LT_50` VALUES LESS THAN (50),\n" + - " PARTITION `P_LT_60` VALUES LESS THAN (60),\n" + - " PARTITION `P_LT_70` VALUES LESS THAN (70),\n" + - " PARTITION `P_LT_80` VALUES LESS THAN (80),\n" + - " PARTITION `P_LT_90` VALUES LESS THAN (90),\n" + - " PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE))")) - - err := tk.ExecToErr("alter table ipt LAST partition less than (100)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported LAST PARTITION when MAXVALUE partition exists", err.Error()) - - tk.MustExec("alter table ipt first partition less than (30)") - tk.MustQuery("select count(*) from ipt").Check(testkit.Rows("27")) - tk.MustQuery("SHOW CREATE TABLE ipt").Check(testkit.Rows( - "ipt CREATE TABLE `ipt` (\n" + - " `id` bigint(20) unsigned NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `val` (`val`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_LT_30` VALUES LESS THAN (30),\n" + - " PARTITION `P_LT_40` VALUES LESS THAN (40),\n" + - " PARTITION `P_LT_50` VALUES LESS THAN (50),\n" + - " PARTITION `P_LT_60` VALUES LESS THAN (60),\n" + - " PARTITION `P_LT_70` VALUES LESS THAN (70),\n" + - " PARTITION `P_LT_80` VALUES LESS THAN (80),\n" + - " PARTITION `P_LT_90` VALUES LESS THAN (90),\n" + - " PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE))")) - - err = tk.ExecToErr("alter table ipt merge first partition less than (60)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported MERGE FIRST PARTITION", err.Error()) - - err = tk.ExecToErr("alter table ipt split maxvalue partition less than (140)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported SPLIT LAST PARTITION", err.Error()) - - tk.MustQuery("select count(*) from ipt").Check(testkit.Rows("27")) - - tk.MustExec("create table idpt (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 week) FIRST PARTITION LESS THAN ('2022-02-01') LAST PARTITION LESS THAN ('2022-03-29') NULL PARTITION MAXVALUE PARTITION") - tk.MustQuery("SHOW CREATE TABLE idpt").Check(testkit.Rows( - "idpt CREATE TABLE `idpt` (\n" + - " `id` date NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " KEY `val` (`val`),\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `P_NULL` VALUES LESS THAN ('0000-01-01'),\n" + - " PARTITION `P_LT_2022-02-01` VALUES LESS THAN ('2022-02-01'),\n" + - " PARTITION `P_LT_2022-02-08` VALUES LESS THAN ('2022-02-08'),\n" + - " PARTITION `P_LT_2022-02-15` VALUES LESS THAN ('2022-02-15'),\n" + - " PARTITION `P_LT_2022-02-22` VALUES LESS THAN ('2022-02-22'),\n" + - " PARTITION `P_LT_2022-03-01` VALUES LESS THAN ('2022-03-01'),\n" + - " PARTITION `P_LT_2022-03-08` VALUES LESS THAN ('2022-03-08'),\n" + - " PARTITION `P_LT_2022-03-15` VALUES LESS THAN ('2022-03-15'),\n" + - " PARTITION `P_LT_2022-03-22` VALUES LESS THAN ('2022-03-22'),\n" + - " PARTITION `P_LT_2022-03-29` VALUES LESS THAN ('2022-03-29'),\n" + - " PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE))")) - - // Notice that '2022-01-31' + INTERVAL n MONTH returns '2022-02-28', '2022-03-31' etc. - // So having a range of the last of the month (normally what you want is LESS THAN first of the months) will work - // if using a month with 31 days. - // But managing partitions with the day-part of 29, 30 or 31 will be troublesome, since once the FIRST is not 31 - // both the ALTER TABLE t FIRST PARTITION and MERGE FIRST PARTITION will have issues - tk.MustExec("create table t (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 MONTH) FIRST PARTITION LESS THAN ('2022-01-31') LAST PARTITION LESS THAN ('2022-05-31')") - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` date NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " KEY `val` (`val`),\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `P_LT_2022-01-31` VALUES LESS THAN ('2022-01-31'),\n" + - " PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'),\n" + - " PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'),\n" + - " PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'),\n" + - " PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31'))")) - tk.MustExec("alter table t first partition less than ('2022-02-28')") - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` date NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " KEY `val` (`val`),\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'),\n" + - " PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'),\n" + - " PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'),\n" + - " PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31'))")) - // Now we are stuck, since we will use the current FIRST PARTITION to check the INTERVAL! - // Should we check and limit FIRST PARTITION for QUARTER and MONTH to not have day part in (29,30,31)? - err = tk.ExecToErr("alter table t first partition less than ('2022-03-31')") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - err = tk.ExecToErr("alter table t last partition less than ('2022-06-30')") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - err = tk.ExecToErr("alter table t last partition less than ('2022-07-31')") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` date NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " KEY `val` (`val`),\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`id`)\n" + - "(PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'),\n" + - " PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'),\n" + - " PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'),\n" + - " PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31'))")) - tk.MustExec("drop table t") - - tk.MustExec("create table t2 (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90)") - tk.MustExec("alter table t2 first partition less than (20)") - tk.MustExec("alter table t2 LAST partition less than (110)") - err = tk.ExecToErr("alter table t2 merge first partition less than (60)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported MERGE FIRST PARTITION", err.Error()) - - err = tk.ExecToErr("alter table t2 split maxvalue partition less than (140)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported SPLIT LAST PARTITION", err.Error()) - - tk.MustQuery("show create table t2").Check(testkit.Rows( - "t2 CREATE TABLE `t2` (\n" + - " `id` bigint(20) unsigned NOT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" + - " KEY `val` (`val`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_LT_20` VALUES LESS THAN (20),\n" + - " PARTITION `P_LT_30` VALUES LESS THAN (30),\n" + - " PARTITION `P_LT_40` VALUES LESS THAN (40),\n" + - " PARTITION `P_LT_50` VALUES LESS THAN (50),\n" + - " PARTITION `P_LT_60` VALUES LESS THAN (60),\n" + - " PARTITION `P_LT_70` VALUES LESS THAN (70),\n" + - " PARTITION `P_LT_80` VALUES LESS THAN (80),\n" + - " PARTITION `P_LT_90` VALUES LESS THAN (90),\n" + - " PARTITION `P_LT_100` VALUES LESS THAN (100),\n" + - " PARTITION `P_LT_110` VALUES LESS THAN (110))")) - err = tk.ExecToErr("alter table t2 first partition less than (20)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported FIRST PARTITION, given value does not generate a list of partition names to be dropped", err.Error()) - err = tk.ExecToErr("alter table t2 first partition less than (10)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr (10) not matching FIRST + n INTERVALs (20 + n * 10)", err.Error()) - err = tk.ExecToErr("alter table t2 last partition less than (110)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr (110) not matching FIRST + n INTERVALs (110 + n * 10)", err.Error()) - err = tk.ExecToErr("alter table t2 last partition less than (100)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr (100) not matching FIRST + n INTERVALs (110 + n * 10)", err.Error()) - tk.MustExec("drop table t2") - - err = tk.ExecToErr("create table t (id timestamp, val varchar(255)) partition by range columns (id) interval (1 minute) first partition less than ('2022-01-01 00:01:00') last partition less than ('2022-01-01 01:00:00')") - require.Error(t, err) - require.Equal(t, "[ddl:1659]Field 'id' is of a not allowed type for this type of partitioning", err.Error()) - err = tk.ExecToErr("create table t (id timestamp, val varchar(255)) partition by range (TO_SECONDS(id)) interval (3600) first partition less than (TO_SECONDS('2022-01-01 00:00:00')) last partition less than ('2022-01-02 00:00:00')") - require.Error(t, err) - require.Equal(t, "[ddl:1486]Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed", err.Error()) - tk.MustExec("set @@time_zone = 'Europe/Amsterdam'") - tk.MustExec("create table t (id timestamp, val varchar(255)) partition by range (unix_timestamp(id)) interval (3600) first partition less than (unix_timestamp('2022-01-01 00:00:00')) last partition less than (unix_timestamp('2022-01-02 00:00:00'))") - tk.MustExec("set @@time_zone = default") - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` timestamp NULL DEFAULT NULL,\n" + - " `val` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (UNIX_TIMESTAMP(`id`))\n" + - "(PARTITION `P_LT_1640991600` VALUES LESS THAN (1640991600),\n" + - " PARTITION `P_LT_1640995200` VALUES LESS THAN (1640995200),\n" + - " PARTITION `P_LT_1640998800` VALUES LESS THAN (1640998800),\n" + - " PARTITION `P_LT_1641002400` VALUES LESS THAN (1641002400),\n" + - " PARTITION `P_LT_1641006000` VALUES LESS THAN (1641006000),\n" + - " PARTITION `P_LT_1641009600` VALUES LESS THAN (1641009600),\n" + - " PARTITION `P_LT_1641013200` VALUES LESS THAN (1641013200),\n" + - " PARTITION `P_LT_1641016800` VALUES LESS THAN (1641016800),\n" + - " PARTITION `P_LT_1641020400` VALUES LESS THAN (1641020400),\n" + - " PARTITION `P_LT_1641024000` VALUES LESS THAN (1641024000),\n" + - " PARTITION `P_LT_1641027600` VALUES LESS THAN (1641027600),\n" + - " PARTITION `P_LT_1641031200` VALUES LESS THAN (1641031200),\n" + - " PARTITION `P_LT_1641034800` VALUES LESS THAN (1641034800),\n" + - " PARTITION `P_LT_1641038400` VALUES LESS THAN (1641038400),\n" + - " PARTITION `P_LT_1641042000` VALUES LESS THAN (1641042000),\n" + - " PARTITION `P_LT_1641045600` VALUES LESS THAN (1641045600),\n" + - " PARTITION `P_LT_1641049200` VALUES LESS THAN (1641049200),\n" + - " PARTITION `P_LT_1641052800` VALUES LESS THAN (1641052800),\n" + - " PARTITION `P_LT_1641056400` VALUES LESS THAN (1641056400),\n" + - " PARTITION `P_LT_1641060000` VALUES LESS THAN (1641060000),\n" + - " PARTITION `P_LT_1641063600` VALUES LESS THAN (1641063600),\n" + - " PARTITION `P_LT_1641067200` VALUES LESS THAN (1641067200),\n" + - " PARTITION `P_LT_1641070800` VALUES LESS THAN (1641070800),\n" + - " PARTITION `P_LT_1641074400` VALUES LESS THAN (1641074400),\n" + - " PARTITION `P_LT_1641078000` VALUES LESS THAN (1641078000))")) - tk.MustExec("alter table t drop partition P_LT_1640995200") - - tk.MustExec("drop table t") - - // OK with out-of-range partitions, see https://github.com/pingcap/tidb/issues/36022 - tk.MustExec("create table t (id tinyint, val varchar(255)) partition by range (id) interval (50) first partition less than (-300) last partition less than (300)") - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` tinyint(4) DEFAULT NULL,\n" + - " `val` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_LT_-300` VALUES LESS THAN (-300),\n" + - " PARTITION `P_LT_-250` VALUES LESS THAN (-250),\n" + - " PARTITION `P_LT_-200` VALUES LESS THAN (-200),\n" + - " PARTITION `P_LT_-150` VALUES LESS THAN (-150),\n" + - " PARTITION `P_LT_-100` VALUES LESS THAN (-100),\n" + - " PARTITION `P_LT_-50` VALUES LESS THAN (-50),\n" + - " PARTITION `P_LT_0` VALUES LESS THAN (0),\n" + - " PARTITION `P_LT_50` VALUES LESS THAN (50),\n" + - " PARTITION `P_LT_100` VALUES LESS THAN (100),\n" + - " PARTITION `P_LT_150` VALUES LESS THAN (150),\n" + - " PARTITION `P_LT_200` VALUES LESS THAN (200),\n" + - " PARTITION `P_LT_250` VALUES LESS THAN (250),\n" + - " PARTITION `P_LT_300` VALUES LESS THAN (300))")) - tk.MustExec("drop table t") - - err = tk.ExecToErr("create table t (id int unsigned, val float, comment varchar(255))" + - " partition by range columns (val) interval (1000 * 1000)" + - " first partition less than (0)" + - " last partition less than (100 * 1000 * 1000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:1659]Field 'val' is of a not allowed type for this type of partitioning", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range columns (val) interval (1000 * 1000)" + - " first partition less than ('0')" + - " last partition less than ('10000000') NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL partitioning, only supports Date, Datetime and INT types", err.Error()) - - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range columns (id) interval (-1000 * 1000)" + - " first partition less than (0)" + - " last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL, should be a positive number", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (0)" + - " first partition less than (0)" + - " last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL, should be a positive number", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval ('1000000')" + - " first partition less than (0)" + - " last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL, should be a positive number", err.Error()) - tk.MustExec("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (01000000)" + - " first partition less than (0)" + - " last partition less than (10000000) MAXVALUE PARTITION") - tk.MustExec("drop table t") - - // Null partition and first partition collides - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (01000000)" + - " first partition less than (0)" + - " last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:1493]VALUES LESS THAN value must be strictly increasing for each partition", err.Error()) - - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (NULL)" + - " first partition less than (0)" + - " last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL, should be a positive number", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (1000000)" + - " first partition less than (NULL)" + - " last partition less than (10000000)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL partitioning: Error when generating partition values", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (1000000)" + - " first partition less than (0)" + - " last partition less than (NULL)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr () not matching FIRST + n INTERVALs (0 + n * 1000000)", err.Error()) - tk.MustExec("create table t (id int, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (100)" + - " first partition less than (-1000)" + - " last partition less than (-1000)") - tk.MustQuery("show create table t").Check(testkit.Rows( - "t CREATE TABLE `t` (\n" + - " `id` int(11) DEFAULT NULL,\n" + - " `val` varchar(255) DEFAULT NULL,\n" + - " `comment` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`id`)\n" + - "(PARTITION `P_LT_-1000` VALUES LESS THAN (-1000))")) - err = tk.ExecToErr("alter table t last partition less than (0)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - tk.MustExec("alter table t add partition (partition `P_LT_0` values less than (-900))") - err = tk.ExecToErr("alter table t last partition less than (0)") - require.Error(t, err) - require.Equal(t, "[ddl:1517]Duplicate partition name P_LT_0", err.Error()) - tk.MustExec("drop table t") - err = tk.ExecToErr("create table t (id int, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (100)" + - " first partition less than (-100)" + - " last partition less than (250)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr (250) not matching FIRST + n INTERVALs (-100 + n * 100)", err.Error()) - err = tk.ExecToErr("create table t (id int unsigned, val varchar(255), comment varchar(255))" + - " partition by range (id) interval (33)" + - " first partition less than (100)" + - " last partition less than (67)") - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported INTERVAL: expr (67) not matching FIRST + n INTERVALs (100 + n * 33)", err.Error()) - - // Non-partitioned tables does not support ALTER of FIRST/LAST PARTITION - tk.MustExec(`create table t (a int, b varchar(255))`) - err = tk.ExecToErr(`ALTER TABLE t FIRST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:1505]Partition management on a not partitioned table is not possible", err.Error()) - err = tk.ExecToErr(`ALTER TABLE t LAST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:1505]Partition management on a not partitioned table is not possible", err.Error()) - tk.MustExec(`drop table t`) - // HASH/LIST [COLUMNS] does not support ALTER of FIRST/LAST PARTITION - tk.MustExec(`create table t (a int, b varchar(255)) partition by hash (a) partitions 4`) - err = tk.ExecToErr(`ALTER TABLE t FIRST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - err = tk.ExecToErr(`ALTER TABLE t LAST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported LAST PARTITION of HASH/KEY partitioned table", err.Error()) - tk.MustExec(`drop table t`) - - tk.MustExec(`create table t (a int, b varchar(255)) partition by list (a) (partition p0 values in (1,2,3), partition p1 values in (22,23,24))`) - err = tk.ExecToErr(`ALTER TABLE t FIRST PARTITION LESS THAN (0)`) - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - err = tk.ExecToErr(`ALTER TABLE t LAST PARTITION LESS THAN (100)`) - require.Error(t, err) - require.Equal(t, "[ddl:1492]For LIST partitions each partition must be defined", err.Error()) - tk.MustExec(`drop table t`) - - tk.MustExec(`create table t (a int, b varchar(255)) partition by list columns (b) (partition p0 values in ("1","2","3"), partition p1 values in ("22","23","24"))`) - err = tk.ExecToErr(`ALTER TABLE t FIRST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:8200]Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table", err.Error()) - err = tk.ExecToErr(`ALTER TABLE t LAST PARTITION LESS THAN (10)`) - require.Error(t, err) - require.Equal(t, "[ddl:1492]For LIST partitions each partition must be defined", err.Error()) - tk.MustExec(`drop table t`) -} - -func TestPartitionTableWithAnsiQuotes(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database partitionWithAnsiQuotes") - defer tk.MustExec("drop database partitionWithAnsiQuotes") - tk.MustExec("use partitionWithAnsiQuotes") - tk.MustExec("SET SESSION sql_mode='ANSI_QUOTES'") - - // Test single quotes. - tk.MustExec(`create table t(created_at datetime) PARTITION BY RANGE COLUMNS(created_at) ( - PARTITION p0 VALUES LESS THAN ('2021-12-01 00:00:00'), - PARTITION p1 VALUES LESS THAN ('2022-01-01 00:00:00'))`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"created_at\" datetime DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(\"created_at\")\n" + - "(PARTITION \"p0\" VALUES LESS THAN ('2021-12-01 00:00:00'),\n" + - " PARTITION \"p1\" VALUES LESS THAN ('2022-01-01 00:00:00'))")) - tk.MustExec("drop table t") - - // Test expression with single quotes. - tk.MustExec("set @@time_zone = 'Asia/Shanghai'") - tk.MustExec(`create table t(created_at timestamp) PARTITION BY RANGE (unix_timestamp(created_at)) ( - PARTITION p0 VALUES LESS THAN (unix_timestamp('2021-12-01 00:00:00')), - PARTITION p1 VALUES LESS THAN (unix_timestamp('2022-01-01 00:00:00')))`) - tk.MustExec("set @@time_zone = default") - // FIXME: should be "created_at" instead of `created_at`, see #35389. - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"created_at\" timestamp NULL DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (UNIX_TIMESTAMP(`created_at`))\n" + - "(PARTITION \"p0\" VALUES LESS THAN (1638288000),\n" + - " PARTITION \"p1\" VALUES LESS THAN (1640966400))")) - tk.MustExec("drop table t") - tk.MustExec("set @@time_zone = default") - - // Test values in. - tk.MustExec(`CREATE TABLE t (a int DEFAULT NULL, b varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a,b) ( - PARTITION p0 VALUES IN ((1,'1'),(2,'2')), - PARTITION p1 VALUES IN ((10,'10'),(11,'11')))`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" int(11) DEFAULT NULL,\n" + - " \"b\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(\"a\",\"b\")\n" + - "(PARTITION \"p0\" VALUES IN ((1,'1'),(2,'2')),\n" + - " PARTITION \"p1\" VALUES IN ((10,'10'),(11,'11')))")) - tk.MustExec("drop table t") - - // Test escaped characters in single quotes. - tk.MustExec(`CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( - PARTITION p0 VALUES IN ('\'','\'\'',''''''''), - PARTITION p1 VALUES IN ('""','\\','\\\'\t\n'))`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(\"a\")\n" + - `(PARTITION "p0" VALUES IN ('''','''''',''''''''),` + "\n" + - ` PARTITION "p1" VALUES IN ('""','\\','\\''\t\n'))`)) - tk.MustExec(`insert into t values (0x5c27090a),('\\''\t\n')`) - tk.MustExec("drop table t") - tk.MustExec(`CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( - PARTITION p0 VALUES IN ('\'','\'\'',''''''''), - PARTITION p1 VALUES IN ('\"\"','\\',0x5c27090a))`) - tk.MustExec(`insert into t values (0x5c27090a),('\\''\t\n')`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(\"a\")\n" + - `(PARTITION "p0" VALUES IN ('''','''''',''''''''),` + "\n" + - ` PARTITION "p1" VALUES IN ('""','\\',x'5c27090a'))`)) - tk.MustExec("drop table t") - tk.MustExec(`CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( - PARTITION p0 VALUES IN ('\'','\'\'',''''''''), - PARTITION p1 VALUES IN ('""','\\',x'5c27090a'))`) - tk.MustExec(`insert into t values (0x5c27090a),('\\''\t\n')`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY LIST COLUMNS(\"a\")\n" + - `(PARTITION "p0" VALUES IN ('''','''''',''''''''),` + "\n" + - ` PARTITION "p1" VALUES IN ('""','\\',x'5c27090a'))`)) - - // https://github.com/pingcap/tidb/issues/37692 - tk.MustExec("drop table t") - tk.MustExec(`CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( - PARTITION p0 VALUES LESS THAN ('"'), - PARTITION p1 VALUES LESS THAN ('""'), - PARTITION p2 VALUES LESS THAN ('\''), - PARTITION p3 VALUES LESS THAN (''''''), - PARTITION p4 VALUES LESS THAN ('\\''\t\n'), - PARTITION pMax VALUES LESS THAN (MAXVALUE))`) - //PARTITION p4 VALUES IN (x'5c27090a'))`) - tk.MustExec(`insert into t values (0x5c27090a),('\\''\t\n')`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - `PARTITION BY RANGE COLUMNS("a")` + "\n" + - `(PARTITION "p0" VALUES LESS THAN ('"'),` + "\n" + - ` PARTITION "p1" VALUES LESS THAN ('""'),` + "\n" + - ` PARTITION "p2" VALUES LESS THAN (''''),` + "\n" + - ` PARTITION "p3" VALUES LESS THAN (''''''),` + "\n" + - ` PARTITION "p4" VALUES LESS THAN ('\\''\t\n'),` + "\n" + - ` PARTITION "pMax" VALUES LESS THAN (MAXVALUE))`)) - tk.MustExec("drop table t") - tk.MustExec(`CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( - PARTITION p0 VALUES LESS THAN ('"'), - PARTITION p1 VALUES LESS THAN ('""'), - PARTITION p2 VALUES LESS THAN ('\''), - PARTITION p3 VALUES LESS THAN (''''''), - PARTITION p4 VALUES LESS THAN (0x5c27090a), - PARTITION pMax VALUES LESS THAN (MAXVALUE))`) - tk.MustExec(`insert into t values (0x5c27090a),('\\''\t\n')`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - `PARTITION BY RANGE COLUMNS("a")` + "\n" + - `(PARTITION "p0" VALUES LESS THAN ('"'),` + "\n" + - ` PARTITION "p1" VALUES LESS THAN ('""'),` + "\n" + - ` PARTITION "p2" VALUES LESS THAN (''''),` + "\n" + - ` PARTITION "p3" VALUES LESS THAN (''''''),` + "\n" + - ` PARTITION "p4" VALUES LESS THAN (x'5c27090a'),` + "\n" + - ` PARTITION "pMax" VALUES LESS THAN (MAXVALUE))`)) - - tk.MustExec("drop table t") - tk.MustExec(`CREATE TABLE t (a varchar(255), b varchar(255)) PARTITION BY RANGE COLUMNS(a,b) ( - PARTITION p0 VALUES LESS THAN ('"','"'), - PARTITION p1 VALUES LESS THAN ('""','""'), - PARTITION p2 VALUES LESS THAN ('\'','\''), - PARTITION p3 VALUES LESS THAN ('''''',''''''), - PARTITION p4 VALUES LESS THAN ('\\''\t\n',0x5c27090a), - PARTITION pMax VALUES LESS THAN (MAXVALUE,maxvalue))`) - tk.MustExec(`insert into t values (0x5c27090a,'\\''\t\n')`) - tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE \"t\" (\n" + - " \"a\" varchar(255) DEFAULT NULL,\n" + - " \"b\" varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - `PARTITION BY RANGE COLUMNS("a","b")` + "\n" + - `(PARTITION "p0" VALUES LESS THAN ('"','"'),` + "\n" + - ` PARTITION "p1" VALUES LESS THAN ('""','""'),` + "\n" + - ` PARTITION "p2" VALUES LESS THAN ('''',''''),` + "\n" + - ` PARTITION "p3" VALUES LESS THAN ('''''',''''''),` + "\n" + - ` PARTITION "p4" VALUES LESS THAN ('\\''\t\n','\\''\t\n'),` + "\n" + - ` PARTITION "pMax" VALUES LESS THAN (MAXVALUE,MAXVALUE))`)) -} - func TestIssue40135Ver2(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -6226,240 +3362,6 @@ func TestAlterModifyPartitionColTruncateWarning(t *testing.T) { tk.MustExec(`admin check table t`) } -func TestAlterModifyColumnOnPartitionedTableRename(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - schemaName := "modColPartRename" - tk.MustExec("create database " + schemaName) - tk.MustExec("use " + schemaName) - tk.MustExec(`create table t (a int, b char) partition by range (a) (partition p0 values less than (10))`) - tk.MustContainErrMsg(`alter table t change a c int`, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed") - tk.MustExec(`drop table t`) - tk.MustExec(`create table t (a char, b char) partition by range columns (a) (partition p0 values less than ('z'))`) - tk.MustContainErrMsg(`alter table t change a c char`, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed") - tk.MustExec(`drop table t`) - tk.MustExec(`create table t (a int, b char) partition by list (a) (partition p0 values in (10))`) - tk.MustContainErrMsg(`alter table t change a c int`, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed") - tk.MustExec(`drop table t`) - tk.MustExec(`create table t (a char, b char) partition by list columns (a) (partition p0 values in ('z'))`) - tk.MustContainErrMsg(`alter table t change a c char`, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed") - tk.MustExec(`drop table t`) - tk.MustExec(`create table t (a int, b char) partition by hash (a) partitions 3`) - tk.MustContainErrMsg(`alter table t change a c int`, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed") -} - -func TestDropPartitionKeyColumn(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database DropPartitionKeyColumn") - defer tk.MustExec("drop database DropPartitionKeyColumn") - tk.MustExec("use DropPartitionKeyColumn") - - tk.MustExec("create table t1 (a tinyint, b char) partition by range (a) ( partition p0 values less than (10) )") - err := tk.ExecToErr("alter table t1 drop column a") - require.Error(t, err) - require.Equal(t, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed", err.Error()) - tk.MustExec("alter table t1 drop column b") - - tk.MustExec("create table t2 (a tinyint, b char) partition by range (a-1) ( partition p0 values less than (10) )") - err = tk.ExecToErr("alter table t2 drop column a") - require.Error(t, err) - require.Equal(t, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed", err.Error()) - tk.MustExec("alter table t2 drop column b") - - tk.MustExec("create table t3 (a tinyint, b char) partition by hash(a) partitions 4;") - err = tk.ExecToErr("alter table t3 drop column a") - require.Error(t, err) - require.Equal(t, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed", err.Error()) - tk.MustExec("alter table t3 drop column b") - - tk.MustExec("create table t4 (a char, b char) partition by list columns (a) ( partition p0 values in ('0'), partition p1 values in ('a'), partition p2 values in ('b'));") - err = tk.ExecToErr("alter table t4 drop column a") - require.Error(t, err) - require.Equal(t, "[ddl:3855]Column 'a' has a partitioning function dependency and cannot be dropped or renamed", err.Error()) - tk.MustExec("alter table t4 drop column b") -} - -func TestRangeExpressions(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RangeExpr") - tk.MustExec("use RangeExpr") - tk.MustExec(`create table t6 (colint int, col1 date) -partition by range(colint) -(partition p0 values less than (extract(year from '1998-11-23')), -partition p1 values less than maxvalue)`) - tk.MustQuery(`show create table t6`).Check(testkit.Rows("" + - "t6 CREATE TABLE `t6` (\n" + - " `colint` int(11) DEFAULT NULL,\n" + - " `col1` date DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`colint`)\n" + - "(PARTITION `p0` VALUES LESS THAN (1998),\n" + - " PARTITION `p1` VALUES LESS THAN (MAXVALUE))")) -} - -func TestRemoveRangePartitioning(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RemovePartitioning") - tk.MustExec("use RemovePartitioning") - tk.MustExec(`create table tRange (a int unsigned primary key, b varchar(255)) - partition by range (a) - (partition p0 values less than (1000000), - partition pMax values less than maxvalue)`) - tk.MustExec(`insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000")`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) 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 `p0` VALUES LESS THAN (1000000),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`alter table tRange remove partitioning`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - - /* Test with nonclustered */ - tk.MustExec(`drop table tRange`) - tk.MustExec(`create table tRange (a int unsigned primary key nonclustered, b varchar(255)) -partition by range (a) -(partition p0 values less than (1000000), -partition pMax values less than maxvalue)`) - tk.MustExec(`insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000")`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN (1000000),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`alter table tRange remove partitioning`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - tk.MustExec(`insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002")`) - - /* Test with both auto_inc and non-clustered */ - tk.MustExec(`drop table tRange`) - tk.MustExec(`create table tRange (a int unsigned primary key nonclustered, b varchar(255)) -partition by range (a) -(partition p0 values less than (1000000), -partition pMax values less than maxvalue)`) - tk.MustExec(`insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000")`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE (`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN (1000000),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`alter table tRange remove partitioning`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` int(10) unsigned NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) - tk.MustExec(`insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002")`) -} - -func TestRemoveRangeColumnPartitioning(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RemovePartitioning") - tk.MustExec("use RemovePartitioning") - tk.MustExec(`create table tRange (a varchar(55) primary key, b varchar(255)) -partition by range columns (a) -(partition p0 values less than ("1000000"), -partition pMax values less than maxvalue)`) - tk.MustExec(`insert into tRange values ("0", "Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000")`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` varchar(55) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`a`)\n" + - "(PARTITION `p0` VALUES LESS THAN ('1000000'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE))")) - tk.MustExec(`alter table tRange remove partitioning`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` varchar(55) NOT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) -} - -func TestRemoveRangeColumnsPartitioning(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RemovePartitioning") - tk.MustExec("use RemovePartitioning") - tk.MustExec(`create table tRange (a varchar(55), b varchar(255)) -partition by range columns (a,b) -(partition p0 values less than ("1000000","1000000"), -partition pMax values less than (maxvalue,1))`) - tk.MustExec(`insert into tRange values ("0", "0Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000")`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` varchar(55) DEFAULT NULL,\n" + - " `b` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + - "PARTITION BY RANGE COLUMNS(`a`,`b`)\n" + - "(PARTITION `p0` VALUES LESS THAN ('1000000','1000000'),\n" + - " PARTITION `pMax` VALUES LESS THAN (MAXVALUE,'1'))")) - tk.MustExec(`alter table tRange remove partitioning`) - tk.MustQuery(`show create table tRange`).Check(testkit.Rows("" + - "tRange CREATE TABLE `tRange` (\n" + - " `a` varchar(55) DEFAULT NULL,\n" + - " `b` varchar(255) DEFAULT NULL\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) -} - -func TestRemoveHashPartitioning(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RemovePartitioning") - tk.MustExec("use RemovePartitioning") - tk.MustExec(`create table t (a int, b varchar(255), key (a,b), key (b)) partition by hash (a) partitions 7`) - for i := 32; i <= 126; i++ { - // Fill the data with int and ascii strings - tk.MustExec(fmt.Sprintf(`insert into t values (%d,char(%d,%d,%d,%d))`, i, i, i, i, i)) - } - tk.MustExec(`analyze table t`) - tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_schema = 'RemovePartitioning' and table_name = 't'`).Sort().Check(testkit.Rows(""+ - "p0 14", - "p1 13", - "p2 13", - "p3 13", - "p4 14", - "p5 14", - "p6 14")) - tk.MustExec(`alter table t remove partitioning`) - tk.MustQuery(`show create table t`).Check(testkit.Rows("" + - "t CREATE TABLE `t` (\n" + - " `a` int(11) DEFAULT NULL,\n" + - " `b` varchar(255) DEFAULT NULL,\n" + - " KEY `a` (`a`,`b`),\n" + - " KEY `b` (`b`)\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) -} - func TestRemoveKeyPartitioning(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) tk := testkit.NewTestKit(t, store) @@ -6743,130 +3645,4 @@ func TestRemovePartitioningAutoIDs(t *testing.T) { "32 31 10", "35 34 21", "38 37 22", "41 40 23")) } -func TestListDefinitionError(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database ListRange") - tk.MustExec("use ListRange") - tk.MustContainErrMsg(`create table t (a int) partition by list (a) (partition p2 values less than (2))`, "[ddl:1480]Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition") - tk.MustContainErrMsg(`create table t (a int) partition by list (a) (partition p2)`, "[ddl:1479]Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition") - tk.MustExec(`create table t (a int) partition by list (a) (partition p1 values in (1))`) - tk.MustContainErrMsg(`alter table t add partition (partition p2 values less than (2))`, "[ddl:1480]Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition") - tk.MustContainErrMsg(`alter table t add partition (partition p2)`, "[ddl:1479]Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition") -} - -func TestListExchangeValidate(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database ListExchange") - tk.MustExec("use ListExchange") - tk.MustExec("create table lcp (id int, create_ts datetime, name varchar(10))\n" + - "partition by list columns (create_ts)\n" + - "(partition p20230829 values in ('2023-08-29'),partition p20230830 values in ('2023-08-30'))") - tk.MustExec(`insert into lcp values (1,'2023-08-29','a')`) - tk.MustExec(`insert into lcp values (2,'2023-08-30','b')`) - tk.MustContainErrMsg(`insert into lcp values (3,'2023-08-31','c')`, - "[table:1526]Table has no partition for value from column_list") - - tk.MustExec(`create table t (id int, create_ts datetime, name varchar(10))`) - tk.MustExec(`insert into t values (3,'2023-08-31','c')`) - - tk.MustContainErrMsg(`alter table lcp EXCHANGE PARTITION p20230829 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lcp add partition - (partition p202302 values in ('2023-02-01','2023-02-28',null), - partition p202303 values in ('2023-03-01','2023-03-02','2023-03-31'))`) - tk.MustContainErrMsg(`alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`truncate table t`) - tk.MustExec(`insert into t values (4,'2023-02-01','d'), (5,'2023-02-28','e'), (6, null, 'f')`) - tk.MustContainErrMsg(`alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t`) - tk.MustExec(`insert into t values (4,'2023-03-01','d'), (5,'2023-03-02','e'), (6,'2023-03-31','f')`) - tk.MustContainErrMsg(`alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t`) - - tk.MustExec(`drop table t`) - tk.MustExec(`CREATE TABLE lmcp (d date, name varchar(10), data varchar(255)) - PARTITION BY LIST COLUMNS(d,name) - (partition p3 values IN (('2021-01-01','a'),('2021-01-02','b'),('2021-01-03','c')), - partition p4 values IN (('2021-01-01','b'),(null,'a'),('2021-01-01',null),(null,null)), - partition p2 values IN (('2021-01-01','c'),('2021-01-02','a')), - partition p1 values IN (('2021-01-02','c')))`) - tk.MustExec(`CREATE TABLE t (d date, name varchar(10), data varchar(255))`) - - tk.MustExec(`insert into t values ('2021-01-02', 'c', "OK")`) - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t`) - - tk.MustExec(`insert into t values ('2021-01-01', 'c', "OK"), ('2021-01-02', 'a', "OK")`) - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t`) - - tk.MustExec(`insert into t values ('2021-01-01', 'a', "OK"), ('2021-01-02','b', "OK"), ('2021-01-03','c', "OK")`) - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t`) - - tk.MustExec(`insert into t values ('2021-01-01', 'b', "OK"), ('2021-01-01',null, "OK"), (null,'a', "OK"), (null,null,"OK")`) - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustContainErrMsg(`alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t`) - - tk.MustExec(`create table lp (a int, data varchar(255)) partition by list (a) (partition p0 values in (0,4), partition pNull values in (null))`) - tk.MustExec(`create table np (a int, data varchar(255))`) - tk.MustExec(`insert into np values (0,"OK"), (4,"OK")`) - tk.MustContainErrMsg(`alter table lp EXCHANGE PARTITION pNull WITH TABLE np`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lp EXCHANGE PARTITION p0 WITH TABLE np`) - tk.MustExec(`insert into np values (null,"OK")`) - tk.MustContainErrMsg(`alter table lp EXCHANGE PARTITION p0 WITH TABLE np`, - "[ddl:1737]Found a row that does not match the partition") - tk.MustExec(`alter table lp EXCHANGE PARTITION pNull WITH TABLE np`) - // TODO: Check EXCHANGE with DEFAULT partition!! -} - -func TestRangeExchangeValidate(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database RangeExchange") - tk.MustExec("use RangeExchange") - tk.MustExec(`CREATE TABLE t (d date, name varchar(10), data varchar(255))`) - tk.MustExec("create table rcp (d date, name varchar(10), data varchar(255))\n" + - "partition by range columns (d)\n" + - "(partition p20230829 values less than ('2023-08-30'),partition p20230830 values less than ('2023-08-31'))") - tk.MustExec(`insert into rcp values ('2023-08-29', 'a', "OK")`) - tk.MustExec(`insert into rcp values ('2023-08-30', 'b', "OK")`) - tk.MustContainErrMsg(`insert into rcp values ('2023-08-31', 'c', "FAIL")`, - "[table:1526]Table has no partition for value from column_list") - tk.MustExec(`insert into t values ('2023-08-31', 'c', "FAIL")`) - tk.MustContainErrMsg(`alter table rcp EXCHANGE PARTITION p20230829 WITH TABLE t`, - "[ddl:1737]Found a row that does not match the partition") - // TODO: Add test with a RANGE single partition (both normal AND maxvalue!) - // TODO: add test with maxvalue (1, 2, and more partitions) - // TODO: add test not in first partition (both last without maxvalue and also not last with/without maxvalue) -} - // TODO: check EXCHANGE how it handles null (for all types of partitioning!!!) diff --git a/ddl/tests/resourcegroup/BUILD.bazel b/ddl/tests/resourcegroup/BUILD.bazel index c34f56c33a041..1c9950fc3a171 100644 --- a/ddl/tests/resourcegroup/BUILD.bazel +++ b/ddl/tests/resourcegroup/BUILD.bazel @@ -6,7 +6,7 @@ go_test( srcs = ["resource_group_test.go"], flaky = True, race = "on", - shard_count = 6, + shard_count = 5, deps = [ "//ddl/resourcegroup", "//ddl/util/callback", diff --git a/ddl/tests/resourcegroup/resource_group_test.go b/ddl/tests/resourcegroup/resource_group_test.go index 60396947b0d0f..c1796a3a6c72a 100644 --- a/ddl/tests/resourcegroup/resource_group_test.go +++ b/ddl/tests/resourcegroup/resource_group_test.go @@ -311,27 +311,6 @@ func TestResourceGroupRunaway(t *testing.T) { tk.MustGetErrCode("select /*+ resource_group(rg3) */ * from t", mysql.ErrResourceGroupQueryRunawayQuarantine) } -func TestResourceGroupHint(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("create table t1(c1 int)") - tk.MustExec("insert into t1 values(1)") - - tk.MustExec("set global tidb_enable_resource_control='on'") - tk.MustExec("create resource group rg1 ru_per_sec=1000") - tk.MustQuery("select /*+ resource_group(default) */ * from t1") - tk.MustQuery("select /*+ resource_group(rg1) */ * from t1") - tk.MustQuery("select /*+ resource_group(rg1) resource_group(default) */ * from t1") - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 RESOURCE_GROUP() is defined more than once, only the last definition takes effect: RESOURCE_GROUP(default)")) - tk.MustQuery("select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist").Check(testkit.Rows("test rg1")) - tk.MustQuery("select DB, RESOURCE_GROUP from information_schema.processlist").Check(testkit.Rows("test default")) - tk.MustExec("set global tidb_enable_resource_control='off'") - tk.MustQuery("select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist").Check(testkit.Rows("test default")) - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 8250 Resource control feature is disabled. Run `SET GLOBAL tidb_enable_resource_control='on'` to enable the feature")) -} - func TestAlreadyExistsDefaultResourceGroup(t *testing.T) { require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/managerAlreadyCreateSomeGroups", `return(true)`)) defer func() { diff --git a/ddl/tests/serial/BUILD.bazel b/ddl/tests/serial/BUILD.bazel index 6e4f7e52504ad..0f08135411e03 100644 --- a/ddl/tests/serial/BUILD.bazel +++ b/ddl/tests/serial/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "serial_test.go", ], flaky = True, - shard_count = 24, + shard_count = 20, deps = [ "//config", "//ddl", diff --git a/ddl/tests/serial/serial_test.go b/ddl/tests/serial/serial_test.go index d436d44a0087d..02e16f8545f49 100644 --- a/ddl/tests/serial/serial_test.go +++ b/ddl/tests/serial/serial_test.go @@ -57,16 +57,6 @@ func GetMaxRowID(store kv.Storage, priority int, t table.Table, startHandle, end return ddl.GetRangeEndKey(ddl.NewJobContext(), store, priority, t.RecordPrefix(), startHandle, endHandle) } -func TestTruncateAllPartitions(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table partition_table (v int) partition by hash (v) partitions 10") - tk.MustExec("insert into partition_table values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)") - tk.MustExec("alter table partition_table truncate partition all") - tk.MustQuery("select count(*) from partition_table").Check(testkit.Rows("0")) -} - func TestIssue23872(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -872,17 +862,6 @@ func TestTableLocksEnable(t *testing.T) { tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1235 UNLOCK TABLES is not supported. To enable this experimental feature, set 'enable-table-lock' in the configuration file.")) } -func TestAutoRandomOnTemporaryTable(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists auto_random_temporary") - err := tk.ExecToErr("create global temporary table auto_random_temporary (a bigint primary key auto_random(3), b varchar(255)) on commit delete rows") - require.Equal(t, core.ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random").Error(), err.Error()) - err = tk.ExecToErr("create temporary table t(a bigint key auto_random)") - require.Equal(t, core.ErrOptOnTemporaryTable.GenWithStackByArgs("auto_random").Error(), err.Error()) -} - func TestAutoRandom(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1121,32 +1100,6 @@ func TestAutoRandom(t *testing.T) { }) } -func TestAutoRandomWithRangeBits(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - // Test normal usages. - tk.MustExec("create table t (a bigint auto_random(5, 64) primary key, b int);") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint unsigned auto_random(5, 32) primary key, b int);") - - // Test create auto_random table with invalid range bits. - expectErr := dbterror.ErrInvalidAutoRandom - tk.MustExec("drop table if exists t;") - err := tk.ExecToErr("create table t (a bigint auto_random(5, 31) primary key, b int);") - require.EqualError(t, err, expectErr.FastGenByArgs(fmt.Sprintf(autoid.AutoRandomInvalidRangeBits, 32, 64, 31)).Error()) - err = tk.ExecToErr("create table t (a bigint auto_random(5, 65) primary key, b int);") - require.EqualError(t, err, expectErr.FastGenByArgs(fmt.Sprintf(autoid.AutoRandomInvalidRangeBits, 32, 64, 65)).Error()) - err = tk.ExecToErr("create table t (a bigint auto_random(15, 32) primary key, b int);") - require.EqualError(t, err, expectErr.FastGenByArgs(autoid.AutoRandomIncrementalBitsTooSmall).Error()) - - // Alter table range bits is not supported. - tk.MustExec("create table t (a bigint auto_random(5, 64) primary key, b int);") - err = tk.ExecToErr("alter table t modify column a bigint auto_random(5, 32);") - require.EqualError(t, err, expectErr.FastGenByArgs(autoid.AutoRandomUnsupportedAlterRangeBits).Error()) - tk.MustExec("alter table t modify column a bigint auto_random(15, 64);") -} - func TestAutoRandomWithPreSplitRegion(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -1184,40 +1137,6 @@ func TestAutoRandomWithPreSplitRegion(t *testing.T) { require.Equal(t, fmt.Sprintf("t_%d_r_3221225472", tbl.Meta().ID), rows[3][1]) } -func TestModifyingColumn4NewCollations(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("create database dct") - tk.MustExec("use dct") - tk.MustExec("create table t(b varchar(10) collate utf8_bin, c varchar(10) collate utf8_general_ci) collate utf8_bin") - // Column collation can be changed as long as there is no index defined. - tk.MustExec("alter table t modify b varchar(10) collate utf8_general_ci") - tk.MustExec("alter table t modify c varchar(10) collate utf8_bin") - tk.MustExec("alter table t modify c varchar(10) collate utf8_unicode_ci") - tk.MustExec("alter table t charset utf8 collate utf8_general_ci") - tk.MustExec("alter table t convert to charset utf8 collate utf8_bin") - tk.MustExec("alter table t convert to charset utf8 collate utf8_unicode_ci") - tk.MustExec("alter table t convert to charset utf8 collate utf8_general_ci") - tk.MustExec("alter table t modify b varchar(10) collate utf8_unicode_ci") - tk.MustExec("alter table t modify b varchar(10) collate utf8_bin") - - tk.MustExec("alter table t add index b_idx(b)") - tk.MustExec("alter table t add index c_idx(c)") - tk.MustGetErrMsg("alter table t modify b varchar(10) collate utf8_general_ci", "[ddl:8200]Unsupported modifying collation of column 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it.") - tk.MustGetErrMsg("alter table t modify c varchar(10) collate utf8_bin", "[ddl:8200]Unsupported modifying collation of column 'c' from 'utf8_general_ci' to 'utf8_bin' when index is defined on it.") - tk.MustGetErrMsg("alter table t modify c varchar(10) collate utf8_unicode_ci", "[ddl:8200]Unsupported modifying collation of column 'c' from 'utf8_general_ci' to 'utf8_unicode_ci' when index is defined on it.") - tk.MustGetErrMsg("alter table t convert to charset utf8 collate utf8_general_ci", "[ddl:8200]Unsupported converting collation of column 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it.") - // Change to a compatible collation is allowed. - tk.MustExec("alter table t modify c varchar(10) collate utf8mb4_general_ci") - // Change the default collation of table is allowed. - tk.MustExec("alter table t collate utf8mb4_general_ci") - tk.MustExec("alter table t charset utf8mb4 collate utf8mb4_bin") - tk.MustExec("alter table t charset utf8mb4 collate utf8mb4_unicode_ci") - tk.MustExec("alter table t charset utf8mb4 collate utf8mb4_zh_pinyin_tidb_as_cs") - // Change the default collation of database is allowed. - tk.MustExec("alter database dct charset utf8mb4 collate utf8mb4_general_ci") -} - func TestForbidUnsupportedCollations(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/ddl/tests/tiflash/BUILD.bazel b/ddl/tests/tiflash/BUILD.bazel index 82226da9ad304..eaa2077dddc39 100644 --- a/ddl/tests/tiflash/BUILD.bazel +++ b/ddl/tests/tiflash/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 34, + shard_count = 32, deps = [ "//config", "//ddl", diff --git a/ddl/tests/tiflash/ddl_tiflash_test.go b/ddl/tests/tiflash/ddl_tiflash_test.go index cf5b7001afc88..e889e4c9da61e 100644 --- a/ddl/tests/tiflash/ddl_tiflash_test.go +++ b/ddl/tests/tiflash/ddl_tiflash_test.go @@ -766,16 +766,6 @@ func TestTiFlashBackoff(t *testing.T) { require.True(t, tb.Meta().TiFlashReplica.Available) } -func TestAlterDatabaseErrorGrammar(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustGetErrMsg("ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'", "[ddl:8200]Unsupported multi schema change for set tiflash replica") - tk.MustGetErrMsg("ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2", "[ddl:8200]Unsupported multi schema change for set tiflash replica") - tk.MustGetErrMsg("ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2", "[ddl:8200]Unsupported multi schema change for set tiflash replica") - tk.MustGetErrMsg("ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'", "[ddl:8200]Unsupported multi schema change for set tiflash replica") -} - func TestAlterDatabaseBasic(t *testing.T) { s, teardown := createTiFlashContext(t) defer teardown() @@ -812,33 +802,6 @@ func TestAlterDatabaseBasic(t *testing.T) { tk.MustGetErrMsg("alter database tiflash_ddl set tiflash replica 3", "the tiflash replica count: 3 should be less than the total tiflash server count: 2") } -func checkBatchPandingNum(t *testing.T, tkx *testkit.TestKit, level string, value string, ok bool) { - l := len(tkx.MustQuery(fmt.Sprintf("show %v variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='%v'", level, value)).Rows()) - if ok { - require.Equal(t, 1, l) - } else { - require.Equal(t, 0, l) - } -} - -func TestTiFlashBatchAddVariables(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("set SESSION tidb_batch_pending_tiflash_count=5") - tk.MustExec("set GLOBAL tidb_batch_pending_tiflash_count=6") - - checkBatchPandingNum(t, tk, "session", "5", true) - checkBatchPandingNum(t, tk, "global", "6", true) - checkBatchPandingNum(t, tk, "global", "1.5", false) - - tk.MustGetErrMsg("set GLOBAL tidb_batch_pending_tiflash_count=1.5", "[variable:1232]Incorrect argument type to variable 'tidb_batch_pending_tiflash_count'") - checkBatchPandingNum(t, tk, "global", "6", true) - - tk2 := testkit.NewTestKit(t, store) - checkBatchPandingNum(t, tk2, "session", "6", true) -} - func execWithTimeout(t *testing.T, tk *testkit.TestKit, to time.Duration, sql string) (bool, error) { ctx, cancel := context.WithTimeout(context.Background(), to) defer cancel() diff --git a/tests/integrationtest/r/ddl/db_partition.result b/tests/integrationtest/r/ddl/db_partition.result new file mode 100644 index 0000000000000..9d9523b794bdc --- /dev/null +++ b/tests/integrationtest/r/ddl/db_partition.result @@ -0,0 +1,3293 @@ +set @@session.tidb_enable_table_partition = 'AUTO'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( +partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add partition ( +partition p4 values in (7), +partition p5 values in (8,9)); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 'OFF'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( +partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add partition ( +partition p4 values in (7), +partition p5 values in (8,9)); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 0; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( +partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add partition ( +partition p4 values in (7), +partition p5 values in (8,9)); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 'ON'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( +partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +alter table t add partition ( +partition p4 values in (7), +partition p5 values in (8,9)); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = default; +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +create table t (a varchar(255) charset utf8mb4 collate utf8mb4_bin) partition by range columns (a) +(partition p0A values less than ("A"), +partition p1AA values less than ("AA"), +partition p2Aa values less than ("Aa"), +partition p3BB values less than ("BB"), +partition p4Bb values less than ("Bb"), +partition p5aA values less than ("aA"), +partition p6aa values less than ("aa"), +partition p7bB values less than ("bB"), +partition p8bb values less than ("bb"), +partition pMax values less than (MAXVALUE)); +insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"); +analyze table t; +explain format='brief' select * from t where a = "AA" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] eq(ddl__db_partition.t.a, "AA") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a = "AA" collate utf8mb4_general_ci; +a +AA +Aa +aA +aa +explain format='brief' select * from t where a = "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] eq(ddl__db_partition.t.a, "aa") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a = "aa" collate utf8mb4_general_ci; +a +AA +Aa +aA +aa +explain format='brief' select * from t where a >= "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] ge(ddl__db_partition.t.a, "aa") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a >= "aa" collate utf8mb4_general_ci; +a +AA +AB +Aa +Ab +B +BA +BB +Ba +Bb +aA +aB +aa +ab +b +bA +bB +ba +bb +explain format='brief' select * from t where a > "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] gt(ddl__db_partition.t.a, "aa") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a > "aa" collate utf8mb4_general_ci; +a +AB +Ab +B +BA +BB +Ba +Bb +aB +ab +b +bA +bB +ba +bb +explain format='brief' select * from t where a <= "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] le(ddl__db_partition.t.a, "aa") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a <= "aa" collate utf8mb4_general_ci; +a +A +AA +Aa +a +aA +aa +explain format='brief' select * from t where a < "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 16.00 root partition:all data:Selection +└─Selection 16.00 cop[tikv] lt(ddl__db_partition.t.a, "aa") + └─TableFullScan 20.00 cop[tikv] table:t keep order:false +select * from t where a < "aa" collate utf8mb4_general_ci; +a +A +a +drop table t; +create table t (a varchar(255) charset utf8mb4 collate utf8mb4_general_ci) partition by range columns (a) +(partition p0 values less than ("A"), +partition p1 values less than ("aa"), +partition p2 values less than ("AAA"), +partition p3 values less than ("aaaa"), +partition p4 values less than ("B"), +partition p5 values less than ("bb"), +partition pMax values less than (MAXVALUE)); +insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"),("ä"),("ÄÄÄ"); +analyze table t; +explain format='brief' select * from t where a = "aa" collate utf8mb4_general_ci; +id estRows task access object operator info +TableReader 4.00 root partition:p2 data:Selection +└─Selection 4.00 cop[tikv] eq(ddl__db_partition.t.a, "aa") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a = "aa" collate utf8mb4_general_ci; +a +AA +Aa +aA +aa +explain format='brief' select * from t where a = "aa" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:p2 data:Selection +└─Selection 17.60 cop[tikv] eq(ddl__db_partition.t.a, "aa") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a = "aa" collate utf8mb4_bin; +a +aa +explain format='brief' select * from t where a = "ä" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:p1 data:Selection +└─Selection 17.60 cop[tikv] eq(ddl__db_partition.t.a, "ä") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a = "ä" collate utf8mb4_bin; +a +ä +explain format='brief' select * from t where a = "b" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:p5 data:Selection +└─Selection 17.60 cop[tikv] eq(ddl__db_partition.t.a, "b") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a = "b" collate utf8mb4_bin; +a +b +explain format='brief' select * from t where a <= "b" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:all data:Selection +└─Selection 17.60 cop[tikv] le(ddl__db_partition.t.a, "b") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a <= "b" collate utf8mb4_bin; +a +A +AA +AB +Aa +Ab +B +BA +BB +Ba +Bb +a +aA +aB +aa +ab +b +explain format='brief' select * from t where a < "b" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:all data:Selection +└─Selection 17.60 cop[tikv] lt(ddl__db_partition.t.a, "b") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a < "b" collate utf8mb4_bin; +a +A +AA +AB +Aa +Ab +B +BA +BB +Ba +Bb +a +aA +aB +aa +ab +explain format='brief' select * from t where a >= "b" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:all data:Selection +└─Selection 17.60 cop[tikv] ge(ddl__db_partition.t.a, "b") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a >= "b" collate utf8mb4_bin; +a +b +bA +bB +ba +bb +ÄÄÄ +ä +explain format='brief' select * from t where a > "b" collate utf8mb4_bin; +id estRows task access object operator info +TableReader 17.60 root partition:all data:Selection +└─Selection 17.60 cop[tikv] gt(ddl__db_partition.t.a, "b") + └─TableFullScan 22.00 cop[tikv] table:t keep order:false +select * from t where a > "b" collate utf8mb4_bin; +a +bA +bB +ba +bb +ÄÄÄ +ä +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (a int) partition by list (a) (partition p1 values in (null, 6), partition p2 values in (7,null)); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +create table t (id int) partition by list (id) ( +partition p0 values in (1,2), +partition p1 values in (3,4), +partition p3 values in (5,null) +); +alter table t add partition ( +partition p4 values in (7), +partition p5 values in (8,9)); +alter table t add partition (partition p6 values in (null, 6)); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +alter table t add partition (partition pDef values in (default, 6)); +alter table t add partition (partition pDef2 values in (10, default)); +Error 8200 (HY000): Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead +alter table t add partition (partition pDef2 values in (10)); +Error 8200 (HY000): Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`id`) +(PARTITION `p0` VALUES IN (1,2), + PARTITION `p1` VALUES IN (3,4), + PARTITION `p3` VALUES IN (5,NULL), + PARTITION `p4` VALUES IN (7), + PARTITION `p5` VALUES IN (8,9), + PARTITION `pDef` VALUES IN (DEFAULT,6)) +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='ddl__db_partition'; +partition_name partition_ordinal_position partition_method partition_expression partition_description partition_comment tidb_placement_policy_name +p0 1 LIST `id` 1,2 NULL +p1 2 LIST `id` 3,4 NULL +p3 3 LIST `id` 5,NULL NULL +p4 4 LIST `id` 7 NULL +p5 5 LIST `id` 8,9 NULL +pDef 6 LIST `id` DEFAULT,6 NULL +alter table t reorganize partition pDef into (partition p_6 values in (6)); +alter table t add partition (partition p4 values in (7)); +Error 1517 (HY000): Duplicate partition name p4 +alter table t add partition (partition p6 values less than (7)); +Error 1480 (HY000): Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition +alter table t add partition (partition p6 values in (null)); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +alter table t add partition (partition p6 values in (7)); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +alter table t add partition (partition p6 values in ('a')); +Error 1697 (HY000): VALUES value for partition 'p6' must have type INT +alter table t add partition (partition p5 values in (10),partition p6 values in (7)); +Error 1517 (HY000): Duplicate partition name p5 +drop table if exists t; +create table t (a bigint unsigned) partition by list columns (a) (partition p0 values in (1)); +alter table t add partition (partition p1 values in (-1)); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists t; +create table t (a varchar(2)) partition by list columns (a) (partition p0 values in ('a','b')); +alter table t add partition (partition p1 values in ('abc')); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists t; +create table t (a tinyint) partition by list columns (a) (partition p0 values in (1,2,3)); +alter table t add partition (partition p1 values in (65536)); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists t; +create table t (a bigint) partition by list columns (a) (partition p0 values in (1,2,3)); +alter table t add partition (partition p1 values in (18446744073709551615)); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists t; +create table t (a char) partition by list columns (a) (partition p0 values in ('a','b')); +alter table t add partition (partition p1 values in ('abc')); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists t; +create table t (a datetime) partition by list columns (a) (partition p0 values in ('2020-11-30 12:00:00')); +alter table t add partition (partition p1 values in ('2020-11-31 12:00:00')); +Error 1654 (HY000): Partition column values of incorrect type +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int, name varchar(10)) partition by list columns (id,name) ( +partition p0 values in ((1,'a'),(2,'b')), +partition p1 values in ((3,'a'),(4,'b')), +partition p3 values in ((5,null)) +); +create table t (a int) partition by list (a) (partition p0 values in (default), partition p1 values in (maxvalue)); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 112 near "maxvalue));" +alter table t add partition ( +partition p4 values in ((7,'a')), +partition p5 values in ((8,'a'))); +alter table t add partition ( +partition pDef values in (10, default)); +Error 1653 (HY000): Inconsistency in usage of column lists for partitioning +alter table t add partition ( +partition pDef values in ((10, default))); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 40 near ")));" +alter table t add partition ( +partition pDef values in (default, 10)); +Error 1653 (HY000): Inconsistency in usage of column lists for partitioning +alter table t add partition ( +partition pDef values in ((default, 10))); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 36 near ", 10)));" +alter table t add partition ( +partition pDef values in ((9,'a'), (default, 10, 'q')); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 45 near ", 10, 'q'));" +alter table t add partition ( +partition pDef values in ((9,'a'), (10, default, 'q')); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 49 near ", 'q'));" +alter table t add partition ( +partition pDef values in (default)); +alter table t add partition ( +partition pDef2 values in (default)); +Error 8200 (HY000): Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead +alter table t add partition (partition p6 values in ((9,'d'))); +Error 8200 (HY000): Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead +alter table t add partition (partition p6 values in (default)); +Error 8200 (HY000): Unsupported ADD List partition, already contains DEFAULT partition. Please use REORGANIZE PARTITION instead +alter table t drop partition pDef; +alter table t add partition (partition pDef default); +alter table t drop partition pDef; +alter table t add partition ( +partition pDef values in ((9, 'c'), default)); +alter table t add partition ( +partition pDef values in ((9, 'c'), (default))); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 46 near ")));" +alter table t drop partition pDef; +alter table t add partition ( +partition pDef values in (default, (9,'c'))); +alter table t drop partition pDef; +alter table t add partition ( +partition pDef values in ((9,'d'), default)); +alter table t drop partition pDef; +alter table t add partition ( +partition pDef values in (default, (9,'c'), (10, 'd'))); +alter table t drop partition pDef; +alter table t add partition ( +partition pDef values in ((9,'d'), default, (10, 'd'))); +alter table t add partition ( +partition pDef values in ((9,'a'), (10, default, 'q')); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 49 near ", 'q'));" +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `name` varchar(10) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`id`,`name`) +(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')), + PARTITION `p1` VALUES IN ((3,'a'),(4,'b')), + PARTITION `p3` VALUES IN ((5,NULL)), + PARTITION `p4` VALUES IN ((7,'a')), + PARTITION `p5` VALUES IN ((8,'a')), + PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d'))) +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name partition_ordinal_position partition_method partition_expression partition_description partition_comment tidb_placement_policy_name +p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') NULL +p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') NULL +p3 3 LIST COLUMNS `id`,`name` (5,NULL) NULL +p4 4 LIST COLUMNS `id`,`name` (7,'a') NULL +p5 5 LIST COLUMNS `id`,`name` (8,'a') NULL +pDef 6 LIST COLUMNS `id`,`name` (9,'d'),DEFAULT,(10,'d') NULL +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `name` varchar(10) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`id`,`name`) +(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')), + PARTITION `p1` VALUES IN ((3,'a'),(4,'b')), + PARTITION `p3` VALUES IN ((5,NULL)), + PARTITION `p4` VALUES IN ((7,'a')), + PARTITION `p5` VALUES IN ((8,'a')), + PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d'))) +alter table t reorganize partition pDef into (partition pd VALUES IN ((9,'d'),(10,'d'))); +alter table t add partition (partition p4 values in ((7,'b'))); +Error 1517 (HY000): Duplicate partition name p4 +alter table t add partition (partition p6 values less than ((7,'a'))); +Error 1480 (HY000): Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition +alter table t add partition (partition p6 values in ((5,null))); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +alter table t add partition (partition p6 values in (('a','a'))); +Error 1654 (HY000): Partition column values of incorrect type +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL, +`name` varchar(10) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`id`,`name`) +(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')), +PARTITION `p1` VALUES IN ((3,'a'),(4,'b')), +PARTITION `p3` VALUES IN ((5,NULL)), +PARTITION `p4` VALUES IN ((7,'a')), +PARTITION `p5` VALUES IN ((8,'a')), +PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d'))); +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name partition_ordinal_position partition_method partition_expression partition_description partition_comment tidb_placement_policy_name +p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') NULL +p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') NULL +p3 3 LIST COLUMNS `id`,`name` (5,NULL) NULL +p4 4 LIST COLUMNS `id`,`name` (7,'a') NULL +p5 5 LIST COLUMNS `id`,`name` (8,'a') NULL +pDef 6 LIST COLUMNS `id`,`name` (9,'d'),DEFAULT,(10,'d') NULL +set @@session.tidb_enable_list_partition = OFF; +alter table t drop partition pDef; +alter table t add partition (partition pDef); +Error 1479 (HY000): Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition +alter table t add partition (partition pDef default); +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name partition_ordinal_position partition_method partition_expression partition_description partition_comment tidb_placement_policy_name +p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') NULL +p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') NULL +p3 3 LIST COLUMNS `id`,`name` (5,NULL) NULL +p4 4 LIST COLUMNS `id`,`name` (7,'a') NULL +p5 5 LIST COLUMNS `id`,`name` (8,'a') NULL +pDef 6 LIST COLUMNS `id`,`name` DEFAULT NULL +alter table t reorganize partition pDef into (partition pDef VALUES IN (DEFAULT,(9,'c'))); +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name partition_ordinal_position partition_method partition_expression partition_description partition_comment tidb_placement_policy_name +p0 1 LIST COLUMNS `id`,`name` (1,'a'),(2,'b') NULL +p1 2 LIST COLUMNS `id`,`name` (3,'a'),(4,'b') NULL +p3 3 LIST COLUMNS `id`,`name` (5,NULL) NULL +p4 4 LIST COLUMNS `id`,`name` (7,'a') NULL +p5 5 LIST COLUMNS `id`,`name` (8,'a') NULL +pDef 6 LIST COLUMNS `id`,`name` DEFAULT,(9,'c') NULL +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +create table t (a int, b varchar(255), unique key (a), key (b)) partition by list (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10)); +insert into t values (1, "1"), (2, "2"), (3,'3'), (null, "null"), (4, "4"), (11, "11"); +analyze table t; +select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +partition_name partition_method partition_expression partition_description table_rows +p0 LIST `a` 0,4 1 +p1 LIST `a` 1,NULL,DEFAULT 4 +p2 LIST `a` 2,7,10 1 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` varchar(255) DEFAULT NULL, + UNIQUE KEY `a` (`a`), + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`a`) +(PARTITION `p0` VALUES IN (0,4), + PARTITION `p1` VALUES IN (1,NULL,DEFAULT), + PARTITION `p2` VALUES IN (2,7,10)) +set @@tidb_partition_prune_mode = 'dynamic'; +select * from t where a = 4; +a b +4 4 +select * from t where a is null; +a b +NULL null +select * from t where a = 3; +a b +3 3 +explain format = 'brief' select * from t where a = 4; +id estRows task access object operator info +Point_Get 1.00 root table:t, partition:p0, index:a(a) +explain format = 'brief' select * from t where a is null; +id estRows task access object operator info +TableReader 1.00 root partition:p1 data:Selection +└─Selection 1.00 cop[tikv] isnull(ddl__db_partition.t.a) + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +explain format = 'brief' select * from t where a = 3; +id estRows task access object operator info +Point_Get 1.00 root table:t, partition:p1, index:a(a) +set @@tidb_partition_prune_mode = 'static'; +select * from t where a = 4; +a b +4 4 +select * from t where a is null; +a b +NULL null +select * from t where a = 3; +a b +3 3 +explain format = 'brief' select * from t where a = 4; +id estRows task access object operator info +Point_Get 1.00 root table:t, partition:p0, index:a(a) +explain format = 'brief' select * from t where a is null; +id estRows task access object operator info +TableReader 1.00 root data:Selection +└─Selection 1.00 cop[tikv] isnull(ddl__db_partition.t.a) + └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false +explain format = 'brief' select * from t where a = 3; +id estRows task access object operator info +Point_Get 1.00 root table:t, partition:p1, index:a(a) +alter table t reorganize partition p0 into (partition p0 values in (0)); +Error 1526 (HY000): Table has no partition for value 4 +drop table t; +CREATE TABLE t (a VARCHAR(100),b INT) PARTITION BY LIST COLUMNS (a) (PARTITION p1 VALUES IN ('a', 'b', 'DEFAULT'),PARTITION pDef DEFAULT); +set @@tidb_partition_prune_mode = default; +drop table if exists t; +create table t (b int, a varchar(255)) partition by list columns (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `b` int(11) DEFAULT NULL, + `a` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`a`) +(PARTITION `p0` VALUES IN (0,4), + PARTITION `p1` VALUES IN (1,NULL,DEFAULT), + PARTITION `p2` VALUES IN (2,7,10)) +insert into t values (1, "1"), (2, "2"), (3,'3'), (null, null), (4, "4"), (11, "11"); +analyze table t; +select * from t partition(p0); +b a +4 4 +select * from t partition(p1); +b a +NULL NULL +1 1 +11 11 +3 3 +select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +partition_name partition_method partition_expression partition_description table_rows +p0 LIST COLUMNS `a` 0,4 1 +p1 LIST COLUMNS `a` 1,NULL,DEFAULT 4 +p2 LIST COLUMNS `a` 2,7,10 1 +set @@tidb_partition_prune_mode = 'dynamic'; +explain format = 'brief' select * from t where a is null; +id estRows task access object operator info +TableReader 1.00 root partition:p1 data:Selection +└─Selection 1.00 cop[tikv] isnull(ddl__db_partition.t.a) + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +select * from t where a is null; +b a +NULL NULL +select * from t where a = 4; +b a +4 4 +explain format = 'brief' select * from t where a = 4; +id estRows task access object operator info +TableReader 4.80 root partition:p0,p1,p2 data:Selection +└─Selection 4.80 cop[tikv] eq(cast(ddl__db_partition.t.a, double BINARY), 4) + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +select * from t where a = 3; +b a +3 3 +explain format = 'brief' select * from t where a = 3; +id estRows task access object operator info +TableReader 4.80 root partition:p0,p1,p2 data:Selection +└─Selection 4.80 cop[tikv] eq(cast(ddl__db_partition.t.a, double BINARY), 3) + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +explain format = 'brief' select * from t where a = "3"; +id estRows task access object operator info +TableReader 1.00 root partition:p1 data:Selection +└─Selection 1.00 cop[tikv] eq(ddl__db_partition.t.a, "3") + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +select * from t where a = "3"; +b a +3 3 +select * from t where a = "4"; +b a +4 4 +explain format = 'brief' select * from t where a = "4"; +id estRows task access object operator info +TableReader 1.00 root partition:p0 data:Selection +└─Selection 1.00 cop[tikv] eq(ddl__db_partition.t.a, "4") + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +explain format = 'brief' select * from t where a between "3" and "4"; +id estRows task access object operator info +TableReader 2.00 root partition:p0,p1 data:Selection +└─Selection 2.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +select * from t where a between "3" and "4"; +b a +3 3 +4 4 +explain format = 'brief' select * from t where a >= "3" and a <= "4"; +id estRows task access object operator info +TableReader 2.00 root partition:p0,p1 data:Selection +└─Selection 2.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") + └─TableFullScan 6.00 cop[tikv] table:t keep order:false +select * from t where a >= "3" and a <= "4"; +b a +3 3 +4 4 +set @@tidb_partition_prune_mode = 'static'; +select * from t where a is null; +b a +NULL NULL +explain format = 'brief' select * from t where a is null; +id estRows task access object operator info +TableReader 1.00 root data:Selection +└─Selection 1.00 cop[tikv] isnull(ddl__db_partition.t.a) + └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false +select * from t where a = 4; +b a +4 4 +select * from t where a = "4"; +b a +4 4 +explain format = 'brief' select * from t where a = "4"; +id estRows task access object operator info +TableReader 1.00 root data:Selection +└─Selection 1.00 cop[tikv] eq(ddl__db_partition.t.a, "4") + └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false +select * from t where a = 3; +b a +3 3 +select * from t where a = "3"; +b a +3 3 +explain format = 'brief' select * from t where a = "3"; +id estRows task access object operator info +TableReader 1.00 root data:Selection +└─Selection 1.00 cop[tikv] eq(ddl__db_partition.t.a, "3") + └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false +explain format = 'brief' select * from t where a between "3" and "4"; +id estRows task access object operator info +PartitionUnion 2.00 root +├─TableReader 1.00 root data:Selection +│ └─Selection 1.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") +│ └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false +└─TableReader 1.00 root data:Selection + └─Selection 1.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") + └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false +select * from t where a between "3" and "4"; +b a +3 3 +4 4 +explain format = 'brief' select * from t where a >= "3" and a <= "4"; +id estRows task access object operator info +PartitionUnion 2.00 root +├─TableReader 1.00 root data:Selection +│ └─Selection 1.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") +│ └─TableFullScan 1.00 cop[tikv] table:t, partition:p0 keep order:false +└─TableReader 1.00 root data:Selection + └─Selection 1.00 cop[tikv] ge(ddl__db_partition.t.a, "3"), le(ddl__db_partition.t.a, "4") + └─TableFullScan 4.00 cop[tikv] table:t, partition:p1 keep order:false +select * from t where a >= "3" and a <= "4"; +b a +3 3 +4 4 +alter table t reorganize partition p0 into (partition p0 values in (0)); +Error 1526 (HY000): Table has no partition for value from column_list +set @@tidb_partition_prune_mode = default; +drop table if exists t; +create table t (a int) partition by range (a) (partition p0 values less than (default)); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 86 near "));" +create table t (a int) partition by range (a) (partition p0 values less than ("default")); +Error 1697 (HY000): VALUES value for partition 'p0' must have type INT +create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than ("default")); +drop table t; +create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than (default)); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 102 near "));" +create table t (a varchar(55)) partition by range columns (a) (partition p0 default); +[ddl:1480]Only LIST PARTITIONING can use VALUES IN in partition definition +create table t (a varchar(55)) partition by list columns (a) (partition p0 default); +insert into t values ('Hi'); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` varchar(55) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`a`) +(PARTITION `p0` DEFAULT) +drop table t; +create table t (a int) partition by list (a) (partition p0 default); +insert into t values (2); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`a`) +(PARTITION `p0` DEFAULT) +drop table t; +create table t (a int) partition by list (a) (partition p0 values in ()); +[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 71 near "));" +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int) partition by list (id) ( +partition p0 values in (1,2), +partition p1 values in (3,4), +partition p3 values in (5,null) +); +insert into t values (1),(3),(5),(null); +alter table t drop partition p1; +select * from t order by id; +id +NULL +1 +5 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`id`) +(PARTITION `p0` VALUES IN (1,2), + PARTITION `p3` VALUES IN (5,NULL)) +alter table t drop partition p10; +Error 1507 (HY000): Error in list of partitions to DROP +alter table t drop partition p3; +select * from t; +id +1 +alter table t drop partition p0; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int, name varchar(10)) partition by list columns (id,name) ( +partition p0 values in ((1,'a'),(2,'b')), +partition p1 values in ((3,'a'),(4,'b')), +partition p3 values in ((5,'a'),(null,null)), +partition pDef values in (default) +); +insert into t values (1,'a'),(3,'a'),(5,'a'),(null,null),(9,9); +alter table t drop partition p1; +alter table t drop partition pDef; +select * from t; +id name +NULL NULL +1 a +5 a +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `name` varchar(10) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`id`,`name`) +(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')), + PARTITION `p3` VALUES IN ((5,'a'),(NULL,NULL))) +alter table t drop partition p10; +Error 1507 (HY000): Error in list of partitions to DROP +alter table t drop partition p3; +select * from t; +id name +1 a +alter table t drop partition p0; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +set @@session.tidb_enable_list_partition = default; +drop table if exists employees; +create table employees ( +id int not null, +hired date not null +) +partition by range( year(hired) ) ( +partition p1 values less than (1991), +partition p2 values less than (1996), +partition p3 values less than (2001) +); +alter table employees add partition ( +partition p4 values less than (2010), +partition p5 values less than MAXVALUE +); +show create table employees; +Table Create Table +employees CREATE TABLE `employees` ( + `id` int(11) NOT NULL, + `hired` date NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (YEAR(`hired`)) +(PARTITION `p1` VALUES LESS THAN (1991), + PARTITION `p2` VALUES LESS THAN (1996), + PARTITION `p3` VALUES LESS THAN (2001), + PARTITION `p4` VALUES LESS THAN (2010), + PARTITION `p5` VALUES LESS THAN (MAXVALUE)) +drop table if exists table1; +create table table1(a int); +alter table table1 add partition ( +partition p1 values less than (2010), +partition p2 values less than maxvalue +); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +create table table_MustBeDefined ( +id int not null, +hired date not null +) +partition by range( year(hired) ) ( +partition p1 values less than (1991), +partition p2 values less than (1996), +partition p3 values less than (2001) +); +alter table table_MustBeDefined add partition; +Error 1492 (HY000): For RANGE partitions each partition must be defined +drop table if exists table2; +create table table2 ( +id int not null, +hired date not null +) partition by range( year(hired) ) ( +partition p1 values less than (1991), +partition p2 values less than maxvalue +); +alter table table2 add partition ( +partition p3 values less than (2010) +); +Error 1481 (HY000): MAXVALUE can only be used in last partition definition +drop table if exists table3; +create table table3 ( +id int not null, +hired date not null +) +partition by range( year(hired) ) ( +partition p1 values less than (1991), +partition p2 values less than (2001) +); +alter table table3 add partition ( +partition p3 values less than (1993) +); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table table3 add partition ( +partition p1 values less than (1993) +); +Error 1517 (HY000): Duplicate partition name p1 +alter table table3 add partition ( +partition p1 values less than (1993), +partition p1 values less than (1995) +); +Error 1517 (HY000): Duplicate partition name p1 +alter table table3 add partition ( +partition p4 values less than (1993), +partition p1 values less than (1995), +partition p5 values less than maxvalue +); +Error 1517 (HY000): Duplicate partition name p1 +alter table table3 add partition (partition p6); +Error 1479 (HY000): Syntax : RANGE PARTITIONING requires definition of VALUES LESS THAN for each partition +alter table table3 add partition (partition p7 values in (2018)); +Error 1480 (HY000): Only LIST PARTITIONING can use VALUES IN in partition definition +alter table table3 add partition partitions 4; +Error 1492 (HY000): For RANGE partitions each partition must be defined +alter table table3 add partition (partition p3 values less than (2001 + 10)); +CREATE TABLE tt5 ( +c3 bigint(20) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE ( c3 ) ( +PARTITION p0 VALUES LESS THAN (-3), +PARTITION p1 VALUES LESS THAN (-2) +); +ALTER TABLE tt5 add partition ( partition p2 values less than (-1) ); +ALTER TABLE tt5 add partition ( partition p3 values less than (5-1) ); +drop table if exists t; +create table t (a datetime) partition by range columns (a) (partition p1 values less than ('2019-06-01'), partition p2 values less than ('2019-07-01')); +alter table t add partition ( partition p3 values less than ('2019-07-01')); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table t add partition ( partition p3 values less than ('2019-08-01')); +drop table if exists t; +create table t ( +col date not null default '2000-01-01') +partition by range columns (col) ( +PARTITION p0 VALUES LESS THAN ('20190905'), +PARTITION p1 VALUES LESS THAN ('20190906') +); +alter table t add partition (partition p2 values less than (20190907)); +Error 1654 (HY000): Partition column values of incorrect type +drop table if exists employees; +create table employees ( +id int not null, +hired int not null +) +partition by range( hired ) ( +partition p1 values less than (1991), +partition p2 values less than (1996), +partition p3 values less than (2001) +); +alter table employees drop partition p3; +show create table employees; +Table Create Table +employees CREATE TABLE `employees` ( + `id` int(11) NOT NULL, + `hired` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`hired`) +(PARTITION `p1` VALUES LESS THAN (1991), + PARTITION `p2` VALUES LESS THAN (1996)) +drop table if exists table1; +create table table1 (a int); +alter table table1 drop partition p10; +Error 1505 (HY000): Partition management on a not partitioned table is not possible +drop table if exists table2; +create table table2 ( +id int not null, +hired date not null +) +partition by range( year(hired) ) ( +partition p1 values less than (1991), +partition p2 values less than (1996), +partition p3 values less than (2001) +); +alter table table2 drop partition p10; +Error 1507 (HY000): Error in list of partitions to DROP +drop table if exists table3; +create table table3 ( +id int not null +) +partition by range( id ) ( +partition p1 values less than (1991) +); +alter table table3 drop partition p1; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +drop table if exists table4; +create table table4 ( +id int not null +) +partition by range( id ) ( +partition p1 values less than (10), +partition p2 values less than (20), +partition p3 values less than MAXVALUE +); +alter table table4 drop partition p2; +show create table table4; +Table Create Table +table4 CREATE TABLE `table4` ( + `id` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `p1` VALUES LESS THAN (10), + PARTITION `p3` VALUES LESS THAN (MAXVALUE)) +drop table if exists tr; +create table tr( +id int, name varchar(50), +purchased date +) +partition by range( year(purchased) ) ( +partition p0 values less than (1990), +partition p1 values less than (1995), +partition p2 values less than (2000), +partition p3 values less than (2005), +partition p4 values less than (2010), +partition p5 values less than (2015) +); +INSERT INTO tr VALUES +(1, 'desk organiser', '2003-10-15'), +(2, 'alarm clock', '1997-11-05'), +(3, 'chair', '2009-03-10'), +(4, 'bookcase', '1989-01-10'), +(5, 'exercise bike', '2014-05-09'), +(6, 'sofa', '1987-06-05'), +(7, 'espresso maker', '2011-11-22'), +(8, 'aquarium', '1992-08-04'), +(9, 'study desk', '2006-09-16'), +(10, 'lava lamp', '1998-12-25'); +select * from tr where purchased between '1995-01-01' and '1999-12-31'; +id name purchased +2 alarm clock 1997-11-05 +10 lava lamp 1998-12-25 +alter table tr drop partition p2; +select * from tr where purchased between '1995-01-01' and '1999-12-31'; +id name purchased +select * from tr where purchased between '2010-01-01' and '2014-12-31'; +id name purchased +5 exercise bike 2014-05-09 +7 espresso maker 2011-11-22 +alter table tr drop partition p5; +select * from tr where purchased between '2010-01-01' and '2014-12-31'; +id name purchased +alter table tr drop partition p4; +select * from tr where purchased between '2005-01-01' and '2009-12-31'; +id name purchased +drop table if exists table4; +create table table4 ( +id int not null +) +partition by range( id ) ( +partition Par1 values less than (1991), +partition pAR2 values less than (1992), +partition Par3 values less than (1995), +partition PaR5 values less than (1996) +); +alter table table4 drop partition Par2; +alter table table4 drop partition PAR5; +alter table table4 drop partition PAR0; +Error 1507 (HY000): Error in list of partitions to DROP +CREATE TABLE t1 (a int(11), b varchar(64)) PARTITION BY HASH(a) PARTITIONS 3; +alter table t1 drop partition p2; +Error 1512 (HY000): DROP PARTITION can only be used on RANGE/LIST partitions +drop table if exists employees; +create table employees ( +hired int not null +) +partition by range( hired ) ( +partition p1 values less than (1991), +partition p2 values less than (1996), +partition p3 values less than (2001), +partition p4 values less than (2006), +partition p5 values less than (2011) +); +INSERT INTO employees VALUES (1990), (1995), (2000), (2005), (2010); +alter table employees drop partition p1, p2; +select * from employees; +hired +2000 +2005 +2010 +alter table employees truncate partition p3, p4; +select * from employees; +hired +2010 +drop table if exists t1, t1p; +CREATE TABLE t1 ( +d date NOT NULL , +name varchar(10) NOT NULL, +UNIQUE KEY (d,name) +); +CREATE TABLE t1p ( +d date NOT NULL , +name varchar(10) NOT NULL, +UNIQUE KEY (d,name) +) +PARTITION BY RANGE COLUMNS(d) +(PARTITION p202307 VALUES LESS THAN ('2023-08-01'), +PARTITION p202308 VALUES LESS THAN ('2023-09-01'), +PARTITION p202309 VALUES LESS THAN ('2023-10-01'), +PARTITION p202310 VALUES LESS THAN ('2023-11-01'), +PARTITION p202311 VALUES LESS THAN ('2023-12-01'), +PARTITION p202312 VALUES LESS THAN ('2024-01-01'), +PARTITION pfuture VALUES LESS THAN (MAXVALUE)); +insert into t1 values ("2023-08-06","0000"); +alter table t1p exchange partition p202307 with table t1 with validation; +Error 1737 (HY000): Found a row that does not match the partition +insert into t1 values ("2023-08-06","0001"); +drop database if exists db_one; +drop database if exists db_two; +create database db_one; +create database db_two; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (60, 60); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (60, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (60, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (80, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +insert into db_two.pt values (60, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (60, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +Error 1737 (HY000): Found a row that does not match the partition +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = default; +drop placement policy if exists rule1; +drop placement policy if exists rule2; +drop table if exists t1, t1p; +CREATE PLACEMENT POLICY rule1 FOLLOWERS=1; +CREATE PLACEMENT POLICY rule2 FOLLOWERS=2; +CREATE TABLE t1 ( +d date NOT NULL , +name varchar(10) NOT NULL, +UNIQUE KEY (d,name) +) PLACEMENT POLICY="rule1"; +CREATE TABLE t1p ( +d date NOT NULL , +name varchar(10) NOT NULL, +UNIQUE KEY (d,name) +) PLACEMENT POLICY="rule2" +PARTITION BY RANGE COLUMNS(d) +(PARTITION p202307 VALUES LESS THAN ('2023-08-01'), +PARTITION p202308 VALUES LESS THAN ('2023-09-01'), +PARTITION p202309 VALUES LESS THAN ('2023-10-01'), +PARTITION p202310 VALUES LESS THAN ('2023-11-01'), +PARTITION p202311 VALUES LESS THAN ('2023-12-01'), +PARTITION p202312 VALUES LESS THAN ('2024-01-01'), +PARTITION pfuture VALUES LESS THAN (MAXVALUE)); +alter table t1p exchange partition p202307 with table t1; +Error 1736 (HY000): Tables have different definitions +insert into t1 values ("2023-08-06","0000"); +drop table if exists pt; +create table pt (a int primary key auto_increment) partition by range(a) ( +partition p0 values less than (3), +partition p1 values less than (6), +PARTITION p2 values less than (9) +); +select @@tidb_enable_exchange_partition; +@@tidb_enable_exchange_partition +1 +create table nt(a int primary key auto_increment); +alter table pt exchange partition p0 with table nt; +show warnings; +Level Code Message +Warning 1105 after the exchange, please analyze related table of the exchange to update statistics +set @@tidb_enable_exchange_partition=0; +show warnings; +Level Code Message +Warning 1105 tidb_enable_exchange_partition is always turned on. This variable has been deprecated and will be removed in the future releases +select @@tidb_enable_exchange_partition; +@@tidb_enable_exchange_partition +1 +alter table pt exchange partition p0 with table nt; +show warnings; +Level Code Message +Warning 1105 after the exchange, please analyze related table of the exchange to update statistics +set @@tidb_enable_exchange_partition=1; +show warnings; +Level Code Message +select @@tidb_enable_exchange_partition; +@@tidb_enable_exchange_partition +1 +alter table pt exchange partition p0 with table nt; +show warnings; +Level Code Message +Warning 1105 after the exchange, please analyze related table of the exchange to update statistics +set @@tidb_enable_exchange_partition=default; +drop table if exists part1; +create table part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1, col2) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part2; +create table part2 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1, col2, col3), +unique key (col3) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part3; +create table part3 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col2) +) +partition by range( col1 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part4; +create table part4 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col2), +unique key(col2) +) +partition by range( year(col2) ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part5; +create table part5 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col2, col4), +unique key(col2, col1) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1, col2) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1), +unique key (col3) +) +partition by range( col1 + col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1), +unique key (col3) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +unique key (col1, col2, col3), +unique key (col3) +) +partition by range( col1 + col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col2) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col3), +unique key(col2) +) +partition by range( year(col2) ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function +drop table if exists Part1; +create table Part1 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +primary key(col1, col3, col4), +unique key(col2, col1) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function +drop table if exists part6; +create table part6 ( +col1 int not null, +col2 date not null, +col3 int not null, +col4 int not null, +col5 int not null, +unique key(col1, col2), +unique key(col1, col3) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +create table part7 ( +col1 int not null, +col2 int not null, +col3 int not null unique, +unique key(col1, col2) +) +partition by range (col1 + col2) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +create table part8 ( +a int not null, +b int not null, +c int default null, +d int default null, +e int default null, +primary key (a, b), +unique key (c, d) +) +partition by range columns (b) ( +partition p0 values less than (4), +partition p1 values less than (7), +partition p2 values less than (11) +); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +create table part9 ( +a int not null, +b int not null, +c int default null, +d int default null, +e int default null, +primary key (a, b), +unique key (b, c, d) +) +partition by range columns (b, c) ( +partition p0 values less than (4, 5), +partition p1 values less than (7, 9), +partition p2 values less than (11, 22) +); +Error 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function +create table part12 (a varchar(20), b binary, unique index (a(5))) partition by range columns (a) ( +partition p0 values less than ('aaaaa'), +partition p1 values less than ('bbbbb'), +partition p2 values less than ('ccccc')); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +create table part12 (a varchar(20), b binary) partition by range columns (a) ( +partition p0 values less than ('aaaaa'), +partition p1 values less than ('bbbbb'), +partition p2 values less than ('ccccc')); +alter table part12 add unique index (a(5)); +Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function +create table part13 (a varchar(20), b varchar(10), unique index (a(5),b)) partition by range columns (b) ( +partition p0 values less than ('aaaaa'), +partition p1 values less than ('bbbbb'), +partition p2 values less than ('ccccc')); +drop table if exists t; +create table t ( +id int, +fname varchar(30), +lname varchar(30), +signed date +) +partition by hash( month(signed) ) +partitions 12; +insert into t values (0, "Joe", "Doe", from_days(738974 + 0)); +insert into t values (1, "Joe", "Doe", from_days(738974 + 3)); +insert into t values (2, "Joe", "Doe", from_days(738974 + 6)); +insert into t values (3, "Joe", "Doe", from_days(738974 + 9)); +insert into t values (4, "Joe", "Doe", from_days(738974 + 12)); +insert into t values (5, "Joe", "Doe", from_days(738974 + 15)); +insert into t values (6, "Joe", "Doe", from_days(738974 + 18)); +insert into t values (7, "Joe", "Doe", from_days(738974 + 21)); +insert into t values (8, "Joe", "Doe", from_days(738974 + 24)); +insert into t values (9, "Joe", "Doe", from_days(738974 + 27)); +insert into t values (10, "Joe", "Doe", from_days(738974 + 30)); +insert into t values (11, "Joe", "Doe", from_days(738974 + 33)); +insert into t values (12, "Joe", "Doe", from_days(738974 + 36)); +insert into t values (13, "Joe", "Doe", from_days(738974 + 39)); +insert into t values (14, "Joe", "Doe", from_days(738974 + 42)); +insert into t values (15, "Joe", "Doe", from_days(738974 + 45)); +insert into t values (16, "Joe", "Doe", from_days(738974 + 48)); +insert into t values (17, "Joe", "Doe", from_days(738974 + 51)); +insert into t values (18, "Joe", "Doe", from_days(738974 + 54)); +insert into t values (19, "Joe", "Doe", from_days(738974 + 57)); +insert into t values (20, "Joe", "Doe", from_days(738974 + 60)); +insert into t values (21, "Joe", "Doe", from_days(738974 + 63)); +insert into t values (22, "Joe", "Doe", from_days(738974 + 66)); +insert into t values (23, "Joe", "Doe", from_days(738974 + 69)); +insert into t values (24, "Joe", "Doe", from_days(738974 + 72)); +insert into t values (25, "Joe", "Doe", from_days(738974 + 75)); +insert into t values (26, "Joe", "Doe", from_days(738974 + 78)); +insert into t values (27, "Joe", "Doe", from_days(738974 + 81)); +insert into t values (28, "Joe", "Doe", from_days(738974 + 84)); +insert into t values (29, "Joe", "Doe", from_days(738974 + 87)); +alter table t coalesce partition 4; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `signed` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (MONTH(`signed`)) PARTITIONS 8 +analyze table t; +select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name table_rows +p0 0 +p1 0 +p2 0 +p3 1 +p4 10 +p5 10 +p6 9 +p7 0 +(select 'p0', count(*) from t partition (p0) UNION select 'p1', count(*) from t partition (p1) UNION select 'p2', count(*) from t partition (p2) UNION select 'p3', count(*) from t partition (p3) UNION select 'p4', count(*) from t partition (p4) UNION select 'p5', count(*) from t partition (p5) UNION select 'p6', count(*) from t partition (p6) UNION select 'p7', count(*) from t partition (p7)) ORDER BY 1; +p0 count(*) +p0 0 +p1 0 +p2 0 +p3 1 +p4 10 +p5 10 +p6 9 +p7 0 +drop table t; +create table t ( +id int, +fname varchar(30), +lname varchar(30), +signed date +) +partition by key(signed,fname) +partitions 12; +insert into t values (0, "Joe", "Doe", from_days(738974 + 0)); +insert into t values (1, "Joe", "Doe", from_days(738974 + 3)); +insert into t values (2, "Joe", "Doe", from_days(738974 + 6)); +insert into t values (3, "Joe", "Doe", from_days(738974 + 9)); +insert into t values (4, "Joe", "Doe", from_days(738974 + 12)); +insert into t values (5, "Joe", "Doe", from_days(738974 + 15)); +insert into t values (6, "Joe", "Doe", from_days(738974 + 18)); +insert into t values (7, "Joe", "Doe", from_days(738974 + 21)); +insert into t values (8, "Joe", "Doe", from_days(738974 + 24)); +insert into t values (9, "Joe", "Doe", from_days(738974 + 27)); +insert into t values (10, "Joe", "Doe", from_days(738974 + 30)); +insert into t values (11, "Joe", "Doe", from_days(738974 + 33)); +insert into t values (12, "Joe", "Doe", from_days(738974 + 36)); +insert into t values (13, "Joe", "Doe", from_days(738974 + 39)); +insert into t values (14, "Joe", "Doe", from_days(738974 + 42)); +insert into t values (15, "Joe", "Doe", from_days(738974 + 45)); +insert into t values (16, "Joe", "Doe", from_days(738974 + 48)); +insert into t values (17, "Joe", "Doe", from_days(738974 + 51)); +insert into t values (18, "Joe", "Doe", from_days(738974 + 54)); +insert into t values (19, "Joe", "Doe", from_days(738974 + 57)); +insert into t values (20, "Joe", "Doe", from_days(738974 + 60)); +insert into t values (21, "Joe", "Doe", from_days(738974 + 63)); +insert into t values (22, "Joe", "Doe", from_days(738974 + 66)); +insert into t values (23, "Joe", "Doe", from_days(738974 + 69)); +insert into t values (24, "Joe", "Doe", from_days(738974 + 72)); +insert into t values (25, "Joe", "Doe", from_days(738974 + 75)); +insert into t values (26, "Joe", "Doe", from_days(738974 + 78)); +insert into t values (27, "Joe", "Doe", from_days(738974 + 81)); +insert into t values (28, "Joe", "Doe", from_days(738974 + 84)); +insert into t values (29, "Joe", "Doe", from_days(738974 + 87)); +alter table t coalesce partition 4; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `signed` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY KEY (`signed`,`fname`) PARTITIONS 8 +analyze table t; +select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +partition_name table_rows +p0 4 +p1 5 +p2 3 +p3 3 +p4 5 +p5 4 +p6 3 +p7 3 +(select 'p0', count(*) from t partition (p0) UNION select 'p1', count(*) from t partition (p1) UNION select 'p2', count(*) from t partition (p2) UNION select 'p3', count(*) from t partition (p3) UNION select 'p4', count(*) from t partition (p4) UNION select 'p5', count(*) from t partition (p5) UNION select 'p6', count(*) from t partition (p6) UNION select 'p7', count(*) from t partition (p7)) ORDER BY 1; +p0 count(*) +p0 4 +p1 5 +p2 3 +p3 3 +p4 5 +p5 4 +p6 3 +p7 3 +drop table if exists t; +create table t ( +id int not null, +fname varchar(30), +lname varchar(30), +hired date not null default '1970-01-01', +separated date, +job_code int, +store_id int +) +partition by hash(store_id) +partitions 4; +insert into t values (1,"one", "two","1999-01-04", null, 1, 1), (2, "Who", "Else", '2020-12-02', '2022-12-31', 2, 2); +insert into t select id + 2, fname, lname, hired, separated, job_code, store_id + 2 from t; +insert into t select id + 4, fname, lname, hired, separated, job_code, store_id + 4 from t; +insert into t select id + 8, fname, lname, hired, separated, job_code, store_id + 8 from t; +insert into t select id + 16, fname, lname, hired, separated, job_code, store_id + 16 from t; +insert into t select id + 32, fname, lname, hired, separated, job_code, store_id + 32 from t; +insert into t select id + 64, fname, lname, hired, separated, job_code, store_id + 64 from t; +insert into t select id + 128, fname, lname, hired, separated, job_code, store_id + 128 from t; +insert into t select id + 256, fname, lname, hired, separated, job_code, store_id + 256 from t; +insert into t select id + 512, fname, lname, hired, separated, job_code, store_id + 512 from t; +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +sum(store_id) avg(store_id) max(store_id) min(store_id) sum(id) +524800 512.5000 1024 1 524800 +alter table t add partition (partition pHist history); +Error 1480 (HY000): Only SYSTEM_TIME PARTITIONING can use VALUES HISTORY in partition definition +alter table t add partition (partition pList values in (22)); +Error 1480 (HY000): Only LIST PARTITIONING can use VALUES IN in partition definition +alter table t add partition (partition pRange values less than (22)); +Error 1480 (HY000): Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition +insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1); +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +sum(store_id) avg(store_id) max(store_id) min(store_id) sum(id) +524802 511.5029 1024 1 524841 +alter table t add partition partitions 8; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`store_id`) PARTITIONS 12 +alter table t coalesce partition 0; +Error 1515 (HY000): At least one partition must be coalesced +alter table t coalesce partition 12; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +create placement policy tworeplicas followers=1; +create placement policy threereplicas followers=2; +create placement policy fourreplicas followers=3; +alter table t add partition (partition pp13 comment 'p13' placement policy tworeplicas, partition pp14 comment 'p14' placement policy threereplicas, partition pp15 comment 'p15' placement policy fourreplicas); +alter table t add partition partitions 1; +alter table t coalesce partition 1; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`store_id`) +(PARTITION `p0`, + PARTITION `p1`, + PARTITION `p2`, + PARTITION `p3`, + PARTITION `p4`, + PARTITION `p5`, + PARTITION `p6`, + PARTITION `p7`, + PARTITION `p8`, + PARTITION `p9`, + PARTITION `p10`, + PARTITION `p11`, + PARTITION `pp13` COMMENT 'p13' /*T![placement] PLACEMENT POLICY=`tworeplicas` */, + PARTITION `pp14` COMMENT 'p14' /*T![placement] PLACEMENT POLICY=`threereplicas` */, + PARTITION `pp15` COMMENT 'p15' /*T![placement] PLACEMENT POLICY=`fourreplicas` */) +alter table t coalesce partition 2; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`store_id`) +(PARTITION `p0`, + PARTITION `p1`, + PARTITION `p2`, + PARTITION `p3`, + PARTITION `p4`, + PARTITION `p5`, + PARTITION `p6`, + PARTITION `p7`, + PARTITION `p8`, + PARTITION `p9`, + PARTITION `p10`, + PARTITION `p11`, + PARTITION `pp13` COMMENT 'p13' /*T![placement] PLACEMENT POLICY=`tworeplicas` */) +alter table t coalesce partition 1; +alter table t add partition (partition p13 comment 'p13'); +alter table t add partition partitions 1; +Error 1517 (HY000): Duplicate partition name p13 +alter table t coalesce partition 1; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`store_id`) PARTITIONS 12 +alter table t add partition (partition p12 comment 'p12' placement policy tworeplicas); +alter table t placement policy fourreplicas; +alter table t add partition partitions 1; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`fourreplicas` */ +PARTITION BY HASH (`store_id`) +(PARTITION `p0`, + PARTITION `p1`, + PARTITION `p2`, + PARTITION `p3`, + PARTITION `p4`, + PARTITION `p5`, + PARTITION `p6`, + PARTITION `p7`, + PARTITION `p8`, + PARTITION `p9`, + PARTITION `p10`, + PARTITION `p11`, + PARTITION `p12` COMMENT 'p12' /*T![placement] PLACEMENT POLICY=`tworeplicas` */, + PARTITION `p13`) +alter table t remove partitioning; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PLACEMENT POLICY=`fourreplicas` */ +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +sum(store_id) avg(store_id) max(store_id) min(store_id) sum(id) +524802 511.5029 1024 1 524841 +alter table t placement policy default; +drop placement policy tworeplicas; +drop placement policy threereplicas; +drop placement policy fourreplicas; +drop table t; +create table t ( +id int not null, +fname varchar(30), +lname varchar(30), +hired date not null default '1970-01-01', +separated date, +job_code int, +store_id int +) +partition by key (hired) +partitions 4; +insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1); +alter table t add partition partitions 8; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY KEY (`hired`) PARTITIONS 12 +alter table t add partition (partition p13); +alter table t add partition partitions 1; +Error 1517 (HY000): Duplicate partition name p13 +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY KEY (`hired`) +(PARTITION `p0`, + PARTITION `p1`, + PARTITION `p2`, + PARTITION `p3`, + PARTITION `p4`, + PARTITION `p5`, + PARTITION `p6`, + PARTITION `p7`, + PARTITION `p8`, + PARTITION `p9`, + PARTITION `p10`, + PARTITION `p11`, + PARTITION `p13`) +alter table t coalesce partition 2; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) NOT NULL, + `fname` varchar(30) DEFAULT NULL, + `lname` varchar(30) DEFAULT NULL, + `hired` date NOT NULL DEFAULT '1970-01-01', + `separated` date DEFAULT NULL, + `job_code` int(11) DEFAULT NULL, + `store_id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY KEY (`hired`) PARTITIONS 11 +set @@session.tidb_enable_table_partition = 1; +drop database if exists test_db_with_partition_const; +create database test_db_with_partition_const; +use test_db_with_partition_const; +create table t1 ( id int ) +partition by range(4) ( +partition p1 values less than (10) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t2 ( time_recorded timestamp ) +partition by range(TO_DAYS(time_recorded)) ( +partition p1 values less than (1559192604) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t3 ( id int ) +partition by range(DAY(id)) ( +partition p1 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t4 ( id int ) +partition by hash(4) partitions 4 +; +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t5 ( time_recorded timestamp ) +partition by range(to_seconds(time_recorded)) ( +partition p1 values less than (1559192604) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t6 ( id int ) +partition by range(to_seconds(id)) ( +partition p1 values less than (1559192604) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t7 ( time_recorded timestamp ) +partition by range(abs(time_recorded)) ( +partition p1 values less than (1559192604) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t2332 ( time_recorded time ) +partition by range(TO_DAYS(time_recorded)) ( +partition p0 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t1 ( id int ) +partition by hash(4) partitions 4; +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t1 ( id int ) +partition by hash(ed) partitions 4; +Error 1054 (42S22): Unknown column 'ed' in 'partition function' +create table t2332 ( time_recorded time ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t2332 ( time_recorded time ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t2332 ( time_recorded time ) +partition by range(day(time_recorded)) ( +partition p0 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +create table t2332 ( time_recorded timestamp ) +partition by range(day(time_recorded)) ( +partition p0 values less than (1) +); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +set @@session.tidb_enable_table_partition = default; +drop table if exists test_1465; +create table test_1465 (a int) +partition by range(a) ( +partition p1 values less than (10), +partition p2 values less than (20), +partition p3 values less than (30) +); +alter table test_1465 truncate partition p1; +alter table test_1465 check partition p1; +Error 8200 (HY000): Unsupported check partition +alter table test_1465 optimize partition p1; +Error 8200 (HY000): Unsupported optimize partition +alter table test_1465 repair partition p1; +Error 8200 (HY000): Unsupported repair partition +alter table test_1465 import partition p1 tablespace; +Error 8200 (HY000): Unsupported Unsupported/unknown ALTER TABLE specification +alter table test_1465 discard partition p1 tablespace; +Error 8200 (HY000): Unsupported Unsupported/unknown ALTER TABLE specification +alter table test_1465 rebuild partition p1; +Error 8200 (HY000): Unsupported rebuild partition +alter table test_1465 coalesce partition 1; +Error 1509 (HY000): COALESCE PARTITION can only be used on HASH/KEY partitions +alter table test_1465 partition by hash(a); +show create table test_1465; +Table Create Table +test_1465 CREATE TABLE `test_1465` ( + `a` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY HASH (`a`) PARTITIONS 1 +alter table test_1465 drop partition p0; +Error 1512 (HY000): DROP PARTITION can only be used on RANGE/LIST partitions +drop table if exists t; +create table t( +b int(10) +) partition by range columns (b) ( +partition p0 values less than (0x10), +partition p3 values less than (0x20) +); +Error 1654 (HY000): Partition column values of incorrect type +create table t( +b int(10) +) partition by range columns (b) ( +partition p0 values less than ('g'), +partition p3 values less than ('k') +); +Error 1654 (HY000): Partition column values of incorrect type +create table t( +b char(10) +) partition by range columns (b) ( +partition p0 values less than (30), +partition p3 values less than (60) +); +Error 1654 (HY000): Partition column values of incorrect type +create table t( +b datetime +) partition by range columns (b) ( +partition p0 values less than ('g'), +partition p3 values less than ('m') +); +Error 1654 (HY000): Partition column values of incorrect type +drop tables if exists t_int, t_char, t_date; +create table t_int(b int(10)) +partition by range columns (b) ( +partition p0 values less than (10) +); +create table t_char(b char(10)) +partition by range columns (b) ( +partition p0 values less than ('a') +); +create table t_date(b datetime) +partition by range columns (b) ( +partition p0 values less than ('2020-09-01') +); +alter table t_int add partition (partition p1 values less than ('g')); +Error 1654 (HY000): Partition column values of incorrect type +alter table t_int add partition (partition p1 values less than (0x20)); +Error 1654 (HY000): Partition column values of incorrect type +alter table t_char add partition (partition p1 values less than (0x20)); +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +alter table t_char add partition (partition p1 values less than (10)); +Error 1654 (HY000): Partition column values of incorrect type +alter table t_date add partition (partition p1 values less than ('m')); +Error 1654 (HY000): Partition column values of incorrect type +alter table t_date add partition (partition p1 values less than (0x20)); +Error 1654 (HY000): Partition column values of incorrect type +alter table t_date add partition (partition p1 values less than (20)); +Error 1654 (HY000): Partition column values of incorrect type +set @@session.tidb_enable_list_partition = ON; +drop table if exists t_list1; +create table t_list1(a date) partition by list columns (a) (partition p0 values in ('2010-02-02', '20180203'), partition p1 values in ('20200202')); +insert into t_list1(a) values (20180203); +select * from t_list1 partition (p0); +a +2018-02-03 +set @@session.tidb_enable_list_partition = default; +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'A')); +Error 1495 (HY000): Multiple definition of same constant in list partitioning +create table t11(a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'b'), partition p1 values in ('C', 'D')); +insert into t11(a) values ('A'), ('c'), ('C'), ('d'), ('B'); +analyze table t11; +select * from t11 order by a; +a +A +B +c +C +d +select * from t11 partition (p0); +a +A +B +select * from t11 partition (p1); +a +c +C +d +desc select * from t11 where a = 'b'; +id estRows task access object operator info +TableReader_7 1.00 root partition:p0 data:Selection_6 +└─Selection_6 1.00 cop[tikv] eq(test_db_with_partition_const.t11.a, "b") + └─TableFullScan_5 5.00 cop[tikv] table:t11 keep order:false +set @@session.tidb_enable_list_partition = default; +drop table if exists global_partition_table; +create global temporary table global_partition_table (a int, b int) partition by hash(a) partitions 3 ON COMMIT DELETE ROWS; +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists global_partition_table; +drop table if exists partition_table; +create table partition_table (a int, b int) partition by hash(a) partitions 3; +drop table if exists partition_table; +drop table if exists partition_range_table; +create global temporary table partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( +partition p0 values less than (10), +partition p1 values less than (20), +partition p2 values less than (30), +partition p3 values less than (MAXVALUE) +) ON COMMIT DELETE ROWS; +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists partition_range_table; +drop table if exists partition_list_table; +set @@session.tidb_enable_list_partition = ON; +create global temporary table partition_list_table (id int) partition by list (id) ( +partition p0 values in (1,2), +partition p1 values in (3,4), +partition p3 values in (5,null) +) ON COMMIT DELETE ROWS; +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists partition_list_table; +drop table if exists local_partition_table; +create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3; +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists local_partition_table; +drop table if exists partition_table; +create table partition_table (a int, b int) partition by hash(a) partitions 3; +drop table if exists partition_table; +drop table if exists local_partition_range_table; +create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( +partition p0 values less than (10), +partition p1 values less than (20), +partition p2 values less than (30), +partition p3 values less than (MAXVALUE) +); +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists local_partition_range_table; +drop table if exists local_partition_list_table; +set @@session.tidb_enable_list_partition = ON; +create temporary table local_partition_list_table (id int) partition by list (id) ( +partition p0 values in (1,2), +partition p1 values in (3,4), +partition p3 values in (5,null) +); +Error 1562 (HY000): Cannot create temporary table with partitions +drop table if exists local_partition_list_table; +set @@session.tidb_enable_list_partition = default; +drop table if exists t1; +set @@tidb_enable_list_partition=on; +create table t1 (a int) partition by list (a) (partition p1 values in (1), partition p2 values in (2), partition p3 values in (3)); +insert into t1 values (1),(2),(3); +alter table t1 truncate partition p1,p1; +select * from t1; +a +2 +3 +insert into t1 values (1); +alter table t1 drop partition p1,p1; +Error 1507 (HY000): Error in list of partitions to DROP +alter table t1 drop partition p1,p9; +Error 1507 (HY000): Error in list of partitions to DROP +alter table t1 drop partition p1,p1,p1; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +alter table t1 drop partition p1,p9,p1; +Error 1508 (HY000): Cannot remove all partitions, use DROP TABLE instead +select * from t1; +a +1 +2 +3 +alter table t1 drop partition p1; +select * from t1; +a +2 +3 +Show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST (`a`) +(PARTITION `p2` VALUES IN (2), + PARTITION `p3` VALUES IN (3)) +set @@tidb_enable_list_partition=default; +drop table if exists t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), +PARTITION `p_0` VALUES LESS THAN (0), +PARTITION `p_10000000` VALUES LESS THAN (10000000), +PARTITION `p_20000000` VALUES LESS THAN (20000000), +PARTITION `p_30000000` VALUES LESS THAN (30000000), +PARTITION `p_40000000` VALUES LESS THAN (40000000), +PARTITION `p_50000000` VALUES LESS THAN (50000000), +PARTITION `p_60000000` VALUES LESS THAN (60000000), +PARTITION `p_70000000` VALUES LESS THAN (70000000), +PARTITION `p_80000000` VALUES LESS THAN (80000000), +PARTITION `p_90000000` VALUES LESS THAN (90000000), +PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), +PARTITION `p_0` VALUES LESS THAN (0), +PARTITION `p_10000000` VALUES LESS THAN (10000000), +PARTITION `p_20000000` VALUES LESS THAN (20000000), +PARTITION `p_30000000` VALUES LESS THAN (30000000), +PARTITION `p_40000000` VALUES LESS THAN (40000000), +PARTITION `p_50000000` VALUES LESS THAN (50000000), +PARTITION `p_60000000` VALUES LESS THAN (60000000), +PARTITION `p_70000000` VALUES LESS THAN (70000000), +PARTITION `p_80000000` VALUES LESS THAN (80000000), +PARTITION `p_90000000` VALUES LESS THAN (90000000), +PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), +PARTITION `p_0` VALUES LESS THAN (0), +PARTITION `p_10000000` VALUES LESS THAN (10000000), +PARTITION `p_20000000` VALUES LESS THAN (20000000), +PARTITION `p_30000000` VALUES LESS THAN (30000000), +PARTITION `p_40000000` VALUES LESS THAN (40000000), +PARTITION `p_50000000` VALUES LESS THAN (50000000), +PARTITION `p_60000000` VALUES LESS THAN (60000000), +PARTITION `p_70000000` VALUES LESS THAN (70000000), +PARTITION `p_80000000` VALUES LESS THAN (80000000), +PARTITION `p_90000000` VALUES LESS THAN (90000000), +PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), +PARTITION `p_0` VALUES LESS THAN (0), +PARTITION `p_10000000` VALUES LESS THAN (10000000), +PARTITION `p_20000000` VALUES LESS THAN (20000000), +PARTITION `p_30000000` VALUES LESS THAN (30000000), +PARTITION `p_40000000` VALUES LESS THAN (40000000), +PARTITION `p_50000000` VALUES LESS THAN (50000000), +PARTITION `p_60000000` VALUES LESS THAN (60000000), +PARTITION `p_70000000` VALUES LESS THAN (70000000), +PARTITION `p_80000000` VALUES LESS THAN (80000000), +PARTITION `p_90000000` VALUES LESS THAN (90000000), +PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)) +drop table t; +create table t (id int) partition by range (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-9223372036854775808), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-9223372036854775808), +PARTITION `P_LT_0` VALUES LESS THAN (0), +PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), +PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), +PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), +PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), +PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), +PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), +PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), +PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), +PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), +PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-9223372036854775808), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +drop table t; +create table t (id int) partition by range columns (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-2147483648), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE `t` ( +`id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-2147483648), +PARTITION `P_LT_0` VALUES LESS THAN (0), +PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), +PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), +PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), +PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), +PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), +PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), +PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), +PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), +PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), +PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-2147483648), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +drop table t; +drop table if exists ipt, t, idpt; +create table ipt (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90) MAXVALUE PARTITION; +insert into ipt values (0, '0'), (1, '1'), (2, '2'); +insert into ipt select id + 10, concat('1', val) FROM ipt; +insert into ipt select id + 20, concat('2', val) FROM ipt where id < 10; +insert into ipt select id + 30, concat('3', val) FROM ipt where id < 10; +insert into ipt select id + 40, concat('4', val) FROM ipt where id < 10; +insert into ipt select id + 50, concat('5', val) FROM ipt where id < 10; +insert into ipt select id + 60, concat('6', val) FROM ipt where id < 10; +insert into ipt select id + 70, concat('7', val) FROM ipt where id < 10; +insert into ipt select id + 80, concat('8', val) FROM ipt where id < 10; +insert into ipt select id + 90, concat('9', val) FROM ipt where id < 10; +insert into ipt select id + 100, concat('10', val) FROM ipt where id < 10; +SHOW CREATE TABLE ipt; +Table Create Table +ipt CREATE TABLE `ipt` ( + `id` bigint(20) unsigned NOT NULL, + `val` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */, + KEY `val` (`val`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_LT_10` VALUES LESS THAN (10), + PARTITION `P_LT_20` VALUES LESS THAN (20), + PARTITION `P_LT_30` VALUES LESS THAN (30), + PARTITION `P_LT_40` VALUES LESS THAN (40), + PARTITION `P_LT_50` VALUES LESS THAN (50), + PARTITION `P_LT_60` VALUES LESS THAN (60), + PARTITION `P_LT_70` VALUES LESS THAN (70), + PARTITION `P_LT_80` VALUES LESS THAN (80), + PARTITION `P_LT_90` VALUES LESS THAN (90), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +alter table ipt LAST partition less than (100); +Error 8200 (HY000): Unsupported LAST PARTITION when MAXVALUE partition exists +alter table ipt first partition less than (30); +select count(*) from ipt; +count(*) +27 +SHOW CREATE TABLE ipt; +Table Create Table +ipt CREATE TABLE `ipt` ( + `id` bigint(20) unsigned NOT NULL, + `val` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */, + KEY `val` (`val`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_LT_30` VALUES LESS THAN (30), + PARTITION `P_LT_40` VALUES LESS THAN (40), + PARTITION `P_LT_50` VALUES LESS THAN (50), + PARTITION `P_LT_60` VALUES LESS THAN (60), + PARTITION `P_LT_70` VALUES LESS THAN (70), + PARTITION `P_LT_80` VALUES LESS THAN (80), + PARTITION `P_LT_90` VALUES LESS THAN (90), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +alter table ipt merge first partition less than (60); +Error 8200 (HY000): Unsupported MERGE FIRST PARTITION +alter table ipt split maxvalue partition less than (140); +Error 8200 (HY000): Unsupported SPLIT LAST PARTITION +select count(*) from ipt; +count(*) +27 +create table idpt (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 week) FIRST PARTITION LESS THAN ('2022-02-01') LAST PARTITION LESS THAN ('2022-03-29') NULL PARTITION MAXVALUE PARTITION; +SHOW CREATE TABLE idpt; +Table Create Table +idpt CREATE TABLE `idpt` ( + `id` date NOT NULL, + `val` varchar(255) DEFAULT NULL, + KEY `val` (`val`), + PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_NULL` VALUES LESS THAN ('0000-01-01'), + PARTITION `P_LT_2022-02-01` VALUES LESS THAN ('2022-02-01'), + PARTITION `P_LT_2022-02-08` VALUES LESS THAN ('2022-02-08'), + PARTITION `P_LT_2022-02-15` VALUES LESS THAN ('2022-02-15'), + PARTITION `P_LT_2022-02-22` VALUES LESS THAN ('2022-02-22'), + PARTITION `P_LT_2022-03-01` VALUES LESS THAN ('2022-03-01'), + PARTITION `P_LT_2022-03-08` VALUES LESS THAN ('2022-03-08'), + PARTITION `P_LT_2022-03-15` VALUES LESS THAN ('2022-03-15'), + PARTITION `P_LT_2022-03-22` VALUES LESS THAN ('2022-03-22'), + PARTITION `P_LT_2022-03-29` VALUES LESS THAN ('2022-03-29'), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)) +create table t (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 MONTH) FIRST PARTITION LESS THAN ('2022-01-31') LAST PARTITION LESS THAN ('2022-05-31'); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` date NOT NULL, + `val` varchar(255) DEFAULT NULL, + KEY `val` (`val`), + PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_LT_2022-01-31` VALUES LESS THAN ('2022-01-31'), + PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'), + PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'), + PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'), + PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31')) +alter table t first partition less than ('2022-02-28'); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` date NOT NULL, + `val` varchar(255) DEFAULT NULL, + KEY `val` (`val`), + PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'), + PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'), + PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'), + PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31')) +alter table t first partition less than ('2022-03-31'); +Error 8200 (HY000): Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table +alter table t last partition less than ('2022-06-30'); +Error 8200 (HY000): Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table +alter table t last partition less than ('2022-07-31'); +Error 8200 (HY000): Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` date NOT NULL, + `val` varchar(255) DEFAULT NULL, + KEY `val` (`val`), + PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_LT_2022-02-28` VALUES LESS THAN ('2022-02-28'), + PARTITION `P_LT_2022-03-31` VALUES LESS THAN ('2022-03-31'), + PARTITION `P_LT_2022-04-30` VALUES LESS THAN ('2022-04-30'), + PARTITION `P_LT_2022-05-31` VALUES LESS THAN ('2022-05-31')) +drop table t; +create table t2 (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90); +alter table t2 first partition less than (20); +alter table t2 LAST partition less than (110); +alter table t2 merge first partition less than (60); +Error 8200 (HY000): Unsupported MERGE FIRST PARTITION +alter table t2 split maxvalue partition less than (140); +Error 8200 (HY000): Unsupported SPLIT LAST PARTITION +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `id` bigint(20) unsigned NOT NULL, + `val` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */, + KEY `val` (`val`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_LT_20` VALUES LESS THAN (20), + PARTITION `P_LT_30` VALUES LESS THAN (30), + PARTITION `P_LT_40` VALUES LESS THAN (40), + PARTITION `P_LT_50` VALUES LESS THAN (50), + PARTITION `P_LT_60` VALUES LESS THAN (60), + PARTITION `P_LT_70` VALUES LESS THAN (70), + PARTITION `P_LT_80` VALUES LESS THAN (80), + PARTITION `P_LT_90` VALUES LESS THAN (90), + PARTITION `P_LT_100` VALUES LESS THAN (100), + PARTITION `P_LT_110` VALUES LESS THAN (110)) +alter table t2 first partition less than (20); +Error 8200 (HY000): Unsupported FIRST PARTITION, given value does not generate a list of partition names to be dropped +alter table t2 first partition less than (10); +Error 8200 (HY000): Unsupported INTERVAL: expr (10) not matching FIRST + n INTERVALs (20 + n * 10) +alter table t2 last partition less than (110); +Error 8200 (HY000): Unsupported INTERVAL: expr (110) not matching FIRST + n INTERVALs (110 + n * 10) +alter table t2 last partition less than (100); +Error 8200 (HY000): Unsupported INTERVAL: expr (100) not matching FIRST + n INTERVALs (110 + n * 10) +drop table t2; +create table t (id timestamp, val varchar(255)) partition by range columns (id) interval (1 minute) first partition less than ('2022-01-01 00:01:00') last partition less than ('2022-01-01 01:00:00'); +Error 1659 (HY000): Field 'id' is of a not allowed type for this type of partitioning +create table t (id timestamp, val varchar(255)) partition by range (TO_SECONDS(id)) interval (3600) first partition less than (TO_SECONDS('2022-01-01 00:00:00')) last partition less than ('2022-01-02 00:00:00'); +Error 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed +set @@time_zone = 'Europe/Amsterdam'; +create table t (id timestamp, val varchar(255)) partition by range (unix_timestamp(id)) interval (3600) first partition less than (unix_timestamp('2022-01-01 00:00:00')) last partition less than (unix_timestamp('2022-01-02 00:00:00')); +set @@time_zone = default; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` timestamp NULL DEFAULT NULL, + `val` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (UNIX_TIMESTAMP(`id`)) +(PARTITION `P_LT_1640991600` VALUES LESS THAN (1640991600), + PARTITION `P_LT_1640995200` VALUES LESS THAN (1640995200), + PARTITION `P_LT_1640998800` VALUES LESS THAN (1640998800), + PARTITION `P_LT_1641002400` VALUES LESS THAN (1641002400), + PARTITION `P_LT_1641006000` VALUES LESS THAN (1641006000), + PARTITION `P_LT_1641009600` VALUES LESS THAN (1641009600), + PARTITION `P_LT_1641013200` VALUES LESS THAN (1641013200), + PARTITION `P_LT_1641016800` VALUES LESS THAN (1641016800), + PARTITION `P_LT_1641020400` VALUES LESS THAN (1641020400), + PARTITION `P_LT_1641024000` VALUES LESS THAN (1641024000), + PARTITION `P_LT_1641027600` VALUES LESS THAN (1641027600), + PARTITION `P_LT_1641031200` VALUES LESS THAN (1641031200), + PARTITION `P_LT_1641034800` VALUES LESS THAN (1641034800), + PARTITION `P_LT_1641038400` VALUES LESS THAN (1641038400), + PARTITION `P_LT_1641042000` VALUES LESS THAN (1641042000), + PARTITION `P_LT_1641045600` VALUES LESS THAN (1641045600), + PARTITION `P_LT_1641049200` VALUES LESS THAN (1641049200), + PARTITION `P_LT_1641052800` VALUES LESS THAN (1641052800), + PARTITION `P_LT_1641056400` VALUES LESS THAN (1641056400), + PARTITION `P_LT_1641060000` VALUES LESS THAN (1641060000), + PARTITION `P_LT_1641063600` VALUES LESS THAN (1641063600), + PARTITION `P_LT_1641067200` VALUES LESS THAN (1641067200), + PARTITION `P_LT_1641070800` VALUES LESS THAN (1641070800), + PARTITION `P_LT_1641074400` VALUES LESS THAN (1641074400), + PARTITION `P_LT_1641078000` VALUES LESS THAN (1641078000)) +alter table t drop partition P_LT_1640995200; +drop table t; +create table t (id tinyint, val varchar(255)) partition by range (id) interval (50) first partition less than (-300) last partition less than (300); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` tinyint(4) DEFAULT NULL, + `val` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_LT_-300` VALUES LESS THAN (-300), + PARTITION `P_LT_-250` VALUES LESS THAN (-250), + PARTITION `P_LT_-200` VALUES LESS THAN (-200), + PARTITION `P_LT_-150` VALUES LESS THAN (-150), + PARTITION `P_LT_-100` VALUES LESS THAN (-100), + PARTITION `P_LT_-50` VALUES LESS THAN (-50), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_50` VALUES LESS THAN (50), + PARTITION `P_LT_100` VALUES LESS THAN (100), + PARTITION `P_LT_150` VALUES LESS THAN (150), + PARTITION `P_LT_200` VALUES LESS THAN (200), + PARTITION `P_LT_250` VALUES LESS THAN (250), + PARTITION `P_LT_300` VALUES LESS THAN (300)) +drop table t; +create table t (id int unsigned, val float, comment varchar(255)) partition by range columns (val) interval (1000 * 1000) first partition less than (0) last partition less than (100 * 1000 * 1000) NULL PARTITION MAXVALUE PARTITION; +Error 1659 (HY000): Field 'val' is of a not allowed type for this type of partitioning +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range columns (val) interval (1000 * 1000) first partition less than ('0') last partition less than ('10000000') NULL PARTITION MAXVALUE PARTITION; +Error 8200 (HY000): Unsupported INTERVAL partitioning, only supports Date, Datetime and INT types +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range columns (id) interval (-1000 * 1000) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +Error 8200 (HY000): Unsupported INTERVAL, should be a positive number +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (0) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +Error 8200 (HY000): Unsupported INTERVAL, should be a positive number +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval ('1000000') first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +Error 8200 (HY000): Unsupported INTERVAL, should be a positive number +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (01000000) first partition less than (0) last partition less than (10000000) MAXVALUE PARTITION; +drop table t; +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (01000000) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (NULL) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +Error 8200 (HY000): Unsupported INTERVAL, should be a positive number +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (1000000) first partition less than (NULL) last partition less than (10000000); +Error 8200 (HY000): Unsupported INTERVAL partitioning: Error when generating partition values +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (1000000) first partition less than (0) last partition less than (NULL); +Error 8200 (HY000): Unsupported INTERVAL: expr () not matching FIRST + n INTERVALs (0 + n * 1000000) +create table t (id int, val varchar(255), comment varchar(255)) partition by range (id) interval (100) first partition less than (-1000) last partition less than (-1000); +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `val` varchar(255) DEFAULT NULL, + `comment` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_LT_-1000` VALUES LESS THAN (-1000)) +alter table t last partition less than (0); +Error 8200 (HY000): Unsupported LAST PARTITION, does not seem like an INTERVAL partitioned table +alter table t add partition (partition `P_LT_0` values less than (-900)); +alter table t last partition less than (0); +Error 1517 (HY000): Duplicate partition name P_LT_0 +drop table t; +create table t (id int, val varchar(255), comment varchar(255)) partition by range (id) interval (100) first partition less than (-100) last partition less than (250); +Error 8200 (HY000): Unsupported INTERVAL: expr (250) not matching FIRST + n INTERVALs (-100 + n * 100) +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (33) first partition less than (100) last partition less than (67); +Error 8200 (HY000): Unsupported INTERVAL: expr (67) not matching FIRST + n INTERVALs (100 + n * 33) +create table t (a int, b varchar(255)); +ALTER TABLE t FIRST PARTITION LESS THAN (10); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +ALTER TABLE t LAST PARTITION LESS THAN (10); +Error 1505 (HY000): Partition management on a not partitioned table is not possible +drop table t; +create table t (a int, b varchar(255)) partition by hash (a) partitions 4; +ALTER TABLE t FIRST PARTITION LESS THAN (10); +Error 8200 (HY000): Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table +ALTER TABLE t LAST PARTITION LESS THAN (10); +Error 8200 (HY000): Unsupported LAST PARTITION of HASH/KEY partitioned table +drop table t; +create table t (a int, b varchar(255)) partition by list (a) (partition p0 values in (1,2,3), partition p1 values in (22,23,24)); +ALTER TABLE t FIRST PARTITION LESS THAN (0); +Error 8200 (HY000): Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table +ALTER TABLE t LAST PARTITION LESS THAN (100); +Error 1492 (HY000): For LIST partitions each partition must be defined +drop table t; +create table t (a int, b varchar(255)) partition by list columns (b) (partition p0 values in ("1","2","3"), partition p1 values in ("22","23","24")); +ALTER TABLE t FIRST PARTITION LESS THAN (10); +Error 8200 (HY000): Unsupported FIRST PARTITION, does not seem like an INTERVAL partitioned table +ALTER TABLE t LAST PARTITION LESS THAN (10); +Error 1492 (HY000): For LIST partitions each partition must be defined +drop table t; +drop table if exists t; +SET SESSION sql_mode='ANSI_QUOTES'; +create table t(created_at datetime) PARTITION BY RANGE COLUMNS(created_at) ( +PARTITION p0 VALUES LESS THAN ('2021-12-01 00:00:00'), +PARTITION p1 VALUES LESS THAN ('2022-01-01 00:00:00')); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "created_at" datetime DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS("created_at") +(PARTITION "p0" VALUES LESS THAN ('2021-12-01 00:00:00'), + PARTITION "p1" VALUES LESS THAN ('2022-01-01 00:00:00')) +drop table t; +set @@time_zone = 'Asia/Shanghai'; +create table t(created_at timestamp) PARTITION BY RANGE (unix_timestamp(created_at)) ( +PARTITION p0 VALUES LESS THAN (unix_timestamp('2021-12-01 00:00:00')), +PARTITION p1 VALUES LESS THAN (unix_timestamp('2022-01-01 00:00:00'))); +set @@time_zone = default; +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "created_at" timestamp NULL DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (UNIX_TIMESTAMP(`created_at`)) +(PARTITION "p0" VALUES LESS THAN (1638288000), + PARTITION "p1" VALUES LESS THAN (1640966400)) +drop table t; +set @@time_zone = default; +CREATE TABLE t (a int DEFAULT NULL, b varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a,b) ( +PARTITION p0 VALUES IN ((1,'1'),(2,'2')), +PARTITION p1 VALUES IN ((10,'10'),(11,'11'))); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" int(11) DEFAULT NULL, + "b" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS("a","b") +(PARTITION "p0" VALUES IN ((1,'1'),(2,'2')), + PARTITION "p1" VALUES IN ((10,'10'),(11,'11'))) +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( +PARTITION p0 VALUES IN ('\'','\'\'',''''''''), +PARTITION p1 VALUES IN ('""','\\','\\\'\t\n')); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS("a") +(PARTITION "p0" VALUES IN ('''','''''',''''''''), + PARTITION "p1" VALUES IN ('""','\\','\\''\t\n')) +insert into t values (0x5c27090a),('\\''\t\n'); +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( +PARTITION p0 VALUES IN ('\'','\'\'',''''''''), +PARTITION p1 VALUES IN ('\"\"','\\',0x5c27090a)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS("a") +(PARTITION "p0" VALUES IN ('''','''''',''''''''), + PARTITION "p1" VALUES IN ('""','\\',x'5c27090a')) +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( +PARTITION p0 VALUES IN ('\'','\'\'',''''''''), +PARTITION p1 VALUES IN ('""','\\',x'5c27090a')); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS("a") +(PARTITION "p0" VALUES IN ('''','''''',''''''''), + PARTITION "p1" VALUES IN ('""','\\',x'5c27090a')) +drop table t; +CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( +PARTITION p0 VALUES LESS THAN ('"'), +PARTITION p1 VALUES LESS THAN ('""'), +PARTITION p2 VALUES LESS THAN ('\''), +PARTITION p3 VALUES LESS THAN (''''''), +PARTITION p4 VALUES LESS THAN ('\\''\t\n'), +PARTITION pMax VALUES LESS THAN (MAXVALUE)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS("a") +(PARTITION "p0" VALUES LESS THAN ('"'), + PARTITION "p1" VALUES LESS THAN ('""'), + PARTITION "p2" VALUES LESS THAN (''''), + PARTITION "p3" VALUES LESS THAN (''''''), + PARTITION "p4" VALUES LESS THAN ('\\''\t\n'), + PARTITION "pMax" VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( +PARTITION p0 VALUES LESS THAN ('"'), +PARTITION p1 VALUES LESS THAN ('""'), +PARTITION p2 VALUES LESS THAN ('\''), +PARTITION p3 VALUES LESS THAN (''''''), +PARTITION p4 VALUES LESS THAN (0x5c27090a), +PARTITION pMax VALUES LESS THAN (MAXVALUE)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS("a") +(PARTITION "p0" VALUES LESS THAN ('"'), + PARTITION "p1" VALUES LESS THAN ('""'), + PARTITION "p2" VALUES LESS THAN (''''), + PARTITION "p3" VALUES LESS THAN (''''''), + PARTITION "p4" VALUES LESS THAN (x'5c27090a'), + PARTITION "pMax" VALUES LESS THAN (MAXVALUE)) +drop table t; +CREATE TABLE t (a varchar(255), b varchar(255)) PARTITION BY RANGE COLUMNS(a,b) ( +PARTITION p0 VALUES LESS THAN ('"','"'), +PARTITION p1 VALUES LESS THAN ('""','""'), +PARTITION p2 VALUES LESS THAN ('\'','\''), +PARTITION p3 VALUES LESS THAN ('''''',''''''), +PARTITION p4 VALUES LESS THAN ('\\''\t\n',0x5c27090a), +PARTITION pMax VALUES LESS THAN (MAXVALUE,maxvalue)); +insert into t values (0x5c27090a,'\\''\t\n'); +show create table t; +Table Create Table +t CREATE TABLE "t" ( + "a" varchar(255) DEFAULT NULL, + "b" varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS("a","b") +(PARTITION "p0" VALUES LESS THAN ('"','"'), + PARTITION "p1" VALUES LESS THAN ('""','""'), + PARTITION "p2" VALUES LESS THAN ('''',''''), + PARTITION "p3" VALUES LESS THAN ('''''',''''''), + PARTITION "p4" VALUES LESS THAN ('\\''\t\n','\\''\t\n'), + PARTITION "pMax" VALUES LESS THAN (MAXVALUE,MAXVALUE)) +SET SESSION sql_mode=default; +drop table if exists t; +create table t (a int, b char) partition by range (a) (partition p0 values less than (10)); +alter table t change a c int; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +drop table t; +create table t (a char, b char) partition by range columns (a) (partition p0 values less than ('z')); +alter table t change a c char; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +drop table t; +create table t (a int, b char) partition by list (a) (partition p0 values in (10)); +alter table t change a c int; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +drop table t; +create table t (a char, b char) partition by list columns (a) (partition p0 values in ('z')); +alter table t change a c char; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +drop table t; +create table t (a int, b char) partition by hash (a) partitions 3; +alter table t change a c int; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +drop table if exists t1, t2, t3, t4; +create table t1 (a tinyint, b char) partition by range (a) ( partition p0 values less than (10) ); +alter table t1 drop column a; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +alter table t1 drop column b; +create table t2 (a tinyint, b char) partition by range (a-1) ( partition p0 values less than (10) ); +alter table t2 drop column a; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +alter table t2 drop column b; +create table t3 (a tinyint, b char) partition by hash(a) partitions 4; +alter table t3 drop column a; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +alter table t3 drop column b; +create table t4 (a char, b char) partition by list columns (a) ( partition p0 values in ('0'), partition p1 values in ('a'), partition p2 values in ('b')); +alter table t4 drop column a; +Error 3855 (HY000): Column 'a' has a partitioning function dependency and cannot be dropped or renamed +alter table t4 drop column b; +drop table if exists t6; +create table t6 (colint int, col1 date) +partition by range(colint) +(partition p0 values less than (extract(year from '1998-11-23')), +partition p1 values less than maxvalue); +show create table t6; +Table Create Table +t6 CREATE TABLE `t6` ( + `colint` int(11) DEFAULT NULL, + `col1` date DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`colint`) +(PARTITION `p0` VALUES LESS THAN (1998), + PARTITION `p1` VALUES LESS THAN (MAXVALUE)) +drop table if exists tRange; +create table tRange (a int unsigned primary key, b varchar(255)) +partition by range (a) +(partition p0 values less than (1000000), +partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (1000000), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table tRange remove partitioning; +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table tRange; +create table tRange (a int unsigned primary key nonclustered, b varchar(255)) +partition by range (a) +(partition p0 values less than (1000000), +partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (1000000), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table tRange remove partitioning; +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002"); +drop table tRange; +create table tRange (a int unsigned primary key nonclustered, b varchar(255)) +partition by range (a) +(partition p0 values less than (1000000), +partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`a`) +(PARTITION `p0` VALUES LESS THAN (1000000), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table tRange remove partitioning; +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` int(10) unsigned NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002"); +drop table if exists tRange; +create table tRange (a varchar(55) primary key, b varchar(255)) +partition by range columns (a) +(partition p0 values less than ("1000000"), +partition pMax values less than maxvalue); +insert into tRange values ("0", "Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000"); +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` varchar(55) NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`a`) +(PARTITION `p0` VALUES LESS THAN ('1000000'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE)) +alter table tRange remove partitioning; +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` varchar(55) NOT NULL, + `b` varchar(255) DEFAULT NULL, + PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table if exists tRange; +create table tRange (a varchar(55), b varchar(255)) +partition by range columns (a,b) +(partition p0 values less than ("1000000","1000000"), +partition pMax values less than (maxvalue,1)); +insert into tRange values ("0", "0Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000"); +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` varchar(55) DEFAULT NULL, + `b` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`a`,`b`) +(PARTITION `p0` VALUES LESS THAN ('1000000','1000000'), + PARTITION `pMax` VALUES LESS THAN (MAXVALUE,'1')) +alter table tRange remove partitioning; +show create table tRange; +Table Create Table +tRange CREATE TABLE `tRange` ( + `a` varchar(55) DEFAULT NULL, + `b` varchar(255) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table if exists t; +create table t (a int, b varchar(255), key (a,b), key (b)) partition by hash (a) partitions 7; +insert into t values (32,char(32,32,32,32)); +insert into t values (33,char(33,33,33,33)); +insert into t values (34,char(34,34,34,34)); +insert into t values (35,char(35,35,35,35)); +insert into t values (36,char(36,36,36,36)); +insert into t values (37,char(37,37,37,37)); +insert into t values (38,char(38,38,38,38)); +insert into t values (39,char(39,39,39,39)); +insert into t values (40,char(40,40,40,40)); +insert into t values (41,char(41,41,41,41)); +insert into t values (42,char(42,42,42,42)); +insert into t values (43,char(43,43,43,43)); +insert into t values (44,char(44,44,44,44)); +insert into t values (45,char(45,45,45,45)); +insert into t values (46,char(46,46,46,46)); +insert into t values (47,char(47,47,47,47)); +insert into t values (48,char(48,48,48,48)); +insert into t values (49,char(49,49,49,49)); +insert into t values (50,char(50,50,50,50)); +insert into t values (51,char(51,51,51,51)); +insert into t values (52,char(52,52,52,52)); +insert into t values (53,char(53,53,53,53)); +insert into t values (54,char(54,54,54,54)); +insert into t values (55,char(55,55,55,55)); +insert into t values (56,char(56,56,56,56)); +insert into t values (57,char(57,57,57,57)); +insert into t values (58,char(58,58,58,58)); +insert into t values (59,char(59,59,59,59)); +insert into t values (60,char(60,60,60,60)); +insert into t values (61,char(61,61,61,61)); +insert into t values (62,char(62,62,62,62)); +insert into t values (63,char(63,63,63,63)); +insert into t values (64,char(64,64,64,64)); +insert into t values (65,char(65,65,65,65)); +insert into t values (66,char(66,66,66,66)); +insert into t values (67,char(67,67,67,67)); +insert into t values (68,char(68,68,68,68)); +insert into t values (69,char(69,69,69,69)); +insert into t values (70,char(70,70,70,70)); +insert into t values (71,char(71,71,71,71)); +insert into t values (72,char(72,72,72,72)); +insert into t values (73,char(73,73,73,73)); +insert into t values (74,char(74,74,74,74)); +insert into t values (75,char(75,75,75,75)); +insert into t values (76,char(76,76,76,76)); +insert into t values (77,char(77,77,77,77)); +insert into t values (78,char(78,78,78,78)); +insert into t values (79,char(79,79,79,79)); +insert into t values (80,char(80,80,80,80)); +insert into t values (81,char(81,81,81,81)); +insert into t values (82,char(82,82,82,82)); +insert into t values (83,char(83,83,83,83)); +insert into t values (84,char(84,84,84,84)); +insert into t values (85,char(85,85,85,85)); +insert into t values (86,char(86,86,86,86)); +insert into t values (87,char(87,87,87,87)); +insert into t values (88,char(88,88,88,88)); +insert into t values (89,char(89,89,89,89)); +insert into t values (90,char(90,90,90,90)); +insert into t values (91,char(91,91,91,91)); +insert into t values (92,char(92,92,92,92)); +insert into t values (93,char(93,93,93,93)); +insert into t values (94,char(94,94,94,94)); +insert into t values (95,char(95,95,95,95)); +insert into t values (96,char(96,96,96,96)); +insert into t values (97,char(97,97,97,97)); +insert into t values (98,char(98,98,98,98)); +insert into t values (99,char(99,99,99,99)); +insert into t values (100,char(100,100,100,100)); +insert into t values (101,char(101,101,101,101)); +insert into t values (102,char(102,102,102,102)); +insert into t values (103,char(103,103,103,103)); +insert into t values (104,char(104,104,104,104)); +insert into t values (105,char(105,105,105,105)); +insert into t values (106,char(106,106,106,106)); +insert into t values (107,char(107,107,107,107)); +insert into t values (108,char(108,108,108,108)); +insert into t values (109,char(109,109,109,109)); +insert into t values (110,char(110,110,110,110)); +insert into t values (111,char(111,111,111,111)); +insert into t values (112,char(112,112,112,112)); +insert into t values (113,char(113,113,113,113)); +insert into t values (114,char(114,114,114,114)); +insert into t values (115,char(115,115,115,115)); +insert into t values (116,char(116,116,116,116)); +insert into t values (117,char(117,117,117,117)); +insert into t values (118,char(118,118,118,118)); +insert into t values (119,char(119,119,119,119)); +insert into t values (120,char(120,120,120,120)); +insert into t values (121,char(121,121,121,121)); +insert into t values (122,char(122,122,122,122)); +insert into t values (123,char(123,123,123,123)); +insert into t values (124,char(124,124,124,124)); +insert into t values (125,char(125,125,125,125)); +insert into t values (126,char(126,126,126,126)); +analyze table t; +select partition_name, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +partition_name table_rows +p0 0 +p1 0 +p10 0 +p2 0 +p3 0 +p4 0 +p5 0 +p6 0 +p7 0 +p8 0 +p9 0 +alter table t remove partitioning; +show create table t; +Table Create Table +t CREATE TABLE `t` ( + `a` int(11) DEFAULT NULL, + `b` varchar(255) DEFAULT NULL, + KEY `a` (`a`,`b`), + KEY `b` (`b`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +drop table if exists t; +create table t (a int) partition by list (a) (partition p2 values less than (2)); +[ddl:1480]Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition +create table t (a int) partition by list (a) (partition p2); +[ddl:1479]Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition +create table t (a int) partition by list (a) (partition p1 values in (1)); +alter table t add partition (partition p2 values less than (2)); +Error 1480 (HY000): Only RANGE PARTITIONING can use VALUES LESS THAN in partition definition +alter table t add partition (partition p2); +Error 1479 (HY000): Syntax : LIST PARTITIONING requires definition of VALUES IN for each partition +drop table if exists lcp, lmcp, np, lp, t; +create table lcp (id int, create_ts datetime, name varchar(10)) +partition by list columns (create_ts) +(partition p20230829 values in ('2023-08-29'),partition p20230830 values in ('2023-08-30')); +insert into lcp values (1,'2023-08-29','a'); +insert into lcp values (2,'2023-08-30','b'); +insert into lcp values (3,'2023-08-31','c'); +Error 1526 (HY000): Table has no partition for value from column_list +create table t (id int, create_ts datetime, name varchar(10)); +insert into t values (3,'2023-08-31','c'); +alter table lcp EXCHANGE PARTITION p20230829 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lcp add partition +(partition p202302 values in ('2023-02-01','2023-02-28',null), +partition p202303 values in ('2023-03-01','2023-03-02','2023-03-31')); +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +truncate table t; +insert into t values (4,'2023-02-01','d'), (5,'2023-02-28','e'), (6, null, 'f'); +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +insert into t values (4,'2023-03-01','d'), (5,'2023-03-02','e'), (6,'2023-03-31','f'); +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +drop table t; +CREATE TABLE lmcp (d date, name varchar(10), data varchar(255)) +PARTITION BY LIST COLUMNS(d,name) +(partition p3 values IN (('2021-01-01','a'),('2021-01-02','b'),('2021-01-03','c')), +partition p4 values IN (('2021-01-01','b'),(null,'a'),('2021-01-01',null),(null,null)), +partition p2 values IN (('2021-01-01','c'),('2021-01-02','a')), +partition p1 values IN (('2021-01-02','c'))); +CREATE TABLE t (d date, name varchar(10), data varchar(255)); +insert into t values ('2021-01-02', 'c', "OK"); +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +insert into t values ('2021-01-01', 'c', "OK"), ('2021-01-02', 'a', "OK"); +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +insert into t values ('2021-01-01', 'a', "OK"), ('2021-01-02','b', "OK"), ('2021-01-03','c', "OK"); +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +insert into t values ('2021-01-01', 'b', "OK"), ('2021-01-01',null, "OK"), (null,'a', "OK"), (null,null,"OK"); +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +create table lp (a int, data varchar(255)) partition by list (a) (partition p0 values in (0,4), partition pNull values in (null)); +create table np (a int, data varchar(255)); +insert into np values (0,"OK"), (4,"OK"); +alter table lp EXCHANGE PARTITION pNull WITH TABLE np; +Error 1737 (HY000): Found a row that does not match the partition +alter table lp EXCHANGE PARTITION p0 WITH TABLE np; +insert into np values (null,"OK"); +alter table lp EXCHANGE PARTITION p0 WITH TABLE np; +Error 1737 (HY000): Found a row that does not match the partition +alter table lp EXCHANGE PARTITION pNull WITH TABLE np; +drop table if exists t, tcp; +CREATE TABLE t (d date, name varchar(10), data varchar(255)); +create table rcp (d date, name varchar(10), data varchar(255)) +partition by range columns (d) +(partition p20230829 values less than ('2023-08-30'),partition p20230830 values less than ('2023-08-31')); +insert into rcp values ('2023-08-29', 'a', "OK"); +insert into rcp values ('2023-08-30', 'b', "OK"); +insert into rcp values ('2023-08-31', 'c', "FAIL"); +Error 1526 (HY000): Table has no partition for value from column_list +insert into t values ('2023-08-31', 'c', "FAIL"); +alter table rcp EXCHANGE PARTITION p20230829 WITH TABLE t; +Error 1737 (HY000): Found a row that does not match the partition +set @@session.tidb_enable_table_partition = 1; +drop table if exists t1, t2, t3, t4, t5; +create table t1 ( time_recorded datetime ) +partition by range(TO_DAYS(time_recorded)) ( +partition p0 values less than (1)); +create table t2 ( time_recorded date ) +partition by range(TO_DAYS(time_recorded)) ( +partition p0 values less than (1)); +create table t3 ( time_recorded date ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1)); +create table t4 ( time_recorded date ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1)); +create table t5 ( time_recorded timestamp ) +partition by range(unix_timestamp(time_recorded)) ( +partition p1 values less than (1559192604) +); +set @@session.tidb_enable_table_partition = default; diff --git a/tests/integrationtest/r/ddl/ddl_tiflash.result b/tests/integrationtest/r/ddl/ddl_tiflash.result new file mode 100644 index 0000000000000..a68ffb0bed714 --- /dev/null +++ b/tests/integrationtest/r/ddl/ddl_tiflash.result @@ -0,0 +1,28 @@ +ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'; +Error 8200 (HY000): Unsupported multi schema change for set tiflash replica +ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2; +Error 8200 (HY000): Unsupported multi schema change for set tiflash replica +ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2; +Error 8200 (HY000): Unsupported multi schema change for set tiflash replica +ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'; +Error 8200 (HY000): Unsupported multi schema change for set tiflash replica +set SESSION tidb_batch_pending_tiflash_count=5; +set GLOBAL tidb_batch_pending_tiflash_count=6; +show session variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='5'; +Variable_name Value +tidb_batch_pending_tiflash_count 5 +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +Variable_name Value +tidb_batch_pending_tiflash_count 6 +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='1.5'; +Variable_name Value +set GLOBAL tidb_batch_pending_tiflash_count=1.5; +Error 1232 (42000): Incorrect argument type to variable 'tidb_batch_pending_tiflash_count' +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +Variable_name Value +tidb_batch_pending_tiflash_count 6 +show session variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +Variable_name Value +tidb_batch_pending_tiflash_count 6 +set global tidb_batch_pending_tiflash_count = default; +set session tidb_batch_pending_tiflash_count = default; diff --git a/tests/integrationtest/r/ddl/resource_group.result b/tests/integrationtest/r/ddl/resource_group.result new file mode 100644 index 0000000000000..4123c083a198d --- /dev/null +++ b/tests/integrationtest/r/ddl/resource_group.result @@ -0,0 +1,29 @@ +drop table if exists t; +drop resource group if exists rg1; +create table t1(c1 int); +set global tidb_enable_resource_control='on'; +create resource group rg1 ru_per_sec=1000; +select /*+ resource_group(default) */ * from t1; +c1 +select /*+ resource_group(rg1) */ * from t1; +c1 +select /*+ resource_group(rg1) resource_group(default) */ * from t1; +c1 +show warnings; +Level Code Message +Warning 1105 RESOURCE_GROUP() is defined more than once, only the last definition takes effect: RESOURCE_GROUP(default) +select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist where DB = "ddl__resource_group"; +DB RESOURCE_GROUP +ddl__resource_group rg1 +select DB, RESOURCE_GROUP from information_schema.processlist; +DB RESOURCE_GROUP +ddl__resource_group default +set global tidb_enable_resource_control='off'; +select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist where DB = "ddl__resource_group"; +DB RESOURCE_GROUP +ddl__resource_group default +show warnings; +Level Code Message +Warning 8250 Resource control feature is disabled. Run `SET GLOBAL tidb_enable_resource_control='on'` to enable the feature +set global tidb_enable_resource_control=default; +drop resource group rg1; diff --git a/tests/integrationtest/r/ddl/serial.result b/tests/integrationtest/r/ddl/serial.result new file mode 100644 index 0000000000000..0de92fadf720e --- /dev/null +++ b/tests/integrationtest/r/ddl/serial.result @@ -0,0 +1,90 @@ +drop table if exists partition_table; +create table partition_table (v int) partition by hash (v) partitions 10; +insert into partition_table values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); +alter table partition_table truncate partition all; +select count(*) from partition_table; +count(*) +0 +drop table if exists auto_random_temporary, t; +create global temporary table auto_random_temporary (a bigint primary key auto_random(3), b varchar(255)) on commit delete rows; +Error 8006 (HY000): `auto_random` is unsupported on temporary tables. +create temporary table t(a bigint key auto_random); +Error 8006 (HY000): `auto_random` is unsupported on temporary tables. +drop table if exists t; +create table t (a bigint auto_random(5, 64) primary key, b int); +drop table if exists t; +create table t (a bigint unsigned auto_random(5, 32) primary key, b int); +drop table if exists t; +create table t (a bigint auto_random(5, 31) primary key, b int); +Error 8216 (HY000): Invalid auto random: auto_random range bits must be between 32 and 64, but got 31 +create table t (a bigint auto_random(5, 65) primary key, b int); +Error 8216 (HY000): Invalid auto random: auto_random range bits must be between 32 and 64, but got 65 +create table t (a bigint auto_random(15, 32) primary key, b int); +Error 8216 (HY000): Invalid auto random: auto_random ID space is too small, please decrease the shard bits or increase the range bits +create table t (a bigint auto_random(5, 64) primary key, b int); +alter table t modify column a bigint auto_random(5, 32); +Error 8216 (HY000): Invalid auto random: alter the range bits of auto_random column is not supported +alter table t modify column a bigint auto_random(15, 64); +create database dct; +use dct; +create table t(b varchar(10) collate utf8_bin, c varchar(10) collate utf8_general_ci) collate utf8_bin; +alter table t modify b varchar(10) collate utf8_general_ci; +alter table t modify c varchar(10) collate utf8_bin; +alter table t modify c varchar(10) collate utf8_unicode_ci; +alter table t charset utf8 collate utf8_general_ci; +alter table t convert to charset utf8 collate utf8_bin; +alter table t convert to charset utf8 collate utf8_unicode_ci; +alter table t convert to charset utf8 collate utf8_general_ci; +alter table t modify b varchar(10) collate utf8_unicode_ci; +alter table t modify b varchar(10) collate utf8_bin; +alter table t add index b_idx(b); +alter table t add index c_idx(c); +alter table t modify b varchar(10) collate utf8_general_ci; +Error 8200 (HY000): Unsupported modifying collation of column 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it. +alter table t modify c varchar(10) collate utf8_bin; +Error 8200 (HY000): Unsupported modifying collation of column 'c' from 'utf8_general_ci' to 'utf8_bin' when index is defined on it. +alter table t modify c varchar(10) collate utf8_unicode_ci; +Error 8200 (HY000): Unsupported modifying collation of column 'c' from 'utf8_general_ci' to 'utf8_unicode_ci' when index is defined on it. +alter table t convert to charset utf8 collate utf8_general_ci; +Error 8200 (HY000): Unsupported converting collation of column 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it. +alter table t modify c varchar(10) collate utf8mb4_general_ci; +alter table t collate utf8mb4_general_ci; +alter table t charset utf8mb4 collate utf8mb4_bin; +alter table t charset utf8mb4 collate utf8mb4_unicode_ci; +alter table t charset utf8mb4 collate utf8mb4_zh_pinyin_tidb_as_cs; +alter database dct charset utf8mb4 collate utf8mb4_general_ci; +use ddl__serial; +drop database dct; +drop database if exists ucd; +create database ucd charset utf8mb4 collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +create database ucd charset utf8 collate utf8_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8_roman_ci' +create database ucd; +alter database ucd charset utf8mb4 collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +alter database ucd collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +use ucd; +create table t(a varchar(20)) charset utf8mb4 collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +create table t(a varchar(20)) collate utf8_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8_roman_ci' +create table t(a varchar(20)) collate utf8mb4_general_ci; +alter table t default collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +alter table t convert to charset utf8mb4 collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +create table t1(a varchar(20)) collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +create table t1(a varchar(20)) charset utf8 collate utf8_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8_roman_ci' +create table t1(a varchar(20)); +alter table t1 modify a varchar(20) collate utf8mb4_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8mb4_roman_ci' +alter table t1 modify a varchar(20) charset utf8 collate utf8_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8_roman_ci' +alter table t1 modify a varchar(20) charset utf8 collate utf8_roman_ci; +Error 1273 (HY000): Unsupported collation when new collation is enabled: 'utf8_roman_ci' +drop database if exists ucd; +use ddl__serial; diff --git a/tests/integrationtest/r/planner/core/integration_partition.result b/tests/integrationtest/r/planner/core/integration_partition.result index 53918f543509c..8110fb3f66387 100644 --- a/tests/integrationtest/r/planner/core/integration_partition.result +++ b/tests/integrationtest/r/planner/core/integration_partition.result @@ -653,11 +653,12 @@ c1 c2 c3 c4 4 4 4 4 drop table t2; drop database issue_27532; -use test; +use planner__core__integration_partition; set @@tidb_partition_prune_mode = 'dynamic'; select @@tidb_partition_prune_mode; @@tidb_partition_prune_mode dynamic +drop table if exists t1; create table t1 (id int, c date) partition by range (to_days(c)) (partition p0 values less than (to_days('2022-01-11')), partition p1 values less than (to_days('2022-02-11')), @@ -666,14 +667,14 @@ analyze table t1; explain select * from t1 where c in ('2022-01-23', '2022-01-22'); id estRows task access object operator info TableReader_7 20.00 root partition:p1 data:Selection_6 -└─Selection_6 20.00 cop[tikv] in(test.t1.c, 2022-01-23 00:00:00.000000, 2022-01-22 00:00:00.000000) +└─Selection_6 20.00 cop[tikv] in(planner__core__integration_partition.t1.c, 2022-01-23 00:00:00.000000, 2022-01-22 00:00:00.000000) └─TableFullScan_5 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo select * from t1 where c in ('2022-01-23', '2022-01-22'); id c explain select * from t1 where c in (NULL, '2022-01-23'); id estRows task access object operator info TableReader_7 10.00 root partition:p0,p1 data:Selection_6 -└─Selection_6 10.00 cop[tikv] in(test.t1.c, NULL, 2022-01-23 00:00:00.000000) +└─Selection_6 10.00 cop[tikv] in(planner__core__integration_partition.t1.c, NULL, 2022-01-23 00:00:00.000000) └─TableFullScan_5 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo select * from t1 where c in (NULL, '2022-01-23'); id c diff --git a/tests/integrationtest/t/ddl/db_partition.test b/tests/integrationtest/t/ddl/db_partition.test new file mode 100644 index 0000000000000..ea88dfa63b86e --- /dev/null +++ b/tests/integrationtest/t/ddl/db_partition.test @@ -0,0 +1,2212 @@ +# TestDisableTablePartition +set @@session.tidb_enable_table_partition = 'AUTO'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( + partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +-- error 1505 +alter table t add partition ( + partition p4 values in (7), + partition p5 values in (8,9)); +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 'OFF'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( + partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +-- error 1505 +alter table t add partition ( + partition p4 values in (7), + partition p5 values in (8,9)); +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 0; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( + partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +-- error 1505 +alter table t add partition ( + partition p4 values in (7), + partition p5 values in (8,9)); +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = 'ON'; +set @@session.tidb_enable_list_partition = OFF; +drop table if exists t; +create table t (id int) partition by list (id) ( + partition p0 values in (1,2),partition p1 values in (3,4)); +show create table t; +-- error 1505 +alter table t add partition ( + partition p4 values in (7), + partition p5 values in (8,9)); +insert into t values (1),(3),(5),(100),(null); +set @@session.tidb_enable_table_partition = default; +set @@session.tidb_enable_list_partition = default; + +# TestPartitionRangeColumnsCollate +drop table if exists t; +create table t (a varchar(255) charset utf8mb4 collate utf8mb4_bin) partition by range columns (a) + (partition p0A values less than ("A"), + partition p1AA values less than ("AA"), + partition p2Aa values less than ("Aa"), + partition p3BB values less than ("BB"), + partition p4Bb values less than ("Bb"), + partition p5aA values less than ("aA"), + partition p6aa values less than ("aa"), + partition p7bB values less than ("bB"), + partition p8bb values less than ("bb"), + partition pMax values less than (MAXVALUE)); +insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"); +analyze table t; +explain format='brief' select * from t where a = "AA" collate utf8mb4_general_ci; +--sorted_result +select * from t where a = "AA" collate utf8mb4_general_ci; +explain format='brief' select * from t where a = "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a = "aa" collate utf8mb4_general_ci; +explain format='brief' select * from t where a >= "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a >= "aa" collate utf8mb4_general_ci; +explain format='brief' select * from t where a > "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a > "aa" collate utf8mb4_general_ci; +explain format='brief' select * from t where a <= "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a <= "aa" collate utf8mb4_general_ci; +explain format='brief' select * from t where a < "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a < "aa" collate utf8mb4_general_ci; +drop table t; +create table t (a varchar(255) charset utf8mb4 collate utf8mb4_general_ci) partition by range columns (a) +(partition p0 values less than ("A"), + partition p1 values less than ("aa"), + partition p2 values less than ("AAA"), + partition p3 values less than ("aaaa"), + partition p4 values less than ("B"), + partition p5 values less than ("bb"), + partition pMax values less than (MAXVALUE)); +insert into t values ("A"),("a"),("b"),("B"),("aa"),("AA"),("aA"),("Aa"),("BB"),("Bb"),("bB"),("bb"),("AB"),("BA"),("Ab"),("Ba"),("aB"),("bA"),("ab"),("ba"),("ä"),("ÄÄÄ"); +analyze table t; +explain format='brief' select * from t where a = "aa" collate utf8mb4_general_ci; +--sorted_result +select * from t where a = "aa" collate utf8mb4_general_ci; +explain format='brief' select * from t where a = "aa" collate utf8mb4_bin; +--sorted_result +select * from t where a = "aa" collate utf8mb4_bin; +explain format='brief' select * from t where a = "ä" collate utf8mb4_bin; +--sorted_result +select * from t where a = "ä" collate utf8mb4_bin; +explain format='brief' select * from t where a = "b" collate utf8mb4_bin; +--sorted_result +select * from t where a = "b" collate utf8mb4_bin; +explain format='brief' select * from t where a <= "b" collate utf8mb4_bin; +--sorted_result +select * from t where a <= "b" collate utf8mb4_bin; +explain format='brief' select * from t where a < "b" collate utf8mb4_bin; +--sorted_result +select * from t where a < "b" collate utf8mb4_bin; +explain format='brief' select * from t where a >= "b" collate utf8mb4_bin; +--sorted_result +select * from t where a >= "b" collate utf8mb4_bin; +explain format='brief' select * from t where a > "b" collate utf8mb4_bin; +--sorted_result +select * from t where a > "b" collate utf8mb4_bin; + +# TestAlterTableAddPartitionByList +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +-- error 1495 +create table t (a int) partition by list (a) (partition p1 values in (null, 6), partition p2 values in (7,null)); +create table t (id int) partition by list (id) ( + partition p0 values in (1,2), + partition p1 values in (3,4), + partition p3 values in (5,null) +); +alter table t add partition ( + partition p4 values in (7), + partition p5 values in (8,9)); +-- error 1495 +alter table t add partition (partition p6 values in (null, 6)); +alter table t add partition (partition pDef values in (default, 6)); +-- error 8200 +alter table t add partition (partition pDef2 values in (10, default)); +-- error 8200 +alter table t add partition (partition pDef2 values in (10)); +show create table t; +--sorted_result +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema ='ddl__db_partition'; +alter table t reorganize partition pDef into (partition p_6 values in (6)); +-- error 1517 +alter table t add partition (partition p4 values in (7)); +-- error 1480 +alter table t add partition (partition p6 values less than (7)); +-- error 1495 +alter table t add partition (partition p6 values in (null)); +-- error 1495 +alter table t add partition (partition p6 values in (7)); +-- error 1697 +alter table t add partition (partition p6 values in ('a')); +-- error 1517 +alter table t add partition (partition p5 values in (10),partition p6 values in (7)); +drop table if exists t; +create table t (a bigint unsigned) partition by list columns (a) (partition p0 values in (1)); +-- error 1654 +alter table t add partition (partition p1 values in (-1)); +drop table if exists t; +create table t (a varchar(2)) partition by list columns (a) (partition p0 values in ('a','b')); +-- error 1654 +alter table t add partition (partition p1 values in ('abc')); +drop table if exists t; +create table t (a tinyint) partition by list columns (a) (partition p0 values in (1,2,3)); +-- error 1654 +alter table t add partition (partition p1 values in (65536)); +drop table if exists t; +create table t (a bigint) partition by list columns (a) (partition p0 values in (1,2,3)); +-- error 1654 +alter table t add partition (partition p1 values in (18446744073709551615)); +drop table if exists t; +create table t (a char) partition by list columns (a) (partition p0 values in ('a','b')); +-- error 1654 +alter table t add partition (partition p1 values in ('abc')); +drop table if exists t; +create table t (a datetime) partition by list columns (a) (partition p0 values in ('2020-11-30 12:00:00')); +-- error 1654 +alter table t add partition (partition p1 values in ('2020-11-31 12:00:00')); +set @@session.tidb_enable_list_partition = default; + +# TestAlterTableAddPartitionByListColumns +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int, name varchar(10)) partition by list columns (id,name) ( + partition p0 values in ((1,'a'),(2,'b')), + partition p1 values in ((3,'a'),(4,'b')), + partition p3 values in ((5,null)) + ); +-- error 1064 +create table t (a int) partition by list (a) (partition p0 values in (default), partition p1 values in (maxvalue)); +alter table t add partition ( + partition p4 values in ((7,'a')), + partition p5 values in ((8,'a'))); +# We only support a single DEFAULT (catch-all), +# Not a DEFAULT per column in LIST COLUMNS! +-- error 1653 +alter table t add partition ( + partition pDef values in (10, default)); +-- error 1064 +alter table t add partition ( + partition pDef values in ((10, default))); +-- error 1653 +alter table t add partition ( + partition pDef values in (default, 10)); +-- error 1064 +alter table t add partition ( + partition pDef values in ((default, 10))); +-- error 1064 +alter table t add partition ( + partition pDef values in ((9,'a'), (default, 10, 'q')); +-- error 1064 +alter table t add partition ( + partition pDef values in ((9,'a'), (10, default, 'q')); +alter table t add partition ( + partition pDef values in (default)); +-- error 8200 +alter table t add partition ( + partition pDef2 values in (default)); +-- error 8200 +alter table t add partition (partition p6 values in ((9,'d'))); +-- error 8200 +alter table t add partition (partition p6 values in (default)); +alter table t drop partition pDef; +alter table t add partition (partition pDef default); +alter table t drop partition pDef; +alter table t add partition ( + partition pDef values in ((9, 'c'), default)); +# DEFAULT cannot be within parentheses (i.e. like a list). +-- error 1064 +alter table t add partition ( + partition pDef values in ((9, 'c'), (default))); +alter table t drop partition pDef; +alter table t add partition ( + partition pDef values in (default, (9,'c'))); +alter table t drop partition pDef; +alter table t add partition ( + partition pDef values in ((9,'d'), default)); +alter table t drop partition pDef; +alter table t add partition ( + partition pDef values in (default, (9,'c'), (10, 'd'))); +alter table t drop partition pDef; +alter table t add partition ( + partition pDef values in ((9,'d'), default, (10, 'd'))); +# After a default value is included in a partition, +# it can no longer support ADD PARTITION, but should do REORGANIZE instead. +-- error 1064 +alter table t add partition ( + partition pDef values in ((9,'a'), (10, default, 'q')); +show create table t; +--sorted_result +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +show create table t; +alter table t reorganize partition pDef into (partition pd VALUES IN ((9,'d'),(10,'d'))); +-- error 1517 +alter table t add partition (partition p4 values in ((7,'b'))); +-- error 1480 +alter table t add partition (partition p6 values less than ((7,'a'))); +-- error 1495 +alter table t add partition (partition p6 values in ((5,null))); +-- error 1654 +alter table t add partition (partition p6 values in (('a','a'))); +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL, + `name` varchar(10) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY LIST COLUMNS(`id`,`name`) +(PARTITION `p0` VALUES IN ((1,'a'),(2,'b')), + PARTITION `p1` VALUES IN ((3,'a'),(4,'b')), + PARTITION `p3` VALUES IN ((5,NULL)), + PARTITION `p4` VALUES IN ((7,'a')), + PARTITION `p5` VALUES IN ((8,'a')), + PARTITION `pDef` VALUES IN ((9,'d'),DEFAULT,(10,'d'))); +--sorted_result +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +set @@session.tidb_enable_list_partition = OFF; +alter table t drop partition pDef; +-- error 1479 +alter table t add partition (partition pDef); +alter table t add partition (partition pDef default); +--sorted_result +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +alter table t reorganize partition pDef into (partition pDef VALUES IN (DEFAULT,(9,'c'))); +--sorted_result +select partition_name,partition_ordinal_position,partition_method,partition_expression,partition_description,partition_comment,tidb_placement_policy_name from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +set @@session.tidb_enable_list_partition = default; + +# TestDefaultListPartition +drop table if exists t; +create table t (a int, b varchar(255), unique key (a), key (b)) partition by list (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10)); +insert into t values (1, "1"), (2, "2"), (3,'3'), (null, "null"), (4, "4"), (11, "11"); +analyze table t; +select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +show create table t; +set @@tidb_partition_prune_mode = 'dynamic'; +select * from t where a = 4; +select * from t where a is null; +select * from t where a = 3; +explain format = 'brief' select * from t where a = 4; +explain format = 'brief' select * from t where a is null; +explain format = 'brief' select * from t where a = 3; +set @@tidb_partition_prune_mode = 'static'; +select * from t where a = 4; +select * from t where a is null; +select * from t where a = 3; +explain format = 'brief' select * from t where a = 4; +explain format = 'brief' select * from t where a is null; +explain format = 'brief' select * from t where a = 3; +# TODO: check ranges with 1,2 and 3 matching values, including 'holes' matching DEFAULT +# DEFAULT partition will not be used in reorganize partition if not included in the source/destination +-- error 8112 +alter table t reorganize partition p0 into (partition p0 values in (0)); +drop table t; +CREATE TABLE t (a VARCHAR(100),b INT) PARTITION BY LIST COLUMNS (a) (PARTITION p1 VALUES IN ('a', 'b', 'DEFAULT'),PARTITION pDef DEFAULT); +set @@tidb_partition_prune_mode = default; + +# TestDefaultListColumnPartition +drop table if exists t; +create table t (b int, a varchar(255)) partition by list columns (a) (partition p0 values in (0,4), partition p1 values in (1, null, default), partition p2 values in (2,7,10)); +show create table t; +insert into t values (1, "1"), (2, "2"), (3,'3'), (null, null), (4, "4"), (11, "11"); +analyze table t; +--sorted_result +select * from t partition(p0); +--sorted_result +select * from t partition(p1); +--sorted_result +select partition_name, partition_method, partition_expression, partition_description, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +set @@tidb_partition_prune_mode = 'dynamic'; +explain format = 'brief' select * from t where a is null; +--sorted_result +select * from t where a is null; +--sorted_result +select * from t where a = 4; +explain format = 'brief' select * from t where a = 4; +--sorted_result +select * from t where a = 3; +explain format = 'brief' select * from t where a = 3; +explain format = 'brief' select * from t where a = "3"; +--sorted_result +select * from t where a = "3"; +--sorted_result +select * from t where a = "4"; +explain format = 'brief' select * from t where a = "4"; +# Notice in string context that "35" is between "3" and "4" +explain format = 'brief' select * from t where a between "3" and "4"; +--sorted_result +select * from t where a between "3" and "4"; +explain format = 'brief' select * from t where a >= "3" and a <= "4"; +--sorted_result +select * from t where a >= "3" and a <= "4"; +set @@tidb_partition_prune_mode = 'static'; +--sorted_result +select * from t where a is null; +explain format = 'brief' select * from t where a is null; +--sorted_result +select * from t where a = 4; +--sorted_result +select * from t where a = "4"; +explain format = 'brief' select * from t where a = "4"; +--sorted_result +select * from t where a = 3; +--sorted_result +select * from t where a = "3"; +explain format = 'brief' select * from t where a = "3"; +explain format = 'brief' select * from t where a between "3" and "4"; +--sorted_result +select * from t where a between "3" and "4"; +explain format = 'brief' select * from t where a >= "3" and a <= "4"; +--sorted_result +select * from t where a >= "3" and a <= "4"; +# DEFAULT partition will not be used in reorganize partition if not included in the source/destination +-- error 8112 +alter table t reorganize partition p0 into (partition p0 values in (0)); +set @@tidb_partition_prune_mode = default; + +# TestDefaultListErrors +drop table if exists t; +-- error 1064 +create table t (a int) partition by range (a) (partition p0 values less than (default)); +-- error 1697 +create table t (a int) partition by range (a) (partition p0 values less than ("default")); +create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than ("default")); +drop table t; +-- error 1064 +create table t (a varchar(55)) partition by range columns (a) (partition p0 values less than (default)); +-- error 1480 +create table t (a varchar(55)) partition by range columns (a) (partition p0 default); +create table t (a varchar(55)) partition by list columns (a) (partition p0 default); +insert into t values ('Hi'); +show create table t; +drop table t; +create table t (a int) partition by list (a) (partition p0 default); +insert into t values (2); +show create table t; +drop table t; +-- error 1064 +create table t (a int) partition by list (a) (partition p0 values in ()); + +# TestAlterTableDropPartitionByList +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int) partition by list (id) ( + partition p0 values in (1,2), + partition p1 values in (3,4), + partition p3 values in (5,null) +); +# TODO: Add cases for dropping DEFAULT partition +insert into t values (1),(3),(5),(null); +alter table t drop partition p1; +select * from t order by id; +show create table t; +-- error 1507 +alter table t drop partition p10; +alter table t drop partition p3; +select * from t; +-- error 1508 +alter table t drop partition p0; +set @@session.tidb_enable_list_partition = default; + +# TestAlterTableDropPartitionByListColumns +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +create table t (id int, name varchar(10)) partition by list columns (id,name) ( + partition p0 values in ((1,'a'),(2,'b')), + partition p1 values in ((3,'a'),(4,'b')), + partition p3 values in ((5,'a'),(null,null)), + partition pDef values in (default) +); +insert into t values (1,'a'),(3,'a'),(5,'a'),(null,null),(9,9); +alter table t drop partition p1; +alter table t drop partition pDef; +--sorted_result +select * from t; +show create table t; +-- error 1507 +alter table t drop partition p10; +alter table t drop partition p3; +select * from t; +-- error 1508 +alter table t drop partition p0; +set @@session.tidb_enable_list_partition = default; + +# TestAlterTableAddPartition +drop table if exists employees; +create table employees ( +id int not null, +hired date not null +) +partition by range( year(hired) ) ( + partition p1 values less than (1991), + partition p2 values less than (1996), + partition p3 values less than (2001) +); +alter table employees add partition ( + partition p4 values less than (2010), + partition p5 values less than MAXVALUE +); +show create table employees; +drop table if exists table1; +create table table1(a int); +-- error 1505 +alter table table1 add partition ( + partition p1 values less than (2010), + partition p2 values less than maxvalue +); +create table table_MustBeDefined ( + id int not null, + hired date not null +) +partition by range( year(hired) ) ( + partition p1 values less than (1991), + partition p2 values less than (1996), + partition p3 values less than (2001) +); +-- error 1492 +alter table table_MustBeDefined add partition; +drop table if exists table2; +create table table2 ( + id int not null, + hired date not null +) partition by range( year(hired) ) ( + partition p1 values less than (1991), + partition p2 values less than maxvalue +); +-- error 1481 +alter table table2 add partition ( + partition p3 values less than (2010) +); +drop table if exists table3; +create table table3 ( + id int not null, + hired date not null +) +partition by range( year(hired) ) ( + partition p1 values less than (1991), + partition p2 values less than (2001) +); +-- error 1493 +alter table table3 add partition ( + partition p3 values less than (1993) +); +-- error 1517 +alter table table3 add partition ( + partition p1 values less than (1993) +); +-- error 1517 +alter table table3 add partition ( + partition p1 values less than (1993), + partition p1 values less than (1995) +); +-- error 1517 +alter table table3 add partition ( + partition p4 values less than (1993), + partition p1 values less than (1995), + partition p5 values less than maxvalue +); +-- error 1479 +alter table table3 add partition (partition p6); +-- error 1480 +alter table table3 add partition (partition p7 values in (2018)); +-- error 1492 +alter table table3 add partition partitions 4; +alter table table3 add partition (partition p3 values less than (2001 + 10)); +CREATE TABLE tt5 ( + c3 bigint(20) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE ( c3 ) ( + PARTITION p0 VALUES LESS THAN (-3), + PARTITION p1 VALUES LESS THAN (-2) +); +ALTER TABLE tt5 add partition ( partition p2 values less than (-1) ); +ALTER TABLE tt5 add partition ( partition p3 values less than (5-1) ); +drop table if exists t; +create table t (a datetime) partition by range columns (a) (partition p1 values less than ('2019-06-01'), partition p2 values less than ('2019-07-01')); +-- error 1493 +alter table t add partition ( partition p3 values less than ('2019-07-01')); +alter table t add partition ( partition p3 values less than ('2019-08-01')); +drop table if exists t; +create table t ( + col date not null default '2000-01-01') +partition by range columns (col) ( + PARTITION p0 VALUES LESS THAN ('20190905'), + PARTITION p1 VALUES LESS THAN ('20190906') +); +-- error 1654 +alter table t add partition (partition p2 values less than (20190907)); + +# TestAlterTableDropPartition +drop table if exists employees; +create table employees ( + id int not null, + hired int not null +) +partition by range( hired ) ( + partition p1 values less than (1991), + partition p2 values less than (1996), + partition p3 values less than (2001) +); +alter table employees drop partition p3; +show create table employees; +drop table if exists table1; +create table table1 (a int); +-- error 1505 +alter table table1 drop partition p10; +drop table if exists table2; +create table table2 ( + id int not null, + hired date not null +) +partition by range( year(hired) ) ( + partition p1 values less than (1991), + partition p2 values less than (1996), + partition p3 values less than (2001) +); +-- error 1507 +alter table table2 drop partition p10; +drop table if exists table3; +create table table3 ( + id int not null +) +partition by range( id ) ( + partition p1 values less than (1991) +); +-- error 1508 +alter table table3 drop partition p1; +drop table if exists table4; +create table table4 ( + id int not null +) +partition by range( id ) ( + partition p1 values less than (10), + partition p2 values less than (20), + partition p3 values less than MAXVALUE +); +alter table table4 drop partition p2; +show create table table4; +drop table if exists tr; +create table tr( + id int, name varchar(50), + purchased date +) +partition by range( year(purchased) ) ( + partition p0 values less than (1990), + partition p1 values less than (1995), + partition p2 values less than (2000), + partition p3 values less than (2005), + partition p4 values less than (2010), + partition p5 values less than (2015) +); +INSERT INTO tr VALUES + (1, 'desk organiser', '2003-10-15'), + (2, 'alarm clock', '1997-11-05'), + (3, 'chair', '2009-03-10'), + (4, 'bookcase', '1989-01-10'), + (5, 'exercise bike', '2014-05-09'), + (6, 'sofa', '1987-06-05'), + (7, 'espresso maker', '2011-11-22'), + (8, 'aquarium', '1992-08-04'), + (9, 'study desk', '2006-09-16'), + (10, 'lava lamp', '1998-12-25'); +select * from tr where purchased between '1995-01-01' and '1999-12-31'; +alter table tr drop partition p2; +select * from tr where purchased between '1995-01-01' and '1999-12-31'; +select * from tr where purchased between '2010-01-01' and '2014-12-31'; +alter table tr drop partition p5; +select * from tr where purchased between '2010-01-01' and '2014-12-31'; +alter table tr drop partition p4; +select * from tr where purchased between '2005-01-01' and '2009-12-31'; +drop table if exists table4; +create table table4 ( + id int not null +) +partition by range( id ) ( + partition Par1 values less than (1991), + partition pAR2 values less than (1992), + partition Par3 values less than (1995), + partition PaR5 values less than (1996) +); +alter table table4 drop partition Par2; +alter table table4 drop partition PAR5; +-- error 1507 +alter table table4 drop partition PAR0; +CREATE TABLE t1 (a int(11), b varchar(64)) PARTITION BY HASH(a) PARTITIONS 3; +-- error 1512 +alter table t1 drop partition p2; + +# TestMultiPartitionDropAndTruncate +drop table if exists employees; +create table employees ( + hired int not null +) +partition by range( hired ) ( + partition p1 values less than (1991), + partition p2 values less than (1996), + partition p3 values less than (2001), + partition p4 values less than (2006), + partition p5 values less than (2011) +); +INSERT INTO employees VALUES (1990), (1995), (2000), (2005), (2010); +alter table employees drop partition p1, p2; +--sorted_result +select * from employees; +alter table employees truncate partition p3, p4; +select * from employees; + +# TestExchangePartitionValidation +drop table if exists t1, t1p; +CREATE TABLE t1 ( + d date NOT NULL , + name varchar(10) NOT NULL, + UNIQUE KEY (d,name) +); +CREATE TABLE t1p ( + d date NOT NULL , + name varchar(10) NOT NULL, + UNIQUE KEY (d,name) +) +PARTITION BY RANGE COLUMNS(d) +(PARTITION p202307 VALUES LESS THAN ('2023-08-01'), + PARTITION p202308 VALUES LESS THAN ('2023-09-01'), + PARTITION p202309 VALUES LESS THAN ('2023-10-01'), + PARTITION p202310 VALUES LESS THAN ('2023-11-01'), + PARTITION p202311 VALUES LESS THAN ('2023-12-01'), + PARTITION p202312 VALUES LESS THAN ('2024-01-01'), + PARTITION pfuture VALUES LESS THAN (MAXVALUE)); +insert into t1 values ("2023-08-06","0000"); +-- error 1737 +alter table t1p exchange partition p202307 with table t1 with validation; +insert into t1 values ("2023-08-06","0001"); + +# TestExchangePartitionCheckConstraint +drop database if exists db_one; +drop database if exists db_two; +create database db_one; +create database db_two; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (80, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (60, 60); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (60, 50); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (60, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (80, 120); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +insert into db_two.pt values (60, 50); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = 1; +create table db_one.nt (a int check (a > 75) not ENFORCED, b int check (b > 50) ENFORCED); +insert into db_one.nt values (60, 60); +insert into db_one.nt values (60, 80); +create table db_two.pt (a int check (a < 75) ENFORCED, b int check (b < 75 or b > 100) ENFORCED) partition by range (a) (partition p0 values less than (50), partition p1 values less than (100) ); +insert into db_two.pt values (70, 70); +insert into db_two.pt values (30, 50); +-- error 1737 +alter table db_two.pt exchange partition p1 with table db_one.nt; +set @@global.tidb_enable_check_constraint = 0; +alter table db_two.pt exchange partition p1 with table db_one.nt; +drop table db_one.nt, db_two.pt; +set @@global.tidb_enable_check_constraint = default; + +# TestExchangePartitionPlacementPolicy +drop placement policy if exists rule1; +drop placement policy if exists rule2; +drop table if exists t1, t1p; +CREATE PLACEMENT POLICY rule1 FOLLOWERS=1; +CREATE PLACEMENT POLICY rule2 FOLLOWERS=2; +CREATE TABLE t1 ( + d date NOT NULL , + name varchar(10) NOT NULL, + UNIQUE KEY (d,name) +) PLACEMENT POLICY="rule1"; +CREATE TABLE t1p ( + d date NOT NULL , + name varchar(10) NOT NULL, + UNIQUE KEY (d,name) +) PLACEMENT POLICY="rule2" +PARTITION BY RANGE COLUMNS(d) +(PARTITION p202307 VALUES LESS THAN ('2023-08-01'), + PARTITION p202308 VALUES LESS THAN ('2023-09-01'), + PARTITION p202309 VALUES LESS THAN ('2023-10-01'), + PARTITION p202310 VALUES LESS THAN ('2023-11-01'), + PARTITION p202311 VALUES LESS THAN ('2023-12-01'), + PARTITION p202312 VALUES LESS THAN ('2024-01-01'), + PARTITION pfuture VALUES LESS THAN (MAXVALUE)); +-- error 1736 +alter table t1p exchange partition p202307 with table t1; +insert into t1 values ("2023-08-06","0000"); + +# TestTiDBEnableExchangePartition +drop table if exists pt; +create table pt (a int primary key auto_increment) partition by range(a) ( + partition p0 values less than (3), + partition p1 values less than (6), + PARTITION p2 values less than (9) +); +select @@tidb_enable_exchange_partition; +create table nt(a int primary key auto_increment); +alter table pt exchange partition p0 with table nt; +show warnings; +set @@tidb_enable_exchange_partition=0; +show warnings; +select @@tidb_enable_exchange_partition; +alter table pt exchange partition p0 with table nt; +show warnings; +set @@tidb_enable_exchange_partition=1; +show warnings; +select @@tidb_enable_exchange_partition; +alter table pt exchange partition p0 with table nt; +show warnings; +set @@tidb_enable_exchange_partition=default; + +# TestPartitionUniqueKeyNeedAllFieldsInPf +drop table if exists part1; +create table part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1, col2) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part2; +create table part2 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1, col2, col3), + unique key (col3) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part3; +create table part3 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col2) +) +partition by range( col1 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part4; +create table part4 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col2), + unique key(col2) +) +partition by range( year(col2) ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part5; +create table part5 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col2, col4), + unique key(col2, col1) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1, col2) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1), + unique key (col3) +) +partition by range( col1 + col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1), + unique key (col3) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + unique key (col1, col2, col3), + unique key (col3) +) +partition by range( col1 + col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col2) +) +partition by range( col3 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col3), + unique key(col2) +) +partition by range( year(col2) ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists Part1; +-- error 1503 +create table Part1 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + primary key(col1, col3, col4), + unique key(col2, col1) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +drop table if exists part6; +-- error 1503 +create table part6 ( + col1 int not null, + col2 date not null, + col3 int not null, + col4 int not null, + col5 int not null, + unique key(col1, col2), + unique key(col1, col3) +) +partition by range( col1 + col2 ) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +-- error 1503 +create table part7 ( + col1 int not null, + col2 int not null, + col3 int not null unique, + unique key(col1, col2) +) +partition by range (col1 + col2) ( +partition p1 values less than (11), +partition p2 values less than (15) +); +-- error 1503 +create table part8 ( + a int not null, + b int not null, + c int default null, + d int default null, + e int default null, + primary key (a, b), + unique key (c, d) +) +partition by range columns (b) ( + partition p0 values less than (4), + partition p1 values less than (7), + partition p2 values less than (11) +); +-- error 1503 +create table part9 ( + a int not null, + b int not null, + c int default null, + d int default null, + e int default null, + primary key (a, b), + unique key (b, c, d) +) +partition by range columns (b, c) ( + partition p0 values less than (4, 5), + partition p1 values less than (7, 9), + partition p2 values less than (11, 22) +); +-- error 1503 +create table part12 (a varchar(20), b binary, unique index (a(5))) partition by range columns (a) ( + partition p0 values less than ('aaaaa'), + partition p1 values less than ('bbbbb'), + partition p2 values less than ('ccccc')); +create table part12 (a varchar(20), b binary) partition by range columns (a) ( + partition p0 values less than ('aaaaa'), + partition p1 values less than ('bbbbb'), + partition p2 values less than ('ccccc')); +-- error 1503 +alter table part12 add unique index (a(5)); +create table part13 (a varchar(20), b varchar(10), unique index (a(5),b)) partition by range columns (b) ( + partition p0 values less than ('aaaaa'), + partition p1 values less than ('bbbbb'), + partition p2 values less than ('ccccc')); + +# TestCoalescePartition +drop table if exists t; +create table t ( + id int, + fname varchar(30), + lname varchar(30), + signed date +) +partition by hash( month(signed) ) +partitions 12; +insert into t values (0, "Joe", "Doe", from_days(738974 + 0)); +insert into t values (1, "Joe", "Doe", from_days(738974 + 3)); +insert into t values (2, "Joe", "Doe", from_days(738974 + 6)); +insert into t values (3, "Joe", "Doe", from_days(738974 + 9)); +insert into t values (4, "Joe", "Doe", from_days(738974 + 12)); +insert into t values (5, "Joe", "Doe", from_days(738974 + 15)); +insert into t values (6, "Joe", "Doe", from_days(738974 + 18)); +insert into t values (7, "Joe", "Doe", from_days(738974 + 21)); +insert into t values (8, "Joe", "Doe", from_days(738974 + 24)); +insert into t values (9, "Joe", "Doe", from_days(738974 + 27)); +insert into t values (10, "Joe", "Doe", from_days(738974 + 30)); +insert into t values (11, "Joe", "Doe", from_days(738974 + 33)); +insert into t values (12, "Joe", "Doe", from_days(738974 + 36)); +insert into t values (13, "Joe", "Doe", from_days(738974 + 39)); +insert into t values (14, "Joe", "Doe", from_days(738974 + 42)); +insert into t values (15, "Joe", "Doe", from_days(738974 + 45)); +insert into t values (16, "Joe", "Doe", from_days(738974 + 48)); +insert into t values (17, "Joe", "Doe", from_days(738974 + 51)); +insert into t values (18, "Joe", "Doe", from_days(738974 + 54)); +insert into t values (19, "Joe", "Doe", from_days(738974 + 57)); +insert into t values (20, "Joe", "Doe", from_days(738974 + 60)); +insert into t values (21, "Joe", "Doe", from_days(738974 + 63)); +insert into t values (22, "Joe", "Doe", from_days(738974 + 66)); +insert into t values (23, "Joe", "Doe", from_days(738974 + 69)); +insert into t values (24, "Joe", "Doe", from_days(738974 + 72)); +insert into t values (25, "Joe", "Doe", from_days(738974 + 75)); +insert into t values (26, "Joe", "Doe", from_days(738974 + 78)); +insert into t values (27, "Joe", "Doe", from_days(738974 + 81)); +insert into t values (28, "Joe", "Doe", from_days(738974 + 84)); +insert into t values (29, "Joe", "Doe", from_days(738974 + 87)); +alter table t coalesce partition 4; +show create table t; +analyze table t; +--sorted_result +select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +(select 'p0', count(*) from t partition (p0) UNION select 'p1', count(*) from t partition (p1) UNION select 'p2', count(*) from t partition (p2) UNION select 'p3', count(*) from t partition (p3) UNION select 'p4', count(*) from t partition (p4) UNION select 'p5', count(*) from t partition (p5) UNION select 'p6', count(*) from t partition (p6) UNION select 'p7', count(*) from t partition (p7)) ORDER BY 1; +drop table t; +create table t ( + id int, + fname varchar(30), + lname varchar(30), + signed date +) +partition by key(signed,fname) +partitions 12; +insert into t values (0, "Joe", "Doe", from_days(738974 + 0)); +insert into t values (1, "Joe", "Doe", from_days(738974 + 3)); +insert into t values (2, "Joe", "Doe", from_days(738974 + 6)); +insert into t values (3, "Joe", "Doe", from_days(738974 + 9)); +insert into t values (4, "Joe", "Doe", from_days(738974 + 12)); +insert into t values (5, "Joe", "Doe", from_days(738974 + 15)); +insert into t values (6, "Joe", "Doe", from_days(738974 + 18)); +insert into t values (7, "Joe", "Doe", from_days(738974 + 21)); +insert into t values (8, "Joe", "Doe", from_days(738974 + 24)); +insert into t values (9, "Joe", "Doe", from_days(738974 + 27)); +insert into t values (10, "Joe", "Doe", from_days(738974 + 30)); +insert into t values (11, "Joe", "Doe", from_days(738974 + 33)); +insert into t values (12, "Joe", "Doe", from_days(738974 + 36)); +insert into t values (13, "Joe", "Doe", from_days(738974 + 39)); +insert into t values (14, "Joe", "Doe", from_days(738974 + 42)); +insert into t values (15, "Joe", "Doe", from_days(738974 + 45)); +insert into t values (16, "Joe", "Doe", from_days(738974 + 48)); +insert into t values (17, "Joe", "Doe", from_days(738974 + 51)); +insert into t values (18, "Joe", "Doe", from_days(738974 + 54)); +insert into t values (19, "Joe", "Doe", from_days(738974 + 57)); +insert into t values (20, "Joe", "Doe", from_days(738974 + 60)); +insert into t values (21, "Joe", "Doe", from_days(738974 + 63)); +insert into t values (22, "Joe", "Doe", from_days(738974 + 66)); +insert into t values (23, "Joe", "Doe", from_days(738974 + 69)); +insert into t values (24, "Joe", "Doe", from_days(738974 + 72)); +insert into t values (25, "Joe", "Doe", from_days(738974 + 75)); +insert into t values (26, "Joe", "Doe", from_days(738974 + 78)); +insert into t values (27, "Joe", "Doe", from_days(738974 + 81)); +insert into t values (28, "Joe", "Doe", from_days(738974 + 84)); +insert into t values (29, "Joe", "Doe", from_days(738974 + 87)); +alter table t coalesce partition 4; +show create table t; +analyze table t; +--sorted_result +select partition_name, table_rows from information_schema.partitions where table_name = 't' and table_schema = 'ddl__db_partition'; +(select 'p0', count(*) from t partition (p0) UNION select 'p1', count(*) from t partition (p1) UNION select 'p2', count(*) from t partition (p2) UNION select 'p3', count(*) from t partition (p3) UNION select 'p4', count(*) from t partition (p4) UNION select 'p5', count(*) from t partition (p5) UNION select 'p6', count(*) from t partition (p6) UNION select 'p7', count(*) from t partition (p7)) ORDER BY 1; + +# TestAddHashPartition +drop table if exists t; +create table t ( + id int not null, + fname varchar(30), + lname varchar(30), + hired date not null default '1970-01-01', + separated date, + job_code int, + store_id int +) +partition by hash(store_id) +partitions 4; +insert into t values (1,"one", "two","1999-01-04", null, 1, 1), (2, "Who", "Else", '2020-12-02', '2022-12-31', 2, 2); +insert into t select id + 2, fname, lname, hired, separated, job_code, store_id + 2 from t; +insert into t select id + 4, fname, lname, hired, separated, job_code, store_id + 4 from t; +insert into t select id + 8, fname, lname, hired, separated, job_code, store_id + 8 from t; +insert into t select id + 16, fname, lname, hired, separated, job_code, store_id + 16 from t; +insert into t select id + 32, fname, lname, hired, separated, job_code, store_id + 32 from t; +insert into t select id + 64, fname, lname, hired, separated, job_code, store_id + 64 from t; +insert into t select id + 128, fname, lname, hired, separated, job_code, store_id + 128 from t; +insert into t select id + 256, fname, lname, hired, separated, job_code, store_id + 256 from t; +insert into t select id + 512, fname, lname, hired, separated, job_code, store_id + 512 from t; +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +-- error 1480 +alter table t add partition (partition pHist history); +-- error 1480 +alter table t add partition (partition pList values in (22)); +-- error 1480 +alter table t add partition (partition pRange values less than (22)); +insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1); +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +# TiDB does not support system versioned tables / SYSTEM_TIME +# also the error is slightly wrong with 'VALUES HISTORY' +# instead of just 'HISTORY' +alter table t add partition partitions 8; +show create table t; +-- error 1515 +alter table t coalesce partition 0; +-- error 1508 +alter table t coalesce partition 12; +create placement policy tworeplicas followers=1; +create placement policy threereplicas followers=2; +create placement policy fourreplicas followers=3; +alter table t add partition (partition pp13 comment 'p13' placement policy tworeplicas, partition pp14 comment 'p14' placement policy threereplicas, partition pp15 comment 'p15' placement policy fourreplicas); +alter table t add partition partitions 1; +alter table t coalesce partition 1; +show create table t; +alter table t coalesce partition 2; +show create table t; +alter table t coalesce partition 1; +alter table t add partition (partition p13 comment 'p13'); +-- error 1517 +alter table t add partition partitions 1; +alter table t coalesce partition 1; +show create table t; +alter table t add partition (partition p12 comment 'p12' placement policy tworeplicas); +alter table t placement policy fourreplicas; +alter table t add partition partitions 1; +show create table t; +alter table t remove partitioning; +show create table t; +select sum(store_id), avg(store_id), max(store_id), min(store_id), sum(id) from t; +alter table t placement policy default; +drop placement policy tworeplicas; +drop placement policy threereplicas; +drop placement policy fourreplicas; +drop table t; +create table t ( + id int not null, + fname varchar(30), + lname varchar(30), + hired date not null default '1970-01-01', + separated date, + job_code int, + store_id int +) +partition by key (hired) +partitions 4; +insert into t values (20, "Joe", "Doe", '2020-01-05', null, 1,1), (21, "Jane", "Doe", '2021-07-05', null, 2,1); +alter table t add partition partitions 8; +show create table t; +alter table t add partition (partition p13); +-- error 1517 +alter table t add partition partitions 1; +show create table t; +alter table t coalesce partition 2; +show create table t; + +# TestConstAndTimezoneDepent +set @@session.tidb_enable_table_partition = 1; +drop database if exists test_db_with_partition_const; +create database test_db_with_partition_const; +use test_db_with_partition_const; +-- error 1486 +create table t1 ( id int ) + partition by range(4) ( + partition p1 values less than (10) + ); +-- error 1486 +create table t2 ( time_recorded timestamp ) + partition by range(TO_DAYS(time_recorded)) ( + partition p1 values less than (1559192604) + ); +-- error 1486 +create table t3 ( id int ) + partition by range(DAY(id)) ( + partition p1 values less than (1) + ); +-- error 1486 +create table t4 ( id int ) + partition by hash(4) partitions 4 + ; +-- error 1486 +create table t5 ( time_recorded timestamp ) + partition by range(to_seconds(time_recorded)) ( + partition p1 values less than (1559192604) + ); +-- error 1486 +create table t6 ( id int ) + partition by range(to_seconds(id)) ( + partition p1 values less than (1559192604) + ); +-- error 1486 +create table t7 ( time_recorded timestamp ) + partition by range(abs(time_recorded)) ( + partition p1 values less than (1559192604) + ); +-- error 1486 +create table t2332 ( time_recorded time ) + partition by range(TO_DAYS(time_recorded)) ( + partition p0 values less than (1) + ); +-- error 1486 +create table t1 ( id int ) + partition by hash(4) partitions 4; +-- error 1054 +create table t1 ( id int ) + partition by hash(ed) partitions 4; +-- error 1486 +create table t2332 ( time_recorded time ) + partition by range(TO_SECONDS(time_recorded)) ( + partition p0 values less than (1) + ); +-- error 1486 +create table t2332 ( time_recorded time ) + partition by range(TO_SECONDS(time_recorded)) ( + partition p0 values less than (1) + ); +-- error 1486 +create table t2332 ( time_recorded time ) + partition by range(day(time_recorded)) ( + partition p0 values less than (1) + ); +-- error 1486 +create table t2332 ( time_recorded timestamp ) + partition by range(day(time_recorded)) ( + partition p0 values less than (1) + ); +set @@session.tidb_enable_table_partition = default; + +# TestUnsupportedPartitionManagementDDLs +drop table if exists test_1465; +create table test_1465 (a int) +partition by range(a) ( + partition p1 values less than (10), + partition p2 values less than (20), + partition p3 values less than (30) +); +alter table test_1465 truncate partition p1; +-- error 8200 +alter table test_1465 check partition p1; +-- error 8200 +alter table test_1465 optimize partition p1; +-- error 8200 +alter table test_1465 repair partition p1; +-- error 8200 +alter table test_1465 import partition p1 tablespace; +-- error 8200 +alter table test_1465 discard partition p1 tablespace; +-- error 8200 +alter table test_1465 rebuild partition p1; +-- error 1509 +alter table test_1465 coalesce partition 1; +alter table test_1465 partition by hash(a); +show create table test_1465; +-- error 1512 +alter table test_1465 drop partition p0; + +# TestCreatePartitionTableWithWrongType +drop table if exists t; +-- error 1654 +create table t( + b int(10) +) partition by range columns (b) ( + partition p0 values less than (0x10), + partition p3 values less than (0x20) +); +-- error 1654 +create table t( + b int(10) +) partition by range columns (b) ( + partition p0 values less than ('g'), + partition p3 values less than ('k') +); +-- error 1654 +create table t( + b char(10) +) partition by range columns (b) ( + partition p0 values less than (30), + partition p3 values less than (60) +); +-- error 1654 +create table t( + b datetime +) partition by range columns (b) ( + partition p0 values less than ('g'), + partition p3 values less than ('m') +); + +# TestAddPartitionForTableWithWrongType +drop tables if exists t_int, t_char, t_date; +create table t_int(b int(10)) +partition by range columns (b) ( + partition p0 values less than (10) +); +create table t_char(b char(10)) +partition by range columns (b) ( + partition p0 values less than ('a') +); +create table t_date(b datetime) +partition by range columns (b) ( + partition p0 values less than ('2020-09-01') +); +-- error 1654 +alter table t_int add partition (partition p1 values less than ('g')); +-- error 1654 +alter table t_int add partition (partition p1 values less than (0x20)); +-- error 1493 +alter table t_char add partition (partition p1 values less than (0x20)); +-- error 1654 +alter table t_char add partition (partition p1 values less than (10)); +-- error 1654 +alter table t_date add partition (partition p1 values less than ('m')); +-- error 1654 +alter table t_date add partition (partition p1 values less than (0x20)); +-- error 1654 +alter table t_date add partition (partition p1 values less than (20)); + +# TestPartitionListWithTimeType +set @@session.tidb_enable_list_partition = ON; +drop table if exists t_list1; +create table t_list1(a date) partition by list columns (a) (partition p0 values in ('2010-02-02', '20180203'), partition p1 values in ('20200202')); +insert into t_list1(a) values (20180203); +select * from t_list1 partition (p0); +set @@session.tidb_enable_list_partition = default; + +# TestPartitionListWithNewCollation +drop table if exists t; +set @@session.tidb_enable_list_partition = ON; +-- error 1495 +create table t (a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'A')); +create table t11(a char(10) collate utf8mb4_general_ci) partition by list columns (a) (partition p0 values in ('a', 'b'), partition p1 values in ('C', 'D')); +insert into t11(a) values ('A'), ('c'), ('C'), ('d'), ('B'); +analyze table t11; +select * from t11 order by a; +select * from t11 partition (p0); +select * from t11 partition (p1); +desc select * from t11 where a = 'b'; +set @@session.tidb_enable_list_partition = default; + +# TestAddTableWithPartition +drop table if exists global_partition_table; +-- error 1562 +create global temporary table global_partition_table (a int, b int) partition by hash(a) partitions 3 ON COMMIT DELETE ROWS; +drop table if exists global_partition_table; +drop table if exists partition_table; +create table partition_table (a int, b int) partition by hash(a) partitions 3; +drop table if exists partition_table; +drop table if exists partition_range_table; +-- error 1562 +create global temporary table partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( + partition p0 values less than (10), + partition p1 values less than (20), + partition p2 values less than (30), + partition p3 values less than (MAXVALUE) +) ON COMMIT DELETE ROWS; +drop table if exists partition_range_table; +drop table if exists partition_list_table; +set @@session.tidb_enable_list_partition = ON; +-- error 1562 +create global temporary table partition_list_table (id int) partition by list (id) ( + partition p0 values in (1,2), + partition p1 values in (3,4), + partition p3 values in (5,null) +) ON COMMIT DELETE ROWS; +drop table if exists partition_list_table; +drop table if exists local_partition_table; +-- error 1562 +create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3; +drop table if exists local_partition_table; +drop table if exists partition_table; +create table partition_table (a int, b int) partition by hash(a) partitions 3; +drop table if exists partition_table; +drop table if exists local_partition_range_table; +-- error 1562 +create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( + partition p0 values less than (10), + partition p1 values less than (20), + partition p2 values less than (30), + partition p3 values less than (MAXVALUE) +); +drop table if exists local_partition_range_table; +drop table if exists local_partition_list_table; +set @@session.tidb_enable_list_partition = ON; +-- error 1562 +create temporary table local_partition_list_table (id int) partition by list (id) ( + partition p0 values in (1,2), + partition p1 values in (3,4), + partition p3 values in (5,null) +); +drop table if exists local_partition_list_table; +set @@session.tidb_enable_list_partition = default; + +# TestDuplicatePartitionNames +drop table if exists t1; +set @@tidb_enable_list_partition=on; +create table t1 (a int) partition by list (a) (partition p1 values in (1), partition p2 values in (2), partition p3 values in (3)); +insert into t1 values (1),(2),(3); +alter table t1 truncate partition p1,p1; +--sorted_result +select * from t1; +insert into t1 values (1); +-- error 1507 +alter table t1 drop partition p1,p1; +-- error 1507 +alter table t1 drop partition p1,p9; +-- error 1508 +alter table t1 drop partition p1,p1,p1; +-- error 1508 +alter table t1 drop partition p1,p9,p1; +--sorted_result +select * from t1; +alter table t1 drop partition p1; +--sorted_result +select * from t1; +Show create table t1; +set @@tidb_enable_list_partition=default; + +# TestCreateIntervalPartitionSyntax +drop table if exists t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `pNull` VALUES LESS THAN (-9223372036854775808), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `p_Maxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `pNull` VALUES LESS THAN (-2147483648), + PARTITION `p_0` VALUES LESS THAN (0), + PARTITION `p_10000000` VALUES LESS THAN (10000000), + PARTITION `p_20000000` VALUES LESS THAN (20000000), + PARTITION `p_30000000` VALUES LESS THAN (30000000), + PARTITION `p_40000000` VALUES LESS THAN (40000000), + PARTITION `p_50000000` VALUES LESS THAN (50000000), + PARTITION `p_60000000` VALUES LESS THAN (60000000), + PARTITION `p_70000000` VALUES LESS THAN (70000000), + PARTITION `p_80000000` VALUES LESS THAN (80000000), + PARTITION `p_90000000` VALUES LESS THAN (90000000), + PARTITION `pMaxvalue` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; +create table t (id int) partition by range (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition; +show create table t; +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE (`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-9223372036854775808), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; +create table t (id int) partition by range columns (id) interval (10000000) first partition less than (0) last partition less than (90000000) NULL PARTITION maxvalue partition; +show create table t; +drop table t; +CREATE TABLE `t` ( + `id` int(11) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin +PARTITION BY RANGE COLUMNS(`id`) +(PARTITION `P_NULL` VALUES LESS THAN (-2147483648), + PARTITION `P_LT_0` VALUES LESS THAN (0), + PARTITION `P_LT_10000000` VALUES LESS THAN (10000000), + PARTITION `P_LT_20000000` VALUES LESS THAN (20000000), + PARTITION `P_LT_30000000` VALUES LESS THAN (30000000), + PARTITION `P_LT_40000000` VALUES LESS THAN (40000000), + PARTITION `P_LT_50000000` VALUES LESS THAN (50000000), + PARTITION `P_LT_60000000` VALUES LESS THAN (60000000), + PARTITION `P_LT_70000000` VALUES LESS THAN (70000000), + PARTITION `P_LT_80000000` VALUES LESS THAN (80000000), + PARTITION `P_LT_90000000` VALUES LESS THAN (90000000), + PARTITION `P_MAXVALUE` VALUES LESS THAN (MAXVALUE)); +show create table t; +drop table t; + +# TestCreateAndAlterIntervalPartition +drop table if exists ipt, t, idpt; +create table ipt (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90) MAXVALUE PARTITION; +insert into ipt values (0, '0'), (1, '1'), (2, '2'); +insert into ipt select id + 10, concat('1', val) FROM ipt; +insert into ipt select id + 20, concat('2', val) FROM ipt where id < 10; +insert into ipt select id + 30, concat('3', val) FROM ipt where id < 10; +insert into ipt select id + 40, concat('4', val) FROM ipt where id < 10; +insert into ipt select id + 50, concat('5', val) FROM ipt where id < 10; +insert into ipt select id + 60, concat('6', val) FROM ipt where id < 10; +insert into ipt select id + 70, concat('7', val) FROM ipt where id < 10; +insert into ipt select id + 80, concat('8', val) FROM ipt where id < 10; +insert into ipt select id + 90, concat('9', val) FROM ipt where id < 10; +insert into ipt select id + 100, concat('10', val) FROM ipt where id < 10; +SHOW CREATE TABLE ipt; +-- error 8200 +alter table ipt LAST partition less than (100); +alter table ipt first partition less than (30); +select count(*) from ipt; +SHOW CREATE TABLE ipt; +-- error 8200 +alter table ipt merge first partition less than (60); +-- error 8200 +alter table ipt split maxvalue partition less than (140); +select count(*) from ipt; +create table idpt (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 week) FIRST PARTITION LESS THAN ('2022-02-01') LAST PARTITION LESS THAN ('2022-03-29') NULL PARTITION MAXVALUE PARTITION; +SHOW CREATE TABLE idpt; +# Notice that '2022-01-31' + INTERVAL n MONTH returns '2022-02-28', '2022-03-31' etc. +# So having a range of the last of the month (normally what you want is LESS THAN first of the months) will work +# if using a month with 31 days. +# But managing partitions with the day-part of 29, 30 or 31 will be troublesome, since once the FIRST is not 31 +# both the ALTER TABLE t FIRST PARTITION and MERGE FIRST PARTITION will have issues +create table t (id date primary key nonclustered, val varchar(255), key (val)) partition by range COLUMNS (id) INTERVAL (1 MONTH) FIRST PARTITION LESS THAN ('2022-01-31') LAST PARTITION LESS THAN ('2022-05-31'); +show create table t; +alter table t first partition less than ('2022-02-28'); +show create table t; +# Now we are stuck, since we will use the current FIRST PARTITION to check the INTERVAL! +# Should we check and limit FIRST PARTITION for QUARTER and MONTH to not have day part in (29,30,31)? +-- error 8200 +alter table t first partition less than ('2022-03-31'); +-- error 8200 +alter table t last partition less than ('2022-06-30'); +-- error 8200 +alter table t last partition less than ('2022-07-31'); +show create table t; +drop table t; +create table t2 (id bigint unsigned primary key, val varchar(255), key (val)) partition by range (id) INTERVAL (10) FIRST PARTITION LESS THAN (10) LAST PARTITION LESS THAN (90); +alter table t2 first partition less than (20); +alter table t2 LAST partition less than (110); +-- error 8200 +alter table t2 merge first partition less than (60); +-- error 8200 +alter table t2 split maxvalue partition less than (140); +show create table t2; +-- error 8200 +alter table t2 first partition less than (20); +-- error 8200 +alter table t2 first partition less than (10); +-- error 8200 +alter table t2 last partition less than (110); +-- error 8200 +alter table t2 last partition less than (100); +drop table t2; +-- error 1659 +create table t (id timestamp, val varchar(255)) partition by range columns (id) interval (1 minute) first partition less than ('2022-01-01 00:01:00') last partition less than ('2022-01-01 01:00:00'); +-- error 1486 +create table t (id timestamp, val varchar(255)) partition by range (TO_SECONDS(id)) interval (3600) first partition less than (TO_SECONDS('2022-01-01 00:00:00')) last partition less than ('2022-01-02 00:00:00'); +set @@time_zone = 'Europe/Amsterdam'; +create table t (id timestamp, val varchar(255)) partition by range (unix_timestamp(id)) interval (3600) first partition less than (unix_timestamp('2022-01-01 00:00:00')) last partition less than (unix_timestamp('2022-01-02 00:00:00')); +set @@time_zone = default; +show create table t; +alter table t drop partition P_LT_1640995200; +drop table t; +# OK with out-of-range partitions, see https://github.com/pingcap/tidb/issues/36022 +create table t (id tinyint, val varchar(255)) partition by range (id) interval (50) first partition less than (-300) last partition less than (300); +show create table t; +drop table t; +-- error 1659 +create table t (id int unsigned, val float, comment varchar(255)) partition by range columns (val) interval (1000 * 1000) first partition less than (0) last partition less than (100 * 1000 * 1000) NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range columns (val) interval (1000 * 1000) first partition less than ('0') last partition less than ('10000000') NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range columns (id) interval (-1000 * 1000) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (0) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval ('1000000') first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (01000000) first partition less than (0) last partition less than (10000000) MAXVALUE PARTITION; +drop table t; +# Null partition and first partition collides +-- error 1493 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (01000000) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (NULL) first partition less than (0) last partition less than (10000000) NULL PARTITION MAXVALUE PARTITION; +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (1000000) first partition less than (NULL) last partition less than (10000000); +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (1000000) first partition less than (0) last partition less than (NULL); +create table t (id int, val varchar(255), comment varchar(255)) partition by range (id) interval (100) first partition less than (-1000) last partition less than (-1000); +show create table t; +-- error 8200 +alter table t last partition less than (0); +alter table t add partition (partition `P_LT_0` values less than (-900)); +-- error 1517 +alter table t last partition less than (0); +drop table t; +-- error 8200 +create table t (id int, val varchar(255), comment varchar(255)) partition by range (id) interval (100) first partition less than (-100) last partition less than (250); +-- error 8200 +create table t (id int unsigned, val varchar(255), comment varchar(255)) partition by range (id) interval (33) first partition less than (100) last partition less than (67); +# Non-partitioned tables does not support ALTER of FIRST/LAST PARTITION +create table t (a int, b varchar(255)); +-- error 1505 +ALTER TABLE t FIRST PARTITION LESS THAN (10); +-- error 1505 +ALTER TABLE t LAST PARTITION LESS THAN (10); +drop table t; +# HASH/LIST [COLUMNS] does not support ALTER of FIRST/LAST PARTITION +create table t (a int, b varchar(255)) partition by hash (a) partitions 4; +-- error 8200 +ALTER TABLE t FIRST PARTITION LESS THAN (10); +-- error 8200 +ALTER TABLE t LAST PARTITION LESS THAN (10); +drop table t; +create table t (a int, b varchar(255)) partition by list (a) (partition p0 values in (1,2,3), partition p1 values in (22,23,24)); +-- error 8200 +ALTER TABLE t FIRST PARTITION LESS THAN (0); +-- error 1492 +ALTER TABLE t LAST PARTITION LESS THAN (100); +drop table t; +create table t (a int, b varchar(255)) partition by list columns (b) (partition p0 values in ("1","2","3"), partition p1 values in ("22","23","24")); +-- error 8200 +ALTER TABLE t FIRST PARTITION LESS THAN (10); +-- error 1492 +ALTER TABLE t LAST PARTITION LESS THAN (10); +drop table t; + +# TestPartitionTableWithAnsiQuotes +drop table if exists t; +SET SESSION sql_mode='ANSI_QUOTES'; +create table t(created_at datetime) PARTITION BY RANGE COLUMNS(created_at) ( + PARTITION p0 VALUES LESS THAN ('2021-12-01 00:00:00'), + PARTITION p1 VALUES LESS THAN ('2022-01-01 00:00:00')); +show create table t; +drop table t; +set @@time_zone = 'Asia/Shanghai'; +create table t(created_at timestamp) PARTITION BY RANGE (unix_timestamp(created_at)) ( + PARTITION p0 VALUES LESS THAN (unix_timestamp('2021-12-01 00:00:00')), + PARTITION p1 VALUES LESS THAN (unix_timestamp('2022-01-01 00:00:00'))); +set @@time_zone = default; +# FIXME: should be "created_at" instead of `created_at`, see #35389. +show create table t; +drop table t; +set @@time_zone = default; +CREATE TABLE t (a int DEFAULT NULL, b varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a,b) ( + PARTITION p0 VALUES IN ((1,'1'),(2,'2')), + PARTITION p1 VALUES IN ((10,'10'),(11,'11'))); +show create table t; +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( + PARTITION p0 VALUES IN ('\'','\'\'',''''''''), + PARTITION p1 VALUES IN ('""','\\','\\\'\t\n')); +show create table t; +insert into t values (0x5c27090a),('\\''\t\n'); +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( + PARTITION p0 VALUES IN ('\'','\'\'',''''''''), + PARTITION p1 VALUES IN ('\"\"','\\',0x5c27090a)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +# https://github.com/pingcap/tidb/issues/37692 +drop table t; +CREATE TABLE t (a varchar(255) DEFAULT NULL) PARTITION BY LIST COLUMNS(a) ( + PARTITION p0 VALUES IN ('\'','\'\'',''''''''), + PARTITION p1 VALUES IN ('""','\\',x'5c27090a')); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +drop table t; +CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( + PARTITION p0 VALUES LESS THAN ('"'), + PARTITION p1 VALUES LESS THAN ('""'), + PARTITION p2 VALUES LESS THAN ('\''), + PARTITION p3 VALUES LESS THAN (''''''), + PARTITION p4 VALUES LESS THAN ('\\''\t\n'), + PARTITION pMax VALUES LESS THAN (MAXVALUE)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +drop table t; +CREATE TABLE t (a varchar(255)) PARTITION BY RANGE COLUMNS(a) ( + PARTITION p0 VALUES LESS THAN ('"'), + PARTITION p1 VALUES LESS THAN ('""'), + PARTITION p2 VALUES LESS THAN ('\''), + PARTITION p3 VALUES LESS THAN (''''''), + PARTITION p4 VALUES LESS THAN (0x5c27090a), + PARTITION pMax VALUES LESS THAN (MAXVALUE)); +insert into t values (0x5c27090a),('\\''\t\n'); +show create table t; +drop table t; +CREATE TABLE t (a varchar(255), b varchar(255)) PARTITION BY RANGE COLUMNS(a,b) ( + PARTITION p0 VALUES LESS THAN ('"','"'), + PARTITION p1 VALUES LESS THAN ('""','""'), + PARTITION p2 VALUES LESS THAN ('\'','\''), + PARTITION p3 VALUES LESS THAN ('''''',''''''), + PARTITION p4 VALUES LESS THAN ('\\''\t\n',0x5c27090a), + PARTITION pMax VALUES LESS THAN (MAXVALUE,maxvalue)); +insert into t values (0x5c27090a,'\\''\t\n'); +show create table t; +SET SESSION sql_mode=default; + +# TestAlterModifyColumnOnPartitionedTableRename +drop table if exists t; +create table t (a int, b char) partition by range (a) (partition p0 values less than (10)); +-- error 3855 +alter table t change a c int; +drop table t; +create table t (a char, b char) partition by range columns (a) (partition p0 values less than ('z')); +-- error 3855 +alter table t change a c char; +drop table t; +create table t (a int, b char) partition by list (a) (partition p0 values in (10)); +-- error 3855 +alter table t change a c int; +drop table t; +create table t (a char, b char) partition by list columns (a) (partition p0 values in ('z')); +-- error 3855 +alter table t change a c char; +drop table t; +create table t (a int, b char) partition by hash (a) partitions 3; +-- error 3855 +alter table t change a c int; + +# TestDropPartitionKeyColumn +drop table if exists t1, t2, t3, t4; +create table t1 (a tinyint, b char) partition by range (a) ( partition p0 values less than (10) ); +-- error 3855 +alter table t1 drop column a; +alter table t1 drop column b; +create table t2 (a tinyint, b char) partition by range (a-1) ( partition p0 values less than (10) ); +-- error 3855 +alter table t2 drop column a; +alter table t2 drop column b; +create table t3 (a tinyint, b char) partition by hash(a) partitions 4; +-- error 3855 +alter table t3 drop column a; +alter table t3 drop column b; +create table t4 (a char, b char) partition by list columns (a) ( partition p0 values in ('0'), partition p1 values in ('a'), partition p2 values in ('b')); +-- error 3855 +alter table t4 drop column a; +alter table t4 drop column b; + +# TestRangeExpressions +drop table if exists t6; +create table t6 (colint int, col1 date) +partition by range(colint) +(partition p0 values less than (extract(year from '1998-11-23')), +partition p1 values less than maxvalue); +show create table t6; + +# TestRemoveRangePartitioning +drop table if exists tRange; +create table tRange (a int unsigned primary key, b varchar(255)) + partition by range (a) + (partition p0 values less than (1000000), + partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +alter table tRange remove partitioning; +show create table tRange; +drop table tRange; +create table tRange (a int unsigned primary key nonclustered, b varchar(255)) +partition by range (a) +(partition p0 values less than (1000000), +partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +alter table tRange remove partitioning; +show create table tRange; +insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002"); +drop table tRange; +create table tRange (a int unsigned primary key nonclustered, b varchar(255)) +partition by range (a) +(partition p0 values less than (1000000), +partition pMax values less than maxvalue); +insert into tRange values (0, "Zero"), (999999, "999999"), (1000000, "1000000"), (20000000, "20000000"); +show create table tRange; +alter table tRange remove partitioning; +show create table tRange; +insert into tRange values (1, "One"), (999998, "999998"), (1000001, "1000001"), (20000002, "20000002"); + +# TestRemoveRangeColumnPartitioning +drop table if exists tRange; +create table tRange (a varchar(55) primary key, b varchar(255)) +partition by range columns (a) +(partition p0 values less than ("1000000"), +partition pMax values less than maxvalue); +insert into tRange values ("0", "Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000"); +show create table tRange; +alter table tRange remove partitioning; +show create table tRange; + +# TestRemoveRangeColumnsPartitioning +drop table if exists tRange; +create table tRange (a varchar(55), b varchar(255)) +partition by range columns (a,b) +(partition p0 values less than ("1000000","1000000"), +partition pMax values less than (maxvalue,1)); +insert into tRange values ("0", "0Zero"), ("0999999", "0999999"), ("1000000", "1000000"), ("20000000", "20000000"); +show create table tRange; +alter table tRange remove partitioning; +show create table tRange; + +# TestRemoveHashPartitioning +drop table if exists t; +create table t (a int, b varchar(255), key (a,b), key (b)) partition by hash (a) partitions 7; +insert into t values (32,char(32,32,32,32)); +insert into t values (33,char(33,33,33,33)); +insert into t values (34,char(34,34,34,34)); +insert into t values (35,char(35,35,35,35)); +insert into t values (36,char(36,36,36,36)); +insert into t values (37,char(37,37,37,37)); +insert into t values (38,char(38,38,38,38)); +insert into t values (39,char(39,39,39,39)); +insert into t values (40,char(40,40,40,40)); +insert into t values (41,char(41,41,41,41)); +insert into t values (42,char(42,42,42,42)); +insert into t values (43,char(43,43,43,43)); +insert into t values (44,char(44,44,44,44)); +insert into t values (45,char(45,45,45,45)); +insert into t values (46,char(46,46,46,46)); +insert into t values (47,char(47,47,47,47)); +insert into t values (48,char(48,48,48,48)); +insert into t values (49,char(49,49,49,49)); +insert into t values (50,char(50,50,50,50)); +insert into t values (51,char(51,51,51,51)); +insert into t values (52,char(52,52,52,52)); +insert into t values (53,char(53,53,53,53)); +insert into t values (54,char(54,54,54,54)); +insert into t values (55,char(55,55,55,55)); +insert into t values (56,char(56,56,56,56)); +insert into t values (57,char(57,57,57,57)); +insert into t values (58,char(58,58,58,58)); +insert into t values (59,char(59,59,59,59)); +insert into t values (60,char(60,60,60,60)); +insert into t values (61,char(61,61,61,61)); +insert into t values (62,char(62,62,62,62)); +insert into t values (63,char(63,63,63,63)); +insert into t values (64,char(64,64,64,64)); +insert into t values (65,char(65,65,65,65)); +insert into t values (66,char(66,66,66,66)); +insert into t values (67,char(67,67,67,67)); +insert into t values (68,char(68,68,68,68)); +insert into t values (69,char(69,69,69,69)); +insert into t values (70,char(70,70,70,70)); +insert into t values (71,char(71,71,71,71)); +insert into t values (72,char(72,72,72,72)); +insert into t values (73,char(73,73,73,73)); +insert into t values (74,char(74,74,74,74)); +insert into t values (75,char(75,75,75,75)); +insert into t values (76,char(76,76,76,76)); +insert into t values (77,char(77,77,77,77)); +insert into t values (78,char(78,78,78,78)); +insert into t values (79,char(79,79,79,79)); +insert into t values (80,char(80,80,80,80)); +insert into t values (81,char(81,81,81,81)); +insert into t values (82,char(82,82,82,82)); +insert into t values (83,char(83,83,83,83)); +insert into t values (84,char(84,84,84,84)); +insert into t values (85,char(85,85,85,85)); +insert into t values (86,char(86,86,86,86)); +insert into t values (87,char(87,87,87,87)); +insert into t values (88,char(88,88,88,88)); +insert into t values (89,char(89,89,89,89)); +insert into t values (90,char(90,90,90,90)); +insert into t values (91,char(91,91,91,91)); +insert into t values (92,char(92,92,92,92)); +insert into t values (93,char(93,93,93,93)); +insert into t values (94,char(94,94,94,94)); +insert into t values (95,char(95,95,95,95)); +insert into t values (96,char(96,96,96,96)); +insert into t values (97,char(97,97,97,97)); +insert into t values (98,char(98,98,98,98)); +insert into t values (99,char(99,99,99,99)); +insert into t values (100,char(100,100,100,100)); +insert into t values (101,char(101,101,101,101)); +insert into t values (102,char(102,102,102,102)); +insert into t values (103,char(103,103,103,103)); +insert into t values (104,char(104,104,104,104)); +insert into t values (105,char(105,105,105,105)); +insert into t values (106,char(106,106,106,106)); +insert into t values (107,char(107,107,107,107)); +insert into t values (108,char(108,108,108,108)); +insert into t values (109,char(109,109,109,109)); +insert into t values (110,char(110,110,110,110)); +insert into t values (111,char(111,111,111,111)); +insert into t values (112,char(112,112,112,112)); +insert into t values (113,char(113,113,113,113)); +insert into t values (114,char(114,114,114,114)); +insert into t values (115,char(115,115,115,115)); +insert into t values (116,char(116,116,116,116)); +insert into t values (117,char(117,117,117,117)); +insert into t values (118,char(118,118,118,118)); +insert into t values (119,char(119,119,119,119)); +insert into t values (120,char(120,120,120,120)); +insert into t values (121,char(121,121,121,121)); +insert into t values (122,char(122,122,122,122)); +insert into t values (123,char(123,123,123,123)); +insert into t values (124,char(124,124,124,124)); +insert into t values (125,char(125,125,125,125)); +insert into t values (126,char(126,126,126,126)); +analyze table t; +--sorted_result +select partition_name, table_rows from information_schema.partitions where table_schema = 'ddl__db_partition' and table_name = 't'; +alter table t remove partitioning; +show create table t; + +# TestListDefinitionError +drop table if exists t; +-- error 1480 +create table t (a int) partition by list (a) (partition p2 values less than (2)); +-- error 1479 +create table t (a int) partition by list (a) (partition p2); +create table t (a int) partition by list (a) (partition p1 values in (1)); +-- error 1480 +alter table t add partition (partition p2 values less than (2)); +-- error 1479 +alter table t add partition (partition p2); + +# TestListExchangeValidate +drop table if exists lcp, lmcp, np, lp, t; +create table lcp (id int, create_ts datetime, name varchar(10)) +partition by list columns (create_ts) +(partition p20230829 values in ('2023-08-29'),partition p20230830 values in ('2023-08-30')); +insert into lcp values (1,'2023-08-29','a'); +insert into lcp values (2,'2023-08-30','b'); +-- error 1526 +insert into lcp values (3,'2023-08-31','c'); +create table t (id int, create_ts datetime, name varchar(10)); +insert into t values (3,'2023-08-31','c'); +-- error 1737 +alter table lcp EXCHANGE PARTITION p20230829 WITH TABLE t; +alter table lcp add partition + (partition p202302 values in ('2023-02-01','2023-02-28',null), + partition p202303 values in ('2023-03-01','2023-03-02','2023-03-31')); +-- error 1737 +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +-- error 1737 +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +truncate table t; +insert into t values (4,'2023-02-01','d'), (5,'2023-02-28','e'), (6, null, 'f'); +-- error 1737 +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +insert into t values (4,'2023-03-01','d'), (5,'2023-03-02','e'), (6,'2023-03-31','f'); +-- error 1737 +alter table lcp EXCHANGE PARTITION p202302 WITH TABLE t; +alter table lcp EXCHANGE PARTITION p202303 WITH TABLE t; +drop table t; +CREATE TABLE lmcp (d date, name varchar(10), data varchar(255)) + PARTITION BY LIST COLUMNS(d,name) + (partition p3 values IN (('2021-01-01','a'),('2021-01-02','b'),('2021-01-03','c')), + partition p4 values IN (('2021-01-01','b'),(null,'a'),('2021-01-01',null),(null,null)), + partition p2 values IN (('2021-01-01','c'),('2021-01-02','a')), + partition p1 values IN (('2021-01-02','c'))); +CREATE TABLE t (d date, name varchar(10), data varchar(255)); +insert into t values ('2021-01-02', 'c', "OK"); +-- error 1737 +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +insert into t values ('2021-01-01', 'c', "OK"), ('2021-01-02', 'a', "OK"); +-- error 1737 +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +insert into t values ('2021-01-01', 'a', "OK"), ('2021-01-02','b', "OK"), ('2021-01-03','c', "OK"); +-- error 1737 +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +insert into t values ('2021-01-01', 'b', "OK"), ('2021-01-01',null, "OK"), (null,'a', "OK"), (null,null,"OK"); +-- error 1737 +alter table lmcp EXCHANGE PARTITION p1 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p2 WITH TABLE t; +-- error 1737 +alter table lmcp EXCHANGE PARTITION p3 WITH TABLE t; +alter table lmcp EXCHANGE PARTITION p4 WITH TABLE t; +create table lp (a int, data varchar(255)) partition by list (a) (partition p0 values in (0,4), partition pNull values in (null)); +create table np (a int, data varchar(255)); +insert into np values (0,"OK"), (4,"OK"); +-- error 1737 +alter table lp EXCHANGE PARTITION pNull WITH TABLE np; +alter table lp EXCHANGE PARTITION p0 WITH TABLE np; +insert into np values (null,"OK"); +-- error 1737 +alter table lp EXCHANGE PARTITION p0 WITH TABLE np; +alter table lp EXCHANGE PARTITION pNull WITH TABLE np; +# TODO: Check EXCHANGE with DEFAULT partition!! + +# TestRangeExchangeValidate +drop table if exists t, tcp; +CREATE TABLE t (d date, name varchar(10), data varchar(255)); +create table rcp (d date, name varchar(10), data varchar(255)) +partition by range columns (d) +(partition p20230829 values less than ('2023-08-30'),partition p20230830 values less than ('2023-08-31')); +insert into rcp values ('2023-08-29', 'a', "OK"); +insert into rcp values ('2023-08-30', 'b', "OK"); +-- error 1526 +insert into rcp values ('2023-08-31', 'c', "FAIL"); +insert into t values ('2023-08-31', 'c', "FAIL"); +-- error 1737 +alter table rcp EXCHANGE PARTITION p20230829 WITH TABLE t; +# TODO: Add test with a RANGE single partition (both normal AND maxvalue!) +# TODO: add test with maxvalue (1, 2, and more partitions) +# TODO: add test not in first partition (both last without maxvalue and also not last with/without maxvalue) + +# TestConstAndTimezoneDepent2 +set @@session.tidb_enable_table_partition = 1; +drop table if exists t1, t2, t3, t4, t5; +create table t1 ( time_recorded datetime ) +partition by range(TO_DAYS(time_recorded)) ( +partition p0 values less than (1)); +create table t2 ( time_recorded date ) +partition by range(TO_DAYS(time_recorded)) ( +partition p0 values less than (1)); +create table t3 ( time_recorded date ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1)); +create table t4 ( time_recorded date ) +partition by range(TO_SECONDS(time_recorded)) ( +partition p0 values less than (1)); +create table t5 ( time_recorded timestamp ) +partition by range(unix_timestamp(time_recorded)) ( + partition p1 values less than (1559192604) +); +set @@session.tidb_enable_table_partition = default; \ No newline at end of file diff --git a/tests/integrationtest/t/ddl/ddl_tiflash.test b/tests/integrationtest/t/ddl/ddl_tiflash.test new file mode 100644 index 0000000000000..88030e3559a5f --- /dev/null +++ b/tests/integrationtest/t/ddl/ddl_tiflash.test @@ -0,0 +1,26 @@ +# TestAlterDatabaseErrorGrammar +-- error 8200 +ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'; +-- error 8200 +ALTER DATABASE t SET TIFLASH REPLICA 1 SET TIFLASH REPLICA 2; +-- error 8200 +ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2; +-- error 8200 +ALTER DATABASE t SET TIFLASH REPLICA 1 LOCATION LABELS 'a','b' SET TIFLASH REPLICA 2 LOCATION LABELS 'a','b'; + +# TestTiFlashBatchAddVariables +set SESSION tidb_batch_pending_tiflash_count=5; +set GLOBAL tidb_batch_pending_tiflash_count=6; +show session variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='5'; +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='1.5'; +-- error 1232 +set GLOBAL tidb_batch_pending_tiflash_count=1.5; +show global variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +connect (conn1, localhost, root,, ddl__ddl_tiflash); +connection conn1; +show session variables where Variable_name='tidb_batch_pending_tiflash_count' and Value='6'; +connection default; +disconnect conn1; +set global tidb_batch_pending_tiflash_count = default; +set session tidb_batch_pending_tiflash_count = default; diff --git a/tests/integrationtest/t/ddl/resource_group.test b/tests/integrationtest/t/ddl/resource_group.test new file mode 100644 index 0000000000000..a23c3ffeaf6f9 --- /dev/null +++ b/tests/integrationtest/t/ddl/resource_group.test @@ -0,0 +1,18 @@ +# TestResourceGroupHint +drop table if exists t; +drop resource group if exists rg1; +create table t1(c1 int); +set global tidb_enable_resource_control='on'; +create resource group rg1 ru_per_sec=1000; +select /*+ resource_group(default) */ * from t1; +select /*+ resource_group(rg1) */ * from t1; +select /*+ resource_group(rg1) resource_group(default) */ * from t1; +show warnings; +select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist where DB = "ddl__resource_group"; +select DB, RESOURCE_GROUP from information_schema.processlist; +set global tidb_enable_resource_control='off'; +select /*+ resource_group(rg1) */ DB, RESOURCE_GROUP from information_schema.processlist where DB = "ddl__resource_group"; +show warnings; +set global tidb_enable_resource_control=default; +drop resource group rg1; + diff --git a/tests/integrationtest/t/ddl/serial.test b/tests/integrationtest/t/ddl/serial.test new file mode 100644 index 0000000000000..bb136851aab21 --- /dev/null +++ b/tests/integrationtest/t/ddl/serial.test @@ -0,0 +1,100 @@ +# TestTruncateAllPartitions +drop table if exists partition_table; +create table partition_table (v int) partition by hash (v) partitions 10; +insert into partition_table values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); +alter table partition_table truncate partition all; +select count(*) from partition_table; + +# TestAutoRandomOnTemporaryTable +drop table if exists auto_random_temporary, t; +-- error 8006 +create global temporary table auto_random_temporary (a bigint primary key auto_random(3), b varchar(255)) on commit delete rows; +-- error 8006 +create temporary table t(a bigint key auto_random); + +# TestAutoRandomWithRangeBits +drop table if exists t; +create table t (a bigint auto_random(5, 64) primary key, b int); +drop table if exists t; +create table t (a bigint unsigned auto_random(5, 32) primary key, b int); +drop table if exists t; +-- error 8216 +create table t (a bigint auto_random(5, 31) primary key, b int); +-- error 8216 +create table t (a bigint auto_random(5, 65) primary key, b int); +-- error 8216 +create table t (a bigint auto_random(15, 32) primary key, b int); +create table t (a bigint auto_random(5, 64) primary key, b int); +-- error 8216 +alter table t modify column a bigint auto_random(5, 32); +alter table t modify column a bigint auto_random(15, 64); + +# TestModifyingColumn4NewCollations +create database dct; +use dct; +create table t(b varchar(10) collate utf8_bin, c varchar(10) collate utf8_general_ci) collate utf8_bin; +alter table t modify b varchar(10) collate utf8_general_ci; +alter table t modify c varchar(10) collate utf8_bin; +alter table t modify c varchar(10) collate utf8_unicode_ci; +alter table t charset utf8 collate utf8_general_ci; +alter table t convert to charset utf8 collate utf8_bin; +alter table t convert to charset utf8 collate utf8_unicode_ci; +alter table t convert to charset utf8 collate utf8_general_ci; +alter table t modify b varchar(10) collate utf8_unicode_ci; +alter table t modify b varchar(10) collate utf8_bin; +alter table t add index b_idx(b); +alter table t add index c_idx(c); +-- error 8200 +alter table t modify b varchar(10) collate utf8_general_ci; +-- error 8200 +alter table t modify c varchar(10) collate utf8_bin; +-- error 8200 +alter table t modify c varchar(10) collate utf8_unicode_ci; +-- error 8200 +alter table t convert to charset utf8 collate utf8_general_ci; +alter table t modify c varchar(10) collate utf8mb4_general_ci; +alter table t collate utf8mb4_general_ci; +alter table t charset utf8mb4 collate utf8mb4_bin; +alter table t charset utf8mb4 collate utf8mb4_unicode_ci; +alter table t charset utf8mb4 collate utf8mb4_zh_pinyin_tidb_as_cs; +alter database dct charset utf8mb4 collate utf8mb4_general_ci; +use ddl__serial; +drop database dct; + +# TestForbidUnsupportedCollations +drop database if exists ucd; +-- error 1273 +create database ucd charset utf8mb4 collate utf8mb4_roman_ci; +-- error 1273 +create database ucd charset utf8 collate utf8_roman_ci; +create database ucd; +-- error 1273 +alter database ucd charset utf8mb4 collate utf8mb4_roman_ci; +-- error 1273 +alter database ucd collate utf8mb4_roman_ci; +use ucd; +-- error 1273 +create table t(a varchar(20)) charset utf8mb4 collate utf8mb4_roman_ci; +-- error 1273 +create table t(a varchar(20)) collate utf8_roman_ci; +create table t(a varchar(20)) collate utf8mb4_general_ci; +-- error 1273 +alter table t default collate utf8mb4_roman_ci; +-- error 1273 +alter table t convert to charset utf8mb4 collate utf8mb4_roman_ci; +-- error 1273 +create table t1(a varchar(20)) collate utf8mb4_roman_ci; +-- error 1273 +create table t1(a varchar(20)) charset utf8 collate utf8_roman_ci; +create table t1(a varchar(20)); +-- error 1273 +alter table t1 modify a varchar(20) collate utf8mb4_roman_ci; +-- error 1273 +alter table t1 modify a varchar(20) charset utf8 collate utf8_roman_ci; +-- error 1273 +alter table t1 modify a varchar(20) charset utf8 collate utf8_roman_ci; +# TODO(bb7133): fix the following cases by setting charset from collate firstly. +# mustGetUnsupportedCollation("create database ucd collate utf8mb4_unicode_ci", errMsgUnsupportedUnicodeCI) +# mustGetUnsupportedCollation("alter table t convert to collate utf8mb4_unicode_ci", "utf8mb4_unicode_ci") +drop database if exists ucd; +use ddl__serial; diff --git a/tests/integrationtest/t/planner/core/integration_partition.test b/tests/integrationtest/t/planner/core/integration_partition.test index bb50b9e206c5b..f4a5bc71f3d8c 100644 --- a/tests/integrationtest/t/planner/core/integration_partition.test +++ b/tests/integrationtest/t/planner/core/integration_partition.test @@ -544,9 +544,10 @@ drop database issue_27532; # TestIssue37508 -use test; +use planner__core__integration_partition; set @@tidb_partition_prune_mode = 'dynamic'; select @@tidb_partition_prune_mode; +drop table if exists t1; create table t1 (id int, c date) partition by range (to_days(c)) (partition p0 values less than (to_days('2022-01-11')), partition p1 values less than (to_days('2022-02-11')),