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

Added IsGenerated to sql.Index #359

Merged
merged 1 commit into from
Apr 6, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions memory/mergeable_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (i *MergeableIndex) Database() string { return i.DB }
func (i *MergeableIndex) Driver() string { return i.DriverName }
func (i *MergeableIndex) MemTable() *Table { return i.Tbl }
func (i *MergeableIndex) ColumnExpressions() []sql.Expression { return i.Exprs }
func (i *MergeableIndex) IsGenerated() bool { return false }

func (i *MergeableIndex) Expressions() []string {
var exprs []string
Expand Down
17 changes: 14 additions & 3 deletions sql/analyzer/assign_info_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ func assignInfoSchema(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (
return nil, err
}

x.IndexesToShow = tableIndexes
x.IndexesToShow = filterGeneratedIndexes(tableIndexes)
case *plan.ShowCreateTable:
if !x.IsView {
tableIndexes, err := getIndexesForTable(ctx, a, x.Child)
if err != nil {
return nil, err
}

x.Indexes = tableIndexes
x.Indexes = filterGeneratedIndexes(tableIndexes)
}
case *plan.ShowColumns:
tableIndexes, err := getIndexesForTable(ctx, a, x.Child)
if err != nil {
return nil, err
}

x.Indexes = tableIndexes
x.Indexes = filterGeneratedIndexes(tableIndexes)
case *plan.ShowCharset:
rt, err := getInformationSchemaTable(ctx, a, "character_sets")
if err != nil {
Expand All @@ -64,6 +64,17 @@ func assignInfoSchema(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (
})
}

// filterGeneratedIndexes removes all generated indexes from a slice of indexes.
func filterGeneratedIndexes(indexes []sql.Index) []sql.Index {
var newIndexes []sql.Index
for _, index := range indexes {
if !index.IsGenerated() {
newIndexes = append(newIndexes, index)
}
}
return newIndexes
}

// getIndexesForTable returns all indexes on the table represented by the node given. If the node isn't a
// *(plan.ResolvedTable), returns an empty slice.
func getIndexesForTable(ctx *sql.Context, a *Analyzer, node sql.Node) ([]sql.Index, error) {
Expand Down
3 changes: 3 additions & 0 deletions sql/analyzer/resolve_create_like.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func resolveCreateLike(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope)
return nil, err
}
for _, index := range indexes {
if index.IsGenerated() {
continue
}
constraint := sql.IndexConstraint_None
if index.IsUnique() {
constraint = sql.IndexConstraint_Unique
Expand Down
3 changes: 3 additions & 0 deletions sql/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Index interface {
Comment() string
// IndexType returns the type of this index, e.g. BTREE
IndexType() string
// IsGenerated returns whether this index was generated. Generated indexes
// are used for index access, but are not displayed (such as with SHOW INDEXES).
IsGenerated() bool
}

// AscendIndex is an index that is sorted in ascending order.
Expand Down
1 change: 1 addition & 0 deletions sql/index_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ func (i dummyIdx) Table() string { return i.table
func (i dummyIdx) Driver() string { return "dummy" }
func (i dummyIdx) IsUnique() bool { return false }
func (i dummyIdx) Comment() string { return "" }
func (i dummyIdx) IsGenerated() bool { return false }
func (i dummyIdx) IndexType() string { return "BTREE" }

type dummyExpr struct {
Expand Down
1 change: 1 addition & 0 deletions sql/plan/create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func (i *mockIndex) Database() string { return i.db }
func (i *mockIndex) IsUnique() bool { return i.unique }
func (i *mockIndex) Comment() string { return i.comment }
func (i *mockIndex) IndexType() string { return "BTREE" }
func (i *mockIndex) IsGenerated() bool { return false }
func (i *mockIndex) Expressions() []string {
exprs := make([]string, len(i.exprs))
for i, e := range i.exprs {
Expand Down