-
Notifications
You must be signed in to change notification settings - Fork 53
/
schema.go
1022 lines (877 loc) · 24.3 KB
/
schema.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
package goparquet
import (
"fmt"
"strings"
"github.com/fraugster/parquet-go/parquet"
"github.com/fraugster/parquet-go/parquetschema"
"github.com/pkg/errors"
)
const (
_ int = iota
listParent
mapParent
)
// Column is composed of a schema definition for the column, a column store
// that contains the implementation to write the data to a parquet file, and
// any additional parameters that are necessary to correctly write the data.
// Please the NewDataColumn, NewListColumn or NewMapColumn functions to create
// a Column object correctly.
type Column struct {
index int
name, flatName string
nameArray []string
// one of the following should be not null. data or children
data *ColumnStore
children []*Column
rep parquet.FieldRepetitionType
maxR, maxD uint16
parent int // one of noParent, listParent, mapParent
// for the reader we should read this element from the meta, for the writer we need to build this element
element *parquet.SchemaElement
params *ColumnParameters
}
// Children returns the column's child columns.
func (c *Column) Children() []*Column {
return c.children
}
func (c *Column) pathArray() []string {
if c.nameArray == nil {
c.nameArray = strings.Split(c.flatName, ".")
}
return c.nameArray
}
func (c *Column) getSchemaArray() []*parquet.SchemaElement {
ret := []*parquet.SchemaElement{c.Element()}
if c.data != nil {
return ret
}
for i := range c.children {
ret = append(ret, c.children[i].getSchemaArray()...)
}
return ret
}
// MaxDefinitionLevel returns the maximum definition level for this column.
func (c *Column) MaxDefinitionLevel() uint16 {
return c.maxD
}
// MaxRepetitionLevel returns the maximum repetition value for this column.
func (c *Column) MaxRepetitionLevel() uint16 {
return c.maxR
}
// FlatName returns the name of the column and its parents in dotted notation.
func (c *Column) FlatName() string {
return c.flatName
}
// Name returns the column name.
func (c *Column) Name() string {
return c.name
}
// Index returns the index of the column in schema, zero based.
func (c *Column) Index() int {
return c.index
}
// Element returns schema element definition of the column.
func (c *Column) Element() *parquet.SchemaElement {
if c.element == nil {
// If this is a no-element node, we need to re-create element every time to make sure the content is always up-to-date
return c.buildElement()
}
return c.element
}
// Type returns the parquet type of the value. If the column is a group, then the
// method will return nil.
func (c *Column) Type() *parquet.Type {
if c.data == nil {
return nil
}
return parquet.TypePtr(c.data.parquetType())
}
// RepetitionType returns the repetition type for the current column.
func (c *Column) RepetitionType() *parquet.FieldRepetitionType {
return &c.rep
}
// DataColumn returns true if the column is data column, false otherwise.
func (c *Column) DataColumn() bool {
return c.data != nil
}
// ChildrenCount returns the number of children in a group. If the column is
// a data column, it returns -1.
func (c *Column) ChildrenCount() int {
if c.data != nil {
return -1
}
return len(c.children)
}
func (c *Column) getColumnStore() *ColumnStore {
return c.data
}
func (c *Column) buildElement() *parquet.SchemaElement {
rep := c.rep
elem := &parquet.SchemaElement{
RepetitionType: &rep,
Name: c.name,
}
if c.params != nil {
elem.FieldID = c.params.FieldID
elem.ConvertedType = c.params.ConvertedType
elem.LogicalType = c.params.LogicalType
}
if c.data != nil {
elem.Type = parquet.TypePtr(c.data.parquetType())
elem.TypeLength = c.params.TypeLength
elem.Scale = c.params.Scale
elem.Precision = c.params.Precision
} else {
nc := int32(len(c.children))
elem.NumChildren = &nc
}
return elem
}
func (c *Column) getDataSize() int64 {
if _, ok := c.data.typedColumnStore.(*booleanStore); ok {
// Booleans are stored in one bit, so the result is the number of items / 8
return int64(c.data.values.numValues())/8 + 1
}
return c.data.values.size
}
func (c *Column) getNextData() (map[string]interface{}, int32, error) {
if c.children == nil {
return nil, 0, errors.New("bug: call getNextData on non group node")
}
ret := make(map[string]interface{})
notNil := 0
var maxD int32
for i := range c.children {
data, dl, err := c.children[i].getData()
if err != nil {
return nil, 0, err
}
if dl > maxD {
maxD = dl
}
// https://golang.org/doc/faq#nil_error
if m, ok := data.(map[string]interface{}); ok && m == nil {
data = nil
}
// if the data is not nil, then its ok, but if its nil, we need to know in which definition level is this nil is.
// if its exactly one below max definition level, then the parent is there
if data != nil {
ret[c.children[i].name] = data
notNil++
}
var diff int32
if c.children[i].rep != parquet.FieldRepetitionType_REQUIRED {
diff++
}
if dl == int32(c.children[i].maxD)-diff {
notNil++
}
}
if notNil == 0 {
return nil, maxD, nil
}
return ret, int32(c.maxD), nil
}
func (c *Column) getFirstRDLevel() (int32, int32, bool) {
if c.data != nil {
return c.data.getRDLevelAt(-1)
}
// there should be at lease 1 child,
for i := range c.children {
rl, dl, last := c.children[i].getFirstRDLevel()
if last {
return rl, dl, last
}
// if this value is not nil, dLevel less than this level is not interesting
if dl == int32(c.children[i].maxD) {
return rl, dl, last
}
}
return -1, -1, false
}
func (c *Column) getData() (interface{}, int32, error) {
if c.children != nil {
data, maxD, err := c.getNextData()
if err != nil {
return nil, 0, err
}
if c.rep != parquet.FieldRepetitionType_REPEATED || data == nil {
return data, maxD, nil
}
ret := []map[string]interface{}{data}
for {
rl, _, last := c.getFirstRDLevel()
if last || rl < int32(c.maxR) || rl == 0 {
// end of this object
return ret, maxD, nil
}
data, _, err := c.getNextData()
if err != nil {
return nil, maxD, err
}
ret = append(ret, data)
}
}
return c.data.get(int32(c.maxD), int32(c.maxR))
}
type schema struct {
schemaDef *parquetschema.SchemaDefinition
root *Column
numRecords int64
readOnly int
// selected columns in reading. if the size is zero, it means all the columns
selectedColumn []string
}
func (r *schema) ensureRoot() {
if r.root == nil {
r.root = &Column{
index: 0,
name: "msg",
flatName: "", // the flat name for root element is empty
data: nil,
children: []*Column{},
rep: 0,
maxR: 0,
maxD: 0,
element: nil,
}
}
}
func (r *schema) setSelectedColumns(selected ...string) {
r.selectedColumn = selected
}
func (r *schema) isSelected(path string) bool {
if len(r.selectedColumn) == 0 {
return true
}
for _, pattern := range r.selectedColumn {
if pattern == path {
return true
}
if strings.HasPrefix(path, pattern+".") {
return true
}
}
return false
}
func (r *schema) getSchemaArray() []*parquet.SchemaElement {
r.ensureRoot()
elem := r.root.getSchemaArray()
// the root doesn't have repetition type
elem[0].RepetitionType = nil
return elem
}
func (r *schema) Columns() []*Column {
var ret []*Column
var fn func([]*Column)
fn = func(columns []*Column) {
for i := range columns {
if columns[i].data != nil {
ret = append(ret, columns[i])
} else {
fn(columns[i].children)
}
}
}
r.ensureRoot()
fn(r.root.children)
return ret
}
func (r *schema) GetColumnByName(path string) *Column {
data := r.Columns()
for i := range data {
if data[i].flatName == path {
return data[i]
}
}
return nil
}
// resetData is useful for resetting data after writing a chunk, to collect data for the next chunk
func (r *schema) resetData() {
data := r.Columns()
for i := range data {
data[i].data.reset(data[i].rep, data[i].maxR, data[i].maxD)
}
r.numRecords = 0
}
func (r *schema) setNumRecords(n int64) {
r.numRecords = n
}
func (r *schema) sortIndex() {
var (
idx int
fn func(c *[]*Column)
)
fn = func(c *[]*Column) {
if c == nil {
return
}
for data := range *c {
if (*c)[data].data != nil {
(*c)[data].index = idx
idx++
} else {
fn(&(*c)[data].children)
}
}
}
r.ensureRoot()
fn(&r.root.children)
}
func (r *schema) SetSchemaDefinition(sd *parquetschema.SchemaDefinition) error {
r.schemaDef = sd
root, err := createColumnFromColumnDefinition(r.schemaDef.RootColumn)
if err != nil {
return err
}
r.root = root
for _, c := range r.root.children {
recursiveFix(c, "", 0, 0)
}
return nil
}
func createColumnFromColumnDefinition(root *parquetschema.ColumnDefinition) (*Column, error) {
params := &ColumnParameters{
LogicalType: root.SchemaElement.LogicalType,
ConvertedType: root.SchemaElement.ConvertedType,
TypeLength: root.SchemaElement.TypeLength,
FieldID: root.SchemaElement.FieldID,
Scale: root.SchemaElement.Scale,
Precision: root.SchemaElement.Precision,
}
col := &Column{
name: root.SchemaElement.GetName(),
rep: root.SchemaElement.GetRepetitionType(),
params: params,
}
if len(root.Children) > 0 {
for _, c := range root.Children {
childColumn, err := createColumnFromColumnDefinition(c)
if err != nil {
return nil, err
}
col.children = append(col.children, childColumn)
}
} else {
dataColumn, err := getColumnStore(root.SchemaElement, params)
if err != nil {
return nil, err
}
col.data = dataColumn
}
col.element = col.buildElement()
return col, nil
}
func getColumnStore(elem *parquet.SchemaElement, params *ColumnParameters) (*ColumnStore, error) {
if elem.Type == nil {
return nil, nil
}
var (
colStore *ColumnStore
err error
)
typ := elem.GetType()
switch typ {
case parquet.Type_BYTE_ARRAY:
colStore, err = NewByteArrayStore(parquet.Encoding_PLAIN, true, params)
case parquet.Type_FLOAT:
colStore, err = NewFloatStore(parquet.Encoding_PLAIN, true, params)
case parquet.Type_DOUBLE:
colStore, err = NewDoubleStore(parquet.Encoding_PLAIN, true, params)
case parquet.Type_BOOLEAN:
colStore, err = NewBooleanStore(parquet.Encoding_PLAIN, params)
case parquet.Type_INT32:
colStore, err = NewInt32Store(parquet.Encoding_PLAIN, true, params)
case parquet.Type_INT64:
colStore, err = NewInt64Store(parquet.Encoding_PLAIN, true, params)
case parquet.Type_INT96:
colStore, err = NewInt96Store(parquet.Encoding_PLAIN, true, params)
case parquet.Type_FIXED_LEN_BYTE_ARRAY:
colStore, err = NewFixedByteArrayStore(parquet.Encoding_PLAIN, true, params)
default:
return nil, fmt.Errorf("unsupported type %q when creating Column store", typ.String())
}
if err != nil {
return nil, fmt.Errorf("creating Column store for type %q failed: %v", typ.String(), err)
}
return colStore, nil
}
// ColumnParameters contains common parameters related to a column.
type ColumnParameters struct {
LogicalType *parquet.LogicalType
ConvertedType *parquet.ConvertedType
TypeLength *int32
FieldID *int32
Scale *int32
Precision *int32
}
// NewDataColumn creates a new data column of the provided field repetition type, using
// the provided column store to write data. Do not use this function to create a group.
func NewDataColumn(store *ColumnStore, rep parquet.FieldRepetitionType) *Column {
return &Column{
data: store,
children: nil,
rep: rep,
params: store.typedColumnStore.params(),
}
}
// NewListColumn return a new LIST column, which is a group of converted type LIST
// with a repeated group named "list" as child which then contains a child which is
// the element column.
func NewListColumn(element *Column, rep parquet.FieldRepetitionType) (*Column, error) {
// the higher level element doesn't need name, but all lower level does.
element.name = "element"
return &Column{
data: nil,
rep: rep,
parent: listParent,
children: []*Column{
{
name: "list",
data: nil,
rep: parquet.FieldRepetitionType_REPEATED,
children: []*Column{element},
},
},
params: &ColumnParameters{
LogicalType: &parquet.LogicalType{
LIST: parquet.NewListType(),
},
ConvertedType: parquet.ConvertedTypePtr(parquet.ConvertedType_LIST),
},
}, nil
}
// NewMapColumn returns a new MAP column, which is a group of converted type LIST
// with a repeated group named "key_value" of converted type MAP_KEY_VALUE. This
// group in turn contains two columns "key" and "value".
func NewMapColumn(key, value *Column, rep parquet.FieldRepetitionType) (*Column, error) {
// the higher level element doesn't need name, but all lower level does.
if key.rep != parquet.FieldRepetitionType_REQUIRED {
return nil, errors.New("the key repetition type should be REQUIRED")
}
key.name = "key"
value.name = "value"
return &Column{
data: nil,
rep: rep,
parent: mapParent,
children: []*Column{
{
name: "key_value",
data: nil,
rep: parquet.FieldRepetitionType_REPEATED,
children: []*Column{
key,
value,
},
params: &ColumnParameters{
ConvertedType: parquet.ConvertedTypePtr(parquet.ConvertedType_MAP_KEY_VALUE),
},
},
},
params: &ColumnParameters{
LogicalType: &parquet.LogicalType{
MAP: parquet.NewMapType(),
},
ConvertedType: parquet.ConvertedTypePtr(parquet.ConvertedType_MAP),
},
}, nil
}
// AddGroup adds a new group to the parquet schema. The provided path is written in dotted notation.
// All parent elements in this dot-separated path need to exist, otherwise the method returns an error.
func (r *schema) AddGroup(path string, rep parquet.FieldRepetitionType) error {
return r.addColumnOrGroup(path, &Column{
children: []*Column{},
data: nil,
rep: rep,
params: &ColumnParameters{},
})
}
// AddColumn adds a single column to the parquet schema. The path is provided in dotted notation. All
// parent elements in this dot-separated path need to exist, otherwise the method returns an error. Any
// data contained in the column store is reset.
func (r *schema) AddColumn(path string, col *Column) error {
return r.addColumnOrGroup(path, col)
}
func recursiveFix(col *Column, path string, maxR, maxD uint16) {
if col.rep != parquet.FieldRepetitionType_REQUIRED {
maxD++
}
if col.rep == parquet.FieldRepetitionType_REPEATED {
maxR++
}
col.maxR = maxR
col.maxD = maxD
col.flatName = path + "." + col.name
if path == "" {
col.flatName = col.name
}
if col.data != nil {
col.data.reset(col.rep, col.maxR, col.maxD)
return
}
for i := range col.children {
recursiveFix(col.children[i], col.flatName, maxR, maxD)
}
}
// do not call this function externally
func (r *schema) addColumnOrGroup(path string, col *Column) error {
if r.readOnly != 0 {
return errors.New("the schema is read only")
}
r.ensureRoot()
pa := strings.Split(path, ".")
name := strings.Trim(pa[len(pa)-1], " \n\r\t")
if name == "" {
return errors.Errorf("the name of the Column is required")
}
col.name = name
c := r.root
for i := 0; i < len(pa)-1; i++ {
found := false
if c.children == nil {
break
}
for j := range c.children {
if c.children[j].name == pa[i] {
found = true
c = c.children[j]
break
}
}
if !found {
return errors.Errorf("path %s failed on %q", path, pa[i])
}
if c.parent != 0 {
return errors.New("can not add a new Column to a list or map logical type")
}
if c.children == nil && i < len(pa)-1 {
return errors.Errorf("path %s is not parent at %q", path, pa[i])
}
}
if c.children == nil {
return errors.New("the children are nil")
}
recursiveFix(col, c.flatName, c.maxR, c.maxD)
c.children = append(c.children, col)
r.sortIndex()
return nil
}
func (r *schema) findDataColumn(path string) (*Column, error) {
pa := strings.Split(path, ".")
r.ensureRoot()
c := r.root.children
var ret *Column
for i := 0; i < len(pa); i++ {
found := false
for j := range c {
if c[j].name == pa[i] {
found = true
ret = c[j]
c = c[j].children
break
}
}
if !found {
return nil, errors.Errorf("path %s failed on %q", path, pa[i])
}
if c == nil && i < len(pa)-1 {
return nil, errors.Errorf("path %s is not parent at %q", path, pa[i])
}
}
if ret == nil || ret.data == nil {
return nil, errors.Errorf("path %s doesnt end on data", path)
}
return ret, nil
}
func (r *schema) AddData(m map[string]interface{}) error {
r.readOnly = 1
r.ensureRoot()
err := recursiveAddColumnData(r.root.children, m, 0, 0, 0)
if err == nil {
r.numRecords++
}
return err
}
func (r *schema) getData() (map[string]interface{}, error) {
d, _, err := r.root.getData()
if err != nil {
return nil, err
}
if d.(map[string]interface{}) == nil {
d = make(map[string]interface{}) // just non nil root doc
}
return d.(map[string]interface{}), nil
}
func recursiveAddColumnNil(c []*Column, defLvl, maxRepLvl uint16, repLvl uint16) error {
for i := range c {
if c[i].data != nil {
if c[i].rep == parquet.FieldRepetitionType_REQUIRED && defLvl == c[i].maxD {
return errors.Errorf("the value %q is required", c[i].flatName)
}
if err := c[i].data.add(nil, defLvl, maxRepLvl, repLvl); err != nil {
return err
}
}
if c[i].children != nil {
if err := recursiveAddColumnNil(c[i].children, defLvl, maxRepLvl, repLvl); err != nil {
return err
}
}
}
return nil
}
func recursiveAddColumnData(c []*Column, m interface{}, defLvl uint16, maxRepLvl uint16, repLvl uint16) error {
var data = m.(map[string]interface{})
for i := range c {
d := data[c[i].name]
if c[i].data != nil {
if err := c[i].data.add(d, defLvl, maxRepLvl, repLvl); err != nil {
return err
}
}
if c[i].children != nil {
l := defLvl
// In case of required value, there is no need to add a definition value, since it should be there always,
// also for nil value, it means we should skip from this level to the lowest level
if c[i].rep != parquet.FieldRepetitionType_REQUIRED && d != nil {
l++
}
switch v := d.(type) {
case nil:
if err := recursiveAddColumnNil(c[i].children, l, maxRepLvl, repLvl); err != nil {
return err
}
case map[string]interface{}: // Not repeated
if c[i].rep == parquet.FieldRepetitionType_REPEATED {
return errors.Errorf("repeated group should be array")
}
if err := recursiveAddColumnData(c[i].children, v, l, maxRepLvl, repLvl); err != nil {
return err
}
case []map[string]interface{}:
if c[i].rep != parquet.FieldRepetitionType_REPEATED {
return errors.Errorf("no repeated group should not be array")
}
m := maxRepLvl + 1
rL := repLvl
if len(v) == 0 {
return recursiveAddColumnNil(c[i].children, l, m, rL)
}
for vi := range v {
if vi > 0 {
rL = m
}
if err := recursiveAddColumnData(c[i].children, v[vi], l, m, rL); err != nil {
return err
}
}
default:
return errors.Errorf("data is not a map or array of map, its a %T", v)
}
}
}
return nil
}
func (c *Column) readColumnSchema(schema []*parquet.SchemaElement, name string, idx int, dLevel, rLevel uint16) (int, error) {
s := schema[idx]
if s.Name == "" {
return 0, errors.Errorf("name in schema on index %d is empty", idx)
}
if s.RepetitionType == nil {
return 0, errors.Errorf("field RepetitionType is nil in index %d", idx)
}
if *s.RepetitionType != parquet.FieldRepetitionType_REQUIRED {
dLevel++
}
if *s.RepetitionType == parquet.FieldRepetitionType_REPEATED {
rLevel++
}
c.element = s
c.maxR = rLevel
c.maxD = dLevel
data, err := getValuesStore(s)
if err != nil {
return 0, err
}
c.rep = *s.RepetitionType
c.data = data
c.flatName = name + "." + s.Name
c.name = s.Name
if name == "" {
c.flatName = s.Name
}
return idx + 1, nil
}
func (c *Column) readGroupSchema(schema []*parquet.SchemaElement, name string, idx int, dLevel, rLevel uint16) (int, error) {
if len(schema) <= idx {
return 0, errors.New("schema index out of bound")
}
s := schema[idx]
if s.Type != nil {
return 0, errors.Errorf("field Type is not nil in index %d", idx)
}
if s.NumChildren == nil {
return 0, errors.Errorf("the field NumChildren is invalid in index %d", idx)
}
if *s.NumChildren <= 0 {
return 0, errors.Errorf("the field NumChildren is zero in index %d", idx)
}
l := int(*s.NumChildren)
if len(schema) <= idx+l {
return 0, errors.Errorf("not enough element in the schema list in index %d", idx)
}
if s.RepetitionType != nil && *s.RepetitionType != parquet.FieldRepetitionType_REQUIRED {
dLevel++
}
if s.RepetitionType != nil && *s.RepetitionType == parquet.FieldRepetitionType_REPEATED {
rLevel++
}
c.maxD = dLevel
c.maxR = rLevel
if name == "" {
name = s.Name
} else {
name += "." + s.Name
}
c.flatName = name
c.name = s.Name
c.element = s
c.children = make([]*Column, 0, l)
c.rep = *s.RepetitionType
var err error
idx++ // move idx from this group to next
for i := 0; i < l; i++ {
if schema[idx].Type == nil {
// another group
child := &Column{}
idx, err = child.readGroupSchema(schema, name, idx, dLevel, rLevel)
if err != nil {
return 0, err
}
c.children = append(c.children, child)
} else {
child := &Column{}
idx, err = child.readColumnSchema(schema, name, idx, dLevel, rLevel)
if err != nil {
return 0, err
}
c.children = append(c.children, child)
}
}
return idx, nil
}
func (r *schema) readSchema(schema []*parquet.SchemaElement) error {
r.readOnly = 1
var err error
for idx := 0; idx < len(schema); {
if schema[idx].Type == nil {
c := &Column{}
idx, err = c.readGroupSchema(schema, "", idx, 0, 0)
if err != nil {
return err
}
r.root.children = append(r.root.children, c)
} else {
c := &Column{}
idx, err = c.readColumnSchema(schema, "", idx, 0, 0)
if err != nil {
return err
}
r.root.children = append(r.root.children, c)
}
}
r.sortIndex()
r.schemaDef = parquetschema.SchemaDefinitionFromColumnDefinition(createColumnDefinitionFromColumn(r.root))
return nil
}
func createColumnDefinitionFromColumn(c *Column) *parquetschema.ColumnDefinition {
col := &parquetschema.ColumnDefinition{
SchemaElement: c.Element(),
}
for _, child := range c.Children() {
col.Children = append(col.Children, createColumnDefinitionFromColumn(child))
}
return col
}
func (r *schema) GetSchemaDefinition() *parquetschema.SchemaDefinition {
def, err := parquetschema.ParseSchemaDefinition(r.schemaDef.String())
if err != nil {
panic(err)
}
return def
}
// DataSize return the size of data stored in the schema right now
func (r *schema) DataSize() int64 {
cols := r.Columns()
var size int64
for i := range cols {
size += cols[i].getDataSize()
}
return size
}
func (r *schema) rowGroupNumRecords() int64 {
return r.numRecords
}
// SchemaCommon contains methods shared by FileReader and FileWriter
// to retrieve and set information related to the parquet schema and
// columns that are used by the reader resp. writer.
type SchemaCommon interface {
// Columns return only data columns, not all columns
Columns() []*Column
// Return a column by its name
GetColumnByName(path string) *Column
// GetSchemaDefinition returns the schema definition.
GetSchemaDefinition() *parquetschema.SchemaDefinition
SetSchemaDefinition(*parquetschema.SchemaDefinition) error
// Internal functions
rowGroupNumRecords() int64
resetData()
getSchemaArray() []*parquet.SchemaElement
}
// SchemaReader is an interface with methods necessary in the FileReader.
type SchemaReader interface {
SchemaCommon
setNumRecords(int64)
getData() (map[string]interface{}, error)
setSelectedColumns(selected ...string)
isSelected(string) bool
}
// SchemaWriter is an interface with methods necessary in the FileWriter
// to add groups and columns and to write data.
type SchemaWriter interface {
SchemaCommon
AddData(m map[string]interface{}) error
AddGroup(path string, rep parquet.FieldRepetitionType) error
AddColumn(path string, col *Column) error
DataSize() int64
}
func makeSchema(meta *parquet.FileMetaData) (SchemaReader, error) {
if len(meta.Schema) < 1 {
return nil, errors.New("no schema element found")
}
s := &schema{
root: &Column{
index: 0,
name: meta.Schema[0].Name,