forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_bytearray.go
396 lines (331 loc) · 8.75 KB
/
type_bytearray.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 goparquet
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/fraugster/parquet-go/parquet"
)
type byteArrayPlainDecoder struct {
r io.Reader
// if the length is set, then this is a fix size array decoder, unless it reads the len first
length int
}
func (b *byteArrayPlainDecoder) init(r io.Reader) error {
b.r = r
return nil
}
func (b *byteArrayPlainDecoder) next() ([]byte, error) {
var l = int32(b.length)
if l == 0 {
if err := binary.Read(b.r, binary.LittleEndian, &l); err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("bytearray/plain: len is negative")
}
} else if l < 0 {
return nil, errors.New("bytearray/plain: len is negative")
}
buf := make([]byte, l)
_, err := io.ReadFull(b.r, buf)
if err != nil {
return nil, err
}
return buf, nil
}
func (b *byteArrayPlainDecoder) decodeValues(dst []interface{}) (int, error) {
var err error
for i := range dst {
if dst[i], err = b.next(); err != nil {
return i, err
}
}
return len(dst), nil
}
type byteArrayPlainEncoder struct {
w io.Writer
length int
}
func (b *byteArrayPlainEncoder) init(w io.Writer) error {
b.w = w
return nil
}
func (b *byteArrayPlainEncoder) writeBytes(data []byte) error {
l := b.length
if l == 0 { // variable length
l = len(data)
l32 := int32(l)
if err := binary.Write(b.w, binary.LittleEndian, l32); err != nil {
return err
}
} else if len(data) != l {
return fmt.Errorf("the byte array should be with length %d but is %d", l, len(data))
}
return writeFull(b.w, data)
}
func (b *byteArrayPlainEncoder) encodeValues(values []interface{}) error {
for i := range values {
if err := b.writeBytes(values[i].([]byte)); err != nil {
return err
}
}
return nil
}
func (*byteArrayPlainEncoder) Close() error {
return nil
}
type byteArrayDeltaLengthDecoder struct {
r io.Reader
position int
lens []int32
}
func (b *byteArrayDeltaLengthDecoder) init(r io.Reader) error {
b.r = r
b.position = 0
lensDecoder := int32DeltaBPDecoder{}
if err := lensDecoder.init(r); err != nil {
return err
}
b.lens = make([]int32, lensDecoder.valuesCount)
return decodeInt32(&lensDecoder, b.lens)
}
func (b *byteArrayDeltaLengthDecoder) next() ([]byte, error) {
if b.position >= len(b.lens) {
return nil, io.EOF
}
size := int(b.lens[b.position])
value := make([]byte, size)
if _, err := io.ReadFull(b.r, value); err != nil {
return nil, fmt.Errorf("there is no byte left: %w", err)
}
b.position++
return value, nil
}
func (b *byteArrayDeltaLengthDecoder) decodeValues(dst []interface{}) (int, error) {
total := len(dst)
for i := 0; i < total; i++ {
v, err := b.next()
if err != nil {
return i, err
}
dst[i] = v
}
return total, nil
}
// this type is used inside the byteArrayDeltaEncoder, the Close method should do the actual write, not before.
type byteArrayDeltaLengthEncoder struct {
w io.Writer
buf *bytes.Buffer
lens []interface{}
}
func (b *byteArrayDeltaLengthEncoder) init(w io.Writer) error {
b.w = w
b.buf = &bytes.Buffer{}
return nil
}
func (b *byteArrayDeltaLengthEncoder) writeOne(data []byte) error {
b.lens = append(b.lens, int32(len(data)))
return writeFull(b.buf, data)
}
func (b *byteArrayDeltaLengthEncoder) encodeValues(values []interface{}) error {
if b.lens == nil {
// this is just for the first time, maybe we need to copy and increase the cap in the next calls?
b.lens = make([]interface{}, 0, len(values))
}
for i := range values {
if err := b.writeOne(values[i].([]byte)); err != nil {
return err
}
}
return nil
}
func (b *byteArrayDeltaLengthEncoder) Close() error {
enc := &int32DeltaBPEncoder{
deltaBitPackEncoder32: deltaBitPackEncoder32{
blockSize: 128,
miniBlockCount: 4,
},
}
if err := encodeValue(b.w, enc, b.lens); err != nil {
return err
}
return writeFull(b.w, b.buf.Bytes())
}
type byteArrayDeltaDecoder struct {
suffixDecoder byteArrayDeltaLengthDecoder
prefixLens []int32
previousValue []byte
}
func (d *byteArrayDeltaDecoder) init(r io.Reader) error {
lensDecoder := deltaBitPackDecoder32{}
if err := lensDecoder.init(r); err != nil {
return err
}
d.prefixLens = make([]int32, lensDecoder.valuesCount)
if err := decodeInt32(&lensDecoder, d.prefixLens); err != nil {
return err
}
if err := d.suffixDecoder.init(r); err != nil {
return err
}
if len(d.prefixLens) != len(d.suffixDecoder.lens) {
return errors.New("bytearray/delta: different number of suffixes and prefixes")
}
d.previousValue = make([]byte, 0)
return nil
}
func (d *byteArrayDeltaDecoder) decodeValues(dst []interface{}) (int, error) {
total := len(dst)
for i := 0; i < total; i++ {
suffix, err := d.suffixDecoder.next()
if err != nil {
return i, err
}
// after this line no error is acceptable
prefixLen := int(d.prefixLens[d.suffixDecoder.position-1])
value := make([]byte, 0, prefixLen+len(suffix))
if len(d.previousValue) < prefixLen {
// prevent panic from invalid input
return 0, fmt.Errorf("invalid prefix len in the stream, the value is %d byte but the it needs %d byte", len(d.previousValue), prefixLen)
}
if prefixLen > 0 {
value = append(value, d.previousValue[:prefixLen]...)
}
value = append(value, suffix...)
d.previousValue = value
dst[i] = value
}
return total, nil
}
type byteArrayDeltaEncoder struct {
w io.Writer
prefixLens []interface{}
previousValue []byte
values *byteArrayDeltaLengthEncoder
}
func (b *byteArrayDeltaEncoder) init(w io.Writer) error {
b.w = w
b.prefixLens = nil
b.previousValue = []byte{}
b.values = &byteArrayDeltaLengthEncoder{}
return b.values.init(w)
}
func (b *byteArrayDeltaEncoder) encodeValues(values []interface{}) error {
if b.prefixLens == nil {
b.prefixLens = make([]interface{}, 0, len(values))
b.values.lens = make([]interface{}, 0, len(values))
}
for i := range values {
data := values[i].([]byte)
pLen := prefix(b.previousValue, data)
b.prefixLens = append(b.prefixLens, int32(pLen))
if err := b.values.writeOne(data[pLen:]); err != nil {
return err
}
b.previousValue = data
}
return nil
}
func (b *byteArrayDeltaEncoder) Close() error {
// write the lens first
enc := &int32DeltaBPEncoder{
deltaBitPackEncoder32: deltaBitPackEncoder32{
blockSize: 128,
miniBlockCount: 4,
},
}
if err := encodeValue(b.w, enc, b.prefixLens); err != nil {
return err
}
return b.values.Close()
}
type byteArrayStore struct {
repTyp parquet.FieldRepetitionType
stats statistics
pageStats statistics
*ColumnParameters
}
func (is *byteArrayStore) getStats() minMaxValues {
return &is.stats
}
func (is *byteArrayStore) getPageStats() minMaxValues {
return &is.pageStats
}
func (is *byteArrayStore) params() *ColumnParameters {
if is.ColumnParameters == nil {
panic("ColumnParameters is nil")
}
return is.ColumnParameters
}
func (is *byteArrayStore) sizeOf(v interface{}) int {
if vv, ok := v.([][]byte); ok {
l := 0
for _, vvv := range vv {
l += len(vvv)
}
return l
}
return len(v.([]byte))
}
func (is *byteArrayStore) parquetType() parquet.Type {
if is.TypeLength != nil && *is.TypeLength > 0 {
return parquet.Type_FIXED_LEN_BYTE_ARRAY
}
return parquet.Type_BYTE_ARRAY
}
func (is *byteArrayStore) repetitionType() parquet.FieldRepetitionType {
return is.repTyp
}
func (is *byteArrayStore) reset(repetitionType parquet.FieldRepetitionType) {
is.repTyp = repetitionType
is.stats.reset()
is.pageStats.reset()
}
func (is *byteArrayStore) setMinMax(j []byte) error {
if is.TypeLength != nil && *is.TypeLength > 0 && int32(len(j)) != *is.TypeLength {
return fmt.Errorf("the size of data should be %d but is %d", *is.TypeLength, len(j))
}
// For nil value there is no need to set the min/max
if j == nil {
return nil
}
is.stats.setMinMax(j)
is.pageStats.setMinMax(j)
return nil
}
func (is *byteArrayStore) getValues(v interface{}) ([]interface{}, error) {
var vals []interface{}
switch typed := v.(type) {
case []byte:
vals = []interface{}{typed}
case [][]byte:
if is.repTyp != parquet.FieldRepetitionType_REPEATED {
return nil, fmt.Errorf("the value is not repeated but it is an array")
}
vals = make([]interface{}, len(typed))
for j := range typed {
vals[j] = typed[j]
}
case string:
vals = []interface{}{[]byte(typed)}
case []string:
if is.repTyp != parquet.FieldRepetitionType_REPEATED {
return nil, fmt.Errorf("the value is not repeated but it is an array")
}
vals = make([]interface{}, len(typed))
for j := range typed {
vals[j] = []byte(typed[j])
}
default:
return nil, fmt.Errorf("unsupported type for storing in []byte column %T => %+v", v, v)
}
return vals, nil
}
func (*byteArrayStore) append(arrayIn interface{}, value interface{}) interface{} {
if arrayIn == nil {
arrayIn = make([][]byte, 0, 1)
}
return append(arrayIn.([][]byte), value.([]byte))
}