-
Notifications
You must be signed in to change notification settings - Fork 53
/
deltabp_encoder.go
329 lines (268 loc) · 7.65 KB
/
deltabp_encoder.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
package goparquet
import (
"bytes"
"encoding/binary"
"io"
"math/bits"
"math"
"github.com/pkg/errors"
)
type deltaBitPackEncoder32 struct {
deltas []int32
bitWidth []uint8
packed [][]byte
w io.Writer
// this value should be there before the init
blockSize int // Must be multiply of 128
miniBlockCount int // blockSize % miniBlockCount should be 0
miniBlockValueCount int
valuesCount int
buffer *bytes.Buffer
firstValue int32 // the first value to write
minDelta int32
previousValue int32
}
func (d *deltaBitPackEncoder32) init(w io.Writer) error {
d.w = w
if d.blockSize%128 != 0 || d.blockSize <= 0 {
return errors.Errorf("invalid block size, it should be multiply of 128, it is %d", d.blockSize)
}
if d.miniBlockCount <= 0 || d.blockSize%d.miniBlockCount != 0 {
return errors.Errorf("invalid mini block count, it is %d", d.miniBlockCount)
}
d.miniBlockValueCount = d.blockSize / d.miniBlockCount
if d.miniBlockValueCount%8 != 0 {
return errors.Errorf("invalid mini block count, the mini block value count should be multiply of 8, it is %d", d.miniBlockCount)
}
d.firstValue = 0
d.valuesCount = 0
d.minDelta = math.MaxInt32
d.deltas = make([]int32, 0, d.blockSize)
d.previousValue = 0
d.buffer = &bytes.Buffer{}
d.bitWidth = make([]uint8, 0, d.miniBlockCount)
return nil
}
func (d *deltaBitPackEncoder32) flush() error {
// Technically, based on the spec after this step all values are positive, but NO, it's not. the problem is when
// the min delta is small enough (lets say MinInt) and one of deltas are MaxInt, the the result of MaxInt-MinInt is
// -1, get the idea, there is a lot of numbers here because of overflow can produce negative value
for i := range d.deltas {
d.deltas[i] -= d.minDelta
}
if err := writeVariant(d.buffer, int64(d.minDelta)); err != nil {
return err
}
d.bitWidth = d.bitWidth[:0] //reset the bitWidth buffer
d.packed = d.packed[:0]
for i := 0; i < len(d.deltas); i += d.miniBlockValueCount {
end := i + d.miniBlockValueCount
if end >= len(d.deltas) {
end = len(d.deltas)
}
// The cast to uint32 here, is the key. or the max not works at all
max := uint32(d.deltas[i])
buf := make([][8]int32, d.miniBlockValueCount/8)
for j := i; j < end; j++ {
if max < uint32(d.deltas[j]) {
max = uint32(d.deltas[j])
}
t := j - i
buf[t/8][t%8] = d.deltas[j]
}
bw := bits.Len32(max)
d.bitWidth = append(d.bitWidth, uint8(bw))
data := make([]byte, 0, bw*len(buf))
packer := pack8Int32FuncByWidth[bw]
for j := range buf {
data = append(data, packer(buf[j])...)
}
d.packed = append(d.packed, data)
}
for len(d.bitWidth) < d.miniBlockCount {
d.bitWidth = append(d.bitWidth, 0)
}
if err := binary.Write(d.buffer, binary.LittleEndian, d.bitWidth); err != nil {
return err
}
for i := range d.packed {
if err := writeFull(d.buffer, d.packed[i]); err != nil {
return err
}
}
d.minDelta = math.MaxInt32
d.deltas = d.deltas[:0]
return nil
}
func (d *deltaBitPackEncoder32) addInt32(i int32) error {
d.valuesCount++
if d.valuesCount == 1 {
d.firstValue = i
d.previousValue = i
return nil
}
delta := i - d.previousValue
d.previousValue = i
d.deltas = append(d.deltas, delta)
if delta < d.minDelta {
d.minDelta = delta
}
if len(d.deltas) == d.blockSize {
// flush
return d.flush()
}
return nil
}
func (d *deltaBitPackEncoder32) write() error {
if len(d.deltas) > 0 {
if err := d.flush(); err != nil {
return err
}
}
if err := writeUVariant(d.w, uint64(d.blockSize)); err != nil {
return err
}
if err := writeUVariant(d.w, uint64(d.miniBlockCount)); err != nil {
return err
}
if err := writeUVariant(d.w, uint64(d.valuesCount)); err != nil {
return err
}
if err := writeVariant(d.w, int64(d.firstValue)); err != nil {
return err
}
return writeFull(d.w, d.buffer.Bytes())
}
func (d *deltaBitPackEncoder32) Close() error {
return d.write()
}
type deltaBitPackEncoder64 struct {
// this value should be there before the init
blockSize int // Must be multiply of 128
miniBlockCount int // blockSize % miniBlockCount should be 0
//
miniBlockValueCount int
w io.Writer
firstValue int64 // the first value to write
valuesCount int
minDelta int64
deltas []int64
previousValue int64
buffer *bytes.Buffer
bitWidth []uint8
packed [][]byte
}
func (d *deltaBitPackEncoder64) init(w io.Writer) error {
d.w = w
if d.blockSize%128 != 0 || d.blockSize <= 0 {
return errors.Errorf("invalid block size, it should be multiply of 128, it is %d", d.blockSize)
}
if d.miniBlockCount <= 0 || d.blockSize%d.miniBlockCount != 0 {
return errors.Errorf("invalid mini block count, it is %d", d.miniBlockCount)
}
d.miniBlockValueCount = d.blockSize / d.miniBlockCount
if d.miniBlockValueCount%8 != 0 {
return errors.Errorf("invalid mini block count, the mini block value count should be multiply of 8, it is %d", d.miniBlockCount)
}
d.firstValue = 0
d.valuesCount = 0
d.minDelta = math.MaxInt32
d.deltas = make([]int64, 0, d.blockSize)
d.previousValue = 0
d.buffer = &bytes.Buffer{}
d.bitWidth = make([]uint8, 0, d.miniBlockCount)
return nil
}
func (d *deltaBitPackEncoder64) flush() error {
// Technically, based on the spec after this step all values are positive, but NO, it's not. the problem is when
// the min delta is small enough (lets say MinInt) and one of deltas are MaxInt, the the result of MaxInt-MinInt is
// -1, get the idea, there is a lot of numbers here because of overflow can produce negative value
for i := range d.deltas {
d.deltas[i] -= d.minDelta
}
if err := writeVariant(d.buffer, d.minDelta); err != nil {
return err
}
d.bitWidth = d.bitWidth[:0] //reset the bitWidth buffer
d.packed = d.packed[:0]
for i := 0; i < len(d.deltas); i += d.miniBlockValueCount {
end := i + d.miniBlockValueCount
if end >= len(d.deltas) {
end = len(d.deltas)
}
// The cast to uint64 here, is the key. or the max not works at all
max := uint64(d.deltas[i])
buf := make([][8]int64, d.miniBlockValueCount/8)
for j := i; j < end; j++ {
if max < uint64(d.deltas[j]) {
max = uint64(d.deltas[j])
}
t := j - i
buf[t/8][t%8] = d.deltas[j]
}
bw := bits.Len64(max)
d.bitWidth = append(d.bitWidth, uint8(bw))
data := make([]byte, 0, bw*len(buf))
packer := pack8Int64FuncByWidth[bw]
for j := range buf {
data = append(data, packer(buf[j])...)
}
d.packed = append(d.packed, data)
}
for len(d.bitWidth) < d.miniBlockCount {
d.bitWidth = append(d.bitWidth, 0)
}
if err := binary.Write(d.buffer, binary.LittleEndian, d.bitWidth); err != nil {
return err
}
for i := range d.packed {
if err := writeFull(d.buffer, d.packed[i]); err != nil {
return err
}
}
d.minDelta = math.MaxInt32
d.deltas = d.deltas[:0]
return nil
}
func (d *deltaBitPackEncoder64) addInt64(i int64) error {
d.valuesCount++
if d.valuesCount == 1 {
d.firstValue = i
d.previousValue = i
return nil
}
delta := i - d.previousValue
d.previousValue = i
d.deltas = append(d.deltas, delta)
if delta < d.minDelta {
d.minDelta = delta
}
if len(d.deltas) == d.blockSize {
// flush
return d.flush()
}
return nil
}
func (d *deltaBitPackEncoder64) write() error {
if len(d.deltas) > 0 {
if err := d.flush(); err != nil {
return err
}
}
if err := writeUVariant(d.w, uint64(d.blockSize)); err != nil {
return err
}
if err := writeUVariant(d.w, uint64(d.miniBlockCount)); err != nil {
return err
}
if err := writeUVariant(d.w, uint64(d.valuesCount)); err != nil {
return err
}
if err := writeVariant(d.w, d.firstValue); err != nil {
return err
}
return writeFull(d.w, d.buffer.Bytes())
}
func (d *deltaBitPackEncoder64) Close() error {
return d.write()
}