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

planner: support Hypo indexes #43607

Merged
merged 5 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6722,6 +6722,28 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, stmt *ast.CreateIndexStmt) err
stmt.IndexPartSpecifications, stmt.IndexOption, stmt.IfNotExists)
}

// addHypoIndexIntoCtx adds this index as a hypo-index into this ctx.
func (d *ddl) addHypoIndexIntoCtx(ctx sessionctx.Context, schemaName, tableName model.CIStr, indexInfo *model.IndexInfo) error {
sctx := ctx.GetSessionVars()
indexName := indexInfo.Name

if sctx.HypoIndexes == nil {
sctx.HypoIndexes = make(map[string]map[string]map[string]*model.IndexInfo)
}
if sctx.HypoIndexes[schemaName.L] == nil {
sctx.HypoIndexes[schemaName.L] = make(map[string]map[string]*model.IndexInfo)
}
if sctx.HypoIndexes[schemaName.L][tableName.L] == nil {
sctx.HypoIndexes[schemaName.L][tableName.L] = make(map[string]*model.IndexInfo)
}
if _, exist := sctx.HypoIndexes[schemaName.L][tableName.L][indexName.L]; exist {
return errors.Trace(errors.Errorf("conflict hypo index name %s", indexName.L))
}

sctx.HypoIndexes[schemaName.L][tableName.L][indexName.L] = indexInfo
return nil
}

func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.IndexKeyType, indexName model.CIStr,
indexPartSpecifications []*ast.IndexPartSpecification, indexOption *ast.IndexOption, ifNotExists bool) error {
// not support Spatial and FullText index
Expand Down Expand Up @@ -6812,6 +6834,15 @@ func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde
}
}

if indexOption != nil && indexOption.Tp == model.IndexTypeHypo { // for hypo-index
indexInfo, err := BuildIndexInfo(ctx, tblInfo.Columns, indexName, false, unique, global,
indexPartSpecifications, indexOption, model.StatePublic)
if err != nil {
return err
}
return d.addHypoIndexIntoCtx(ctx, ti.Schema, ti.Name, indexInfo)
}

tzName, tzOffset := ddlutil.GetTimeZone(ctx)
charset, collate := ctx.GetSessionVars().GetCharsetInfo()
job := &model.Job{
Expand Down Expand Up @@ -7041,6 +7072,19 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, stmt *ast.DropIndexStmt) error {
return err
}

// dropHypoIndexFromCtx drops this hypo-index from this ctx.
func (d *ddl) dropHypoIndexFromCtx(ctx sessionctx.Context, schema, table, index model.CIStr) bool {
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 false
}

func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CIStr, ifExists bool) error {
is := d.infoCache.GetLatest()
schema, ok := is.SchemaByName(ti.Schema)
Expand All @@ -7055,6 +7099,11 @@ 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
}

indexInfo := t.Meta().FindIndexByName(indexName.L)

isPK, err := CheckIsDropPrimaryKey(indexName, indexInfo, t)
Expand Down
21 changes: 20 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,10 @@ func getDefaultCollate(charsetName string) string {

// ConstructResultOfShowCreateTable constructs the result for show create table.
func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.TableInfo, allocators autoid.Allocators, buf *bytes.Buffer) (err error) {
return constructResultOfShowCreateTable(ctx, nil, tableInfo, allocators, buf)
}

func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CIStr, tableInfo *model.TableInfo, allocators autoid.Allocators, buf *bytes.Buffer) (err error) {
if tableInfo.IsView() {
fetchShowCreateTable4View(ctx, tableInfo, buf)
return nil
Expand Down Expand Up @@ -1109,6 +1113,18 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
publicIndices = append(publicIndices, idx)
}
}

// consider hypo-indexes
hypoIndexes := ctx.GetSessionVars().HypoIndexes
if hypoIndexes != nil && dbName != nil {
schemaName := dbName.L
tblName := tableInfo.Name.L
if hypoIndexes[schemaName] != nil && hypoIndexes[schemaName][tblName] != nil {
for _, index := range hypoIndexes[schemaName][tblName] {
publicIndices = append(publicIndices, index)
}
}
}
if len(publicIndices) > 0 {
buf.WriteString(",\n")
}
Expand Down Expand Up @@ -1142,6 +1158,9 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if idxInfo.Comment != "" {
fmt.Fprintf(buf, ` COMMENT '%s'`, format.OutputFormat(idxInfo.Comment))
}
if idxInfo.Tp == model.IndexTypeHypo {
fmt.Fprintf(buf, ` /* HYPO INDEX */`)
}
if idxInfo.Primary {
if tableInfo.HasClusteredIndex() {
buf.WriteString(" /*T![clustered_index] CLUSTERED */")
Expand Down Expand Up @@ -1392,7 +1411,7 @@ func (e *ShowExec) fetchShowCreateTable() error {
tableInfo := tb.Meta()
var buf bytes.Buffer
// TODO: let the result more like MySQL.
if err = ConstructResultOfShowCreateTable(e.ctx, tableInfo, tb.Allocators(e.ctx), &buf); err != nil {
if err = constructResultOfShowCreateTable(e.ctx, &e.DBName, tableInfo, tb.Allocators(e.ctx), &buf); err != nil {
return err
}
if tableInfo.IsView() {
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ var tokenMap = map[string]int{
"ROW": row,
"ROWS": rows,
"RTREE": rtree,
"HYPO": hypo,
"RESUME": resume,
"RUN": run,
"RUNNING": running,
Expand Down
3 changes: 3 additions & 0 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,8 @@ func (t IndexType) String() string {
return "HASH"
case IndexTypeRtree:
return "RTREE"
case IndexTypeHypo:
return "HYPO"
default:
return ""
}
Expand All @@ -1375,6 +1377,7 @@ const (
IndexTypeBtree
IndexTypeHash
IndexTypeRtree
IndexTypeHypo
)

// IndexInfo provides meta data describing a DB index.
Expand Down