forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parquet_generated_test.go
2496 lines (2091 loc) · 52.6 KB
/
parquet_generated_test.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 parquet_test
// Code generated by github.com/parsyl/parquet. DO NOT EDIT.
import (
"encoding/binary"
"fmt"
"io"
"math"
"strings"
"github.com/parsyl/parquet"
sch "github.com/parsyl/parquet/schema"
"github.com/valyala/bytebufferpool"
)
var _ = math.MaxInt32 // to avoid unused import
type compression int
const (
compressionUncompressed compression = 0
compressionSnappy compression = 1
compressionGzip compression = 2
compressionUnknown compression = -1
)
var buffpool = bytebufferpool.Pool{}
// ParquetWriter reprents a row group
type ParquetWriter struct {
fields []Field
len int
// child points to the next page
child *ParquetWriter
// max is the number of Record items that can get written before
// a new set of column chunks is written
max int
meta *parquet.Metadata
w io.Writer
compression compression
}
func Fields(compression compression) []Field {
return []Field{
NewInt32Field(readID, writeID, []string{"id"}, fieldCompression(compression)),
NewStringField(readName, writeName, []string{"name"}, fieldCompression(compression)),
NewInt32OptionalField(readAge, writeAge, []string{"age"}, []int{1}, optionalFieldCompression(compression)),
NewInt64Field(readHappiness, writeHappiness, []string{"happiness"}, fieldCompression(compression)),
NewInt64OptionalField(readSadness, writeSadness, []string{"sadness"}, []int{1}, optionalFieldCompression(compression)),
NewStringOptionalField(readCode, writeCode, []string{"code"}, []int{1}, optionalFieldCompression(compression)),
NewFloat32Field(readFunkiness, writeFunkiness, []string{"funkiness"}, fieldCompression(compression)),
NewFloat64Field(readBoldness, writeBoldness, []string{"boldness"}, fieldCompression(compression)),
NewFloat32OptionalField(readLameness, writeLameness, []string{"lameness"}, []int{1}, optionalFieldCompression(compression)),
NewBoolOptionalField(readKeen, writeKeen, []string{"keen"}, []int{1}, optionalFieldCompression(compression)),
NewUint32Field(readBirthday, writeBirthday, []string{"birthday"}, fieldCompression(compression)),
NewUint64OptionalField(readAnniversary, writeAnniversary, []string{"anniversary"}, []int{1}, optionalFieldCompression(compression)),
NewStringField(readBFF, writeBFF, []string{"bff"}, fieldCompression(compression)),
NewBoolField(readHungry, writeHungry, []string{"hungry"}, fieldCompression(compression)),
NewStringOptionalField(readHobbyName, writeHobbyName, []string{"hobby", "name"}, []int{1, 0}, optionalFieldCompression(compression)),
NewInt32OptionalField(readHobbyDifficulty, writeHobbyDifficulty, []string{"hobby", "difficulty"}, []int{1, 1}, optionalFieldCompression(compression)),
NewStringOptionalField(readHobbySkillsName, writeHobbySkillsName, []string{"hobby", "skills", "name"}, []int{1, 2, 0}, optionalFieldCompression(compression)),
NewStringOptionalField(readHobbySkillsDifficulty, writeHobbySkillsDifficulty, []string{"hobby", "skills", "difficulty"}, []int{1, 2, 0}, optionalFieldCompression(compression)),
NewInt32OptionalField(readFriendsID, writeFriendsID, []string{"friends", "id"}, []int{2, 0}, optionalFieldCompression(compression)),
NewStringOptionalField(readFriendsName, writeFriendsName, []string{"friends", "name"}, []int{2, 0}, optionalFieldCompression(compression)),
NewInt32OptionalField(readFriendsAge, writeFriendsAge, []string{"friends", "age"}, []int{2, 1}, optionalFieldCompression(compression)),
NewBoolField(readSleepy, writeSleepy, []string{"Sleepy"}, fieldCompression(compression)),
}
}
func readID(x Person) int32 {
return x.ID
}
func writeID(x *Person, vals []int32) {
x.ID = vals[0]
}
func readName(x Person) string {
return x.Name
}
func writeName(x *Person, vals []string) {
x.Name = vals[0]
}
func readAge(x Person, vals []int32, defs, reps []uint8) ([]int32, []uint8, []uint8) {
switch {
case x.Age == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Age)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeAge(x *Person, vals []int32, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Age = pint32(vals[0])
return 1, 1
}
return 0, 1
}
func readHappiness(x Person) int64 {
return x.Happiness
}
func writeHappiness(x *Person, vals []int64) {
x.Happiness = vals[0]
}
func readSadness(x Person, vals []int64, defs, reps []uint8) ([]int64, []uint8, []uint8) {
switch {
case x.Sadness == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Sadness)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeSadness(x *Person, vals []int64, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Sadness = pint64(vals[0])
return 1, 1
}
return 0, 1
}
func readCode(x Person, vals []string, defs, reps []uint8) ([]string, []uint8, []uint8) {
switch {
case x.Code == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Code)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeCode(x *Person, vals []string, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Code = pstring(vals[0])
return 1, 1
}
return 0, 1
}
func readFunkiness(x Person) float32 {
return x.Funkiness
}
func writeFunkiness(x *Person, vals []float32) {
x.Funkiness = vals[0]
}
func readBoldness(x Person) float64 {
return x.Boldness
}
func writeBoldness(x *Person, vals []float64) {
x.Boldness = vals[0]
}
func readLameness(x Person, vals []float32, defs, reps []uint8) ([]float32, []uint8, []uint8) {
switch {
case x.Lameness == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Lameness)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeLameness(x *Person, vals []float32, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Lameness = pfloat32(vals[0])
return 1, 1
}
return 0, 1
}
func readKeen(x Person, vals []bool, defs, reps []uint8) ([]bool, []uint8, []uint8) {
switch {
case x.Keen == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Keen)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeKeen(x *Person, vals []bool, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Keen = pbool(vals[0])
return 1, 1
}
return 0, 1
}
func readBirthday(x Person) uint32 {
return x.Birthday
}
func writeBirthday(x *Person, vals []uint32) {
x.Birthday = vals[0]
}
func readAnniversary(x Person, vals []uint64, defs, reps []uint8) ([]uint64, []uint8, []uint8) {
switch {
case x.Anniversary == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, *x.Anniversary)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeAnniversary(x *Person, vals []uint64, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Anniversary = puint64(vals[0])
return 1, 1
}
return 0, 1
}
func readBFF(x Person) string {
return x.BFF
}
func writeBFF(x *Person, vals []string) {
x.BFF = vals[0]
}
func readHungry(x Person) bool {
return x.Hungry
}
func writeHungry(x *Person, vals []bool) {
x.Hungry = vals[0]
}
func readHobbyName(x Person, vals []string, defs, reps []uint8) ([]string, []uint8, []uint8) {
switch {
case x.Hobby == nil:
defs = append(defs, 0)
return vals, defs, reps
default:
vals = append(vals, x.Hobby.Name)
defs = append(defs, 1)
return vals, defs, reps
}
}
func writeHobbyName(x *Person, vals []string, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 1:
x.Hobby = &Hobby{Name: vals[0]}
return 1, 1
}
return 0, 1
}
func readHobbyDifficulty(x Person, vals []int32, defs, reps []uint8) ([]int32, []uint8, []uint8) {
switch {
case x.Hobby == nil:
defs = append(defs, 0)
return vals, defs, reps
case x.Hobby.Difficulty == nil:
defs = append(defs, 1)
return vals, defs, reps
default:
vals = append(vals, *x.Hobby.Difficulty)
defs = append(defs, 2)
return vals, defs, reps
}
}
func writeHobbyDifficulty(x *Person, vals []int32, defs, reps []uint8) (int, int) {
def := defs[0]
switch def {
case 2:
x.Hobby.Difficulty = pint32(vals[0])
return 1, 1
}
return 0, 1
}
func readHobbySkillsName(x Person, vals []string, defs, reps []uint8) ([]string, []uint8, []uint8) {
var lastRep uint8
if x.Hobby == nil {
defs = append(defs, 0)
reps = append(reps, lastRep)
} else {
if len(x.Hobby.Skills) == 0 {
defs = append(defs, 1)
reps = append(reps, lastRep)
} else {
for i0, x0 := range x.Hobby.Skills {
if i0 >= 1 {
lastRep = 1
}
defs = append(defs, 2)
reps = append(reps, lastRep)
vals = append(vals, x0.Name)
}
}
}
return vals, defs, reps
}
func writeHobbySkillsName(x *Person, vals []string, defs, reps []uint8) (int, int) {
var nVals, nLevels int
ind := make(indices, 1)
for i := range defs {
def := defs[i]
rep := reps[i]
if i > 0 && rep == 0 {
break
}
nLevels++
ind.rep(rep)
switch def {
case 2:
x.Hobby.Skills = append(x.Hobby.Skills, Skill{Name: vals[nVals]})
nVals++
}
}
return nVals, nLevels
}
func readHobbySkillsDifficulty(x Person, vals []string, defs, reps []uint8) ([]string, []uint8, []uint8) {
var lastRep uint8
if x.Hobby == nil {
defs = append(defs, 0)
reps = append(reps, lastRep)
} else {
if len(x.Hobby.Skills) == 0 {
defs = append(defs, 1)
reps = append(reps, lastRep)
} else {
for i0, x0 := range x.Hobby.Skills {
if i0 >= 1 {
lastRep = 1
}
defs = append(defs, 2)
reps = append(reps, lastRep)
vals = append(vals, x0.Difficulty)
}
}
}
return vals, defs, reps
}
func writeHobbySkillsDifficulty(x *Person, vals []string, defs, reps []uint8) (int, int) {
var nVals, nLevels int
ind := make(indices, 1)
for i := range defs {
def := defs[i]
rep := reps[i]
if i > 0 && rep == 0 {
break
}
nLevels++
ind.rep(rep)
switch def {
case 2:
x.Hobby.Skills[ind[0]].Difficulty = vals[nVals]
nVals++
}
}
return nVals, nLevels
}
func readFriendsID(x Person, vals []int32, defs, reps []uint8) ([]int32, []uint8, []uint8) {
var lastRep uint8
if len(x.Friends) == 0 {
defs = append(defs, 0)
reps = append(reps, lastRep)
} else {
for i0, x0 := range x.Friends {
if i0 >= 1 {
lastRep = 1
}
defs = append(defs, 1)
reps = append(reps, lastRep)
vals = append(vals, x0.ID)
}
}
return vals, defs, reps
}
func writeFriendsID(x *Person, vals []int32, defs, reps []uint8) (int, int) {
var nVals, nLevels int
ind := make(indices, 1)
for i := range defs {
def := defs[i]
rep := reps[i]
if i > 0 && rep == 0 {
break
}
nLevels++
ind.rep(rep)
switch def {
case 1:
x.Friends = append(x.Friends, Being{ID: vals[nVals]})
nVals++
}
}
return nVals, nLevels
}
func readFriendsName(x Person, vals []string, defs, reps []uint8) ([]string, []uint8, []uint8) {
var lastRep uint8
if len(x.Friends) == 0 {
defs = append(defs, 0)
reps = append(reps, lastRep)
} else {
for i0, x0 := range x.Friends {
if i0 >= 1 {
lastRep = 1
}
defs = append(defs, 1)
reps = append(reps, lastRep)
vals = append(vals, x0.Name)
}
}
return vals, defs, reps
}
func writeFriendsName(x *Person, vals []string, defs, reps []uint8) (int, int) {
var nVals, nLevels int
ind := make(indices, 1)
for i := range defs {
def := defs[i]
rep := reps[i]
if i > 0 && rep == 0 {
break
}
nLevels++
ind.rep(rep)
switch def {
case 1:
x.Friends[ind[0]].Name = vals[nVals]
nVals++
}
}
return nVals, nLevels
}
func readFriendsAge(x Person, vals []int32, defs, reps []uint8) ([]int32, []uint8, []uint8) {
var lastRep uint8
if len(x.Friends) == 0 {
defs = append(defs, 0)
reps = append(reps, lastRep)
} else {
for i0, x0 := range x.Friends {
if i0 >= 1 {
lastRep = 1
}
if x0.Age == nil {
defs = append(defs, 1)
reps = append(reps, lastRep)
} else {
defs = append(defs, 2)
reps = append(reps, lastRep)
vals = append(vals, *x0.Age)
}
}
}
return vals, defs, reps
}
func writeFriendsAge(x *Person, vals []int32, defs, reps []uint8) (int, int) {
var nVals, nLevels int
ind := make(indices, 1)
for i := range defs {
def := defs[i]
rep := reps[i]
if i > 0 && rep == 0 {
break
}
nLevels++
ind.rep(rep)
switch def {
case 2:
x.Friends[ind[0]].Age = pint32(vals[nVals])
nVals++
}
}
return nVals, nLevels
}
func readSleepy(x Person) bool {
return x.Sleepy
}
func writeSleepy(x *Person, vals []bool) {
x.Sleepy = vals[0]
}
func fieldCompression(c compression) func(*parquet.RequiredField) {
switch c {
case compressionUncompressed:
return parquet.RequiredFieldUncompressed
case compressionSnappy:
return parquet.RequiredFieldSnappy
case compressionGzip:
return parquet.RequiredFieldGzip
default:
return parquet.RequiredFieldUncompressed
}
}
func optionalFieldCompression(c compression) func(*parquet.OptionalField) {
switch c {
case compressionUncompressed:
return parquet.OptionalFieldUncompressed
case compressionSnappy:
return parquet.OptionalFieldSnappy
case compressionGzip:
return parquet.OptionalFieldGzip
default:
return parquet.OptionalFieldUncompressed
}
}
func NewParquetWriter(w io.Writer, opts ...func(*ParquetWriter) error) (*ParquetWriter, error) {
return newParquetWriter(w, append(opts, begin)...)
}
func newParquetWriter(w io.Writer, opts ...func(*ParquetWriter) error) (*ParquetWriter, error) {
p := &ParquetWriter{
max: 1000,
w: w,
compression: compressionSnappy,
}
for _, opt := range opts {
if err := opt(p); err != nil {
return nil, err
}
}
p.fields = Fields(p.compression)
if p.meta == nil {
ff := Fields(p.compression)
schema := make([]parquet.Field, len(ff))
for i, f := range ff {
schema[i] = f.Schema()
}
p.meta = parquet.New(schema...)
}
return p, nil
}
// MaxPageSize is the maximum number of rows in each row groups' page.
func MaxPageSize(m int) func(*ParquetWriter) error {
return func(p *ParquetWriter) error {
p.max = m
return nil
}
}
var par1 = []byte("PAR1")
func begin(p *ParquetWriter) error {
_, err := p.w.Write(par1)
return err
}
func withMeta(m *parquet.Metadata) func(*ParquetWriter) error {
return func(p *ParquetWriter) error {
p.meta = m
return nil
}
}
func Uncompressed(p *ParquetWriter) error {
p.compression = compressionUncompressed
return nil
}
func Snappy(p *ParquetWriter) error {
p.compression = compressionSnappy
return nil
}
func Gzip(p *ParquetWriter) error {
p.compression = compressionGzip
return nil
}
func withCompression(c compression) func(*ParquetWriter) error {
return func(p *ParquetWriter) error {
p.compression = c
return nil
}
}
func (p *ParquetWriter) Write() error {
for i, f := range p.fields {
if err := f.Write(p.w, p.meta); err != nil {
return err
}
for child := p.child; child != nil; child = child.child {
if err := child.fields[i].Write(p.w, p.meta); err != nil {
return err
}
}
}
p.fields = Fields(p.compression)
p.child = nil
p.len = 0
schema := make([]parquet.Field, len(p.fields))
for i, f := range p.fields {
schema[i] = f.Schema()
}
p.meta.StartRowGroup(schema...)
return nil
}
func (p *ParquetWriter) Close() error {
if err := p.meta.Footer(p.w); err != nil {
return err
}
_, err := p.w.Write(par1)
return err
}
func (p *ParquetWriter) Add(rec Person) {
if p.len == p.max {
if p.child == nil {
// an error can't happen here
p.child, _ = newParquetWriter(p.w, MaxPageSize(p.max), withMeta(p.meta), withCompression(p.compression))
}
p.child.Add(rec)
return
}
p.meta.NextDoc()
for _, f := range p.fields {
f.Add(rec)
}
p.len++
}
type Field interface {
Add(r Person)
Write(w io.Writer, meta *parquet.Metadata) error
Schema() parquet.Field
Scan(r *Person)
Read(r io.ReadSeeker, pg parquet.Page) error
Name() string
Levels() ([]uint8, []uint8)
}
func getFields(ff []Field) map[string]Field {
m := make(map[string]Field, len(ff))
for _, f := range ff {
m[f.Name()] = f
}
return m
}
func NewParquetReader(r io.ReadSeeker, opts ...func(*ParquetReader)) (*ParquetReader, error) {
ff := Fields(compressionUnknown)
pr := &ParquetReader{
r: r,
}
for _, opt := range opts {
opt(pr)
}
schema := make([]parquet.Field, len(ff))
for i, f := range ff {
pr.fieldNames = append(pr.fieldNames, f.Name())
schema[i] = f.Schema()
}
meta := parquet.New(schema...)
if err := meta.ReadFooter(r); err != nil {
return nil, err
}
pr.rows = meta.Rows()
var err error
pr.pages, err = meta.Pages()
if err != nil {
return nil, err
}
pr.rowGroups = meta.RowGroups()
_, err = r.Seek(4, io.SeekStart)
if err != nil {
return nil, err
}
pr.meta = meta
return pr, pr.readRowGroup()
}
func readerIndex(i int) func(*ParquetReader) {
return func(p *ParquetReader) {
p.index = i
}
}
// ParquetReader reads one page from a row group.
type ParquetReader struct {
fields map[string]Field
fieldNames []string
index int
cursor int64
rows int64
rowGroupCursor int64
rowGroupCount int64
pages map[string][]parquet.Page
meta *parquet.Metadata
err error
r io.ReadSeeker
rowGroups []parquet.RowGroup
}
type Levels struct {
Name string
Defs []uint8
Reps []uint8
}
func (p *ParquetReader) Levels() []Levels {
var out []Levels
//for {
for _, name := range p.fieldNames {
f := p.fields[name]
d, r := f.Levels()
out = append(out, Levels{Name: f.Name(), Defs: d, Reps: r})
}
// if err := p.readRowGroup(); err != nil {
// break
// }
//}
return out
}
func (p *ParquetReader) Error() error {
return p.err
}
func (p *ParquetReader) readRowGroup() error {
p.rowGroupCursor = 0
if len(p.rowGroups) == 0 {
p.rowGroupCount = 0
return nil
}
rg := p.rowGroups[0]
p.fields = getFields(Fields(compressionUnknown))
p.rowGroupCount = rg.Rows
p.rowGroupCursor = 0
for _, col := range rg.Columns() {
name := strings.Join(col.MetaData.PathInSchema, ".")
f, ok := p.fields[name]
if !ok {
return fmt.Errorf("unknown field: %s", name)
}
pages := p.pages[name]
if len(pages) <= p.index {
break
}
pg := pages[0]
if err := f.Read(p.r, pg); err != nil {
return fmt.Errorf("unable to read field %s, err: %s", f.Name(), err)
}
p.pages[name] = p.pages[name][1:]
}
p.rowGroups = p.rowGroups[1:]
return nil
}
func (p *ParquetReader) Rows() int64 {
return p.rows
}
func (p *ParquetReader) Next() bool {
if p.err == nil && p.cursor >= p.rows {
return false
}
if p.rowGroupCursor >= p.rowGroupCount {
p.err = p.readRowGroup()
if p.err != nil {
return false
}
}
p.cursor++
p.rowGroupCursor++
return true
}
func (p *ParquetReader) Scan(x *Person) {
if p.err != nil {
return
}
for _, name := range p.fieldNames {
f := p.fields[name]
f.Scan(x)
}
}
type Int32Field struct {
vals []int32
parquet.RequiredField
read func(r Person) int32
write func(r *Person, vals []int32)
stats *int32stats
}
func NewInt32Field(read func(r Person) int32, write func(r *Person, vals []int32), path []string, opts ...func(*parquet.RequiredField)) *Int32Field {
return &Int32Field{
read: read,
write: write,
RequiredField: parquet.NewRequiredField(path, opts...),
stats: newInt32stats(),
}
}
func (f *Int32Field) Schema() parquet.Field {
return parquet.Field{Name: f.Name(), Path: f.Path(), Type: Int32Type, RepetitionType: parquet.RepetitionRequired, Types: []int{0}}
}
func (f *Int32Field) Read(r io.ReadSeeker, pg parquet.Page) error {
rr, _, err := f.DoRead(r, pg)
if err != nil {
return err
}
v := make([]int32, int(pg.N))
err = binary.Read(rr, binary.LittleEndian, &v)
f.vals = append(f.vals, v...)
return err
}
func (f *Int32Field) Write(w io.Writer, meta *parquet.Metadata) error {
buf := buffpool.Get()
defer buffpool.Put(buf)
bs := make([]byte, 4)
for _, v := range f.vals {
binary.LittleEndian.PutUint32(bs, uint32(v))
if _, err := buf.Write(bs); err != nil {
return err
}
}
return f.DoWrite(w, meta, buf.Bytes(), len(f.vals), f.stats)
}
func (f *Int32Field) Scan(r *Person) {
if len(f.vals) == 0 {
return
}
f.write(r, f.vals)
f.vals = f.vals[1:]
}
func (f *Int32Field) Add(r Person) {
v := f.read(r)
f.stats.add(v)
f.vals = append(f.vals, v)
}
func (f *Int32Field) Levels() ([]uint8, []uint8) {
return nil, nil
}
type StringField struct {
parquet.RequiredField
vals []string
read func(r Person) string
write func(r *Person, vals []string)
stats *stringStats
}
func NewStringField(read func(r Person) string, write func(r *Person, vals []string), path []string, opts ...func(*parquet.RequiredField)) *StringField {
return &StringField{
read: read,
write: write,
RequiredField: parquet.NewRequiredField(path, opts...),
stats: newStringStats(),
}
}
func (f *StringField) Schema() parquet.Field {
return parquet.Field{Name: f.Name(), Path: f.Path(), Type: StringType, RepetitionType: parquet.RepetitionRequired, Types: []int{0}}
}
func (f *StringField) Write(w io.Writer, meta *parquet.Metadata) error {
buf := buffpool.Get()
defer buffpool.Put(buf)
bs := make([]byte, 4)
for _, s := range f.vals {
binary.LittleEndian.PutUint32(bs, uint32(len(s)))
if _, err := buf.Write(bs); err != nil {
return err
}
buf.WriteString(s)
}
return f.DoWrite(w, meta, buf.Bytes(), len(f.vals), f.stats)
}
func (f *StringField) Read(r io.ReadSeeker, pg parquet.Page) error {
rr, _, err := f.DoRead(r, pg)
if err != nil {
return err
}
for j := 0; j < pg.N; j++ {
var x int32
if err := binary.Read(rr, binary.LittleEndian, &x); err != nil {