-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
builder.go
252 lines (224 loc) · 7.46 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
/*
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 (
"strings"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
func analyzeSelect(sel *sqlparser.Select, tables map[string]*schema.Table) (plan *Plan, err error) {
plan = &Plan{
PlanID: PlanSelect,
FullQuery: GenerateLimitQuery(sel),
}
plan.Table, plan.AllTables = lookupTables(sel.From, tables)
if sel.Where != nil {
comp, ok := sel.Where.Expr.(*sqlparser.ComparisonExpr)
if ok && comp.IsImpossible() {
plan.PlanID = PlanSelectImpossible
return plan, nil
}
}
// Check if it's a NEXT VALUE statement.
if nextVal, ok := sel.SelectExprs[0].(*sqlparser.Nextval); ok {
if plan.Table == nil || plan.Table.Type != schema.Sequence {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "%s is not a sequence", sqlparser.ToString(sel.From))
}
plan.PlanID = PlanNextval
v, err := evalengine.Translate(nextVal.Expr, semantics.EmptySemTable())
if err != nil {
return nil, err
}
plan.NextCount = v
plan.FullQuery = nil
}
if hasLockFunc(sel) {
plan.PlanID = PlanSelectLockFunc
plan.NeedsReservedConn = true
}
return plan, nil
}
// analyzeUpdate code is almost identical to analyzeDelete.
func analyzeUpdate(upd *sqlparser.Update, tables map[string]*schema.Table) (plan *Plan, err error) {
plan = &Plan{
PlanID: PlanUpdate,
}
plan.Table, plan.AllTables = lookupTables(upd.TableExprs, tables)
// Store the WHERE clause as string for the hot row protection (txserializer).
if upd.Where != nil {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", upd.Where)
plan.WhereClause = buf.ParsedQuery()
}
// Situations when we pass-through:
// PassthroughDMLs flag is set.
// plan.Table==nil: it's likely a multi-table statement. MySQL doesn't allow limit clauses for multi-table dmls.
// If there's an explicit Limit.
if PassthroughDMLs || plan.Table == nil || upd.Limit != nil {
plan.FullQuery = GenerateFullQuery(upd)
return plan, nil
}
plan.PlanID = PlanUpdateLimit
upd.Limit = execLimit
plan.FullQuery = GenerateFullQuery(upd)
upd.Limit = nil
return plan, nil
}
// analyzeDelete code is almost identical to analyzeUpdate.
func analyzeDelete(del *sqlparser.Delete, tables map[string]*schema.Table) (plan *Plan, err error) {
plan = &Plan{
PlanID: PlanDelete,
}
plan.Table, plan.AllTables = lookupTables(del.TableExprs, tables)
if del.Where != nil {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", del.Where)
plan.WhereClause = buf.ParsedQuery()
}
if PassthroughDMLs || plan.Table == nil || del.Limit != nil {
plan.FullQuery = GenerateFullQuery(del)
return plan, nil
}
plan.PlanID = PlanDeleteLimit
del.Limit = execLimit
plan.FullQuery = GenerateFullQuery(del)
del.Limit = nil
return plan, nil
}
func analyzeInsert(ins *sqlparser.Insert, tables map[string]*schema.Table) (plan *Plan, err error) {
plan = &Plan{
PlanID: PlanInsert,
FullQuery: GenerateFullQuery(ins),
}
tableName := sqlparser.GetTableName(ins.Table)
plan.Table = tables[tableName.String()]
return plan, nil
}
func analyzeShow(show *sqlparser.Show, dbName string) (plan *Plan, err error) {
switch showInternal := show.Internal.(type) {
case *sqlparser.ShowBasic:
if showInternal.Command == sqlparser.Table {
// rewrite WHERE clause if it exists
// `where Tables_in_Keyspace` => `where Tables_in_DbName`
if showInternal.Filter != nil {
showTableRewrite(showInternal, dbName)
}
}
return &Plan{
PlanID: PlanShow,
FullQuery: GenerateFullQuery(show),
}, nil
case *sqlparser.ShowCreate:
if showInternal.Command == sqlparser.CreateDb && !sqlparser.SystemSchema(showInternal.Op.Name.String()) {
showInternal.Op.Name = sqlparser.NewIdentifierCS(dbName)
}
return &Plan{
PlanID: PlanShow,
FullQuery: GenerateFullQuery(show),
}, nil
}
return &Plan{PlanID: PlanOtherRead}, nil
}
func showTableRewrite(show *sqlparser.ShowBasic, dbName string) {
filter := show.Filter.Filter
if filter == nil {
return
}
_ = sqlparser.SafeRewrite(filter, nil, func(cursor *sqlparser.Cursor) bool {
switch n := cursor.Node().(type) {
case *sqlparser.ColName:
if n.Qualifier.IsEmpty() && strings.HasPrefix(n.Name.Lowered(), "tables_in_") {
cursor.Replace(sqlparser.NewColName("Tables_in_" + dbName))
}
}
return true
})
}
func analyzeSet(set *sqlparser.Set) (plan *Plan) {
return &Plan{
PlanID: PlanSet,
FullQuery: GenerateFullQuery(set),
NeedsReservedConn: true,
}
}
func lookupTables(tableExprs sqlparser.TableExprs, tables map[string]*schema.Table) (singleTable *schema.Table, allTables []*schema.Table) {
for _, tableExpr := range tableExprs {
if t := lookupSingleTable(tableExpr, tables); t != nil {
allTables = append(allTables, t)
}
}
if len(allTables) == 1 {
singleTable = allTables[0]
}
return singleTable, allTables
}
func lookupSingleTable(tableExpr sqlparser.TableExpr, tables map[string]*schema.Table) *schema.Table {
aliased, ok := tableExpr.(*sqlparser.AliasedTableExpr)
if !ok {
return nil
}
tableName := sqlparser.GetTableName(aliased.Expr)
if tableName.IsEmpty() {
return nil
}
return tables[tableName.String()]
}
func analyzeDDL(stmt sqlparser.DDLStatement, viewsEnabled bool) (*Plan, error) {
switch stmt.(type) {
case *sqlparser.AlterView, *sqlparser.DropView, *sqlparser.CreateView:
if viewsEnabled {
return analyzeViewsDDL(stmt)
}
}
// DDLs and some other statements below don't get fully parsed.
// We have to use the original query at the time of execution.
// We are in the process of changing this
var fullQuery *sqlparser.ParsedQuery
// If the query is fully parsed, then use the ast and store the fullQuery
if stmt.IsFullyParsed() {
fullQuery = GenerateFullQuery(stmt)
}
return &Plan{PlanID: PlanDDL, FullQuery: fullQuery, FullStmt: stmt, NeedsReservedConn: stmt.IsTemporary()}, nil
}
func analyzeViewsDDL(stmt sqlparser.DDLStatement) (*Plan, error) {
switch viewDDL := stmt.(type) {
case *sqlparser.CreateView:
query := mysql.InsertIntoViewsTable
if viewDDL.IsReplace {
query = mysql.ReplaceIntoViewsTable
}
insert, err := sqlparser.Parse(query)
if err != nil {
return nil, err
}
return &Plan{PlanID: PlanViewDDL, FullQuery: GenerateFullQuery(insert), FullStmt: viewDDL}, nil
case *sqlparser.AlterView:
update, err := sqlparser.Parse(mysql.UpdateViewsTable)
if err != nil {
return nil, err
}
return &Plan{PlanID: PlanViewDDL, FullQuery: GenerateFullQuery(update), FullStmt: viewDDL}, nil
case *sqlparser.DropView:
del, err := sqlparser.Parse(mysql.DeleteFromViewsTable)
if err != nil {
return nil, err
}
return &Plan{PlanID: PlanViewDDL, FullQuery: GenerateFullQuery(del), FullStmt: viewDDL}, nil
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unknown view DDL type: %T", stmt)
}