-
Notifications
You must be signed in to change notification settings - Fork 46
/
runlengthintegerreaderv2.go
425 lines (375 loc) · 10.6 KB
/
runlengthintegerreaderv2.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package orc
import (
"errors"
"fmt"
"io"
)
var (
ErrEOFUnsignedVInt = errors.New("EOF while reading unsigned vint")
ErrCorrupt = errors.New("ORC file is corrupt")
)
const (
// MinRepeatSize is the minimum number of repeated values required to use run length encoding.
MinRepeatSize = 3
// MaxShortRepeatLength is the maximum run length used for RLEV2IntShortRepeat sequences.
MaxShortRepeatLength = 10
// MaxScope is the maximum number of values that can be buffered before being flushed.
MaxScope = 512
)
//go:generate stringer -type=RLEEncodingType
// RLEEncodingType is a run length encoding type specified within the Apache
// ORC file documentation: https://orc.apache.org/docs/run-length.html
type RLEEncodingType int
const (
RLEV2IntShortRepeat RLEEncodingType = 0
RLEV2IntDirect RLEEncodingType = 1
RLEV2IntPatchedBase RLEEncodingType = 2
RLEV2IntDelta RLEEncodingType = 3
)
type RunLengthIntegerReaderV2 struct {
r io.ByteReader
signed bool
literals []int64
isRepeating bool
numLiterals int
used int
skipCorrupt bool
currentEncoding RLEEncodingType
err error
nextByte *byte
minRepeatSize int
}
func NewRunLengthIntegerReaderV2(r io.ByteReader, signed bool, skipCorrupt bool) *RunLengthIntegerReaderV2 {
return &RunLengthIntegerReaderV2{
r: r,
signed: signed,
skipCorrupt: skipCorrupt,
literals: make([]int64, MaxScope),
minRepeatSize: MinRepeatSize,
}
}
func (r *RunLengthIntegerReaderV2) available() error {
byt, err := r.ReadByte()
if err != nil {
r.err = err
return err
}
r.nextByte = &byt
return nil
}
func (r *RunLengthIntegerReaderV2) ReadByte() (byte, error) {
if r.nextByte != nil {
byt := *r.nextByte
r.nextByte = nil
return byt, nil
}
return r.r.ReadByte()
}
func (r *RunLengthIntegerReaderV2) Next() bool {
if r.err != nil {
return false
}
return r.used != r.numLiterals || r.available() == nil
}
func (r *RunLengthIntegerReaderV2) Value() interface{} {
return r.Int()
}
func (r *RunLengthIntegerReaderV2) Int() int64 {
var result int64
if r.used == r.numLiterals {
r.numLiterals = 0
r.used = 0
err := r.readValues(false)
if err != nil {
r.err = err
return 0
}
}
result = r.literals[r.used]
r.used++
return result
}
func (r *RunLengthIntegerReaderV2) readValues(ignoreEOF bool) error {
// read the first 2 bits and determine the encoding type
r.isRepeating = false
firstByte, err := r.ReadByte()
if err != nil {
return err
}
if firstByte < 0 {
return fmt.Errorf("Read past end of RLE integer from from %v", r)
}
r.currentEncoding = RLEEncodingType((uint64(firstByte) >> 6) & 0x03)
switch r.currentEncoding {
case RLEV2IntShortRepeat:
return r.readShortRepeatValues(firstByte)
case RLEV2IntDirect:
return r.readDirectValues(firstByte)
case RLEV2IntPatchedBase:
return r.readPatchedBaseValues(firstByte)
case RLEV2IntDelta:
return r.readDeltaValues(firstByte)
default:
return fmt.Errorf("Unknown encoding %v", r.currentEncoding)
}
}
func (r *RunLengthIntegerReaderV2) readDeltaValues(firstByte byte) error {
// extract the number of fixed bits
fb := int((uint64(firstByte) >> 1) & 0x1f)
if fb != 0 {
fb = decodeBitWidth(int(fb))
}
// extract the blob run length
l := int((uint64(firstByte) & 0x01) << 8)
nextByt, err := r.ReadByte()
if err != nil {
return err
}
// Set the run-length (this is actual run-length - 1)
l |= int(nextByt)
// read the first value stored as vint
var firstVal int64
if r.signed {
firstVal, err = readVslong(r)
if err != nil {
return err
}
} else {
firstVal, err = readVulong(r)
if err != nil {
return err
}
}
// store first value to result buffer
prevVal := firstVal
r.literals[r.numLiterals] = firstVal
r.numLiterals++
// if fixed bits is 0 then all values have fixed delta
if fb == 0 {
// read the fixed delta value stored as vint (deltas can be negative even
// if all number are positive)
fd, err := readVslong(r)
if err != nil {
return err
}
if fd == 0 {
r.isRepeating = true
if r.numLiterals != 1 {
return errors.New("expected numLiterals to equal 1")
}
for i := r.numLiterals; i < r.numLiterals+l; i++ {
r.literals[i] = r.literals[0]
}
r.numLiterals += l
} else {
// add fixed deltas to adjacent values
for i := 0; i < l; i++ {
r.literals[r.numLiterals] = r.literals[r.numLiterals-1] + fd
r.numLiterals++
}
}
} else {
deltaBase, err := readVslong(r)
if err != nil {
return err
}
// add delta base and first value
r.literals[r.numLiterals] = firstVal + deltaBase
r.numLiterals++
prevVal = r.literals[r.numLiterals-1]
l--
// write the unpacked values, add it to previous value and store final
// value to result buffer. if the delta base value is negative then it
// is a decreasing sequence else an increasing sequence
err = readInts(r.literals, r.numLiterals, l, fb, r)
if err != nil {
return err
}
for l > 0 {
if deltaBase < 0 {
r.literals[r.numLiterals] = prevVal - r.literals[r.numLiterals]
} else {
r.literals[r.numLiterals] = prevVal + r.literals[r.numLiterals]
}
prevVal = r.literals[r.numLiterals]
l--
r.numLiterals++
}
}
return nil
}
func (r *RunLengthIntegerReaderV2) readShortRepeatValues(firstByte byte) error {
// read the number of bytes occupied by the value
size := (uint64(firstByte) >> 3) & 0x07
// #bytes are one off
size++
// read the run length
l := int(firstByte & 0x07)
// run lengths values are stored only after MIN_REPEAT value is met
l += r.minRepeatSize
val, err := bytesToLongBE(r, int(size))
if err != nil {
return err
}
if r.signed {
val = zigzagDecode(uint64(val))
}
if r.numLiterals != 0 {
// Currently this always holds, which makes peekNextAvailLength simpler.
// If this changes, peekNextAvailLength should be adjusted accordingly.
return errors.New("readValues called with existing values present")
}
// repeat the value for length times
r.isRepeating = true
// TODO: this is not so useful and V1 reader doesn't do that. Fix? Same if delta == 0
for i := 0; i < l; i++ {
r.literals[i] = val
}
r.numLiterals = l
return nil
}
func (r *RunLengthIntegerReaderV2) readDirectValues(firstByte byte) error {
// extract the number of fixed bits
fbo := (uint64(firstByte) >> 1) & 0x1f
fb := uint64(decodeBitWidth(int(fbo)))
// extract the run length
l := int((int64(firstByte) & 0x01) << 8)
nextByte, err := r.ReadByte()
if err != nil {
return err
}
l |= int(nextByte)
// runs are one off
l++
// write the unpacked values and zigzag decode to result buffer
err = readInts(r.literals, r.numLiterals, l, int(fb), r)
if err != nil {
return err
}
if r.signed {
for i := 0; i < l; i++ {
r.literals[r.numLiterals] = zigzagDecode(uint64(r.literals[r.numLiterals]))
r.numLiterals++
}
} else {
r.numLiterals += l
}
return nil
}
func (r *RunLengthIntegerReaderV2) readPatchedBaseValues(firstByte byte) error {
// extract the number of fixed bits
fixedBits := decodeBitWidth(int(uint64(firstByte) >> 1 & 0x1f))
// extract the run length
length := int(uint64(firstByte&0x01) << 8)
// read a byte
b, err := r.ReadByte()
if err != nil {
return err
}
length |= int(b)
// runs are one off
length++
// extract the number of bytes occupied by base
thirdByte, err := r.ReadByte()
if err != nil {
return err
}
baseWidth := uint64(thirdByte) >> 5 & 0x07
// base width is one off
baseWidth++
// extract patch width
patchWidth := decodeBitWidth(int(thirdByte) & 0x1F)
// read fourth byte and extract patch gap width
fourthByte, err := r.ReadByte()
if err != nil {
return err
}
patchGapWidth := uint64(fourthByte) >> 5 & 0x07
// patch gap width is one off
patchGapWidth++
// extract the length of the patch list
patchListLength := fourthByte & 0x1F
// read the next base width number of bytes to extract base value
base, err := bytesToLongBE(r, int(baseWidth))
if err != nil {
return err
}
mask := int64(1 << ((baseWidth * 8) - 1))
// if MSB of base value is 1 then base is negative value else positive
if (base & mask) != 0 {
base = base & ^mask
base = -base
}
// unpack the data blob
unpacked := make([]int64, length)
err = readInts(unpacked, 0, length, int(fixedBits), r)
if err != nil {
return err
}
// unpack the patch blob
unpackedPatch := make([]int64, int(patchListLength))
if (patchWidth+int(patchGapWidth)) > 64 && !r.skipCorrupt {
return errors.New(`Corruption in ORC data encountered. To skip` +
` reading corrupted data, set skipCorrupt to true`)
}
bitSize := getClosestFixedBits(patchWidth + int(patchGapWidth))
err = readInts(unpackedPatch, 0, int(patchListLength), bitSize, r)
if err != nil && err != io.EOF {
return err
}
var patchIndex int
var currentGap int64
var currentPatch int64
patchMask := int64((1 << uint(patchWidth)) - 1)
currentGap = int64(uint64(unpackedPatch[patchIndex]) >> uint64(patchWidth))
currentPatch = unpackedPatch[patchIndex] & patchMask
var actualGap int64
// special case: gap is >255 then patch value will be 0.
// if gap is <=255 then patch value cannot be 0
for currentGap == 255 && currentPatch == 0 {
actualGap += 255
patchIndex++
currentGap = int64(uint64(unpackedPatch[patchIndex]) >> uint64(patchWidth))
currentPatch = unpackedPatch[patchIndex] & patchMask
}
// add the left over gap
actualGap += currentGap
// unpack data blob, patch it (if required), add base to get final result
for i := 0; i < len(unpacked); i++ {
if i == int(actualGap) {
// extract the patch value
patchedValue := int64(unpacked[i] | (currentPatch << uint64(fixedBits)))
// add base to patched value
r.literals[r.numLiterals] = base + patchedValue
r.numLiterals++
// increment the patch to point to next entry in patch list
patchIndex++
if patchIndex < int(patchListLength) {
// read the next gap and patch
currentGap = int64(uint64(unpackedPatch[patchIndex]) >> uint64(patchWidth))
currentPatch = unpackedPatch[patchIndex] & patchMask
actualGap = 0
// special case: gap is >255 then patch will be 0. if gap is
// <=255 then patch cannot be 0
for currentGap == 255 && currentPatch == 0 {
actualGap += 255
patchIndex++
currentGap = int64(uint64(unpackedPatch[patchIndex]) >> uint64(patchWidth))
currentPatch = unpackedPatch[patchIndex] & patchMask
}
// add the left over gap
actualGap += currentGap
// next gap is relative to the current gap
actualGap += int64(i)
}
} else {
// no patching required. add base to unpacked value to get final value
r.literals[r.numLiterals] = base + unpacked[i]
r.numLiterals++
}
}
return nil
}
func (r *RunLengthIntegerReaderV2) Err() error {
return r.err
}