-
Notifications
You must be signed in to change notification settings - Fork 2k
/
from.go
388 lines (353 loc) · 13.4 KB
/
from.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
/*
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 (
"errors"
"fmt"
"strings"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/vtgate/evalengine"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// This file has functions to analyze the FROM clause.
// processDMLTable analyzes the FROM clause for DMLs and returns a route.
func (pb *primitiveBuilder) processDMLTable(tableExprs sqlparser.TableExprs, reservedVars *sqlparser.ReservedVars, where sqlparser.Expr) (*route, error) {
if err := pb.processTableExprs(tableExprs, reservedVars, where); err != nil {
return nil, err
}
rb, ok := pb.plan.(*route)
if !ok {
return nil, errors.New("unsupported: multi-shard or vindex write statement")
}
for _, sub := range rb.substitutions {
*sub.oldExpr = *sub.newExpr
}
return rb, nil
}
// processTableExprs analyzes the FROM clause. It produces a logicalPlan
// with all the routes identified.
func (pb *primitiveBuilder) processTableExprs(tableExprs sqlparser.TableExprs, reservedVars *sqlparser.ReservedVars, where sqlparser.Expr) error {
if len(tableExprs) == 1 {
return pb.processTableExpr(tableExprs[0], reservedVars, where)
}
if err := pb.processTableExpr(tableExprs[0], reservedVars, where); err != nil {
return err
}
rpb := newPrimitiveBuilder(pb.vschema, pb.jt)
if err := rpb.processTableExprs(tableExprs[1:], reservedVars, where); err != nil {
return err
}
return pb.join(rpb, nil, reservedVars, where)
}
// processTableExpr produces a logicalPlan subtree for the given TableExpr.
func (pb *primitiveBuilder) processTableExpr(tableExpr sqlparser.TableExpr, reservedVars *sqlparser.ReservedVars, where sqlparser.Expr) error {
switch tableExpr := tableExpr.(type) {
case *sqlparser.AliasedTableExpr:
return pb.processAliasedTable(tableExpr, reservedVars)
case *sqlparser.ParenTableExpr:
err := pb.processTableExprs(tableExpr.Exprs, reservedVars, where)
// If it's a route, preserve the parenthesis so things
// don't associate differently when more things are pushed
// into it. FROM a, (b, c) should not become FROM a, b, c.
if rb, ok := pb.plan.(*route); ok {
sel, ok := rb.Select.(*sqlparser.Select)
if !ok {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", sqlparser.String(rb.Select))
}
sel.From = sqlparser.TableExprs{&sqlparser.ParenTableExpr{Exprs: sel.From}}
}
return err
case *sqlparser.JoinTableExpr:
return pb.processJoin(tableExpr, reservedVars, where)
case *sqlparser.JSONTableExpr:
return vterrors.New(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: json_table expressions")
}
return fmt.Errorf("BUG: unexpected table expression type: %T", tableExpr)
}
// processAliasedTable produces a logicalPlan subtree for the given AliasedTableExpr.
// If the expression is a subquery, then the primitive will create a table
// for it in the symtab. If the subquery is a route, then we build a route
// primitive with the subquery in the From clause, because a route is more
// versatile than a subquery. If a subquery becomes a route, then any result
// columns that represent underlying vindex columns are also exposed as
// vindex columns.
func (pb *primitiveBuilder) processAliasedTable(tableExpr *sqlparser.AliasedTableExpr, reservedVars *sqlparser.ReservedVars) error {
if tableExpr.Columns != nil {
return vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: column aliases in derived table")
}
switch expr := tableExpr.Expr.(type) {
case sqlparser.TableName:
return pb.buildTablePrimitive(tableExpr, expr)
case *sqlparser.DerivedTable:
if expr.Lateral {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "unsupported: lateral derived tables")
}
spb := newPrimitiveBuilder(pb.vschema, pb.jt)
switch stmt := expr.Select.(type) {
case *sqlparser.Select:
if err := spb.processSelect(stmt, reservedVars, nil, ""); err != nil {
return err
}
case *sqlparser.Union:
if err := spb.processUnion(stmt, reservedVars, nil); err != nil {
return err
}
default:
return fmt.Errorf("BUG: unexpected SELECT type: %T", stmt)
}
subroute, ok := spb.plan.(*route)
if !ok {
var err error
pb.plan, pb.st, err = newSimpleProjection(tableExpr.As, spb.plan)
if err != nil {
return err
}
pb.plan.Reorder(0)
return nil
}
// Since a route is more versatile than a subquery, we
// build a route primitive that has the subquery in its
// FROM clause. This allows for other constructs to be
// later pushed into it.
rb, st := newRoute(&sqlparser.Select{From: []sqlparser.TableExpr{tableExpr}})
rb.substitutions = subroute.substitutions
rb.condition = subroute.condition
rb.eroute = subroute.eroute
subroute.Redirect = rb
// The subquery needs to be represented as a new logical table in the symtab.
// The new route will inherit the routeOptions of the underlying subquery.
// For this, we first build new vschema tables based on the columns returned
// by the subquery, and re-expose possible vindexes. When added to the symtab,
// a new set of column references will be generated against the new tables,
// and those vindex maps will be returned. They have to replace the old vindex
// maps of the inherited route options.
vschemaTable := &vindexes.Table{
Keyspace: subroute.eroute.Keyspace,
}
for _, rc := range subroute.ResultColumns() {
if rc.column.vindex == nil {
continue
}
// Check if a colvindex of the same name already exists.
// Dups are not allowed in subqueries in this situation.
for _, colVindex := range vschemaTable.ColumnVindexes {
if colVindex.Columns[0].Equal(rc.alias) {
return fmt.Errorf("duplicate column aliases: %v", rc.alias)
}
}
vschemaTable.ColumnVindexes = append(vschemaTable.ColumnVindexes, &vindexes.ColumnVindex{
Columns: []sqlparser.ColIdent{rc.alias},
Vindex: rc.column.vindex,
})
}
if err := st.AddVSchemaTable(sqlparser.TableName{Name: tableExpr.As}, vschemaTable, rb); err != nil {
return err
}
pb.plan, pb.st = rb, st
return nil
}
return fmt.Errorf("BUG: unexpected table expression type: %T", tableExpr.Expr)
}
// buildTablePrimitive builds a primitive based on the table name.
func (pb *primitiveBuilder) buildTablePrimitive(tableExpr *sqlparser.AliasedTableExpr, tableName sqlparser.TableName) error {
alias := tableName
if !tableExpr.As.IsEmpty() {
alias = sqlparser.TableName{Name: tableExpr.As}
}
sel := &sqlparser.Select{From: sqlparser.TableExprs([]sqlparser.TableExpr{tableExpr})}
if sqlparser.SystemSchema(tableName.Qualifier.String()) {
ks, err := pb.vschema.AnyKeyspace()
if err != nil {
return err
}
rb, st := newRoute(sel)
rb.eroute = engine.NewSimpleRoute(engine.DBA, ks)
rb.eroute.TableName = sqlparser.String(tableName)
pb.plan, pb.st = rb, st
// Add the table to symtab
return st.AddTable(&table{
alias: alias,
origin: rb,
})
}
vschemaTable, vindex, _, destTableType, destTarget, err := pb.vschema.FindTableOrVindex(tableName)
if err != nil {
return err
}
if vindex != nil {
single, ok := vindex.(vindexes.SingleColumn)
if !ok {
return fmt.Errorf("multi-column vindexes not supported")
}
pb.plan, pb.st = newVindexFunc(alias, single)
return nil
}
rb, st := newRoute(sel)
pb.plan, pb.st = rb, st
if err := st.AddVSchemaTable(alias, vschemaTable, rb); err != nil {
return err
}
sub := &tableSubstitution{
oldExpr: tableExpr,
}
if tableExpr.As.IsEmpty() {
if tableName.Name != vschemaTable.Name {
// Table name does not match. Change and alias it to old name.
sub.newExpr = &sqlparser.AliasedTableExpr{
Expr: sqlparser.TableName{Name: vschemaTable.Name},
As: tableName.Name,
}
}
} else {
// Table is already aliased.
if tableName.Name != vschemaTable.Name {
// Table name does not match. Change it and reuse existing alias.
sub.newExpr = &sqlparser.AliasedTableExpr{
Expr: sqlparser.TableName{Name: vschemaTable.Name},
As: tableExpr.As,
}
}
}
if sub != nil && sub.newExpr != nil {
rb.substitutions = []*tableSubstitution{sub}
}
var eroute *engine.Route
switch {
case vschemaTable.Type == vindexes.TypeSequence:
eroute = engine.NewSimpleRoute(engine.Next, vschemaTable.Keyspace)
case vschemaTable.Type == vindexes.TypeReference:
eroute = engine.NewSimpleRoute(engine.Reference, vschemaTable.Keyspace)
case !vschemaTable.Keyspace.Sharded:
eroute = engine.NewSimpleRoute(engine.Unsharded, vschemaTable.Keyspace)
case vschemaTable.Pinned == nil:
eroute = engine.NewSimpleRoute(engine.Scatter, vschemaTable.Keyspace)
eroute.TargetDestination = destTarget
eroute.TargetTabletType = destTableType
default:
// Pinned tables have their keyspace ids already assigned.
// Use the Binary vindex, which is the identity function
// for keyspace id.
eroute = engine.NewSimpleRoute(engine.EqualUnique, vschemaTable.Keyspace)
vindex, _ = vindexes.NewBinary("binary", nil)
eroute.Vindex = vindex
lit := evalengine.NewLiteralString(vschemaTable.Pinned, collations.TypedCollation{})
eroute.Values = []evalengine.Expr{lit}
}
eroute.TableName = sqlparser.String(vschemaTable.Name)
rb.eroute = eroute
return nil
}
// processJoin produces a logicalPlan subtree for the given Join.
// If the left and right nodes can be part of the same route,
// then it's a route. Otherwise, it's a join.
func (pb *primitiveBuilder) processJoin(ajoin *sqlparser.JoinTableExpr, reservedVars *sqlparser.ReservedVars, where sqlparser.Expr) error {
switch ajoin.Join {
case sqlparser.NormalJoinType, sqlparser.StraightJoinType, sqlparser.LeftJoinType:
case sqlparser.RightJoinType:
convertToLeftJoin(ajoin)
default:
return fmt.Errorf("unsupported: %s", ajoin.Join.ToString())
}
if err := pb.processTableExpr(ajoin.LeftExpr, reservedVars, where); err != nil {
return err
}
rpb := newPrimitiveBuilder(pb.vschema, pb.jt)
if err := rpb.processTableExpr(ajoin.RightExpr, reservedVars, where); err != nil {
return err
}
return pb.join(rpb, ajoin, reservedVars, where)
}
// convertToLeftJoin converts a right join into a left join.
func convertToLeftJoin(ajoin *sqlparser.JoinTableExpr) {
newRHS := ajoin.LeftExpr
// If the LHS is a join, we have to parenthesize it.
// Otherwise, it can be used as is.
if _, ok := newRHS.(*sqlparser.JoinTableExpr); ok {
newRHS = &sqlparser.ParenTableExpr{
Exprs: sqlparser.TableExprs{newRHS},
}
}
ajoin.LeftExpr, ajoin.RightExpr = ajoin.RightExpr, newRHS
ajoin.Join = sqlparser.LeftJoinType
}
func (pb *primitiveBuilder) join(rpb *primitiveBuilder, ajoin *sqlparser.JoinTableExpr, reservedVars *sqlparser.ReservedVars, where sqlparser.Expr) error {
// Merge the symbol tables. In the case of a left join, we have to
// ideally create new symbols that originate from the join primitive.
// However, this is not worth it for now, because the Push functions
// verify that only valid constructs are passed through in case of left join.
err := pb.st.Merge(rpb.st)
if err != nil {
return err
}
lRoute, leftIsRoute := pb.plan.(*route)
rRoute, rightIsRoute := rpb.plan.(*route)
if !leftIsRoute || !rightIsRoute {
return newJoin(pb, rpb, ajoin, reservedVars)
}
// Try merging the routes.
if !lRoute.JoinCanMerge(pb, rRoute, ajoin, where) {
return newJoin(pb, rpb, ajoin, reservedVars)
}
if lRoute.eroute.Opcode == engine.Reference {
// Swap the conditions & eroutes, and then merge.
lRoute.condition, rRoute.condition = rRoute.condition, lRoute.condition
lRoute.eroute, rRoute.eroute = rRoute.eroute, lRoute.eroute
}
lRoute.substitutions = append(lRoute.substitutions, rRoute.substitutions...)
rRoute.Redirect = lRoute
// Merge the AST.
sel, ok := lRoute.Select.(*sqlparser.Select)
if !ok {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", sqlparser.String(lRoute.Select))
}
if ajoin == nil {
rhsSel, ok := rRoute.Select.(*sqlparser.Select)
if !ok {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", sqlparser.String(rRoute.Select))
}
sel.From = append(sel.From, rhsSel.From...)
} else {
sel.From = sqlparser.TableExprs{ajoin}
}
// join table name
if lRoute.eroute.TableName != rRoute.eroute.TableName {
lRoute.eroute.TableName = strings.Join([]string{lRoute.eroute.TableName, rRoute.eroute.TableName}, ", ")
}
// join sysTableNames
for tableName, expr := range rRoute.eroute.SysTableTableName {
_, ok := lRoute.eroute.SysTableTableName[tableName]
if !ok {
lRoute.eroute.SysTableTableName[tableName] = expr
}
}
// Since the routes have merged, set st.singleRoute to point at
// the merged route.
pb.st.singleRoute = lRoute
if ajoin == nil {
return nil
}
pullouts, _, expr, err := pb.findOrigin(ajoin.Condition.On, reservedVars)
if err != nil {
return err
}
ajoin.Condition.On = expr
pb.addPullouts(pullouts)
for _, filter := range sqlparser.SplitAndExpression(nil, ajoin.Condition.On) {
lRoute.UpdatePlan(pb, filter)
}
return nil
}