-
Notifications
You must be signed in to change notification settings - Fork 294
/
encodingread.go
374 lines (336 loc) · 8.72 KB
/
encodingread.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
package encoding
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/xitongsys/parquet-go/parquet"
)
func ReadPlain(bytesReader *bytes.Reader, dataType parquet.Type, cnt uint64, bitWidth uint64) ([]interface{}, error) {
if dataType == parquet.Type_BOOLEAN {
return ReadPlainBOOLEAN(bytesReader, cnt)
} else if dataType == parquet.Type_INT32 {
return ReadPlainINT32(bytesReader, cnt)
} else if dataType == parquet.Type_INT64 {
return ReadPlainINT64(bytesReader, cnt)
} else if dataType == parquet.Type_INT96 {
return ReadPlainINT96(bytesReader, cnt)
} else if dataType == parquet.Type_FLOAT {
return ReadPlainFLOAT(bytesReader, cnt)
} else if dataType == parquet.Type_DOUBLE {
return ReadPlainDOUBLE(bytesReader, cnt)
} else if dataType == parquet.Type_BYTE_ARRAY {
return ReadPlainBYTE_ARRAY(bytesReader, cnt)
} else if dataType == parquet.Type_FIXED_LEN_BYTE_ARRAY {
return ReadPlainFIXED_LEN_BYTE_ARRAY(bytesReader, cnt, bitWidth)
} else {
return nil, fmt.Errorf("Unknown parquet type")
}
}
func ReadPlainBOOLEAN(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var (
res []interface{}
err error
)
res = make([]interface{}, cnt)
resInt, err := ReadBitPacked(bytesReader, uint64(cnt<<1), 1)
if err != nil {
return res, err
}
for i := 0; i < int(cnt); i++ {
if resInt[i].(int64) > 0 {
res[i] = true
} else {
res[i] = false
}
}
return res, err
}
func ReadPlainINT32(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
err = BinaryReadINT32(bytesReader, res)
return res, err
}
func ReadPlainINT64(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
err = BinaryReadINT64(bytesReader, res)
return res, err
}
func ReadPlainINT96(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
cur := make([]byte, 12)
for i := 0; i < int(cnt); i++ {
if _, err = bytesReader.Read(cur); err != nil {
break
}
res[i] = string(cur[:12])
}
return res, err
}
func ReadPlainFLOAT(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
err = BinaryReadFLOAT32(bytesReader, res)
return res, err
}
func ReadPlainDOUBLE(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
err = BinaryReadFLOAT64(bytesReader, res)
return res, err
}
func ReadPlainBYTE_ARRAY(bytesReader *bytes.Reader, cnt uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
for i := 0; i < int(cnt); i++ {
buf := make([]byte, 4)
if _, err = bytesReader.Read(buf); err != nil {
break
}
ln := binary.LittleEndian.Uint32(buf)
cur := make([]byte, ln)
bytesReader.Read(cur)
res[i] = string(cur)
}
return res, err
}
func ReadPlainFIXED_LEN_BYTE_ARRAY(bytesReader *bytes.Reader, cnt uint64, fixedLength uint64) ([]interface{}, error) {
var err error
res := make([]interface{}, cnt)
for i := 0; i < int(cnt); i++ {
cur := make([]byte, fixedLength)
if _, err = bytesReader.Read(cur); err != nil {
break
}
res[i] = string(cur)
}
return res, err
}
func ReadUnsignedVarInt(bytesReader *bytes.Reader) (uint64, error) {
var err error
var res uint64 = 0
var shift uint64 = 0
for {
b, err := bytesReader.ReadByte()
if err != nil {
break
}
res |= ((uint64(b) & uint64(0x7F)) << uint64(shift))
if (b & 0x80) == 0 {
break
}
shift += 7
}
return res, err
}
//RLE return res is []INT64
func ReadRLE(bytesReader *bytes.Reader, header uint64, bitWidth uint64) ([]interface{}, error) {
var err error
var res []interface{}
cnt := header >> 1
width := (bitWidth + 7) / 8
data := make([]byte, width)
if width > 0 {
if _, err = bytesReader.Read(data); err != nil {
return res, err
}
}
for len(data) < 4 {
data = append(data, byte(0))
}
val := int64(binary.LittleEndian.Uint32(data))
res = make([]interface{}, cnt)
for i := 0; i < int(cnt); i++ {
res[i] = val
}
return res, err
}
//return res is []INT64
func ReadBitPacked(bytesReader *bytes.Reader, header uint64, bitWidth uint64) ([]interface{}, error) {
var err error
numGroup := (header >> 1)
cnt := numGroup * 8
byteCnt := cnt * bitWidth / 8
res := make([]interface{}, 0, cnt)
if cnt == 0 {
return res, nil
}
if bitWidth == 0 {
for i := 0; i < int(cnt); i++ {
res = append(res, int64(0))
}
return res, err
}
bytesBuf := make([]byte, byteCnt)
if _, err = bytesReader.Read(bytesBuf); err != nil {
return res, err
}
i := 0
var resCur uint64 = 0
var resCurNeedBits uint64 = bitWidth
var used uint64 = 0
var left uint64 = 8 - used
b := bytesBuf[i]
for i < len(bytesBuf) {
if left >= resCurNeedBits {
resCur |= uint64(((uint64(b) >> uint64(used)) & ((1 << uint64(resCurNeedBits)) - 1)) << uint64(bitWidth-resCurNeedBits))
res = append(res, int64(resCur))
left -= resCurNeedBits
used += resCurNeedBits
resCurNeedBits = bitWidth
resCur = 0
if left <= 0 && i+1 < len(bytesBuf) {
i += 1
b = bytesBuf[i]
left = 8
used = 0
}
} else {
resCur |= uint64((uint64(b) >> uint64(used)) << uint64(bitWidth-resCurNeedBits))
i += 1
if i < len(bytesBuf) {
b = bytesBuf[i]
}
resCurNeedBits -= left
left = 8
used = 0
}
}
return res, err
}
//res is INT64
func ReadRLEBitPackedHybrid(bytesReader *bytes.Reader, bitWidth uint64, length uint64) ([]interface{}, error) {
res := make([]interface{}, 0)
if length <= 0 {
lb, err := ReadPlainINT32(bytesReader, 1)
if err != nil {
return res, err
}
length = uint64(lb[0].(int32))
}
buf := make([]byte, length)
if _, err := bytesReader.Read(buf); err != nil {
return res, err
}
newReader := bytes.NewReader(buf)
for newReader.Len() > 0 {
header, err := ReadUnsignedVarInt(newReader)
if err != nil {
return res, err
}
if header&1 == 0 {
buf, err := ReadRLE(newReader, header, bitWidth)
if err != nil {
return res, err
}
res = append(res, buf...)
} else {
buf, err := ReadBitPacked(newReader, header, bitWidth)
if err != nil {
return res, err
}
res = append(res, buf...)
}
}
return res, nil
}
//res is INT64
func ReadDeltaBinaryPackedINT(bytesReader *bytes.Reader) ([]interface{}, error) {
var (
err error
res []interface{}
)
blockSize, err := ReadUnsignedVarInt(bytesReader)
if err != nil {
return res, err
}
numMiniblocksInBlock, err := ReadUnsignedVarInt(bytesReader)
if err != nil {
return res, err
}
numValues, err := ReadUnsignedVarInt(bytesReader)
if err != nil {
return res, err
}
firstValueZigZag, err := ReadUnsignedVarInt(bytesReader)
if err != nil {
return res, err
}
var firstValue int64 = int64(firstValueZigZag>>1) ^ (-int64(firstValueZigZag & 1))
numValuesInMiniBlock := blockSize / numMiniblocksInBlock
res = make([]interface{}, 0)
res = append(res, int64(firstValue))
for uint64(len(res)) < numValues {
minDeltaZigZag, err := ReadUnsignedVarInt(bytesReader)
if err != nil {
return res, err
}
var minDelta int64 = int64(minDeltaZigZag>>1) ^ (-int64(minDeltaZigZag & 1))
var bitWidths = make([]uint64, numMiniblocksInBlock)
for i := 0; uint64(i) < numMiniblocksInBlock; i++ {
b, err := bytesReader.ReadByte()
if err != nil {
return res, err
}
bitWidths[i] = uint64(b)
}
for i := 0; uint64(i) < numMiniblocksInBlock && uint64(len(res)) < numValues; i++ {
cur, err := ReadBitPacked(bytesReader, (numValuesInMiniBlock/8)<<1, bitWidths[i])
if err != nil {
return res, err
}
for j := 0; j < len(cur); j++ {
res = append(res, (res[len(res)-1].(int64) + cur[j].(int64) + minDelta))
}
}
}
return res[:numValues], err
}
func ReadDeltaLengthByteArray(bytesReader *bytes.Reader) ([]interface{}, error) {
var (
res []interface{}
err error
)
lengths, err := ReadDeltaBinaryPackedINT(bytesReader)
if err != nil {
return res, err
}
res = make([]interface{}, len(lengths))
for i := 0; i < len(lengths); i++ {
res[i] = ""
length := uint64(lengths[i].(int64))
if length > 0 {
cur, err := ReadPlainFIXED_LEN_BYTE_ARRAY(bytesReader, 1, length)
if err != nil {
return res, err
}
res[i] = cur[0]
}
}
return res, err
}
func ReadDeltaByteArray(bytesReader *bytes.Reader) ([]interface{}, error) {
var (
res []interface{}
err error
)
prefixLengths, err := ReadDeltaBinaryPackedINT(bytesReader)
if err != nil {
return res, err
}
suffixes, err := ReadDeltaLengthByteArray(bytesReader)
if err != nil {
return res, err
}
res = make([]interface{}, len(prefixLengths))
res[0] = suffixes[0]
for i := 1; i < len(prefixLengths); i++ {
prefixLength := prefixLengths[i].(int64)
prefix := res[i-1].(string)[:prefixLength]
suffix := suffixes[i].(string)
res[i] = prefix + suffix
}
return res, err
}