forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
524 lines (471 loc) · 12.5 KB
/
helper.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2014 The TiDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found PatternIn the LICENSE file.
package expression
import (
"strconv"
"strings"
"time"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression/builtin"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/types"
)
const (
// ExprEvalDefaultName is the key saving default column name for Default expression.
ExprEvalDefaultName = "$defaultName"
// ExprEvalIdentFunc is the key saving a function to retrieve value for identifier name.
ExprEvalIdentFunc = "$identFunc"
// ExprEvalPositionFunc is the key saving a Position expresion.
ExprEvalPositionFunc = "$positionFunc"
// ExprEvalValuesFunc is the key saving a function to retrieve value for column name.
ExprEvalValuesFunc = "$valuesFunc"
)
var (
// CurrentTimestamp is the keyword getting default value for datetime and timestamp type.
CurrentTimestamp = "CURRENT_TIMESTAMP"
// CurrentTimeExpr is the expression retireving default value for datetime and timestamp type.
CurrentTimeExpr = &Ident{model.NewCIStr(CurrentTimestamp)}
// ZeroTimestamp shows the zero datetime and timestamp.
ZeroTimestamp = "0000-00-00 00:00:00"
)
var (
errDefaultValue = errors.New("invalid default value")
)
// TypeStar is the type for *
type TypeStar string
// Expr removes parenthese expression, e.g, (expr) -> expr.
func Expr(v interface{}) Expression {
e := v.(Expression)
for {
x, ok := e.(*PExpr)
if !ok {
return e
}
e = x.Expr
}
}
func cloneExpressionList(list []Expression) []Expression {
r := make([]Expression, len(list))
for i, v := range list {
r[i] = v.Clone()
}
return r
}
// FastEval evaluates Value and static +/- Unary expression and returns its value.
func FastEval(v interface{}) interface{} {
switch x := v.(type) {
case Value:
return x.Val
case int64, uint64:
return v
case *UnaryOperation:
if x.Op != opcode.Plus && x.Op != opcode.Minus {
return nil
}
if !x.IsStatic() {
return nil
}
m := map[interface{}]interface{}{}
return Eval(x, nil, m)
default:
return nil
}
}
// IsQualified returns whether name contains ".".
func IsQualified(name string) bool {
return strings.Contains(name, ".")
}
// Eval is a helper function evaluates expression v and do a panic if evaluating error.
func Eval(v Expression, ctx context.Context, env map[interface{}]interface{}) (y interface{}) {
var err error
y, err = v.Eval(ctx, env)
if err != nil {
panic(err) // panic ok here
}
return
}
// MentionedAggregateFuncs returns a list of the Call expression which is aggregate function.
func MentionedAggregateFuncs(e Expression) []Expression {
var m []Expression
mentionedAggregateFuncs(e, &m)
return m
}
func mentionedAggregateFuncs(e Expression, m *[]Expression) {
switch x := e.(type) {
case Value, *Value, *Variable, *Default,
*Ident, SubQuery, *Position, *ExistsSubQuery:
// nop
case *BinaryOperation:
mentionedAggregateFuncs(x.L, m)
mentionedAggregateFuncs(x.R, m)
case *Call:
f, ok := builtin.Funcs[strings.ToLower(x.F)]
if !ok {
log.Errorf("unknown function %s", x.F)
return
}
if f.IsAggregate {
// if f is aggregate function, we don't need check the arguments,
// because using an aggregate function in the aggregate arg like count(max(c1)) is invalid
// TODO: check whether argument contains an aggregate function and return error.
*m = append(*m, e)
return
}
for _, e := range x.Args {
mentionedAggregateFuncs(e, m)
}
case *IsNull:
mentionedAggregateFuncs(x.Expr, m)
case *PExpr:
mentionedAggregateFuncs(x.Expr, m)
case *PatternIn:
mentionedAggregateFuncs(x.Expr, m)
for _, e := range x.List {
mentionedAggregateFuncs(e, m)
}
case *PatternLike:
mentionedAggregateFuncs(x.Expr, m)
mentionedAggregateFuncs(x.Pattern, m)
case *UnaryOperation:
mentionedAggregateFuncs(x.V, m)
case *ParamMarker:
if x.Expr != nil {
mentionedAggregateFuncs(x.Expr, m)
}
case *FunctionCast:
if x.Expr != nil {
mentionedAggregateFuncs(x.Expr, m)
}
case *FunctionConvert:
if x.Expr != nil {
mentionedAggregateFuncs(x.Expr, m)
}
case *FunctionSubstring:
if x.StrExpr != nil {
mentionedAggregateFuncs(x.StrExpr, m)
}
if x.Pos != nil {
mentionedAggregateFuncs(x.Pos, m)
}
if x.Len != nil {
mentionedAggregateFuncs(x.Len, m)
}
case *FunctionCase:
if x.Value != nil {
mentionedAggregateFuncs(x.Value, m)
}
for _, w := range x.WhenClauses {
mentionedAggregateFuncs(w, m)
}
if x.ElseClause != nil {
mentionedAggregateFuncs(x.ElseClause, m)
}
case *WhenClause:
mentionedAggregateFuncs(x.Expr, m)
mentionedAggregateFuncs(x.Result, m)
case *IsTruth:
mentionedAggregateFuncs(x.Expr, m)
case *Between:
mentionedAggregateFuncs(x.Expr, m)
mentionedAggregateFuncs(x.Left, m)
mentionedAggregateFuncs(x.Right, m)
case *Row:
for _, expr := range x.Values {
mentionedAggregateFuncs(expr, m)
}
case *CompareSubQuery:
mentionedAggregateFuncs(x.L, m)
default:
log.Errorf("Unknown Expression: %T", e)
}
}
// ContainAggregateFunc checks whether expression e contains an aggregate function, like count(*) or other.
func ContainAggregateFunc(e Expression) bool {
m := MentionedAggregateFuncs(e)
return len(m) > 0
}
func mentionedColumns(e Expression, m map[string]bool, names *[]string) {
switch x := e.(type) {
case Value, *Value, *Variable, *Default,
SubQuery, *Position, *ExistsSubQuery:
// nop
case *BinaryOperation:
mentionedColumns(x.L, m, names)
mentionedColumns(x.R, m, names)
case *Call:
for _, e := range x.Args {
mentionedColumns(e, m, names)
}
case *Ident:
name := x.L
if !m[name] {
m[name] = true
*names = append(*names, name)
}
case *IsNull:
mentionedColumns(x.Expr, m, names)
case *PExpr:
mentionedColumns(x.Expr, m, names)
case *PatternIn:
mentionedColumns(x.Expr, m, names)
for _, e := range x.List {
mentionedColumns(e, m, names)
}
case *PatternLike:
mentionedColumns(x.Expr, m, names)
mentionedColumns(x.Pattern, m, names)
case *UnaryOperation:
mentionedColumns(x.V, m, names)
case *ParamMarker:
if x.Expr != nil {
mentionedColumns(x.Expr, m, names)
}
case *FunctionCast:
if x.Expr != nil {
mentionedColumns(x.Expr, m, names)
}
case *FunctionConvert:
if x.Expr != nil {
mentionedColumns(x.Expr, m, names)
}
case *FunctionSubstring:
if x.StrExpr != nil {
mentionedColumns(x.StrExpr, m, names)
}
if x.Pos != nil {
mentionedColumns(x.Pos, m, names)
}
if x.Len != nil {
mentionedColumns(x.Len, m, names)
}
case *FunctionCase:
if x.Value != nil {
mentionedColumns(x.Value, m, names)
}
for _, w := range x.WhenClauses {
mentionedColumns(w, m, names)
}
if x.ElseClause != nil {
mentionedColumns(x.ElseClause, m, names)
}
case *WhenClause:
mentionedColumns(x.Expr, m, names)
mentionedColumns(x.Result, m, names)
case *IsTruth:
mentionedColumns(x.Expr, m, names)
case *Between:
mentionedColumns(x.Expr, m, names)
mentionedColumns(x.Left, m, names)
mentionedColumns(x.Right, m, names)
case *Row:
for _, expr := range x.Values {
mentionedColumns(expr, m, names)
}
case *CompareSubQuery:
mentionedColumns(x.L, m, names)
default:
log.Errorf("Unknown Expression: %T", e)
}
}
// MentionedColumns returns a list of names for Ident expression.
func MentionedColumns(e Expression) []string {
var names []string
m := make(map[string]bool)
mentionedColumns(e, m, &names)
return names
}
func staticExpr(e Expression) (Expression, error) {
if e.IsStatic() {
v, err := e.Eval(nil, nil)
if err != nil {
return nil, err
}
if v == nil {
return Value{nil}, nil
}
return Value{v}, nil
}
return e, nil
}
// IsCurrentTimeExpr returns whether e is CurrentTimeExpr.
func IsCurrentTimeExpr(e Expression) bool {
x, ok := e.(*Ident)
if !ok {
return false
}
return x.Equal(CurrentTimeExpr)
}
func getSystemTimestamp(ctx context.Context) (time.Time, error) {
value := time.Now()
if ctx == nil {
return value, nil
}
// check whether use timestamp varibale
sessionVars := variable.GetSessionVars(ctx)
if v, ok := sessionVars.Systems["timestamp"]; ok {
if v != "" {
timestamp, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return time.Time{}, errors.Trace(err)
}
if timestamp <= 0 {
return value, nil
}
return time.Unix(timestamp, 0), nil
}
}
return value, nil
}
// GetTimeValue gets the time value with type tp.
func GetTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (interface{}, error) {
return getTimeValue(ctx, v, tp, fsp)
}
func getTimeValue(ctx context.Context, v interface{}, tp byte, fsp int) (interface{}, error) {
value := mysql.Time{
Type: tp,
Fsp: fsp,
}
defaultTime, err := getSystemTimestamp(ctx)
if err != nil {
return nil, errors.Trace(err)
}
switch x := v.(type) {
case string:
if x == CurrentTimestamp {
value.Time = defaultTime
} else if x == ZeroTimestamp {
value, _ = mysql.ParseTimeFromNum(0, tp, fsp)
} else {
value, err = mysql.ParseTime(x, tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
}
case Value:
switch xval := x.Val.(type) {
case string:
value, err = mysql.ParseTime(xval, tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
case int64:
value, err = mysql.ParseTimeFromNum(int64(xval), tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
case nil:
return nil, nil
default:
return nil, errors.Trace(errDefaultValue)
}
case *Ident:
if x.Equal(CurrentTimeExpr) {
return CurrentTimestamp, nil
}
return nil, errors.Trace(errDefaultValue)
case *UnaryOperation:
// support some expression, like `-1`
m := map[interface{}]interface{}{}
v := Eval(x, nil, m)
ft := types.NewFieldType(mysql.TypeLonglong)
xval, err := types.Convert(v, ft)
if err != nil {
return nil, errors.Trace(err)
}
value, err = mysql.ParseTimeFromNum(xval.(int64), tp, fsp)
if err != nil {
return nil, errors.Trace(err)
}
default:
return nil, nil
}
return value, nil
}
// EvalBoolExpr evaluates an expression and convert its return value to bool.
func EvalBoolExpr(ctx context.Context, expr Expression, m map[interface{}]interface{}) (bool, error) {
val, err := expr.Eval(ctx, m)
if err != nil {
return false, err
}
if val == nil {
return false, nil
}
x, err := types.ToBool(val)
if err != nil {
return false, err
}
return x != 0, nil
}
// CheckOneColumn checks whether expression e has only one column for the evaluation result.
// Now most of the expressions have one column except Row expression.
func CheckOneColumn(ctx context.Context, e Expression) error {
n, err := columnCount(ctx, e)
if err != nil {
return errors.Trace(err)
}
if n != 1 {
return errors.Errorf("Operand should contain 1 column(s)")
}
return nil
}
// CheckAllOneColumns checks all expressions have one column.
func CheckAllOneColumns(ctx context.Context, args ...Expression) error {
for _, e := range args {
if err := CheckOneColumn(ctx, e); err != nil {
return err
}
}
return nil
}
func columnCount(ctx context.Context, e Expression) (int, error) {
switch x := e.(type) {
case *Row:
n := len(x.Values)
if n <= 1 {
return 0, errors.Errorf("Operand should contain >= 2 columns for Row")
}
return n, nil
case SubQuery:
return x.ColumnCount(ctx)
default:
return 1, nil
}
}
func hasSameColumnCount(ctx context.Context, e Expression, args ...Expression) error {
l, err := columnCount(ctx, e)
if err != nil {
return errors.Trace(err)
}
var n int
for _, arg := range args {
n, err = columnCount(ctx, arg)
if err != nil {
return errors.Trace(err)
}
if n != l {
return errors.Errorf("Operand should contain %d column(s)", l)
}
}
return nil
}