Skip to content

Commit

Permalink
ddl: fix creating expression in clustered index table (pingcap#26385)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Jul 21, 2021
1 parent 7f6cfcb commit 88aa287
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,11 @@ func (s *testSerialDBSuite1) TestAddExpressionIndex(c *C) {

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

// Issue #26371
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int, b int, primary key(a, b) clustered)")
tk.MustExec("alter table t1 add index idx((a+1))")

// Issue #17111
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a varchar(10), b varchar(10));")
Expand Down
7 changes: 5 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5140,13 +5140,16 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde
return errors.Trace(err)
}

finalColumns := make([]*model.ColumnInfo, len(tblInfo.Columns), len(tblInfo.Columns)+len(hiddenCols))
copy(finalColumns, tblInfo.Columns)
finalColumns = append(finalColumns, hiddenCols...)
// Check before the job is put to the queue.
// This check is redundant, but useful. If DDL check fail before the job is put
// to job queue, the fail path logic is super fast.
// After DDL job is put to the queue, and if the check fail, TiDB will run the DDL cancel logic.
// The recover step causes DDL wait a few seconds, makes the unit test painfully slow.
// For same reason, decide whether index is global here.
indexColumns, err := buildIndexColumns(append(tblInfo.Columns, hiddenCols...), indexPartSpecifications)
indexColumns, err := buildIndexColumns(finalColumns, indexPartSpecifications)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -5158,7 +5161,7 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde
if err != nil {
return err
}
idxLen, err = indexColumnsLen(tblInfo.Columns, indexColumns)
idxLen, err = indexColumnsLen(finalColumns, indexColumns)
if err != nil {
return err
}
Expand Down

0 comments on commit 88aa287

Please sign in to comment.