-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
builtin_other.go
785 lines (699 loc) · 21.5 KB
/
builtin_other.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright 2016 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.
package expression
import (
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
tipb "github.com/pingcap/tipb/go-tipb"
)
var (
_ functionClass = &inFunctionClass{}
_ functionClass = &rowFunctionClass{}
_ functionClass = &setVarFunctionClass{}
_ functionClass = &getVarFunctionClass{}
_ functionClass = &lockFunctionClass{}
_ functionClass = &releaseLockFunctionClass{}
_ functionClass = &valuesFunctionClass{}
_ functionClass = &bitCountFunctionClass{}
_ functionClass = &getParamFunctionClass{}
)
var (
_ builtinFunc = &builtinSleepSig{}
_ builtinFunc = &builtinInIntSig{}
_ builtinFunc = &builtinInStringSig{}
_ builtinFunc = &builtinInDecimalSig{}
_ builtinFunc = &builtinInRealSig{}
_ builtinFunc = &builtinInTimeSig{}
_ builtinFunc = &builtinInDurationSig{}
_ builtinFunc = &builtinInJSONSig{}
_ builtinFunc = &builtinRowSig{}
_ builtinFunc = &builtinSetVarSig{}
_ builtinFunc = &builtinGetVarSig{}
_ builtinFunc = &builtinLockSig{}
_ builtinFunc = &builtinReleaseLockSig{}
_ builtinFunc = &builtinValuesIntSig{}
_ builtinFunc = &builtinValuesRealSig{}
_ builtinFunc = &builtinValuesDecimalSig{}
_ builtinFunc = &builtinValuesStringSig{}
_ builtinFunc = &builtinValuesTimeSig{}
_ builtinFunc = &builtinValuesDurationSig{}
_ builtinFunc = &builtinValuesJSONSig{}
_ builtinFunc = &builtinBitCountSig{}
_ builtinFunc = &builtinGetParamStringSig{}
)
type inFunctionClass struct {
baseFunctionClass
}
func (c *inFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
argTps := make([]types.EvalType, len(args))
for i := range args {
argTps[i] = args[0].GetType().EvalType()
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, argTps...)
bf.tp.Flen = 1
switch args[0].GetType().EvalType() {
case types.ETInt:
sig = &builtinInIntSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InInt)
case types.ETString:
sig = &builtinInStringSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InString)
case types.ETReal:
sig = &builtinInRealSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InReal)
case types.ETDecimal:
sig = &builtinInDecimalSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InDecimal)
case types.ETDatetime, types.ETTimestamp:
sig = &builtinInTimeSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InTime)
case types.ETDuration:
sig = &builtinInDurationSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InDuration)
case types.ETJson:
sig = &builtinInJSONSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InJson)
}
return sig, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInIntSig struct {
baseBuiltinFunc
}
func (b *builtinInIntSig) Clone() builtinFunc {
newSig := &builtinInIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInIntSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalInt(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
isUnsigned0 := mysql.HasUnsignedFlag(b.args[0].GetType().Flag)
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalInt(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
isUnsigned := mysql.HasUnsignedFlag(arg.GetType().Flag)
if isUnsigned0 && isUnsigned {
if evaledArg == arg0 {
return 1, false, nil
}
} else if !isUnsigned0 && !isUnsigned {
if evaledArg == arg0 {
return 1, false, nil
}
} else if !isUnsigned0 && isUnsigned {
if arg0 >= 0 && evaledArg == arg0 {
return 1, false, nil
}
} else {
if evaledArg >= 0 && evaledArg == arg0 {
return 1, false, nil
}
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInStringSig struct {
baseBuiltinFunc
}
func (b *builtinInStringSig) Clone() builtinFunc {
newSig := &builtinInStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInStringSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalString(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalString(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
if arg0 == evaledArg {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInRealSig struct {
baseBuiltinFunc
}
func (b *builtinInRealSig) Clone() builtinFunc {
newSig := &builtinInRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInRealSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalReal(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalReal(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
if arg0 == evaledArg {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInDecimalSig struct {
baseBuiltinFunc
}
func (b *builtinInDecimalSig) Clone() builtinFunc {
newSig := &builtinInDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInDecimalSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalDecimal(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInTimeSig struct {
baseBuiltinFunc
}
func (b *builtinInTimeSig) Clone() builtinFunc {
newSig := &builtinInTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInTimeSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalTime(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalTime(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInDurationSig struct {
baseBuiltinFunc
}
func (b *builtinInDurationSig) Clone() builtinFunc {
newSig := &builtinInDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInDurationSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalDuration(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalDuration(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// See https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInJSONSig struct {
baseBuiltinFunc
}
func (b *builtinInJSONSig) Clone() builtinFunc {
newSig := &builtinInJSONSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInJSONSig) evalInt(row types.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalJSON(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, errors.Trace(err)
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalJSON(b.ctx, row)
if err != nil {
return 0, true, errors.Trace(err)
}
if isNull {
hasNull = true
continue
}
result := json.CompareBinary(evaledArg, arg0)
if result == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
type rowFunctionClass struct {
baseFunctionClass
}
func (c *rowFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
argTps := make([]types.EvalType, len(args))
for i := range argTps {
argTps[i] = args[i].GetType().EvalType()
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, argTps...)
sig = &builtinRowSig{bf}
return sig, nil
}
type builtinRowSig struct {
baseBuiltinFunc
}
func (b *builtinRowSig) Clone() builtinFunc {
newSig := &builtinRowSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalString rowFunc should always be flattened in expression rewrite phrase.
func (b *builtinRowSig) evalString(row types.Row) (string, bool, error) {
panic("builtinRowSig.evalString() should never be called.")
}
type setVarFunctionClass struct {
baseFunctionClass
}
func (c *setVarFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = errors.Trace(c.verifyArgs(args)); err != nil {
return nil, err
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, types.ETString, types.ETString)
bf.tp.Flen = args[1].GetType().Flen
// TODO: we should consider the type of the argument, but not take it as string for all situations.
sig = &builtinSetVarSig{bf}
return sig, errors.Trace(err)
}
type builtinSetVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetVarSig) Clone() builtinFunc {
newSig := &builtinSetVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetVarSig) evalString(row types.Row) (res string, isNull bool, err error) {
var varName string
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err = b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
res, isNull, err = b.args[1].EvalString(b.ctx, row)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
varName = strings.ToLower(varName)
sessionVars.UsersLock.Lock()
sessionVars.Users[varName] = res
sessionVars.UsersLock.Unlock()
return res, false, nil
}
type getVarFunctionClass struct {
baseFunctionClass
}
func (c *getVarFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = errors.Trace(c.verifyArgs(args)); err != nil {
return nil, err
}
// TODO: we should consider the type of the argument, but not take it as string for all situations.
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, types.ETString)
bf.tp.Flen = mysql.MaxFieldVarCharLength
sig = &builtinGetVarSig{bf}
return sig, nil
}
type builtinGetVarSig struct {
baseBuiltinFunc
}
func (b *builtinGetVarSig) Clone() builtinFunc {
newSig := &builtinGetVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinGetVarSig) evalString(row types.Row) (string, bool, error) {
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
varName = strings.ToLower(varName)
sessionVars.UsersLock.RLock()
defer sessionVars.UsersLock.RUnlock()
if v, ok := sessionVars.Users[varName]; ok {
return v, false, nil
}
return "", true, nil
}
type valuesFunctionClass struct {
baseFunctionClass
offset int
tp *types.FieldType
}
func (c *valuesFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = errors.Trace(c.verifyArgs(args)); err != nil {
return nil, err
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
switch c.tp.EvalType() {
case types.ETInt:
sig = &builtinValuesIntSig{bf, c.offset}
case types.ETReal:
sig = &builtinValuesRealSig{bf, c.offset}
case types.ETDecimal:
sig = &builtinValuesDecimalSig{bf, c.offset}
case types.ETString:
sig = &builtinValuesStringSig{bf, c.offset}
case types.ETDatetime, types.ETTimestamp:
sig = &builtinValuesTimeSig{bf, c.offset}
case types.ETDuration:
sig = &builtinValuesDurationSig{bf, c.offset}
case types.ETJson:
sig = &builtinValuesJSONSig{bf, c.offset}
}
return sig, nil
}
type builtinValuesIntSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesIntSig) Clone() builtinFunc {
newSig := &builtinValuesIntSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalInt evals a builtinValuesIntSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesIntSig) evalInt(_ types.Row) (int64, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return 0, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return 0, true, nil
}
return row.GetInt64(b.offset), false, nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesRealSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesRealSig) Clone() builtinFunc {
newSig := &builtinValuesRealSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalReal evals a builtinValuesRealSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesRealSig) evalReal(_ types.Row) (float64, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return 0, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return 0, true, nil
}
return row.GetFloat64(b.offset), false, nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesDecimalSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesDecimalSig) Clone() builtinFunc {
newSig := &builtinValuesDecimalSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalDecimal evals a builtinValuesDecimalSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesDecimalSig) evalDecimal(_ types.Row) (*types.MyDecimal, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return nil, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return nil, true, nil
}
return row.GetMyDecimal(b.offset), false, nil
}
return nil, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesStringSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesStringSig) Clone() builtinFunc {
newSig := &builtinValuesStringSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalString evals a builtinValuesStringSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesStringSig) evalString(_ types.Row) (string, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return "", true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return "", true, nil
}
return row.GetString(b.offset), false, nil
}
return "", true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesTimeSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesTimeSig) Clone() builtinFunc {
newSig := &builtinValuesTimeSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalTime evals a builtinValuesTimeSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesTimeSig) evalTime(_ types.Row) (types.Time, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return types.Time{}, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return types.Time{}, true, nil
}
return row.GetTime(b.offset), false, nil
}
return types.Time{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesDurationSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesDurationSig) Clone() builtinFunc {
newSig := &builtinValuesDurationSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalDuration evals a builtinValuesDurationSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesDurationSig) evalDuration(_ types.Row) (types.Duration, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return types.Duration{}, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return types.Duration{}, true, nil
}
return row.GetDuration(b.offset), false, nil
}
return types.Duration{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type builtinValuesJSONSig struct {
baseBuiltinFunc
offset int
}
func (b *builtinValuesJSONSig) Clone() builtinFunc {
newSig := &builtinValuesJSONSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalJSON evals a builtinValuesJSONSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesJSONSig) evalJSON(_ types.Row) (json.BinaryJSON, bool, error) {
values := b.ctx.GetSessionVars().CurrInsertValues
if values == nil {
return json.BinaryJSON{}, true, errors.New("Session current insert values is nil")
}
row := values.(types.Row)
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return json.BinaryJSON{}, true, nil
}
return row.GetJSON(b.offset), false, nil
}
return json.BinaryJSON{}, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}
type bitCountFunctionClass struct {
baseFunctionClass
}
func (c *bitCountFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, types.ETInt)
bf.tp.Flen = 2
sig := &builtinBitCountSig{bf}
return sig, nil
}
type builtinBitCountSig struct {
baseBuiltinFunc
}
func (b *builtinBitCountSig) Clone() builtinFunc {
newSig := &builtinBitCountSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalInt evals BIT_COUNT(N).
// See https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html#function_bit-count
func (b *builtinBitCountSig) evalInt(row types.Row) (int64, bool, error) {
n, isNull, err := b.args[0].EvalInt(b.ctx, row)
if err != nil || isNull {
if err != nil && types.ErrOverflow.Equal(err) {
return 64, false, nil
}
return 0, true, errors.Trace(err)
}
var count int64
for ; n != 0; n = (n - 1) & n {
count++
}
return count, false, nil
}
// getParamFunctionClass for plan cache of prepared statements
type getParamFunctionClass struct {
baseFunctionClass
}
// getFunction gets function
// TODO: more typed functions will be added when typed parameters are supported.
func (c *getParamFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString, types.ETInt)
bf.tp.Flen = mysql.MaxFieldVarCharLength
sig := &builtinGetParamStringSig{bf}
return sig, nil
}
type builtinGetParamStringSig struct {
baseBuiltinFunc
}
func (b *builtinGetParamStringSig) Clone() builtinFunc {
newSig := &builtinGetParamStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinGetParamStringSig) evalString(row types.Row) (string, bool, error) {
sessionVars := b.ctx.GetSessionVars()
idx, isNull, err := b.args[0].EvalInt(b.ctx, row)
if isNull || err != nil {
return "", isNull, errors.Trace(err)
}
v := sessionVars.PreparedParams[idx]
dt := v.(types.Datum)
str, err := (&dt).ToString()
if err != nil {
return "", true, nil
}
return str, false, nil
}