forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
219 lines (198 loc) · 5.53 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
// Copyright 2014, 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 (
"fmt"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
)
func analyzeSelect(sel *sqlparser.Select, getTable TableGetter) (plan *ExecPlan, err error) {
// Default plan
plan = &ExecPlan{
PlanId: PLAN_PASS_SELECT,
FieldQuery: GenerateFieldQuery(sel),
FullQuery: GenerateSelectLimitQuery(sel),
}
// There are bind variables in the SELECT list
if plan.FieldQuery == nil {
plan.Reason = REASON_SELECT_LIST
return plan, nil
}
if sel.Distinct != "" || sel.GroupBy != nil || sel.Having != nil {
plan.Reason = REASON_SELECT
return plan, nil
}
// from
tableName, hasHints := analyzeFrom(sel.From)
if tableName == "" {
plan.Reason = REASON_TABLE
return plan, nil
}
tableInfo, err := plan.setTableInfo(tableName, getTable)
if err != nil {
return nil, err
}
// Don't improve the plan if the select is locking the row
if sel.Lock != "" {
plan.Reason = REASON_LOCK
return plan, nil
}
// Further improvements possible only if table is row-cached
if tableInfo.CacheType == schema.CACHE_NONE || tableInfo.CacheType == schema.CACHE_W {
plan.Reason = REASON_NOCACHE
return plan, nil
}
// Select expressions
selects, err := analyzeSelectExprs(sel.SelectExprs, tableInfo)
if err != nil {
return nil, err
}
if selects == nil {
plan.Reason = REASON_SELECT_LIST
return plan, nil
}
plan.ColumnNumbers = selects
// where
conditions := analyzeWhere(sel.Where)
if conditions == nil {
plan.Reason = REASON_WHERE
return plan, nil
}
// order
if sel.OrderBy != nil {
plan.Reason = REASON_ORDER
return plan, nil
}
// This check should never fail because we only cache tables with primary keys.
if len(tableInfo.Indexes) == 0 || tableInfo.Indexes[0].Name != "PRIMARY" {
panic("unexpected")
}
// Attempt PK match only if there's no limit clause
if sel.Limit == nil {
planId, pkValues, err := getSelectPKValues(conditions, tableInfo.Indexes[0])
if err != nil {
return nil, err
}
switch planId {
case PLAN_PK_EQUAL:
plan.PlanId = PLAN_PK_EQUAL
plan.OuterQuery = GenerateEqualOuterQuery(sel, tableInfo)
plan.PKValues = pkValues
return plan, nil
case PLAN_PK_IN:
plan.PlanId = PLAN_PK_IN
plan.OuterQuery = GenerateInOuterQuery(sel, tableInfo)
plan.PKValues = pkValues
return plan, nil
}
}
if len(tableInfo.Indexes[0].Columns) != 1 {
plan.Reason = REASON_COMPOSITE_PK
return plan, nil
}
// TODO: Analyze hints to improve plan.
if hasHints {
plan.Reason = REASON_HAS_HINTS
return plan, nil
}
plan.IndexUsed = getIndexMatch(conditions, tableInfo.Indexes)
if plan.IndexUsed == "" {
plan.Reason = REASON_NOINDEX_MATCH
return plan, nil
}
if plan.IndexUsed == "PRIMARY" {
plan.Reason = REASON_PKINDEX
return plan, nil
}
// TODO: We can further optimize. Change this to pass-through if select list matches all columns in index.
plan.PlanId = PLAN_SELECT_SUBQUERY
plan.OuterQuery = GenerateInOuterQuery(sel, tableInfo)
plan.Subquery = GenerateSelectSubquery(sel, tableInfo, plan.IndexUsed)
return plan, nil
}
func analyzeSelectExprs(exprs sqlparser.SelectExprs, table *schema.Table) (selects []int, err error) {
selects = make([]int, 0, len(exprs))
for _, expr := range exprs {
switch expr := expr.(type) {
case *sqlparser.StarExpr:
// Append all columns.
for colIndex := range table.Columns {
selects = append(selects, colIndex)
}
case *sqlparser.NonStarExpr:
name := sqlparser.GetColName(expr.Expr)
if name == "" {
// Not a simple column name.
return nil, nil
}
colIndex := table.FindColumn(name)
if colIndex == -1 {
return nil, fmt.Errorf("column %s not found in table %s", name, table.Name)
}
selects = append(selects, colIndex)
default:
panic("unreachable")
}
}
return selects, nil
}
func analyzeFrom(tableExprs sqlparser.TableExprs) (tablename string, hasHints bool) {
if len(tableExprs) > 1 {
return "", false
}
node, ok := tableExprs[0].(*sqlparser.AliasedTableExpr)
if !ok {
return "", false
}
return sqlparser.GetTableName(node.Expr), node.Hints != nil
}
func analyzeWhere(node *sqlparser.Where) (conditions []sqlparser.BoolExpr) {
if node == nil {
return nil
}
return analyzeBoolean(node.Expr)
}
func analyzeBoolean(node sqlparser.BoolExpr) (conditions []sqlparser.BoolExpr) {
switch node := node.(type) {
case *sqlparser.AndExpr:
left := analyzeBoolean(node.Left)
right := analyzeBoolean(node.Right)
if left == nil || right == nil {
return nil
}
if sqlparser.HasINClause(left) && sqlparser.HasINClause(right) {
return nil
}
return append(left, right...)
case *sqlparser.ParenBoolExpr:
return analyzeBoolean(node.Expr)
case *sqlparser.ComparisonExpr:
switch {
case sqlparser.StringIn(
node.Operator,
sqlparser.AST_EQ,
sqlparser.AST_LT,
sqlparser.AST_GT,
sqlparser.AST_LE,
sqlparser.AST_GE,
sqlparser.AST_NSE,
sqlparser.AST_LIKE):
if sqlparser.IsColName(node.Left) && sqlparser.IsValue(node.Right) {
return []sqlparser.BoolExpr{node}
}
case node.Operator == sqlparser.AST_IN:
if sqlparser.IsColName(node.Left) && sqlparser.IsSimpleTuple(node.Right) {
return []sqlparser.BoolExpr{node}
}
}
case *sqlparser.RangeCond:
if node.Operator != sqlparser.AST_BETWEEN {
return nil
}
if sqlparser.IsColName(node.Left) && sqlparser.IsValue(node.From) && sqlparser.IsValue(node.To) {
return []sqlparser.BoolExpr{node}
}
}
return nil
}