forked from src-d/go-mysql-server
/
type.go
734 lines (610 loc) · 16.1 KB
/
type.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
package sql
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/spf13/cast"
"gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-vitess.v0/sqltypes"
"gopkg.in/src-d/go-vitess.v0/vt/proto/query"
)
var (
// ErrTypeNotSupported is thrown when a specific type is not supported
ErrTypeNotSupported = errors.NewKind("Type not supported: %s")
// ErrUnexpectedType is thrown when a received type is not the expected
ErrUnexpectedType = errors.NewKind("value at %d has unexpected type: %s")
// ErrConvertingToTime is thrown when a value cannot be converted to a Time
ErrConvertingToTime = errors.NewKind("value %q can't be converted to time.Time")
// ErrValueNotNil is thrown when a value that was expected to be nil, is not
ErrValueNotNil = errors.NewKind("value not nil: %#v")
// ErrNotTuple is retuned when the value is not a tuple.
ErrNotTuple = errors.NewKind("value of type %T is not a tuple")
// ErrInvalidColumnNumber is returned when a tuple has an invalid number of
// arguments.
ErrInvalidColumnNumber = errors.NewKind("tuple should contain %d column(s), but has %d")
// ErrNotArray is returned when the value is not an array.
ErrNotArray = errors.NewKind("value of type %T is not an array")
)
// Schema is the definition of a table.
type Schema []*Column
// CheckRow checks the row conforms to the schema.
func (s Schema) CheckRow(row Row) error {
expected := len(s)
got := len(row)
if expected != got {
return ErrUnexpectedRowLength.New(expected, got)
}
for idx, f := range s {
v := row[idx]
if f.Check(v) {
continue
}
typ := reflect.TypeOf(v).String()
return ErrUnexpectedType.New(idx, typ)
}
return nil
}
// Contains returns whether the schema contains a column with the given name.
func (s Schema) Contains(column string, source string) bool {
return s.IndexOf(column, source) >= 0
}
// IndexOf returns the index of the given column in the schema or -1 if it's
// not present.
func (s Schema) IndexOf(column, source string) int {
for i, col := range s {
if col.Name == column && col.Source == source {
return i
}
}
return -1
}
// Equals checks whether the given schema is equal to this one.
func (s Schema) Equals(s2 Schema) bool {
if len(s) != len(s2) {
return false
}
for i := range s {
if !s[i].Equals(s2[i]) {
return false
}
}
return true
}
// Column is the definition of a table column.
// As SQL:2016 puts it:
// A column is a named component of a table. It has a data type, a default,
// and a nullability characteristic.
type Column struct {
// Name is the name of the column.
Name string
// Type is the data type of the column.
Type Type
// Default contains the default value of the column or nil if it is NULL.
Default interface{}
// Nullable is true if the column can contain NULL values, or false
// otherwise.
Nullable bool
// Source is the name of the table this column came from.
Source string
}
// Check ensures the value is correct for this column.
func (c *Column) Check(v interface{}) bool {
if v == nil {
return c.Nullable
}
_, err := c.Type.Convert(v)
return err == nil
}
// Equals checks whether two columns are equal.
func (c *Column) Equals(c2 *Column) bool {
return c.Name == c2.Name &&
c.Source == c2.Source &&
c.Nullable == c2.Nullable &&
reflect.DeepEqual(c.Default, c2.Default) &&
reflect.DeepEqual(c.Type, c2.Type)
}
// Type represent a SQL type.
type Type interface {
// Type returns the query.Type for the given Type.
Type() query.Type
// Covert a value of a compatible type to a most accurate type.
Convert(interface{}) (interface{}, error)
// Compare returns an integer comparing two values.
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
Compare(interface{}, interface{}) (int, error)
// SQL returns the sqltypes.Value for the given value.
SQL(interface{}) sqltypes.Value
}
var (
// Null represents the null type.
Null nullT
// Numeric types
// Int32 is an integer of 32 bits.
Int32 = numberT{t: sqltypes.Int32}
// Int64 is an integer of 64 bytes.
Int64 = numberT{t: sqltypes.Int64}
// Uint32 is an unsigned integer of 32 bytes.
Uint32 = numberT{t: sqltypes.Uint32}
// Uint64 is an unsigned integer of 64 bytes.
Uint64 = numberT{t: sqltypes.Uint64}
// Float32 is a floating point number of 32 bytes.
Float32 = numberT{t: sqltypes.Float32}
// Float64 is a floating point number of 64 bytes.
Float64 = numberT{t: sqltypes.Float64}
// Timestamp is an UNIX timestamp.
Timestamp timestampT
// Date is a date with day, month and year.
Date dateT
// Text is a string type.
Text textT
// Boolean is a boolean type.
Boolean booleanT
// JSON is a type that holds any valid JSON object.
JSON jsonT
// Blob is a type that holds a chunk of binary data.
Blob blobT
)
// Tuple returns a new tuple type with the given element types.
func Tuple(types ...Type) Type {
return tupleT(types)
}
// Array returns a new Array type of the given underlying type.
func Array(underlying Type) Type {
return arrayT{underlying}
}
// MysqlTypeToType gets the column type using the mysql type
func MysqlTypeToType(sql query.Type) (Type, error) {
switch sql {
case sqltypes.Null:
return Null, nil
case sqltypes.Int32:
return Int32, nil
case sqltypes.Int64:
return Int64, nil
case sqltypes.Uint32:
return Uint32, nil
case sqltypes.Uint64:
return Uint64, nil
case sqltypes.Float32:
return Float32, nil
case sqltypes.Float64:
return Float64, nil
case sqltypes.Timestamp:
return Timestamp, nil
case sqltypes.Date:
return Date, nil
case sqltypes.Text, sqltypes.VarChar:
return Text, nil
case sqltypes.Bit:
return Boolean, nil
case sqltypes.TypeJSON:
return JSON, nil
case sqltypes.Blob:
return Blob, nil
default:
return nil, ErrTypeNotSupported.New(sql)
}
}
type nullT struct{}
// Type implements Type interface.
func (t nullT) Type() query.Type {
return sqltypes.Null
}
// SQL implements Type interface.
func (t nullT) SQL(interface{}) sqltypes.Value {
return sqltypes.NULL
}
// Convert implements Type interface.
func (t nullT) Convert(v interface{}) (interface{}, error) {
if v != nil {
return nil, ErrValueNotNil.New(v)
}
return nil, nil
}
// Compare implements Type interface. Note that while this returns 0 (equals)
// for ordering purposes, in SQL NULL != NULL.
func (t nullT) Compare(a interface{}, b interface{}) (int, error) {
return 0, nil
}
type numberT struct {
t query.Type
}
// Type implements Type interface.
func (t numberT) Type() query.Type {
return t.t
}
// SQL implements Type interface.
func (t numberT) SQL(v interface{}) sqltypes.Value {
return sqltypes.MakeTrusted(t.t, strconv.AppendInt(nil, cast.ToInt64(v), 10))
}
// Convert implements Type interface.
func (t numberT) Convert(v interface{}) (interface{}, error) {
switch t.t {
case sqltypes.Int32:
return cast.ToInt32E(v)
case sqltypes.Int64:
return cast.ToInt64E(v)
case sqltypes.Uint32:
return cast.ToUint32E(v)
case sqltypes.Uint64:
return cast.ToUint64E(v)
case sqltypes.Float32:
return cast.ToFloat32E(v)
case sqltypes.Float64:
return cast.ToFloat64E(v)
default:
return nil, ErrInvalidType.New(t.t)
}
}
// Compare implements Type interface.
func (t numberT) Compare(a interface{}, b interface{}) (int, error) {
if IsUnsigned(t) {
return compareUnsigned(a, b)
}
return compareSigned(a, b)
}
func compareSigned(a interface{}, b interface{}) (int, error) {
ca, err := cast.ToInt64E(a)
if err != nil {
return 0, err
}
cb, err := cast.ToInt64E(b)
if err != nil {
return 0, err
}
if ca == cb {
return 0, nil
}
if ca < cb {
return -1, nil
}
return +1, nil
}
func compareUnsigned(a interface{}, b interface{}) (int, error) {
ca, err := cast.ToUint64E(a)
if err != nil {
return 0, err
}
cb, err := cast.ToUint64E(b)
if err != nil {
return 0, err
}
if ca == cb {
return 0, nil
}
if ca < cb {
return -1, nil
}
return +1, nil
}
type timestampT struct{}
// Type implements Type interface.
func (t timestampT) Type() query.Type {
return sqltypes.Timestamp
}
// TimestampLayout is the formatting string with the layout of the timestamp
// using the format of Go "time" package.
const TimestampLayout = "2006-01-02 15:04:05"
// SQL implements Type interface.
func (t timestampT) SQL(v interface{}) sqltypes.Value {
time := MustConvert(t, v).(time.Time)
return sqltypes.MakeTrusted(
sqltypes.Timestamp,
[]byte(time.Format(TimestampLayout)),
)
}
// Convert implements Type interface.
func (t timestampT) Convert(v interface{}) (interface{}, error) {
switch value := v.(type) {
case time.Time:
return value.UTC(), nil
case string:
t, err := time.Parse(TimestampLayout, value)
if err != nil {
return nil, ErrConvertingToTime.Wrap(err, v)
}
return t.UTC(), nil
default:
ts, err := Int64.Convert(v)
if err != nil {
return nil, ErrInvalidType.New(reflect.TypeOf(v))
}
return time.Unix(ts.(int64), 0).UTC(), nil
}
}
// Compare implements Type interface.
func (t timestampT) Compare(a interface{}, b interface{}) (int, error) {
av := a.(time.Time)
bv := b.(time.Time)
if av.Before(bv) {
return -1, nil
} else if av.After(bv) {
return 1, nil
}
return 0, nil
}
type dateT struct{}
// DateLayout is the layout of the MySQL date format in the representation
// Go understands.
const DateLayout = "2006-01-02"
func truncateDate(t time.Time) time.Time {
return t.Truncate(24 * time.Hour)
}
func (t dateT) Type() query.Type {
return sqltypes.Date
}
func (t dateT) SQL(v interface{}) sqltypes.Value {
time := MustConvert(t, v).(time.Time)
return sqltypes.MakeTrusted(
sqltypes.Timestamp,
[]byte(time.Format(DateLayout)),
)
}
func (t dateT) Convert(v interface{}) (interface{}, error) {
switch value := v.(type) {
case time.Time:
return truncateDate(value).UTC(), nil
case string:
t, err := time.Parse(DateLayout, value)
if err != nil {
return nil, ErrConvertingToTime.Wrap(err, v)
}
return truncateDate(t).UTC(), nil
default:
ts, err := Int64.Convert(v)
if err != nil {
return nil, ErrInvalidType.New(reflect.TypeOf(v))
}
return truncateDate(time.Unix(ts.(int64), 0)).UTC(), nil
}
}
func (t dateT) Compare(a, b interface{}) (int, error) {
av := truncateDate(a.(time.Time))
bv := truncateDate(b.(time.Time))
if av.Before(bv) {
return -1, nil
} else if av.After(bv) {
return 1, nil
}
return 0, nil
}
type textT struct{}
// Type implements Type interface.
func (t textT) Type() query.Type {
return sqltypes.Text
}
// SQL implements Type interface.
func (t textT) SQL(v interface{}) sqltypes.Value {
return sqltypes.MakeTrusted(sqltypes.Text, []byte(MustConvert(t, v).(string)))
}
// Convert implements Type interface.
func (t textT) Convert(v interface{}) (interface{}, error) {
return cast.ToStringE(v)
}
// Compare implements Type interface.
func (t textT) Compare(a interface{}, b interface{}) (int, error) {
return strings.Compare(a.(string), b.(string)), nil
}
type booleanT struct{}
// Type implements Type interface.
func (t booleanT) Type() query.Type {
return sqltypes.Bit
}
// SQL implements Type interface.
func (t booleanT) SQL(v interface{}) sqltypes.Value {
b := []byte{'0'}
if cast.ToBool(v) {
b[0] = '1'
}
return sqltypes.MakeTrusted(sqltypes.Bit, b)
}
// Convert implements Type interface.
func (t booleanT) Convert(v interface{}) (interface{}, error) {
return cast.ToBoolE(v)
}
// Compare implements Type interface.
func (t booleanT) Compare(a interface{}, b interface{}) (int, error) {
if a == b {
return 0, nil
}
if a.(bool) == false {
return -1, nil
}
return +1, nil
}
type blobT struct{}
// Type implements Type interface.
func (t blobT) Type() query.Type {
return sqltypes.Blob
}
// SQL implements Type interface.
func (t blobT) SQL(v interface{}) sqltypes.Value {
return sqltypes.MakeTrusted(sqltypes.Blob, MustConvert(t, v).([]byte))
}
// Convert implements Type interface.
func (t blobT) Convert(v interface{}) (interface{}, error) {
switch value := v.(type) {
case []byte:
return value, nil
case string:
return []byte(value), nil
case fmt.Stringer:
return []byte(value.String()), nil
default:
return nil, ErrInvalidType.New(reflect.TypeOf(v))
}
}
// Compare implements Type interface.
func (t blobT) Compare(a interface{}, b interface{}) (int, error) {
return bytes.Compare(a.([]byte), b.([]byte)), nil
}
type jsonT struct{}
// Type implements Type interface.
func (t jsonT) Type() query.Type {
return sqltypes.TypeJSON
}
// SQL implements Type interface.
func (t jsonT) SQL(v interface{}) sqltypes.Value {
return sqltypes.MakeTrusted(sqltypes.TypeJSON, MustConvert(t, v).([]byte))
}
// Convert implements Type interface.
func (t jsonT) Convert(v interface{}) (interface{}, error) {
return json.Marshal(v)
}
// Compare implements Type interface.
func (t jsonT) Compare(a interface{}, b interface{}) (int, error) {
return bytes.Compare(a.([]byte), b.([]byte)), nil
}
type tupleT []Type
func (t tupleT) Type() query.Type {
return sqltypes.Expression
}
func (t tupleT) SQL(v interface{}) sqltypes.Value {
panic("unable to convert tuple type to SQL")
}
func (t tupleT) Convert(v interface{}) (interface{}, error) {
if vals, ok := v.([]interface{}); ok {
if len(vals) != len(t) {
return nil, ErrInvalidColumnNumber.New(len(t), len(vals))
}
var result = make([]interface{}, len(t))
for i, typ := range t {
var err error
result[i], err = typ.Convert(vals[i])
if err != nil {
return nil, err
}
}
return result, nil
}
return nil, ErrNotTuple.New(v)
}
func (t tupleT) Compare(a, b interface{}) (int, error) {
a, err := t.Convert(a)
if err != nil {
return 0, err
}
b, err = t.Convert(b)
if err != nil {
return 0, err
}
left := a.([]interface{})
right := b.([]interface{})
for i := range left {
cmp, err := t[i].Compare(left[i], right[i])
if err != nil {
return 0, err
}
if cmp != 0 {
return cmp, nil
}
}
return 0, nil
}
type arrayT struct {
underlying Type
}
func (t arrayT) Type() query.Type {
return sqltypes.TypeJSON
}
func (t arrayT) SQL(v interface{}) sqltypes.Value {
return JSON.SQL(v)
}
func (t arrayT) Convert(v interface{}) (interface{}, error) {
if vals, ok := v.([]interface{}); ok {
var result = make([]interface{}, len(vals))
for i, v := range vals {
var err error
result[i], err = t.underlying.Convert(v)
if err != nil {
return nil, err
}
}
return result, nil
}
return nil, ErrNotArray.New(v)
}
func (t arrayT) Compare(a, b interface{}) (int, error) {
a, err := t.Convert(a)
if err != nil {
return 0, err
}
b, err = t.Convert(b)
if err != nil {
return 0, err
}
left := a.([]interface{})
right := b.([]interface{})
if len(left) < len(right) {
return -1, nil
} else if len(left) > len(right) {
return 1, nil
}
for i := range left {
cmp, err := t.underlying.Compare(left[i], right[i])
if err != nil {
return 0, err
}
if cmp != 0 {
return cmp, nil
}
}
return 0, nil
}
// MustConvert calls the Convert function from a given Type, it err panics.
func MustConvert(t Type, v interface{}) interface{} {
c, err := t.Convert(v)
if err != nil {
panic(err)
}
return c
}
// IsNumber checks if t is a number type
func IsNumber(t Type) bool {
return IsInteger(t) || IsDecimal(t)
}
// IsSigned checks if t is a signed type.
func IsSigned(t Type) bool {
return t == Int32 || t == Int64
}
// IsUnsigned checks if t is an unsigned type.
func IsUnsigned(t Type) bool {
return t == Uint32 || t == Uint64
}
// IsInteger check if t is a (U)Int32/64 type
func IsInteger(t Type) bool {
return IsSigned(t) || IsUnsigned(t)
}
// IsDecimal checks if t is decimal type.
func IsDecimal(t Type) bool {
return t == Float32 || t == Float64
}
// IsText checks if t is a text type.
func IsText(t Type) bool {
return t == Text || t == Blob || t == JSON
}
// IsTuple checks if t is a tuple type.
// Note that tupleT instances with just 1 value are not considered
// as a tuple, but a parenthesized value.
func IsTuple(t Type) bool {
v, ok := t.(tupleT)
return ok && len(v) > 1
}
// IsArray returns whether the given type is an array.
func IsArray(t Type) bool {
_, ok := t.(arrayT)
return ok
}
// NumColumns returns the number of columns in a type. This is one for all
// types, except tuples.
func NumColumns(t Type) int {
v, ok := t.(tupleT)
if !ok {
return 1
}
return len(v)
}