-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
semantic_state.go
267 lines (223 loc) · 8.76 KB
/
semantic_state.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
/*
Copyright 2020 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 semantics
import (
"vitess.io/vitess/go/vt/key"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/sqlparser"
)
type (
// TableInfo contains information about tables
TableInfo interface {
// Name returns the table name
Name() (sqlparser.TableName, error)
// GetVindexTable returns the vschema version of this TableInfo
GetVindexTable() *vindexes.Table
// IsInfSchema returns true if this table is information_schema
IsInfSchema() bool
// matches returns true if the provided table name matches this TableInfo
matches(name sqlparser.TableName) bool
// authoritative is true if we have exhaustive column information
authoritative() bool
// getExpr returns the AST struct behind this table
getExpr() *sqlparser.AliasedTableExpr
// getColumns returns the known column information for this table
getColumns() []ColumnInfo
dependencies(colName string, org originable) (dependencies, error)
getExprFor(s string) (sqlparser.Expr, error)
getTableSet(org originable) TableSet
}
// ColumnInfo contains information about columns
ColumnInfo struct {
Name string
Type querypb.Type
}
// ExprDependencies stores the tables that an expression depends on as a map
ExprDependencies map[sqlparser.Expr]TableSet
// SemTable contains semantic analysis information about the query.
SemTable struct {
Tables []TableInfo
// ProjectionErr stores the error that we got during the semantic analysis of the SelectExprs.
// This is only a real error if we are unable to plan the query as a single route
ProjectionErr error
// Recursive contains the dependencies from the expression to the actual tables
// in the query (i.e. not including derived tables). If an expression is a column on a derived table,
// this map will contain the accumulated dependencies for the column expression inside the derived table
Recursive ExprDependencies
// Direct keeps information about the closest dependency for an expression.
// It does not recurse inside derived tables and the like to find the original dependencies
Direct ExprDependencies
exprTypes map[sqlparser.Expr]querypb.Type
selectScope map[*sqlparser.Select]*scope
Comments sqlparser.Comments
SubqueryMap map[*sqlparser.Select][]*subquery
SubqueryRef map[*sqlparser.Subquery]*subquery
// ColumnEqualities is used to enable transitive closures
// if a == b and b == c then a == c
ColumnEqualities map[columnName][]sqlparser.Expr
}
columnName struct {
Table TableSet
ColumnName string
}
subquery struct {
ArgName string
HasValues string
SubQuery *sqlparser.Subquery
OpCode engine.PulloutOpcode
// ExprsNeedReplace list all the expressions that, if the subquery is later rewritten, need to
// be removed and replaced by ReplaceBy.
ExprsNeedReplace []sqlparser.Expr
ReplaceBy sqlparser.Expr
}
// SchemaInformation is used tp provide table information from Vschema.
SchemaInformation interface {
FindTableOrVindex(tablename sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error)
}
)
var (
// ErrMultipleTables refers to an error happening when something should be used only for single tables
ErrMultipleTables = vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] should only be used for single tables")
)
// CopyDependencies copies the dependencies from one expression into the other
func (st *SemTable) CopyDependencies(from, to sqlparser.Expr) {
st.Recursive[to] = st.RecursiveDeps(from)
st.Direct[to] = st.DirectDeps(from)
}
// NewSemTable creates a new empty SemTable
func NewSemTable() *SemTable {
return &SemTable{Recursive: map[sqlparser.Expr]TableSet{}, ColumnEqualities: map[columnName][]sqlparser.Expr{}}
}
// TableSetFor returns the bitmask for this particular table
func (st *SemTable) TableSetFor(t *sqlparser.AliasedTableExpr) TableSet {
for idx, t2 := range st.Tables {
if t == t2.getExpr() {
return SingleTableSet(idx)
}
}
return TableSet{}
}
// TableInfoFor returns the table info for the table set. It should contains only single table.
func (st *SemTable) TableInfoFor(id TableSet) (TableInfo, error) {
offset := id.TableOffset()
if offset < 0 {
return nil, ErrMultipleTables
}
return st.Tables[offset], nil
}
// RecursiveDeps return the table dependencies of the expression.
func (st *SemTable) RecursiveDeps(expr sqlparser.Expr) TableSet {
return st.Recursive.Dependencies(expr)
}
// DirectDeps return the table dependencies of the expression.
func (st *SemTable) DirectDeps(expr sqlparser.Expr) TableSet {
return st.Direct.Dependencies(expr)
}
// AddColumnEquality adds a relation of the given colName to the ColumnEqualities map
func (st *SemTable) AddColumnEquality(colName *sqlparser.ColName, expr sqlparser.Expr) {
ts := st.Direct.Dependencies(colName)
columnName := columnName{
Table: ts,
ColumnName: colName.Name.String(),
}
elem := st.ColumnEqualities[columnName]
elem = append(elem, expr)
st.ColumnEqualities[columnName] = elem
}
// GetExprAndEqualities returns a slice containing the given expression, and it's known equalities if any
func (st *SemTable) GetExprAndEqualities(expr sqlparser.Expr) []sqlparser.Expr {
result := []sqlparser.Expr{expr}
switch expr := expr.(type) {
case *sqlparser.ColName:
table := st.DirectDeps(expr)
k := columnName{Table: table, ColumnName: expr.Name.String()}
result = append(result, st.ColumnEqualities[k]...)
}
return result
}
// TableInfoForExpr returns the table info of the table that this expression depends on.
// Careful: this only works for expressions that have a single table dependency
func (st *SemTable) TableInfoForExpr(expr sqlparser.Expr) (TableInfo, error) {
return st.TableInfoFor(st.Direct.Dependencies(expr))
}
// GetSelectTables returns the table in the select.
func (st *SemTable) GetSelectTables(node *sqlparser.Select) []TableInfo {
scope := st.selectScope[node]
return scope.tables
}
// AddExprs adds new select exprs to the SemTable.
func (st *SemTable) AddExprs(tbl *sqlparser.AliasedTableExpr, cols sqlparser.SelectExprs) {
tableSet := st.TableSetFor(tbl)
for _, col := range cols {
st.Recursive[col.(*sqlparser.AliasedExpr).Expr] = tableSet
}
}
// TypeFor returns the type of expressions in the query
func (st *SemTable) TypeFor(e sqlparser.Expr) *querypb.Type {
typ, found := st.exprTypes[e]
if found {
return &typ
}
return nil
}
// Dependencies return the table dependencies of the expression. This method finds table dependencies recursively
func (d ExprDependencies) Dependencies(expr sqlparser.Expr) TableSet {
deps, found := d[expr]
if found {
return deps
}
// During the original semantic analysis, all ColName:s were found and bound the the corresponding tables
// Here, we'll walk the expression tree and look to see if we can found any sub-expressions
// that have already set dependencies.
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
expr, ok := node.(sqlparser.Expr)
if !ok || !validAsMapKey(expr) {
// if this is not an expression, or it is an expression we can't use as a map-key,
// just carry on down the tree
return true, nil
}
set, found := d[expr]
if found {
deps.MergeInPlace(set)
}
// if we found a cached value, there is no need to continue down to visit children
return !found, nil
}, expr)
d[expr] = deps
return deps
}
// RewriteDerivedExpression rewrites all the ColName instances in the supplied expression with
// the expressions behind the column definition of the derived table
// SELECT foo FROM (SELECT id+42 as foo FROM user) as t
// We need `foo` to be translated to `id+42` on the inside of the derived table
func RewriteDerivedExpression(expr sqlparser.Expr, vt TableInfo) (sqlparser.Expr, error) {
newExpr := sqlparser.CloneExpr(expr)
sqlparser.Rewrite(newExpr, func(cursor *sqlparser.Cursor) bool {
switch node := cursor.Node().(type) {
case *sqlparser.ColName:
exp, err := vt.getExprFor(node.Name.String())
if err != nil {
return false
}
cursor.Replace(exp)
return false
}
return true
}, nil)
return newExpr, nil
}