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 drop index failed when index contain auto_increment column and more than 2 index contain auto_increment_column #12230

Merged
merged 16 commits into from
Sep 24, 2019
Merged
Changes from 9 commits
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
12 changes: 11 additions & 1 deletion ddl/db_change_test.go
Original file line number Diff line number Diff line change
@@ -754,6 +754,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;"
@@ -770,7 +780,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")

9 changes: 4 additions & 5 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
@@ -3396,11 +3396,10 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return err
}

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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we extract this code(line3400-3405) directly into a function instead of using countOfIndexContainColumn ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, Done.


job := &model.Job{
32 changes: 32 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
@@ -465,9 +466,40 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde
job.State = model.JobStateCancelled
return nil, nil, 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 nil, nil, autoid.ErrWrongAutoKey
}

return tblInfo, indexInfo, nil
}

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
}

func checkRenameIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, model.CIStr, model.CIStr, error) {
var from, to model.CIStr
schemaID := job.SchemaID