diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index 5c53a7ee5d649..eb8c4011b35bb 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -5132,3 +5132,433 @@ partition p1 values less than maxvalue)`) "(PARTITION `p0` VALUES LESS THAN (1998),\n" + " PARTITION `p1` VALUES LESS THAN (MAXVALUE))")) } +<<<<<<< HEAD:ddl/db_partition_test.go +======= + +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")) +} + +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) + h := dom.StatsHandle() + tk.MustExec("create database RemovePartitioning") + tk.MustExec("use RemovePartitioning") + tk.MustExec(`create table t (a varchar(255), b varchar(255), key (a,b), key (b)) partition by key (a) partitions 7`) + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // Fill the data with ascii strings + for i := 32; i <= 126; i++ { + tk.MustExec(fmt.Sprintf(`insert into t values (char(%d,%d,%d),char(%d,%d,%d,%d))`, i, i, i, i, i, i, i)) + } + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemovePartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemovePartitioning", "t", "global", "0", "95"}, + {"RemovePartitioning", "t", "p0", "0", "9"}, + {"RemovePartitioning", "t", "p1", "0", "11"}, + {"RemovePartitioning", "t", "p2", "0", "12"}, + {"RemovePartitioning", "t", "p3", "0", "13"}, + {"RemovePartitioning", "t", "p4", "0", "16"}, + {"RemovePartitioning", "t", "p5", "0", "23"}, + {"RemovePartitioning", "t", "p6", "0", "11"}}) + tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_schema = 'RemovePartitioning' and table_name = 't'`).Sort().Check(testkit.Rows(""+ + "p0 9", + "p1 11", + "p2 12", + "p3 13", + "p4 16", + "p5 23", + "p6 11")) + tk.MustExec(`alter table t remove partitioning`) + 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" + + " KEY `a` (`a`,`b`),\n" + + " KEY `b` (`b`)\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + // Statistics are updated asynchronously + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // And also cached and lazy loaded + h.Clear() + require.NoError(t, h.Update(dom.InfoSchema())) + tk.MustQuery(`show stats_meta where db_name = 'RemovePartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemovePartitioning", "t", "", "0", "95"}}) + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemovePartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemovePartitioning", "t", "", "0", "95"}}) +} + +func TestRemoveListPartitioning(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + h := dom.StatsHandle() + tk.MustExec("create database RemoveListPartitioning") + tk.MustExec("use RemoveListPartitioning") + tk.MustExec(`create table t (a int, b varchar(255), key (a,b), key (b)) partition by list (a) (partition p0 values in (0), partition p1 values in (1), partition p2 values in (2), partition p3 values in (3), partition p4 values in (4))`) + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // Fill the data with ascii strings + for i := 32; i <= 126; i++ { + tk.MustExec(fmt.Sprintf(`insert into t values (%d,char(%d,%d,%d,%d))`, i%5, i, i, i, i)) + } + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "global", "0", "95"}, + {"RemoveListPartitioning", "t", "p0", "0", "19"}, + {"RemoveListPartitioning", "t", "p1", "0", "19"}, + {"RemoveListPartitioning", "t", "p2", "0", "19"}, + {"RemoveListPartitioning", "t", "p3", "0", "19"}, + {"RemoveListPartitioning", "t", "p4", "0", "19"}}) + tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_schema = 'RemoveListPartitioning' and table_name = 't'`).Sort().Check(testkit.Rows(""+ + "p0 19", + "p1 19", + "p2 19", + "p3 19", + "p4 19")) + 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")) + // Statistics are updated asynchronously + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // And also cached and lazy loaded + h.Clear() + require.NoError(t, h.Update(dom.InfoSchema())) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) +} + +func TestRemoveListColumnPartitioning(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + h := dom.StatsHandle() + tk.MustExec("create database RemoveListPartitioning") + tk.MustExec("use RemoveListPartitioning") + tk.MustExec(`create table t (a varchar(255), b varchar(255), key (a,b), key (b)) partition by list columns (a) (partition p0 values in ("0"), partition p1 values in ("1"), partition p2 values in ("2"), partition p3 values in ("3"), partition p4 values in ("4"))`) + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // Fill the data with ascii strings + for i := 32; i <= 126; i++ { + tk.MustExec(fmt.Sprintf(`insert into t values ("%d",char(%d,%d,%d,%d))`, i%5, i, i, i, i)) + } + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "global", "0", "95"}, + {"RemoveListPartitioning", "t", "p0", "0", "19"}, + {"RemoveListPartitioning", "t", "p1", "0", "19"}, + {"RemoveListPartitioning", "t", "p2", "0", "19"}, + {"RemoveListPartitioning", "t", "p3", "0", "19"}, + {"RemoveListPartitioning", "t", "p4", "0", "19"}}) + tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_schema = 'RemoveListPartitioning' and table_name = 't'`).Sort().Check(testkit.Rows(""+ + "p0 19", + "p1 19", + "p2 19", + "p3 19", + "p4 19")) + tk.MustExec(`alter table t remove partitioning`) + 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" + + " KEY `a` (`a`,`b`),\n" + + " KEY `b` (`b`)\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + // Statistics are updated asynchronously + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // And also cached and lazy loaded + h.Clear() + require.NoError(t, h.Update(dom.InfoSchema())) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) +} + +func TestRemoveListColumnsPartitioning(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + h := dom.StatsHandle() + tk.MustExec("create database RemoveListPartitioning") + tk.MustExec("use RemoveListPartitioning") + tk.MustExec(`create table t (a int, b varchar(255), key (a,b), key (b)) partition by list columns (a,b) (partition p0 values in ((0,"0")), partition p1 values in ((1,"1")), partition p2 values in ((2,"2")), partition p3 values in ((3,"3")), partition p4 values in ((4,"4")))`) + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // Fill the data + for i := 32; i <= 126; i++ { + tk.MustExec(fmt.Sprintf(`insert into t values (%d,"%d")`, i%5, i%5)) + } + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "global", "0", "95"}, + {"RemoveListPartitioning", "t", "p0", "0", "19"}, + {"RemoveListPartitioning", "t", "p1", "0", "19"}, + {"RemoveListPartitioning", "t", "p2", "0", "19"}, + {"RemoveListPartitioning", "t", "p3", "0", "19"}, + {"RemoveListPartitioning", "t", "p4", "0", "19"}}) + tk.MustQuery(`select partition_name, table_rows from information_schema.partitions where table_schema = 'RemoveListPartitioning' and table_name = 't'`).Sort().Check(testkit.Rows(""+ + "p0 19", + "p1 19", + "p2 19", + "p3 19", + "p4 19")) + 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")) + // Statistics are updated asynchronously + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + // And also cached and lazy loaded + h.Clear() + require.NoError(t, h.Update(dom.InfoSchema())) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) + tk.MustExec(`analyze table t`) + tk.MustQuery(`show stats_meta where db_name = 'RemoveListPartitioning' and table_name = 't'`).Sort().CheckAt([]int{0, 1, 2, 4, 5}, [][]interface{}{ + {"RemoveListPartitioning", "t", "", "0", "95"}}) +} + +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!!!) +>>>>>>> e8b590c0cbf (*: Exchange partition, fix LIST COLUMNs validation as well as NULL validation (#46533)):ddl/tests/partition/db_partition_test.go diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 4509aa0d954e9..db6697a27ecca 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -7245,7 +7245,7 @@ func checkAndGetColumnsTypeAndValuesMatch(ctx sessionctx.Context, colTypes []typ switch colType.GetType() { case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeDuration: switch vkind { - case types.KindString, types.KindBytes: + case types.KindString, types.KindBytes, types.KindNull: default: return nil, dbterror.ErrWrongTypeColumnValue.GenWithStackByArgs() } diff --git a/ddl/partition.go b/ddl/partition.go index 612a7e4d4165f..0d412dfa1a60b 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -3054,6 +3054,8 @@ func checkExchangePartitionRecordValidation(w *worker, pt *model.TableInfo, inde if rowCount != 0 { return errors.Trace(dbterror.ErrRowDoesNotMatchPartition) } + // Check warnings! + // Is it possible to check how many rows where checked as well? return nil } @@ -3089,13 +3091,13 @@ func buildCheckSQLForRangeExprPartition(pi *model.PartitionInfo, index int, sche buf.WriteString("select 1 from %n.%n where ") buf.WriteString(pi.Expr) buf.WriteString(" >= %? limit 1") - paramList = append(paramList, schemaName.L, tableName.L, trimQuotation(pi.Definitions[index].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, driver.UnwrapFromSingleQuotes(pi.Definitions[index].LessThan[0])) return buf.String(), paramList } else if index == len(pi.Definitions)-1 && strings.EqualFold(pi.Definitions[index].LessThan[0], partitionMaxValue) { buf.WriteString("select 1 from %n.%n where ") buf.WriteString(pi.Expr) buf.WriteString(" < %? limit 1") - paramList = append(paramList, schemaName.L, tableName.L, trimQuotation(pi.Definitions[index-1].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, driver.UnwrapFromSingleQuotes(pi.Definitions[index-1].LessThan[0])) return buf.String(), paramList } else { buf.WriteString("select 1 from %n.%n where ") @@ -3103,26 +3105,22 @@ func buildCheckSQLForRangeExprPartition(pi *model.PartitionInfo, index int, sche buf.WriteString(" < %? or ") buf.WriteString(pi.Expr) buf.WriteString(" >= %? limit 1") - paramList = append(paramList, schemaName.L, tableName.L, trimQuotation(pi.Definitions[index-1].LessThan[0]), trimQuotation(pi.Definitions[index].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, driver.UnwrapFromSingleQuotes(pi.Definitions[index-1].LessThan[0]), driver.UnwrapFromSingleQuotes(pi.Definitions[index].LessThan[0])) return buf.String(), paramList } } -func trimQuotation(str string) string { - return strings.Trim(str, "'") -} - func buildCheckSQLForRangeColumnsPartition(pi *model.PartitionInfo, index int, schemaName, tableName model.CIStr) (string, []interface{}) { paramList := make([]interface{}, 0, 6) colName := pi.Columns[0].L if index == 0 { - paramList = append(paramList, schemaName.L, tableName.L, colName, trimQuotation(pi.Definitions[index].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, colName, driver.UnwrapFromSingleQuotes(pi.Definitions[index].LessThan[0])) return "select 1 from %n.%n where %n >= %? limit 1", paramList } else if index == len(pi.Definitions)-1 && strings.EqualFold(pi.Definitions[index].LessThan[0], partitionMaxValue) { - paramList = append(paramList, schemaName.L, tableName.L, colName, trimQuotation(pi.Definitions[index-1].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, colName, driver.UnwrapFromSingleQuotes(pi.Definitions[index-1].LessThan[0])) return "select 1 from %n.%n where %n < %? limit 1", paramList } else { - paramList = append(paramList, schemaName.L, tableName.L, colName, trimQuotation(pi.Definitions[index-1].LessThan[0]), colName, trimQuotation(pi.Definitions[index].LessThan[0])) + paramList = append(paramList, schemaName.L, tableName.L, colName, driver.UnwrapFromSingleQuotes(pi.Definitions[index-1].LessThan[0]), colName, driver.UnwrapFromSingleQuotes(pi.Definitions[index].LessThan[0])) return "select 1 from %n.%n where %n < %? or %n >= %? limit 1", paramList } } @@ -3130,34 +3128,59 @@ func buildCheckSQLForRangeColumnsPartition(pi *model.PartitionInfo, index int, s func buildCheckSQLForListPartition(pi *model.PartitionInfo, index int, schemaName, tableName model.CIStr) (string, []interface{}) { var buf strings.Builder buf.WriteString("select 1 from %n.%n where ") - buf.WriteString(pi.Expr) - buf.WriteString(" not in (%?) limit 1") - inValues := getInValues(pi, index) - - paramList := make([]interface{}, 0, 3) - paramList = append(paramList, schemaName.L, tableName.L, inValues) + buf.WriteString(" not (") + for i, inValue := range pi.Definitions[index].InValues { + if i != 0 { + buf.WriteString(" OR ") + } + // AND has higher priority than OR, so no need for parentheses + for j, val := range inValue { + if j != 0 { + // Should never happen, since there should be no multi-columns, only a single expression :) + buf.WriteString(" AND ") + } + // null-safe compare '<=>' + buf.WriteString(fmt.Sprintf("(%s) <=> %s", pi.Expr, val)) + } + } + buf.WriteString(") limit 1") + paramList := make([]interface{}, 0, 2) + paramList = append(paramList, schemaName.L, tableName.L) return buf.String(), paramList } func buildCheckSQLForListColumnsPartition(pi *model.PartitionInfo, index int, schemaName, tableName model.CIStr) (string, []interface{}) { - colName := pi.Columns[0].L var buf strings.Builder - buf.WriteString("select 1 from %n.%n where %n not in (%?) limit 1") - inValues := getInValues(pi, index) - - paramList := make([]interface{}, 0, 4) - paramList = append(paramList, schemaName.L, tableName.L, colName, inValues) + // How to find a match? + // (row <=> vals1) OR (row <=> vals2) + // How to find a non-matching row: + // NOT ( (row <=> vals1) OR (row <=> vals2) ... ) + buf.WriteString("select 1 from %n.%n where not (") + colNames := make([]string, 0, len(pi.Columns)) + for i := range pi.Columns { + // TODO: check if there are no proper quoting function for this? + n := "`" + strings.ReplaceAll(pi.Columns[i].O, "`", "``") + "`" + colNames = append(colNames, n) + } + for i, colValues := range pi.Definitions[index].InValues { + if i != 0 { + buf.WriteString(" OR ") + } + // AND has higher priority than OR, so no need for parentheses + for j, val := range colValues { + if j != 0 { + buf.WriteString(" AND ") + } + // null-safe compare '<=>' + buf.WriteString(fmt.Sprintf("%s <=> %s", colNames[j], val)) + } + } + buf.WriteString(") limit 1") + paramList := make([]interface{}, 0, 2) + paramList = append(paramList, schemaName.L, tableName.L) return buf.String(), paramList } -func getInValues(pi *model.PartitionInfo, index int) []string { - inValues := make([]string, 0, len(pi.Definitions[index].InValues)) - for _, inValue := range pi.Definitions[index].InValues { - inValues = append(inValues, inValue...) - } - return inValues -} - func checkAddPartitionTooManyPartitions(piDefs uint64) error { if piDefs > uint64(mysql.PartitionCountLimit) { return errors.Trace(dbterror.ErrTooManyPartitions) diff --git a/planner/core/integration_partition_test.go b/planner/core/integration_partition_test.go index 5af37d080189a..0cb0835d3c8aa 100644 --- a/planner/core/integration_partition_test.go +++ b/planner/core/integration_partition_test.go @@ -986,7 +986,7 @@ func TestRangeColumnsMultiColumn(t *testing.T) { tk.MustGetErrCode(`create table t (a int, b datetime, c varchar(255)) partition by range columns (a,b,c)`+ `(partition p0 values less than (NULL,NULL,NULL))`, - errno.ErrWrongTypeColumnValue) + errno.ErrParse) tk.MustGetErrCode(`create table t (a int, b datetime, c varchar(255)) partition by range columns (a,b,c)`+ `(partition p1 values less than (`+strconv.FormatInt(math.MinInt32-1, 10)+`,'0000-00-00',""))`, errno.ErrWrongTypeColumnValue) diff --git a/types/parser_driver/value_expr.go b/types/parser_driver/value_expr.go index 5a3fcf1b8ef8a..b100f1302e3a6 100644 --- a/types/parser_driver/value_expr.go +++ b/types/parser_driver/value_expr.go @@ -183,7 +183,7 @@ func (n *ValueExpr) Format(w io.Writer) { } // WrapInSingleQuotes escapes single quotes and backslashs -// and adds single quotes arond the string +// and adds single quotes around the string func WrapInSingleQuotes(inStr string) string { s := strings.ReplaceAll(inStr, "\\", "\\\\") s = strings.ReplaceAll(s, `'`, `''`) @@ -191,7 +191,7 @@ func WrapInSingleQuotes(inStr string) string { } // UnwrapFromSingleQuotes the reverse of WrapInSingleQuotes -// but also allows non single quoted strings +// but also allows non-single quoted strings func UnwrapFromSingleQuotes(inStr string) string { if len(inStr) < 2 || inStr[:1] != "'" || inStr[len(inStr)-1:] != "'" { return inStr