forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathdecimal.go
More file actions
393 lines (326 loc) · 9.63 KB
/
decimal.go
File metadata and controls
393 lines (326 loc) · 9.63 KB
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
// Package decimal with support of Tarantool's decimal data type.
//
// Decimal data type supported in Tarantool since 2.2.
//
// Since: 1.7.0
//
// See also:
//
// - Tarantool MessagePack extensions:
// https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/#the-decimal-type
//
// - Tarantool data model:
// https://www.tarantool.io/en/doc/latest/book/box/data_model/
//
// - Tarantool issue for support decimal type:
// https://github.com/tarantool/tarantool/issues/692
//
// - Tarantool module decimal:
// https://www.tarantool.io/en/doc/latest/reference/reference_lua/decimal/
package decimal
import (
"fmt"
"math"
"reflect"
"strconv"
"github.com/shopspring/decimal"
"github.com/vmihailenco/msgpack/v5"
)
// Decimal numbers have 38 digits of precision, that is, the total
// number of digits before and after the decimal point can be 38.
// A decimal operation will fail if overflow happens (when a number is
// greater than 10^38 - 1 or less than -10^38 - 1).
//
// See also:
//
// - Tarantool module decimal:
// https://www.tarantool.io/en/doc/latest/reference/reference_lua/decimal/
const (
// Decimal external type.
decimalExtID = 1
decimalPrecision = 38
)
var (
one = decimal.NewFromInt(1)
// -10^decimalPrecision - 1
minSupportedDecimal = maxSupportedDecimal.Neg().Sub(one)
// 10^decimalPrecision - 1
maxSupportedDecimal = decimal.New(1, decimalPrecision).Sub(one)
)
//go:generate go tool gentypes -ext-code 1 Decimal
type Decimal struct {
decimal.Decimal
}
// MakeDecimal creates a new Decimal from a decimal.Decimal.
func MakeDecimal(decimal decimal.Decimal) Decimal {
return Decimal{Decimal: decimal}
}
// MakeDecimalFromString creates a new Decimal from a string.
func MakeDecimalFromString(src string) (Decimal, error) {
result := Decimal{}
dec, err := decimal.NewFromString(src)
if err != nil {
return result, err
}
result = MakeDecimal(dec)
return result, nil
}
var (
ErrDecimalOverflow = fmt.Errorf("msgpack: decimal number is bigger than"+
" maximum supported number (10^%d - 1)", decimalPrecision)
ErrDecimalUnderflow = fmt.Errorf("msgpack: decimal number is lesser than"+
" minimum supported number (-10^%d - 1)", decimalPrecision)
)
// MarshalMsgpack implements a custom msgpack marshaler.
func (d Decimal) MarshalMsgpack() ([]byte, error) {
switch {
case d.GreaterThan(maxSupportedDecimal):
return nil, ErrDecimalOverflow
case d.LessThan(minSupportedDecimal):
return nil, ErrDecimalUnderflow
}
// Decimal values can be encoded to fixext MessagePack, where buffer
// has a fixed length encoded by first byte, and ext MessagePack, where
// buffer length is not fixed and encoded by a number in a separate
// field:
//
// +--------+-------------------+------------+===============+
// | MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal |
// +--------+-------------------+------------+===============+
strBuf := d.Decimal.String()
bcdBuf, err := encodeStringToBCD(strBuf)
if err != nil {
return nil, fmt.Errorf("msgpack: can't encode string (%s) to a BCD buffer: %w", strBuf, err)
}
return bcdBuf, nil
}
// UnmarshalMsgpack implements a custom msgpack unmarshaler.
func (d *Decimal) UnmarshalMsgpack(data []byte) error {
digits, exp, err := decodeStringFromBCD(data)
if err != nil {
return fmt.Errorf("msgpack: can't decode string from BCD buffer (%x): %w", data, err)
}
dec, err := decimal.NewFromString(digits)
if err != nil {
return fmt.Errorf("msgpack: can't encode string (%s) to a decimal number: %w", digits, err)
}
if exp != 0 {
dec = dec.Shift(int32(exp))
}
*d = MakeDecimal(dec)
return nil
}
func decimalEncoder(e *msgpack.Encoder, v reflect.Value) ([]byte, error) {
dec := v.Interface().(Decimal)
return dec.MarshalMsgpack()
}
func decimalDecoder(d *msgpack.Decoder, v reflect.Value, extLen int) error {
b := make([]byte, extLen)
switch n, err := d.Buffered().Read(b); {
case err != nil:
return err
case n < extLen:
return fmt.Errorf("msgpack: unexpected end of stream after %d decimal bytes", n)
}
ptr := v.Addr().Interface().(*Decimal)
return ptr.UnmarshalMsgpack(b)
}
// This method converts the decimal type to a string.
// Use shopspring/decimal by default.
// String - optimized version for Tarantool Decimal
// taking into account the limitations of int64 and support for large numbers via fallback
// Tarantool decimal has 38 digits, which can exceed int64.
// Therefore, we cannot use int64 for all cases.
// For the general case, use shopspring/decimal.String().
// For cases where it is known that numbers contain less than 26 characters,
// you can use the optimized version.
func (d Decimal) String() string {
coefficient := d.Decimal.Coefficient() // Note: In shopspring/decimal
// the number is stored as coefficient *10^exponent, where exponent can be negative.
exponent := d.Decimal.Exponent()
// If exponent is positive, then we use the standard method.
if exponent > 0 {
return d.Decimal.String()
}
scale := -exponent
if !coefficient.IsInt64() {
return d.Decimal.String()
}
int64Value := coefficient.Int64()
return d.stringFromInt64(int64Value, int(scale))
}
// StringFromInt64 is an internal method for converting int64
// and scale to a string (for numbers up to 19 digits).
func (d Decimal) stringFromInt64(value int64, scale int) string {
var buf [64]byte
pos := 0
negative := value < 0
if negative {
if value == math.MinInt64 {
return d.handleMinInt64(scale)
}
buf[pos] = '-'
pos++
value = -value
}
str := strconv.FormatInt(value, 10)
length := len(str)
// Special case: zero value.
if value == 0 {
return "0" // Always return "0" regardless of scale.
}
if scale == 0 {
// No fractional part.
if pos+length > len(buf) {
return d.Decimal.String()
}
copy(buf[pos:], str)
pos += length
return string(buf[:pos])
}
if scale >= length {
// Numbers like 0.00123.
// Count trailing zeros in the fractional part.
trailingZeros := 0
// In this case, the fractional part consists
// of (scale-length) zeros followed by the number.
// We need to count trailing zeros in the actual number part.
for i := length - 1; i >= 0 && str[i] == '0'; i-- {
trailingZeros++
}
effectiveDigits := length - trailingZeros
// If all digits are zeros after leading zeros, we need to adjust.
if effectiveDigits == 0 {
return "0"
}
required := 2 + (scale - length) + effectiveDigits
if pos+required > len(buf) {
return d.Decimal.String()
}
buf[pos] = '0'
buf[pos+1] = '.'
pos += 2
// Add leading zeros.
zeros := scale - length
for i := 0; i < zeros; i++ {
buf[pos] = '0'
pos++
}
// Copy only significant digits (without trailing zeros).
copy(buf[pos:], str[:effectiveDigits])
pos += effectiveDigits
} else {
// Numbers like 123.45.
integerLen := length - scale
// Count trailing zeros in fractional part.
trailingZeros := 0
for i := length - 1; i >= integerLen && str[i] == '0'; i-- {
trailingZeros++
}
effectiveScale := scale - trailingZeros
// If all fractional digits are zeros, return just integer part.
if effectiveScale == 0 {
if pos+integerLen > len(buf) {
return d.Decimal.String()
}
copy(buf[pos:], str[:integerLen])
pos += integerLen
return string(buf[:pos])
}
required := integerLen + 1 + effectiveScale
if pos+required > len(buf) {
return d.Decimal.String()
}
// Integer part.
copy(buf[pos:], str[:integerLen])
pos += integerLen
// Decimal point.
buf[pos] = '.'
pos++
// Fractional part without trailing zeros.
fractionalEnd := integerLen + effectiveScale
copy(buf[pos:], str[integerLen:fractionalEnd])
pos += effectiveScale
}
return string(buf[:pos])
}
func (d Decimal) handleMinInt64(scale int) string {
const minInt64Str = "9223372036854775808"
var buf [64]byte
pos := 0
buf[pos] = '-'
pos++
length := len(minInt64Str)
if scale == 0 {
if pos+length > len(buf) {
return "-" + minInt64Str
}
copy(buf[pos:], minInt64Str)
pos += length
return string(buf[:pos])
}
if scale >= length {
// Count trailing zeros in the actual number part.
trailingZeros := 0
for i := length - 1; i >= 0 && minInt64Str[i] == '0'; i-- {
trailingZeros++
}
effectiveDigits := length - trailingZeros
if effectiveDigits == 0 {
return "0"
}
required := 2 + (scale - length) + effectiveDigits
if pos+required > len(buf) {
return d.Decimal.String()
}
buf[pos] = '0'
buf[pos+1] = '.'
pos += 2
zeros := scale - length
for i := 0; i < zeros; i++ {
buf[pos] = '0'
pos++
}
copy(buf[pos:], minInt64Str[:effectiveDigits])
pos += effectiveDigits
} else {
integerLen := length - scale
// Count trailing zeros for minInt64Str fractional part.
trailingZeros := 0
for i := length - 1; i >= integerLen && minInt64Str[i] == '0'; i-- {
trailingZeros++
}
effectiveScale := scale - trailingZeros
if effectiveScale == 0 {
if pos+integerLen > len(buf) {
return d.Decimal.String()
}
copy(buf[pos:], minInt64Str[:integerLen])
pos += integerLen
return string(buf[:pos])
}
required := integerLen + 1 + effectiveScale
if pos+required > len(buf) {
return d.Decimal.String()
}
copy(buf[pos:], minInt64Str[:integerLen])
pos += integerLen
buf[pos] = '.'
pos++
fractionalEnd := integerLen + effectiveScale
copy(buf[pos:], minInt64Str[integerLen:fractionalEnd])
pos += effectiveScale
}
return string(buf[:pos])
}
func MustMakeDecimal(src string) Decimal {
dec, err := MakeDecimalFromString(src)
if err != nil {
panic(fmt.Sprintf("MustMakeDecimalFromString: %v", err))
}
return dec
}
func init() {
msgpack.RegisterExtDecoder(decimalExtID, Decimal{}, decimalDecoder)
msgpack.RegisterExtEncoder(decimalExtID, Decimal{}, decimalEncoder)
}