-
Notifications
You must be signed in to change notification settings - Fork 2k
/
builder.go
507 lines (451 loc) · 15.8 KB
/
builder.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
Copyright 2019 The Vitess Authors.
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 planbuilder
import (
"fmt"
"sort"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
const (
// V3 is also the default planner
V3 = querypb.ExecuteOptions_V3
// Gen4 uses the default Gen4 planner, which is the greedy planner
Gen4 = querypb.ExecuteOptions_Gen4
// Gen4GreedyOnly uses only the faster greedy planner
Gen4GreedyOnly = querypb.ExecuteOptions_Gen4Greedy
// Gen4Left2Right tries to emulate the V3 planner by only joining plans in the order they are listed in the FROM-clause
Gen4Left2Right = querypb.ExecuteOptions_Gen4Left2Right
// Gen4WithFallback first attempts to use the Gen4 planner, and if that fails, uses the V3 planner instead
Gen4WithFallback = querypb.ExecuteOptions_Gen4WithFallback
// Gen4CompareV3 executes queries on both Gen4 and V3 to compare their results.
Gen4CompareV3 = querypb.ExecuteOptions_Gen4CompareV3
)
var (
plannerVersions = []plancontext.PlannerVersion{V3, Gen4, Gen4GreedyOnly, Gen4Left2Right, Gen4WithFallback, Gen4CompareV3}
)
type (
truncater interface {
SetTruncateColumnCount(int)
}
planResult struct {
primitive engine.Primitive
tables []string
}
stmtPlanner func(sqlparser.Statement, *sqlparser.ReservedVars, plancontext.VSchema) (*planResult, error)
)
func newPlanResult(prim engine.Primitive, tablesUsed ...string) *planResult {
return &planResult{primitive: prim, tables: tablesUsed}
}
func singleTable(ks, tbl string) string {
return fmt.Sprintf("%s.%s", ks, tbl)
}
func tablesFromSemantics(semTable *semantics.SemTable) []string {
tables := make(map[string]any, len(semTable.Tables))
for _, info := range semTable.Tables {
vindexTable := info.GetVindexTable()
if vindexTable == nil {
continue
}
tables[vindexTable.String()] = nil
}
names := make([]string, 0, len(tables))
for tbl := range tables {
names = append(names, tbl)
}
sort.Strings(names)
return names
}
// TestBuilder builds a plan for a query based on the specified vschema.
// This method is only used from tests
func TestBuilder(query string, vschema plancontext.VSchema, keyspace string) (*engine.Plan, error) {
stmt, reserved, err := sqlparser.Parse2(query)
if err != nil {
return nil, err
}
result, err := sqlparser.RewriteAST(stmt, keyspace, sqlparser.SQLSelectLimitUnset, "", nil, vschema)
if err != nil {
return nil, err
}
reservedVars := sqlparser.NewReservedVars("vtg", reserved)
return BuildFromStmt(query, result.AST, reservedVars, vschema, result.BindVarNeeds, true, true)
}
// BuildFromStmt builds a plan based on the AST provided.
func BuildFromStmt(query string, stmt sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, bindVarNeeds *sqlparser.BindVarNeeds, enableOnlineDDL, enableDirectDDL bool) (*engine.Plan, error) {
planResult, err := createInstructionFor(query, stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
if err != nil {
return nil, err
}
var primitive engine.Primitive
var tablesUsed []string
if planResult != nil {
primitive = planResult.primitive
tablesUsed = planResult.tables
}
plan := &engine.Plan{
Type: sqlparser.ASTToStatementType(stmt),
Original: query,
Instructions: primitive,
BindVarNeeds: bindVarNeeds,
TablesUsed: tablesUsed,
}
return plan, nil
}
func getConfiguredPlanner(vschema plancontext.VSchema, v3planner func(string) stmtPlanner, stmt sqlparser.Statement, query string) (stmtPlanner, error) {
planner, ok := getPlannerFromQuery(stmt)
if !ok {
// if the query doesn't specify the planner, we check what the configuration is
planner = vschema.Planner()
}
switch planner {
case Gen4CompareV3:
return gen4CompareV3Planner(query), nil
case Gen4Left2Right, Gen4GreedyOnly:
return gen4Planner(query, planner), nil
case Gen4WithFallback:
fp := &fallbackPlanner{
primary: gen4Planner(query, querypb.ExecuteOptions_Gen4),
fallback: v3planner(query),
}
return fp.plan, nil
case V3:
return v3planner(query), nil
default:
// default is gen4 plan
return gen4Planner(query, Gen4), nil
}
}
// getPlannerFromQuery chooses the planner to use based on the query
// The default planner can be overridden using /*vt+ PLANNER=gen4 */
// We will also fall back on the gen4 planner if we encounter outer join,
// since there are known problems with the v3 planner and outer joins
func getPlannerFromQuery(stmt sqlparser.Statement) (version plancontext.PlannerVersion, found bool) {
version, found = getPlannerFromQueryHint(stmt)
if found {
return
}
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
join, ok := node.(*sqlparser.JoinTableExpr)
if ok {
if join.Join == sqlparser.LeftJoinType || join.Join == sqlparser.RightJoinType {
version = querypb.ExecuteOptions_Gen4
found = true
return false, nil
}
}
return true, nil
}, stmt)
return
}
func getPlannerFromQueryHint(stmt sqlparser.Statement) (plancontext.PlannerVersion, bool) {
cm, isCom := stmt.(sqlparser.Commented)
if !isCom {
return plancontext.PlannerVersion(0), false
}
d := cm.GetParsedComments().Directives()
val, ok := d.GetString(sqlparser.DirectiveQueryPlanner, "")
if !ok {
return plancontext.PlannerVersion(0), false
}
return plancontext.PlannerNameToVersion(val)
}
func buildRoutePlan(stmt sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, f func(statement sqlparser.Statement, reservedVars *sqlparser.ReservedVars, schema plancontext.VSchema) (*planResult, error)) (*planResult, error) {
if vschema.Destination() != nil {
return buildPlanForBypass(stmt, reservedVars, vschema)
}
return f(stmt, reservedVars, vschema)
}
func createInstructionFor(query string, stmt sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
switch stmt := stmt.(type) {
case *sqlparser.Select:
configuredPlanner, err := getConfiguredPlanner(vschema, buildSelectPlan, stmt, query)
if err != nil {
return nil, err
}
return buildRoutePlan(stmt, reservedVars, vschema, configuredPlanner)
case *sqlparser.Insert:
return buildRoutePlan(stmt, reservedVars, vschema, buildInsertPlan)
case *sqlparser.Update:
configuredPlanner, err := getConfiguredPlanner(vschema, buildUpdatePlan, stmt, query)
if err != nil {
return nil, err
}
return buildRoutePlan(stmt, reservedVars, vschema, configuredPlanner)
case *sqlparser.Delete:
configuredPlanner, err := getConfiguredPlanner(vschema, buildDeletePlan, stmt, query)
if err != nil {
return nil, err
}
return buildRoutePlan(stmt, reservedVars, vschema, configuredPlanner)
case *sqlparser.Union:
configuredPlanner, err := getConfiguredPlanner(vschema, buildUnionPlan, stmt, query)
if err != nil {
return nil, err
}
return buildRoutePlan(stmt, reservedVars, vschema, configuredPlanner)
case sqlparser.DDLStatement:
return buildGeneralDDLPlan(query, stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
case *sqlparser.AlterMigration:
return buildAlterMigrationPlan(query, vschema, enableOnlineDDL)
case *sqlparser.RevertMigration:
return buildRevertMigrationPlan(query, stmt, vschema, enableOnlineDDL)
case *sqlparser.ShowMigrationLogs:
return buildShowMigrationLogsPlan(query, vschema, enableOnlineDDL)
case *sqlparser.ShowThrottledApps:
return buildShowThrottledAppsPlan(query, vschema)
case *sqlparser.ShowThrottlerStatus:
return buildShowThrottlerStatusPlan(query, vschema)
case *sqlparser.AlterVschema:
return buildVSchemaDDLPlan(stmt, vschema)
case *sqlparser.Use:
return buildUsePlan(stmt)
case sqlparser.Explain:
return buildExplainPlan(stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
case *sqlparser.VExplainStmt:
return buildVExplainPlan(stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
case *sqlparser.OtherRead, *sqlparser.OtherAdmin:
return buildOtherReadAndAdmin(query, vschema)
case *sqlparser.Set:
return buildSetPlan(stmt, vschema)
case *sqlparser.Load:
return buildLoadPlan(query, vschema)
case sqlparser.DBDDLStatement:
return buildRoutePlan(stmt, reservedVars, vschema, buildDBDDLPlan)
case *sqlparser.Begin, *sqlparser.Commit, *sqlparser.Rollback, *sqlparser.Savepoint, *sqlparser.SRollback, *sqlparser.Release:
// Empty by design. Not executed by a plan
return nil, nil
case *sqlparser.Show:
return buildShowPlan(query, stmt, reservedVars, vschema)
case *sqlparser.LockTables:
return buildRoutePlan(stmt, reservedVars, vschema, buildLockPlan)
case *sqlparser.UnlockTables:
return buildRoutePlan(stmt, reservedVars, vschema, buildUnlockPlan)
case *sqlparser.Flush:
return buildFlushPlan(stmt, vschema)
case *sqlparser.CallProc:
return buildCallProcPlan(stmt, vschema)
case *sqlparser.Stream:
return buildStreamPlan(stmt, vschema)
case *sqlparser.VStream:
return buildVStreamPlan(stmt, vschema)
case *sqlparser.CommentOnly:
// There is only a comment in the input.
// This is essentially a No-op
return newPlanResult(engine.NewRowsPrimitive(nil, nil)), nil
}
return nil, vterrors.VT13001(fmt.Sprintf("unexpected statement type: %T", stmt))
}
func buildDBDDLPlan(stmt sqlparser.Statement, _ *sqlparser.ReservedVars, vschema plancontext.VSchema) (*planResult, error) {
dbDDLstmt := stmt.(sqlparser.DBDDLStatement)
ksName := dbDDLstmt.GetDatabaseName()
if ksName == "" {
ks, err := vschema.DefaultKeyspace()
if err != nil {
return nil, err
}
ksName = ks.Name
}
ksExists := vschema.KeyspaceExists(ksName)
switch dbDDL := dbDDLstmt.(type) {
case *sqlparser.DropDatabase:
if dbDDL.IfExists && !ksExists {
return newPlanResult(engine.NewRowsPrimitive(make([][]sqltypes.Value, 0), make([]*querypb.Field, 0))), nil
}
if !ksExists {
return nil, vterrors.VT05001(ksName)
}
return newPlanResult(engine.NewDBDDL(ksName, false, queryTimeout(dbDDL.Comments.Directives()))), nil
case *sqlparser.AlterDatabase:
if !ksExists {
return nil, vterrors.VT05002(ksName)
}
return nil, vterrors.VT12001("ALTER DATABASE")
case *sqlparser.CreateDatabase:
if dbDDL.IfNotExists && ksExists {
return newPlanResult(engine.NewRowsPrimitive(make([][]sqltypes.Value, 0), make([]*querypb.Field, 0))), nil
}
if !dbDDL.IfNotExists && ksExists {
return nil, vterrors.VT06001(ksName)
}
return newPlanResult(engine.NewDBDDL(ksName, true, queryTimeout(dbDDL.Comments.Directives()))), nil
}
return nil, vterrors.VT13001(fmt.Sprintf("database DDL not recognized: %s", sqlparser.String(dbDDLstmt)))
}
func buildLoadPlan(query string, vschema plancontext.VSchema) (*planResult, error) {
keyspace, err := vschema.DefaultKeyspace()
if err != nil {
return nil, err
}
destination := vschema.Destination()
if destination == nil {
if err := vschema.ErrorIfShardedF(keyspace, "LOAD", "LOAD is not supported on sharded keyspace"); err != nil {
return nil, err
}
destination = key.DestinationAnyShard{}
}
return newPlanResult(&engine.Send{
Keyspace: keyspace,
TargetDestination: destination,
Query: query,
IsDML: true,
SingleShardOnly: true,
}), nil
}
func buildVSchemaDDLPlan(stmt *sqlparser.AlterVschema, vschema plancontext.VSchema) (*planResult, error) {
_, keyspace, _, err := vschema.TargetDestination(stmt.Table.Qualifier.String())
if err != nil {
return nil, err
}
return newPlanResult(&engine.AlterVSchema{
Keyspace: keyspace,
AlterVschemaDDL: stmt,
}, singleTable(keyspace.Name, stmt.Table.Name.String())), nil
}
func buildFlushPlan(stmt *sqlparser.Flush, vschema plancontext.VSchema) (*planResult, error) {
if len(stmt.TableNames) == 0 {
return buildFlushOptions(stmt, vschema)
}
return buildFlushTables(stmt, vschema)
}
func buildFlushOptions(stmt *sqlparser.Flush, vschema plancontext.VSchema) (*planResult, error) {
dest, keyspace, _, err := vschema.TargetDestination("")
if err != nil {
return nil, err
}
if dest == nil {
dest = key.DestinationAllShards{}
}
tc := &tableCollector{}
for _, tbl := range stmt.TableNames {
tc.addASTTable(keyspace.Name, tbl)
}
return newPlanResult(&engine.Send{
Keyspace: keyspace,
TargetDestination: dest,
Query: sqlparser.String(stmt),
IsDML: false,
SingleShardOnly: false,
}, tc.getTables()...), nil
}
func buildFlushTables(stmt *sqlparser.Flush, vschema plancontext.VSchema) (*planResult, error) {
tc := &tableCollector{}
type sendDest struct {
ks *vindexes.Keyspace
dest key.Destination
}
dest := vschema.Destination()
if dest == nil {
dest = key.DestinationAllShards{}
}
tablesMap := make(map[sendDest]sqlparser.TableNames)
var keys []sendDest
for i, tab := range stmt.TableNames {
var ksTab *vindexes.Keyspace
var table *vindexes.Table
var err error
table, _, _, _, _, err = vschema.FindTableOrVindex(tab)
if err != nil {
return nil, err
}
if table == nil {
return nil, vindexes.NotFoundError{TableName: tab.Name.String()}
}
tc.addTable(table.Keyspace.Name, table.Name.String())
ksTab = table.Keyspace
stmt.TableNames[i] = sqlparser.TableName{
Name: table.Name,
}
key := sendDest{ksTab, dest}
tables, isAvail := tablesMap[key]
if !isAvail {
keys = append(keys, key)
}
tables = append(tables, stmt.TableNames[i])
tablesMap[key] = tables
}
if len(tablesMap) == 1 {
for sendDest, tables := range tablesMap {
return newPlanResult(&engine.Send{
Keyspace: sendDest.ks,
TargetDestination: sendDest.dest,
Query: sqlparser.String(newFlushStmt(stmt, tables)),
}, tc.getTables()...), nil
}
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].ks.Name < keys[j].ks.Name
})
var sources []engine.Primitive
for _, sendDest := range keys {
plan := &engine.Send{
Keyspace: sendDest.ks,
TargetDestination: sendDest.dest,
Query: sqlparser.String(newFlushStmt(stmt, tablesMap[sendDest])),
}
sources = append(sources, plan)
}
return newPlanResult(engine.NewConcatenate(sources, nil), tc.getTables()...), nil
}
type tableCollector struct {
tables map[string]any
}
func (tc *tableCollector) addTable(ks, tbl string) {
if tc.tables == nil {
tc.tables = map[string]any{}
}
tc.tables[fmt.Sprintf("%s.%s", ks, tbl)] = nil
}
func (tc *tableCollector) addASTTable(ks string, tbl sqlparser.TableName) {
tc.addTable(ks, tbl.Name.String())
}
func (tc *tableCollector) getTables() []string {
tableNames := make([]string, 0, len(tc.tables))
for tbl := range tc.tables {
tableNames = append(tableNames, tbl)
}
sort.Strings(tableNames)
return tableNames
}
func (tc *tableCollector) addVindexTable(t *vindexes.Table) {
if t == nil {
return
}
ks, tbl := "", t.Name.String()
if t.Keyspace != nil {
ks = t.Keyspace.Name
}
tc.addTable(ks, tbl)
}
func (tc *tableCollector) addAllTables(tables []string) {
if tc.tables == nil {
tc.tables = map[string]any{}
}
for _, tbl := range tables {
tc.tables[tbl] = nil
}
}
func newFlushStmt(stmt *sqlparser.Flush, tables sqlparser.TableNames) *sqlparser.Flush {
return &sqlparser.Flush{
IsLocal: stmt.IsLocal,
TableNames: tables,
WithLock: stmt.WithLock,
ForExport: stmt.ForExport,
}
}