-
Notifications
You must be signed in to change notification settings - Fork 12
/
decimal.go
330 lines (277 loc) · 7.36 KB
/
decimal.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
package column
import (
"encoding/binary"
"fmt"
"math"
"math/big"
"strconv"
"github.com/bytehouse-cloud/driver-go/driver/lib/bytepool"
"github.com/bytehouse-cloud/driver-go/driver/lib/ch_encoding"
"github.com/bytehouse-cloud/driver-go/errors"
)
const (
invalidDecimalStringErr = "invalid decimal string: %v"
precisionNotSupportedErr = "precision of %v not supported"
unreachableExecutionErr = "unreachable execution path"
unsupportedPrecisionErr = "unsupported decimal precision: %v"
)
const (
maxSupportedDecimalBytes = 16
maxSupportedDecimalPrecision = 38
maxMantissaBit128Precision = 120
)
var factors10 = []float64{
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10,
1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20,
}
// DecimalColumnData represents Decimal(P, S) in Clickhouse.
// Decimals are fixed-point numbers that preserve precision for add, sub and mul operations.
//
// For division least significant digits are discarded (not rounded).
// See https://clickhouse.tech/docs/en/sql-reference/data-types/decimal
type DecimalColumnData struct {
precision int // support up to 38 digits
scale int // support up to 38 decimal values.
byteCount int
raw []byte
fmtTemplate string
isClosed bool
}
func (d *DecimalColumnData) ReadFromDecoder(decoder *ch_encoding.Decoder) error {
if d.precision > maxSupportedDecimalPrecision {
return errors.ErrorfWithCaller(unsupportedPrecisionErr, d.precision)
}
_, err := decoder.Read(d.raw)
return err
}
func (d *DecimalColumnData) WriteToEncoder(encoder *ch_encoding.Encoder) error {
if d.precision > maxSupportedDecimalPrecision {
return errors.ErrorfWithCaller(unsupportedPrecisionErr, d.precision)
}
_, err := encoder.Write(d.raw)
return err
}
func (d *DecimalColumnData) ReadFromValues(values []interface{}) (int, error) {
if len(values) == 0 {
return 0, nil
}
if d.byteCount > maxSupportedDecimalBytes {
return 0, errors.ErrorfWithCaller(precisionNotSupportedErr, d.precision)
}
for i, value := range values {
if err := d.putDecimalIntoBytes(i, value); err != nil {
return i, err
}
}
return len(values), nil
}
func (d *DecimalColumnData) ReadFromTexts(texts []string) (int, error) {
if d.byteCount > maxSupportedDecimalBytes {
return 0, errors.ErrorfWithCaller(precisionNotSupportedErr, d.precision)
}
for i, text := range texts {
if text == "" {
_ = d.putDecimalIntoBytes(i, 0.0)
continue
}
// Attempt parsing the float
v, err := d.parseDecimal(text)
if err != nil {
return i, errors.ErrorfWithCaller(invalidDecimalStringErr, text)
}
// Put it into little-endian bytes
if err := d.putDecimalIntoBytes(i, v); err != nil {
return i, err
}
}
return len(texts), nil
}
func (d *DecimalColumnData) GetValue(row int) interface{} {
switch d.byteCount {
case 4:
return d.decimal32ToFloat64(row)
case 8:
return d.decimal64ToFloat64(row)
case 16:
return d.decimal128ToBigFloat(row)
default:
panic(unreachableExecutionErr)
}
}
func (d *DecimalColumnData) GetString(row int) string {
switch d.byteCount {
case 4:
v := d.decimal32ToFloat64(row)
return fmt.Sprintf(d.fmtTemplate, v)
case 8:
v := d.decimal64ToFloat64(row)
return fmt.Sprintf(d.fmtTemplate, v)
case 16:
v := d.decimal128ToBigFloat(row)
return v.Text('f', d.scale)
default:
return "0.0"
}
}
func (d *DecimalColumnData) Zero() interface{} {
return float64(0)
}
func (d *DecimalColumnData) ZeroString() string {
return zeroString
}
func (d *DecimalColumnData) Len() int {
return len(d.raw) / d.byteCount
}
func (d *DecimalColumnData) Close() error {
if d.isClosed {
return nil
}
d.isClosed = true
bytepool.PutBytes(d.raw)
return nil
}
func (d *DecimalColumnData) decimal32ToFloat64(row int) float64 {
n := bufferRowToUint32(d.raw, row)
val := float64(n)
return val / factors10[d.scale]
}
func (d *DecimalColumnData) decimal64ToFloat64(row int) float64 {
n := bufferRowToUint64(d.raw, row)
val := float64(n)
return val / factors10[d.scale]
}
func (d *DecimalColumnData) decimal128ToBigFloat(row int) *big.Float {
// The bytes sent is a 16 bytes int128 in
// little big endian
bytes := d.raw[row*16 : (row+1)*16]
// Reverse to make it big endian, since big
// pkg uses big endian
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
bytes[i], bytes[j] = bytes[j], bytes[i]
}
bi := big.NewInt(0).SetBytes(bytes)
bf := new(big.Float).SetInt(bi)
factor10 := big.NewFloat(math.Pow10(d.scale))
bf = bf.Quo(bf, factor10) // division by scale factor
return bf
}
func (d *DecimalColumnData) parseDecimal(s string) (interface{}, error) {
switch d.byteCount {
case 16:
v, _, err := big.ParseFloat(s, 10, maxMantissaBit128Precision, big.ToNearestEven)
if err != nil {
return nil, err
}
return v, nil
default:
f64, err := strconv.ParseFloat(s, d.byteCount*8)
if err != nil {
return nil, err
}
return f64, nil
}
}
func (d *DecimalColumnData) putDecimalIntoBytes(i int, decimal interface{}) error {
switch d.byteCount {
case 4:
v, err := d.getDecimalToFloat64(decimal)
if err != nil {
return err
}
x := uint32(v * factors10[d.scale]) // apply scale factor
binary.LittleEndian.PutUint32(d.raw[i*4:], x) // serialize to bytes
case 8:
v, err := d.getDecimalToFloat64(decimal)
if err != nil {
return err
}
x := uint64(v * factors10[d.scale]) // apply scale factor
binary.LittleEndian.PutUint64(d.raw[i*8:], x) // serialize to bytes
case 16:
v, err := d.getDecimalToBigFloat(decimal)
if err != nil {
return err
}
factor := new(big.Float).SetFloat64(factors10[d.scale])
v = v.Mul(v, factor) // apply scale factor
z := new(big.Int) // serialize as 16-byte integer (int128)
v.Int(z)
// NOTE: can panic if value of z >= 10^39 - 1.
// For value beyond this threshold, it needs more than 16 bytes.
buff := d.raw[i*16 : (i+1)*16]
z.FillBytes(buff)
// Reverse byte-by-byte from big to little endian
for i, j := 0, len(buff)-1; i < j; i, j = i+1, j-1 {
buff[i], buff[j] = buff[j], buff[i]
}
default:
panic(errors.ErrorfWithCaller(unreachableExecutionErr))
}
return nil
}
func (d *DecimalColumnData) getDecimalToFloat64(decimal interface{}) (float64, error) {
switch v := decimal.(type) {
case float64:
return float64(v), nil
case float32:
return float64(v), nil
case int:
return float64(v), nil
case int8:
return float64(v), nil
case int16:
return float64(v), nil
case int32:
return float64(v), nil
case int64:
return float64(v), nil
case uint:
return float64(v), nil
case uint8:
return float64(v), nil
case uint16:
return float64(v), nil
case uint32:
return float64(v), nil
case uint64:
return float64(v), nil
default:
return 0.0, NewErrInvalidColumnType(v, 0.0)
}
}
func (d *DecimalColumnData) getDecimalToBigFloat(decimal interface{}) (*big.Float, error) {
switch v := decimal.(type) {
case *big.Float:
return v, nil
case *big.Int:
return new(big.Float).SetInt(v), nil
default:
f64, err := d.getDecimalToFloat64(decimal)
if err != nil {
return nil, err
}
return big.NewFloat(f64), nil
}
}
func getByteCountFromPrecision(p int) int {
var result = 4
if p <= 9 {
return result
}
result <<= 1
if p <= 18 {
return result
}
result <<= 1
if p <= 38 {
return result
}
result <<= 1
if p <= 76 {
return result
}
return result << 1
}
func makeDecimalFmtTemplate(scale int) string {
return "%" + fmt.Sprintf(".%vf", scale)
}