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
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -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;"
Expand All @@ -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")

Expand Down
17 changes: 16 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3396,9 +3396,10 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return err
}

// Check for drop index on auto_increment column.
cols := t.Cols()
for _, idxCol := range indexInfo.Columns {
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) {
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) && countOfIndexContainColumn(t.Meta(), idxCol.Name.L) < 2 {
return autoid.ErrWrongAutoKey
}
}
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.

Expand All @@ -3422,6 +3423,20 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return errors.Trace(err)
}

func countOfIndexContainColumn(tblInfo *model.TableInfo, colName string) int {
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 put this function to index.go?

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.

count := 0
for _, idx := range tblInfo.Indices {
for _, c := range idx.Columns {
if c.Name.L != colName {
continue
}
count++
break
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can change this code as follows:

			if c.Name.L == colName {
				count++
				break
			}

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.

}
}
return count
}

func isDroppableColumn(tblInfo *model.TableInfo, colName model.CIStr) error {
// Check whether there are other columns depend on this column or not.
for _, col := range tblInfo.Columns {
Expand Down
11 changes: 11 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -465,6 +466,16 @@ 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.
cols := tblInfo.Columns
for _, idxCol := range indexInfo.Columns {
if mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) && countOfIndexContainColumn(tblInfo, idxCol.Name.L) < 2 {
job.State = model.JobStateCancelled
return nil, nil, autoid.ErrWrongAutoKey
}
}

return tblInfo, indexInfo, nil
}

Expand Down