Skip to content

Commit

Permalink
ddl: fix add unique index on partitioned table (by RANGE COLUMNS) fai…
Browse files Browse the repository at this point in the history
…led (#11946) (#11958)
  • Loading branch information
sre-bot authored and winkyao committed Aug 30, 2019
1 parent c706745 commit 1f87310
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
28 changes: 28 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,34 @@ func (s *testIntegrationSuite3) TestAlterAlgorithm(c *C) {
s.tk.MustExec("alter table t default charset = utf8mb4, ALGORITHM=INSTANT")
}

func (s *testIntegrationSuite3) TestAlterTableAddUniqueOnPartionRangeColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
defer s.tk.MustExec("drop table if exists t")

s.tk.MustExec(`create table t(
a int,
b varchar(100),
c int,
INDEX idx_c(c))
PARTITION BY RANGE COLUMNS( a ) (
PARTITION p0 VALUES LESS THAN (6),
PARTITION p1 VALUES LESS THAN (11),
PARTITION p2 VALUES LESS THAN (16),
PARTITION p3 VALUES LESS THAN (21)
)`)
s.tk.MustExec("insert into t values (4, 'xxx', 4)")
s.tk.MustExec("insert into t values (4, 'xxx', 9)") // Note the repeated 4
s.tk.MustExec("insert into t values (17, 'xxx', 12)")
assertErrorCode(c, s.tk, "alter table t add unique index idx_a(a)", mysql.ErrDupEntry)

s.tk.MustExec("delete from t where a = 4")
s.tk.MustExec("alter table t add unique index idx_a(a)")
s.tk.MustExec("alter table t add unique index idx_ac(a, c)")
assertErrorCode(c, s.tk, "alter table t add unique index idx_b(b)", mysql.ErrUniqueKeyNeedAllFieldsInPf)
}

func (s *testIntegrationSuite5) TestFulltextIndexIgnore(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3104,7 +3104,7 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, unique bool, ind
return errors.Trace(err)
}
if unique && tblInfo.GetPartitionInfo() != nil {
if err := checkPartitionKeysConstraint(ctx, tblInfo.GetPartitionInfo().Expr, idxColNames, tblInfo); err != nil {
if err := checkPartitionKeysConstraint(tblInfo.GetPartitionInfo(), idxColNames, tblInfo); err != nil {
return err
}
}
Expand Down
28 changes: 23 additions & 5 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/opcode"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
Expand Down Expand Up @@ -522,11 +523,28 @@ func checkRangePartitioningKeysConstraints(sctx sessionctx.Context, s *ast.Creat
return nil
}

func checkPartitionKeysConstraint(sctx sessionctx.Context, partExpr string, idxColNames []*ast.IndexColName, tblInfo *model.TableInfo) error {
// Parse partitioning key, extract the column names in the partitioning key to slice.
partCols, err := extractPartitionColumns(partExpr, tblInfo)
if err != nil {
return err
func checkPartitionKeysConstraint(pi *model.PartitionInfo, idxColNames []*ast.IndexColName, tblInfo *model.TableInfo) error {
var (
partCols []*model.ColumnInfo
err error
)
// The expr will be an empty string if the partition is defined by:
// CREATE TABLE t (...) PARTITION BY RANGE COLUMNS(...)
if partExpr := pi.Expr; partExpr != "" {
// Parse partitioning key, extract the column names in the partitioning key to slice.
partCols, err = extractPartitionColumns(partExpr, tblInfo)
if err != nil {
return err
}
} else {
partCols = make([]*model.ColumnInfo, 0, len(pi.Columns))
for _, col := range pi.Columns {
colInfo := getColumnInfoByName(tblInfo, col.L)
if colInfo == nil {
return infoschema.ErrColumnNotExists.GenWithStackByArgs(col, tblInfo.Name)
}
partCols = append(partCols, colInfo)
}
}

// Every unique key on the table must use every column in the table's partitioning expression.
Expand Down

0 comments on commit 1f87310

Please sign in to comment.