-
Notifications
You must be signed in to change notification settings - Fork 38
/
literals.go
1174 lines (1025 loc) · 31.5 KB
/
literals.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 iceberg
import (
"bytes"
"cmp"
"encoding"
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"time"
"unsafe"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/decimal128"
"github.com/google/uuid"
)
// LiteralType is a generic type constraint for the explicit Go types that we allow
// for literal values. This represents the actual primitive types that exist in Iceberg
type LiteralType interface {
bool | int32 | int64 | float32 | float64 | Date |
Time | Timestamp | string | []byte | uuid.UUID | Decimal
}
// Comparator is a comparison function for specific literal types:
//
// returns 0 if v1 == v2
// returns <0 if v1 < v2
// returns >0 if v1 > v2
type Comparator[T LiteralType] func(v1, v2 T) int
// Literal is a non-null literal value. It can be casted using To and be checked for
// equality against other literals.
type Literal interface {
fmt.Stringer
encoding.BinaryMarshaler
Type() Type
To(Type) (Literal, error)
Equals(Literal) bool
}
// TypedLiteral is a generic interface for Literals so that you can retrieve the value.
// This is based on the physical representative type, which means that FixedLiteral and
// BinaryLiteral will both return []byte, etc.
type TypedLiteral[T LiteralType] interface {
Literal
Value() T
Comparator() Comparator[T]
}
type NumericLiteral interface {
Literal
Increment() Literal
Decrement() Literal
}
// NewLiteral provides a literal based on the type of T
func NewLiteral[T LiteralType](val T) Literal {
switch v := any(val).(type) {
case bool:
return BoolLiteral(v)
case int32:
return Int32Literal(v)
case int64:
return Int64Literal(v)
case float32:
return Float32Literal(v)
case float64:
return Float64Literal(v)
case Date:
return DateLiteral(v)
case Time:
return TimeLiteral(v)
case Timestamp:
return TimestampLiteral(v)
case string:
return StringLiteral(v)
case []byte:
return BinaryLiteral(v)
case uuid.UUID:
return UUIDLiteral(v)
case Decimal:
return DecimalLiteral(v)
}
panic("can't happen due to literal type constraint")
}
// LiteralFromBytes uses the defined Iceberg spec for how to serialize a value of
// a the provided type and returns the appropriate Literal value from it.
//
// If you already have a value of the desired Literal type, you could alternatively
// call UnmarshalBinary on it yourself manually.
//
// This is primarily used for retrieving stat values.
func LiteralFromBytes(typ Type, data []byte) (Literal, error) {
if data == nil {
return nil, ErrInvalidBinSerialization
}
switch t := typ.(type) {
case BooleanType:
var v BoolLiteral
err := v.UnmarshalBinary(data)
return v, err
case Int32Type:
var v Int32Literal
err := v.UnmarshalBinary(data)
return v, err
case Int64Type:
var v Int64Literal
err := v.UnmarshalBinary(data)
return v, err
case Float32Type:
var v Float32Literal
err := v.UnmarshalBinary(data)
return v, err
case Float64Type:
var v Float64Literal
err := v.UnmarshalBinary(data)
return v, err
case StringType:
var v StringLiteral
err := v.UnmarshalBinary(data)
return v, err
case BinaryType:
var v BinaryLiteral
err := v.UnmarshalBinary(data)
return v, err
case FixedType:
if len(data) != t.Len() {
// looks like some writers will write a prefix of the fixed length
// for lower/upper bounds instead of the full length. so let's pad
// it out to the full length if unpacking a fixed length literal
padded := make([]byte, t.Len())
copy(padded, data)
data = padded
}
var v FixedLiteral
err := v.UnmarshalBinary(data)
return v, err
case DecimalType:
v := DecimalLiteral{Scale: t.scale}
err := v.UnmarshalBinary(data)
return v, err
case DateType:
var v DateLiteral
err := v.UnmarshalBinary(data)
return v, err
case TimeType:
var v TimeLiteral
err := v.UnmarshalBinary(data)
return v, err
case TimestampType, TimestampTzType:
var v TimestampLiteral
err := v.UnmarshalBinary(data)
return v, err
case UUIDType:
var v UUIDLiteral
err := v.UnmarshalBinary(data)
return v, err
}
return nil, ErrType
}
// convenience to avoid repreating this pattern for primitive types
func literalEq[L interface {
comparable
LiteralType
}, T TypedLiteral[L]](lhs T, other Literal) bool {
rhs, ok := other.(T)
if !ok {
return false
}
return lhs.Value() == rhs.Value()
}
// AboveMaxLiteral represents values that are above the maximum for their type
// such as values > math.MaxInt32 for an Int32Literal
type AboveMaxLiteral interface {
Literal
aboveMax()
}
// BelowMinLiteral represents values that are below the minimum for their type
// such as values < math.MinInt32 for an Int32Literal
type BelowMinLiteral interface {
Literal
belowMin()
}
type aboveMaxLiteral[T int32 | int64 | float32 | float64] struct {
value T
}
func (ab aboveMaxLiteral[T]) MarshalBinary() (data []byte, err error) {
return nil, fmt.Errorf("%w: cannot marshal above max literal",
ErrInvalidBinSerialization)
}
func (ab aboveMaxLiteral[T]) aboveMax() {}
func (ab aboveMaxLiteral[T]) Type() Type {
var z T
switch any(z).(type) {
case int32:
return PrimitiveTypes.Int32
case int64:
return PrimitiveTypes.Int64
case float32:
return PrimitiveTypes.Float32
case float64:
return PrimitiveTypes.Float64
default:
panic("should never happen")
}
}
func (ab aboveMaxLiteral[T]) To(t Type) (Literal, error) {
if ab.Type().Equals(t) {
return ab, nil
}
return nil, fmt.Errorf("%w: cannot change type of AboveMax%sLiteral",
ErrBadCast, reflect.TypeOf(T(0)).String())
}
func (ab aboveMaxLiteral[T]) Value() T { return ab.value }
func (ab aboveMaxLiteral[T]) String() string { return "AboveMax" }
func (ab aboveMaxLiteral[T]) Equals(other Literal) bool {
// AboveMaxLiteral isn't comparable and thus isn't even equal to itself
return false
}
type belowMinLiteral[T int32 | int64 | float32 | float64] struct {
value T
}
func (bm belowMinLiteral[T]) MarshalBinary() (data []byte, err error) {
return nil, fmt.Errorf("%w: cannot marshal above max literal",
ErrInvalidBinSerialization)
}
func (bm belowMinLiteral[T]) belowMin() {}
func (bm belowMinLiteral[T]) Type() Type {
var z T
switch any(z).(type) {
case int32:
return PrimitiveTypes.Int32
case int64:
return PrimitiveTypes.Int64
case float32:
return PrimitiveTypes.Float32
case float64:
return PrimitiveTypes.Float64
default:
panic("should never happen")
}
}
func (bm belowMinLiteral[T]) To(t Type) (Literal, error) {
if bm.Type().Equals(t) {
return bm, nil
}
return nil, fmt.Errorf("%w: cannot change type of BelowMin%sLiteral",
ErrBadCast, reflect.TypeOf(T(0)).String())
}
func (bm belowMinLiteral[T]) Value() T { return bm.value }
func (bm belowMinLiteral[T]) String() string { return "BelowMin" }
func (bm belowMinLiteral[T]) Equals(other Literal) bool {
// BelowMinLiteral isn't comparable and thus isn't even equal to itself
return false
}
func Int32AboveMaxLiteral() Literal {
return aboveMaxLiteral[int32]{value: math.MaxInt32}
}
func Int64AboveMaxLiteral() Literal {
return aboveMaxLiteral[int64]{value: math.MaxInt64}
}
func Float32AboveMaxLiteral() Literal {
return aboveMaxLiteral[float32]{value: math.MaxFloat32}
}
func Float64AboveMaxLiteral() Literal {
return aboveMaxLiteral[float64]{value: math.MaxFloat64}
}
func Int32BelowMinLiteral() Literal {
return belowMinLiteral[int32]{value: math.MinInt32}
}
func Int64BelowMinLiteral() Literal {
return belowMinLiteral[int64]{value: math.MinInt64}
}
func Float32BelowMinLiteral() Literal {
return belowMinLiteral[float32]{value: -math.MaxFloat32}
}
func Float64BelowMinLiteral() Literal {
return belowMinLiteral[float64]{value: -math.MaxFloat64}
}
type BoolLiteral bool
func (BoolLiteral) Comparator() Comparator[bool] {
return func(v1, v2 bool) int {
if v1 {
if v2 {
return 0
}
return 1
}
return -1
}
}
func (b BoolLiteral) Type() Type { return PrimitiveTypes.Bool }
func (b BoolLiteral) Value() bool { return bool(b) }
func (b BoolLiteral) String() string { return strconv.FormatBool(bool(b)) }
func (b BoolLiteral) To(t Type) (Literal, error) {
switch t.(type) {
case BooleanType:
return b, nil
}
return nil, fmt.Errorf("%w: BoolLiteral to %s", ErrBadCast, t)
}
func (b BoolLiteral) Equals(l Literal) bool {
return literalEq(b, l)
}
var (
falseBin, trueBin = [1]byte{0x0}, [1]byte{0x1}
)
func (b BoolLiteral) MarshalBinary() (data []byte, err error) {
// stored as 0x00 for false, and anything non-zero for True
if b {
return trueBin[:], nil
}
return falseBin[:], nil
}
func (b *BoolLiteral) UnmarshalBinary(data []byte) error {
// stored as 0x00 for false and anything non-zero for True
if len(data) < 1 {
return fmt.Errorf("%w: expected at least 1 byte for bool", ErrInvalidBinSerialization)
}
*b = data[0] != 0
return nil
}
type Int32Literal int32
func (Int32Literal) Comparator() Comparator[int32] { return cmp.Compare[int32] }
func (i Int32Literal) Type() Type { return PrimitiveTypes.Int32 }
func (i Int32Literal) Value() int32 { return int32(i) }
func (i Int32Literal) String() string { return strconv.FormatInt(int64(i), 10) }
func (i Int32Literal) To(t Type) (Literal, error) {
switch t := t.(type) {
case Int32Type:
return i, nil
case Int64Type:
return Int64Literal(i), nil
case Float32Type:
return Float32Literal(i), nil
case Float64Type:
return Float64Literal(i), nil
case DateType:
return DateLiteral(i), nil
case TimeType:
return TimeLiteral(i), nil
case TimestampType:
return TimestampLiteral(i), nil
case TimestampTzType:
return TimestampLiteral(i), nil
case DecimalType:
unscaled := Decimal{Val: decimal128.FromI64(int64(i)), Scale: 0}
if t.scale == 0 {
return DecimalLiteral(unscaled), nil
}
out, err := unscaled.Val.Rescale(0, int32(t.scale))
if err != nil {
return nil, fmt.Errorf("%w: failed to cast to DecimalType: %s", ErrBadCast, err.Error())
}
return DecimalLiteral{Val: out, Scale: t.scale}, nil
}
return nil, fmt.Errorf("%w: Int32Literal to %s", ErrBadCast, t)
}
func (i Int32Literal) Equals(other Literal) bool {
return literalEq(i, other)
}
func (i Int32Literal) Increment() Literal {
if i == math.MaxInt32 {
return Int32AboveMaxLiteral()
}
return Int32Literal(i + 1)
}
func (i Int32Literal) Decrement() Literal {
if i == math.MinInt32 {
return Int32BelowMinLiteral()
}
return Int32Literal(i - 1)
}
func (i Int32Literal) MarshalBinary() (data []byte, err error) {
// stored as 4 bytes in little endian order
data = make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(i))
return
}
func (i *Int32Literal) UnmarshalBinary(data []byte) error {
// stored as 4 bytes little endian
if len(data) != 4 {
return fmt.Errorf("%w: expected 4 bytes for int32 value, got %d",
ErrInvalidBinSerialization, len(data))
}
*i = Int32Literal(binary.LittleEndian.Uint32(data))
return nil
}
type Int64Literal int64
func (Int64Literal) Comparator() Comparator[int64] { return cmp.Compare[int64] }
func (i Int64Literal) Type() Type { return PrimitiveTypes.Int64 }
func (i Int64Literal) Value() int64 { return int64(i) }
func (i Int64Literal) String() string { return strconv.FormatInt(int64(i), 10) }
func (i Int64Literal) To(t Type) (Literal, error) {
switch t := t.(type) {
case Int32Type:
if math.MaxInt32 < i {
return Int32AboveMaxLiteral(), nil
} else if math.MinInt32 > i {
return Int32BelowMinLiteral(), nil
}
return Int32Literal(i), nil
case Int64Type:
return i, nil
case Float32Type:
return Float32Literal(i), nil
case Float64Type:
return Float64Literal(i), nil
case DateType:
return DateLiteral(i), nil
case TimeType:
return TimeLiteral(i), nil
case TimestampType:
return TimestampLiteral(i), nil
case TimestampTzType:
return TimestampLiteral(i), nil
case DecimalType:
unscaled := Decimal{Val: decimal128.FromI64(int64(i)), Scale: 0}
if t.scale == 0 {
return DecimalLiteral(unscaled), nil
}
out, err := unscaled.Val.Rescale(0, int32(t.scale))
if err != nil {
return nil, fmt.Errorf("%w: failed to cast to DecimalType: %s", ErrBadCast, err.Error())
}
return DecimalLiteral{Val: out, Scale: t.scale}, nil
}
return nil, fmt.Errorf("%w: Int64Literal to %s", ErrBadCast, t)
}
func (i Int64Literal) Equals(other Literal) bool {
return literalEq(i, other)
}
func (i Int64Literal) Increment() Literal {
if i == math.MaxInt64 {
return Int64AboveMaxLiteral()
}
return Int64Literal(i + 1)
}
func (i Int64Literal) Decrement() Literal {
if i == math.MinInt64 {
return Int64BelowMinLiteral()
}
return Int64Literal(i - 1)
}
func (i Int64Literal) MarshalBinary() (data []byte, err error) {
// stored as 8 byte little-endian
data = make([]byte, 8)
binary.LittleEndian.PutUint64(data, uint64(i))
return
}
func (i *Int64Literal) UnmarshalBinary(data []byte) error {
// stored as 8 byte little-endian
if len(data) != 8 {
return fmt.Errorf("%w: expected 8 bytes for int64 value, got %d",
ErrInvalidBinSerialization, len(data))
}
*i = Int64Literal(binary.LittleEndian.Uint64(data))
return nil
}
type Float32Literal float32
func (Float32Literal) Comparator() Comparator[float32] { return cmp.Compare[float32] }
func (f Float32Literal) Type() Type { return PrimitiveTypes.Float32 }
func (f Float32Literal) Value() float32 { return float32(f) }
func (f Float32Literal) String() string { return strconv.FormatFloat(float64(f), 'g', -1, 32) }
func (f Float32Literal) To(t Type) (Literal, error) {
switch t := t.(type) {
case Float32Type:
return f, nil
case Float64Type:
return Float64Literal(f), nil
case DecimalType:
v, err := decimal128.FromFloat32(float32(f), int32(t.precision), int32(t.scale))
if err != nil {
return nil, err
}
return DecimalLiteral{Val: v, Scale: t.scale}, nil
}
return nil, fmt.Errorf("%w: Float32Literal to %s", ErrBadCast, t)
}
func (f Float32Literal) Equals(other Literal) bool {
return literalEq(f, other)
}
func (f Float32Literal) MarshalBinary() (data []byte, err error) {
// stored as 4 bytes little endian
data = make([]byte, 4)
binary.LittleEndian.PutUint32(data, math.Float32bits(float32(f)))
return
}
func (f *Float32Literal) UnmarshalBinary(data []byte) error {
// stored as 4 bytes little endian
if len(data) != 4 {
return fmt.Errorf("%w: expected 4 bytes for float32 value, got %d",
ErrInvalidBinSerialization, len(data))
}
*f = Float32Literal(math.Float32frombits(binary.LittleEndian.Uint32(data)))
return nil
}
type Float64Literal float64
func (Float64Literal) Comparator() Comparator[float64] { return cmp.Compare[float64] }
func (f Float64Literal) Type() Type { return PrimitiveTypes.Float64 }
func (f Float64Literal) Value() float64 { return float64(f) }
func (f Float64Literal) String() string { return strconv.FormatFloat(float64(f), 'g', -1, 64) }
func (f Float64Literal) To(t Type) (Literal, error) {
switch t := t.(type) {
case Float32Type:
if math.MaxFloat32 < f {
return Float32AboveMaxLiteral(), nil
} else if -math.MaxFloat32 > f {
return Float32BelowMinLiteral(), nil
}
return Float32Literal(f), nil
case Float64Type:
return f, nil
case DecimalType:
v, err := decimal128.FromFloat64(float64(f), int32(t.precision), int32(t.scale))
if err != nil {
return nil, err
}
return DecimalLiteral{Val: v, Scale: t.scale}, nil
}
return nil, fmt.Errorf("%w: Float64Literal to %s", ErrBadCast, t)
}
func (f Float64Literal) Equals(other Literal) bool {
return literalEq(f, other)
}
func (f Float64Literal) MarshalBinary() (data []byte, err error) {
// stored as 8 bytes little endian
data = make([]byte, 8)
binary.LittleEndian.PutUint64(data, math.Float64bits(float64(f)))
return
}
func (f *Float64Literal) UnmarshalBinary(data []byte) error {
// stored as 8 bytes in little endian
if len(data) != 8 {
return fmt.Errorf("%w: expected 8 bytes for float64 value, got %d",
ErrInvalidBinSerialization, len(data))
}
*f = Float64Literal(math.Float64frombits(binary.LittleEndian.Uint64(data)))
return nil
}
type DateLiteral Date
func (DateLiteral) Comparator() Comparator[Date] { return cmp.Compare[Date] }
func (d DateLiteral) Type() Type { return PrimitiveTypes.Date }
func (d DateLiteral) Value() Date { return Date(d) }
func (d DateLiteral) String() string {
t := Date(d).ToTime()
return t.Format("2006-01-02")
}
func (d DateLiteral) To(t Type) (Literal, error) {
switch t.(type) {
case DateType:
return d, nil
}
return nil, fmt.Errorf("%w: DateLiteral to %s", ErrBadCast, t)
}
func (d DateLiteral) Equals(other Literal) bool {
return literalEq(d, other)
}
func (d DateLiteral) Increment() Literal { return DateLiteral(d + 1) }
func (d DateLiteral) Decrement() Literal { return DateLiteral(d - 1) }
func (d DateLiteral) MarshalBinary() (data []byte, err error) {
// stored as 4 byte little endian
data = make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(d))
return
}
func (d *DateLiteral) UnmarshalBinary(data []byte) error {
// stored as 4 byte little endian
if len(data) != 4 {
return fmt.Errorf("%w: expected 4 bytes for date value, got %d",
ErrInvalidBinSerialization, len(data))
}
*d = DateLiteral(binary.LittleEndian.Uint32(data))
return nil
}
type TimeLiteral Time
func (TimeLiteral) Comparator() Comparator[Time] { return cmp.Compare[Time] }
func (t TimeLiteral) Type() Type { return PrimitiveTypes.Time }
func (t TimeLiteral) Value() Time { return Time(t) }
func (t TimeLiteral) String() string {
tm := time.UnixMicro(int64(t)).UTC()
return tm.Format("15:04:05.000000")
}
func (t TimeLiteral) To(typ Type) (Literal, error) {
switch typ.(type) {
case TimeType:
return t, nil
}
return nil, fmt.Errorf("%w: TimeLiteral to %s", ErrBadCast, typ)
}
func (t TimeLiteral) Equals(other Literal) bool {
return literalEq(t, other)
}
func (t TimeLiteral) MarshalBinary() (data []byte, err error) {
// stored as 8 byte little-endian
data = make([]byte, 8)
binary.LittleEndian.PutUint64(data, uint64(t))
return
}
func (t *TimeLiteral) UnmarshalBinary(data []byte) error {
// stored as 8 byte little-endian representing microseconds from midnight
if len(data) != 8 {
return fmt.Errorf("%w: expected 8 bytes for time value, got %d",
ErrInvalidBinSerialization, len(data))
}
*t = TimeLiteral(binary.LittleEndian.Uint64(data))
return nil
}
type TimestampLiteral Timestamp
func (TimestampLiteral) Comparator() Comparator[Timestamp] { return cmp.Compare[Timestamp] }
func (t TimestampLiteral) Type() Type { return PrimitiveTypes.Timestamp }
func (t TimestampLiteral) Value() Timestamp { return Timestamp(t) }
func (t TimestampLiteral) String() string {
tm := Timestamp(t).ToTime()
return tm.Format("2006-01-02 15:04:05.000000")
}
func (t TimestampLiteral) To(typ Type) (Literal, error) {
switch typ.(type) {
case TimestampType:
return t, nil
case TimestampTzType:
return t, nil
case DateType:
return DateLiteral(Timestamp(t).ToDate()), nil
}
return nil, fmt.Errorf("%w: TimestampLiteral to %s", ErrBadCast, typ)
}
func (t TimestampLiteral) Equals(other Literal) bool {
return literalEq(t, other)
}
func (t TimestampLiteral) Increment() Literal { return TimestampLiteral(t + 1) }
func (t TimestampLiteral) Decrement() Literal { return TimestampLiteral(t - 1) }
func (t TimestampLiteral) MarshalBinary() (data []byte, err error) {
// stored as 8 byte little endian
data = make([]byte, 8)
binary.LittleEndian.PutUint64(data, uint64(t))
return
}
func (t *TimestampLiteral) UnmarshalBinary(data []byte) error {
// stored as 8 byte little endian value representing microseconds since epoch
if len(data) != 8 {
return fmt.Errorf("%w: expected 8 bytes for timestamp value, got %d",
ErrInvalidBinSerialization, len(data))
}
*t = TimestampLiteral(binary.LittleEndian.Uint64(data))
return nil
}
type StringLiteral string
func (StringLiteral) Comparator() Comparator[string] { return cmp.Compare[string] }
func (s StringLiteral) Type() Type { return PrimitiveTypes.String }
func (s StringLiteral) Value() string { return string(s) }
func (s StringLiteral) String() string { return string(s) }
func (s StringLiteral) To(typ Type) (Literal, error) {
switch t := typ.(type) {
case StringType:
return s, nil
case Int32Type:
n, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s",
errors.Join(ErrBadCast, err), s, typ)
}
if math.MaxInt32 < n {
return Int32AboveMaxLiteral(), nil
} else if math.MinInt32 > n {
return Int32BelowMinLiteral(), nil
}
return Int32Literal(n), nil
case Int64Type:
n, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s",
errors.Join(ErrBadCast, err), s, typ)
}
return Int64Literal(n), nil
case Float32Type:
n, err := strconv.ParseFloat(string(s), 32)
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s",
errors.Join(ErrBadCast, err), s, typ)
}
return Float32Literal(n), nil
case Float64Type:
n, err := strconv.ParseFloat(string(s), 64)
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s",
errors.Join(ErrBadCast, err), s, typ)
}
return Float64Literal(n), nil
case DateType:
tm, err := time.Parse("2006-01-02", string(s))
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s - %s",
ErrBadCast, s, typ, err.Error())
}
return DateLiteral(tm.Truncate(24*time.Hour).Unix() / int64((time.Hour * 24).Seconds())), nil
case TimeType:
val, err := arrow.Time64FromString(string(s), arrow.Microsecond)
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s - %s",
ErrBadCast, s, typ, err.Error())
}
return TimeLiteral(val), nil
case TimestampType:
// requires RFC3339 with no time zone
tm, err := time.Parse("2006-01-02T15:04:05", string(s))
if err != nil {
return nil, fmt.Errorf("%w: invalid Timestamp format for casting from string '%s': %s",
ErrBadCast, s, err.Error())
}
return TimestampLiteral(Timestamp(tm.UTC().UnixMicro())), nil
case TimestampTzType:
// requires RFC3339 format WITH time zone
tm, err := time.Parse(time.RFC3339, string(s))
if err != nil {
return nil, fmt.Errorf("%w: invalid TimestampTz format for casting from string '%s': %s",
ErrBadCast, s, err.Error())
}
return TimestampLiteral(Timestamp(tm.UTC().UnixMicro())), nil
case UUIDType:
val, err := uuid.Parse(string(s))
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s - %s",
ErrBadCast, s, typ, err.Error())
}
return UUIDLiteral(val), nil
case DecimalType:
n, err := decimal128.FromString(string(s), int32(t.precision), int32(t.scale))
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s - %s",
ErrBadCast, s, typ, err.Error())
}
return DecimalLiteral{Val: n, Scale: t.scale}, nil
case BooleanType:
val, err := strconv.ParseBool(string(s))
if err != nil {
return nil, fmt.Errorf("%w: casting '%s' to %s - %s",
ErrBadCast, s, typ, err.Error())
}
return BoolLiteral(val), nil
case BinaryType:
return BinaryLiteral(s), nil
case FixedType:
if len(s) != t.len {
return nil, fmt.Errorf("%w: cast '%s' to %s - wrong length",
ErrBadCast, s, t)
}
return FixedLiteral(s), nil
}
return nil, fmt.Errorf("%w: StringLiteral to %s", ErrBadCast, typ)
}
func (s StringLiteral) Equals(other Literal) bool {
return literalEq(s, other)
}
func (s StringLiteral) MarshalBinary() (data []byte, err error) {
// stored as UTF-8 bytes without length
// avoid copying by just returning a slice of the raw bytes
data = unsafe.Slice(unsafe.StringData(string(s)), len(s))
return
}
func (s *StringLiteral) UnmarshalBinary(data []byte) error {
// stored as UTF-8 bytes without length
// avoid copy, but this means that the passed in slice is being given
// to the literal for ownership
*s = StringLiteral(unsafe.String(unsafe.SliceData(data), len(data)))
return nil
}
type BinaryLiteral []byte
func (BinaryLiteral) Comparator() Comparator[[]byte] {
return bytes.Compare
}
func (b BinaryLiteral) Type() Type { return PrimitiveTypes.Binary }
func (b BinaryLiteral) Value() []byte { return []byte(b) }
func (b BinaryLiteral) String() string { return string(b) }
func (b BinaryLiteral) To(typ Type) (Literal, error) {
switch t := typ.(type) {
case UUIDType:
val, err := uuid.FromBytes(b)
if err != nil {
return nil, fmt.Errorf("%w: cannot convert BinaryLiteral to UUID",
errors.Join(ErrBadCast, err))
}
return UUIDLiteral(val), nil
case FixedType:
if len(b) == t.len {
return FixedLiteral(b), nil
}
return nil, fmt.Errorf("%w: cannot convert BinaryLiteral to %s, different length - %d <> %d",
ErrBadCast, typ, len(b), t.len)
case BinaryType:
return b, nil
}
return nil, fmt.Errorf("%w: BinaryLiteral to %s", ErrBadCast, typ)
}
func (b BinaryLiteral) Equals(other Literal) bool {
rhs, ok := other.(BinaryLiteral)
if !ok {
return false
}
return bytes.Equal([]byte(b), rhs)
}
func (b BinaryLiteral) MarshalBinary() (data []byte, err error) {
// stored directly as is
data = b
return
}
func (b *BinaryLiteral) UnmarshalBinary(data []byte) error {
// stored directly as is
*b = BinaryLiteral(data)
return nil
}
type FixedLiteral []byte
func (FixedLiteral) Comparator() Comparator[[]byte] { return bytes.Compare }
func (f FixedLiteral) Type() Type { return FixedTypeOf(len(f)) }
func (f FixedLiteral) Value() []byte { return []byte(f) }
func (f FixedLiteral) String() string { return string(f) }
func (f FixedLiteral) To(typ Type) (Literal, error) {
switch t := typ.(type) {
case UUIDType:
val, err := uuid.FromBytes(f)
if err != nil {
return nil, fmt.Errorf("%w: cannot convert FixedLiteral to UUID - %s",
ErrBadCast, err.Error())
}
return UUIDLiteral(val), nil
case FixedType:
if len(f) == t.len {
return FixedLiteral(f), nil
}
return nil, fmt.Errorf("%w: cannot convert FixedLiteral to %s, different length - %d <> %d",
ErrBadCast, typ, len(f), t.len)
case BinaryType:
return f, nil
}
return nil, fmt.Errorf("%w: FixedLiteral[%d] to %s",
ErrBadCast, len(f), typ)
}
func (f FixedLiteral) Equals(other Literal) bool {
rhs, ok := other.(FixedLiteral)
if !ok {
return false
}
return bytes.Equal([]byte(f), rhs)
}
func (f FixedLiteral) MarshalBinary() (data []byte, err error) {
// stored directly as is
data = f
return
}
func (f *FixedLiteral) UnmarshalBinary(data []byte) error {
// stored directly as is
*f = FixedLiteral(data)
return nil
}
type UUIDLiteral uuid.UUID
func (UUIDLiteral) Comparator() Comparator[uuid.UUID] {
return func(v1, v2 uuid.UUID) int {
return bytes.Compare(v1[:], v2[:])
}
}
func (UUIDLiteral) Type() Type { return PrimitiveTypes.UUID }
func (u UUIDLiteral) Value() uuid.UUID { return uuid.UUID(u) }