-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
reflection.go
828 lines (756 loc) · 27.6 KB
/
reflection.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
// 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 schema
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/apache/arrow/go/v12/parquet"
format "github.com/apache/arrow/go/v12/parquet/internal/gen-go/parquet"
"golang.org/x/xerrors"
)
type taggedInfo struct {
Name string
Type parquet.Type
KeyType parquet.Type
ValueType parquet.Type
Length int32
KeyLength int32
ValueLength int32
Scale int32
KeyScale int32
ValueScale int32
Precision int32
KeyPrecision int32
ValuePrecision int32
FieldID int32
KeyFieldID int32
ValueFieldID int32
RepetitionType parquet.Repetition
ValueRepetition parquet.Repetition
Converted ConvertedType
KeyConverted ConvertedType
ValueConverted ConvertedType
LogicalFields map[string]string
KeyLogicalFields map[string]string
ValueLogicalFields map[string]string
LogicalType LogicalType
KeyLogicalType LogicalType
ValueLogicalType LogicalType
}
func (t *taggedInfo) CopyForKey() (ret taggedInfo) {
ret = *t
ret.Type = t.KeyType
ret.Length = t.KeyLength
ret.Scale = t.KeyScale
ret.Precision = t.KeyPrecision
ret.FieldID = t.KeyFieldID
ret.RepetitionType = parquet.Repetitions.Required
ret.Converted = t.KeyConverted
ret.LogicalType = t.KeyLogicalType
return
}
func (t *taggedInfo) CopyForValue() (ret taggedInfo) {
ret = *t
ret.Type = t.ValueType
ret.Length = t.ValueLength
ret.Scale = t.ValueScale
ret.Precision = t.ValuePrecision
ret.FieldID = t.ValueFieldID
ret.RepetitionType = t.ValueRepetition
ret.Converted = t.ValueConverted
ret.LogicalType = t.ValueLogicalType
return
}
func (t *taggedInfo) UpdateLogicalTypes() {
processLogicalType := func(fields map[string]string, precision, scale int32) LogicalType {
t, ok := fields["type"]
if !ok {
return NoLogicalType{}
}
switch strings.ToLower(t) {
case "string":
return StringLogicalType{}
case "map":
return MapLogicalType{}
case "list":
return ListLogicalType{}
case "enum":
return EnumLogicalType{}
case "decimal":
if v, ok := fields["precision"]; ok {
precision = int32FromType(v)
}
if v, ok := fields["scale"]; ok {
scale = int32FromType(v)
}
return NewDecimalLogicalType(precision, scale)
case "date":
return DateLogicalType{}
case "time":
unit, ok := fields["unit"]
if !ok {
panic("must specify unit for time logical type")
}
adjustedToUtc, ok := fields["isadjustedutc"]
if !ok {
adjustedToUtc = "true"
}
return NewTimeLogicalType(boolFromStr(adjustedToUtc), timeUnitFromString(strings.ToLower(unit)))
case "timestamp":
unit, ok := fields["unit"]
if !ok {
panic("must specify unit for time logical type")
}
adjustedToUtc, ok := fields["isadjustedutc"]
if !ok {
adjustedToUtc = "true"
}
return NewTimestampLogicalType(boolFromStr(adjustedToUtc), timeUnitFromString(unit))
case "integer":
width, ok := fields["bitwidth"]
if !ok {
panic("must specify bitwidth if explicitly setting integer logical type")
}
signed, ok := fields["signed"]
if !ok {
signed = "true"
}
return NewIntLogicalType(int8(int32FromType(width)), boolFromStr(signed))
case "null":
return NullLogicalType{}
case "json":
return JSONLogicalType{}
case "bson":
return BSONLogicalType{}
case "uuid":
return UUIDLogicalType{}
default:
panic(fmt.Errorf("invalid logical type specified: %s", t))
}
}
t.LogicalType = processLogicalType(t.LogicalFields, t.Precision, t.Scale)
t.KeyLogicalType = processLogicalType(t.KeyLogicalFields, t.KeyPrecision, t.KeyScale)
t.ValueLogicalType = processLogicalType(t.ValueLogicalFields, t.ValuePrecision, t.ValueScale)
}
func newTaggedInfo() taggedInfo {
return taggedInfo{
Type: parquet.Types.Undefined,
KeyType: parquet.Types.Undefined,
ValueType: parquet.Types.Undefined,
RepetitionType: parquet.Repetitions.Undefined,
ValueRepetition: parquet.Repetitions.Undefined,
Converted: ConvertedTypes.NA,
KeyConverted: ConvertedTypes.NA,
ValueConverted: ConvertedTypes.NA,
FieldID: -1,
KeyFieldID: -1,
ValueFieldID: -1,
LogicalFields: make(map[string]string),
KeyLogicalFields: make(map[string]string),
ValueLogicalFields: make(map[string]string),
LogicalType: NoLogicalType{},
KeyLogicalType: NoLogicalType{},
ValueLogicalType: NoLogicalType{},
}
}
var int32FromType = func(v string) int32 {
val, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
return int32(val)
}
var boolFromStr = func(v string) bool {
val, err := strconv.ParseBool(v)
if err != nil {
panic(err)
}
return val
}
func infoFromTags(f reflect.StructTag) *taggedInfo {
typeFromStr := func(v string) parquet.Type {
t, err := format.TypeFromString(strings.ToUpper(v))
if err != nil {
panic(fmt.Errorf("invalid type specified: %s", v))
}
return parquet.Type(t)
}
repFromStr := func(v string) parquet.Repetition {
r, err := format.FieldRepetitionTypeFromString(strings.ToUpper(v))
if err != nil {
panic(err)
}
return parquet.Repetition(r)
}
convertedFromStr := func(v string) ConvertedType {
c, err := format.ConvertedTypeFromString(strings.ToUpper(v))
if err != nil {
panic(err)
}
return ConvertedType(c)
}
if ptags, ok := f.Lookup("parquet"); ok {
info := newTaggedInfo()
for _, tag := range strings.Split(strings.Replace(ptags, "\t", "", -1), ",") {
tag = strings.TrimSpace(tag)
kv := strings.SplitN(tag, "=", 2)
key := strings.TrimSpace(strings.ToLower(kv[0]))
value := strings.TrimSpace(kv[1])
switch key {
case "name":
info.Name = value
case "type":
info.Type = typeFromStr(value)
case "keytype":
info.KeyType = typeFromStr(value)
case "valuetype":
info.ValueType = typeFromStr(value)
case "length":
info.Length = int32FromType(value)
case "keylength":
info.KeyLength = int32FromType(value)
case "valuelength":
info.ValueLength = int32FromType(value)
case "scale":
info.Scale = int32FromType(value)
case "keyscale":
info.KeyScale = int32FromType(value)
case "valuescale":
info.ValueScale = int32FromType(value)
case "precision":
info.Precision = int32FromType(value)
case "keyprecision":
info.KeyPrecision = int32FromType(value)
case "valueprecision":
info.ValuePrecision = int32FromType(value)
case "fieldid":
info.FieldID = int32FromType(value)
case "keyfieldid":
info.KeyFieldID = int32FromType(value)
case "valuefieldid":
info.ValueFieldID = int32FromType(value)
case "repetition":
info.RepetitionType = repFromStr(value)
case "valuerepetition":
info.ValueRepetition = repFromStr(value)
case "converted":
info.Converted = convertedFromStr(value)
case "keyconverted":
info.KeyConverted = convertedFromStr(value)
case "valueconverted":
info.ValueConverted = convertedFromStr(value)
case "logical":
info.LogicalFields["type"] = value
case "keylogical":
info.KeyLogicalFields["type"] = value
case "valuelogical":
info.ValueLogicalFields["type"] = value
default:
switch {
case strings.HasPrefix(key, "logical."):
info.LogicalFields[strings.TrimPrefix(key, "logical.")] = value
case strings.HasPrefix(key, "keylogical."):
info.KeyLogicalFields[strings.TrimPrefix(key, "keylogical.")] = value
case strings.HasPrefix(key, "valuelogical."):
info.ValueLogicalFields[strings.TrimPrefix(key, "valuelogical.")] = value
}
}
}
info.UpdateLogicalTypes()
return &info
}
return nil
}
// typeToNode recurseively converts a physical type and the tag info into parquet Nodes
//
// to avoid having to propagate errors up potentially high numbers of recursive calls
// we use panics and then recover in the public function NewSchemaFromStruct so that a
// failure very far down the stack quickly unwinds.
func typeToNode(name string, typ reflect.Type, repType parquet.Repetition, info *taggedInfo) Node {
// set up our default values for everything
var (
converted = ConvertedTypes.None
logical LogicalType = NoLogicalType{}
fieldID = int32(-1)
physical = parquet.Types.Undefined
typeLen = 0
precision = 0
scale = 0
)
if info != nil { // we have struct tag info to process
fieldID = info.FieldID
if info.Converted != ConvertedTypes.NA {
converted = info.Converted
}
logical = info.LogicalType
physical = info.Type
typeLen = int(info.Length)
precision = int(info.Precision)
scale = int(info.Scale)
if info.Name != "" {
name = info.Name
}
if info.RepetitionType != parquet.Repetitions.Undefined {
repType = info.RepetitionType
}
}
// simplify the logic by switching based on the reflection Kind
switch typ.Kind() {
case reflect.Map:
// a map must have a logical type of MAP or have no tag for logical type in which case
// we assume MAP logical type.
if !logical.IsNone() && !logical.Equals(MapLogicalType{}) {
panic("cannot set logical type to something other than map for a map")
}
infoCopy := newTaggedInfo()
if info != nil { // populate any value specific tags to propagate for the value type
infoCopy = info.CopyForValue()
}
// create the node for the value type of the map
value := typeToNode("value", typ.Elem(), parquet.Repetitions.Required, &infoCopy)
if info != nil { // change our copy to now use the key specific tags if they exist
infoCopy = info.CopyForKey()
}
// create the node for the key type of the map
key := typeToNode("key", typ.Key(), parquet.Repetitions.Required, &infoCopy)
if key.RepetitionType() != parquet.Repetitions.Required { // key cannot be optional
panic("key type of map must be Required")
}
return Must(MapOf(name, key, value, repType, fieldID))
case reflect.Struct:
// structs are Group nodes
fields := make(FieldList, 0)
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
fields = append(fields, typeToNode(f.Name, f.Type, parquet.Repetitions.Required, infoFromTags(f.Tag)))
}
// group nodes don't have a physical type
if physical != parquet.Types.Undefined {
panic("cannot specify custom type on struct")
}
// group nodes don't have converted or logical types
if converted != ConvertedTypes.None {
panic("cannot specify converted types for a struct")
}
if !logical.IsNone() {
panic("cannot specify logicaltype for a struct")
}
return Must(NewGroupNode(name, repType, fields, fieldID))
case reflect.Ptr: // if we encounter a pointer create a node for the type it points to, but mark it as optional
return typeToNode(name, typ.Elem(), parquet.Repetitions.Optional, info)
case reflect.Array:
// arrays are repeated or fixed size
if typ == reflect.TypeOf(parquet.Int96{}) {
return NewInt96Node(name, repType, fieldID)
}
if typ.Elem() == reflect.TypeOf(byte(0)) { // something like [12]byte translates to FixedLenByteArray with length 12
if physical == parquet.Types.Undefined {
physical = parquet.Types.FixedLenByteArray
}
if typeLen == 0 { // if there was no type length specified in the tag, use the length of the type.
typeLen = typ.Len()
}
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, physical, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, physical, converted, typeLen, precision, scale, fieldID))
}
fallthrough // if it's not a fixed len byte array type, then just treat it like a slice
case reflect.Slice:
// for slices, we default to treating them as lists unless the repetition type is set to REPEATED or they are
// a bytearray/fixedlenbytearray
switch {
case repType == parquet.Repetitions.Repeated:
return typeToNode(name, typ.Elem(), parquet.Repetitions.Repeated, info)
case physical == parquet.Types.FixedLenByteArray || physical == parquet.Types.ByteArray:
if typ.Elem() != reflect.TypeOf(byte(0)) {
panic("slice with physical type ByteArray or FixedLenByteArray must be []byte")
}
fallthrough
case typ.Elem() == reflect.TypeOf(byte(0)):
if physical == parquet.Types.Undefined {
physical = parquet.Types.ByteArray
}
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, physical, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, physical, converted, typeLen, precision, scale, fieldID))
default:
var elemInfo *taggedInfo
if info != nil {
elemInfo = &taggedInfo{}
*elemInfo = info.CopyForValue()
}
if !logical.IsNone() && !logical.Equals(ListLogicalType{}) {
panic("slice must either be repeated or a List type")
}
if converted != ConvertedTypes.None && converted != ConvertedTypes.List {
panic("slice must either be repeated or a List type")
}
return Must(ListOf(typeToNode(name, typ.Elem(), parquet.Repetitions.Required, elemInfo), repType, fieldID))
}
case reflect.String:
// strings are byte arrays or fixedlen byte array
t := parquet.Types.ByteArray
switch physical {
case parquet.Types.Undefined, parquet.Types.ByteArray:
case parquet.Types.FixedLenByteArray:
t = parquet.Types.FixedLenByteArray
default:
panic("string fields should be of type bytearray or fixedlenbytearray only")
}
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, t, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, t, converted, typeLen, precision, scale, fieldID))
case reflect.Int, reflect.Int32, reflect.Int8, reflect.Int16, reflect.Int64:
// handle integer types, default to setting the corresponding logical type
ptyp := parquet.Types.Int32
if typ.Bits() == 64 {
ptyp = parquet.Types.Int64
}
if physical != parquet.Types.Undefined {
ptyp = physical
}
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, ptyp, typeLen, fieldID))
}
bitwidth := int8(typ.Bits())
if physical != parquet.Types.Undefined {
if ptyp == parquet.Types.Int32 {
bitwidth = 32
} else if ptyp == parquet.Types.Int64 {
bitwidth = 64
}
}
if converted != ConvertedTypes.None {
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, ptyp, converted, 0, precision, scale, fieldID))
}
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, NewIntLogicalType(bitwidth, true), ptyp, 0, fieldID))
case reflect.Uint, reflect.Uint32, reflect.Uint8, reflect.Uint16, reflect.Uint64:
// handle unsigned integer types and default to the corresponding logical type for it.
ptyp := parquet.Types.Int32
if typ.Bits() == 64 {
ptyp = parquet.Types.Int64
}
if physical != parquet.Types.Undefined {
ptyp = physical
}
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, ptyp, typeLen, fieldID))
}
bitwidth := int8(typ.Bits())
if physical != parquet.Types.Undefined {
if ptyp == parquet.Types.Int32 {
bitwidth = 32
} else if ptyp == parquet.Types.Int64 {
bitwidth = 64
}
}
if converted != ConvertedTypes.None {
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, ptyp, converted, 0, precision, scale, fieldID))
}
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, NewIntLogicalType(bitwidth, false), ptyp, 0, fieldID))
case reflect.Bool:
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, parquet.Types.Boolean, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, parquet.Types.Boolean, converted, typeLen, precision, scale, fieldID))
case reflect.Float32:
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, parquet.Types.Float, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, parquet.Types.Float, converted, typeLen, precision, scale, fieldID))
case reflect.Float64:
if !logical.IsNone() {
return MustPrimitive(NewPrimitiveNodeLogical(name, repType, logical, parquet.Types.Double, typeLen, fieldID))
}
return MustPrimitive(NewPrimitiveNodeConverted(name, repType, parquet.Types.Double, converted, typeLen, precision, scale, fieldID))
}
return nil
}
// NewSchemaFromStruct generates a schema from an object type via reflection of
// the type and reading struct tags for "parquet".
//
// Rules
//
// Everything defaults to Required repetition, unless otherwise specified.
// Pointer types become Optional repetition.
// Arrays and Slices become logical List types unless using the tag `repetition=repeated`.
//
// A length specified byte field (like [5]byte) becomes a fixed_len_byte_array of that length
// unless otherwise specified by tags.
//
// string and []byte both become ByteArray unless otherwise specified.
//
// Integer types will default to having a logical type of the appropriate bit width
// and signedness rather than having no logical type, ie: an int8 will become an int32
// node with logical type Int(bitWidth=8, signed=true).
//
// Structs will become group nodes with the fields of the struct as the fields of the group,
// recursively creating the nodes.
//
// maps will become appropriate Map structures in the schema of the defined key and values.
//
// Available Tags
//
// name: by default the node will have the same name as the field, this tag let's you specify a name
//
// type: Specify the physical type instead of using the field type
//
// length: specify the type length of the node, only relevant for fixed_len_byte_array
//
// scale: specify the scale for a decimal field
//
// precision: specify the precision for a decimal field
//
// fieldid: specify the field ID for that node, defaults to -1 which means it is not set in the parquet file.
//
// repetition: specify the repetition as something other than what is determined by the type
//
// converted: specify the Converted Type of the field
//
// logical: specify the logical type of the field, if using decimal then the scale and precision
// will be determined by the precision and scale fields, or by the logical.precision / logical.scale fields
// with the logical. prefixed versions taking precedence. For Time or Timestamp logical types,
// use logical.unit=<millis|micros|nanos> and logical.isadjustedutc=<true|false> to set those. Unit is required
// isadjustedutc defaults to true. For Integer logical type, use logical.bitwidth and logical.signed to specify
// those values, with bitwidth being required, and signed defaulting to true.
//
// All tags other than name can use a prefix of "key<tagname>=<value>" to refer to the type of the key for a map
// and "value<tagname>=<value>" to refer to the value type of a map or the element of a list (such as the type of a slice)
func NewSchemaFromStruct(obj interface{}) (sc *Schema, err error) {
ot := reflect.TypeOf(obj)
if ot.Kind() == reflect.Ptr {
ot = ot.Elem()
}
// typeToNode uses panics to fail fast / fail early instead of propagating
// errors up recursive stacks. so we recover here and return it as an error
defer func() {
if r := recover(); r != nil {
sc = nil
switch x := r.(type) {
case string:
err = xerrors.New(x)
case error:
err = x
default:
err = xerrors.New("unknown panic")
}
}
}()
root := typeToNode(ot.Name(), ot, parquet.Repetitions.Repeated, nil)
return NewSchema(root.(*GroupNode)), nil
}
var parquetTypeToReflect = map[parquet.Type]reflect.Type{
parquet.Types.Boolean: reflect.TypeOf(true),
parquet.Types.Int32: reflect.TypeOf(int32(0)),
parquet.Types.Int64: reflect.TypeOf(int64(0)),
parquet.Types.Float: reflect.TypeOf(float32(0)),
parquet.Types.Double: reflect.TypeOf(float64(0)),
parquet.Types.Int96: reflect.TypeOf(parquet.Int96{}),
parquet.Types.ByteArray: reflect.TypeOf(parquet.ByteArray{}),
parquet.Types.FixedLenByteArray: reflect.TypeOf(parquet.FixedLenByteArray{}),
}
func typeFromNode(n Node) reflect.Type {
switch n.Type() {
case Primitive:
typ := parquetTypeToReflect[n.(*PrimitiveNode).PhysicalType()]
// if a bytearray field is annoted as a String logical type or a UTF8 converted type
// then use a string instead of parquet.ByteArray / parquet.FixedLenByteArray which are []byte
if n.LogicalType().Equals(StringLogicalType{}) || n.ConvertedType() == ConvertedTypes.UTF8 {
typ = reflect.TypeOf(string(""))
}
if n.RepetitionType() == parquet.Repetitions.Optional {
typ = reflect.PtrTo(typ)
} else if n.RepetitionType() == parquet.Repetitions.Repeated {
typ = reflect.SliceOf(typ)
}
return typ
case Group:
gnode := n.(*GroupNode)
switch gnode.ConvertedType() {
case ConvertedTypes.List:
// According to the Parquet Spec, a list should always be a 3-level structure
//
// <list-repetition> group <name> (LIST) {
// repeated group list {
// <element-repetition> <element-type> element;
// }
// }
//
// Outer-most level must be a group annotated with LIST containing a single field named "list".
// this level must be only optional (if the list is nullable) or required
// Middle level, named list, must be repeated group with a single field named "element"
// "element" field is the lists element type and repetition, which should be only required or optional
if gnode.fields.Len() != 1 {
panic("invalid list node, should have exactly 1 child.")
}
if gnode.fields[0].RepetitionType() != parquet.Repetitions.Repeated {
panic("invalid list node, child should be repeated")
}
// it is required that the repeated group of elements is named "list" and it's element
// field is named "element", however existing data may not use this so readers shouldn't
// enforce them as errors
//
// Rules for backward compatibility from the parquet spec:
//
// 1) if the repeated field is not a group, then it's type is the element type and elements
// must be required.
// 2) if the repeated field is a group with multiple fields, then its type is the element type
// and elements must be required.
// 3) if the repeated field is a group with one field AND is named either "array" or uses the
// LIST-annotated group's name with "_tuple" suffix, then the repeated type is the element
// type and the elements must be required.
// 4) otherwise, the repeated field's type is the element type with the repeated field's repetition
elemMustBeRequired := false
addSlice := false
var elemType reflect.Type
elemNode := gnode.fields[0]
switch {
case elemNode.Type() == Primitive,
elemNode.(*GroupNode).fields.Len() > 1,
elemNode.(*GroupNode).fields.Len() == 1 && (elemNode.Name() == "array" || elemNode.Name() == gnode.Name()+"_tuple"):
elemMustBeRequired = true
elemType = typeFromNode(elemNode)
default:
addSlice = true
elemType = typeFromNode(elemNode.(*GroupNode).fields[0])
}
if elemMustBeRequired && elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
if addSlice {
elemType = reflect.SliceOf(elemType)
}
if gnode.RepetitionType() == parquet.Repetitions.Optional {
elemType = reflect.PtrTo(elemType)
}
return elemType
case ConvertedTypes.Map, ConvertedTypes.MapKeyValue:
// According to the Parquet Spec, the outer-most level should be
// a group containing a single field named "key_value" with repetition
// either optional or required for whether or not the map is nullable.
//
// The key_value middle level *must* be a repeated group with a "key" field
// and *optionally* a "value" field
//
// the "key" field *must* be required and must always exist
//
// the "value" field can be required or optional or omitted.
//
// <map-repetition> group <name> (MAP) {
// repeated group key_value {
// required <key-type> key;
// <value-repetition> <value-type> value;
// }
// }
if gnode.fields.Len() != 1 {
panic("invalid map node, should have exactly 1 child")
}
if gnode.fields[0].Type() != Group {
panic("invalid map node, child should be a group node")
}
// that said, this may not be used in existing data and should not be
// enforced as errors when reading.
//
// some data may also incorrectly use MAP_KEY_VALUE instead of MAP
//
// so any group with MAP_KEY_VALUE that is not contained inside of a "MAP"
// group, should be considered equivalent to being a MAP group itself.
//
// in addition, the fields may not be called "key" and "value" in existing
// data, and as such should not be enforced as errors when reading.
keyval := gnode.fields[0].(*GroupNode)
keyIndex := keyval.FieldIndexByName("key")
if keyIndex == -1 {
keyIndex = 0 // use first child if there is no child named "key"
}
keyType := typeFromNode(keyval.fields[keyIndex])
if keyType.Kind() == reflect.Ptr {
keyType = keyType.Elem()
}
// can't use a []byte as a key for a map, so use string
if keyType == reflect.TypeOf(parquet.ByteArray{}) || keyType == reflect.TypeOf(parquet.FixedLenByteArray{}) {
keyType = reflect.TypeOf(string(""))
}
// if the value node is omitted, then consider this a "set" and make it a
// map[key-type]bool
valType := reflect.TypeOf(true)
if keyval.fields.Len() > 1 {
valIndex := keyval.FieldIndexByName("value")
if valIndex == -1 {
valIndex = 1 // use second child if there is no child named "value"
}
valType = typeFromNode(keyval.fields[valIndex])
}
mapType := reflect.MapOf(keyType, valType)
if gnode.RepetitionType() == parquet.Repetitions.Optional {
mapType = reflect.PtrTo(mapType)
}
return mapType
default:
fields := []reflect.StructField{}
for _, f := range gnode.fields {
fields = append(fields, reflect.StructField{
Name: f.Name(),
Type: typeFromNode(f),
PkgPath: "parquet",
})
}
structType := reflect.StructOf(fields)
if gnode.RepetitionType() == parquet.Repetitions.Repeated {
return reflect.SliceOf(structType)
}
if gnode.RepetitionType() == parquet.Repetitions.Optional {
return reflect.PtrTo(structType)
}
return structType
}
}
panic("what happened?")
}
// NewStructFromSchema generates a struct type as a reflect.Type from the schema
// by using the appropriate physical types and making things either pointers or slices
// based on whether they are repeated/optional/required. It does not use the logical
// or converted types to change the physical storage so that it is more efficient to use
// the resulting type for reading without having to do conversions.
//
// It will use maps for map types and slices for list types, but otherwise ignores the
// converted and logical types of the nodes. Group nodes that are not List or Map will
// be nested structs.
func NewStructFromSchema(sc *Schema) (t reflect.Type, err error) {
defer func() {
if r := recover(); r != nil {
t = nil
switch x := r.(type) {
case string:
err = xerrors.New(x)
case error:
err = x
default:
err = xerrors.New("unknown panic")
}
}
}()
t = typeFromNode(sc.root)
if t.Kind() == reflect.Slice || t.Kind() == reflect.Ptr {
return t.Elem(), nil
}
return
}