forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_index.go
242 lines (218 loc) · 7.42 KB
/
alter_index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"strings"
"gopkg.in/src-d/go-errors.v1"
"github.com/Sndav/go-mysql-server/sql"
)
var (
// ErrIndexActionNotImplemented is returned when the action has not been implemented
ErrIndexActionNotImplemented = errors.NewKind("alter table index action is not implemented: %v")
// ErrCreateIndexMissingColumns is returned when a CREATE INDEX statement does not provide any columns
ErrCreateIndexMissingColumns = errors.NewKind("cannot create an index without columns")
// ErrCreateIndexNonExistentColumn is returned when a key is provided in the index that isn't in the table
ErrCreateIndexNonExistentColumn = errors.NewKind("column `%v` does not exist in the table")
// ErrCreateIndexDuplicateColumn is returned when a CREATE INDEX statement has the same column multiple times
ErrCreateIndexDuplicateColumn = errors.NewKind("cannot have duplicates of columns in an index: `%v`")
)
type IndexAction byte
const (
IndexAction_Create IndexAction = iota
IndexAction_Drop
IndexAction_Rename
)
type AlterIndex struct {
// Action states whether it's a CREATE, DROP, or RENAME
Action IndexAction
// Table is the table that is being referenced
Table sql.Node
// IndexName is the index name, and in the case of a RENAME it represents the new name
IndexName string
// PreviousIndexName states the old name when renaming an index
PreviousIndexName string
// Using states whether you're using BTREE, HASH, or none
Using sql.IndexUsing
// Constraint specifies whether this is UNIQUE, FULLTEXT, SPATIAL, or none
Constraint sql.IndexConstraint
// Columns contains the column names (and possibly lengths) when creating an index
Columns []sql.IndexColumn
// Comment is the comment that was left at index creation, if any
Comment string
}
func NewAlterCreateIndex(table sql.Node, indexName string, using sql.IndexUsing, constraint sql.IndexConstraint, columns []sql.IndexColumn, comment string) *AlterIndex {
return &AlterIndex{
Action: IndexAction_Create,
Table: table,
IndexName: indexName,
Using: using,
Constraint: constraint,
Columns: columns,
Comment: comment,
}
}
func NewAlterDropIndex(table sql.Node, indexName string) *AlterIndex {
return &AlterIndex{
Action: IndexAction_Drop,
Table: table,
IndexName: indexName,
}
}
func NewAlterRenameIndex(table sql.Node, fromIndexName, toIndexName string) *AlterIndex {
return &AlterIndex{
Action: IndexAction_Rename,
Table: table,
IndexName: toIndexName,
PreviousIndexName: fromIndexName,
}
}
// Schema implements the Node interface.
func (p *AlterIndex) Schema() sql.Schema {
return nil
}
func getIndexAlterable(node sql.Node) (sql.IndexAlterableTable, error) {
switch node := node.(type) {
case sql.IndexAlterableTable:
return node, nil
case *ResolvedTable:
return getIndexAlterableTable(node.Table)
case sql.TableWrapper:
return getIndexAlterableTable(node.Underlying())
default:
return nil, ErrNotIndexable.New()
}
}
func getIndexAlterableTable(t sql.Table) (sql.IndexAlterableTable, error) {
switch t := t.(type) {
case sql.IndexAlterableTable:
return t, nil
case sql.TableWrapper:
return getIndexAlterableTable(t.Underlying())
default:
return nil, ErrNotIndexable.New()
}
}
// Execute inserts the rows in the database.
func (p *AlterIndex) Execute(ctx *sql.Context) error {
indexable, err := getIndexAlterable(p.Table)
if err != nil {
return err
}
switch p.Action {
case IndexAction_Create:
if len(p.Columns) == 0 {
return ErrCreateIndexMissingColumns.New()
}
// Make sure that all columns are valid, in the table, and there are no duplicates
seenCols := make(map[string]bool)
for _, col := range indexable.Schema() {
seenCols[col.Name] = false
}
for _, indexCol := range p.Columns {
if seen, ok := seenCols[indexCol.Name]; ok {
if !seen {
seenCols[indexCol.Name] = true
} else {
return ErrCreateIndexDuplicateColumn.New(indexCol.Name)
}
} else {
return ErrCreateIndexNonExistentColumn.New(indexCol.Name)
}
}
return indexable.CreateIndex(ctx, p.IndexName, p.Using, p.Constraint, p.Columns, p.Comment)
case IndexAction_Drop:
return indexable.DropIndex(ctx, p.IndexName)
case IndexAction_Rename:
return indexable.RenameIndex(ctx, p.PreviousIndexName, p.IndexName)
default:
return ErrIndexActionNotImplemented.New(p.Action)
}
}
// RowIter implements the Node interface.
func (p *AlterIndex) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
err := p.Execute(ctx)
if err != nil {
return nil, err
}
return sql.RowsToRowIter(), nil
}
// WithChildren implements the Node interface.
func (p *AlterIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
}
switch p.Action {
case IndexAction_Create:
return NewAlterCreateIndex(children[0], p.IndexName, p.Using, p.Constraint, p.Columns, p.Comment), nil
case IndexAction_Drop:
return NewAlterDropIndex(children[0], p.IndexName), nil
case IndexAction_Rename:
return NewAlterRenameIndex(children[0], p.PreviousIndexName, p.IndexName), nil
default:
return nil, ErrIndexActionNotImplemented.New(p.Action)
}
}
func (p AlterIndex) String() string {
pr := sql.NewTreePrinter()
switch p.Action {
case IndexAction_Create:
_ = pr.WriteNode("CreateIndex(%s)", p.IndexName)
children := []string{fmt.Sprintf("Table(%s)", p.Table.String())}
switch p.Constraint {
case sql.IndexConstraint_Unique:
children = append(children, "Constraint(UNIQUE)")
case sql.IndexConstraint_Spatial:
children = append(children, "Constraint(SPATIAL)")
case sql.IndexConstraint_Fulltext:
children = append(children, "Constraint(FULLTEXT)")
}
switch p.Using {
case sql.IndexUsing_BTree, sql.IndexUsing_Default:
children = append(children, "Using(BTREE)")
case sql.IndexUsing_Hash:
children = append(children, "Using(HASH)")
}
cols := make([]string, len(p.Columns))
for i, col := range p.Columns {
if col.Length == 0 {
cols[i] = col.Name
} else {
cols[i] = fmt.Sprintf("%s(%v)", col.Name, col.Length)
}
}
children = append(children, fmt.Sprintf("Columns(%s)", strings.Join(cols, ", ")))
children = append(children, fmt.Sprintf("Comment(%s)", p.Comment))
_ = pr.WriteChildren(children...)
case IndexAction_Drop:
_ = pr.WriteNode("DropIndex(%s)", p.IndexName)
_ = pr.WriteChildren(fmt.Sprintf("Table(%s)", p.Table.String()))
case IndexAction_Rename:
_ = pr.WriteNode("RenameIndex")
_ = pr.WriteChildren(
fmt.Sprintf("Table(%s)", p.Table.String()),
fmt.Sprintf("FromIndex(%s)", p.PreviousIndexName),
fmt.Sprintf("ToIndex(%s)", p.IndexName),
)
default:
_ = pr.WriteNode("Unknown_Index_Action(%v)", p.Action)
}
return pr.String()
}
func (p *AlterIndex) Resolved() bool {
return p.Table.Resolved()
}
func (p *AlterIndex) Children() []sql.Node {
return []sql.Node{p.Table}
}