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: Support the operation of dropping multi-indexes in one statement #20457

Merged
merged 100 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
f99363c
Merge pull request #1 from pingcap/master
ou-bing Sep 4, 2020
35c9f20
Merge pull request #2 from pingcap/master
ou-bing Sep 21, 2020
b6accd8
Merge pull request #3 from pingcap/master
ou-bing Sep 28, 2020
207e1b5
Merge pull request #4 from pingcap/master
ou-bing Oct 14, 2020
2f12751
add func:DropIndexes
ou-bing Oct 14, 2020
59af45c
add func: onDropIndexes
ou-bing Oct 14, 2020
05e91bb
call func: DropIndexes
ou-bing Oct 14, 2020
3fcdc88
add func: rollingbackDropIndex
ou-bing Oct 15, 2020
550a5c1
Replace onDropIndex with onDropIndexes
ou-bing Oct 15, 2020
d8b29d1
Replace rollingbackDropIndex with rollingbackDropIndexes
ou-bing Oct 15, 2020
0d21ae3
compatible DropIndex
ou-bing Oct 15, 2020
de673ef
remove func: onDropIndex
ou-bing Oct 15, 2020
6fdddca
remove func: rollingbackDropIndex
ou-bing Oct 16, 2020
acb1497
remove func: checkDropIndex
ou-bing Oct 16, 2020
7489ad4
update func: insertJobIntoDeleteRangeTable
ou-bing Oct 16, 2020
213089e
modify variable type
ou-bing Oct 17, 2020
b0301ce
update func: DropIndexes
ou-bing Oct 18, 2020
2035b28
update func: insertJobIntoDeleteRangeTable
ou-bing Oct 18, 2020
6211065
update func: checkDropIndexes
ou-bing Oct 18, 2020
716fa79
update func: insertJobIntoDeleteRangeTable
ou-bing Oct 21, 2020
68032f4
add test case: TestDropIndexes
ou-bing Oct 21, 2020
ff0d5d7
update func: onDropIndexes
ou-bing Oct 21, 2020
7eb7b13
refactor functions: DropIndex、DropIndexes
ou-bing Oct 22, 2020
28bf039
refactor func: checkDropIndexes
ou-bing Oct 23, 2020
d4cc82e
add conditional branch
ou-bing Oct 25, 2020
ec26868
add updateHiddenColumns
ou-bing Oct 25, 2020
1aef3d5
add test case
ou-bing Oct 26, 2020
7355982
add test case
ou-bing Oct 26, 2020
a72172a
fix drop primary key and index
ou-bing Oct 26, 2020
5cd8b49
add test case
ou-bing Oct 26, 2020
fcd5a23
Optimize the code
ou-bing Oct 30, 2020
9fbd625
Optimize the code
ou-bing Oct 30, 2020
385b557
Optimize the code
ou-bing Oct 30, 2020
a7ad554
Merge pull request #5 from pingcap/master
ou-bing Nov 2, 2020
f2576ff
Merge pull request #6 from pingcap/master
ou-bing Nov 5, 2020
ebcb76e
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Nov 5, 2020
4969290
Merge pull request #7 from pingcap/master
ou-bing Nov 9, 2020
0a53b5d
Merge branch 'master' into feature/dropping-multi-indexes
AilinKid Nov 12, 2020
644f4be
Merge branch 'master' into feature/dropping-multi-indexes
AilinKid Nov 12, 2020
c68d7fb
fix bug for drop indexes
ou-bing Nov 12, 2020
f90605c
Merge branch 'master' into feature/dropping-multi-indexes
AilinKid Nov 13, 2020
e56e489
Update logic for modifying job status
ou-bing Nov 13, 2020
2fa1745
Merge branch 'feature/dropping-multi-indexes' of https://github.com/o…
ou-bing Nov 13, 2020
f0f6a01
Merge branch 'master' into feature/dropping-multi-indexes
AilinKid Nov 20, 2020
3b3f3ac
Merge pull request #8 from pingcap/master
ou-bing Nov 20, 2020
0019e4f
Merge pull request #9 from pingcap/master
ou-bing Nov 24, 2020
d8c49c0
Merge pull request #10 from pingcap/master
ou-bing Dec 3, 2020
94c51dd
Merge pull request #11 from pingcap/master
ou-bing Dec 8, 2020
ef4669f
Merge pull request #12 from pingcap/master
ou-bing Dec 25, 2020
2a09406
Merge pull request #13 from pingcap/master
ou-bing Feb 24, 2021
2685390
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Feb 24, 2021
cca227f
typo
ou-bing Feb 24, 2021
3e75858
Merge pull request #14 from pingcap/master
ou-bing Mar 9, 2021
82a80f6
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Mar 9, 2021
65eb3fb
Resume the process of drop index
ou-bing Mar 9, 2021
9a32310
Resume the process of drop index
ou-bing Mar 10, 2021
a9cef9e
typo
ou-bing Mar 10, 2021
db891e1
Merge pull request #15 from pingcap/master
ou-bing Mar 16, 2021
3b83e20
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Mar 16, 2021
75fa9a0
The process of modifying drop
ou-bing Mar 19, 2021
aeccb27
Merge pull request #16 from pingcap/master
ou-bing Mar 19, 2021
71e0491
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Mar 19, 2021
25f9b9c
Optimize code
ou-bing Mar 19, 2021
ffcd5ba
Update test cases
ou-bing Mar 20, 2021
5f06638
Optimize code
ou-bing Mar 23, 2021
4ec2f31
typo
ou-bing Mar 23, 2021
7361b2c
typo
ou-bing Mar 23, 2021
0065255
Merge branch 'feature/dropping-multi-indexes' of https://github.com/o…
ou-bing Mar 23, 2021
76006c0
update test case
ou-bing Mar 28, 2021
36088b1
add func: checkNewIndexes
ou-bing Mar 30, 2021
bcac1fd
Remove rollback mechanism
ou-bing Mar 30, 2021
b24c9db
add para: ifExists
ou-bing Apr 5, 2021
d76d926
Update ddl/rollingback.go
ou-bing Apr 8, 2021
424c956
Update ddl/delete_range.go
ou-bing Apr 8, 2021
e7b376f
typo
ou-bing Apr 11, 2021
70a8b0f
remove func: getSchemaAndTable
ou-bing Apr 11, 2021
d7ad94c
Merge pull request #17 from pingcap/master
ou-bing Apr 20, 2021
c7cfcea
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Apr 20, 2021
760bfdc
add case to `IsJobRollbackable`
ou-bing Apr 21, 2021
ec8ee31
add test case
ou-bing Apr 21, 2021
62af0e8
add test case
ou-bing Apr 24, 2021
7483cdc
add test case
ou-bing Apr 27, 2021
581061c
Merge branch 'pingcap:master' into master
ou-bing May 7, 2021
8ce2b24
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing May 7, 2021
69fd5bb
add test case
ou-bing May 10, 2021
cf9c379
Merge branch 'pingcap:master' into master
ou-bing Jun 19, 2021
237ab27
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Jun 19, 2021
6952ac7
Transform the judgment of "if exists"
ou-bing Jun 28, 2021
73f5c9e
An error is thrown when deleting an invalid index.
ou-bing Jul 11, 2021
6b6c1f4
Merge branch 'pingcap:master' into master
ou-bing Aug 23, 2021
904cb3f
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Aug 23, 2021
ee8c776
update code
ou-bing Aug 23, 2021
27f5db4
update code
ou-bing Aug 25, 2021
6556377
update code
ou-bing Aug 25, 2021
6e46603
Merge branch 'master' into feature/dropping-multi-indexes
zimulala Aug 26, 2021
681cd4a
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Aug 26, 2021
c3107e8
update func: isSameTypeMultiSpecs
ou-bing Sep 1, 2021
0c1bcd1
add test case
ou-bing Sep 1, 2021
e2b6933
Merge branch 'master' into feature/dropping-multi-indexes
ou-bing Sep 1, 2021
f96b07b
Merge branch 'master' into feature/dropping-multi-indexes
zimulala Sep 2, 2021
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
90 changes: 89 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
err = d.AddColumns(ctx, ident, validSpecs)
case ast.AlterTableDropColumn:
err = d.DropColumns(ctx, ident, validSpecs)
case ast.AlterTableDropIndex:
err = d.DropIndexes(ctx, ident, validSpecs)
default:
return errRunMultiSchemaChanges
}
Expand Down Expand Up @@ -4789,7 +4791,6 @@ func (d *ddl) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName mode
err = d.doDDLJob(ctx, job)
err = d.callHookOnChanged(err)
return errors.Trace(err)

}

