Skip to content

Commit

Permalink
ddl: Only warn of subpartitions, still allow first level partitioning (
Browse files Browse the repository at this point in the history
…#41207)

close #41198, close #41200
  • Loading branch information
mjonss authored May 16, 2023
1 parent 4422222 commit 7a44294
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
44 changes: 43 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,56 @@ func TestCreateTableWithHashPartition(t *testing.T) {
PARTITION p0 VALUES LESS THAN (100),
PARTITION p1 VALUES LESS THAN (200),
PARTITION p2 VALUES LESS THAN MAXVALUE)`)
tk.MustGetErrCode("select * from t_sub partition (p0)", errno.ErrPartitionClauseOnNonpartitioned)
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 8200 Unsupported subpartitioning, only using RANGE partitioning"))
tk.MustQuery("select * from t_sub partition (p0)").Check(testkit.Rows())
tk.MustQuery("show create table t_sub").Check(testkit.Rows("" +
"t_sub CREATE TABLE `t_sub` (\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` varchar(128) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" +
"PARTITION BY RANGE (`a`)\n" +
"(PARTITION `p0` VALUES LESS THAN (100),\n" +
" PARTITION `p1` VALUES LESS THAN (200),\n" +
" PARTITION `p2` VALUES LESS THAN (MAXVALUE))"))

// Fix create partition table using extract() function as partition key.
tk.MustExec("create table t2 (a date, b datetime) partition by hash (EXTRACT(YEAR_MONTH FROM a)) partitions 7")
tk.MustExec("create table t3 (a int, b int) partition by hash(ceiling(a-b)) partitions 10")
tk.MustExec("create table t4 (a int, b int) partition by hash(floor(a-b)) partitions 10")
}

func TestSubPartitioning(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t (a int) partition by range (a) subpartition by hash (a) subpartitions 2 (partition pMax values less than (maxvalue))`)
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 8200 Unsupported subpartitioning, only using RANGE partitioning"))
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 RANGE (`a`)\n" +
"(PARTITION `pMax` VALUES LESS THAN (MAXVALUE))"))
tk.MustExec(`drop table t`)

tk.MustExec(`create table t (a int) partition by list (a) subpartition by key (a) subpartitions 2 (partition pMax values in (1,3,4))`)
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 8200 Unsupported subpartitioning, only using LIST partitioning"))
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 `pMax` VALUES IN (1,3,4))"))
tk.MustExec(`drop table t`)

tk.MustGetErrMsg(`create table t (a int) partition by hash (a) partitions 2 subpartition by key (a) subpartitions 2`, "[ddl:1500]It is only possible to mix RANGE/LIST partitioning with HASH/KEY partitioning for subpartitioning")
tk.MustGetErrMsg(`create table t (a int) partition by key (a) partitions 2 subpartition by hash (a) subpartitions 2`, "[ddl:1500]It is only possible to mix RANGE/LIST partitioning with HASH/KEY partitioning for subpartitioning")

tk.MustGetErrMsg(`CREATE TABLE t ( col1 INT NOT NULL, col2 INT NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, primary KEY (col1,col3) ) PARTITION BY HASH(col1) PARTITIONS 4 SUBPARTITION BY HASH(col3) SUBPARTITIONS 2`, "[ddl:1500]It is only possible to mix RANGE/LIST partitioning with HASH/KEY partitioning for subpartitioning")
tk.MustGetErrMsg(`CREATE TABLE t ( col1 INT NOT NULL, col2 INT NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, primary KEY (col1,col3) ) PARTITION BY KEY(col1) PARTITIONS 4 SUBPARTITION BY KEY(col3) SUBPARTITIONS 2`, "[ddl:1500]It is only possible to mix RANGE/LIST partitioning with HASH/KEY partitioning for subpartitioning")
}

func TestCreateTableWithRangeColumnPartition(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())

Expand Down
33 changes: 15 additions & 18 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,35 +445,32 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb
var enable bool
switch s.Tp {
case model.PartitionTypeRange:
if s.Sub == nil {
enable = true
}
case model.PartitionTypeHash:
// Partition by hash is enabled by default.
// Note that linear hash is simply ignored, and creates non-linear hash.
if s.Linear {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack("LINEAR HASH is not supported, using non-linear HASH instead"))
}
if s.Sub == nil {
enable = true
enable = true
case model.PartitionTypeList:
// Partition by list is enabled only when tidb_enable_list_partition is 'ON'.
enable = ctx.GetSessionVars().EnableListTablePartition
case model.PartitionTypeHash, model.PartitionTypeKey:
// Partition by hash and key is enabled by default.
if s.Sub != nil {
// Subpartitioning only allowed with Range or List
return ast.ErrSubpartition
}
case model.PartitionTypeKey:
// Note that linear key is simply ignored, and creates non-linear hash.
// Note that linear hash is simply ignored, and creates non-linear hash/key.
if s.Linear {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack("LINEAR KEY is not supported, using non-linear KEY instead"))
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("LINEAR %s is not supported, using non-linear %s instead", s.Tp.String(), s.Tp.String())))
}
if s.Sub == nil && len(s.ColumnNames) != 0 {
if s.Tp == model.PartitionTypeHash || len(s.ColumnNames) != 0 {
enable = true
}
case model.PartitionTypeList:
// Partition by list is enabled only when tidb_enable_list_partition is 'ON'.
enable = ctx.GetSessionVars().EnableListTablePartition
}

if !enable {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("Unsupported partition type %v, treat as normal table", s.Tp)))
return nil
}
if s.Sub != nil {
ctx.GetSessionVars().StmtCtx.AppendWarning(dbterror.ErrUnsupportedCreatePartition.GenWithStack(fmt.Sprintf("Unsupported subpartitioning, only using %v partitioning", s.Tp)))
}

pi := &model.PartitionInfo{
Type: s.Tp,
Expand Down
4 changes: 3 additions & 1 deletion table/tables/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,8 @@ func TestKeyPartitionTableMixed(t *testing.T) {
require.Regexp(t, "You have an error in your SQL syntax", err)

// Key partition can't be as subpartition
tk.MustContainErrMsg("CREATE TABLE tkey_subpartition1 (a INT not null,b VARCHAR(12) not null,c CHAR(14) not null,primary key (a, b, c)) PARTITION BY KEY (a) SUBPARTITION BY KEY(b) SUBPARTITIONS 2", "[ddl:1500]It is only possible to mix RANGE/LIST partitioning with HASH/KEY partitioning for subpartitioning")

tk.MustExec("CREATE TABLE tkey_subpartition1 (JYRQ INT not null,KHH VARCHAR(12) not null,ZJZH CHAR(14) not null,primary key (JYRQ, KHH, ZJZH))" +
"PARTITION BY RANGE(JYRQ)\n" +
"SUBPARTITION BY KEY(KHH) SUBPARTITIONS 2 \n" +
Expand All @@ -2089,7 +2091,7 @@ func TestKeyPartitionTableMixed(t *testing.T) {
"PARTITION p2 VALUES LESS THAN MAXVALUE\n" +
")")
result = tk.MustQuery("show warnings")
result.CheckContain("Unsupported partition type RANGE, treat as normal table")
result.CheckContain("Unsupported subpartitioning, only using RANGE partitioning")

// It ignores /*!50100 */ format
tk.MustExec("CREATE TABLE tkey10 (`col1` int, `col2` char(5),`col3` date)" +
Expand Down

0 comments on commit 7a44294

Please sign in to comment.