planner: update the syntax of drop hypo index (#44944)

close pingcap/tidb#43817
This commit is contained in:
Yuanjia Zhang
2023-06-26 17:08:35 +08:00
committed by GitHub
parent 352b44e1e4
commit 09bbf08d72
7 changed files with 5183 additions and 5147 deletions

View File

@ -3526,9 +3526,9 @@ func (d *ddl) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast
case ast.AlterTableDropColumn:
err = d.DropColumn(sctx, ident, spec)
case ast.AlterTableDropIndex:
err = d.dropIndex(sctx, ident, model.NewCIStr(spec.Name), spec.IfExists)
err = d.dropIndex(sctx, ident, model.NewCIStr(spec.Name), spec.IfExists, false)
case ast.AlterTableDropPrimaryKey:
err = d.dropIndex(sctx, ident, model.NewCIStr(mysql.PrimaryKeyName), spec.IfExists)
err = d.dropIndex(sctx, ident, model.NewCIStr(mysql.PrimaryKeyName), spec.IfExists, false)
case ast.AlterTableRenameIndex:
err = d.RenameIndex(sctx, ident, spec)
case ast.AlterTableDropPartition, ast.AlterTableDropFirstPartition:
@ -7235,7 +7235,7 @@ func (d *ddl) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName model.
func (d *ddl) DropIndex(ctx sessionctx.Context, stmt *ast.DropIndexStmt) error {
ti := ast.Ident{Schema: stmt.Table.Schema, Name: stmt.Table.Name}
err := d.dropIndex(ctx, ti, model.NewCIStr(stmt.IndexName), stmt.IfExists)
err := d.dropIndex(ctx, ti, model.NewCIStr(stmt.IndexName), stmt.IfExists, stmt.IsHypo)
if (infoschema.ErrDatabaseNotExists.Equal(err) || infoschema.ErrTableNotExists.Equal(err)) && stmt.IfExists {
err = nil
}
@ -7243,19 +7243,24 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, stmt *ast.DropIndexStmt) error {
}
// dropHypoIndexFromCtx drops this hypo-index from this ctx.
func (*ddl) dropHypoIndexFromCtx(ctx sessionctx.Context, schema, table, index model.CIStr) bool {
func (*ddl) dropHypoIndexFromCtx(ctx sessionctx.Context, schema, table, index model.CIStr, ifExists bool) error {
sctx := ctx.GetSessionVars()
if sctx.HypoIndexes != nil &&
sctx.HypoIndexes[schema.L] != nil &&
sctx.HypoIndexes[schema.L][table.L] != nil &&
sctx.HypoIndexes[schema.L][table.L][index.L] != nil {
delete(sctx.HypoIndexes[schema.L][table.L], index.L)
return true
return nil
}
return false
if !ifExists {
return dbterror.ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", index)
}
return nil
}
func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CIStr, ifExists bool) error {
// dropIndex drops the specified index.
// isHypo is used to indicate whether this operation is for a hypo-index.
func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CIStr, ifExists, isHypo bool) error {
is := d.infoCache.GetLatest()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
@ -7269,9 +7274,8 @@ func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
return errors.Trace(dbterror.ErrOptOnCacheTable.GenWithStackByArgs("Drop Index"))
}
// try hypo-index first
if d.dropHypoIndexFromCtx(ctx, ti.Schema, ti.Name, indexName) {
return nil
if isHypo {
return d.dropHypoIndexFromCtx(ctx, ti.Schema, ti.Name, indexName, ifExists)
}
indexInfo := t.Meta().FindIndexByName(indexName.L)