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) (#43888)

close #41198, close #41200
  • Loading branch information
ti-chi-bot authored Jun 30, 2023
1 parent 75c751b commit 5f63550
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
55 changes: 53 additions & 2 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,72 @@ func TestCreateTableWithHashPartition(t *testing.T) {
) PARTITION BY HASH(store_id) PARTITIONS 102400000000;`, tmysql.ErrTooManyPartitions)

tk.MustExec("CREATE TABLE t_linear (a int, b varchar(128)) PARTITION BY LINEAR HASH(a) PARTITIONS 4")
tk.MustGetErrCode("select * from t_linear partition (p0)", tmysql.ErrPartitionClauseOnNonpartitioned)
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 8200 LINEAR HASH is not supported, using non-linear HASH instead"))
tk.MustQuery(`show create table t_linear`).Check(testkit.Rows("" +
"t_linear CREATE TABLE `t_linear` (\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` varchar(128) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" +
"PARTITION BY HASH (`a`) PARTITIONS 4"))
tk.MustQuery("select * from t_linear partition (p0)").Check(testkit.Rows())

tk.MustExec(`CREATE TABLE t_sub (a int, b varchar(128)) PARTITION BY RANGE( a ) SUBPARTITION BY HASH( a )
SUBPARTITIONS 2 (
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)", tmysql.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, clean := testkit.CreateMockStore(t)
defer clean()

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.MustExec(`create table t (a int) partition by key (a) partitions 2 subpartition by hash (a) subpartitions 2`)
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 8200 Unsupported partition type KEY, treat as normal table"))

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")
}

func TestCreateTableWithRangeColumnPartition(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
33 changes: 19 additions & 14 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,25 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb
var enable bool
switch s.Tp {
case model.PartitionTypeRange:
// When tidb_enable_table_partition is 'on' or 'auto'.
if s.Sub == nil {
// Partition by range expression is enabled by default.
if s.ColumnNames == nil {
enable = true
}
// Partition by range columns and just one column.
if len(s.ColumnNames) == 1 {
enable = true
}
// Partition by range expression is enabled by default.
if s.ColumnNames == nil {
enable = true
}
case model.PartitionTypeHash:
// Partition by hash is enabled by default.
// Note that linear hash is not enabled.
if !s.Linear && s.Sub == nil {
// Partition by range columns and just one column.
if len(s.ColumnNames) == 1 {
enable = true
}
case model.PartitionTypeHash:
// Partition by hash and key is enabled by default.
if s.Sub != nil {
// Subpartitioning only allowed with Range or List
return ast.ErrSubpartition
}
// Note that linear hash is simply ignored, and creates non-linear hash/key.
if s.Linear {
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())))
}
enable = true
case model.PartitionTypeList:
// Partition by list is enabled only when tidb_enable_list_partition is 'ON'.
enable = ctx.GetSessionVars().EnableListTablePartition
Expand All @@ -464,6 +466,9 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.PartitionOptions, tb
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

0 comments on commit 5f63550

Please sign in to comment.