Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix a panic when creating key partition table (#16260) #17242

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ func (s *testIntegrationSuite3) TestCreateTableWithKeyPartition(c *C) {
s1 char(32) primary key
)
partition by key(s1) partitions 10;`)

tk.MustExec(`drop table if exists tm2`)
tk.MustExec(`create table tm2 (a char(5), unique key(a(5))) partition by key() partitions 5;`)
}

func (s *testIntegrationSuite5) TestAlterTableAddPartition(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ func checkTableInfoValidWithStmt(ctx sessionctx.Context, tbInfo *model.TableInfo
}
}

if err = checkRangePartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
if err = checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
return errors.Trace(err)
}
}
Expand Down
27 changes: 17 additions & 10 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.CreateTableStmt) (*m
var enable bool
// When tidb_enable_table_partition is 'on' or 'auto'.
if s.Partition.Tp == model.PartitionTypeRange {
// Partition by range expression is enabled by default.
if s.Partition.ColumnNames == nil {
enable = true
}
// Partition by range columns and just one column.
if len(s.Partition.ColumnNames) == 1 {
enable = true
if s.Partition.Sub == nil {
// Partition by range expression is enabled by default.
if s.Partition.ColumnNames == nil {
enable = true
}
// Partition by range columns and just one column.
if len(s.Partition.ColumnNames) == 1 {
enable = true
}
}
}
// Partition by hash is enabled by default.
// Note that linear hash is not enabled.
if s.Partition.Tp == model.PartitionTypeHash {
enable = true
if !s.Partition.Linear && s.Partition.Sub == nil {
enable = true
}
}

if !enable {
Expand Down Expand Up @@ -713,8 +717,8 @@ func getPartitionIDs(table *model.TableInfo) []int64 {
return physicalTableIDs
}

// checkRangePartitioningKeysConstraints checks that the range partitioning key is included in the table constraint.
func checkRangePartitioningKeysConstraints(sctx sessionctx.Context, s *ast.CreateTableStmt, tblInfo *model.TableInfo) error {
// checkPartitioningKeysConstraints checks that the range partitioning key is included in the table constraint.
func checkPartitioningKeysConstraints(sctx sessionctx.Context, s *ast.CreateTableStmt, tblInfo *model.TableInfo) error {
// Returns directly if there are no unique keys in the table.
if len(tblInfo.Indices) == 0 && !tblInfo.PKIsHandle {
return nil
Expand All @@ -732,6 +736,9 @@ func checkRangePartitioningKeysConstraints(sctx sessionctx.Context, s *ast.Creat
partCols = columnInfoSlice(partColumns)
} else if len(s.Partition.ColumnNames) > 0 {
partCols = columnNameSlice(s.Partition.ColumnNames)
} else {
// TODO: Check keys constraints for list, key partition type and so on.
return nil
}

// Checks that the partitioning key is included in the constraint.
Expand Down