forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
788 lines (688 loc) · 22.8 KB
/
column.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
package parquet
import (
"encoding/binary"
"fmt"
"io"
"reflect"
"github.com/segmentio/parquet-go/compress"
"github.com/segmentio/parquet-go/deprecated"
"github.com/segmentio/parquet-go/encoding"
"github.com/segmentio/parquet-go/format"
"github.com/segmentio/parquet-go/internal/unsafecast"
)
// Column represents a column in a parquet file.
//
// Methods of Column values are safe to call concurrently from multiple
// goroutines.
//
// Column instances satisfy the Node interface.
type Column struct {
typ Type
file *File
schema *format.SchemaElement
order *format.ColumnOrder
path columnPath
columns []*Column
chunks []*format.ColumnChunk
columnIndex []*format.ColumnIndex
offsetIndex []*format.OffsetIndex
encoding encoding.Encoding
compression compress.Codec
depth int8
maxRepetitionLevel byte
maxDefinitionLevel byte
index int16
}
// Type returns the type of the column.
//
// The returned value is unspecified if c is not a leaf column.
func (c *Column) Type() Type { return c.typ }
// Optional returns true if the column is optional.
func (c *Column) Optional() bool { return schemaRepetitionTypeOf(c.schema) == format.Optional }
// Repeated returns true if the column may repeat.
func (c *Column) Repeated() bool { return schemaRepetitionTypeOf(c.schema) == format.Repeated }
// Required returns true if the column is required.
func (c *Column) Required() bool { return schemaRepetitionTypeOf(c.schema) == format.Required }
// Leaf returns true if c is a leaf column.
func (c *Column) Leaf() bool { return c.index >= 0 }
// Fields returns the list of fields on the column.
func (c *Column) Fields() []Field {
fields := make([]Field, len(c.columns))
for i, column := range c.columns {
fields[i] = column
}
return fields
}
// Encoding returns the encodings used by this column.
func (c *Column) Encoding() encoding.Encoding { return c.encoding }
// Compression returns the compression codecs used by this column.
func (c *Column) Compression() compress.Codec { return c.compression }
// Path of the column in the parquet schema.
func (c *Column) Path() []string { return c.path }
// Name returns the column name.
func (c *Column) Name() string { return c.schema.Name }
// Columns returns the list of child columns.
//
// The method returns the same slice across multiple calls, the program must
// treat it as a read-only value.
func (c *Column) Columns() []*Column { return c.columns }
// Column returns the child column matching the given name.
func (c *Column) Column(name string) *Column {
for _, child := range c.columns {
if child.Name() == name {
return child
}
}
return nil
}
// Pages returns a reader exposing all pages in this column, across row groups.
func (c *Column) Pages() Pages {
if c.index < 0 {
return emptyPages{}
}
r := &columnPages{
pages: make([]filePages, len(c.file.rowGroups)),
}
for i := range r.pages {
r.pages[i].init(c.file.rowGroups[i].(*fileRowGroup).columns[c.index].(*fileColumnChunk))
}
return r
}
type columnPages struct {
pages []filePages
index int
}
func (c *columnPages) ReadPage() (Page, error) {
for {
if c.index >= len(c.pages) {
return nil, io.EOF
}
p, err := c.pages[c.index].ReadPage()
if err == nil || err != io.EOF {
return p, err
}
c.index++
}
}
func (c *columnPages) SeekToRow(rowIndex int64) error {
c.index = 0
for c.index < len(c.pages) && c.pages[c.index].chunk.rowGroup.NumRows >= rowIndex {
rowIndex -= c.pages[c.index].chunk.rowGroup.NumRows
c.index++
}
if c.index < len(c.pages) {
if err := c.pages[c.index].SeekToRow(rowIndex); err != nil {
return err
}
for i := range c.pages[c.index:] {
p := &c.pages[c.index+i]
if err := p.SeekToRow(0); err != nil {
return err
}
}
}
return nil
}
func (c *columnPages) Close() error {
var lastErr error
for i := range c.pages {
if err := c.pages[i].Close(); err != nil {
lastErr = err
}
}
c.pages = nil
c.index = 0
return lastErr
}
// Depth returns the position of the column relative to the root.
func (c *Column) Depth() int { return int(c.depth) }
// MaxRepetitionLevel returns the maximum value of repetition levels on this
// column.
func (c *Column) MaxRepetitionLevel() int { return int(c.maxRepetitionLevel) }
// MaxDefinitionLevel returns the maximum value of definition levels on this
// column.
func (c *Column) MaxDefinitionLevel() int { return int(c.maxDefinitionLevel) }
// Index returns the position of the column in a row. Only leaf columns have a
// column index, the method returns -1 when called on non-leaf columns.
func (c *Column) Index() int { return int(c.index) }
// GoType returns the Go type that best represents the parquet column.
func (c *Column) GoType() reflect.Type { return goTypeOf(c) }
// Value returns the sub-value in base for the child column at the given
// index.
func (c *Column) Value(base reflect.Value) reflect.Value {
return base.MapIndex(reflect.ValueOf(&c.schema.Name).Elem())
}
// String returns a human-readable string representation of the column.
func (c *Column) String() string { return c.path.String() + ": " + sprint(c.Name(), c) }
func (c *Column) forEachLeaf(do func(*Column)) {
if len(c.columns) == 0 {
do(c)
} else {
for _, child := range c.columns {
child.forEachLeaf(do)
}
}
}
func openColumns(file *File) (*Column, error) {
cl := columnLoader{}
c, err := cl.open(file, nil)
if err != nil {
return nil, err
}
// Validate that there aren't extra entries in the row group columns,
// which would otherwise indicate that there are dangling data pages
// in the file.
for index, rowGroup := range file.metadata.RowGroups {
if cl.rowGroupColumnIndex != len(rowGroup.Columns) {
return nil, fmt.Errorf("row group at index %d contains %d columns but %d were referenced by the column schemas",
index, len(rowGroup.Columns), cl.rowGroupColumnIndex)
}
}
_, err = c.setLevels(0, 0, 0, 0)
return c, err
}
func (c *Column) setLevels(depth, repetition, definition, index int) (int, error) {
if depth > MaxColumnDepth {
return -1, fmt.Errorf("cannot represent parquet columns with more than %d nested levels: %s", MaxColumnDepth, c.path)
}
if index > MaxColumnIndex {
return -1, fmt.Errorf("cannot represent parquet rows with more than %d columns: %s", MaxColumnIndex, c.path)
}
if repetition > MaxRepetitionLevel {
return -1, fmt.Errorf("cannot represent parquet columns with more than %d repetition levels: %s", MaxRepetitionLevel, c.path)
}
if definition > MaxDefinitionLevel {
return -1, fmt.Errorf("cannot represent parquet columns with more than %d definition levels: %s", MaxDefinitionLevel, c.path)
}
switch schemaRepetitionTypeOf(c.schema) {
case format.Optional:
definition++
case format.Repeated:
repetition++
definition++
}
c.depth = int8(depth)
c.maxRepetitionLevel = byte(repetition)
c.maxDefinitionLevel = byte(definition)
depth++
if len(c.columns) > 0 {
c.index = -1
} else {
c.index = int16(index)
index++
}
var err error
for _, child := range c.columns {
if index, err = child.setLevels(depth, repetition, definition, index); err != nil {
return -1, err
}
}
return index, nil
}
type columnLoader struct {
schemaIndex int
columnOrderIndex int
rowGroupColumnIndex int
}
func (cl *columnLoader) open(file *File, path []string) (*Column, error) {
c := &Column{
file: file,
schema: &file.metadata.Schema[cl.schemaIndex],
}
c.path = c.path.append(c.schema.Name)
cl.schemaIndex++
numChildren := int(c.schema.NumChildren)
if numChildren == 0 {
c.typ = schemaElementTypeOf(c.schema)
if cl.columnOrderIndex < len(file.metadata.ColumnOrders) {
c.order = &file.metadata.ColumnOrders[cl.columnOrderIndex]
cl.columnOrderIndex++
}
rowGroups := file.metadata.RowGroups
rowGroupColumnIndex := cl.rowGroupColumnIndex
cl.rowGroupColumnIndex++
c.chunks = make([]*format.ColumnChunk, 0, len(rowGroups))
c.columnIndex = make([]*format.ColumnIndex, 0, len(rowGroups))
c.offsetIndex = make([]*format.OffsetIndex, 0, len(rowGroups))
for i, rowGroup := range rowGroups {
if rowGroupColumnIndex >= len(rowGroup.Columns) {
return nil, fmt.Errorf("row group at index %d does not have enough columns", i)
}
c.chunks = append(c.chunks, &rowGroup.Columns[rowGroupColumnIndex])
}
if len(file.columnIndexes) > 0 {
for i := range rowGroups {
if rowGroupColumnIndex >= len(file.columnIndexes) {
return nil, fmt.Errorf("row group at index %d does not have enough column index pages", i)
}
c.columnIndex = append(c.columnIndex, &file.columnIndexes[rowGroupColumnIndex])
}
}
if len(file.offsetIndexes) > 0 {
for i := range rowGroups {
if rowGroupColumnIndex >= len(file.offsetIndexes) {
return nil, fmt.Errorf("row group at index %d does not have enough offset index pages", i)
}
c.offsetIndex = append(c.offsetIndex, &file.offsetIndexes[rowGroupColumnIndex])
}
}
if len(c.chunks) > 0 {
// Pick the encoding and compression codec of the first chunk.
//
// Technically each column chunk may use a different compression
// codec, and each page of the column chunk might have a different
// encoding. Exposing these details does not provide a lot of value
// to the end user.
//
// Programs that wish to determine the encoding and compression of
// each page of the column should iterate through the pages and read
// the page headers to determine which compression and encodings are
// applied.
for _, encoding := range c.chunks[0].MetaData.Encoding {
if c.encoding == nil {
c.encoding = LookupEncoding(encoding)
}
if encoding != format.Plain && encoding != format.RLE {
c.encoding = LookupEncoding(encoding)
break
}
}
c.compression = LookupCompressionCodec(c.chunks[0].MetaData.Codec)
}
return c, nil
}
c.typ = &groupType{}
c.columns = make([]*Column, numChildren)
for i := range c.columns {
if cl.schemaIndex >= len(file.metadata.Schema) {
return nil, fmt.Errorf("column %q has more children than there are schemas in the file: %d > %d",
c.schema.Name, cl.schemaIndex+1, len(file.metadata.Schema))
}
var err error
c.columns[i], err = cl.open(file, path)
if err != nil {
return nil, fmt.Errorf("%s: %w", c.schema.Name, err)
}
}
return c, nil
}
func schemaElementTypeOf(s *format.SchemaElement) Type {
if lt := s.LogicalType; lt != nil {
// A logical type exists, the Type interface implementations in this
// package are all based on the logical parquet types declared in the
// format sub-package so we can return them directly via a pointer type
// conversion.
switch {
case lt.UTF8 != nil:
return (*stringType)(lt.UTF8)
case lt.Map != nil:
return (*mapType)(lt.Map)
case lt.List != nil:
return (*listType)(lt.List)
case lt.Enum != nil:
return (*enumType)(lt.Enum)
case lt.Decimal != nil:
// A parquet decimal can be one of several different physical types.
if t := s.Type; t != nil {
var typ Type
switch kind := Kind(*s.Type); kind {
case Int32:
typ = Int32Type
case Int64:
typ = Int64Type
case FixedLenByteArray:
if s.TypeLength == nil {
panic("DECIMAL using FIXED_LEN_BYTE_ARRAY must specify a length")
}
typ = FixedLenByteArrayType(int(*s.TypeLength))
default:
panic("DECIMAL must be of type INT32, INT64, or FIXED_LEN_BYTE_ARRAY but got " + kind.String())
}
return &decimalType{
decimal: *lt.Decimal,
Type: typ,
}
}
case lt.Date != nil:
return (*dateType)(lt.Date)
case lt.Time != nil:
return (*timeType)(lt.Time)
case lt.Timestamp != nil:
return (*timestampType)(lt.Timestamp)
case lt.Integer != nil:
return (*intType)(lt.Integer)
case lt.Unknown != nil:
return (*nullType)(lt.Unknown)
case lt.Json != nil:
return (*jsonType)(lt.Json)
case lt.Bson != nil:
return (*bsonType)(lt.Bson)
case lt.UUID != nil:
return (*uuidType)(lt.UUID)
}
}
if ct := s.ConvertedType; ct != nil {
// This column contains no logical type but has a converted type, it
// was likely created by an older parquet writer. Convert the legacy
// type representation to the equivalent logical parquet type.
switch *ct {
case deprecated.UTF8:
return &stringType{}
case deprecated.Map:
return &mapType{}
case deprecated.MapKeyValue:
return &groupType{}
case deprecated.List:
return &listType{}
case deprecated.Enum:
return &enumType{}
case deprecated.Decimal:
// TODO
case deprecated.Date:
return &dateType{}
case deprecated.TimeMillis:
return &timeType{IsAdjustedToUTC: true, Unit: Millisecond.TimeUnit()}
case deprecated.TimeMicros:
return &timeType{IsAdjustedToUTC: true, Unit: Microsecond.TimeUnit()}
case deprecated.TimestampMillis:
return ×tampType{IsAdjustedToUTC: true, Unit: Millisecond.TimeUnit()}
case deprecated.TimestampMicros:
return ×tampType{IsAdjustedToUTC: true, Unit: Microsecond.TimeUnit()}
case deprecated.Uint8:
return &unsignedIntTypes[0]
case deprecated.Uint16:
return &unsignedIntTypes[1]
case deprecated.Uint32:
return &unsignedIntTypes[2]
case deprecated.Uint64:
return &unsignedIntTypes[3]
case deprecated.Int8:
return &signedIntTypes[0]
case deprecated.Int16:
return &signedIntTypes[1]
case deprecated.Int32:
return &signedIntTypes[2]
case deprecated.Int64:
return &signedIntTypes[3]
case deprecated.Json:
return &jsonType{}
case deprecated.Bson:
return &bsonType{}
case deprecated.Interval:
// TODO
}
}
if t := s.Type; t != nil {
// The column only has a physical type, convert it to one of the
// primitive types supported by this package.
switch kind := Kind(*t); kind {
case Boolean:
return BooleanType
case Int32:
return Int32Type
case Int64:
return Int64Type
case Int96:
return Int96Type
case Float:
return FloatType
case Double:
return DoubleType
case ByteArray:
return ByteArrayType
case FixedLenByteArray:
if s.TypeLength != nil {
return FixedLenByteArrayType(int(*s.TypeLength))
}
}
}
// If we reach this point, we are likely reading a parquet column that was
// written with a non-standard type or is in a newer version of the format
// than this package supports.
return &nullType{}
}
func schemaRepetitionTypeOf(s *format.SchemaElement) format.FieldRepetitionType {
if s.RepetitionType != nil {
return *s.RepetitionType
}
return format.Required
}
func (c *Column) decompress(compressedPageData []byte, uncompressedPageSize int32) (page *buffer, err error) {
page = buffers.get(int(uncompressedPageSize))
page.data, err = c.compression.Decode(page.data, compressedPageData)
if err != nil {
page.unref()
page = nil
}
return page, err
}
// DecodeDataPageV1 decodes a data page from the header, compressed data, and
// optional dictionary passed as arguments.
func (c *Column) DecodeDataPageV1(header DataPageHeaderV1, page []byte, dict Dictionary) (Page, error) {
return c.decodeDataPageV1(header, &buffer{data: page}, dict, -1)
}
func (c *Column) decodeDataPageV1(header DataPageHeaderV1, page *buffer, dict Dictionary, size int32) (Page, error) {
var pageData = page.data
var err error
if isCompressed(c.compression) {
if page, err = c.decompress(pageData, size); err != nil {
return nil, fmt.Errorf("decompressing data page v1: %w", err)
}
defer page.unref()
pageData = page.data
}
var numValues = int(header.NumValues())
var repetitionLevels *buffer
var definitionLevels *buffer
if c.maxRepetitionLevel > 0 {
encoding := lookupLevelEncoding(header.RepetitionLevelEncoding(), c.maxRepetitionLevel)
repetitionLevels, pageData, err = decodeLevelsV1(encoding, numValues, pageData)
if err != nil {
return nil, fmt.Errorf("decoding repetition levels of data page v1: %w", err)
}
defer repetitionLevels.unref()
}
if c.maxDefinitionLevel > 0 {
encoding := lookupLevelEncoding(header.DefinitionLevelEncoding(), c.maxDefinitionLevel)
definitionLevels, pageData, err = decodeLevelsV1(encoding, numValues, pageData)
if err != nil {
return nil, fmt.Errorf("decoding definition levels of data page v1: %w", err)
}
defer definitionLevels.unref()
// Data pages v1 did not embed the number of null values,
// so we have to compute it from the definition levels.
numValues -= countLevelsNotEqual(definitionLevels.data, c.maxDefinitionLevel)
}
page = &buffer{data: pageData}
return c.decodeDataPage(header, numValues, repetitionLevels, definitionLevels, page, dict)
}
// DecodeDataPageV2 decodes a data page from the header, compressed data, and
// optional dictionary passed as arguments.
func (c *Column) DecodeDataPageV2(header DataPageHeaderV2, page []byte, dict Dictionary) (Page, error) {
return c.decodeDataPageV2(header, &buffer{data: page}, dict, -1)
}
func (c *Column) decodeDataPageV2(header DataPageHeaderV2, page *buffer, dict Dictionary, size int32) (Page, error) {
var numValues = int(header.NumValues())
var pageData = page.data
var err error
var repetitionLevels *buffer
var definitionLevels *buffer
if length := header.RepetitionLevelsByteLength(); length > 0 {
if c.maxRepetitionLevel == 0 {
// In some cases we've observed files which have a non-zero
// repetition level despite the column not being repeated
// (nor nested within a repeated column).
//
// See https://github.com/apache/parquet-testing/pull/24
pageData, err = skipLevelsV2(pageData, length)
} else {
encoding := lookupLevelEncoding(header.RepetitionLevelEncoding(), c.maxRepetitionLevel)
repetitionLevels, pageData, err = decodeLevelsV2(encoding, numValues, pageData, length)
}
if err != nil {
return nil, fmt.Errorf("decoding repetition levels of data page v2: %w", io.ErrUnexpectedEOF)
}
if repetitionLevels != nil {
defer repetitionLevels.unref()
}
}
if length := header.DefinitionLevelsByteLength(); length > 0 {
if c.maxDefinitionLevel == 0 {
pageData, err = skipLevelsV2(pageData, length)
} else {
encoding := lookupLevelEncoding(header.DefinitionLevelEncoding(), c.maxDefinitionLevel)
definitionLevels, pageData, err = decodeLevelsV2(encoding, numValues, pageData, length)
}
if err != nil {
return nil, fmt.Errorf("decoding definition levels of data page v2: %w", io.ErrUnexpectedEOF)
}
if definitionLevels != nil {
defer definitionLevels.unref()
}
}
if isCompressed(c.compression) && header.IsCompressed() {
if page, err = c.decompress(pageData, size); err != nil {
return nil, fmt.Errorf("decompressing data page v2: %w", err)
}
defer page.unref()
} else {
page = &buffer{data: pageData}
}
numValues -= int(header.NumNulls())
return c.decodeDataPage(header, numValues, repetitionLevels, definitionLevels, page, dict)
}
func (c *Column) decodeDataPage(header DataPageHeader, numValues int, repetitionLevels, definitionLevels, page *buffer, dict Dictionary) (Page, error) {
pageEncoding := LookupEncoding(header.Encoding())
pageType := c.Type()
if isDictionaryEncoding(pageEncoding) {
// In some legacy configurations, the PLAIN_DICTIONARY encoding is used
// on data page headers to indicate that the page contains indexes into
// the dictionary page, but the page is still encoded using the RLE
// encoding in this case, so we convert it to RLE_DICTIONARY.
pageEncoding = &RLEDictionary
pageType = indexedPageType{newIndexedType(pageType, dict)}
}
var vbuf, obuf *buffer
var pageValues []byte
var pageOffsets []uint32
vbuf = buffers.get(int(pageType.EstimateSize(numValues)))
defer vbuf.unref()
pageValues = vbuf.data
pageKind := pageType.Kind()
// Page offsets not needed when dictionary-encoded
if pageKind == ByteArray && !isDictionaryEncoding(pageEncoding) {
obuf = buffers.get(4 * (numValues + 1))
defer obuf.unref()
pageOffsets = unsafecast.BytesToUint32(obuf.data)
}
values := pageType.NewValues(pageValues, pageOffsets)
values, err := pageType.Decode(values, page.data, pageEncoding)
pageValues, pageOffsets = values.Data()
if vbuf != nil {
vbuf.data = pageValues
}
if obuf != nil {
obuf.data = unsafecast.Uint32ToBytes(pageOffsets)
}
if err != nil {
return nil, err
}
newPage := pageType.NewPage(c.Index(), numValues, values)
switch {
case c.maxRepetitionLevel > 0:
newPage = newRepeatedPage(
newPage,
c.maxRepetitionLevel,
c.maxDefinitionLevel,
repetitionLevels.data,
definitionLevels.data,
)
case c.maxDefinitionLevel > 0:
newPage = newOptionalPage(
newPage,
c.maxDefinitionLevel,
definitionLevels.data,
)
}
bufferRef(vbuf)
bufferRef(obuf)
bufferRef(repetitionLevels)
bufferRef(definitionLevels)
newPage = &bufferedPage{
Page: newPage,
values: vbuf,
offsets: obuf,
repetitionLevels: repetitionLevels,
definitionLevels: definitionLevels,
}
return newPage, nil
}
func decodeLevelsV1(enc encoding.Encoding, numValues int, data []byte) (*buffer, []byte, error) {
if len(data) < 4 {
return nil, data, io.ErrUnexpectedEOF
}
i := 4
j := 4 + int(binary.LittleEndian.Uint32(data))
if j > len(data) {
return nil, data, io.ErrUnexpectedEOF
}
levels, err := decodeLevels(enc, numValues, data[i:j])
return levels, data[j:], err
}
func decodeLevelsV2(enc encoding.Encoding, numValues int, data []byte, length int64) (*buffer, []byte, error) {
levels, err := decodeLevels(enc, numValues, data[:length])
return levels, data[length:], err
}
func decodeLevels(enc encoding.Encoding, numValues int, data []byte) (levels *buffer, err error) {
levels = buffers.get(numValues)
levels.data, err = enc.DecodeLevels(levels.data, data)
if err != nil {
levels.unref()
levels = nil
} else {
switch {
case len(levels.data) < numValues:
err = fmt.Errorf("decoding level expected %d values but got only %d", numValues, len(levels.data))
case len(levels.data) > numValues:
levels.data = levels.data[:numValues]
}
}
return levels, err
}
func skipLevelsV2(data []byte, length int64) ([]byte, error) {
if length >= int64(len(data)) {
return data, io.ErrUnexpectedEOF
}
return data[length:], nil
}
// DecodeDictionary decodes a data page from the header and compressed data
// passed as arguments.
func (c *Column) DecodeDictionary(header DictionaryPageHeader, page []byte) (Dictionary, error) {
return c.decodeDictionary(header, &buffer{data: page}, -1)
}
func (c *Column) decodeDictionary(header DictionaryPageHeader, page *buffer, size int32) (Dictionary, error) {
pageData := page.data
if isCompressed(c.compression) {
var err error
if page, err = c.decompress(pageData, size); err != nil {
return nil, fmt.Errorf("decompressing dictionary page: %w", err)
}
defer page.unref()
pageData = page.data
}
pageType := c.Type()
pageEncoding := header.Encoding()
if pageEncoding == format.PlainDictionary {
pageEncoding = format.Plain
}
numValues := int(header.NumValues())
values := pageType.NewValues(nil, nil)
values, err := pageType.Decode(values, pageData, LookupEncoding(pageEncoding))
if err != nil {
return nil, err
}
return pageType.NewDictionary(int(c.index), numValues, values), nil
}
var (
_ Node = (*Column)(nil)
)