/
DecimalInputStream.go
332 lines (314 loc) · 9.86 KB
/
DecimalInputStream.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
package store
import (
"github.com/mothdb-bd/orc-go/pkg/maths"
"github.com/mothdb-bd/orc-go/pkg/slice"
"github.com/mothdb-bd/orc-go/pkg/util"
)
var (
LONG_MASK uint64 = 0x80_80_80_80_80_80_80_80
INT_MASK uint32 = 0x80_80_80_80
)
type DecimalInputStream struct {
// 继承
ValueInputStream[*DecimalStreamCheckpoint]
chunkLoader MothChunkLoader
block *slice.Slice
blockOffset int32
lastCheckpoint int64
}
func NewDecimalInputStream(chunkLoader MothChunkLoader) *DecimalInputStream {
dm := new(DecimalInputStream)
dm.block = slice.EMPTY_SLICE
dm.chunkLoader = chunkLoader
return dm
}
// @Override
func (dm *DecimalInputStream) SeekToCheckpoint(ct StreamCheckpoint) {
dt := ct.(*DecimalStreamCheckpoint)
newCheckpoint := dt.GetInputStreamCheckpoint()
if dm.block.Size() > 0 && DecodeCompressedBlockOffset(newCheckpoint) == DecodeCompressedBlockOffset(dm.lastCheckpoint) {
blockOffset := DecodeDecompressedOffset(newCheckpoint) - DecodeDecompressedOffset(dm.lastCheckpoint)
if blockOffset >= 0 && blockOffset < int32(dm.block.Size()) {
dm.blockOffset = blockOffset
return
}
}
dm.chunkLoader.SeekToCheckpoint(newCheckpoint)
dm.lastCheckpoint = newCheckpoint
dm.block = slice.EMPTY_SLICE
dm.blockOffset = 0
}
// @SuppressWarnings("PointlessBitwiseExpression")
func (dm *DecimalInputStream) NextLongDecimal(result []int64, batchSize int32) {
count := util.INT32_ZERO
for count < batchSize {
if dm.blockOffset == int32(dm.block.Size()) {
dm.advance()
}
for dm.blockOffset <= int32(dm.block.Size())-20 {
var low int64
middle := util.INT64_ZERO
high := util.INT64_ZERO
current, _ := dm.block.GetInt64LE(int(dm.blockOffset))
zeros := maths.NumberOfTrailingZeros(^current & int64(LONG_MASK))
end := (zeros + 1) / 8
dm.blockOffset += end
negative := (current & 1) == 1
low = maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00_00), 7)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00), 6)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00), 5)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00), 4)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00), 3)
low |= maths.UnsignedRightShift((current & 0x7F_00_00), 2)
low |= maths.UnsignedRightShift((current & 0x7F_00), 1)
low |= maths.UnsignedRightShift((current & 0x7F), 0)
low = low & ((int64(1) << (end * 7)) - 1)
if zeros == 64 {
current, _ = dm.block.GetInt64LE(int(dm.blockOffset))
zeros = maths.NumberOfTrailingZeros(int64(uint64(^current) & LONG_MASK))
end = (zeros + 1) / 8
dm.blockOffset += end
middle = maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00_00), 7)
middle |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00), 6)
middle |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00), 5)
middle |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00), 4)
middle |= maths.UnsignedRightShift((current & 0x7F_00_00_00), 3)
middle |= maths.UnsignedRightShift((current & 0x7F_00_00), 2)
middle |= maths.UnsignedRightShift((current & 0x7F_00), 1)
middle |= maths.UnsignedRightShift((current & 0x7F), 0)
middle = middle & ((int64(1) << (end * 7)) - 1)
if zeros == 64 {
last, _ := dm.block.GetInt32LE(int(dm.blockOffset))
zeros = maths.NumberOfTrailingZerosInt32(int32(uint32(^last) & INT_MASK))
end = (zeros + 1) / 8
dm.blockOffset += end
high = int64(maths.UnsignedRightShiftInt32((last & 0x7F_00_00), 2))
high |= int64(maths.UnsignedRightShiftInt32((last & 0x7F_00), 1))
high |= int64(maths.UnsignedRightShiftInt32((last & 0x7F), 0))
high = high & ((1 << (end * 7)) - 1)
if end == 4 || high > 0xFF_FF {
panic("Decimal exceeds 128 bits")
}
}
}
emitLongDecimal(result, count, low, middle, high, negative)
count++
if count == batchSize {
return
}
}
count = dm.decodeLongDecimalTail(result, count, batchSize)
}
}
func (dm *DecimalInputStream) decodeLongDecimalTail(result []int64, count int32, batchSize int32) int32 {
negative := false
low := util.INT64_ZERO
middle := util.INT64_ZERO
high := util.INT64_ZERO
var value int64
last := false
if dm.blockOffset == int32(dm.block.Size()) {
dm.advance()
}
offset := 0
for true {
tmpValue, _ := dm.block.GetByte(int(dm.blockOffset))
value = int64(tmpValue)
dm.blockOffset++
if offset == 0 {
negative = (value & 1) == 1
low |= (value & 0x7F)
} else if offset < 8 {
low |= (value & 0x7F) << (offset * 7)
} else if offset < 16 {
middle |= (value & 0x7F) << ((offset - 8) * 7)
} else if offset < 19 {
high |= (value & 0x7F) << ((offset - 16) * 7)
} else {
panic("Decimal exceeds 128 bits")
}
offset++
if (value & 0x80) == 0 {
if high > 0xFF_FF {
panic("Decimal exceeds 128 bits")
}
emitLongDecimal(result, count, low, middle, high, negative)
count++
low = 0
middle = 0
high = 0
offset = 0
if dm.blockOffset == int32(dm.block.Size()) {
break
}
if last || count == batchSize {
break
}
} else if dm.blockOffset == int32(dm.block.Size()) {
last = true
dm.advance()
}
}
return count
}
func emitLongDecimal(result []int64, offset int32, low int64, middle int64, high int64, negative bool) {
lower := int64(uint64(low)>>1) | (middle << 55)
upper := int64(uint64(middle)>>9) | (high << 47)
if negative {
// MOTH encodes decimals using a zig-zag vint strategy
// For negative values, the encoded value is given by:
// encoded = -value * 2 - 1
//
// Therefore,
// value = -(encoded + 1) / 2
// = -encoded / 2 - 1/2
//
// Given the identity -v = ~v + 1 for negating a value using
// two's complement representation,
//
// value = (~encoded + 1) / 2 - 1/2
// = ~encoded / 2 + 1/2 - 1/2
// = ~encoded / 2
//
// The shift is performed above as the bits are assembled. The negation
// is performed here.
lower = ^lower
upper = ^upper
}
result[2*offset] = upper
result[2*offset+1] = lower
}
// @SuppressWarnings("PointlessBitwiseExpression")
func (dm *DecimalInputStream) NextShortDecimal(result []int64, batchSize int32) {
count := util.INT32_ZERO
for count < batchSize {
if dm.blockOffset == dm.block.SizeInt32() {
dm.advance()
}
for dm.blockOffset <= dm.block.SizeInt32()-12 {
var low int64
high := util.INT64_ZERO
current, _ := dm.block.GetInt64LE(int(dm.blockOffset))
zeros := maths.NumberOfTrailingZeros(int64(uint64(^current) & LONG_MASK))
end := (zeros + 1) / 8
dm.blockOffset += end
low = maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00_00), 7)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00_00), 6)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00_00), 5)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00_00), 4)
low |= maths.UnsignedRightShift((current & 0x7F_00_00_00), 3)
low |= maths.UnsignedRightShift((current & 0x7F_00_00), 2)
low |= maths.UnsignedRightShift((current & 0x7F_00), 1)
low |= maths.UnsignedRightShift((current & 0x7F), 0)
low = low & ((int64(1) << (end * 7)) - 1)
if zeros == 64 {
last, _ := dm.block.GetInt32LE(int(dm.blockOffset))
zeros = maths.NumberOfTrailingZerosInt32(int32(uint32(^last) & INT_MASK))
end = (zeros + 1) / 8
dm.blockOffset += end
high = int64(maths.UnsignedRightShiftInt32(int32(uint32(last)&0x7F_00), 1))
high |= int64(maths.UnsignedRightShiftInt32(int32(uint32(last)&0x7F), 0))
high = high & ((1 << (end * 7)) - 1)
if end >= 3 || high > 0xFF {
panic("Decimal does not fit long (invalid table schema?)")
}
}
emitShortDecimal(result, count, low, high)
count++
if count == batchSize {
return
}
}
count = dm.decodeShortDecimalTail(result, count, batchSize)
}
}
func (dm *DecimalInputStream) decodeShortDecimalTail(result []int64, count int32, batchSize int32) int32 {
low := util.INT64_ZERO
high := util.INT64_ZERO
var value int64
last := false
offset := 0
if dm.blockOffset == int32(dm.block.Size()) {
dm.advance()
}
for true {
tmpValue, _ := dm.block.GetByte(int(dm.blockOffset))
value = int64(tmpValue)
dm.blockOffset++
if offset == 0 {
low |= (value & 0x7F)
} else if offset < 8 {
low |= (value & 0x7F) << (offset * 7)
} else if offset < 11 {
high |= (value & 0x7F) << ((offset - 8) * 7)
} else {
panic("Decimal does not fit long (invalid table schema?)")
}
offset++
if (value & 0x80) == 0 {
if high > 0xFF {
panic("Decimal does not fit long (invalid table schema?)")
}
emitShortDecimal(result, count, low, high)
count++
low = 0
high = 0
offset = 0
if dm.blockOffset == int32(dm.block.Size()) {
break
}
if last || count == batchSize {
break
}
} else if dm.blockOffset == int32(dm.block.Size()) {
last = true
dm.advance()
}
}
return count
}
func emitShortDecimal(result []int64, offset int32, low int64, high int64) {
negative := (low & 1) == 1
value := int64(uint64(low)>>1) | (high << 55)
if negative {
value = ^value
}
result[offset] = value
}
// @Override
func (dm *DecimalInputStream) Skip(items int64) {
if items == 0 {
return
}
if dm.blockOffset == int32(dm.block.Size()) {
dm.advance()
}
count := util.INT32_ZERO
for true {
for dm.blockOffset <= int32(dm.block.Size())-util.INT64_BYTES {
current, _ := dm.block.GetInt64LE(int(dm.blockOffset))
increment := maths.BitCount(int64(uint64(^current) & LONG_MASK))
if int64(count+increment) >= items {
break
}
count += increment
dm.blockOffset += util.INT64_BYTES
}
for dm.blockOffset < int32(dm.block.Size()) {
current, _ := dm.block.GetByte(int(dm.blockOffset))
dm.blockOffset++
if (current & 0x80) == 0 {
count++
if int64(count) == items {
return
}
}
}
dm.advance()
}
}
func (dm *DecimalInputStream) advance() {
dm.block = dm.chunkLoader.NextChunk()
dm.lastCheckpoint = dm.chunkLoader.GetLastCheckpoint()
dm.blockOffset = 0
}