-
Notifications
You must be signed in to change notification settings - Fork 53
/
data_store.go
361 lines (314 loc) · 11.6 KB
/
data_store.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
package goparquet
import (
"math"
"math/bits"
"github.com/fraugster/parquet-go/parquet"
"github.com/pkg/errors"
)
// ColumnStore is the read/write implementation for a column. It buffers a single
// column's data that is to be written to a parquet file, knows how to encode this
// data and will choose an optimal way according to heuristics. It also ensures the
// correct decoding of column data to be read.
type ColumnStore struct {
typedColumnStore
repTyp parquet.FieldRepetitionType
values *dictStore
dLevels *packedArray
rLevels *packedArray
enc parquet.Encoding
readPos int
allowDict bool
skipped bool
}
// useDictionary is simply a function to decide to use dictionary or not,
func (cs *ColumnStore) useDictionary() bool {
if !cs.allowDict {
return false
}
if len(cs.values.data) > math.MaxInt16 {
return false
}
// There is no point for using dictionary if all values are nil
if len(cs.values.data) == 0 || len(cs.values.values) == 0 {
return false
}
dictLen, noDictLen := cs.values.sizes()
return dictLen < noDictLen
}
func (cs *ColumnStore) encoding() parquet.Encoding {
return cs.enc
}
func (cs *ColumnStore) repetitionType() parquet.FieldRepetitionType {
return cs.repTyp
}
func (cs *ColumnStore) reset(rep parquet.FieldRepetitionType, maxR, maxD uint16) {
if cs.typedColumnStore == nil {
panic("generic should be used with typed column store")
}
cs.repTyp = rep
if cs.values == nil {
cs.values = &dictStore{}
cs.rLevels = &packedArray{}
cs.dLevels = &packedArray{}
}
cs.values.init()
cs.rLevels.reset(bits.Len16(maxR))
cs.dLevels.reset(bits.Len16(maxD))
cs.readPos = 0
cs.skipped = false
cs.typedColumnStore.reset(rep)
}
func (cs *ColumnStore) appendRDLevel(rl, dl uint16) {
cs.rLevels.appendSingle(int32(rl))
cs.dLevels.appendSingle(int32(dl))
}
// Add One row, if the value is null, call Add() , if the value is repeated, call all value in array
// the second argument s the definition level
// if there is a data the the result should be true, if there is only null (or empty array), the the result should be false
func (cs *ColumnStore) add(v interface{}, dL uint16, maxRL, rL uint16) error {
// if the current column is repeated, we should increase the maxRL here
if cs.repTyp == parquet.FieldRepetitionType_REPEATED {
maxRL++
}
if rL > maxRL {
rL = maxRL
}
// the dL is a little tricky. there is some case if the REQUIRED field here are nil (since there is something above
// them is nil) they can not be the first level, but if they are in the next levels, is actually ok, but the
// level is one less
if v == nil {
cs.appendRDLevel(rL, dL)
cs.values.addValue(nil, 0)
return nil
}
vals, err := cs.getValues(v)
if err != nil {
return err
}
if len(vals) == 0 {
// the MaxRl might be increased in the beginning and increased again in the next call but for nil its not important
return cs.add(nil, dL, maxRL, rL)
}
for i, j := range vals {
cs.values.addValue(j, cs.sizeOf(j))
tmp := dL
if cs.repTyp != parquet.FieldRepetitionType_REQUIRED {
tmp++
}
if i == 0 {
cs.appendRDLevel(rL, tmp)
} else {
cs.appendRDLevel(maxRL, tmp)
}
}
return nil
}
// getRDLevelAt return the next rLevel in the read position, if there is no value left, it returns true
// if the position is less than zero, then it returns the current position
// NOTE: make sure always r is before d, in any function
func (cs *ColumnStore) getRDLevelAt(pos int) (int32, int32, bool) {
if pos < 0 {
pos = cs.readPos
}
if pos >= cs.rLevels.count || pos >= cs.dLevels.count {
return 0, 0, true
}
dl, err := cs.dLevels.at(pos)
if err != nil {
return 0, 0, true
}
rl, err := cs.rLevels.at(pos)
if err != nil {
return 0, 0, true
}
return rl, dl, false
}
func (cs *ColumnStore) getNext() (v interface{}, err error) {
v, err = cs.values.getNextValue()
if err != nil {
return nil, err
}
return v, nil
}
func (cs *ColumnStore) get(maxD, maxR int32) (interface{}, int32, error) {
if cs.skipped {
return nil, 0, nil
}
if cs.readPos >= cs.rLevels.count || cs.readPos >= cs.dLevels.count {
return nil, 0, errors.New("out of range")
}
_, dl, _ := cs.getRDLevelAt(cs.readPos)
// this is a null value, increase the read pos, for advancing the rLvl and dLvl but
// do not touch the dict-store
if dl < maxD {
cs.readPos++
return nil, dl, nil
}
v, err := cs.getNext()
if err != nil {
return nil, 0, err
}
// if this is not repeated just return the value, the result is not an array
if cs.repTyp != parquet.FieldRepetitionType_REPEATED {
cs.readPos++
return v, maxD, err
}
// the first rLevel in current object is always less than maxR (only for the repeated values)
// the next data in this object, should have maxR as the rLevel. the first rLevel less than maxR means the value
// is from the next object and we should not touch it in this call
var ret = cs.typedColumnStore.append(nil, v)
for {
cs.readPos++
rl, _, last := cs.getRDLevelAt(cs.readPos)
if last || rl < maxR {
// end of this object
return ret, maxD, nil
}
v, err := cs.getNext()
if err != nil {
return nil, maxD, err
}
ret = cs.typedColumnStore.append(ret, v)
}
}
func newStore(typed typedColumnStore, enc parquet.Encoding, allowDict bool) *ColumnStore {
return &ColumnStore{
enc: enc,
allowDict: allowDict,
typedColumnStore: typed,
}
}
func newPlainStore(typed typedColumnStore) *ColumnStore {
return newStore(typed, parquet.Encoding_PLAIN, true)
}
// getValuesStore is internally used for the reader
func getValuesStore(typ *parquet.SchemaElement) (*ColumnStore, error) {
params := &ColumnParameters{
LogicalType: typ.LogicalType,
ConvertedType: typ.ConvertedType,
TypeLength: typ.TypeLength,
Scale: typ.Scale,
Precision: typ.Precision,
}
switch *typ.Type {
case parquet.Type_BOOLEAN:
return newPlainStore(&booleanStore{ColumnParameters: params}), nil
case parquet.Type_BYTE_ARRAY:
return newPlainStore(&byteArrayStore{ColumnParameters: params}), nil
case parquet.Type_FIXED_LEN_BYTE_ARRAY:
if typ.TypeLength == nil {
return nil, errors.Errorf("type %s with nil type len", typ.Type)
}
return newPlainStore(&byteArrayStore{ColumnParameters: params}), nil
case parquet.Type_FLOAT:
return newPlainStore(&floatStore{ColumnParameters: params}), nil
case parquet.Type_DOUBLE:
return newPlainStore(&doubleStore{ColumnParameters: params}), nil
case parquet.Type_INT32:
return newPlainStore(&int32Store{ColumnParameters: params}), nil
case parquet.Type_INT64:
return newPlainStore(&int64Store{ColumnParameters: params}), nil
case parquet.Type_INT96:
store := &int96Store{}
store.ColumnParameters = params
return newPlainStore(store), nil
default:
return nil, errors.Errorf("unsupported type: %s", typ.Type)
}
}
// NewBooleanStore creates new column store to store boolean values.
func NewBooleanStore(enc parquet.Encoding, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN, parquet.Encoding_RLE:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&booleanStore{ColumnParameters: params}, enc, false), nil
}
// NewInt32Store create a new column store to store int32 values. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewInt32Store(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN, parquet.Encoding_DELTA_BINARY_PACKED:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&int32Store{ColumnParameters: params}, enc, allowDict), nil
}
// NewInt64Store creates a new column store to store int64 values. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewInt64Store(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN, parquet.Encoding_DELTA_BINARY_PACKED:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&int64Store{ColumnParameters: params}, enc, allowDict), nil
}
// NewInt96Store creates a new column store to store int96 values. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewInt96Store(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
store := &int96Store{}
store.ColumnParameters = params
return newStore(store, enc, allowDict), nil
}
// NewFloatStore creates a new column store to store float (float32) values. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewFloatStore(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&floatStore{ColumnParameters: params}, enc, allowDict), nil
}
// NewDoubleStore creates a new column store to store double (float64) values. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewDoubleStore(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&doubleStore{ColumnParameters: params}, enc, allowDict), nil
}
// NewByteArrayStore creates a new column store to store byte arrays. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewByteArrayStore(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN, parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY, parquet.Encoding_DELTA_BYTE_ARRAY:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
return newStore(&byteArrayStore{ColumnParameters: params}, enc, allowDict), nil
}
// NewFixedByteArrayStore creates a new column store to store fixed size byte arrays. If allowDict is true,
// then using a dictionary is considered by the column store depending on its heuristics.
// If allowDict is false, a dictionary will never be used to encode the data.
func NewFixedByteArrayStore(enc parquet.Encoding, allowDict bool, params *ColumnParameters) (*ColumnStore, error) {
switch enc {
case parquet.Encoding_PLAIN, parquet.Encoding_DELTA_LENGTH_BYTE_ARRAY, parquet.Encoding_DELTA_BYTE_ARRAY:
default:
return nil, errors.Errorf("encoding %q is not supported on this type", enc)
}
if params.TypeLength == nil {
return nil, errors.New("no length provided")
}
if *params.TypeLength <= 0 {
return nil, errors.Errorf("fix length with len %d is not possible", *params.TypeLength)
}
return newStore(&byteArrayStore{
ColumnParameters: params,
}, enc, allowDict), nil
}