This repository is currently being migrated. It's locked while the migration is in progress.
forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
396 lines (335 loc) · 8.96 KB
/
fields.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
package parquet
import (
"bytes"
"math/bits"
"strings"
"fmt"
"encoding/binary"
"io"
"github.com/golang/snappy"
"github.com/parsyl/parquet/internal/fields"
"github.com/parsyl/parquet/internal/rle"
sch "github.com/parsyl/parquet/schema"
)
// RequiredField writes the raw data for required columns
type IntStats struct {
len int
}
func NewIntStats(len int) IntStats {
return IntStats{len}
}
func (i IntStats) Statistics(min, max int64) *sch.Statistics {
return &sch.Statistics{
MinValue: i.minmax(min),
MaxValue: i.minmax(min),
}
}
func (i IntStats) minmax(val int64) []byte {
buf := make([]byte, i.len)
n := binary.PutVarint(buf, int64(val))
return buf[:n]
}
type UintStats struct {
len int
}
func NewUintStats(len int) UintStats {
return UintStats{len}
}
func (i UintStats) Statistics(min, max uint64) *sch.Statistics {
return &sch.Statistics{
MinValue: i.minmax(min),
MaxValue: i.minmax(min),
}
}
func (i UintStats) minmax(val uint64) []byte {
buf := make([]byte, i.len)
n := binary.PutUvarint(buf, uint64(val))
return buf[:n]
}
type RequiredField struct {
pth []string
compression sch.CompressionCodec
}
// NewRequiredField creates a new required field.
func NewRequiredField(pth []string, opts ...func(*RequiredField)) RequiredField {
r := RequiredField{
pth: pth,
compression: sch.CompressionCodec_SNAPPY,
}
for _, opt := range opts {
opt(&r)
}
return r
}
// RequiredFieldSnappy sets the compression for a column to snappy
// It is an optional arg to NewRequiredField
func RequiredFieldSnappy(r *RequiredField) {
r.compression = sch.CompressionCodec_SNAPPY
}
// RequiredFieldUncompressed sets the compression to none
// It is an optional arg to NewRequiredField
func RequiredFieldUncompressed(r *RequiredField) {
r.compression = sch.CompressionCodec_UNCOMPRESSED
}
// DoWrite writes the actual raw data.
func (f *RequiredField) DoWrite(w io.Writer, meta *Metadata, vals []byte, count int, stats Stats) error {
l, cl, vals := compress(f.compression, vals)
if err := meta.WritePageHeader(w, f.pth, l, cl, count, count, 0, 0, f.compression, stats); err != nil {
return err
}
_, err := w.Write(vals)
return err
}
func (f *RequiredField) DoRead(r io.ReadSeeker, pg Page) (io.Reader, []int, error) {
var nRead int
var out []byte
var sizes []int
for nRead < pg.N {
ph, err := PageHeader(r)
if err != nil {
return nil, nil, err
}
sizes = append(sizes, int(ph.DataPageHeader.NumValues))
data, err := pageData(r, ph, pg)
if err != nil {
return nil, nil, err
}
out = append(out, data...)
nRead += int(ph.DataPageHeader.NumValues)
}
return bytes.NewBuffer(out), sizes, nil
}
func (f *RequiredField) Name() string {
return strings.Join(f.pth, ".")
}
func (f *RequiredField) Path() []string {
return f.pth
}
func (f *RequiredField) Key() string {
return strings.Join(f.pth, ".")
}
type MaxLevel struct {
Def uint8
Rep uint8
}
type OptionalField struct {
Defs []uint8
Reps []uint8
pth []string
MaxLevels MaxLevel
compression sch.CompressionCodec
RepetitionType FieldFunc
Types []int
repeated bool
}
func getRepetitionTypes(in []int) fields.RepetitionTypes {
out := make([]fields.RepetitionType, len(in))
for i, x := range in {
out[i] = fields.RepetitionType(x)
}
return fields.RepetitionTypes(out)
}
func NewOptionalField(pth []string, types []int, opts ...func(*OptionalField)) OptionalField {
rts := getRepetitionTypes(types)
f := OptionalField{
pth: pth,
compression: sch.CompressionCodec_SNAPPY,
MaxLevels: MaxLevel{
Def: rts.MaxDef(),
Rep: rts.MaxRep(),
},
RepetitionType: fieldFuncs[types[len(types)-1]],
Types: types,
repeated: rts.MaxRep() > 0,
}
for _, opt := range opts {
opt(&f)
}
return f
}
// OptionalFieldSnappy sets the compression for a column to snappy
// It is an optional arg to NewOptionalField
func OptionalFieldSnappy(r *OptionalField) {
r.compression = sch.CompressionCodec_SNAPPY
}
// OptionalFieldUncompressed sets the compression to none
// It is an optional arg to NewOptionalField
func OptionalFieldUncompressed(o *OptionalField) {
o.compression = sch.CompressionCodec_UNCOMPRESSED
}
// Values reads the definition levels and uses them
// to return the values from the page data.
func (f *OptionalField) Values() int {
return f.valsFromDefs(f.Defs, uint8(f.MaxLevels.Def))
}
func (f *OptionalField) valsFromDefs(defs []uint8, max uint8) int {
var out int
for _, d := range defs {
if d == max {
out++
}
}
return out
}
// DoWrite is called by all optional field types to write the definition levels
// and raw data to the io.Writer
func (f *OptionalField) DoWrite(w io.Writer, meta *Metadata, vals []byte, count int, stats Stats) error {
buf := bytes.Buffer{}
wc := &writeCounter{w: &buf}
err := writeLevels(wc, f.Defs, int32(bits.Len(uint(f.MaxLevels.Def))))
if err != nil {
return err
}
defLen := wc.n
if f.repeated {
err := writeLevels(wc, f.Reps, int32(bits.Len(uint(f.MaxLevels.Rep))))
if err != nil {
return err
}
}
repLen := wc.n - defLen
wc.Write(vals)
l, cl, vals := compress(f.compression, buf.Bytes())
if err := meta.WritePageHeader(w, f.pth, l, cl, len(f.Defs), count, defLen, repLen, f.compression, stats); err != nil {
return err
}
_, err = w.Write(vals)
return err
}
func (f *OptionalField) NilCount() *int64 {
var out int64
for _, v := range f.Defs {
if v == 0 {
out++
}
}
return &out
}
// DoRead is called by all optional fields. It reads the definition levels and uses
// them to interpret the raw data.
func (f *OptionalField) DoRead(r io.ReadSeeker, pg Page) (io.Reader, []int, error) {
var nRead int
var out []byte
var sizes []int
var rc *readCounter
for nRead < pg.Size {
rc = &readCounter{r: r}
ph, err := PageHeader(rc)
if err != nil {
return nil, nil, err
}
data, err := pageData(rc, ph, pg)
if err != nil {
return nil, nil, err
}
defs, l, err := readLevels(bytes.NewBuffer(data), int32(bits.Len(uint(f.MaxLevels.Def))))
if err != nil {
return nil, nil, err
}
f.Defs = append(f.Defs, defs[:int(ph.DataPageHeader.NumValues)]...)
if f.repeated {
reps, l2, err := readLevels(bytes.NewBuffer(data[l:]), int32(bits.Len(uint(f.MaxLevels.Rep))))
if err != nil {
return nil, nil, err
}
l += l2
f.Reps = append(f.Reps, reps[:int(ph.DataPageHeader.NumValues)]...)
}
n := f.valsFromDefs(defs, uint8(f.MaxLevels.Def))
sizes = append(sizes, n)
out = append(out, data[l:]...)
nRead += int(rc.n)
}
return bytes.NewBuffer(out), sizes, nil
}
// Name returns the column name of this field
func (f *OptionalField) Name() string {
return strings.Join(f.pth, ".")
}
func (f *OptionalField) Path() []string {
return f.pth
}
func (f *OptionalField) Key() string {
return strings.Join(f.pth, ".")
}
// writeCounter keeps track of the number of bytes written
// it is used for calls to binary.Write, which does not
// return the number of bytes written.
type writeCounter struct {
n int64
w io.Writer
}
// Write makes writeCounter an io.Writer
func (w *writeCounter) Write(p []byte) (int, error) {
n, err := w.w.Write(p)
w.n += int64(n)
return n, err
}
// writeCounter keeps track of the number of bytes written
// it is used for calls to binary.Write, which does not
// return the number of bytes written.
type readCounter struct {
n int64
r io.Reader
}
// Write makes writeCounter an io.Writer
func (r *readCounter) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
r.n += int64(n)
return n, err
}
func pageData(r io.Reader, ph *sch.PageHeader, pg Page) ([]byte, error) {
var data []byte
switch pg.Codec {
case sch.CompressionCodec_SNAPPY:
compressed := make([]byte, ph.CompressedPageSize)
if _, err := r.Read(compressed); err != nil {
return nil, err
}
var err error
data, err = snappy.Decode(nil, compressed)
if err != nil {
return nil, err
}
case sch.CompressionCodec_UNCOMPRESSED:
data = make([]byte, ph.UncompressedPageSize)
if _, err := r.Read(data); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported column chunk codec: %s", pg.Codec)
}
return data, nil
}
func compress(codec sch.CompressionCodec, vals []byte) (int, int, []byte) {
var l, cl int
switch codec {
case sch.CompressionCodec_SNAPPY:
l = len(vals)
vals = snappy.Encode(nil, vals)
cl = len(vals)
case sch.CompressionCodec_UNCOMPRESSED:
l = len(vals)
cl = len(vals)
}
return l, cl, vals
}
// writeLevels writes vals to w as RLE/bitpack encoded data
func writeLevels(w io.Writer, levels []uint8, width int32) error {
enc, _ := rle.New(width, len(levels)) //TODO: len(levels) is probably too big. Chop it down a bit?
for _, l := range levels {
enc.Write(l)
}
_, err := w.Write(enc.Bytes())
return err
}
// readLevels reads the RLE/bitpack encoded definition and repetition levels
func readLevels(in io.Reader, width int32) ([]uint8, int, error) {
var out []uint8
dec, _ := rle.New(width, 0)
out, n, err := dec.Read(in)
if err != nil {
return nil, 0, err
}
return out, n, nil
}