forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
normalizer.go
410 lines (374 loc) · 11.4 KB
/
normalizer.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
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 sqlparser
import (
"bytes"
"vitess.io/vitess/go/mysql/datetime"
"vitess.io/vitess/go/sqltypes"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// BindVars is a set of reserved bind variables from a SQL statement
type BindVars map[string]struct{}
// Normalize changes the statement to use bind values, and
// updates the bind vars to those values. The supplied prefix
// is used to generate the bind var names. The function ensures
// that there are no collisions with existing bind vars.
// Within Select constructs, bind vars are deduped. This allows
// us to identify vindex equality. Otherwise, every value is
// treated as distinct.
func Normalize(stmt Statement, reserved *ReservedVars, bindVars map[string]*querypb.BindVariable) error {
nz := newNormalizer(reserved, bindVars)
_ = SafeRewrite(stmt, nz.walkStatementDown, nz.walkStatementUp)
return nz.err
}
type normalizer struct {
bindVars map[string]*querypb.BindVariable
reserved *ReservedVars
vals map[Literal]string
err error
inDerived bool
}
func newNormalizer(reserved *ReservedVars, bindVars map[string]*querypb.BindVariable) *normalizer {
return &normalizer{
bindVars: bindVars,
reserved: reserved,
vals: make(map[Literal]string),
}
}
// walkStatementUp is one half of the top level walk function.
func (nz *normalizer) walkStatementUp(cursor *Cursor) bool {
if nz.err != nil {
return false
}
node, isLiteral := cursor.Node().(*Literal)
if !isLiteral {
return true
}
nz.convertLiteral(node, cursor)
return nz.err == nil // only continue if we haven't found any errors
}
// walkStatementDown is the top level walk function.
// If it encounters a Select, it switches to a mode
// where variables are deduped.
func (nz *normalizer) walkStatementDown(node, parent SQLNode) bool {
switch node := node.(type) {
// no need to normalize the statement types
case *Set, *Show, *Begin, *Commit, *Rollback, *Savepoint, DDLStatement, *SRollback, *Release, *OtherAdmin, *Analyze:
return false
case *Select:
_, isDerived := parent.(*DerivedTable)
var tmp bool
tmp, nz.inDerived = nz.inDerived, isDerived
_ = SafeRewrite(node, nz.walkDownSelect, nz.walkUpSelect)
// Don't continue
nz.inDerived = tmp
return false
case *ComparisonExpr:
nz.convertComparison(node)
case *UpdateExpr:
nz.convertUpdateExpr(node)
case *ColName, TableName:
// Common node types that never contain Literal or ListArgs but create a lot of object
// allocations.
return false
case *ConvertType: // we should not rewrite the type description
return false
}
return nz.err == nil // only continue if we haven't found any errors
}
// walkDownSelect normalizes the AST in Select mode.
func (nz *normalizer) walkDownSelect(node, parent SQLNode) bool {
switch node := node.(type) {
case *Select:
_, isDerived := parent.(*DerivedTable)
if !isDerived {
return true
}
var tmp bool
tmp, nz.inDerived = nz.inDerived, isDerived
// initiating a new AST walk here means that we might change something while walking down on the tree,
// but since we are only changing literals, we can be safe that we are not changing the SELECT struct,
// only something much further down, and that should be safe
_ = SafeRewrite(node, nz.walkDownSelect, nz.walkUpSelect)
// Don't continue
nz.inDerived = tmp
return false
case SelectExprs:
return !nz.inDerived
case *ComparisonExpr:
nz.convertComparison(node)
case *FramePoint:
// do not make a bind var for rows and range
return false
case *ColName, TableName:
// Common node types that never contain Literals or ListArgs but create a lot of object
// allocations.
return false
case *ConvertType:
// we should not rewrite the type description
return false
}
return nz.err == nil // only continue if we haven't found any errors
}
// walkUpSelect normalizes the Literals in Select mode.
func (nz *normalizer) walkUpSelect(cursor *Cursor) bool {
if nz.err != nil {
return false
}
node, isLiteral := cursor.Node().(*Literal)
if !isLiteral {
return true
}
parent := cursor.Parent()
switch parent.(type) {
case *Order, *GroupBy:
return true
case *Limit:
nz.convertLiteral(node, cursor)
default:
nz.convertLiteralDedup(node, cursor)
}
return nz.err == nil // only continue if we haven't found any errors
}
func validateLiteral(node *Literal) error {
switch node.Type {
case DateVal:
if _, ok := datetime.ParseDate(node.Val); !ok {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Incorrect DATE value: '%s'", node.Val)
}
case TimeVal:
if _, _, timeState := datetime.ParseTime(node.Val, -1); timeState != datetime.TimeOK {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Incorrect TIME value: '%s'", node.Val)
}
case TimestampVal:
if _, _, ok := datetime.ParseDateTime(node.Val, -1); !ok {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "Incorrect DATETIME value: '%s'", node.Val)
}
}
return nil
}
func (nz *normalizer) convertLiteralDedup(node *Literal, cursor *Cursor) {
err := validateLiteral(node)
if err != nil {
nz.err = err
}
// If value is too long, don't dedup.
// Such values are most likely not for vindexes.
// We save a lot of CPU because we avoid building
// the key for them.
if len(node.Val) > 256 {
nz.convertLiteral(node, cursor)
return
}
// Make the bindvar
bval := SQLToBindvar(node)
if bval == nil {
return
}
// Check if there's a bindvar for that value already.
bvname, ok := nz.vals[*node]
if !ok {
// If there's no such bindvar, make a new one.
bvname = nz.reserved.nextUnusedVar()
nz.vals[*node] = bvname
nz.bindVars[bvname] = bval
}
// Modify the AST node to a bindvar.
arg, err := NewTypedArgumentFromLiteral(bvname, node)
if err != nil {
nz.err = err
return
}
cursor.Replace(arg)
}
// convertLiteral converts an Literal without the dedup.
func (nz *normalizer) convertLiteral(node *Literal, cursor *Cursor) {
err := validateLiteral(node)
if err != nil {
nz.err = err
}
bval := SQLToBindvar(node)
if bval == nil {
return
}
bvname := nz.reserved.nextUnusedVar()
nz.bindVars[bvname] = bval
arg, err := NewTypedArgumentFromLiteral(bvname, node)
if err != nil {
nz.err = err
return
}
cursor.Replace(arg)
}
// convertComparison attempts to convert IN clauses to
// use the list bind var construct. If it fails, it returns
// with no change made. The walk function will then continue
// and iterate on converting each individual value into separate
// bind vars.
func (nz *normalizer) convertComparison(node *ComparisonExpr) {
switch node.Operator {
case InOp, NotInOp:
nz.rewriteInComparisons(node)
default:
nz.rewriteOtherComparisons(node)
}
}
func (nz *normalizer) rewriteOtherComparisons(node *ComparisonExpr) {
newR := nz.parameterize(node.Left, node.Right)
if newR != nil {
node.Right = newR
}
}
func (nz *normalizer) parameterize(left, right Expr) Expr {
col, ok := left.(*ColName)
if !ok {
return nil
}
lit, ok := right.(*Literal)
if !ok {
return nil
}
err := validateLiteral(lit)
if err != nil {
nz.err = err
return nil
}
bval := SQLToBindvar(lit)
if bval == nil {
return nil
}
bvname := nz.decideBindVarName(lit, col, bval)
arg, err := NewTypedArgumentFromLiteral(bvname, lit)
if err != nil {
nz.err = err
return nil
}
return arg
}
func (nz *normalizer) decideBindVarName(lit *Literal, col *ColName, bval *querypb.BindVariable) string {
if len(lit.Val) <= 256 {
// first we check if we already have a bindvar for this value. if we do, we re-use that bindvar name
bvname, ok := nz.vals[*lit]
if ok {
return bvname
}
}
// If there's no such bindvar, or we have a big value, make a new one.
// Big values are most likely not for vindexes.
// We save a lot of CPU because we avoid building
bvname := nz.reserved.ReserveColName(col)
nz.vals[*lit] = bvname
nz.bindVars[bvname] = bval
return bvname
}
func (nz *normalizer) rewriteInComparisons(node *ComparisonExpr) {
tupleVals, ok := node.Right.(ValTuple)
if !ok {
return
}
// The RHS is a tuple of values.
// Make a list bindvar.
bvals := &querypb.BindVariable{
Type: querypb.Type_TUPLE,
}
for _, val := range tupleVals {
bval := SQLToBindvar(val)
if bval == nil {
return
}
bvals.Values = append(bvals.Values, &querypb.Value{
Type: bval.Type,
Value: bval.Value,
})
}
bvname := nz.reserved.nextUnusedVar()
nz.bindVars[bvname] = bvals
// Modify RHS to be a list bindvar.
node.Right = ListArg(bvname)
}
func (nz *normalizer) convertUpdateExpr(node *UpdateExpr) {
newR := nz.parameterize(node.Name, node.Expr)
if newR != nil {
node.Expr = newR
}
}
func SQLToBindvar(node SQLNode) *querypb.BindVariable {
if node, ok := node.(*Literal); ok {
var v sqltypes.Value
var err error
switch node.Type {
case StrVal:
v, err = sqltypes.NewValue(sqltypes.VarChar, node.Bytes())
case IntVal:
v, err = sqltypes.NewValue(sqltypes.Int64, node.Bytes())
case FloatVal:
v, err = sqltypes.NewValue(sqltypes.Float64, node.Bytes())
case DecimalVal:
v, err = sqltypes.NewValue(sqltypes.Decimal, node.Bytes())
case HexNum:
buf := make([]byte, 0, len(node.Bytes()))
buf = append(buf, "0x"...)
buf = append(buf, bytes.ToUpper(node.Bytes()[2:])...)
v, err = sqltypes.NewValue(sqltypes.HexNum, buf)
case HexVal:
// We parse the `x'7b7d'` string literal into a hex encoded string of `7b7d` in the parser
// We need to re-encode it back to the original MySQL query format before passing it on as a bindvar value to MySQL
buf := make([]byte, 0, len(node.Bytes())+3)
buf = append(buf, 'x', '\'')
buf = append(buf, bytes.ToUpper(node.Bytes())...)
buf = append(buf, '\'')
v, err = sqltypes.NewValue(sqltypes.HexVal, buf)
case BitNum:
out := make([]byte, 0, len(node.Bytes())+2)
out = append(out, '0', 'b')
out = append(out, node.Bytes()[2:]...)
v, err = sqltypes.NewValue(sqltypes.BitNum, out)
case DateVal:
v, err = sqltypes.NewValue(sqltypes.Date, node.Bytes())
case TimeVal:
v, err = sqltypes.NewValue(sqltypes.Time, node.Bytes())
case TimestampVal:
// This is actually a DATETIME MySQL type. The timestamp literal
// syntax is part of the SQL standard and MySQL DATETIME matches
// the type best.
v, err = sqltypes.NewValue(sqltypes.Datetime, node.Bytes())
default:
return nil
}
if err != nil {
return nil
}
return sqltypes.ValueBindVariable(v)
}
return nil
}
// GetBindvars returns a map of the bind vars referenced in the statement.
func GetBindvars(stmt Statement) map[string]struct{} {
bindvars := make(map[string]struct{})
_ = Walk(func(node SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *ColName, TableName:
// Common node types that never contain expressions but create a lot of object
// allocations.
return false, nil
case *Argument:
bindvars[node.Name] = struct{}{}
case ListArg:
bindvars[string(node)] = struct{}{}
}
return true, nil
}, stmt)
return bindvars
}