ddl: fix bug when add expression index (#17151)

This commit is contained in:
Maxwell
2020-05-15 14:38:19 +08:00
committed by GitHub
parent ea2e215ff8
commit 9014ceee69
3 changed files with 30 additions and 0 deletions

View File

@ -2094,6 +2094,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")

View File

@ -1133,16 +1133,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
}

View File

@ -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