forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_other.go
1489 lines (1344 loc) · 40.2 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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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,
// 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 expression
import (
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/stringutil"
"github.com/pingcap/tipb/go-tipb"
)
var (
_ functionClass = &inFunctionClass{}
_ functionClass = &rowFunctionClass{}
_ functionClass = &setVarFunctionClass{}
_ functionClass = &getIntVarFunctionClass{}
_ functionClass = &getRealVarFunctionClass{}
_ functionClass = &getDecimalVarFunctionClass{}
_ functionClass = &getTimeVarFunctionClass{}
_ functionClass = &getStringVarFunctionClass{}
_ 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 = &builtinSetStringVarSig{}
_ builtinFunc = &builtinSetIntVarSig{}
_ builtinFunc = &builtinSetRealVarSig{}
_ builtinFunc = &builtinSetDecimalVarSig{}
_ builtinFunc = &builtinGetStringVarSig{}
_ builtinFunc = &builtinGetIntVarSig{}
_ builtinFunc = &builtinGetRealVarSig{}
_ builtinFunc = &builtinGetDecimalVarSig{}
_ builtinFunc = &builtinGetTimeVarSig{}
_ 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) {
args, err = c.verifyArgs(ctx, args)
if err != nil {
return nil, err
}
argTps := make([]types.EvalType, len(args))
for i := range args {
argTps[i] = args[0].GetType().EvalType()
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, argTps...)
if err != nil {
return nil, err
}
for i := 1; i < len(args); i++ {
DisableParseJSONFlag4Expr(args[i])
}
bf.tp.SetFlen(1)
switch args[0].GetType().EvalType() {
case types.ETInt:
inInt := builtinInIntSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inInt.buildHashMapForConstArgs(ctx)
if err != nil {
return &inInt, err
}
sig = &inInt
sig.setPbCode(tipb.ScalarFuncSig_InInt)
case types.ETString:
inStr := builtinInStringSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inStr.buildHashMapForConstArgs(ctx)
if err != nil {
return &inStr, err
}
sig = &inStr
sig.setPbCode(tipb.ScalarFuncSig_InString)
case types.ETReal:
inReal := builtinInRealSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inReal.buildHashMapForConstArgs(ctx)
if err != nil {
return &inReal, err
}
sig = &inReal
sig.setPbCode(tipb.ScalarFuncSig_InReal)
case types.ETDecimal:
inDecimal := builtinInDecimalSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inDecimal.buildHashMapForConstArgs(ctx)
if err != nil {
return &inDecimal, err
}
sig = &inDecimal
sig.setPbCode(tipb.ScalarFuncSig_InDecimal)
case types.ETDatetime, types.ETTimestamp:
inTime := builtinInTimeSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inTime.buildHashMapForConstArgs(ctx)
if err != nil {
return &inTime, err
}
sig = &inTime
sig.setPbCode(tipb.ScalarFuncSig_InTime)
case types.ETDuration:
inDuration := builtinInDurationSig{baseInSig: baseInSig{baseBuiltinFunc: bf}}
err := inDuration.buildHashMapForConstArgs(ctx)
if err != nil {
return &inDuration, err
}
sig = &inDuration
sig.setPbCode(tipb.ScalarFuncSig_InDuration)
case types.ETJson:
sig = &builtinInJSONSig{baseBuiltinFunc: bf}
sig.setPbCode(tipb.ScalarFuncSig_InJson)
}
return sig, nil
}
func (c *inFunctionClass) verifyArgs(ctx sessionctx.Context, args []Expression) ([]Expression, error) {
columnType := args[0].GetType()
validatedArgs := make([]Expression, 0, len(args))
for _, arg := range args {
if constant, ok := arg.(*Constant); ok {
switch {
case columnType.GetType() == mysql.TypeBit && constant.Value.Kind() == types.KindInt64:
if constant.Value.GetInt64() < 0 {
if MaybeOverOptimized4PlanCache(ctx, args) {
ctx.GetSessionVars().StmtCtx.SkipPlanCache = true
ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("skip plan-cache: Bit Column in (%v)", constant.Value.GetInt64()))
}
continue
}
}
}
validatedArgs = append(validatedArgs, arg)
}
err := c.baseFunctionClass.verifyArgs(validatedArgs)
return validatedArgs, err
}
// nolint:structcheck
type baseInSig struct {
baseBuiltinFunc
// nonConstArgsIdx stores the indices of non-constant args in the baseBuiltinFunc.args (the first arg is not included).
// It works with builtinInXXXSig.hashset to accelerate 'eval'.
nonConstArgsIdx []int
hasNull bool
}
// builtinInIntSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInIntSig struct {
baseInSig
// the bool value in the map is used to identify whether the constant stored in key is signed or unsigned
hashSet map[int64]bool
}
func (b *builtinInIntSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = make(map[int64]bool, len(b.args)-1)
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalInt(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
b.hashSet[val] = mysql.HasUnsignedFlag(b.args[i].GetType().GetFlag())
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInIntSig) Clone() builtinFunc {
newSig := &builtinInIntSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInIntSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalInt(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
isUnsigned0 := mysql.HasUnsignedFlag(b.args[0].GetType().GetFlag())
args := b.args[1:]
if len(b.hashSet) != 0 {
if isUnsigned, ok := b.hashSet[arg0]; ok {
if (isUnsigned0 && isUnsigned) || (!isUnsigned0 && !isUnsigned) {
return 1, false, nil
}
if arg0 >= 0 {
return 1, false, nil
}
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalInt(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
isUnsigned := mysql.HasUnsignedFlag(arg.GetType().GetFlag())
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
}
// builtinInStringSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInStringSig struct {
baseInSig
hashSet set.StringSet
}
func (b *builtinInStringSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = set.NewStringSet()
collator := collate.GetCollator(b.collation)
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalString(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
b.hashSet.Insert(string(collator.Key(val))) // should do memory copy here
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInStringSig) Clone() builtinFunc {
newSig := &builtinInStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInStringSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalString(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
args := b.args[1:]
collator := collate.GetCollator(b.collation)
if len(b.hashSet) != 0 {
if b.hashSet.Exist(string(collator.Key(arg0))) {
return 1, false, nil
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalString(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
if types.CompareString(arg0, evaledArg, b.collation) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// builtinInRealSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInRealSig struct {
baseInSig
hashSet set.Float64Set
}
func (b *builtinInRealSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = set.NewFloat64Set()
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalReal(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
b.hashSet.Insert(val)
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInRealSig) Clone() builtinFunc {
newSig := &builtinInRealSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInRealSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalReal(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
args := b.args[1:]
if len(b.hashSet) != 0 {
if b.hashSet.Exist(arg0) {
return 1, false, nil
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalReal(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
if arg0 == evaledArg {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// builtinInDecimalSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInDecimalSig struct {
baseInSig
hashSet set.StringSet
}
func (b *builtinInDecimalSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = set.NewStringSet()
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalDecimal(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
key, err := val.ToHashKey()
if err != nil {
return err
}
b.hashSet.Insert(string(key))
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInDecimalSig) Clone() builtinFunc {
newSig := &builtinInDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInDecimalSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalDecimal(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
args := b.args[1:]
key, err := arg0.ToHashKey()
if err != nil {
return 0, true, err
}
if len(b.hashSet) != 0 {
if b.hashSet.Exist(string(key)) {
return 1, false, nil
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalDecimal(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// builtinInTimeSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInTimeSig struct {
baseInSig
hashSet map[types.CoreTime]struct{}
}
func (b *builtinInTimeSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = make(map[types.CoreTime]struct{}, len(b.args)-1)
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalTime(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
b.hashSet[val.CoreTime()] = struct{}{}
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInTimeSig) Clone() builtinFunc {
newSig := &builtinInTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInTimeSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalTime(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
args := b.args[1:]
if len(b.hashSet) != 0 {
if _, ok := b.hashSet[arg0.CoreTime()]; ok {
return 1, false, nil
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalTime(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// builtinInDurationSig see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in
type builtinInDurationSig struct {
baseInSig
hashSet map[time.Duration]struct{}
}
func (b *builtinInDurationSig) buildHashMapForConstArgs(ctx sessionctx.Context) error {
b.nonConstArgsIdx = make([]int, 0)
b.hashSet = make(map[time.Duration]struct{}, len(b.args)-1)
for i := 1; i < len(b.args); i++ {
if b.args[i].ConstItem(b.ctx.GetSessionVars().StmtCtx) {
val, isNull, err := b.args[i].EvalDuration(ctx, chunk.Row{})
if err != nil {
return err
}
if isNull {
b.hasNull = true
continue
}
b.hashSet[val.Duration] = struct{}{}
} else {
b.nonConstArgsIdx = append(b.nonConstArgsIdx, i)
}
}
return nil
}
func (b *builtinInDurationSig) Clone() builtinFunc {
newSig := &builtinInDurationSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
newSig.nonConstArgsIdx = make([]int, len(b.nonConstArgsIdx))
copy(newSig.nonConstArgsIdx, b.nonConstArgsIdx)
newSig.hashSet = b.hashSet
newSig.hasNull = b.hasNull
return newSig
}
func (b *builtinInDurationSig) evalInt(row chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalDuration(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
args := b.args[1:]
if len(b.hashSet) != 0 {
if _, ok := b.hashSet[arg0.Duration]; ok {
return 1, false, nil
}
args = make([]Expression, 0, len(b.nonConstArgsIdx))
for _, i := range b.nonConstArgsIdx {
args = append(args, b.args[i])
}
}
hasNull := b.hasNull
for _, arg := range args {
evaledArg, isNull, err := arg.EvalDuration(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
if arg0.Compare(evaledArg) == 0 {
return 1, false, nil
}
}
return 0, hasNull, nil
}
// builtinInJSONSig 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 chunk.Row) (int64, bool, error) {
arg0, isNull0, err := b.args[0].EvalJSON(b.ctx, row)
if isNull0 || err != nil {
return 0, isNull0, err
}
var hasNull bool
for _, arg := range b.args[1:] {
evaledArg, isNull, err := arg.EvalJSON(b.ctx, row)
if err != nil {
return 0, true, err
}
if isNull {
hasNull = true
continue
}
result := types.CompareBinaryJSON(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, err
}
argTps := make([]types.EvalType, len(args))
for i := range argTps {
argTps[i] = args[i].GetType().EvalType()
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, argTps...)
if err != nil {
return nil, err
}
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 chunk.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 = c.verifyArgs(args); err != nil {
return nil, err
}
argTp := args[1].GetType().EvalType()
if argTp == types.ETTimestamp || argTp == types.ETDuration || argTp == types.ETJson {
argTp = types.ETString
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, argTp, types.ETString, argTp)
if err != nil {
return nil, err
}
bf.tp.SetFlenUnderLimit(args[1].GetType().GetFlen())
switch argTp {
case types.ETString:
sig = &builtinSetStringVarSig{bf}
case types.ETReal:
sig = &builtinSetRealVarSig{bf}
case types.ETDecimal:
sig = &builtinSetDecimalVarSig{bf}
case types.ETInt:
sig = &builtinSetIntVarSig{bf}
case types.ETDatetime:
sig = &builtinSetTimeVarSig{bf}
default:
return nil, errors.Errorf("unexpected types.EvalType %v", argTp)
}
return sig, nil
}
type builtinSetStringVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetStringVarSig) Clone() builtinFunc {
newSig := &builtinSetStringVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetStringVarSig) evalString(row chunk.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, err
}
datum, err := b.args[1].Eval(row)
isNull = datum.IsNull()
if isNull || err != nil {
return "", isNull, err
}
res, err = datum.ToString()
if err != nil {
return "", isNull, err
}
sessionVars.SetStringUserVar(varName, stringutil.Copy(res), datum.Collation())
return res, false, nil
}
type builtinSetRealVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetRealVarSig) Clone() builtinFunc {
newSig := &builtinSetRealVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetRealVarSig) evalReal(row chunk.Row) (res float64, 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 0, isNull, err
}
datum, err := b.args[1].Eval(row)
isNull = datum.IsNull()
if isNull || err != nil {
return 0, isNull, err
}
res = datum.GetFloat64()
varName = strings.ToLower(varName)
sessionVars.SetUserVarVal(varName, datum)
return res, false, nil
}
type builtinSetDecimalVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetDecimalVarSig) Clone() builtinFunc {
newSig := &builtinSetDecimalVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetDecimalVarSig) evalDecimal(row chunk.Row) (*types.MyDecimal, bool, error) {
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return nil, isNull, err
}
datum, err := b.args[1].Eval(row)
isNull = datum.IsNull()
if isNull || err != nil {
return nil, isNull, err
}
res := datum.GetMysqlDecimal()
varName = strings.ToLower(varName)
sessionVars.SetUserVarVal(varName, datum)
return res, false, nil
}
type builtinSetIntVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetIntVarSig) Clone() builtinFunc {
newSig := &builtinSetIntVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetIntVarSig) evalInt(row chunk.Row) (int64, bool, error) {
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return 0, isNull, err
}
datum, err := b.args[1].Eval(row)
isNull = datum.IsNull()
if isNull || err != nil {
return 0, isNull, err
}
res := datum.GetInt64()
varName = strings.ToLower(varName)
sessionVars.SetUserVarVal(varName, datum)
return res, false, nil
}
type builtinSetTimeVarSig struct {
baseBuiltinFunc
}
func (b *builtinSetTimeVarSig) Clone() builtinFunc {
newSig := &builtinSetTimeVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinSetTimeVarSig) evalTime(row chunk.Row) (types.Time, bool, error) {
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return types.ZeroTime, isNull, err
}
datum, err := b.args[1].Eval(row)
if err != nil || datum.IsNull() {
return types.ZeroTime, datum.IsNull(), handleInvalidTimeError(b.ctx, err)
}
res := datum.GetMysqlTime()
varName = strings.ToLower(varName)
sessionVars.SetUserVarVal(varName, datum)
return res, false, nil
}
// BuildGetVarFunction builds a GetVar ScalarFunction from the Expression.
func BuildGetVarFunction(ctx sessionctx.Context, expr Expression, retType *types.FieldType) (Expression, error) {
var fc functionClass
switch retType.EvalType() {
case types.ETInt:
fc = &getIntVarFunctionClass{getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}, retType}}
case types.ETDecimal:
fc = &getDecimalVarFunctionClass{getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}, retType}}
case types.ETReal:
fc = &getRealVarFunctionClass{getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}, retType}}
case types.ETDatetime:
fc = &getTimeVarFunctionClass{getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}, retType}}
default:
fc = &getStringVarFunctionClass{getVarFunctionClass{baseFunctionClass{ast.GetVar, 1, 1}, retType}}
}
f, err := fc.getFunction(ctx, []Expression{expr})
if err != nil {
return nil, err
}
if builtinRetTp := f.getRetTp(); builtinRetTp.GetType() != mysql.TypeUnspecified || retType.GetType() == mysql.TypeUnspecified {
retType = builtinRetTp
}
return &ScalarFunction{
FuncName: model.NewCIStr(ast.GetVar),
RetType: retType,
Function: f,
}, nil
}
type getVarFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
type getStringVarFunctionClass struct {
getVarFunctionClass
}
func (c *getStringVarFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = c.verifyArgs(args); err != nil {
return nil, err
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString)
if err != nil {
return nil, err
}
bf.tp.SetFlen(c.tp.GetFlen())
if len(c.tp.GetCharset()) > 0 {
bf.tp.SetCharset(c.tp.GetCharset())
bf.tp.SetCollate(c.tp.GetCollate())
}
sig = &builtinGetStringVarSig{bf}
return sig, nil
}
type builtinGetStringVarSig struct {
baseBuiltinFunc
}
func (b *builtinGetStringVarSig) Clone() builtinFunc {
newSig := &builtinGetStringVarSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinGetStringVarSig) evalString(row chunk.Row) (string, bool, error) {
sessionVars := b.ctx.GetSessionVars()
varName, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return "", isNull, err
}
varName = strings.ToLower(varName)
if v, ok := sessionVars.GetUserVarVal(varName); ok {
// We cannot use v.GetString() here, because the datum may be in KindMysqlTime, which
// stores the data in datum.x.
// This seems controversial with https://dev.mysql.com/doc/refman/8.0/en/user-variables.html:
// > User variables can be assigned a value from a limited set of data types: integer, decimal,
// > floating-point, binary or nonbinary string, or NULL value.
// However, MySQL actually does support query like `set @p = now()`, so we should not assume the datum stored
// must be of one of the following types: string, decimal, int, float.
res, err := v.ToString()
if err != nil {
return "", false, err
}
return res, false, nil
}
return "", true, nil
}
type getIntVarFunctionClass struct {
getVarFunctionClass
}
func (c *getIntVarFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err = c.verifyArgs(args); err != nil {
return nil, err
}
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
if err != nil {
return nil, err
}
bf.tp.SetFlen(c.tp.GetFlen())
bf.tp.SetFlag(c.tp.GetFlag())
sig = &builtinGetIntVarSig{bf}
return sig, nil
}
type builtinGetIntVarSig struct {
baseBuiltinFunc