func (d *ddl) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName model.CIStr) error {
Expand Down Expand Up @@ -4891,6 +4892,93 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return errors.Trace(err)
}

func (d *ddl) DropIndexes(ctx sessionctx.Context, ti ast.Ident, specs []*ast.AlterTableSpec) error {
ou-bing marked this conversation as resolved.
Show resolved Hide resolved
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return errors.Trace(infoschema.ErrDatabaseNotExists)
}
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name))
}

allIndexIsExists := true
indexNames := make([]interface{}, 0, len(specs))
ou-bing marked this conversation as resolved.
Show resolved Hide resolved
for _, spec := range specs {
var indexName model.CIStr
switch spec.Tp {
case ast.AlterTableDropIndex:
indexName = model.NewCIStr(spec.Name)
case ast.AlterTableDropPrimaryKey:
indexName = model.NewCIStr(mysql.PrimaryKeyName)
default:
return nil
}

indexInfo := t.Meta().FindIndexByName(indexName.L)
var isPK bool
if indexName.L == strings.ToLower(mysql.PrimaryKeyName) &&
// Before we fixed #14243, there might be a general index named `primary` but not a primary key.
(indexInfo == nil || indexInfo.Primary) {
isPK = true
}
if isPK {
if !config.GetGlobalConfig().AlterPrimaryKey {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when alter-primary-key is false")
}
// If the table's PKIsHandle is true, we can't find the index from the table. So we check the value of PKIsHandle.
if indexInfo == nil && !t.Meta().PKIsHandle {
return ErrCantDropFieldOrKey.GenWithStack("Can't DROP 'PRIMARY'; check that column/key exists")
}
if t.Meta().PKIsHandle {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when the table's pkIsHandle is true")
}
if t.Meta().IsCommonHandle {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when the table is using clustered index")
}
}
if indexInfo == nil {
err = ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
if spec.IfExists {
ctx.GetSessionVars().StmtCtx.AppendNote(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about the case as follows:
We have two parallel DDL operations, one is add index and another is drop indexes. Assume we do these operations on table t with idx1.

  • connection 1, add index idx2;
  • connection 2, drop indexes idx1, idx2 with ifexists;(Here we can't find idx2)

In the logic, if we haven't done add index idx2 and we do drop indexes idx1, idx2 with ifexists after adding index. Then we don't append indexNames and ifExists. At last, we drop idx1 and don't drop idx2.

I think we'd better put it to onDropIndexes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

continue
}
return err
}

// Check for drop index on auto_increment column.
err = checkDropIndexOnAutoIncrementColumn(t.Meta(), indexInfo)
if err != nil {
return errors.Trace(err)
}

indexNames = append(indexNames, indexName)
ou-bing marked this conversation as resolved.
Show resolved Hide resolved

if !spec.IfExists {
allIndexIsExists = false
}
}

job := &model.Job{
SchemaID: schema.ID,
TableID: t.Meta().ID,
SchemaName: schema.Name.L,
Type: model.ActionDropIndex,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{indexNames},
}

err = d.doDDLJob(ctx, job)
// index not exists, but if_exists flags is true, so we ignore this error.
if ErrCantDropFieldOrKey.Equal(err) && allIndexIsExists {
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil
}
err = d.callHookOnChanged(err)
return errors.Trace(err)
}

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
2 changes: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64,
case model.ActionAddPrimaryKey:
ver, err = w.onCreateIndex(d, t, job, true)
case model.ActionDropIndex, model.ActionDropPrimaryKey:
ver, err = onDropIndex(t, job)
ver, err = onDropIndexes(t, job)
case model.ActionRenameIndex:
ver, err = onRenameIndex(t, job)
case model.ActionAddForeignKey:
Expand Down
61 changes: 35 additions & 26 deletions ddl/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,44 +303,53 @@ func insertJobIntoDeleteRangeTable(ctx sessionctx.Context, job *model.Job) error
// ActionAddIndex, ActionAddPrimaryKey needs do it, because it needs to be rolled back when it's canceled.
case model.ActionAddIndex, model.ActionAddPrimaryKey:
tableID := job.TableID
var indexID int64
var indexIDs []int64
var partitionIDs []int64
if err := job.DecodeArgs(&indexID, &partitionIDs); err != nil {
if err := job.DecodeArgs(&indexIDs, &partitionIDs); err != nil {
return errors.Trace(err)
}
if len(partitionIDs) > 0 {
for _, pid := range partitionIDs {
startKey := tablecodec.EncodeTableIndexPrefix(pid, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(pid, indexID+1)
if err := doInsert(s, job.ID, indexID, startKey, endKey, now); err != nil {
return errors.Trace(err)
for _, indexID := range indexIDs {
if len(partitionIDs) > 0 {
for _, pid := range partitionIDs {
startKey := tablecodec.EncodeTableIndexPrefix(pid, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(pid, indexID+1)
if err := doInsert(s, job.ID, indexID, startKey, endKey, now); err != nil {
return errors.Trace(err)
}
}
} else {
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
}
} else {
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
}
case model.ActionDropIndex, model.ActionDropPrimaryKey:
tableID := job.TableID
var indexName interface{}
var indexID int64
var indexNames []model.CIStr
var indexIDs []int64
var partitionIDs []int64
if err := job.DecodeArgs(&indexName, &indexID, &partitionIDs); err != nil {
return errors.Trace(err)
if err := job.DecodeArgs(&indexNames, &indexIDs, &partitionIDs); err != nil {
var indexName model.CIStr
if err := job.DecodeArgs(&indexName, &indexIDs, &partitionIDs); err != nil {
return errors.Trace(err)
}
indexNames = append(indexNames, indexName)
}
if len(partitionIDs) > 0 {
for _, pid := range partitionIDs {
startKey := tablecodec.EncodeTableIndexPrefix(pid, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(pid, indexID+1)
if err := doInsert(s, job.ID, indexID, startKey, endKey, now); err != nil {
return errors.Trace(err)

for _, indexID := range indexIDs {
if len(partitionIDs) > 0 {
for _, pid := range partitionIDs {
startKey := tablecodec.EncodeTableIndexPrefix(pid, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(pid, indexID+1)
if err := doInsert(s, job.ID, indexID, startKey, endKey, now); err != nil {
return errors.Trace(err)
}
}
} else {
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
}
} else {
startKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID)
endKey := tablecodec.EncodeTableIndexPrefix(tableID, indexID+1)
return doInsert(s, job.ID, indexID, startKey, endKey, now)
}
case model.ActionDropColumn:
var colName model.CIStr
Expand Down
99 changes: 66 additions & 33 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func updateHiddenColumns(tblInfo *model.TableInfo, idxInfo *model.IndexInfo, sta
func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK bool) (ver int64, err error) {
// Handle the rolling back job.
if job.IsRollingback() {
ver, err = onDropIndex(t, job)
ver, err = onDropIndexes(t, job)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down Expand Up @@ -599,25 +599,34 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK boo
return ver, errors.Trace(err)
}

func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
tblInfo, indexInfo, err := checkDropIndex(t, job)
func onDropIndexes(t *meta.Meta, job *model.Job) (ver int64, _ error) {
tblInfo, indexesInfo, err := checkDropIndexes(t, job)
if err != nil {
return ver, errors.Trace(err)
}
if len(indexesInfo) == 0 {
job.State = model.JobStateCancelled
return ver, nil
}

dependentHiddenCols := make([]*model.ColumnInfo, 0)
for _, indexColumn := range indexInfo.Columns {
if tblInfo.Columns[indexColumn.Offset].Hidden {
dependentHiddenCols = append(dependentHiddenCols, tblInfo.Columns[indexColumn.Offset])
for _, indexInfo := range indexesInfo {
for _, indexColumn := range indexInfo.Columns {
if tblInfo.Columns[indexColumn.Offset].Hidden {
dependentHiddenCols = append(dependentHiddenCols, tblInfo.Columns[indexColumn.Offset])
}
}
}

indexInfo := indexesInfo[0]
originalState := indexInfo.State
switch indexInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
indexInfo.State = model.StateWriteOnly
for _, indexInfo := range indexesInfo {
indexInfo.State = model.StateWriteOnly
}
if len(dependentHiddenCols) > 0 {
firstHiddenOffset := dependentHiddenCols[0].Offset
for i := 0; i < len(dependentHiddenCols); i++ {
Expand All @@ -630,26 +639,37 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
indexInfo.State = model.StateDeleteOnly
updateHiddenColumns(tblInfo, indexInfo, model.StateDeleteOnly)
for _, indexInfo := range indexesInfo {
indexInfo.State = model.StateDeleteOnly
updateHiddenColumns(tblInfo, indexInfo, model.StateDeleteOnly)
}
ver, err = updateVersionAndTableInfo(t, job, tblInfo, originalState != indexInfo.State)
case model.StateDeleteOnly:
// delete only -> reorganization
job.SchemaState = model.StateDeleteReorganization
indexInfo.State = model.StateDeleteReorganization
updateHiddenColumns(tblInfo, indexInfo, model.StateDeleteReorganization)
for _, indexInfo := range indexesInfo {
indexInfo.State = model.StateDeleteReorganization
updateHiddenColumns(tblInfo, indexInfo, model.StateDeleteReorganization)
}
ver, err = updateVersionAndTableInfo(t, job, tblInfo, originalState != indexInfo.State)
case model.StateDeleteReorganization:
// reorganization -> absent
indexesIDs := make([]int64, 0, len(indexesInfo))
indexesLName := make(map[string]bool, len(indexesInfo))
for _, indexInfo := range indexesInfo {
indexesLName[indexInfo.Name.L] = true
indexesIDs = append(indexesIDs, indexInfo.ID)
}
newIndices := make([]*model.IndexInfo, 0, len(tblInfo.Indices))
for _, idx := range tblInfo.Indices {
if idx.Name.L != indexInfo.Name.L {
if _, ok := indexesLName[idx.Name.L]; !ok {
newIndices = append(newIndices, idx)
}
}
tblInfo.Indices = newIndices
// Set column index flag.
dropIndexColumnFlag(tblInfo, indexInfo)
for _, indexInfo := range indexesInfo {
dropIndexColumnFlag(tblInfo, indexInfo)
}

tblInfo.Columns = tblInfo.Columns[:len(tblInfo.Columns)-len(dependentHiddenCols)]

Expand All @@ -661,52 +681,65 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
// Finish this job.
if job.IsRollingback() {
job.FinishTableJob(model.JobStateRollbackDone, model.StateNone, ver, tblInfo)
job.Args[0] = indexInfo.ID
job.Args[0] = indexesIDs
// the partition ids were append by convertAddIdxJob2RollbackJob, it is weird, but for the compatibility,
// we should keep appending the partitions in the convertAddIdxJob2RollbackJob.
ou-bing marked this conversation as resolved.
Show resolved Hide resolved
} else {
job.FinishTableJob(model.JobStateDone, model.StateNone, ver, tblInfo)
job.Args = append(job.Args, indexInfo.ID, getPartitionIDs(tblInfo))
job.Args = append(job.Args, indexesIDs, getPartitionIDs(tblInfo))
}
default:
err = ErrInvalidDDLState.GenWithStackByArgs("index", indexInfo.State)
}
return ver, errors.Trace(err)
}

func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.IndexInfo, error) {
func checkDropIndexes(t *meta.Meta, job *model.Job) (*model.TableInfo, []*model.IndexInfo, error) {
schemaID := job.SchemaID
tblInfo, err := getTableInfoAndCancelFaultJob(t, job, schemaID)
if err != nil {
return nil, nil, errors.Trace(err)
}

var indexName model.CIStr
if err = job.DecodeArgs(&indexName); err != nil {
job.State = model.JobStateCancelled
return nil, nil, errors.Trace(err)
var indexNames []model.CIStr
if err = job.DecodeArgs(&indexNames); err != nil {
var indexName model.CIStr
if err = job.DecodeArgs(&indexName); err != nil {
job.State = model.JobStateCancelled
return nil, nil, errors.Trace(err)
}
indexNames = append(indexNames, indexName)
}

indexInfo := tblInfo.FindIndexByName(indexName.L)
if indexInfo == nil {
job.State = model.JobStateCancelled
return nil, nil, ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
}
indexesInfo := make([]*model.IndexInfo, 0, len(indexNames))
indexesMap := make(map[string]bool)
for _, indexName := range indexNames {
indexInfo := tblInfo.FindIndexByName(indexName.L)
if indexInfo == nil {
job.State = model.JobStateCancelled
return nil, nil, ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
}

// Double check for drop index on auto_increment column.
err = checkDropIndexOnAutoIncrementColumn(tblInfo, indexInfo)
if err != nil {
job.State = model.JobStateCancelled
return nil, nil, autoid.ErrWrongAutoKey
// Double check for drop index on auto_increment column.
err = checkDropIndexOnAutoIncrementColumn(tblInfo, indexInfo)
if err != nil {
job.State = model.JobStateCancelled
return nil, nil, autoid.ErrWrongAutoKey
}

indexesInfo = append(indexesInfo, indexInfo)
indexesMap[indexName.L] = true
}

// Check that drop primary index will not cause invisible implicit primary index.
newIndices := make([]*model.IndexInfo, 0, len(tblInfo.Indices))

for _, idx := range tblInfo.Indices {
if idx.Name.L != indexInfo.Name.L {
if _, ok := indexesMap[idx.Name.L]; !ok {
newIndices = append(newIndices, idx)
}
}

newTbl := tblInfo.Clone()
newTbl.Indices = newIndices
err = checkInvisibleIndexOnPK(newTbl)
Expand All @@ -715,7 +748,7 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde
return nil, nil, errors.Trace(err)
}
ou-bing marked this conversation as resolved.
Show resolved Hide resolved

return tblInfo, indexInfo, nil
return tblInfo, indexesInfo, nil
}

func checkDropIndexOnAutoIncrementColumn(tblInfo *model.TableInfo, indexInfo *model.IndexInfo) error {
Expand Down
Loading