-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
eval_numeric.go
526 lines (465 loc) · 12.3 KB
/
eval_numeric.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package evalengine
import (
"fmt"
"math"
"strconv"
"vitess.io/vitess/go/mysql/decimal"
"vitess.io/vitess/go/mysql/fastparse"
"vitess.io/vitess/go/mysql/format"
"vitess.io/vitess/go/mysql/json"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vthash"
)
type (
evalNumeric interface {
eval
hashable
toFloat() (*evalFloat, bool)
toDecimal(m, d int32) *evalDecimal
toInt64() *evalInt64
toUint64() *evalUint64
negate() evalNumeric
}
evalInt64 struct {
i int64
}
evalUint64 struct {
u uint64
hexLiteral bool
}
evalFloat struct {
f float64
}
evalDecimal struct {
dec decimal.Decimal
length int32
}
)
var _ evalNumeric = (*evalInt64)(nil)
var _ evalNumeric = (*evalUint64)(nil)
var _ evalNumeric = (*evalFloat)(nil)
var _ evalNumeric = (*evalDecimal)(nil)
var evalBoolTrue = &evalInt64{1}
var evalBoolFalse = &evalInt64{0}
func newEvalUint64(u uint64) *evalUint64 {
return &evalUint64{u: u}
}
func newEvalInt64(i int64) *evalInt64 {
return &evalInt64{i: i}
}
func newEvalFloat(f float64) *evalFloat {
return &evalFloat{f: f}
}
func newEvalDecimal(dec decimal.Decimal, m, d int32) *evalDecimal {
if m == 0 && d == 0 {
return newEvalDecimalWithPrec(dec, -dec.Exponent())
}
return newEvalDecimalWithPrec(dec.Clamp(m-d, d), d)
}
func newEvalDecimalWithPrec(dec decimal.Decimal, prec int32) *evalDecimal {
return &evalDecimal{dec: dec, length: prec}
}
func newEvalBool(b bool) *evalInt64 {
if b {
return evalBoolTrue
}
return evalBoolFalse
}
func evalToNumeric(e eval, preciseDatetime bool) evalNumeric {
switch e := e.(type) {
case evalNumeric:
return e
case *evalBytes:
if e.isHexLiteral {
hex, ok := e.toNumericHex()
if !ok {
// overflow
return newEvalFloat(0)
}
return hex
}
f, _ := fastparse.ParseFloat64(e.string())
return &evalFloat{f: f}
case *evalJSON:
switch e.Type() {
case json.TypeBoolean:
if e == json.ValueTrue {
return &evalFloat{f: 1.0}
}
return &evalFloat{f: 0.0}
case json.TypeNumber:
f, _ := e.Float64()
return &evalFloat{f: f}
case json.TypeString:
f, _ := fastparse.ParseFloat64(e.Raw())
return &evalFloat{f: f}
default:
return &evalFloat{f: 0}
}
case *evalTemporal:
if preciseDatetime {
if e.prec == 0 {
return newEvalInt64(e.toInt64())
}
return newEvalDecimalWithPrec(e.toDecimal(), int32(e.prec))
}
return &evalFloat{f: e.toFloat()}
default:
panic("unsupported")
}
}
func evalToFloat(e eval) (*evalFloat, bool) {
switch e := e.(type) {
case *evalFloat:
return e, true
case evalNumeric:
return e.toFloat()
case *evalBytes:
if e.isHexLiteral {
hex, ok := e.toNumericHex()
if !ok {
// overflow
return newEvalFloat(0), false
}
f, ok := hex.toFloat()
if !ok {
return newEvalFloat(0), false
}
return f, true
}
val, err := fastparse.ParseFloat64(e.string())
return &evalFloat{f: val}, err == nil
case *evalJSON:
switch e.Type() {
case json.TypeBoolean:
if e == json.ValueTrue {
return &evalFloat{f: 1.0}, true
}
return &evalFloat{f: 0.0}, true
case json.TypeNumber:
f, ok := e.Float64()
return &evalFloat{f: f}, ok
case json.TypeString:
val, err := fastparse.ParseFloat64(e.Raw())
return &evalFloat{f: val}, err == nil
default:
return &evalFloat{f: 0}, true
}
case *evalTemporal:
return &evalFloat{f: e.toFloat()}, true
default:
panic(fmt.Sprintf("unsupported type %T", e))
}
}
func evalToDecimal(e eval, m, d int32) *evalDecimal {
switch e := e.(type) {
case evalNumeric:
return e.toDecimal(m, d)
case *evalBytes:
if e.isHexLiteral {
hex, ok := e.toNumericHex()
if !ok {
// overflow
return newEvalDecimal(decimal.Zero, m, d)
}
return hex.toDecimal(m, d)
}
dec, _ := decimal.NewFromString(e.string())
return newEvalDecimal(dec, m, d)
case *evalJSON:
switch e.Type() {
case json.TypeBoolean:
if e == json.ValueTrue {
return newEvalDecimal(decimal.NewFromInt(1), m, d)
}
return newEvalDecimal(decimal.Zero, m, d)
case json.TypeNumber:
switch e.NumberType() {
case json.NumberTypeSigned:
i, _ := e.Int64()
return newEvalDecimal(decimal.NewFromInt(i), m, d)
case json.NumberTypeUnsigned:
// If the value fits in an unsigned integer, convert to that
// and then cast it to a signed integer and then turn it into a decimal.
// SELECT CAST(CAST(18446744073709551615 AS JSON) AS DECIMAL) -> -1
u, _ := e.Uint64()
return newEvalDecimal(decimal.NewFromInt(int64(u)), m, d)
case json.NumberTypeDecimal:
dec, _ := e.Decimal()
return newEvalDecimal(dec, m, d)
case json.NumberTypeFloat:
f, _ := e.Float64()
dec := decimal.NewFromFloat(f)
return newEvalDecimal(dec, m, d)
default:
panic("unreachable")
}
case json.TypeString:
dec, _ := decimal.NewFromString(e.Raw())
return newEvalDecimal(dec, m, d)
default:
return newEvalDecimal(decimal.Zero, m, d)
}
case *evalTemporal:
return newEvalDecimal(e.toDecimal(), m, d)
default:
panic("unsupported")
}
}
func evalToInt64(e eval) *evalInt64 {
switch e := e.(type) {
case *evalInt64:
return e
case evalNumeric:
return e.toInt64()
case *evalBytes:
if e.isHexLiteral {
hex, ok := e.toNumericHex()
if !ok {
// overflow
return newEvalInt64(0)
}
return hex.toInt64()
}
i, _ := fastparse.ParseInt64(e.string(), 10)
return newEvalInt64(i)
case *evalJSON:
switch e.Type() {
case json.TypeBoolean:
if e == json.ValueTrue {
return newEvalInt64(1)
}
return newEvalInt64(0)
case json.TypeNumber:
switch e.NumberType() {
case json.NumberTypeSigned:
i, _ := e.Int64()
return newEvalInt64(i)
case json.NumberTypeUnsigned:
u, _ := e.Uint64()
// OMG, MySQL is really terrible at this.
return newEvalInt64(int64(u))
case json.NumberTypeDecimal:
d, _ := e.Decimal()
return newEvalInt64(decimalToInt64(d))
case json.NumberTypeFloat:
f, _ := e.Float64()
return newEvalInt64(floatToInt64(f))
default:
panic("unsupported")
}
case json.TypeString:
i, _ := fastparse.ParseInt64(e.Raw(), 10)
return newEvalInt64(i)
default:
return newEvalInt64(0)
}
case *evalTemporal:
return newEvalInt64(e.toInt64())
default:
panic(fmt.Sprintf("unsupported type: %T", e))
}
}
func (e *evalInt64) Hash(h *vthash.Hasher) {
if e.i < 0 {
h.Write16(hashPrefixIntegralNegative)
} else {
h.Write16(hashPrefixIntegralPositive)
}
h.Write64(uint64(e.i))
}
func (e *evalInt64) SQLType() sqltypes.Type {
return sqltypes.Int64
}
func (e *evalInt64) ToRawBytes() []byte {
return strconv.AppendInt(nil, e.i, 10)
}
func (e *evalInt64) negate() evalNumeric {
if e.i == math.MinInt64 {
return newEvalDecimalWithPrec(decimal.NewFromInt(e.i).NegInPlace(), 0)
}
return newEvalInt64(-e.i)
}
func (e *evalInt64) toInt64() *evalInt64 {
return e
}
func (e *evalInt64) toFloat0() float64 {
return float64(e.i)
}
func (e *evalInt64) toFloat() (*evalFloat, bool) {
return newEvalFloat(e.toFloat0()), true
}
func (e *evalInt64) toDecimal(m, d int32) *evalDecimal {
return newEvalDecimal(decimal.NewFromInt(e.i), m, d)
}
func (e *evalInt64) toUint64() *evalUint64 {
return newEvalUint64(uint64(e.i))
}
func (e *evalUint64) Hash(h *vthash.Hasher) {
h.Write16(hashPrefixIntegralPositive)
h.Write64(e.u)
}
func (e *evalUint64) SQLType() sqltypes.Type {
return sqltypes.Uint64
}
func (e *evalUint64) ToRawBytes() []byte {
return strconv.AppendUint(nil, e.u, 10)
}
func (e *evalUint64) negate() evalNumeric {
if e.hexLiteral {
return newEvalFloat(-float64(e.u))
}
if e.u > math.MaxInt64+1 {
return newEvalDecimalWithPrec(decimal.NewFromUint(e.u).NegInPlace(), 0)
}
return newEvalInt64(-int64(e.u))
}
func (e *evalUint64) toInt64() *evalInt64 {
return newEvalInt64(int64(e.u))
}
func (e *evalUint64) toFloat0() float64 {
return float64(e.u)
}
func (e *evalUint64) toFloat() (*evalFloat, bool) {
return newEvalFloat(e.toFloat0()), true
}
func (e *evalUint64) toDecimal(m, d int32) *evalDecimal {
return newEvalDecimal(decimal.NewFromUint(e.u), m, d)
}
func (e *evalUint64) toUint64() *evalUint64 {
return e
}
func (e *evalFloat) Hash(h *vthash.Hasher) {
h.Write16(hashPrefixFloat)
h.Write64(math.Float64bits(e.f))
}
func (e *evalFloat) SQLType() sqltypes.Type {
return sqltypes.Float64
}
func (e *evalFloat) ToRawBytes() []byte {
return format.FormatFloat(e.f)
}
func (e *evalFloat) negate() evalNumeric {
return newEvalFloat(-e.f)
}
func floatToInt64(f float64) int64 {
// the int64(f) conversion is always well-defined, but for float values larger than
// MaxInt64, it returns a negative value. Check for underflow: if the sign of
// our integral is negative but our float is not, clamp to MaxInt64 like MySQL does.
i := int64(math.Round(f))
if i < 0 && !math.Signbit(f) {
i = math.MaxInt64
}
return i
}
func (e *evalFloat) toInt64() *evalInt64 {
return newEvalInt64(floatToInt64(e.f))
}
func (e *evalFloat) toFloat() (*evalFloat, bool) {
return e, true
}
func (e *evalFloat) toDecimal(m, d int32) *evalDecimal {
return newEvalDecimal(decimal.NewFromFloatMySQL(e.f), m, d)
}
func (e *evalFloat) toUint64() *evalUint64 {
// We want to convert a float64 to its uint64 representation.
// However, the cast `uint64(f)`, when f < 0, is actually implementation-defined
// behavior in Go, so we cannot use it here.
// The most noticeable example of this are M1 Macs with their ARM64 chipsets, where
// the underflow is clamped at 0:
//
// GOARCH=amd64 | uint64(-2.0) == 18446744073709551614
// GOARCH=arm64 | uint64(-2.0) == 0
//
// The most obvious way to keep this well-defined is to do a two-step conversion:
// float64 -> int64 -> uint64
// where every step of the conversion is well-defined. However, this conversion overflows
// a range of floats, those larger than MaxInt64 but that would still fit in a 64-bit unsigned
// integer. What's the right way to handle this overflow?
//
// Fortunately for us, the `uint64(f)` conversion for negative numbers is also undefined
// behavior in C and C++, so MySQL is already handling this case! From running this
// integration test, we can verify that MySQL is using a two-step conversion and it's clamping
// the value to MaxInt64 on overflow:
//
// mysql> SELECT CAST(18446744073709540000e0 AS UNSIGNED);
// +------------------------------------------+
// | CAST(18446744073709540000e0 AS UNSIGNED) |
// +------------------------------------------+
// | 9223372036854775807 |
// +------------------------------------------+
//
f := math.Round(e.f)
i := uint64(int64(f))
if i > math.MaxInt64 && !math.Signbit(f) {
i = math.MaxInt64
}
return newEvalUint64(i)
}
func (e *evalDecimal) Hash(h *vthash.Hasher) {
h.Write16(hashPrefixDecimal)
e.dec.Hash(h)
}
func (e *evalDecimal) SQLType() sqltypes.Type {
return sqltypes.Decimal
}
func (e *evalDecimal) ToRawBytes() []byte {
return e.dec.FormatMySQL(e.length)
}
func (e *evalDecimal) negate() evalNumeric {
if e.dec.IsZero() {
return e
}
return newEvalDecimalWithPrec(e.dec.Neg(), e.length)
}
func decimalToInt64(dec decimal.Decimal) int64 {
dec = dec.Round(0)
i, valid := dec.Int64()
if !valid {
if dec.Sign() < 0 {
return math.MinInt64
}
return math.MaxInt64
}
return i
}
func (e *evalDecimal) toInt64() *evalInt64 {
return newEvalInt64(decimalToInt64(e.dec))
}
func (e *evalDecimal) toFloat0() (float64, bool) {
return e.dec.Float64()
}
func (e *evalDecimal) toFloat() (*evalFloat, bool) {
f, exact := e.toFloat0()
return newEvalFloat(f), exact
}
func (e *evalDecimal) toDecimal(m, d int32) *evalDecimal {
if m == 0 && d == 0 {
return e
}
return newEvalDecimal(e.dec, m, d)
}
func (e *evalDecimal) toUint64() *evalUint64 {
dec := e.dec.Round(0)
if dec.Sign() < 0 {
i, _ := dec.Int64()
return newEvalUint64(uint64(i))
}
u, valid := dec.Uint64()
if !valid {
return newEvalUint64(math.MaxUint64)
}
return newEvalUint64(u)
}