Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: crazycs520 <crazycs520@gmail.com>
  • Loading branch information
crazycs520 committed Dec 21, 2022
1 parent 9b10663 commit 9ba0d2f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
4 changes: 2 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ func addIndexForForeignKey(ctx sessionctx.Context, tbInfo *model.TableInfo) erro
if handleCol != nil && len(fk.Cols) == 1 && handleCol.Name.L == fk.Cols[0].L {
continue
}
if model.FindIndexByColumns(tbInfo, fk.Cols...) != nil {
if model.FindIndexByColumns(tbInfo, tbInfo.Indices, fk.Cols...) != nil {
continue
}
idxName := fk.Name
Expand Down Expand Up @@ -6578,7 +6578,7 @@ func (d *ddl) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName mode
if err != nil {
return err
}
if model.FindIndexByColumns(t.Meta(), fkInfo.Cols...) == nil {
if model.FindIndexByColumns(t.Meta(), t.Meta().Indices, fkInfo.Cols...) == nil {
// Need to auto create index for fk cols
if ctx.GetSessionVars().StmtCtx.MultiSchemaInfo == nil {
ctx.GetSessionVars().StmtCtx.MultiSchemaInfo = model.NewMultiSchemaInfo()
Expand Down
4 changes: 2 additions & 2 deletions ddl/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func checkTableForeignKey(referTblInfo, tblInfo *model.TableInfo, fkInfo *model.
}
}
// check refer columns should have index.
if model.FindIndexByColumns(referTblInfo, fkInfo.RefCols...) == nil {
if model.FindIndexByColumns(referTblInfo, referTblInfo.Indices, fkInfo.RefCols...) == nil {
return infoschema.ErrForeignKeyNoIndexInParent.GenWithStackByArgs(fkInfo.Name, fkInfo.RefTable)
}
return nil
Expand Down Expand Up @@ -660,7 +660,7 @@ func checkAddForeignKeyValidInOwner(d *ddlCtx, t *meta.Meta, schema string, tbIn
return nil
}
}
if model.FindIndexByColumns(tbInfo, fk.Cols...) == nil {
if model.FindIndexByColumns(tbInfo, tbInfo.Indices, fk.Cols...) == nil {
return errors.Errorf("Failed to add the foreign key constraint. Missing index for '%s' foreign key columns in the table '%s'", fk.Name, tbInfo.Name)
}
return nil
Expand Down
11 changes: 1 addition & 10 deletions ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,13 @@ func checkOperateDropIndexUseByForeignKey(info *model.MultiSchemaInfo, t table.T
}

for _, fk := range info.AddForeignKeys {
if droppingIdx := findIndexByColumns(tbInfo, droppingIndexes, fk.Cols...); droppingIdx != nil && findIndexByColumns(tbInfo, remainIndexes, fk.Cols...) == nil {
if droppingIdx := model.FindIndexByColumns(tbInfo, droppingIndexes, fk.Cols...); droppingIdx != nil && model.FindIndexByColumns(tbInfo, remainIndexes, fk.Cols...) == nil {
return dbterror.ErrDropIndexNeededInForeignKey.GenWithStackByArgs(droppingIdx.Name)
}
}
return nil
}

func findIndexByColumns(tbInfo *model.TableInfo, indexes []*model.IndexInfo, cols ...model.CIStr) *model.IndexInfo {
for _, index := range indexes {
if model.IsIndexPrefixCovered(tbInfo, index, cols...) {
return index
}
}
return nil
}

func checkMultiSchemaInfo(info *model.MultiSchemaInfo, t table.Table) error {
err := checkOperateSameColAndIdx(info)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ func FindFKInfoByName(fks []*FKInfo, name string) *FKInfo {
}

// FindIndexByColumns find IndexInfo in indices which is cover the specified columns.
func FindIndexByColumns(tbInfo *TableInfo, cols ...CIStr) *IndexInfo {
for _, index := range tbInfo.Indices {
func FindIndexByColumns(tbInfo *TableInfo, indices []*IndexInfo, cols ...CIStr) *IndexInfo {
for _, index := range indices {
if IsIndexPrefixCovered(tbInfo, index, cols...) {
return index
}
Expand Down
4 changes: 2 additions & 2 deletions planner/core/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func buildFKCheck(ctx sessionctx.Context, tbl table.Table, cols []model.CIStr, f
}
}

referTbIdxInfo := model.FindIndexByColumns(tblInfo, cols...)
referTbIdxInfo := model.FindIndexByColumns(tblInfo, tblInfo.Indices, cols...)
if referTbIdxInfo == nil {
return nil, failedErr
}
Expand Down Expand Up @@ -460,7 +460,7 @@ func buildFKCascade(ctx sessionctx.Context, tp FKCascadeType, referredFK *model.
return fkCascade, nil
}
}
indexForFK := model.FindIndexByColumns(childTable.Meta(), fk.Cols...)
indexForFK := model.FindIndexByColumns(childTable.Meta(), childTable.Meta().Indices, fk.Cols...)
if indexForFK == nil {
return nil, errors.Errorf("Missing index for '%s' foreign key columns in the table '%s'", fk.Name, childTable.Meta().Name)
}
Expand Down

0 comments on commit 9ba0d2f

Please sign in to comment.