-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ddl.go
417 lines (380 loc) · 13.7 KB
/
ddl.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package planbuilder
import (
"fmt"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// Error messages for CreateView queries
const (
ViewDifferentKeyspace string = "Select query does not belong to the same keyspace as the view statement"
ViewComplex string = "Complex select queries are not supported in create or alter view statements"
DifferentDestinations string = "Tables or Views specified in the query do not belong to the same destination"
)
type fkStrategy int
const (
fkAllow fkStrategy = iota
fkDisallow
)
var fkStrategyMap = map[string]fkStrategy{
"allow": fkAllow,
"disallow": fkDisallow,
}
type fkContraint struct {
found bool
}
func (fk *fkContraint) FkWalk(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node.(type) {
case *sqlparser.CreateTable, *sqlparser.AlterTable,
*sqlparser.TableSpec, *sqlparser.AddConstraintDefinition, *sqlparser.ConstraintDefinition:
return true, nil
case *sqlparser.ForeignKeyDefinition:
fk.found = true
}
return false, nil
}
// buildGeneralDDLPlan builds a general DDL plan, which can be either normal DDL or online DDL.
// The two behave completely differently, and have two very different primitives.
// We want to be able to dynamically choose between normal/online plans according to Session settings.
// However, due to caching of plans, we're unable to make that choice right now. In this function we don't have
// a session context. It's only when we Execute() the primitive that we have that context.
// This is why we return a compound primitive (DDL) which contains fully populated primitives (Send & OnlineDDL),
// and which chooses which of the two to invoke at runtime.
func buildGeneralDDLPlan(sql string, ddlStatement sqlparser.DDLStatement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
if vschema.Destination() != nil {
return buildByPassDDLPlan(sql, vschema)
}
normalDDLPlan, onlineDDLPlan, err := buildDDLPlans(sql, ddlStatement, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
if err != nil {
return nil, err
}
if ddlStatement.IsTemporary() {
err := vschema.ErrorIfShardedF(normalDDLPlan.Keyspace, "temporary table", "Temporary table not supported in sharded database %s", normalDDLPlan.Keyspace.Name)
if err != nil {
return nil, err
}
onlineDDLPlan = nil // emptying this so it does not accidentally gets used somewhere
}
eddl := &engine.DDL{
Keyspace: normalDDLPlan.Keyspace,
SQL: normalDDLPlan.Query,
DDL: ddlStatement,
NormalDDL: normalDDLPlan,
OnlineDDL: onlineDDLPlan,
DirectDDLEnabled: enableDirectDDL,
OnlineDDLEnabled: enableOnlineDDL,
CreateTempTable: ddlStatement.IsTemporary(),
}
tc := &tableCollector{}
for _, tbl := range ddlStatement.AffectedTables() {
tc.addASTTable(normalDDLPlan.Keyspace.Name, tbl)
}
return newPlanResult(eddl, tc.getTables()...), nil
}
func buildByPassDDLPlan(sql string, vschema plancontext.VSchema) (*planResult, error) {
keyspace, err := vschema.DefaultKeyspace()
if err != nil {
return nil, err
}
send := &engine.Send{
Keyspace: keyspace,
TargetDestination: vschema.Destination(),
Query: sql,
}
return newPlanResult(send), nil
}
func buildDDLPlans(sql string, ddlStatement sqlparser.DDLStatement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*engine.Send, *engine.OnlineDDL, error) {
var destination key.Destination
var keyspace *vindexes.Keyspace
var err error
switch ddl := ddlStatement.(type) {
case *sqlparser.AlterTable, *sqlparser.CreateTable, *sqlparser.TruncateTable:
err = checkFKError(vschema, ddlStatement)
if err != nil {
return nil, nil, err
}
// For ALTER TABLE and TRUNCATE TABLE, the table must already exist
//
// For CREATE TABLE, the table may (in the case of --declarative)
// already exist.
//
// We should find the target of the query from this tables location.
destination, keyspace, err = findTableDestinationAndKeyspace(vschema, ddlStatement)
case *sqlparser.CreateView:
destination, keyspace, err = buildCreateView(vschema, ddl, reservedVars, enableOnlineDDL, enableDirectDDL)
case *sqlparser.AlterView:
destination, keyspace, err = buildAlterView(vschema, ddl, reservedVars, enableOnlineDDL, enableDirectDDL)
case *sqlparser.DropView:
destination, keyspace, err = buildDropView(vschema, ddlStatement)
case *sqlparser.DropTable:
destination, keyspace, err = buildDropTable(vschema, ddlStatement)
case *sqlparser.RenameTable:
destination, keyspace, err = buildRenameTable(vschema, ddl)
default:
return nil, nil, vterrors.VT13001(fmt.Sprintf("unexpected DDL statement type: %T", ddlStatement))
}
if err != nil {
return nil, nil, err
}
if destination == nil {
destination = key.DestinationAllShards{}
}
query := sql
// If the query is fully parsed, generate the query from the ast. Otherwise, use the original query
if ddlStatement.IsFullyParsed() {
query = sqlparser.String(ddlStatement)
}
return &engine.Send{
Keyspace: keyspace,
TargetDestination: destination,
Query: query,
}, &engine.OnlineDDL{
Keyspace: keyspace,
TargetDestination: destination,
DDL: ddlStatement,
SQL: query,
}, nil
}
func checkFKError(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStatement) error {
if fkStrategyMap[vschema.ForeignKeyMode()] == fkDisallow {
fk := &fkContraint{}
_ = sqlparser.Walk(fk.FkWalk, ddlStatement)
if fk.found {
return vterrors.VT10001()
}
}
return nil
}
func findTableDestinationAndKeyspace(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStatement) (key.Destination, *vindexes.Keyspace, error) {
var table *vindexes.Table
var destination key.Destination
var keyspace *vindexes.Keyspace
var err error
table, _, _, _, destination, err = vschema.FindTableOrVindex(ddlStatement.GetTable())
if err != nil {
_, isNotFound := err.(vindexes.NotFoundError)
if !isNotFound {
return nil, nil, err
}
}
if table == nil {
destination, keyspace, _, err = vschema.TargetDestination(ddlStatement.GetTable().Qualifier.String())
if err != nil {
return nil, nil, err
}
ddlStatement.SetTable("", ddlStatement.GetTable().Name.String())
} else {
keyspace = table.Keyspace
ddlStatement.SetTable("", table.Name.String())
}
return destination, keyspace, nil
}
func buildAlterView(vschema plancontext.VSchema, ddl *sqlparser.AlterView, reservedVars *sqlparser.ReservedVars, enableOnlineDDL, enableDirectDDL bool) (key.Destination, *vindexes.Keyspace, error) {
// For Alter View, we require that the view exist and the select query can be satisfied within the keyspace itself
// We should remove the keyspace name from the table name, as the database name in MySQL might be different than the keyspace name
destination, keyspace, err := findTableDestinationAndKeyspace(vschema, ddl)
if err != nil {
return nil, nil, err
}
selectPlan, err := createInstructionFor(sqlparser.String(ddl.Select), ddl.Select, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
if err != nil {
return nil, nil, err
}
selPlanKs := selectPlan.primitive.GetKeyspaceName()
if keyspace.Name != selPlanKs {
return nil, nil, vterrors.VT12001(ViewDifferentKeyspace)
}
if vschema.IsViewsEnabled() {
if keyspace == nil {
return nil, nil, vterrors.VT09005()
}
return destination, keyspace, nil
}
isRoutePlan, opCode := tryToGetRoutePlan(selectPlan.primitive)
if !isRoutePlan {
return nil, nil, vterrors.VT12001(ViewComplex)
}
if opCode != engine.Unsharded && opCode != engine.EqualUnique && opCode != engine.Scatter {
return nil, nil, vterrors.VT12001(ViewComplex)
}
_ = sqlparser.SafeRewrite(ddl.Select, nil, func(cursor *sqlparser.Cursor) bool {
switch tableName := cursor.Node().(type) {
case sqlparser.TableName:
cursor.Replace(sqlparser.TableName{
Name: tableName.Name,
})
}
return true
})
return destination, keyspace, nil
}
func buildCreateView(vschema plancontext.VSchema, ddl *sqlparser.CreateView, reservedVars *sqlparser.ReservedVars, enableOnlineDDL, enableDirectDDL bool) (key.Destination, *vindexes.Keyspace, error) {
// For Create View, we require that the keyspace exist and the select query can be satisfied within the keyspace itself
// We should remove the keyspace name from the table name, as the database name in MySQL might be different than the keyspace name
destination, keyspace, _, err := vschema.TargetDestination(ddl.ViewName.Qualifier.String())
if err != nil {
return nil, nil, err
}
ddl.ViewName.Qualifier = sqlparser.NewIdentifierCS("")
selectPlan, err := createInstructionFor(sqlparser.String(ddl.Select), ddl.Select, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
if err != nil {
return nil, nil, err
}
selPlanKs := selectPlan.primitive.GetKeyspaceName()
if keyspace.Name != selPlanKs {
return nil, nil, vterrors.VT12001(ViewDifferentKeyspace)
}
if vschema.IsViewsEnabled() {
if keyspace == nil {
return nil, nil, vterrors.VT09005()
}
return destination, keyspace, nil
}
isRoutePlan, opCode := tryToGetRoutePlan(selectPlan.primitive)
if !isRoutePlan {
return nil, nil, vterrors.VT12001(ViewComplex)
}
if opCode != engine.Unsharded && opCode != engine.EqualUnique && opCode != engine.Scatter {
return nil, nil, vterrors.VT12001(ViewComplex)
}
_ = sqlparser.SafeRewrite(ddl.Select, nil, func(cursor *sqlparser.Cursor) bool {
switch tableName := cursor.Node().(type) {
case sqlparser.TableName:
cursor.Replace(sqlparser.TableName{
Name: tableName.Name,
})
}
return true
})
return destination, keyspace, nil
}
func buildDropView(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStatement) (key.Destination, *vindexes.Keyspace, error) {
if !vschema.IsViewsEnabled() {
return buildDropTable(vschema, ddlStatement)
}
var ks *vindexes.Keyspace
viewMap := make(map[string]any)
for _, tbl := range ddlStatement.GetFromTables() {
_, ksForView, _, err := vschema.TargetDestination(tbl.Qualifier.String())
if err != nil {
return nil, nil, err
}
if ksForView == nil {
return nil, nil, vterrors.VT09005()
}
if ks == nil {
ks = ksForView
} else if ks.Name != ksForView.Name {
return nil, nil, vterrors.VT12001("cannot drop views from multiple keyspace in a single statement")
}
if _, exists := viewMap[tbl.Name.String()]; exists {
return nil, nil, vterrors.VT03013(tbl.Name.String())
}
viewMap[tbl.Name.String()] = nil
tbl.Qualifier = sqlparser.NewIdentifierCS("")
}
return key.DestinationAllShards{}, ks, nil
}
func buildDropTable(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStatement) (key.Destination, *vindexes.Keyspace, error) {
var destination key.Destination
var keyspace *vindexes.Keyspace
for i, tab := range ddlStatement.GetFromTables() {
var destinationTab key.Destination
var keyspaceTab *vindexes.Keyspace
var table *vindexes.Table
var err error
table, _, _, _, destinationTab, err = vschema.FindTableOrVindex(tab)
if err != nil {
_, isNotFound := err.(vindexes.NotFoundError)
if !isNotFound {
return nil, nil, err
}
}
if table == nil {
destinationTab, keyspaceTab, _, err = vschema.TargetDestination(tab.Qualifier.String())
if err != nil {
return nil, nil, err
}
ddlStatement.GetFromTables()[i] = sqlparser.TableName{
Name: tab.Name,
}
} else {
keyspaceTab = table.Keyspace
ddlStatement.GetFromTables()[i] = sqlparser.TableName{
Name: table.Name,
}
}
if destination == nil && keyspace == nil {
destination = destinationTab
keyspace = keyspaceTab
}
if destination != destinationTab || keyspace != keyspaceTab {
return nil, nil, vterrors.VT12001(DifferentDestinations)
}
}
return destination, keyspace, nil
}
func buildRenameTable(vschema plancontext.VSchema, renameTable *sqlparser.RenameTable) (key.Destination, *vindexes.Keyspace, error) {
var destination key.Destination
var keyspace *vindexes.Keyspace
for _, tabPair := range renameTable.TablePairs {
var destinationFrom key.Destination
var keyspaceFrom *vindexes.Keyspace
var table *vindexes.Table
var err error
table, _, _, _, destinationFrom, err = vschema.FindTableOrVindex(tabPair.FromTable)
if err != nil {
_, isNotFound := err.(vindexes.NotFoundError)
if !isNotFound {
return nil, nil, err
}
}
if table == nil {
destinationFrom, keyspaceFrom, _, err = vschema.TargetDestination(tabPair.FromTable.Qualifier.String())
if err != nil {
return nil, nil, err
}
tabPair.FromTable = sqlparser.TableName{
Name: tabPair.FromTable.Name,
}
} else {
keyspaceFrom = table.Keyspace
tabPair.FromTable = sqlparser.TableName{
Name: table.Name,
}
}
if tabPair.ToTable.Qualifier.String() != "" {
_, keyspaceTo, _, err := vschema.TargetDestination(tabPair.ToTable.Qualifier.String())
if err != nil {
return nil, nil, err
}
if keyspaceTo.Name != keyspaceFrom.Name {
return nil, nil, vterrors.VT03002(keyspaceFrom.Name, keyspaceTo.Name)
}
tabPair.ToTable = sqlparser.TableName{
Name: tabPair.ToTable.Name,
}
}
if destination == nil && keyspace == nil {
destination = destinationFrom
keyspace = keyspaceFrom
}
if destination != destinationFrom || keyspace != keyspaceFrom {
return nil, nil, vterrors.VT12001(DifferentDestinations)
}
}
return destination, keyspace, nil
}
func tryToGetRoutePlan(selectPlan engine.Primitive) (valid bool, opCode engine.Opcode) {
switch plan := selectPlan.(type) {
case *engine.Route:
return true, plan.Opcode
case engine.Gen4Comparer:
return tryToGetRoutePlan(plan.GetGen4Primitive())
default:
return false, engine.Opcode(0)
}
}