forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datum.go
1808 lines (1689 loc) · 46.8 KB
/
datum.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,
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/juju/errors"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/hack"
)
// Kind constants.
const (
KindNull byte = 0
KindInt64 byte = 1
KindUint64 byte = 2
KindFloat32 byte = 3
KindFloat64 byte = 4
KindString byte = 5
KindBytes byte = 6
KindBinaryLiteral byte = 7 // Used for BIT / HEX literals.
KindMysqlDecimal byte = 8
KindMysqlDuration byte = 9
KindMysqlEnum byte = 10
KindMysqlBit byte = 11 // Used for BIT table column values.
KindMysqlSet byte = 12
KindMysqlTime byte = 13
KindInterface byte = 14
KindMinNotNull byte = 15
KindMaxValue byte = 16
KindRaw byte = 17
KindMysqlJSON byte = 18
)
// Datum is a data box holds different kind of data.
// It has better performance and is easier to use than `interface{}`.
type Datum struct {
k byte // datum kind.
collation uint8 // collation can hold uint8 values.
decimal uint16 // decimal can hold uint16 values.
length uint32 // length can hold uint32 values.
i int64 // i can hold int64 uint64 float64 values.
b []byte // b can hold string or []byte values.
x interface{} // x hold all other types.
}
// Copy deep copies a Datum.
func (d *Datum) Copy() *Datum {
ret := *d
if d.b != nil {
ret.b = make([]byte, len(d.b))
copy(ret.b, d.b)
}
switch ret.Kind() {
case KindMysqlDecimal:
d := *d.GetMysqlDecimal()
ret.SetMysqlDecimal(&d)
case KindMysqlTime:
ret.SetMysqlTime(d.GetMysqlTime())
}
return &ret
}
// Kind gets the kind of the datum.
func (d *Datum) Kind() byte {
return d.k
}
// Collation gets the collation of the datum.
func (d *Datum) Collation() byte {
return d.collation
}
// SetCollation sets the collation of the datum.
func (d *Datum) SetCollation(collation byte) {
d.collation = collation
}
// Frac gets the frac of the datum.
func (d *Datum) Frac() int {
return int(d.decimal)
}
// SetFrac sets the frac of the datum.
func (d *Datum) SetFrac(frac int) {
d.decimal = uint16(frac)
}
// Length gets the length of the datum.
func (d *Datum) Length() int {
return int(d.length)
}
// SetLength sets the length of the datum
func (d *Datum) SetLength(l int) {
d.length = uint32(l)
}
// IsNull checks if datum is null.
func (d *Datum) IsNull() bool {
return d.k == KindNull
}
// GetInt64 gets int64 value.
func (d *Datum) GetInt64() int64 {
return d.i
}
// SetInt64 sets int64 value.
func (d *Datum) SetInt64(i int64) {
d.k = KindInt64
d.i = i
}
// GetUint64 gets uint64 value.
func (d *Datum) GetUint64() uint64 {
return uint64(d.i)
}
// SetUint64 sets uint64 value.
func (d *Datum) SetUint64(i uint64) {
d.k = KindUint64
d.i = int64(i)
}
// GetFloat64 gets float64 value.
func (d *Datum) GetFloat64() float64 {
return math.Float64frombits(uint64(d.i))
}
// SetFloat64 sets float64 value.
func (d *Datum) SetFloat64(f float64) {
d.k = KindFloat64
d.i = int64(math.Float64bits(f))
}
// GetFloat32 gets float32 value.
func (d *Datum) GetFloat32() float32 {
return float32(math.Float64frombits(uint64(d.i)))
}
// SetFloat32 sets float32 value.
func (d *Datum) SetFloat32(f float32) {
d.k = KindFloat32
d.i = int64(math.Float64bits(float64(f)))
}
// GetString gets string value.
func (d *Datum) GetString() string {
return hack.String(d.b)
}
// SetString sets string value.
func (d *Datum) SetString(s string) {
d.k = KindString
sink(s)
d.b = hack.Slice(s)
}
// sink prevents s from being allocated on the stack.
var sink = func(s string) {
}
// GetBytes gets bytes value.
func (d *Datum) GetBytes() []byte {
return d.b
}
// SetBytes sets bytes value to datum.
func (d *Datum) SetBytes(b []byte) {
d.k = KindBytes
d.b = b
}
// SetBytesAsString sets bytes value to datum as string type.
func (d *Datum) SetBytesAsString(b []byte) {
d.k = KindString
d.b = b
}
// GetInterface gets interface value.
func (d *Datum) GetInterface() interface{} {
return d.x
}
// SetInterface sets interface to datum.
func (d *Datum) SetInterface(x interface{}) {
d.k = KindInterface
d.x = x
}
// SetNull sets datum to nil.
func (d *Datum) SetNull() {
d.k = KindNull
d.x = nil
}
// GetBinaryLiteral gets Bit value
func (d *Datum) GetBinaryLiteral() BinaryLiteral {
return d.b
}
// GetMysqlBit gets MysqlBit value
func (d *Datum) GetMysqlBit() BinaryLiteral {
return d.GetBinaryLiteral()
}
// SetBinaryLiteral sets Bit value
func (d *Datum) SetBinaryLiteral(b BinaryLiteral) {
d.k = KindBinaryLiteral
d.b = b
}
// SetMysqlBit sets MysqlBit value
func (d *Datum) SetMysqlBit(b BinaryLiteral) {
d.k = KindMysqlBit
d.b = b
}
// GetMysqlDecimal gets Decimal value
func (d *Datum) GetMysqlDecimal() *MyDecimal {
return d.x.(*MyDecimal)
}
// SetMysqlDecimal sets Decimal value
func (d *Datum) SetMysqlDecimal(b *MyDecimal) {
d.k = KindMysqlDecimal
d.x = b
}
// GetMysqlDuration gets Duration value
func (d *Datum) GetMysqlDuration() Duration {
return Duration{Duration: time.Duration(d.i), Fsp: int(d.decimal)}
}
// SetMysqlDuration sets Duration value
func (d *Datum) SetMysqlDuration(b Duration) {
d.k = KindMysqlDuration
d.i = int64(b.Duration)
d.decimal = uint16(b.Fsp)
}
// GetMysqlEnum gets Enum value
func (d *Datum) GetMysqlEnum() Enum {
return Enum{Value: uint64(d.i), Name: hack.String(d.b)}
}
// SetMysqlEnum sets Enum value
func (d *Datum) SetMysqlEnum(b Enum) {
d.k = KindMysqlEnum
d.i = int64(b.Value)
sink(b.Name)
d.b = hack.Slice(b.Name)
}
// GetMysqlSet gets Set value
func (d *Datum) GetMysqlSet() Set {
return Set{Value: uint64(d.i), Name: hack.String(d.b)}
}
// SetMysqlSet sets Set value
func (d *Datum) SetMysqlSet(b Set) {
d.k = KindMysqlSet
d.i = int64(b.Value)
sink(b.Name)
d.b = hack.Slice(b.Name)
}
// GetMysqlJSON gets json.BinaryJSON value
func (d *Datum) GetMysqlJSON() json.BinaryJSON {
return json.BinaryJSON{TypeCode: byte(d.i), Value: d.b}
}
// SetMysqlJSON sets json.BinaryJSON value
func (d *Datum) SetMysqlJSON(b json.BinaryJSON) {
d.k = KindMysqlJSON
d.i = int64(b.TypeCode)
d.b = b.Value
}
// GetMysqlTime gets types.Time value
func (d *Datum) GetMysqlTime() Time {
return d.x.(Time)
}
// SetMysqlTime sets types.Time value
func (d *Datum) SetMysqlTime(b Time) {
d.k = KindMysqlTime
d.x = b
}
// SetRaw sets raw value.
func (d *Datum) SetRaw(b []byte) {
d.k = KindRaw
d.b = b
}
// GetRaw gets raw value.
func (d *Datum) GetRaw() []byte {
return d.b
}
// GetValue gets the value of the datum of any kind.
func (d *Datum) GetValue() interface{} {
switch d.k {
case KindInt64:
return d.GetInt64()
case KindUint64:
return d.GetUint64()
case KindFloat32:
return d.GetFloat32()
case KindFloat64:
return d.GetFloat64()
case KindString:
return d.GetString()
case KindBytes:
return d.GetBytes()
case KindMysqlDecimal:
return d.GetMysqlDecimal()
case KindMysqlDuration:
return d.GetMysqlDuration()
case KindMysqlEnum:
return d.GetMysqlEnum()
case KindBinaryLiteral, KindMysqlBit:
return d.GetBinaryLiteral()
case KindMysqlSet:
return d.GetMysqlSet()
case KindMysqlJSON:
return d.GetMysqlJSON()
case KindMysqlTime:
return d.GetMysqlTime()
default:
return d.GetInterface()
}
}
// SetValue sets any kind of value.
func (d *Datum) SetValue(val interface{}) {
switch x := val.(type) {
case nil:
d.SetNull()
case bool:
if x {
d.SetInt64(1)
} else {
d.SetInt64(0)
}
case int:
d.SetInt64(int64(x))
case int64:
d.SetInt64(x)
case uint64:
d.SetUint64(x)
case float32:
d.SetFloat32(x)
case float64:
d.SetFloat64(x)
case string:
d.SetString(x)
case []byte:
d.SetBytes(x)
case *MyDecimal:
d.SetMysqlDecimal(x)
case Duration:
d.SetMysqlDuration(x)
case Enum:
d.SetMysqlEnum(x)
case BinaryLiteral:
d.SetBinaryLiteral(x)
case BitLiteral: // Store as BinaryLiteral for Bit and Hex literals
d.SetBinaryLiteral(BinaryLiteral(x))
case HexLiteral:
d.SetBinaryLiteral(BinaryLiteral(x))
case Set:
d.SetMysqlSet(x)
case json.BinaryJSON:
d.SetMysqlJSON(x)
case Time:
d.SetMysqlTime(x)
default:
d.SetInterface(x)
}
}
// CompareDatum compares datum to another datum.
// TODO: return error properly.
func (d *Datum) CompareDatum(sc *stmtctx.StatementContext, ad *Datum) (int, error) {
if d.k == KindMysqlJSON && ad.k != KindMysqlJSON {
cmp, err := ad.CompareDatum(sc, d)
return cmp * -1, errors.Trace(err)
}
switch ad.k {
case KindNull:
if d.k == KindNull {
return 0, nil
}
return 1, nil
case KindMinNotNull:
if d.k == KindNull {
return -1, nil
} else if d.k == KindMinNotNull {
return 0, nil
}
return 1, nil
case KindMaxValue:
if d.k == KindMaxValue {
return 0, nil
}
return -1, nil
case KindInt64:
return d.compareInt64(sc, ad.GetInt64())
case KindUint64:
return d.compareUint64(sc, ad.GetUint64())
case KindFloat32, KindFloat64:
return d.compareFloat64(sc, ad.GetFloat64())
case KindString:
return d.compareString(sc, ad.GetString())
case KindBytes:
return d.compareBytes(sc, ad.GetBytes())
case KindMysqlDecimal:
return d.compareMysqlDecimal(sc, ad.GetMysqlDecimal())
case KindMysqlDuration:
return d.compareMysqlDuration(sc, ad.GetMysqlDuration())
case KindMysqlEnum:
return d.compareMysqlEnum(sc, ad.GetMysqlEnum())
case KindBinaryLiteral, KindMysqlBit:
return d.compareBinaryLiteral(sc, ad.GetBinaryLiteral())
case KindMysqlSet:
return d.compareMysqlSet(sc, ad.GetMysqlSet())
case KindMysqlJSON:
return d.compareMysqlJSON(sc, ad.GetMysqlJSON())
case KindMysqlTime:
return d.compareMysqlTime(sc, ad.GetMysqlTime())
default:
return 0, nil
}
}
func (d *Datum) compareInt64(sc *stmtctx.StatementContext, i int64) (int, error) {
switch d.k {
case KindMaxValue:
return 1, nil
case KindInt64:
return CompareInt64(d.i, i), nil
case KindUint64:
if i < 0 || d.GetUint64() > math.MaxInt64 {
return 1, nil
}
return CompareInt64(d.i, i), nil
default:
return d.compareFloat64(sc, float64(i))
}
}
func (d *Datum) compareUint64(sc *stmtctx.StatementContext, u uint64) (int, error) {
switch d.k {
case KindMaxValue:
return 1, nil
case KindInt64:
if d.i < 0 || u > math.MaxInt64 {
return -1, nil
}
return CompareInt64(d.i, int64(u)), nil
case KindUint64:
return CompareUint64(d.GetUint64(), u), nil
default:
return d.compareFloat64(sc, float64(u))
}
}
func (d *Datum) compareFloat64(sc *stmtctx.StatementContext, f float64) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindInt64:
return CompareFloat64(float64(d.i), f), nil
case KindUint64:
return CompareFloat64(float64(d.GetUint64()), f), nil
case KindFloat32, KindFloat64:
return CompareFloat64(d.GetFloat64(), f), nil
case KindString, KindBytes:
fVal, err := StrToFloat(sc, d.GetString())
return CompareFloat64(fVal, f), errors.Trace(err)
case KindMysqlDecimal:
fVal, err := d.GetMysqlDecimal().ToFloat64()
return CompareFloat64(fVal, f), errors.Trace(err)
case KindMysqlDuration:
fVal := d.GetMysqlDuration().Seconds()
return CompareFloat64(fVal, f), nil
case KindMysqlEnum:
fVal := d.GetMysqlEnum().ToNumber()
return CompareFloat64(fVal, f), nil
case KindBinaryLiteral, KindMysqlBit:
val, err := d.GetBinaryLiteral().ToInt()
fVal := float64(val)
return CompareFloat64(fVal, f), errors.Trace(err)
case KindMysqlSet:
fVal := d.GetMysqlSet().ToNumber()
return CompareFloat64(fVal, f), nil
case KindMysqlTime:
fVal, err := d.GetMysqlTime().ToNumber().ToFloat64()
return CompareFloat64(fVal, f), errors.Trace(err)
default:
return -1, nil
}
}
func (d *Datum) compareString(sc *stmtctx.StatementContext, s string) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindString, KindBytes:
return CompareString(d.GetString(), s), nil
case KindMysqlDecimal:
dec := new(MyDecimal)
err := sc.HandleTruncate(dec.FromString(hack.Slice(s)))
return d.GetMysqlDecimal().Compare(dec), errors.Trace(err)
case KindMysqlTime:
dt, err := ParseDatetime(sc, s)
return d.GetMysqlTime().Compare(dt), errors.Trace(err)
case KindMysqlDuration:
dur, err := ParseDuration(s, MaxFsp)
return d.GetMysqlDuration().Compare(dur), errors.Trace(err)
case KindMysqlSet:
return CompareString(d.GetMysqlSet().String(), s), nil
case KindMysqlEnum:
return CompareString(d.GetMysqlEnum().String(), s), nil
case KindBinaryLiteral, KindMysqlBit:
return CompareString(d.GetBinaryLiteral().ToString(), s), nil
default:
fVal, err := StrToFloat(sc, s)
if err != nil {
return 0, errors.Trace(err)
}
return d.compareFloat64(sc, fVal)
}
}
func (d *Datum) compareBytes(sc *stmtctx.StatementContext, b []byte) (int, error) {
return d.compareString(sc, hack.String(b))
}
func (d *Datum) compareMysqlDecimal(sc *stmtctx.StatementContext, dec *MyDecimal) (int, error) {
switch d.k {
case KindMysqlDecimal:
return d.GetMysqlDecimal().Compare(dec), nil
case KindString, KindBytes:
dDec := new(MyDecimal)
err := sc.HandleTruncate(dDec.FromString(d.GetBytes()))
return dDec.Compare(dec), errors.Trace(err)
default:
fVal, err := dec.ToFloat64()
if err != nil {
return 0, errors.Trace(err)
}
return d.compareFloat64(sc, fVal)
}
}
func (d *Datum) compareMysqlDuration(sc *stmtctx.StatementContext, dur Duration) (int, error) {
switch d.k {
case KindMysqlDuration:
return d.GetMysqlDuration().Compare(dur), nil
case KindString, KindBytes:
dDur, err := ParseDuration(d.GetString(), MaxFsp)
return dDur.Compare(dur), errors.Trace(err)
default:
return d.compareFloat64(sc, dur.Seconds())
}
}
func (d *Datum) compareMysqlEnum(sc *stmtctx.StatementContext, enum Enum) (int, error) {
switch d.k {
case KindString, KindBytes:
return CompareString(d.GetString(), enum.String()), nil
default:
return d.compareFloat64(sc, enum.ToNumber())
}
}
func (d *Datum) compareBinaryLiteral(sc *stmtctx.StatementContext, b BinaryLiteral) (int, error) {
switch d.k {
case KindString, KindBytes:
return CompareString(d.GetString(), b.ToString()), nil
case KindBinaryLiteral, KindMysqlBit:
return CompareString(d.GetBinaryLiteral().ToString(), b.ToString()), nil
default:
val, err := b.ToInt()
if err != nil {
return 0, errors.Trace(err)
}
result, err := d.compareFloat64(sc, float64(val))
return result, errors.Trace(err)
}
}
func (d *Datum) compareMysqlSet(sc *stmtctx.StatementContext, set Set) (int, error) {
switch d.k {
case KindString, KindBytes:
return CompareString(d.GetString(), set.String()), nil
default:
return d.compareFloat64(sc, set.ToNumber())
}
}
func (d *Datum) compareMysqlJSON(sc *stmtctx.StatementContext, target json.BinaryJSON) (int, error) {
origin, err := d.ToMysqlJSON()
if err != nil {
return 0, errors.Trace(err)
}
return json.CompareBinary(origin, target), nil
}
func (d *Datum) compareMysqlTime(sc *stmtctx.StatementContext, time Time) (int, error) {
switch d.k {
case KindString, KindBytes:
dt, err := ParseDatetime(sc, d.GetString())
return dt.Compare(time), errors.Trace(err)
case KindMysqlTime:
return d.GetMysqlTime().Compare(time), nil
default:
fVal, err := time.ToNumber().ToFloat64()
if err != nil {
return 0, errors.Trace(err)
}
return d.compareFloat64(sc, fVal)
}
}
// ConvertTo converts a datum to the target field type.
func (d *Datum) ConvertTo(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
if d.k == KindNull {
return Datum{}, nil
}
switch target.Tp { // TODO: implement mysql types convert when "CAST() AS" syntax are supported.
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
unsigned := mysql.HasUnsignedFlag(target.Flag)
if unsigned {
return d.convertToUint(sc, target)
}
return d.convertToInt(sc, target)
case mysql.TypeFloat, mysql.TypeDouble:
return d.convertToFloat(sc, target)
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob,
mysql.TypeString, mysql.TypeVarchar, mysql.TypeVarString:
return d.convertToString(sc, target)
case mysql.TypeTimestamp:
return d.convertToMysqlTimestamp(sc, target)
case mysql.TypeDatetime, mysql.TypeDate:
return d.convertToMysqlTime(sc, target)
case mysql.TypeDuration:
return d.convertToMysqlDuration(sc, target)
case mysql.TypeNewDecimal:
return d.convertToMysqlDecimal(sc, target)
case mysql.TypeYear:
return d.convertToMysqlYear(sc, target)
case mysql.TypeEnum:
return d.convertToMysqlEnum(sc, target)
case mysql.TypeBit:
return d.convertToMysqlBit(sc, target)
case mysql.TypeSet:
return d.convertToMysqlSet(sc, target)
case mysql.TypeJSON:
return d.convertToMysqlJSON(sc, target)
case mysql.TypeNull:
return Datum{}, nil
default:
panic("should never happen")
}
}
func (d *Datum) convertToFloat(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
var (
f float64
ret Datum
err error
)
switch d.k {
case KindNull:
return ret, nil
case KindInt64:
f = float64(d.GetInt64())
case KindUint64:
f = float64(d.GetUint64())
case KindFloat32, KindFloat64:
f = d.GetFloat64()
case KindString, KindBytes:
f, err = StrToFloat(sc, d.GetString())
case KindMysqlTime:
f, err = d.GetMysqlTime().ToNumber().ToFloat64()
case KindMysqlDuration:
f, err = d.GetMysqlDuration().ToNumber().ToFloat64()
case KindMysqlDecimal:
f, err = d.GetMysqlDecimal().ToFloat64()
case KindMysqlSet:
f = d.GetMysqlSet().ToNumber()
case KindMysqlEnum:
f = d.GetMysqlEnum().ToNumber()
case KindBinaryLiteral, KindMysqlBit:
val, err1 := d.GetBinaryLiteral().ToInt()
f, err = float64(val), err1
case KindMysqlJSON:
f, err = ConvertJSONToFloat(sc, d.GetMysqlJSON())
default:
return invalidConv(d, target.Tp)
}
var err1 error
f, err1 = ProduceFloatWithSpecifiedTp(f, target, sc)
if err == nil && err1 != nil {
err = err1
}
if target.Tp == mysql.TypeFloat {
ret.SetFloat32(float32(f))
} else {
ret.SetFloat64(f)
}
return ret, errors.Trace(err)
}
// ProduceFloatWithSpecifiedTp produces a new float64 according to `flen` and `decimal`.
func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.StatementContext) (_ float64, err error) {
// For float and following double type, we will only truncate it for float(M, D) format.
// If no D is set, we will handle it like origin float whether M is set or not.
if target.Flen != UnspecifiedLength && target.Decimal != UnspecifiedLength {
f, err = TruncateFloat(f, target.Flen, target.Decimal)
err = sc.HandleOverflow(err, err)
}
return f, errors.Trace(err)
}
func (d *Datum) convertToString(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
var ret Datum
var s string
switch d.k {
case KindInt64:
s = strconv.FormatInt(d.GetInt64(), 10)
case KindUint64:
s = strconv.FormatUint(d.GetUint64(), 10)
case KindFloat32:
s = strconv.FormatFloat(d.GetFloat64(), 'f', -1, 32)
case KindFloat64:
s = strconv.FormatFloat(d.GetFloat64(), 'f', -1, 64)
case KindString, KindBytes:
s = d.GetString()
case KindMysqlTime:
s = d.GetMysqlTime().String()
case KindMysqlDuration:
s = d.GetMysqlDuration().String()
case KindMysqlDecimal:
s = d.GetMysqlDecimal().String()
case KindMysqlEnum:
s = d.GetMysqlEnum().String()
case KindMysqlSet:
s = d.GetMysqlSet().String()
case KindBinaryLiteral, KindMysqlBit:
s = d.GetBinaryLiteral().ToString()
case KindMysqlJSON:
s = d.GetMysqlJSON().String()
default:
return invalidConv(d, target.Tp)
}
s, err := ProduceStrWithSpecifiedTp(s, target, sc)
ret.SetString(s)
if target.Charset == charset.CharsetBin {
ret.k = KindBytes
}
return ret, errors.Trace(err)
}
// ProduceStrWithSpecifiedTp produces a new string according to `flen` and `chs`.
func ProduceStrWithSpecifiedTp(s string, tp *FieldType, sc *stmtctx.StatementContext) (_ string, err error) {
flen, chs := tp.Flen, tp.Charset
if flen >= 0 {
// Flen is the rune length, not binary length, for UTF8 charset, we need to calculate the
// rune count and truncate to Flen runes if it is too long.
if chs == charset.CharsetUTF8 || chs == charset.CharsetUTF8MB4 {
characterLen := utf8.RuneCountInString(s)
if characterLen > flen {
// 1. If len(s) is 0 and flen is 0, truncateLen will be 0, don't truncate s.
// CREATE TABLE t (a char(0));
// INSERT INTO t VALUES (``);
// 2. If len(s) is 10 and flen is 0, truncateLen will be 0 too, but we still need to truncate s.
// SELECT 1, CAST(1234 AS CHAR(0));
// So truncateLen is not a suitable variable to determine to do truncate or not.
var runeCount int
var truncateLen int
for i := range s {
if runeCount == flen {
truncateLen = i
break
}
runeCount++
}
err = ErrDataTooLong.Gen("Data Too Long, field len %d, data len %d", flen, characterLen)
s = truncateStr(s, truncateLen)
}
} else if len(s) > flen {
err = ErrDataTooLong.Gen("Data Too Long, field len %d, data len %d", flen, len(s))
s = truncateStr(s, flen)
} else if tp.Tp == mysql.TypeString && IsBinaryStr(tp) && len(s) < flen {
padding := make([]byte, flen-len(s))
s = string(append([]byte(s), padding...))
}
}
return s, errors.Trace(sc.HandleTruncate(err))
}
func (d *Datum) convertToInt(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
i64, err := d.toSignedInteger(sc, target.Tp)
return NewIntDatum(i64), errors.Trace(err)
}
func (d *Datum) convertToUint(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
tp := target.Tp
upperBound := UnsignedUpperBound[tp]
var (
val uint64
err error
ret Datum
)
switch d.k {
case KindInt64:
val, err = ConvertIntToUint(d.GetInt64(), upperBound, tp)
case KindUint64:
val, err = ConvertUintToUint(d.GetUint64(), upperBound, tp)
case KindFloat32, KindFloat64:
val, err = ConvertFloatToUint(d.GetFloat64(), upperBound, tp)
case KindString, KindBytes:
val, err = StrToUint(sc, d.GetString())
if err != nil {
return ret, errors.Trace(err)
}
val, err = ConvertUintToUint(val, upperBound, tp)
if err != nil {
return ret, errors.Trace(err)
}
ret.SetUint64(val)
case KindMysqlTime:
dec := d.GetMysqlTime().ToNumber()
err = dec.Round(dec, 0, ModeHalfEven)
ival, err1 := dec.ToInt()
if err == nil {
err = err1
}
val, err1 = ConvertIntToUint(ival, upperBound, tp)
if err == nil {
err = err1
}
case KindMysqlDuration:
dec := d.GetMysqlDuration().ToNumber()
err = dec.Round(dec, 0, ModeHalfEven)
ival, err1 := dec.ToInt()
if err1 == nil {
val, err = ConvertIntToUint(ival, upperBound, tp)
}
case KindMysqlDecimal:
fval, err1 := d.GetMysqlDecimal().ToFloat64()
val, err = ConvertFloatToUint(fval, upperBound, tp)
if err == nil {
err = err1
}
case KindMysqlEnum:
val, err = ConvertFloatToUint(d.GetMysqlEnum().ToNumber(), upperBound, tp)
case KindMysqlSet:
val, err = ConvertFloatToUint(d.GetMysqlSet().ToNumber(), upperBound, tp)
case KindBinaryLiteral, KindMysqlBit:
val, err = d.GetBinaryLiteral().ToInt()
case KindMysqlJSON:
var i64 int64
i64, err = ConvertJSONToInt(sc, d.GetMysqlJSON(), true)
val = uint64(i64)
default:
return invalidConv(d, target.Tp)
}
ret.SetUint64(val)
if err != nil {
return ret, errors.Trace(err)
}
return ret, nil
}
func (d *Datum) convertToMysqlTimestamp(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
var (
ret Datum
t Time
err error
)
fsp := DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
switch d.k {
case KindMysqlTime:
t = d.GetMysqlTime()
t, err = t.RoundFrac(sc, fsp)
case KindMysqlDuration:
t, err = d.GetMysqlDuration().ConvertToTime(sc, mysql.TypeTimestamp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(sc, fsp)
case KindString, KindBytes:
t, err = ParseTime(sc, d.GetString(), mysql.TypeTimestamp, fsp)
case KindInt64:
t, err = ParseTimeFromNum(sc, d.GetInt64(), mysql.TypeTimestamp, fsp)
default:
return invalidConv(d, mysql.TypeTimestamp)
}
t.Type = mysql.TypeTimestamp
ret.SetMysqlTime(t)
if err != nil {
return ret, errors.Trace(err)
}
return ret, nil
}
func (d *Datum) convertToMysqlTime(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
tp := target.Tp
fsp := DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
var (
ret Datum
t Time
err error
)
switch d.k {
case KindMysqlTime:
t, err = d.GetMysqlTime().Convert(sc, tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(sc, fsp)
case KindMysqlDuration:
t, err = d.GetMysqlDuration().ConvertToTime(sc, tp)
if err != nil {
ret.SetValue(t)
return ret, errors.Trace(err)
}
t, err = t.RoundFrac(sc, fsp)
case KindString, KindBytes:
t, err = ParseTime(sc, d.GetString(), tp, fsp)
case KindInt64:
t, err = ParseTimeFromNum(sc, d.GetInt64(), tp, fsp)
default:
return invalidConv(d, tp)
}
if tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
t.Time = FromDate(t.Time.Year(), t.Time.Month(), t.Time.Day(), 0, 0, 0, 0)
}
ret.SetValue(t)
if err != nil {
return ret, errors.Trace(err)
}
return ret, nil
}
func (d *Datum) convertToMysqlDuration(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {
tp := target.Tp
fsp := DefaultFsp
if target.Decimal != UnspecifiedLength {
fsp = target.Decimal
}
var ret Datum
switch d.k {
case KindMysqlTime:
dur, err := d.GetMysqlTime().ConvertToDuration()
if err != nil {
ret.SetValue(dur)
return ret, errors.Trace(err)
}