-
Notifications
You must be signed in to change notification settings - Fork 35
/
types.go
689 lines (580 loc) · 16.3 KB
/
types.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
// 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 (
"encoding/json"
"fmt"
"regexp"
"slices"
"strconv"
"strings"
"time"
"github.com/apache/arrow-go/v18/arrow/decimal128"
)
var (
regexFromBrackets = regexp.MustCompile(`^\w+\[(\d+)\]$`)
decimalRegex = regexp.MustCompile(`decimal\(\s*(\d+)\s*,\s*(\d+)\s*\)`)
)
type Properties map[string]string
// Get returns the value of the key if it exists, otherwise it returns the default value.
func (p Properties) Get(key, defVal string) string {
if v, ok := p[key]; ok {
return v
}
return defVal
}
// Type is an interface representing any of the available iceberg types,
// such as primitives (int32/int64/etc.) or nested types (list/struct/map).
type Type interface {
fmt.Stringer
Type() string
Equals(Type) bool
}
// NestedType is an interface that allows access to the child fields of
// a nested type such as a list/struct/map type.
type NestedType interface {
Type
Fields() []NestedField
}
type typeIFace struct {
Type
}
func (t *typeIFace) MarshalJSON() ([]byte, error) {
if nested, ok := t.Type.(NestedType); ok {
return json.Marshal(nested)
}
return []byte(`"` + t.Type.Type() + `"`), nil
}
func (t *typeIFace) UnmarshalJSON(b []byte) error {
var typename string
err := json.Unmarshal(b, &typename)
if err == nil {
switch typename {
case "boolean":
t.Type = BooleanType{}
case "int":
t.Type = Int32Type{}
case "long":
t.Type = Int64Type{}
case "float":
t.Type = Float32Type{}
case "double":
t.Type = Float64Type{}
case "date":
t.Type = DateType{}
case "time":
t.Type = TimeType{}
case "timestamp":
t.Type = TimestampType{}
case "timestamptz":
t.Type = TimestampTzType{}
case "string":
t.Type = StringType{}
case "uuid":
t.Type = UUIDType{}
case "binary":
t.Type = BinaryType{}
default:
switch {
case strings.HasPrefix(typename, "fixed"):
matches := regexFromBrackets.FindStringSubmatch(typename)
if len(matches) != 2 {
return fmt.Errorf("%w: %s", ErrInvalidTypeString, typename)
}
n, _ := strconv.Atoi(matches[1])
t.Type = FixedType{len: n}
case strings.HasPrefix(typename, "decimal"):
matches := decimalRegex.FindStringSubmatch(typename)
if len(matches) != 3 {
return fmt.Errorf("%w: %s", ErrInvalidTypeString, typename)
}
prec, _ := strconv.Atoi(matches[1])
scale, _ := strconv.Atoi(matches[2])
t.Type = DecimalType{precision: prec, scale: scale}
default:
return fmt.Errorf("%w: unrecognized field type", ErrInvalidSchema)
}
}
return nil
}
aux := struct {
TypeName string `json:"type"`
}{}
if err = json.Unmarshal(b, &aux); err != nil {
return err
}
switch aux.TypeName {
case "list":
t.Type = &ListType{}
case "map":
t.Type = &MapType{}
case "struct":
t.Type = &StructType{}
default:
return fmt.Errorf("%w: %s", ErrInvalidTypeString, aux.TypeName)
}
return json.Unmarshal(b, t.Type)
}
type NestedField struct {
Type `json:"-"`
ID int `json:"id"`
Name string `json:"name"`
Required bool `json:"required"`
Doc string `json:"doc,omitempty"`
InitialDefault any `json:"initial-default,omitempty"`
WriteDefault any `json:"write-default,omitempty"`
}
func optOrReq(required bool) string {
if required {
return "required"
}
return "optional"
}
func (n NestedField) String() string {
doc := n.Doc
if doc != "" {
doc = " (" + doc + ")"
}
return fmt.Sprintf("%d: %s: %s %s%s",
n.ID, n.Name, optOrReq(n.Required), n.Type, doc)
}
func (n *NestedField) Equals(other NestedField) bool {
return n.ID == other.ID &&
n.Name == other.Name &&
n.Required == other.Required &&
n.Doc == other.Doc &&
n.InitialDefault == other.InitialDefault &&
n.WriteDefault == other.WriteDefault &&
n.Type.Equals(other.Type)
}
func (n NestedField) MarshalJSON() ([]byte, error) {
type Alias NestedField
return json.Marshal(struct {
Type *typeIFace `json:"type"`
*Alias
}{Type: &typeIFace{n.Type}, Alias: (*Alias)(&n)})
}
func (n *NestedField) UnmarshalJSON(b []byte) error {
type Alias NestedField
aux := struct {
Type typeIFace `json:"type"`
*Alias
}{
Alias: (*Alias)(n),
}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
n.Type = aux.Type.Type
return nil
}
type StructType struct {
FieldList []NestedField `json:"fields"`
}
func (s *StructType) Equals(other Type) bool {
st, ok := other.(*StructType)
if !ok {
return false
}
return slices.EqualFunc(s.FieldList, st.FieldList, func(a, b NestedField) bool {
return a.Equals(b)
})
}
func (s *StructType) Fields() []NestedField { return s.FieldList }
func (s *StructType) MarshalJSON() ([]byte, error) {
type Alias StructType
return json.Marshal(struct {
Type string `json:"type"`
*Alias
}{Type: s.Type(), Alias: (*Alias)(s)})
}
func (*StructType) Type() string { return "struct" }
func (s *StructType) String() string {
var b strings.Builder
b.WriteString("struct<")
for i, f := range s.FieldList {
if i != 0 {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%d: %s: ",
f.ID, f.Name)
if f.Required {
b.WriteString("required ")
} else {
b.WriteString("optional ")
}
b.WriteString(f.Type.String())
if f.Doc != "" {
b.WriteString(" (")
b.WriteString(f.Doc)
b.WriteByte(')')
}
}
b.WriteString(">")
return b.String()
}
type ListType struct {
ElementID int `json:"element-id"`
Element Type `json:"-"`
ElementRequired bool `json:"element-required"`
}
func (l *ListType) MarshalJSON() ([]byte, error) {
type Alias ListType
return json.Marshal(struct {
Type string `json:"type"`
*Alias
Element *typeIFace `json:"element"`
}{Type: l.Type(), Alias: (*Alias)(l), Element: &typeIFace{l.Element}})
}
func (l *ListType) Equals(other Type) bool {
rhs, ok := other.(*ListType)
if !ok {
return false
}
return l.ElementID == rhs.ElementID &&
l.Element.Equals(rhs.Element) &&
l.ElementRequired == rhs.ElementRequired
}
func (l *ListType) Fields() []NestedField {
return []NestedField{l.ElementField()}
}
func (l *ListType) ElementField() NestedField {
return NestedField{
ID: l.ElementID,
Name: "element",
Type: l.Element,
Required: l.ElementRequired,
}
}
func (*ListType) Type() string { return "list" }
func (l *ListType) String() string { return fmt.Sprintf("list<%s>", l.Element) }
func (l *ListType) UnmarshalJSON(b []byte) error {
aux := struct {
ID int `json:"element-id"`
Elem typeIFace `json:"element"`
Req bool `json:"element-required"`
}{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
l.ElementID = aux.ID
l.Element = aux.Elem.Type
l.ElementRequired = aux.Req
return nil
}
type MapType struct {
KeyID int `json:"key-id"`
KeyType Type `json:"-"`
ValueID int `json:"value-id"`
ValueType Type `json:"-"`
ValueRequired bool `json:"value-required"`
}
func (m *MapType) MarshalJSON() ([]byte, error) {
type Alias MapType
return json.Marshal(struct {
Type string `json:"type"`
*Alias
KeyType *typeIFace `json:"key"`
ValueType *typeIFace `json:"value"`
}{Type: m.Type(), Alias: (*Alias)(m),
KeyType: &typeIFace{m.KeyType},
ValueType: &typeIFace{m.ValueType}})
}
func (m *MapType) Equals(other Type) bool {
rhs, ok := other.(*MapType)
if !ok {
return false
}
return m.KeyID == rhs.KeyID &&
m.KeyType.Equals(rhs.KeyType) &&
m.ValueID == rhs.ValueID &&
m.ValueType.Equals(rhs.ValueType) &&
m.ValueRequired == rhs.ValueRequired
}
func (m *MapType) Fields() []NestedField {
return []NestedField{m.KeyField(), m.ValueField()}
}
func (m *MapType) KeyField() NestedField {
return NestedField{
Name: "key",
ID: m.KeyID,
Type: m.KeyType,
Required: true,
}
}
func (m *MapType) ValueField() NestedField {
return NestedField{
Name: "value",
ID: m.ValueID,
Type: m.ValueType,
Required: m.ValueRequired,
}
}
func (*MapType) Type() string { return "map" }
func (m *MapType) String() string {
return fmt.Sprintf("map<%s, %s>", m.KeyType, m.ValueType)
}
func (m *MapType) UnmarshalJSON(b []byte) error {
aux := struct {
KeyID int `json:"key-id"`
Key typeIFace `json:"key"`
ValueID int `json:"value-id"`
Value typeIFace `json:"value"`
ValueReq *bool `json:"value-required"`
}{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
m.KeyID, m.KeyType = aux.KeyID, aux.Key.Type
m.ValueID, m.ValueType = aux.ValueID, aux.Value.Type
if aux.ValueReq == nil {
m.ValueRequired = true
} else {
m.ValueRequired = *aux.ValueReq
}
return nil
}
func FixedTypeOf(n int) FixedType { return FixedType{len: n} }
type FixedType struct {
len int
}
func (f FixedType) Equals(other Type) bool {
rhs, ok := other.(FixedType)
if !ok {
return false
}
return f.len == rhs.len
}
func (f FixedType) Len() int { return f.len }
func (f FixedType) Type() string { return fmt.Sprintf("fixed[%d]", f.len) }
func (f FixedType) String() string { return fmt.Sprintf("fixed[%d]", f.len) }
func (f FixedType) primitive() {}
func DecimalTypeOf(prec, scale int) DecimalType {
return DecimalType{precision: prec, scale: scale}
}
type DecimalType struct {
precision, scale int
}
func (d DecimalType) Equals(other Type) bool {
rhs, ok := other.(DecimalType)
if !ok {
return false
}
return d.precision == rhs.precision &&
d.scale == rhs.scale
}
func (d DecimalType) Type() string { return fmt.Sprintf("decimal(%d, %d)", d.precision, d.scale) }
func (d DecimalType) String() string { return fmt.Sprintf("decimal(%d, %d)", d.precision, d.scale) }
func (d DecimalType) Precision() int { return d.precision }
func (d DecimalType) Scale() int { return d.scale }
func (DecimalType) primitive() {}
type Decimal struct {
Val decimal128.Num
Scale int
}
type PrimitiveType interface {
Type
primitive()
}
type BooleanType struct{}
func (BooleanType) Equals(other Type) bool {
_, ok := other.(BooleanType)
return ok
}
func (BooleanType) primitive() {}
func (BooleanType) Type() string { return "boolean" }
func (BooleanType) String() string { return "boolean" }
// Int32Type is the "int"/"integer" type of the iceberg spec.
type Int32Type struct{}
func (Int32Type) Equals(other Type) bool {
_, ok := other.(Int32Type)
return ok
}
func (Int32Type) primitive() {}
func (Int32Type) Type() string { return "int" }
func (Int32Type) String() string { return "int" }
// Int64Type is the "long" type of the iceberg spec.
type Int64Type struct{}
func (Int64Type) Equals(other Type) bool {
_, ok := other.(Int64Type)
return ok
}
func (Int64Type) primitive() {}
func (Int64Type) Type() string { return "long" }
func (Int64Type) String() string { return "long" }
// Float32Type is the "float" type in the iceberg spec.
type Float32Type struct{}
func (Float32Type) Equals(other Type) bool {
_, ok := other.(Float32Type)
return ok
}
func (Float32Type) primitive() {}
func (Float32Type) Type() string { return "float" }
func (Float32Type) String() string { return "float" }
// Float64Type represents the "double" type of the iceberg spec.
type Float64Type struct{}
func (Float64Type) Equals(other Type) bool {
_, ok := other.(Float64Type)
return ok
}
func (Float64Type) primitive() {}
func (Float64Type) Type() string { return "double" }
func (Float64Type) String() string { return "double" }
type Date int32
func (d Date) ToTime() time.Time {
return epochTM.AddDate(0, 0, int(d))
}
// DateType represents a calendar date without a timezone or time,
// represented as a 32-bit integer denoting the number of days since
// the unix epoch.
type DateType struct{}
func (DateType) Equals(other Type) bool {
_, ok := other.(DateType)
return ok
}
func (DateType) primitive() {}
func (DateType) Type() string { return "date" }
func (DateType) String() string { return "date" }
type Time int64
// TimeType represents a number of microseconds since midnight.
type TimeType struct{}
func (TimeType) Equals(other Type) bool {
_, ok := other.(TimeType)
return ok
}
func (TimeType) primitive() {}
func (TimeType) Type() string { return "time" }
func (TimeType) String() string { return "time" }
type Timestamp int64
func (t Timestamp) ToTime() time.Time {
return time.UnixMicro(int64(t)).UTC()
}
func (t Timestamp) ToDate() Date {
tm := time.UnixMicro(int64(t)).UTC()
return Date(tm.Truncate(24*time.Hour).Unix() / int64((time.Hour * 24).Seconds()))
}
// TimestampType represents a number of microseconds since the unix epoch
// without regard for timezone.
type TimestampType struct{}
func (TimestampType) Equals(other Type) bool {
_, ok := other.(TimestampType)
return ok
}
func (TimestampType) primitive() {}
func (TimestampType) Type() string { return "timestamp" }
func (TimestampType) String() string { return "timestamp" }
// TimestampTzType represents a timestamp stored as UTC representing the
// number of microseconds since the unix epoch.
type TimestampTzType struct{}
func (TimestampTzType) Equals(other Type) bool {
_, ok := other.(TimestampTzType)
return ok
}
func (TimestampTzType) primitive() {}
func (TimestampTzType) Type() string { return "timestamptz" }
func (TimestampTzType) String() string { return "timestamptz" }
type StringType struct{}
func (StringType) Equals(other Type) bool {
_, ok := other.(StringType)
return ok
}
func (StringType) primitive() {}
func (StringType) Type() string { return "string" }
func (StringType) String() string { return "string" }
type UUIDType struct{}
func (UUIDType) Equals(other Type) bool {
_, ok := other.(UUIDType)
return ok
}
func (UUIDType) primitive() {}
func (UUIDType) Type() string { return "uuid" }
func (UUIDType) String() string { return "uuid" }
type BinaryType struct{}
func (BinaryType) Equals(other Type) bool {
_, ok := other.(BinaryType)
return ok
}
func (BinaryType) primitive() {}
func (BinaryType) Type() string { return "binary" }
func (BinaryType) String() string { return "binary" }
var PrimitiveTypes = struct {
Bool PrimitiveType
Int32 PrimitiveType
Int64 PrimitiveType
Float32 PrimitiveType
Float64 PrimitiveType
Date PrimitiveType
Time PrimitiveType
Timestamp PrimitiveType
TimestampTz PrimitiveType
String PrimitiveType
Binary PrimitiveType
UUID PrimitiveType
}{
Bool: BooleanType{},
Int32: Int32Type{},
Int64: Int64Type{},
Float32: Float32Type{},
Float64: Float64Type{},
Date: DateType{},
Time: TimeType{},
Timestamp: TimestampType{},
TimestampTz: TimestampTzType{},
String: StringType{},
Binary: BinaryType{},
UUID: UUIDType{},
}
// PromoteType promotes the type being read from a file to a requested read type.
// fileType is the type from the file being read
// readType is the requested readType
func PromoteType(fileType, readType Type) (Type, error) {
switch t := fileType.(type) {
case Int32Type:
if _, ok := readType.(Int64Type); ok {
return readType, nil
}
case Float32Type:
if _, ok := readType.(Float64Type); ok {
return readType, nil
}
case StringType:
if _, ok := readType.(BinaryType); ok {
return readType, nil
}
case BinaryType:
if _, ok := readType.(StringType); ok {
return readType, nil
}
case DecimalType:
if rt, ok := readType.(DecimalType); ok {
if t.precision <= rt.precision && t.scale <= rt.scale {
return readType, nil
}
return nil, fmt.Errorf("%w: cannot reduce precision from %s to %s",
ErrResolve, fileType, readType)
}
case FixedType:
if _, ok := readType.(UUIDType); ok && t.len == 16 {
return readType, nil
}
default:
if fileType.Equals(readType) {
return fileType, nil
}
}
return nil, fmt.Errorf("%w: cannot promote %s to %s", ErrResolve, fileType, readType)
}