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
/
parquet.go
269 lines (223 loc) · 5.76 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
package parquet
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"github.com/apache/thrift/lib/go/thrift"
sch "github.com/parsyl/parquet/generated"
)
type Field struct {
Name string
Type FieldFunc
RepetitionType FieldFunc
}
type schema struct {
schema []*sch.SchemaElement
lookup map[string]sch.SchemaElement
}
type Metadata struct {
ts *thrift.TSerializer
schema schema
rows int64
rowGroups []rowGroup
//for reading
protocol thrift.TProtocol
metadata *sch.FileMetaData
}
func New(fields ...Field) *Metadata {
ts := thrift.NewTSerializer()
ts.Protocol = thrift.NewTCompactProtocolFactory().GetProtocol(ts.Transport)
m := &Metadata{
ts: ts,
schema: schemaElements(fields),
}
// this is due to my not being sure about the purpose of RowGroup in parquet
m.StartRowGroup(fields...)
return m
}
func (m *Metadata) StartRowGroup(fields ...Field) {
m.rowGroups = append(m.rowGroups, rowGroup{
fields: schemaElements(fields),
columns: make(map[string]sch.ColumnChunk),
})
}
// WritePageHeader indicates you are done writing this columns's chunk
func (m *Metadata) WritePageHeader(w io.Writer, col string, pos int64, dataLen, compressedLen, count int) error {
m.rows += int64(count)
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,
},
}
buf, err := m.ts.Write(context.TODO(), ph)
if err != nil {
return err
}
m.updateRowGroup(col, pos, dataLen, compressedLen, len(buf), count)
_, err = w.Write(buf)
return err
}
func (m *Metadata) updateRowGroup(col string, pos int64, dataLen, compressedLen, headerLen, count int) 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 += int64(count)
err := rg.updateColumnChunk(col, pos, dataLen+headerLen, compressedLen+headerLen, count, m.schema)
m.rowGroups[i-1] = rg
return err
}
func columnType(col string, fields schema) (sch.Type, error) {
for _, f := range fields.schema {
if f.Name == col {
return *f.Type, nil
}
}
return 0, fmt.Errorf("could not find type for column %s", col)
}
func (m *Metadata) Rows() int64 {
return m.metadata.NumRows
}
func (m *Metadata) Footer(w io.Writer) error {
rgs := make([]*sch.RowGroup, len(m.rowGroups))
for i, rg := range m.rowGroups {
for _, col := range rg.fields.schema {
if col.Name == "root" {
continue
}
ch, ok := rg.columns[col.Name]
if !ok {
return fmt.Errorf("unknown column %s", col.Name)
}
rg.rowGroup.TotalByteSize += ch.MetaData.TotalCompressedSize
rg.rowGroup.Columns = append(rg.rowGroup.Columns, &ch)
}
rg.rowGroup.NumRows = rg.rowGroup.NumRows / int64(len(rg.fields.schema)-1)
rgs[i] = &rg.rowGroup
}
f := &sch.FileMetaData{
Version: 1,
Schema: m.schema.schema,
NumRows: m.rows / int64(len(m.schema.schema)-1),
RowGroups: rgs,
}
buf, err := m.ts.Write(context.TODO(), f)
if err != nil {
return err
}
n, err := io.Copy(w, bytes.NewBuffer(buf))
if err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, uint32(n))
}
type rowGroup struct {
fields schema
rowGroup sch.RowGroup
columns map[string]sch.ColumnChunk
child *rowGroup
}
func (r *rowGroup) updateColumnChunk(col string, pos int64, dataLen, compressedLen, count int, fields schema) error {
ch, ok := r.columns[col]
if !ok {
t, err := columnType(col, fields)
if err != nil {
return err
}
ch = sch.ColumnChunk{
FileOffset: pos,
MetaData: &sch.ColumnMetaData{
Type: t,
Encodings: []sch.Encoding{sch.Encoding_PLAIN},
PathInSchema: []string{col},
DataPageOffset: pos,
Codec: sch.CompressionCodec_SNAPPY,
},
}
}
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 {
out := make([]*sch.SchemaElement, len(fields)+1)
m := make(map[string]sch.SchemaElement)
l := int32(len(fields))
rt := sch.FieldRepetitionType_REQUIRED
out[0] = &sch.SchemaElement{
RepetitionType: &rt,
Name: "root",
NumChildren: &l,
}
for i, f := range fields {
var z int32
se := &sch.SchemaElement{
Name: f.Name,
TypeLength: &z,
Scale: &z,
Precision: &z,
FieldID: &z,
}
f.Type(se)
f.RepetitionType(se)
out[i+1] = se
m[f.Name] = *se
}
return schema{schema: out, lookup: m}
}
type FieldFunc func(*sch.SchemaElement)
func RepetitionRequired(se *sch.SchemaElement) {
t := sch.FieldRepetitionType_REQUIRED
se.RepetitionType = &t
}
func RepetitionOptional(se *sch.SchemaElement) {
t := sch.FieldRepetitionType_OPTIONAL
se.RepetitionType = &t
}
func Int32Type(se *sch.SchemaElement) {
t := sch.Type_INT32
se.Type = &t
}
func Uint32Type(se *sch.SchemaElement) {
t := sch.Type_INT32
se.Type = &t
ct := sch.ConvertedType_UINT_32
se.ConvertedType = &ct
}
func Uint64Type(se *sch.SchemaElement) {
t := sch.Type_INT64
se.Type = &t
ct := sch.ConvertedType_UINT_64
se.ConvertedType = &ct
}
func Int64Type(se *sch.SchemaElement) {
t := sch.Type_INT64
se.Type = &t
}
func Float32Type(se *sch.SchemaElement) {
t := sch.Type_FLOAT
se.Type = &t
}
func Float64Type(se *sch.SchemaElement) {
t := sch.Type_DOUBLE
se.Type = &t
}
func BoolType(se *sch.SchemaElement) {
t := sch.Type_BOOLEAN
se.Type = &t
}
func StringType(se *sch.SchemaElement) {
t := sch.Type_BYTE_ARRAY
se.Type = &t
}