-
Notifications
You must be signed in to change notification settings - Fork 53
/
chunk_reader.go
427 lines (375 loc) · 12.8 KB
/
chunk_reader.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
package goparquet
import (
"fmt"
"io"
"math/bits"
"github.com/pkg/errors"
"github.com/fraugster/parquet-go/parquet"
)
type getValueDecoderFn func(parquet.Encoding) (valuesDecoder, error)
type getLevelDecoder func(parquet.Encoding) (levelDecoder, error)
func getDictValuesDecoder(typ *parquet.SchemaElement) (valuesDecoder, error) {
switch *typ.Type {
case parquet.Type_BYTE_ARRAY:
return &byteArrayPlainDecoder{}, nil
case parquet.Type_FIXED_LEN_BYTE_ARRAY:
if typ.TypeLength == nil {
return nil, errors.Errorf("type %s with nil type len", typ)
}
return &byteArrayPlainDecoder{length: int(*typ.TypeLength)}, nil
case parquet.Type_FLOAT:
return &floatPlainDecoder{}, nil
case parquet.Type_DOUBLE:
return &doublePlainDecoder{}, nil
case parquet.Type_INT32:
var unSigned bool
if typ.ConvertedType != nil {
if *typ.ConvertedType == parquet.ConvertedType_UINT_8 || *typ.ConvertedType == parquet.ConvertedType_UINT_16 || *typ.ConvertedType == parquet.ConvertedType_UINT_32 {
unSigned = true
}
}
if typ.LogicalType != nil && typ.LogicalType.INTEGER != nil && !typ.LogicalType.INTEGER.IsSigned {
unSigned = true
}
return &int32PlainDecoder{unSigned: unSigned}, nil
case parquet.Type_INT64:
var unSigned bool
if typ.ConvertedType != nil {
if *typ.ConvertedType == parquet.ConvertedType_UINT_64 {
unSigned = true
}
}
if typ.LogicalType != nil && typ.LogicalType.INTEGER != nil && !typ.LogicalType.INTEGER.IsSigned {
unSigned = true
}
return &int64PlainDecoder{unSigned: unSigned}, nil
case parquet.Type_INT96:
return &int96PlainDecoder{}, nil
}
return nil, errors.Errorf("type %s is not supported for dict value encoder", typ)
}
func getBooleanValuesDecoder(pageEncoding parquet.Encoding, dictValues []interface{}) (valuesDecoder, error) {
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &booleanPlainDecoder{}, nil
case parquet.Encoding_RLE:
return &booleanRLEDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
default:
return nil, errors.Errorf("unsupported encoding %s for boolean", pageEncoding)
}
}
func getByteArrayValuesDecoder(pageEncoding parquet.Encoding, dictValues []interface{}) (valuesDecoder, error) {
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &byteArrayPlainDecoder{}, nil
case parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY:
return &byteArrayDeltaLengthDecoder{}, nil
case parquet.Encoding_DELTA_BYTE_ARRAY:
return &byteArrayDeltaDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
default:
return nil, errors.Errorf("unsupported encoding %s for binary", pageEncoding)
}
}
func getFixedLenByteArrayValuesDecoder(pageEncoding parquet.Encoding, len int, dictValues []interface{}) (valuesDecoder, error) {
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &byteArrayPlainDecoder{length: len}, nil
case parquet.Encoding_DELTA_BYTE_ARRAY:
return &byteArrayDeltaDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
default:
return nil, errors.Errorf("unsupported encoding %s for fixed_len_byte_array(%d)", pageEncoding, len)
}
}
func getInt32ValuesDecoder(pageEncoding parquet.Encoding, typ *parquet.SchemaElement, dictValues []interface{}) (valuesDecoder, error) {
var unSigned bool
if typ.ConvertedType != nil {
if *typ.ConvertedType == parquet.ConvertedType_UINT_8 || *typ.ConvertedType == parquet.ConvertedType_UINT_16 || *typ.ConvertedType == parquet.ConvertedType_UINT_32 {
unSigned = true
}
}
if typ.LogicalType != nil && typ.LogicalType.INTEGER != nil && !typ.LogicalType.INTEGER.IsSigned {
unSigned = true
}
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &int32PlainDecoder{unSigned: unSigned}, nil
case parquet.Encoding_DELTA_BINARY_PACKED:
return &int32DeltaBPDecoder{unSigned: unSigned}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
default:
return nil, errors.Errorf("unsupported encoding %s for int32", pageEncoding)
}
}
func getInt64ValuesDecoder(pageEncoding parquet.Encoding, typ *parquet.SchemaElement, dictValues []interface{}) (valuesDecoder, error) {
var unSigned bool
if typ.ConvertedType != nil {
if *typ.ConvertedType == parquet.ConvertedType_UINT_64 {
unSigned = true
}
}
if typ.LogicalType != nil && typ.LogicalType.INTEGER != nil && !typ.LogicalType.INTEGER.IsSigned {
unSigned = true
}
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &int64PlainDecoder{unSigned: unSigned}, nil
case parquet.Encoding_DELTA_BINARY_PACKED:
return &int64DeltaBPDecoder{unSigned: unSigned}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
default:
return nil, errors.Errorf("unsupported encoding %s for int64", pageEncoding)
}
}
func getValuesDecoder(pageEncoding parquet.Encoding, typ *parquet.SchemaElement, dictValues []interface{}) (valuesDecoder, error) {
// Change the deprecated value
if pageEncoding == parquet.Encoding_PLAIN_DICTIONARY {
pageEncoding = parquet.Encoding_RLE_DICTIONARY
}
switch *typ.Type {
case parquet.Type_BOOLEAN:
return getBooleanValuesDecoder(pageEncoding, dictValues)
case parquet.Type_BYTE_ARRAY:
return getByteArrayValuesDecoder(pageEncoding, dictValues)
case parquet.Type_FIXED_LEN_BYTE_ARRAY:
if typ.TypeLength == nil {
return nil, errors.Errorf("type %s with nil type len", typ.Type)
}
return getFixedLenByteArrayValuesDecoder(pageEncoding, int(*typ.TypeLength), dictValues)
case parquet.Type_FLOAT:
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &floatPlainDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
}
case parquet.Type_DOUBLE:
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &doublePlainDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
}
case parquet.Type_INT32:
return getInt32ValuesDecoder(pageEncoding, typ, dictValues)
case parquet.Type_INT64:
return getInt64ValuesDecoder(pageEncoding, typ, dictValues)
case parquet.Type_INT96:
switch pageEncoding {
case parquet.Encoding_PLAIN:
return &int96PlainDecoder{}, nil
case parquet.Encoding_RLE_DICTIONARY:
return &dictDecoder{values: dictValues}, nil
}
default:
return nil, errors.Errorf("unsupported type %s", typ.Type)
}
return nil, errors.Errorf("unsupported encoding %s for %s type", pageEncoding, typ.Type)
}
func createDataReader(r io.Reader, codec parquet.CompressionCodec, compressedSize int32, uncompressedSize int32) (io.Reader, error) {
if compressedSize < 0 || uncompressedSize < 0 {
return nil, errors.New("invalid page data size")
}
return newBlockReader(r, codec, compressedSize, uncompressedSize)
}
func readPages(r *offsetReader, col *Column, chunkMeta *parquet.ColumnMetaData, dDecoder, rDecoder getLevelDecoder) ([]pageReader, error) {
var (
dictPage *dictPageReader
pages []pageReader
)
for {
if chunkMeta.TotalCompressedSize-r.Count() <= 0 {
break
}
ph := &parquet.PageHeader{}
if err := readThrift(ph, r); err != nil {
return nil, err
}
if ph.Type == parquet.PageType_DICTIONARY_PAGE {
if dictPage != nil {
return nil, errors.New("there should be only one dictionary")
}
p := &dictPageReader{}
de, err := getDictValuesDecoder(col.Element())
if err != nil {
return nil, err
}
if err := p.init(de); err != nil {
return nil, err
}
// re-use the value dictionary store
p.values = col.getColumnStore().values.values
if err := p.read(r, ph, chunkMeta.Codec); err != nil {
return nil, err
}
dictPage = p
// Go to the next data Page
// if we have a DictionaryPageOffset we should return to DataPageOffset
if chunkMeta.DictionaryPageOffset != nil {
if *chunkMeta.DictionaryPageOffset != r.offset {
if _, err := r.Seek(chunkMeta.DataPageOffset, io.SeekStart); err != nil {
return nil, err
}
}
}
continue // go to next page
}
var p pageReader
switch ph.Type {
case parquet.PageType_DATA_PAGE:
p = &dataPageReaderV1{
ph: ph,
}
case parquet.PageType_DATA_PAGE_V2:
p = &dataPageReaderV2{
ph: ph,
}
default:
return nil, errors.Errorf("DATA_PAGE or DATA_PAGE_V2 type supported, but was %s", ph.Type)
}
var dictValue []interface{}
if dictPage != nil {
dictValue = dictPage.values
}
var fn = func(typ parquet.Encoding) (valuesDecoder, error) {
return getValuesDecoder(typ, col.Element(), dictValue)
}
if err := p.init(dDecoder, rDecoder, fn); err != nil {
return nil, err
}
if err := p.read(r, ph, chunkMeta.Codec); err != nil {
return nil, err
}
pages = append(pages, p)
}
return pages, nil
}
func skipChunk(r io.Seeker, col *Column, chunk *parquet.ColumnChunk) error {
if chunk.FilePath != nil {
return fmt.Errorf("nyi: data is in another file: '%s'", *chunk.FilePath)
}
c := col.Index()
// chunk.FileOffset is useless so ChunkMetaData is required here
// as we cannot read it from r
// see https://issues.apache.org/jira/browse/PARQUET-291
if chunk.MetaData == nil {
return errors.Errorf("missing meta data for Column %c", c)
}
if typ := *col.Element().Type; chunk.MetaData.Type != typ {
return errors.Errorf("wrong type in Column chunk metadata, expected %s was %s",
typ, chunk.MetaData.Type)
}
offset := chunk.MetaData.DataPageOffset
if chunk.MetaData.DictionaryPageOffset != nil {
offset = *chunk.MetaData.DictionaryPageOffset
}
offset += chunk.MetaData.TotalCompressedSize
_, err := r.Seek(offset, io.SeekStart)
return err
}
func readChunk(r io.ReadSeeker, col *Column, chunk *parquet.ColumnChunk) ([]pageReader, error) {
if chunk.FilePath != nil {
return nil, fmt.Errorf("nyi: data is in another file: '%s'", *chunk.FilePath)
}
c := col.Index()
// chunk.FileOffset is useless so ChunkMetaData is required here
// as we cannot read it from r
// see https://issues.apache.org/jira/browse/PARQUET-291
if chunk.MetaData == nil {
return nil, errors.Errorf("missing meta data for Column %c", c)
}
if typ := *col.Element().Type; chunk.MetaData.Type != typ {
return nil, errors.Errorf("wrong type in Column chunk metadata, expected %s was %s",
typ, chunk.MetaData.Type)
}
offset := chunk.MetaData.DataPageOffset
if chunk.MetaData.DictionaryPageOffset != nil {
offset = *chunk.MetaData.DictionaryPageOffset
}
// Seek to the beginning of the first Page
_, err := r.Seek(offset, io.SeekStart)
if err != nil {
return nil, err
}
reader := &offsetReader{
inner: r,
offset: offset,
count: 0,
}
rDecoder := func(enc parquet.Encoding) (levelDecoder, error) {
if enc != parquet.Encoding_RLE {
return nil, errors.Errorf("%q is not supported for definition and repetition level", enc)
}
dec := newHybridDecoder(bits.Len16(col.MaxRepetitionLevel()))
dec.buffered = true
return &levelDecoderWrapper{decoder: dec, max: col.MaxRepetitionLevel()}, nil
}
dDecoder := func(enc parquet.Encoding) (levelDecoder, error) {
if enc != parquet.Encoding_RLE {
return nil, errors.Errorf("%q is not supported for definition and repetition level", enc)
}
dec := newHybridDecoder(bits.Len16(col.MaxDefinitionLevel()))
dec.buffered = true
return &levelDecoderWrapper{decoder: dec, max: col.MaxDefinitionLevel()}, nil
}
if col.MaxRepetitionLevel() == 0 {
rDecoder = func(parquet.Encoding) (levelDecoder, error) {
return &levelDecoderWrapper{decoder: constDecoder(0), max: col.MaxRepetitionLevel()}, nil
}
}
if col.MaxDefinitionLevel() == 0 {
dDecoder = func(parquet.Encoding) (levelDecoder, error) {
return &levelDecoderWrapper{decoder: constDecoder(0), max: col.MaxDefinitionLevel()}, nil
}
}
return readPages(reader, col, chunk.MetaData, dDecoder, rDecoder)
}
func readPageData(col *Column, pages []pageReader) error {
s := col.getColumnStore()
for i := range pages {
data := make([]interface{}, pages[i].numValues())
n, dl, rl, err := pages[i].readValues(data)
if err != nil {
return err
}
if int32(n) != pages[i].numValues() {
return errors.Errorf("expect %d value but read %d", pages[i].numValues(), n)
}
// using append to make sure we handle the multiple data page correctly
s.rLevels.appendArray(rl)
s.dLevels.appendArray(dl)
s.values.values = append(s.values.values, data...)
s.values.noDictMode = true
}
return nil
}
func readRowGroup(r io.ReadSeeker, schema SchemaReader, rowGroups *parquet.RowGroup) error {
dataCols := schema.Columns()
schema.resetData()
schema.setNumRecords(rowGroups.NumRows)
for _, c := range dataCols {
chunk := rowGroups.Columns[c.Index()]
if !schema.isSelected(c.flatName) {
if err := skipChunk(r, c, chunk); err != nil {
return err
}
c.data.skipped = true
continue
}
pages, err := readChunk(r, c, chunk)
if err != nil {
return err
}
if err := readPageData(c, pages); err != nil {
return err
}
}
return nil
}