Skip to content

Commit

Permalink
ddl: fix drop index failed when index contain auto_increment column a…
Browse files Browse the repository at this point in the history
…nd more than 2 index contain auto_increment_column (pingcap#12230)

Signed-off-by: crazycs520 <crazycs520@gmail.com>
  • Loading branch information
crazycs520 committed Apr 9, 2020
1 parent c21ca8f commit b72b918
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
12 changes: 11 additions & 1 deletion ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,16 @@ func (s *testStateChangeSuite) TestParallelDropColumn(c *C) {
s.testControlParallelExecSQL(c, sql, sql, f)
}

func (s *testStateChangeSuite) TestParallelDropIndex(c *C) {
sql1 := "alter table t drop index idx1 ;"
sql2 := "alter table t drop index idx2 ;"
f := func(c *C, err1, err2 error) {
c.Assert(err1, IsNil)
c.Assert(err2.Error(), Equals, "[autoid:1075]Incorrect table definition; there can be only one auto column and it must be defined as a key")
}
s.testControlParallelExecSQL(c, sql1, sql2, f)
}

func (s *testStateChangeSuite) TestParallelCreateAndRename(c *C) {
sql1 := "create table t_exists(c int);"
sql2 := "alter table t rename to t_exists;"
Expand All @@ -791,7 +801,7 @@ type checkRet func(c *C, err1, err2 error)
func (s *testStateChangeSuite) testControlParallelExecSQL(c *C, sql1, sql2 string, f checkRet) {
_, err := s.se.Execute(context.Background(), "use test_db_state")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "create table t(a int, b int, c int)")
_, err = s.se.Execute(context.Background(), "create table t(a int, b int, c int, d int auto_increment,e int, index idx1(d), index idx2(d,e))")
c.Assert(err, IsNil)
defer s.se.Execute(context.Background(), "drop table t")

Expand Down
10 changes: 4 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2695,12 +2695,10 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
if indexInfo == nil {
return ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
}

cols := t.Cols()
for _, idxCol := range indexInfo.Columns {
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) {
return autoid.ErrWrongAutoKey
}
// Check for drop index on auto_increment column.
err = checkDropIndexOnAutoIncrementColumn(t.Meta(), indexInfo)
if err != nil {
return errors.Trace(err)
}

job := &model.Job{
Expand Down
31 changes: 31 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ddl

import (
"context"
"github.com/pingcap/tidb/meta/autoid"
"math"
"strconv"
"sync/atomic"
Expand Down Expand Up @@ -395,6 +396,13 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
return ver, ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
}

// Double check for drop index on auto_increment column.
err = checkDropIndexOnAutoIncrementColumn(tblInfo, indexInfo)
if err != nil {
job.State = model.JobStateCancelled
return ver, autoid.ErrWrongAutoKey
}

originalState := indexInfo.State
switch indexInfo.State {
case model.StatePublic:
Expand Down Expand Up @@ -445,6 +453,29 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
return ver, errors.Trace(err)
}

func checkDropIndexOnAutoIncrementColumn(tblInfo *model.TableInfo, indexInfo *model.IndexInfo) error {
cols := tblInfo.Columns
for _, idxCol := range indexInfo.Columns {
if !mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) {
continue
}
// check the count of index on auto_increment column.
count := 0
for _, idx := range tblInfo.Indices {
for _, c := range idx.Columns {
if c.Name.L == idxCol.Name.L {
count++
break
}
}
}
if count < 2 {
return autoid.ErrWrongAutoKey
}
}
return nil
}

const (
// DefaultTaskHandleCnt is default batch size of adding indices.
DefaultTaskHandleCnt = 128
Expand Down

0 comments on commit b72b918

Please sign in to comment.