/
builtin_cast.go
760 lines (660 loc) · 21.4 KB
/
builtin_cast.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
// Copyright 2017 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.
// We implement 6 CastAsXXFunctionClass for `cast` built-in functions.
// XX means the return type of the `cast` built-in functions.
// XX contains the following 6 types:
// Int, Decimal, Real, String.
// We implement 6 CastYYAsXXSig built-in function signatures for every CastAsXXFunctionClass.
// builtinCastXXAsYYSig takes a argument of type XX and returns a value of type YY.
package expression
import (
"fmt"
"github.com/secretflow/scql/pkg/parser/ast"
"github.com/secretflow/scql/pkg/parser/charset"
"github.com/secretflow/scql/pkg/parser/model"
"github.com/secretflow/scql/pkg/parser/mysql"
"github.com/secretflow/scql/pkg/parser/terror"
"github.com/secretflow/scql/pkg/sessionctx"
"github.com/secretflow/scql/pkg/types"
"github.com/secretflow/scql/pkg/util/chunk"
)
var (
_ functionClass = &castAsIntFunctionClass{}
_ functionClass = &castAsRealFunctionClass{}
_ functionClass = &castAsStringFunctionClass{}
_ functionClass = &castAsDecimalFunctionClass{}
_ functionClass = &castAsTimeFunctionClass{}
)
var (
_ builtinFunc = &builtinCastIntAsIntSig{}
_ builtinFunc = &builtinCastIntAsRealSig{}
_ builtinFunc = &builtinCastIntAsStringSig{}
_ builtinFunc = &builtinCastIntAsDecimalSig{}
_ builtinFunc = &builtinCastIntAsTimeSig{}
_ builtinFunc = &builtinCastRealAsIntSig{}
_ builtinFunc = &builtinCastRealAsRealSig{}
_ builtinFunc = &builtinCastRealAsStringSig{}
_ builtinFunc = &builtinCastRealAsDecimalSig{}
_ builtinFunc = &builtinCastRealAsTimeSig{}
_ builtinFunc = &builtinCastDecimalAsIntSig{}
_ builtinFunc = &builtinCastDecimalAsRealSig{}
_ builtinFunc = &builtinCastDecimalAsStringSig{}
_ builtinFunc = &builtinCastDecimalAsDecimalSig{}
_ builtinFunc = &builtinCastDecimalAsTimeSig{}
_ builtinFunc = &builtinCastStringAsIntSig{}
_ builtinFunc = &builtinCastStringAsRealSig{}
_ builtinFunc = &builtinCastStringAsStringSig{}
_ builtinFunc = &builtinCastStringAsDecimalSig{}
_ builtinFunc = &builtinCastStringAsTimeSig{}
_ builtinFunc = &builtinCastTimeAsIntSig{}
_ builtinFunc = &builtinCastTimeAsRealSig{}
_ builtinFunc = &builtinCastTimeAsStringSig{}
_ builtinFunc = &builtinCastTimeAsDecimalSig{}
_ builtinFunc = &builtinCastTimeAsTimeSig{}
)
type castAsIntFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsIntFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinCastFunc(newBaseBuiltinFunc(ctx, args), ctx.Value(inUnionCastContext) != nil)
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
sig = &builtinCastIntAsIntSig{bf}
return sig, nil
}
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsIntSig{bf}
case types.ETReal:
sig = &builtinCastRealAsIntSig{bf}
case types.ETDecimal:
sig = &builtinCastDecimalAsIntSig{bf}
case types.ETString:
sig = &builtinCastStringAsIntSig{bf}
default:
panic("unsupported types.EvalType in castAsIntFunctionClass")
}
return sig, nil
}
type castAsRealFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsRealFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinCastFunc(newBaseBuiltinFunc(ctx, args), ctx.Value(inUnionCastContext) != nil)
bf.tp = c.tp
if IsBinaryLiteral(args[0]) {
sig = &builtinCastRealAsRealSig{bf}
return sig, nil
}
var argTp types.EvalType
if args[0].GetType().Hybrid() {
argTp = types.ETInt
} else {
argTp = args[0].GetType().EvalType()
}
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsRealSig{bf}
case types.ETReal:
sig = &builtinCastRealAsRealSig{bf}
case types.ETDecimal:
sig = &builtinCastDecimalAsRealSig{bf}
case types.ETString:
sig = &builtinCastStringAsRealSig{bf}
default:
panic("unsupported types.EvalType in castAsRealFunctionClass")
}
return sig, nil
}
type castAsDecimalFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsDecimalFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinCastFunc(newBaseBuiltinFunc(ctx, args), ctx.Value(inUnionCastContext) != nil)
bf.tp = c.tp
if IsBinaryLiteral(args[0]) {
sig = &builtinCastDecimalAsDecimalSig{bf}
return sig, nil
}
var argTp types.EvalType
if args[0].GetType().Hybrid() {
argTp = types.ETInt
} else {
argTp = args[0].GetType().EvalType()
}
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsDecimalSig{bf}
case types.ETReal:
sig = &builtinCastRealAsDecimalSig{bf}
case types.ETDecimal:
sig = &builtinCastDecimalAsDecimalSig{bf}
case types.ETString:
sig = &builtinCastStringAsDecimalSig{bf}
default:
panic("unsupported types.EvalType in castAsDecimalFunctionClass")
}
return sig, nil
}
type castAsStringFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsStringFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
if args[0].GetType().Hybrid() || IsBinaryLiteral(args[0]) {
sig = &builtinCastStringAsStringSig{bf}
return sig, nil
}
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsStringSig{bf}
case types.ETReal:
sig = &builtinCastRealAsStringSig{bf}
case types.ETDecimal:
sig = &builtinCastDecimalAsStringSig{bf}
case types.ETString:
sig = &builtinCastStringAsStringSig{bf}
default:
panic("unsupported types.EvalType in castAsStringFunctionClass")
}
return sig, nil
}
type builtinCastIntAsIntSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastIntAsIntSig) Clone() builtinFunc {
newSig := &builtinCastIntAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastIntAsRealSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastIntAsRealSig) Clone() builtinFunc {
newSig := &builtinCastIntAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastIntAsDecimalSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastIntAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastIntAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastIntAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsStringSig) Clone() builtinFunc {
newSig := &builtinCastIntAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
type builtinCastRealAsRealSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastRealAsRealSig) Clone() builtinFunc {
newSig := &builtinCastRealAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastRealAsIntSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastRealAsIntSig) Clone() builtinFunc {
newSig := &builtinCastRealAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastRealAsDecimalSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastRealAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastRealAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastRealAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsStringSig) Clone() builtinFunc {
newSig := &builtinCastRealAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
type builtinCastDecimalAsDecimalSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastDecimalAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastDecimalAsIntSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastDecimalAsIntSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastDecimalAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsStringSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
type builtinCastDecimalAsRealSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastDecimalAsRealSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastStringAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsStringSig) Clone() builtinFunc {
newSig := &builtinCastStringAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
type builtinCastStringAsIntSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastStringAsIntSig) Clone() builtinFunc {
newSig := &builtinCastStringAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastStringAsRealSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastStringAsRealSig) Clone() builtinFunc {
newSig := &builtinCastStringAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
type builtinCastStringAsDecimalSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastStringAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastStringAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
// inCastContext is session key type that indicates whether executing
// in special cast context that negative unsigned num will be zero.
type inCastContext int
func (i inCastContext) String() string {
return "__cast_ctx"
}
// inUnionCastContext is session key value that indicates whether executing in
// union cast context.
// @see BuildCastFunction4Union
const inUnionCastContext inCastContext = 0
// hasSpecialCast checks if this expr has its own special cast function.
// for example(#9713): when doing arithmetic using results of function DayName,
// "Monday" should be regarded as 0, "Tuesday" should be regarded as 1 and so on.
func hasSpecialCast(ctx sessionctx.Context, expr Expression, tp *types.FieldType) bool {
switch f := expr.(type) {
case *ScalarFunction:
switch f.FuncName.L {
case ast.DayName:
switch tp.EvalType() {
case types.ETInt, types.ETReal:
return true
}
}
}
return false
}
// BuildCastFunction4Union build a implicitly CAST ScalarFunction from the Union
// Expression.
func BuildCastFunction4Union(ctx sessionctx.Context, expr Expression, tp *types.FieldType) (res Expression) {
ctx.SetValue(inUnionCastContext, struct{}{})
defer func() {
ctx.SetValue(inUnionCastContext, nil)
}()
return BuildCastFunction(ctx, expr, tp)
}
// BuildCastFunction builds a CAST ScalarFunction from the Expression.
func BuildCastFunction(ctx sessionctx.Context, expr Expression, tp *types.FieldType) (res Expression) {
if hasSpecialCast(ctx, expr, tp) {
return expr
}
var fc functionClass
switch tp.EvalType() {
case types.ETInt:
fc = &castAsIntFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETDecimal:
fc = &castAsDecimalFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETReal:
fc = &castAsRealFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETString:
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETDatetime, types.ETTimestamp:
fc = &castAsTimeFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
default:
terror.Log(fmt.Errorf("unsupported data type in cast: %+v", tp.EvalType()))
return
}
f, err := fc.getFunction(ctx, []Expression{expr})
terror.Log(err)
res = &ScalarFunction{
FuncName: model.NewCIStr(ast.Cast),
RetType: tp,
Function: f,
}
return res
}
// WrapWithCastAsInt wraps `expr` with `cast` if the return type of expr is not
// type int, otherwise, returns `expr` directly.
func WrapWithCastAsInt(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETInt {
return expr
}
tp := types.NewFieldType(mysql.TypeLonglong)
tp.Flen, tp.Decimal = expr.GetType().Flen, 0
types.SetBinChsClnFlag(tp)
tp.Flag |= expr.GetType().Flag & mysql.UnsignedFlag
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsReal wraps `expr` with `cast` if the return type of expr is not
// type real, otherwise, returns `expr` directly.
func WrapWithCastAsReal(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETReal {
return expr
}
tp := types.NewFieldType(mysql.TypeDouble)
tp.Flen, tp.Decimal = mysql.MaxRealWidth, types.UnspecifiedLength
types.SetBinChsClnFlag(tp)
tp.Flag |= expr.GetType().Flag & mysql.UnsignedFlag
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsDecimal wraps `expr` with `cast` if the return type of expr is
// not type decimal, otherwise, returns `expr` directly.
func WrapWithCastAsDecimal(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().EvalType() == types.ETDecimal {
return expr
}
tp := types.NewFieldType(mysql.TypeNewDecimal)
tp.Flen, tp.Decimal = expr.GetType().Flen, expr.GetType().Decimal
if expr.GetType().EvalType() == types.ETInt {
tp.Flen = mysql.MaxIntWidth
}
types.SetBinChsClnFlag(tp)
tp.Flag |= expr.GetType().Flag & mysql.UnsignedFlag
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsTime wraps `expr` with `cast` if the return type of expr is not
// same as type of the specified `tp` , otherwise, returns `expr` directly.
func WrapWithCastAsTime(ctx sessionctx.Context, expr Expression, tp *types.FieldType) Expression {
exprTp := expr.GetType().Tp
if tp.Tp == exprTp {
return expr
} else if (exprTp == mysql.TypeDate || exprTp == mysql.TypeTimestamp) && tp.Tp == mysql.TypeDatetime {
return expr
}
switch x := expr.GetType(); x.Tp {
case mysql.TypeDatetime, mysql.TypeTimestamp, mysql.TypeDate, mysql.TypeDuration:
tp.Decimal = x.Decimal
default:
tp.Decimal = int(types.MaxFsp)
}
switch tp.Tp {
case mysql.TypeDate:
tp.Flen = mysql.MaxDateWidth
case mysql.TypeDatetime, mysql.TypeTimestamp:
tp.Flen = mysql.MaxDatetimeWidthNoFsp
if tp.Decimal > 0 {
tp.Flen = tp.Flen + 1 + tp.Decimal
}
}
types.SetBinChsClnFlag(tp)
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsString wraps `expr` with `cast` if the return type of expr is
// not type string, otherwise, returns `expr` directly.
func WrapWithCastAsString(ctx sessionctx.Context, expr Expression) Expression {
exprTp := expr.GetType()
if exprTp.EvalType() == types.ETString {
return expr
}
argLen := exprTp.Flen
// If expr is decimal, we should take the decimal point and negative sign
// into consideration, so we set `expr.GetType().Flen + 2` as the `argLen`.
// Since the length of float and double is not accurate, we do not handle
// them.
if exprTp.Tp == mysql.TypeNewDecimal && argLen != int(types.UnspecifiedFsp) {
argLen += 2
}
if exprTp.EvalType() == types.ETInt {
argLen = mysql.MaxIntWidth
}
tp := types.NewFieldType(mysql.TypeVarString)
tp.Charset, tp.Collate = charset.GetDefaultCharsetAndCollate()
tp.Flen, tp.Decimal = argLen, types.UnspecifiedLength
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsDuration wraps `expr` with `cast` if the return type of expr is
// not type duration, otherwise, returns `expr` directly.
func WrapWithCastAsDuration(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().Tp == mysql.TypeDuration {
return expr
}
tp := types.NewFieldType(mysql.TypeDuration)
switch x := expr.GetType(); x.Tp {
case mysql.TypeDatetime, mysql.TypeTimestamp, mysql.TypeDate:
tp.Decimal = x.Decimal
default:
tp.Decimal = int(types.MaxFsp)
}
tp.Flen = mysql.MaxDurationWidthNoFsp
if tp.Decimal > 0 {
tp.Flen = tp.Flen + 1 + tp.Decimal
}
return BuildCastFunction(ctx, expr, tp)
}
// WrapWithCastAsJSON wraps `expr` with `cast` if the return type of expr is not
// type json, otherwise, returns `expr` directly.
func WrapWithCastAsJSON(ctx sessionctx.Context, expr Expression) Expression {
if expr.GetType().Tp == mysql.TypeJSON && !mysql.HasParseToJSONFlag(expr.GetType().Flag) {
return expr
}
tp := &types.FieldType{
Tp: mysql.TypeJSON,
Flen: 12582912, // FIXME: Here the Flen is not trusted.
Decimal: 0,
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
Flag: mysql.BinaryFlag,
}
return BuildCastFunction(ctx, expr, tp)
}
type castAsTimeFunctionClass struct {
baseFunctionClass
tp *types.FieldType
}
func (c *castAsTimeFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (sig builtinFunc, err error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinFunc(ctx, args)
bf.tp = c.tp
argTp := args[0].GetType().EvalType()
switch argTp {
case types.ETInt:
sig = &builtinCastIntAsTimeSig{bf}
case types.ETReal:
sig = &builtinCastRealAsTimeSig{bf}
case types.ETDecimal:
sig = &builtinCastDecimalAsTimeSig{bf}
case types.ETDatetime, types.ETTimestamp:
sig = &builtinCastTimeAsTimeSig{bf}
case types.ETString:
sig = &builtinCastStringAsTimeSig{bf}
default:
panic("unsupported types.EvalType in castAsTimeFunctionClass")
}
return sig, nil
}
type builtinCastIntAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastIntAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastIntAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastIntAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNull bool, err error) {
return res, false, nil
}
type builtinCastRealAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastRealAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastRealAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastRealAsTimeSig) evalTime(row chunk.Row) (types.Time, bool, error) {
return types.Time{}, false, nil
}
type builtinCastDecimalAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastDecimalAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastDecimalAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastDecimalAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNull bool, err error) {
return types.Time{}, false, nil
}
type builtinCastTimeAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNull bool, err error) {
res, isNull, err = b.args[0].EvalTime(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
}
sc := b.ctx.GetSessionVars().StmtCtx
if res, err = res.Convert(sc, b.tp.Tp); err != nil {
return types.ZeroTime, true, handleInvalidTimeError(b.ctx, err)
}
res, err = res.RoundFrac(sc, int8(b.tp.Decimal))
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.SetCoreTime(types.FromDate(res.Year(), res.Month(), res.Day(), 0, 0, 0, 0))
res.SetType(b.tp.Tp)
}
return res, false, err
}
type builtinCastStringAsTimeSig struct {
baseBuiltinFunc
}
func (b *builtinCastStringAsTimeSig) Clone() builtinFunc {
newSig := &builtinCastStringAsTimeSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastStringAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, val, b.tp.Tp, int8(b.tp.Decimal))
if err != nil {
return types.ZeroTime, true, handleInvalidTimeError(b.ctx, err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.SetCoreTime(types.FromDate(res.Year(), res.Month(), res.Day(), 0, 0, 0, 0))
}
return res, false, nil
}
type builtinCastTimeAsIntSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastTimeAsIntSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsIntSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
func (b *builtinCastTimeAsIntSig) evalInt(row chunk.Row) (res int64, isNull bool, err error) {
return 0, false, nil
}
type builtinCastTimeAsRealSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastTimeAsRealSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsRealSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
func (b *builtinCastTimeAsRealSig) evalReal(row chunk.Row) (res float64, isNull bool, err error) {
return 0.0, false, nil
}
type builtinCastTimeAsDecimalSig struct {
baseBuiltinCastFunc
}
func (b *builtinCastTimeAsDecimalSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsDecimalSig{}
newSig.cloneFrom(&b.baseBuiltinCastFunc)
return newSig
}
func (b *builtinCastTimeAsDecimalSig) evalDecimal(row chunk.Row) (res *types.MyDecimal, isNull bool, err error) {
return nil, false, nil
}
type builtinCastTimeAsStringSig struct {
baseBuiltinFunc
}
func (b *builtinCastTimeAsStringSig) Clone() builtinFunc {
newSig := &builtinCastTimeAsStringSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinCastTimeAsStringSig) evalString(row chunk.Row) (res string, isNull bool, err error) {
return "", false, nil
}