-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
plan.go
354 lines (318 loc) · 9.87 KB
/
plan.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
/*
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 (
"encoding/json"
"strings"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/tableacl"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
var (
execLimit = &sqlparser.Limit{Rowcount: sqlparser.NewArgument("#maxLimit")}
// PassthroughDMLs will return plans that pass-through the DMLs without changing them.
PassthroughDMLs = false
)
// _______________________________________________
// PlanType indicates a query plan type.
type PlanType int
// The following are PlanType values.
const (
PlanSelect PlanType = iota
PlanNextval
PlanSelectImpossible
PlanSelectLockFunc
PlanInsert
PlanInsertMessage
PlanUpdate
PlanUpdateLimit
PlanDelete
PlanDeleteLimit
PlanDDL
PlanSet
// PlanOtherRead is for statements like show, etc.
PlanOtherRead
// PlanOtherAdmin is for statements like repair, lock table, etc.
PlanOtherAdmin
PlanSelectStream
// PlanMessageStream is for "stream" statements.
PlanMessageStream
PlanSavepoint
PlanRelease
PlanSRollback
PlanShow
// PlanLoad is for Load data statements
PlanLoad
// PlanFlush is for FLUSH statements
PlanFlush
PlanLockTables
PlanUnlockTables
PlanCallProc
PlanAlterMigration
PlanRevertMigration
PlanShowMigrationLogs
PlanShowThrottledApps
PlanShowThrottlerStatus
PlanViewDDL
NumPlans
)
// Must exactly match order of plan constants.
var planName = []string{
"Select",
"Nextval",
"SelectImpossible",
"SelectLockFunc",
"Insert",
"InsertMessage",
"Update",
"UpdateLimit",
"Delete",
"DeleteLimit",
"DDL",
"Set",
"OtherRead",
"OtherAdmin",
"SelectStream",
"MessageStream",
"Savepoint",
"Release",
"RollbackSavepoint",
"Show",
"Load",
"Flush",
"LockTables",
"UnlockTables",
"CallProcedure",
"AlterMigration",
"RevertMigration",
"ShowMigrationLogs",
"ShowThrottledApps",
"ShowThrottlerStatus",
"ViewDDL",
}
func (pt PlanType) String() string {
if pt < 0 || pt >= NumPlans {
return ""
}
return planName[pt]
}
// PlanByName find a PlanType by its string name.
func PlanByName(s string) (pt PlanType, ok bool) {
for i, v := range planName {
if v == s {
return PlanType(i), true
}
}
return NumPlans, false
}
// PlanByNameIC finds a plan type by its string name without case sensitivity
func PlanByNameIC(s string) (pt PlanType, ok bool) {
for i, v := range planName {
if strings.EqualFold(v, s) {
return PlanType(i), true
}
}
return NumPlans, false
}
// MarshalJSON returns a json string for PlanType.
func (pt PlanType) MarshalJSON() ([]byte, error) {
return json.Marshal(pt.String())
}
// _______________________________________________
// Plan contains the parameters for executing a request.
type Plan struct {
PlanID PlanType
// When the query indicates a single table
Table *schema.Table
// SELECT, UPDATE, DELETE statements may list multiple tables
AllTables []*schema.Table
// Permissions stores the permissions for the tables accessed in the query.
Permissions []Permission
// FullQuery will be set for all plans.
FullQuery *sqlparser.ParsedQuery
// NextCount stores the count for "select next".
NextCount evalengine.Expr
// WhereClause is set for DMLs. It is used by the hot row protection
// to serialize e.g. UPDATEs going to the same row.
WhereClause *sqlparser.ParsedQuery
// FullStmt can be used when the query does not operate on tables
FullStmt sqlparser.Statement
// NeedsReservedConn indicates at a reserved connection is needed to execute this plan
NeedsReservedConn bool
}
// TableName returns the table name for the plan.
func (plan *Plan) TableName() sqlparser.IdentifierCS {
var tableName sqlparser.IdentifierCS
if plan.Table != nil {
tableName = plan.Table.Name
}
return tableName
}
// TableNames returns the table names for all tables in the plan.
func (plan *Plan) TableNames() (names []string) {
if len(plan.AllTables) == 0 {
tableName := plan.TableName()
return []string{tableName.String()}
}
for _, table := range plan.AllTables {
names = append(names, table.Name.String())
}
return names
}
// Build builds a plan based on the schema.
func Build(statement sqlparser.Statement, tables map[string]*schema.Table, dbName string, viewsEnabled bool) (plan *Plan, err error) {
switch stmt := statement.(type) {
case *sqlparser.Union:
plan, err = &Plan{
PlanID: PlanSelect,
FullQuery: GenerateLimitQuery(stmt),
}, nil
case *sqlparser.Select:
plan, err = analyzeSelect(stmt, tables)
case *sqlparser.Insert:
plan, err = analyzeInsert(stmt, tables)
case *sqlparser.Update:
plan, err = analyzeUpdate(stmt, tables)
case *sqlparser.Delete:
plan, err = analyzeDelete(stmt, tables)
case *sqlparser.Set:
plan, err = analyzeSet(stmt), nil
case sqlparser.DDLStatement:
plan, err = analyzeDDL(stmt, viewsEnabled)
case *sqlparser.AlterMigration:
plan, err = &Plan{PlanID: PlanAlterMigration, FullStmt: stmt}, nil
case *sqlparser.RevertMigration:
plan, err = &Plan{PlanID: PlanRevertMigration, FullStmt: stmt}, nil
case *sqlparser.ShowMigrationLogs:
plan, err = &Plan{PlanID: PlanShowMigrationLogs, FullStmt: stmt}, nil
case *sqlparser.ShowThrottledApps:
plan, err = &Plan{PlanID: PlanShowThrottledApps, FullStmt: stmt}, nil
case *sqlparser.ShowThrottlerStatus:
plan, err = &Plan{PlanID: PlanShowThrottlerStatus, FullStmt: stmt}, nil
case *sqlparser.Show:
plan, err = analyzeShow(stmt, dbName)
case *sqlparser.OtherRead, sqlparser.Explain:
plan, err = &Plan{PlanID: PlanOtherRead}, nil
case *sqlparser.OtherAdmin:
plan, err = &Plan{PlanID: PlanOtherAdmin}, nil
case *sqlparser.Savepoint:
plan, err = &Plan{PlanID: PlanSavepoint}, nil
case *sqlparser.Release:
plan, err = &Plan{PlanID: PlanRelease}, nil
case *sqlparser.SRollback:
plan, err = &Plan{PlanID: PlanSRollback}, nil
case *sqlparser.Load:
plan, err = &Plan{PlanID: PlanLoad}, nil
case *sqlparser.Flush:
plan, err = &Plan{PlanID: PlanFlush, FullQuery: GenerateFullQuery(stmt)}, nil
case *sqlparser.CallProc:
plan, err = &Plan{PlanID: PlanCallProc, FullQuery: GenerateFullQuery(stmt)}, nil
default:
return nil, vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "invalid SQL")
}
if err != nil {
return nil, err
}
plan.Permissions = BuildPermissions(statement)
return plan, nil
}
// BuildStreaming builds a streaming plan based on the schema.
func BuildStreaming(sql string, tables map[string]*schema.Table) (*Plan, error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan := &Plan{
PlanID: PlanSelectStream,
FullQuery: GenerateFullQuery(statement),
Permissions: BuildPermissions(statement),
}
switch stmt := statement.(type) {
case *sqlparser.Select:
if hasLockFunc(stmt) {
plan.NeedsReservedConn = true
}
plan.Table, plan.AllTables = lookupTables(stmt.From, tables)
case *sqlparser.OtherRead, *sqlparser.Show, *sqlparser.Union, *sqlparser.CallProc, sqlparser.Explain:
// pass
default:
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "%s not allowed for streaming", sqlparser.ASTToStatementType(statement))
}
return plan, nil
}
// BuildMessageStreaming builds a plan for message streaming.
func BuildMessageStreaming(name string, tables map[string]*schema.Table) (*Plan, error) {
plan := &Plan{
PlanID: PlanMessageStream,
Table: tables[name],
}
if plan.Table == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "table %s not found in schema", name)
}
if plan.Table.Type != schema.Message {
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "'%s' is not a message table", name)
}
plan.Permissions = []Permission{{
TableName: plan.Table.Name.String(),
Role: tableacl.WRITER,
}}
return plan, nil
}
// hasLockFunc looks for get_lock function in the select query.
// If it is present then it returns true otherwise false
func hasLockFunc(sel *sqlparser.Select) bool {
var found bool
_ = sqlparser.Walk(func(in sqlparser.SQLNode) (bool, error) {
lFunc, isLFunc := in.(*sqlparser.LockingFunc)
if !isLFunc {
return true, nil
}
if lFunc.Type == sqlparser.GetLock {
found = true
return false, nil
}
return true, nil
}, sel.SelectExprs)
return found
}
// BuildSettingQuery builds a query for system settings.
func BuildSettingQuery(settings []string) (query string, resetQuery string, err error) {
if len(settings) == 0 {
return "", "", vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG]: plan called for empty system settings")
}
var setExprs sqlparser.SetExprs
var resetSetExprs sqlparser.SetExprs
lDefault := sqlparser.NewStrLiteral("default")
for _, setting := range settings {
stmt, err := sqlparser.Parse(setting)
if err != nil {
return "", "", vterrors.Wrapf(err, "[BUG]: failed to parse system setting: %s", setting)
}
set, ok := stmt.(*sqlparser.Set)
if !ok {
return "", "", vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG]: invalid set statement: %s", setting)
}
setExprs = append(setExprs, set.Exprs...)
for _, sExpr := range set.Exprs {
sysVar := sExpr.Var
if sysVar.Scope != sqlparser.SessionScope {
return "", "", vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG]: session scope expected, got: %s", sysVar.Scope.ToString())
}
resetSetExprs = append(resetSetExprs, &sqlparser.SetExpr{Var: sysVar, Expr: lDefault})
}
}
return sqlparser.String(&sqlparser.Set{Exprs: setExprs}), sqlparser.String(&sqlparser.Set{Exprs: resetSetExprs}), nil
}