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: disallow dropping index on auto_increment column #11360

Merged
merged 6 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ func (s *testIntegrationSuite5) TestMySQLErrorCode(c *C) {
assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int null, age int, primary key(id));"
assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int auto_increment);"
assertErrorCode(c, tk, sql, tmysql.ErrWrongAutoKey)

sql = "create table t2 (id int primary key , age int);"
tk.MustExec(sql)
Expand Down Expand Up @@ -1803,3 +1805,19 @@ func (s *testIntegrationSuite5) TestChangingDBCharset(c *C) {
tk.MustExec("ALTER SCHEMA CHARACTER SET = 'utf8mb4' COLLATE = 'utf8mb4_general_ci'")
verifyDBCharsetAndCollate("alterdb2", "utf8mb4", "utf8mb4_general_ci")
}

func (s *testIntegrationSuite4) TestDropAutoIncrementIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists test")
tk.MustExec("use test")

tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a int auto_increment, unique key (a))")
dropIndexSQL := "alter table t1 drop index a"
assertErrorCode(c, tk, dropIndexSQL, mysql.ErrWrongAutoKey)

tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a int(11) not null auto_increment, b int(11), c bigint, unique key (a, b, c))")
dropIndexSQL = "alter table t1 drop index a"
assertErrorCode(c, tk, dropIndexSQL, mysql.ErrWrongAutoKey)
}
10 changes: 9 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3263,7 +3263,8 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name))
}

if indexInfo := t.Meta().FindIndexByName(indexName.L); indexInfo == nil {
var indexInfo *model.IndexInfo
tangenta marked this conversation as resolved.
Show resolved Hide resolved
if indexInfo = t.Meta().FindIndexByName(indexName.L); indexInfo == nil {
err = ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
if ifExists {
ctx.GetSessionVars().StmtCtx.AppendNote(err)
Expand All @@ -3272,6 +3273,13 @@ 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 errors.Trace(autoid.ErrWrongAutoKey)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
}
}

job := &model.Job{
SchemaID: schema.ID,
TableID: t.Meta().ID,
Expand Down
2 changes: 2 additions & 0 deletions meta/autoid/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
// Error instances.
var (
ErrAutoincReadFailed = terror.ClassAutoid.New(mysql.ErrAutoincReadFailed, mysql.MySQLErrName[mysql.ErrAutoincReadFailed])
ErrWrongAutoKey = terror.ClassAutoid.New(mysql.ErrWrongAutoKey, mysql.MySQLErrName[mysql.ErrWrongAutoKey])
)

func init() {
// Map error codes to mysql error codes.
tableMySQLErrCodes := map[terror.ErrCode]uint16{
mysql.ErrAutoincReadFailed: mysql.ErrAutoincReadFailed,
mysql.ErrWrongAutoKey: mysql.ErrWrongAutoKey,
}
terror.ErrClassToMySQLCodes[terror.ClassAutoid] = tableMySQLErrCodes
}
3 changes: 2 additions & 1 deletion planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/parser_driver"
Expand Down Expand Up @@ -295,7 +296,7 @@ func (p *preprocessor) checkAutoIncrement(stmt *ast.CreateTableStmt) {
}
}
if (autoIncrementMustBeKey && !isKey) || count > 1 {
p.err = errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")
p.err = autoid.ErrWrongAutoKey.GenWithStackByArgs()
}

switch autoIncrementCol.Tp.Tp {
Expand Down
6 changes: 3 additions & 3 deletions planner/core/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"create table t(id int auto_increment default null, primary key (id))", true, nil},
{"create table t(id int default null auto_increment, primary key (id))", true, nil},
{"create table t(id int not null auto_increment)", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
errors.New("[autoid:1075]Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id int not null auto_increment, c int auto_increment, key (id, c))", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
errors.New("[autoid:1075]Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id int not null auto_increment, c int, key (c, id))", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
errors.New("[autoid:1075]Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id decimal auto_increment, key (id))", true,
errors.New("Incorrect column specifier for column 'id'")},
{"create table t(id float auto_increment, key (id))", true, nil},
Expand Down