-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
402 lines (358 loc) · 10 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"math"
"os"
"reflect"
"unsafe"
"github.com/DataDog/zstd"
"github.com/apache/thrift/lib/go/thrift"
"github.com/golang/snappy"
"parquet/bytearray"
"parquet/double"
"parquet/float"
"parquet/int_32"
"parquet/int_64"
"parquet/parquet"
)
type Test struct {
Test float64 `parquet:"test"`
TestFloat float64 `parquet:"test_float"`
TestInt int64 `parquet:"test_int"`
TestBytes []byte `parquet:"test_str"`
}
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
f, err := os.Open(os.Args[1])
must(err)
data, err := io.ReadAll(f)
must(err)
parquetFile := newFile(data)
destStructs := make([]Test, parquetFile.NumRows())
parse(parquetFile, destStructs)
fmt.Println(destStructs[:5])
}
func parse(f *File, destStructs interface{}) {
ctx := context.TODO()
r := f.r
reps := map[string]parquet.FieldRepetitionType{}
for _, s := range f.metadata.Schema {
reps[s.Name] = *s.RepetitionType
}
previousRowGroupsTotalRows := 0
for _, rowGroup := range f.metadata.RowGroups {
for _, col := range rowGroup.Columns {
fieldIndex := -1
firstElem := reflect.ValueOf(destStructs).Index(0)
ty := firstElem.Type()
structSize := ty.Size()
for i := 0; i < ty.NumField(); i++ {
tag := ty.Field(i).Tag.Get("parquet")
if tag == col.MetaData.PathInSchema[0] {
fieldIndex = i
break
}
}
if fieldIndex == -1 {
fmt.Printf("Ignoring column %s\n", col.MetaData.PathInSchema[0])
continue
// panic("field not found")
}
offset := firstElem.Field(fieldIndex).UnsafeAddr() - firstElem.UnsafeAddr()
var dictVals interface{}
//fmt.Println(col.MetaData)
dictOffset := col.MetaData.DictionaryPageOffset
if dictOffset != nil {
_, err := r.Seek(*dictOffset, io.SeekStart)
must(err)
dictPageHeader, err := readPageHeader(ctx, r)
must(err)
dictVals = readDictPage(col.MetaData, dictPageHeader, r)
//fmt.Println("Dict", dictVals)
}
dataOffset := col.MetaData.DataPageOffset
_, err := r.Seek(dataOffset, io.SeekStart)
must(err)
dataPageHeader, err := readPageHeader(ctx, r)
must(err)
//fmt.Println(col)
baseAddr := uintptr(firstElem.UnsafeAddr()) + uintptr(offset)
fieldPointer := func(idx int) unsafe.Pointer {
rowIdx := uintptr(previousRowGroupsTotalRows + idx)
return unsafe.Pointer(baseAddr + rowIdx*structSize)
}
switch dataPageHeader.DataPageHeader.Encoding {
case parquet.Encoding_PLAIN_DICTIONARY:
defs, vals := readDataPageRLE(col.MetaData, reps[col.MetaData.PathInSchema[0]], dataPageHeader, &byteReader{r})
//fmt.Println(col.MetaData)
//fmt.Println(dataPageHeader)
//fmt.Println(vals)
//fmt.Println(dictVals)
// reflect way is slower but safer
// may want a hybrid approach if we get to decoding nested structures.
//s := reflect.ValueOf(&destStructs[idx]).Elem()
//floatVal := dictVals[v].(float64)
//s.Field(fieldIndex).SetFloat(floatVal)
// TODO: reading nullable values into a non-nullable field results in a 0-value
// We make an exception for floats because pandas encodes NaN as null.
switch col.MetaData.Type {
case parquet.Type_FLOAT:
values := dictVals.([]float32)
for i, v := range vals {
if len(defs) > 0 && defs[i] == 0 {
*(*float32)(fieldPointer(i)) = float32(math.NaN())
} else {
*(*float32)(fieldPointer(i)) = values[v]
}
}
case parquet.Type_DOUBLE:
values := dictVals.([]float64)
for i, v := range vals {
if len(defs) > 0 && defs[i] == 0 {
*(*float64)(fieldPointer(i)) = math.NaN()
} else {
*(*float64)(fieldPointer(i)) = values[v]
}
}
case parquet.Type_BYTE_ARRAY:
values := dictVals.([][]byte)
for i, v := range vals {
*(*[]byte)(fieldPointer(i)) = values[v]
}
case parquet.Type_INT32:
values := dictVals.([]int32)
for i, v := range vals {
*(*int32)(fieldPointer(i)) = values[v]
}
case parquet.Type_INT64:
values := dictVals.([]int64)
for i, v := range vals {
*(*int64)(fieldPointer(i)) = values[v]
}
default:
panic("Cannot read type: " + col.MetaData.Type.String())
}
case parquet.Encoding_PLAIN:
data := readDecompressed(col.MetaData, dataPageHeader, r)
n := int(col.MetaData.NumValues)
switch col.MetaData.Type {
case parquet.Type_FLOAT:
fr := float.NewReader(data)
for i := 0; i < n; i++ {
*(*float32)(fieldPointer(i)) = fr.Next()
}
must(fr.Error())
case parquet.Type_DOUBLE:
dr := double.NewReader(data)
for i := 0; i < n; i++ {
*(*float64)(fieldPointer(i)) = dr.Next()
}
must(dr.Error())
case parquet.Type_INT32:
ir := int_32.NewReader(data)
for i := 0; i < n; i++ {
*(*int32)(fieldPointer(i)) = ir.Next()
}
must(ir.Error())
case parquet.Type_INT64:
ir := int_64.NewReader(data)
for i := 0; i < n; i++ {
*(*int64)(fieldPointer(i)) = ir.Next()
}
must(ir.Error())
case parquet.Type_BYTE_ARRAY:
bar := bytearray.NewReader(data)
for i := 0; i < n; i++ {
*(*[]byte)(fieldPointer(i)) = bar.Next()
}
default:
panic("Cannot read type: " + col.MetaData.Type.String())
}
default:
panic("wrong encoding: " + dataPageHeader.DataPageHeader.Encoding.String())
}
}
previousRowGroupsTotalRows += int(rowGroup.NumRows)
}
}
func readPageHeader(ctx context.Context, r io.Reader) (*parquet.PageHeader, error) {
proto := thrift.NewTCompactProtocol(&thrift.StreamTransport{Reader: r})
pageHeader := parquet.NewPageHeader()
err := pageHeader.Read(ctx, proto)
return pageHeader, err
}
func wrapReader(col *parquet.ColumnMetaData, header *parquet.PageHeader, r io.Reader) io.Reader {
switch col.Codec {
case parquet.CompressionCodec_UNCOMPRESSED:
return r
default:
decompressed := readDecompressed(col, header, r)
return bytes.NewReader(decompressed)
}
}
func readDecompressed(col *parquet.ColumnMetaData, header *parquet.PageHeader, r io.Reader) []byte {
data := make([]byte, header.CompressedPageSize)
_, err := io.ReadFull(r, data)
must(err)
if col.Codec == parquet.CompressionCodec_UNCOMPRESSED {
return data
}
decompressed := make([]byte, header.UncompressedPageSize)
switch col.Codec {
case parquet.CompressionCodec_ZSTD:
decompressed, err = zstd.Decompress(decompressed, data)
case parquet.CompressionCodec_SNAPPY:
decompressed, err = snappy.Decode(decompressed, data)
default:
panic("Unsupported compression: " + col.Codec.String())
}
must(err)
return decompressed
}
func readDictPage(col *parquet.ColumnMetaData, header *parquet.PageHeader, r io.Reader) interface{} {
if header.Type != parquet.PageType_DICTIONARY_PAGE {
panic("wrong page type")
}
data := readDecompressed(col, header, r)
num := header.DictionaryPageHeader.NumValues
switch col.Type {
case parquet.Type_FLOAT:
vals := make([]float32, num)
fr := float.NewReader(data)
for i := range vals {
vals[i] = fr.Next()
}
must(fr.Error())
return vals
case parquet.Type_DOUBLE:
vals := make([]float64, num)
dr := double.NewReader(data)
for i := range vals {
vals[i] = dr.Next()
}
must(dr.Error())
return vals
case parquet.Type_INT32:
vals := make([]int32, num)
ir := int_32.NewReader(data)
for i := range vals {
vals[i] = ir.Next()
}
must(ir.Error())
return vals
case parquet.Type_INT64:
vals := make([]int64, num)
ir := int_64.NewReader(data)
for i := range vals {
vals[i] = ir.Next()
}
must(ir.Error())
return vals
case parquet.Type_BYTE_ARRAY:
vals := make([][]byte, num)
bar := bytearray.NewReader(data)
for i := range vals {
vals[i] = bar.Next()
}
must(bar.Error())
return vals
default:
panic("Cannot read type: " + col.Type.String())
}
}
// readDataPageRLE returns definition levels and values
func readDataPageRLE(
col *parquet.ColumnMetaData,
repType parquet.FieldRepetitionType,
header *parquet.PageHeader,
r reader,
) ([]int32, []int32) {
if header.Type != parquet.PageType_DATA_PAGE {
panic("wrong page type")
}
r = &byteReader{wrapReader(col, header, r)}
switch header.DataPageHeader.Encoding {
case parquet.Encoding_PLAIN_DICTIONARY, parquet.Encoding_PLAIN:
default:
panic("wrong encoding: " + header.DataPageHeader.Encoding.String())
}
buf2 := make([]byte, header.UncompressedPageSize)
_, err2 := io.ReadFull(r, buf2)
must(err2)
//fmt.Println(buf2)
r = bytes.NewReader(buf2)
switch repType {
case parquet.FieldRepetitionType_REQUIRED:
buf := make([]byte, 1)
_, err := io.ReadFull(r, buf)
must(err)
bitWidth := int(buf[0])
fmt.Println("bw", bitWidth)
vals := make([]int32, header.DataPageHeader.NumValues)
hr := &hybridReader{r: r, bitWidth: bitWidth}
for i := range vals {
val, err := hr.Next()
must(err)
vals[i] = val
}
return nil, vals
case parquet.FieldRepetitionType_OPTIONAL:
defs := make([]int32, 0, header.DataPageHeader.NumValues)
//fmt.Println(header)
var size uint32
err := binary.Read(r, binary.LittleEndian, &size)
must(err)
//fmt.Println("size: ", size)
// TODO: This might be repition level actually
// TODO: Figure out where to compute max def level
// TODO: need these to handle nullability correctly?
hr := &hybridReader{
r: &byteReader{io.LimitReader(r, int64(size))},
bitWidth: 1, // max definition level = 1
}
for {
v, err := hr.Next()
if err == io.EOF {
break
}
defs = append(defs, v)
if err != nil {
panic(err)
}
}
buf := make([]byte, 1)
_, err = io.ReadFull(r, buf)
must(err)
bitWidth := int(buf[0])
vals := make([]int32, header.DataPageHeader.NumValues)
hr = &hybridReader{r: r, bitWidth: bitWidth}
for i, defined := range defs {
if defined == 0 {
continue
}
val, err := hr.Next()
must(err)
vals[i] = val
}
return defs, vals
default:
panic("Unsupported")
}
}
type byteReader struct {
io.Reader
}
func (b *byteReader) ReadByte() (byte, error) {
buf := make([]byte, 1)
// TODO: io.ReadFull would be safer
_, err := b.Reader.Read(buf)
return buf[0], err
}