-
Notifications
You must be signed in to change notification settings - Fork 13
/
parquet.go
515 lines (444 loc) · 12 KB
/
parquet.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
// Package parquet is not intended to be used as a general library. The
// code generated by the 'parquetgen' command is what actually uses it for
// reading and writing parquet files.
package parquet
import (
"context"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/apache/thrift/lib/go/thrift"
sch "github.com/parsyl/parquet/schema"
)
// Field holds the type information for a parquet column
type Field struct {
Name string
Path []string
Types []int
Type FieldFunc
RepetitionType FieldFunc
}
// Page keeps track of metadata for each ColumnChunk
type Page struct {
// N is the number of values in the ColumnChunk
N int
Size int
Offset int64
Codec sch.CompressionCodec
}
type schema struct {
fields []Field
lookup map[string]sch.SchemaElement
}
func (s schema) schema() (int64, []*sch.SchemaElement) {
out := make([]*sch.SchemaElement, 0, len(s.fields)+1)
out = append(out, &sch.SchemaElement{
Name: "root",
})
var children int32
var z int32
m := map[string]*sch.SchemaElement{}
for _, f := range s.fields {
if len(f.Path) > 1 {
for i, name := range f.Path[:len(f.Path)-1] {
par, ok := m[name]
if !ok {
children++
parts := strings.Split(name, ".")
rt := sch.FieldRepetitionType(f.Types[i])
par = &sch.SchemaElement{
Name: parts[len(parts)-1],
RepetitionType: &rt,
NumChildren: &z,
}
out = append(out, par)
}
n := *par.NumChildren
n++
par.NumChildren = &n
m[name] = par
}
} else if len(f.Path) == 1 {
children++
}
se := &sch.SchemaElement{
Name: f.Path[len(f.Path)-1],
}
f.Type(se)
f.RepetitionType(se)
out = append(out, se)
}
out[0].NumChildren = &children
return int64(len(s.fields)), out
}
// Metadata keeps track of the things that need to
// be kept track of in order to write the FileMetaData
// at the end of the parquet file.
type Metadata struct {
ts *thrift.TSerializer
schema schema
docs int64
pageDocs int64
rowGroupDocs int64
rowGroups []RowGroup
metadata *sch.FileMetaData
}
// Stats is passed in by each column's call to DoWrite
type Stats interface {
NullCount() *int64
DistinctCount() *int64
Min() []byte
Max() []byte
}
// New returns a Metadata struct and reads the first row group
// into memory.
func New(fields ...Field) *Metadata {
ts := thrift.NewTSerializer()
ts.Protocol = thrift.NewTCompactProtocolFactory().GetProtocol(ts.Transport)
m := &Metadata{
ts: ts,
schema: schemaElements(fields),
}
m.StartRowGroup(fields...)
return m
}
// StartRowGroup is called when starting a new row group
func (m *Metadata) StartRowGroup(fields ...Field) {
m.rowGroupDocs = 0
m.rowGroups = append(m.rowGroups, RowGroup{
fields: schemaElements(fields),
columns: make(map[string]sch.ColumnChunk),
})
}
// NextDoc keeps track of how many documents have been
// added to this parquet file. The final value of m.docs
// is used for the FileMetaData.NumRows
func (m *Metadata) NextDoc() {
m.docs++
m.rowGroupDocs++
m.pageDocs++
}
// RowGroups returns a summary of each schema.RowGroup
func (m *Metadata) RowGroups() []RowGroup {
rgs := make([]RowGroup, len(m.metadata.RowGroups))
for i, rg := range m.metadata.RowGroups {
rgs[i] = RowGroup{
rowGroup: *rg,
Rows: rg.NumRows,
}
}
return rgs
}
// WritePageHeader is called in order to finish writing to a column chunk.
func (m *Metadata) WritePageHeader(w io.Writer, pth []string, dataLen, compressedLen, defCount, count int, defLen, repLen int64, comp sch.CompressionCodec, stats Stats) error {
ph := &sch.PageHeader{
Type: sch.PageType_DATA_PAGE,
UncompressedPageSize: int32(dataLen),
CompressedPageSize: int32(compressedLen),
DataPageHeader: &sch.DataPageHeader{
NumValues: int32(count),
Encoding: sch.Encoding_PLAIN,
DefinitionLevelEncoding: sch.Encoding_RLE,
RepetitionLevelEncoding: sch.Encoding_RLE,
Statistics: &sch.Statistics{
NullCount: stats.NullCount(),
DistinctCount: stats.DistinctCount(),
MinValue: stats.Min(),
MaxValue: stats.Max(),
},
},
}
m.pageDocs = 0
buf, err := m.ts.Write(context.TODO(), ph)
if err != nil {
return err
}
if err := m.updateRowGroup(pth, dataLen, compressedLen, len(buf), count, comp); err != nil {
return err
}
_, err = w.Write(buf)
return err
}
func (m *Metadata) updateRowGroup(pth []string, dataLen, compressedLen, headerLen, count int, comp sch.CompressionCodec) error {
i := len(m.rowGroups)
if i == 0 {
return fmt.Errorf("no row groups, you must call StartRowGroup at least once")
}
rg := m.rowGroups[i-1]
rg.rowGroup.NumRows = m.rowGroupDocs
err := rg.updateColumnChunk(pth, dataLen+headerLen, compressedLen+headerLen, count, m.schema, comp)
m.rowGroups[i-1] = rg
return err
}
func columnType(col string, fields schema) (sch.Type, error) {
f, ok := fields.lookup[col]
if !ok {
return 0, fmt.Errorf("could not find type for column %s", col)
}
return *f.Type, nil
}
// Rows return the total number of rows that are being written
// in to a parquet file.
func (m *Metadata) Rows() int64 {
return m.metadata.NumRows
}
// Footer writes the FileMetaData at the end of the file.
func (m *Metadata) Footer(w io.Writer) error {
_, s := m.schema.schema()
fmd := &sch.FileMetaData{
Version: 1,
Schema: s,
NumRows: m.docs,
RowGroups: make([]*sch.RowGroup, 0, len(m.rowGroups)),
}
pos := int64(4)
for _, mrg := range m.rowGroups {
rg := mrg.rowGroup
if rg.NumRows == 0 {
continue
}
for _, col := range mrg.fields.fields {
ch, ok := mrg.columns[strings.Join(col.Path, ".")]
if !ok {
continue
}
ch.FileOffset = pos
ch.MetaData.DataPageOffset = pos
rg.TotalByteSize += ch.MetaData.TotalCompressedSize
rg.Columns = append(rg.Columns, &ch)
pos += ch.MetaData.TotalCompressedSize
}
fmd.RowGroups = append(fmd.RowGroups, &rg)
}
buf, err := m.ts.Write(context.TODO(), fmd)
if err != nil {
return err
}
n, err := w.Write(buf)
if err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, uint32(n))
}
// RowGroup wraps schema.RowGroup and adds accounting functions
// that are used to keep track of number of rows written, byte size,
// etc.
type RowGroup struct {
fields schema
rowGroup sch.RowGroup
columns map[string]sch.ColumnChunk
child *RowGroup
Rows int64
}
// Columns returns the Columns of the row group.
func (r *RowGroup) Columns() []*sch.ColumnChunk {
return r.rowGroup.Columns
}
func (r *RowGroup) updateColumnChunk(pth []string, dataLen, compressedLen, count int, fields schema, comp sch.CompressionCodec) error {
col := strings.Join(pth, ".")
ch, ok := r.columns[col]
if !ok {
t, err := columnType(col, fields)
if err != nil {
return err
}
ch = sch.ColumnChunk{
MetaData: &sch.ColumnMetaData{
Type: t,
Encodings: []sch.Encoding{sch.Encoding_PLAIN},
PathInSchema: pth,
Codec: comp,
},
}
}
ch.MetaData.NumValues += int64(count)
ch.MetaData.TotalUncompressedSize += int64(dataLen)
ch.MetaData.TotalCompressedSize += int64(compressedLen)
r.columns[col] = ch
return nil
}
func schemaElements(fields []Field) schema {
m := make(map[string]sch.SchemaElement)
for _, f := range fields {
se := sch.SchemaElement{
Name: f.Name,
}
f.Type(&se)
f.RepetitionType(&se)
m[strings.Join(f.Path, ".")] = se
}
return schema{lookup: m, fields: fields}
}
// Pages maps each column name to its Pages
func (m *Metadata) Pages() (map[string][]Page, error) {
if len(m.metadata.RowGroups) == 0 {
return nil, nil
}
out := map[string][]Page{}
for _, rg := range m.metadata.RowGroups {
for _, ch := range rg.Columns {
pth := ch.MetaData.PathInSchema
_, ok := m.schema.lookup[strings.Join(pth, ".")]
if !ok {
return nil, fmt.Errorf("could not find schema for %v", pth)
}
pg := Page{
N: int(ch.MetaData.NumValues),
Offset: ch.FileOffset,
Size: int(ch.MetaData.TotalCompressedSize),
Codec: ch.MetaData.Codec,
}
k := strings.Join(pth, ".")
out[k] = append(out[k], pg)
}
}
return out, nil
}
// ReadMetaData reads the FileMetaData from the end of a parquet file
func ReadMetaData(r io.ReadSeeker) (*sch.FileMetaData, error) {
p := thrift.NewTCompactProtocol(&thrift.StreamTransport{Reader: r})
size, err := getMetaDataSize(r)
if err != nil {
return nil, err
}
_, err = r.Seek(-int64(size+8), io.SeekEnd)
if err != nil {
return nil, err
}
m := sch.NewFileMetaData()
return m, m.Read(context.TODO(), p)
}
// ReadFooter reads the parquet metadata
func (m *Metadata) ReadFooter(r io.ReadSeeker) error {
meta, err := ReadMetaData(r)
m.metadata = meta
return err
}
// PageHeader reads the page header from a column page
func PageHeader(r io.Reader) (*sch.PageHeader, error) {
p := thrift.NewTCompactProtocol(&thrift.StreamTransport{Reader: r})
pg := &sch.PageHeader{}
err := pg.Read(context.TODO(), p)
return pg, err
}
// PageHeaders reads all the page headers without reading the actual
// data. It is used by parquetgen to print the page headers.
func PageHeaders(footer *sch.FileMetaData, r io.ReadSeeker) ([]sch.PageHeader, error) {
var pageHeaders []sch.PageHeader
for _, rg := range footer.RowGroups {
for _, col := range rg.Columns {
h, err := PageHeadersAtOffset(r, col.MetaData.DataPageOffset, col.MetaData.NumValues)
if err != nil {
return nil, err
}
pageHeaders = append(pageHeaders, h...)
}
}
return pageHeaders, nil
}
// PageHeadersAtOffset seeks to the given offset, then reads the PageHeader
// without reading the data.
func PageHeadersAtOffset(r io.ReadSeeker, o, n int64) ([]sch.PageHeader, error) {
var out []sch.PageHeader
var nRead int64
_, err := r.Seek(o, io.SeekStart)
if err != nil {
return nil, fmt.Errorf("unable to seek to offset %d, err: %s", o, err)
}
var readOne bool
if n > 0 {
readOne = true
}
for !readOne || nRead < n {
if n == 0 {
readOne = true
}
rc := &readCounter{r: r}
ph, err := PageHeader(rc)
if err != nil {
return nil, fmt.Errorf("unable to read page header: %s", err)
}
out = append(out, *ph)
_, err = r.Seek(int64(ph.CompressedPageSize), io.SeekCurrent)
if err != nil {
return nil, fmt.Errorf("unable to seek to next page: %s", err)
}
nRead += int64(ph.DataPageHeader.NumValues)
}
return out, nil
}
// FieldFunc is used to set some of the metadata for each column
type FieldFunc func(*sch.SchemaElement)
// RepetitionRequired sets the repetition type to required
func RepetitionRequired(se *sch.SchemaElement) {
t := sch.FieldRepetitionType_REQUIRED
se.RepetitionType = &t
}
// RepetitionOptional sets the repetition type to optional
func RepetitionOptional(se *sch.SchemaElement) {
t := sch.FieldRepetitionType_OPTIONAL
se.RepetitionType = &t
}
// RepetitionRepeated sets the repetition type to repeated
func RepetitionRepeated(se *sch.SchemaElement) {
t := sch.FieldRepetitionType_REPEATED
se.RepetitionType = &t
}
var fieldFuncs = []FieldFunc{RepetitionRequired, RepetitionOptional, RepetitionRepeated}
// GetBools reads a byte array and turns each bit into a bool
func GetBools(r io.Reader, n int, pageSizes []int) ([]bool, error) {
var vals [8]bool
data, _ := ioutil.ReadAll(r)
out := make([]bool, 0, n)
for _, nVals := range pageSizes {
if nVals == 0 {
continue
}
l := (nVals / 8)
if nVals%8 > 0 {
l++
}
var i int
chunk := data[:l]
data = data[l:]
for _, b := range chunk {
vals = unpackBools(b)
m := min(nVals, 8)
for j := 0; j < m; j++ {
out = append(out, vals[j])
}
i += m
nVals -= m
}
}
return out, nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func unpackBools(data byte) [8]bool {
x := uint8(data)
return [8]bool{
(x>>0)&1 == 1,
(x>>1)&1 == 1,
(x>>2)&1 == 1,
(x>>3)&1 == 1,
(x>>4)&1 == 1,
(x>>5)&1 == 1,
(x>>6)&1 == 1,
(x>>7)&1 == 1,
}
}
func getMetaDataSize(r io.ReadSeeker) (int, error) {
_, err := r.Seek(-8, io.SeekEnd)
if err != nil {
return 0, err
}
var size uint32
return int(size), binary.Read(r, binary.LittleEndian, &size)
}