forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
221 lines (209 loc) · 5.98 KB
/
select.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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package planbuilder
import (
"errors"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/vtgate/engine"
"github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
// buildSelectPlan is the new function to build a Select plan.
func buildSelectPlan(sel *sqlparser.Select, vschema VSchema) (primitive engine.Primitive, err error) {
bindvars := getBindvars(sel)
builder, err := processSelect(sel, vschema, nil)
if err != nil {
return nil, err
}
jt := newJointab(bindvars)
err = builder.Wireup(builder, jt)
if err != nil {
return nil, err
}
return builder.Primitive(), nil
}
// getBindvars returns a map of the bind vars referenced in the statement.
func getBindvars(node sqlparser.SQLNode) map[string]struct{} {
bindvars := make(map[string]struct{})
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case sqlparser.ValArg:
bindvars[string(node[1:])] = struct{}{}
case sqlparser.ListArg:
bindvars[string(node[2:])] = struct{}{}
}
return true, nil
}, node)
return bindvars
}
// processSelect builds a primitive tree for the given query or subquery.
func processSelect(sel *sqlparser.Select, vschema VSchema, outer builder) (builder, error) {
bldr, err := processTableExprs(sel.From, vschema)
if err != nil {
return nil, err
}
if outer != nil {
bldr.Symtab().Outer = outer.Symtab()
}
if sel.Where != nil {
err = pushFilter(sel.Where.Expr, bldr, sqlparser.WhereStr)
if err != nil {
return nil, err
}
}
err = pushSelectExprs(sel, bldr)
if err != nil {
return nil, err
}
if sel.Having != nil {
err = pushFilter(sel.Having.Expr, bldr, sqlparser.HavingStr)
if err != nil {
return nil, err
}
}
err = pushOrderBy(sel.OrderBy, bldr)
if err != nil {
return nil, err
}
err = pushLimit(sel.Limit, bldr)
if err != nil {
return nil, err
}
bldr.PushMisc(sel)
return bldr, nil
}
// pushFilter identifies the target route for the specified bool expr,
// pushes it down, and updates the route info if the new constraint improves
// the primitive. This function can push to a WHERE or HAVING clause.
func pushFilter(boolExpr sqlparser.BoolExpr, bldr builder, whereType string) error {
filters := splitAndExpression(nil, boolExpr)
reorderBySubquery(filters)
for _, filter := range filters {
rb, err := findRoute(filter, bldr)
if err != nil {
return err
}
err = rb.PushFilter(filter, whereType)
if err != nil {
return err
}
}
return nil
}
// reorderBySubquery reorders the filters by pushing subqueries
// to the end. This allows the non-subquery filters to be
// pushed first because they can potentially improve the routing
// plan, which can later allow a filter containing a subquery
// to successfully merge with the corresponding route.
func reorderBySubquery(filters []sqlparser.BoolExpr) {
max := len(filters)
for i := 0; i < max; i++ {
if !hasSubquery(filters[i]) {
continue
}
saved := filters[i]
for j := i; j < len(filters)-1; j++ {
filters[j] = filters[j+1]
}
filters[len(filters)-1] = saved
max--
}
}
// pushSelectExprs identifies the target route for the
// select expressions and pushes them down.
func pushSelectExprs(sel *sqlparser.Select, bldr builder) error {
err := checkAggregates(sel, bldr)
if err != nil {
return err
}
if sel.Distinct != "" {
// We know it's a route, but this may change
// in the distant future.
bldr.(*route).MakeDistinct()
}
colsyms, err := pushSelectRoutes(sel.SelectExprs, bldr)
if err != nil {
return err
}
bldr.Symtab().Colsyms = colsyms
err = pushGroupBy(sel.GroupBy, bldr)
if err != nil {
return err
}
return nil
}
// checkAggregates returns an error if the select statement
// has aggregates that cannot be pushed down due to a complex
// plan.
func checkAggregates(sel *sqlparser.Select, bldr builder) error {
hasAggregates := false
if sel.Distinct != "" {
hasAggregates = true
} else {
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.FuncExpr:
if node.IsAggregate() {
hasAggregates = true
return false, errors.New("dummy")
}
}
return true, nil
}, sel.SelectExprs)
}
if !hasAggregates {
return nil
}
// Check if we can allow aggregates.
rb, ok := bldr.(*route)
if !ok {
return errors.New("unsupported: complex join with aggregates")
}
if rb.IsSingle() {
return nil
}
// It's a scatter rb. We can allow aggregates if there is a unique
// vindex in the select list.
for _, selectExpr := range sel.SelectExprs {
switch selectExpr := selectExpr.(type) {
case *sqlparser.NonStarExpr:
vindex := bldr.Symtab().Vindex(selectExpr.Expr, rb, true)
if vindex != nil && vindexes.IsUnique(vindex) {
return nil
}
}
}
return errors.New("unsupported: scatter with aggregates")
}
// pusheSelectRoutes is a convenience function that pushes all the select
// expressions and returns the list of colsyms generated for it.
func pushSelectRoutes(selectExprs sqlparser.SelectExprs, bldr builder) ([]*colsym, error) {
colsyms := make([]*colsym, len(selectExprs))
for i, node := range selectExprs {
switch node := node.(type) {
case *sqlparser.NonStarExpr:
rb, err := findRoute(node.Expr, bldr)
if err != nil {
return nil, err
}
colsyms[i], _, err = bldr.PushSelect(node, rb)
if err != nil {
return nil, err
}
case *sqlparser.StarExpr:
// We'll allow select * for simple routes.
rb, ok := bldr.(*route)
if !ok {
return nil, errors.New("unsupported: '*' expression in complex join")
}
// We can push without validating the reference because
// MySQL will fail if it's invalid.
colsyms[i] = rb.PushStar(node)
case sqlparser.Nextval:
// For now, this is only supported as an implicit feature
// for auto_inc in inserts.
return nil, errors.New("unsupported: NEXT VALUE construct")
}
}
return colsyms, nil
}