Skip to content

Commit

Permalink
Merge pull request pingcap#6 from Deardrops/4.0-itai
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 authored May 18, 2020
2 parents 0db9f6e + ec0de40 commit b941826
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,12 @@ func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {

tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))

// Issue #17111
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a varchar(10), b varchar(10));")
tk.MustExec("alter table t1 add unique index ei_ab ((concat(a, b)));")
tk.MustExec("alter table t1 alter index ei_ab invisible;")

// Test experiment switch.
config.GetGlobalConfig().Experimental.AllowsExpressionIndex = false
tk.MustGetErrMsg("create index d on t((a+1))", "[ddl:8200]Unsupported creating expression index without allow-expression-index in config")
Expand Down
31 changes: 27 additions & 4 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,13 @@ func checkConstraintNames(constraints []*ast.Constraint) error {
}

// checkInvisibleIndexOnPK check if primary key is invisible index.
// Note: PKIsHandle == true means the table already has a visible primary key,
// we do not need do a check for this case and return directly,
// because whether primary key is invisible has been check when creating table.
func checkInvisibleIndexOnPK(tblInfo *model.TableInfo) error {
if tblInfo.PKIsHandle {
return nil
}
pk := getPrimaryKey(tblInfo)
if pk != nil && pk.Invisible {
return ErrPKIndexCantBeInvisible
Expand All @@ -1154,16 +1160,32 @@ func getPrimaryKey(tblInfo *model.TableInfo) *model.IndexInfo {
// table has explicit primary key
return key
}
// The case index without any columns should never happen, but still do a check here
if len(key.Columns) == 0 {
continue
}
// find the first unique key with NOT NULL columns
if implicitPK == nil && key.Unique {
// ensure all columns in unique key have NOT NULL flag
allColNotNull := true
skip := false
for _, idxCol := range key.Columns {
col := model.FindColumnInfo(tblInfo.Cols(), idxCol.Name.L)
// This index has a column in DeleteOnly state,
// or it is expression index (it defined on a hidden column),
// it can not be implicit PK, go to next index iterator
if col == nil || col.Hidden {
skip = true
break
}
if !mysql.HasNotNullFlag(col.Flag) {
allColNotNull = false
break
}
}
if skip {
continue
}
if allColNotNull {
implicitPK = key
}
Expand Down Expand Up @@ -1270,6 +1292,11 @@ func buildTableInfo(
switch lastCol.Tp {
case mysql.TypeLong, mysql.TypeLonglong,
mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24:
// Primary key cannot be invisible.
if constr.Option != nil && constr.Option.Visibility == ast.IndexVisibilityInvisible {
return nil, ErrPKIndexCantBeInvisible
}

tbInfo.PKIsHandle = true
// Avoid creating index for PK handle column.
continue
Expand Down Expand Up @@ -1498,10 +1525,6 @@ func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh
return nil, errors.Trace(err)
}

if err = checkInvisibleIndexOnPK(tbInfo); err != nil {
return nil, errors.Trace(err)
}

return tbInfo, nil
}

Expand Down
8 changes: 8 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK boo
indexInfo.Unique = unique
indexInfo.ID = allocateIndexID(tblInfo)
tblInfo.Indices = append(tblInfo.Indices, indexInfo)

// Here we need do this check before set state to `DeleteOnly`,
// because if hidden columns has been set to `DeleteOnly`,
// the `DeleteOnly` columns are missing when we do this check.
if err := checkInvisibleIndexOnPK(tblInfo); err != nil {
job.State = model.JobStateCancelled
return ver, err
}
logutil.BgLogger().Info("[ddl] run add index job", zap.String("job", job.String()), zap.Reflect("indexInfo", indexInfo))
}
originalState := indexInfo.State
Expand Down
6 changes: 6 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
tk.MustExec("alter table tt add index (`primary`);")
_, err = tk.Exec("drop index `primary` on tt")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")

// The primary key cannot be invisible, for the case pk_is_handle.
tk.MustExec("drop table if exists t1, t2;")
_, err = tk.Exec("create table t1(c1 int not null, primary key(c1) invisible);")
c.Assert(ddl.ErrPKIndexCantBeInvisible.Equal(err), IsTrue)
tk.MustExec("create table t2 (a int, b int not null, primary key(a), unique(b) invisible);")
}

func (s *testSerialSuite) TestDropAutoIncrementIndex(c *C) {
Expand Down

0 comments on commit b941826

Please sign in to comment.