-
Notifications
You must be signed in to change notification settings - Fork 2k
/
arithmetic.go
717 lines (629 loc) · 17.7 KB
/
arithmetic.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
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 (
"math"
"vitess.io/vitess/go/mysql/decimal"
"vitess.io/vitess/go/sqltypes"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
func dataOutOfRangeError[N1, N2 int64 | uint64 | float64](v1 N1, v2 N2, typ, sign string) error {
return vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.DataOutOfRange, "%s value is out of range in '(%v %s %v)'", typ, v1, sign, v2)
}
func dataOutOfRangeErrorDecimal(v1 decimal.Decimal, v2 decimal.Decimal, typ, sign string) error {
return vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.DataOutOfRange, "%s value is out of range in '(%v %s %v)'", typ, v1.String(), sign, v2.String())
}
func addNumericWithError(left, right eval) (eval, error) {
v1, v2 := makeNumericAndPrioritize(left, right)
switch v1 := v1.(type) {
case *evalInt64:
return mathAdd_ii(v1.i, v2.(*evalInt64).i)
case *evalUint64:
switch v2 := v2.(type) {
case *evalInt64:
return mathAdd_ui(v1.u, v2.i)
case *evalUint64:
return mathAdd_uu(v1.u, v2.u)
}
case *evalDecimal:
return mathAdd_dx(v1, v2), nil
case *evalFloat:
return mathAdd_fx(v1.f, v2)
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid arithmetic between: %s %s", evalToSQLValue(v1), evalToSQLValue(v2))
}
func subtractNumericWithError(left, right eval) (eval, error) {
v1 := evalToNumeric(left, true)
v2 := evalToNumeric(right, true)
switch v1 := v1.(type) {
case *evalInt64:
switch v2 := v2.(type) {
case *evalInt64:
return mathSub_ii(v1.i, v2.i)
case *evalUint64:
return mathSub_iu(v1.i, v2.u)
case *evalFloat:
return mathSub_xf(v1, v2.f)
case *evalDecimal:
return mathSub_xd(v1, v2), nil
}
case *evalUint64:
switch v2 := v2.(type) {
case *evalInt64:
return mathSub_ui(v1.u, v2.i)
case *evalUint64:
return mathSub_uu(v1.u, v2.u)
case *evalFloat:
return mathSub_xf(v1, v2.f)
case *evalDecimal:
return mathSub_xd(v1, v2), nil
}
case *evalFloat:
return mathSub_fx(v1.f, v2)
case *evalDecimal:
switch v2 := v2.(type) {
case *evalFloat:
return mathSub_xf(v1, v2.f)
default:
return mathSub_dx(v1, v2), nil
}
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid arithmetic between: %s %s", evalToSQLValue(v1), evalToSQLValue(v2))
}
func multiplyNumericWithError(left, right eval) (eval, error) {
v1, v2 := makeNumericAndPrioritize(left, right)
switch v1 := v1.(type) {
case *evalInt64:
return mathMul_ii(v1.i, v2.(*evalInt64).i)
case *evalUint64:
switch v2 := v2.(type) {
case *evalInt64:
return mathMul_ui(v1.u, v2.i)
case *evalUint64:
return mathMul_uu(v1.u, v2.u)
}
case *evalFloat:
return mathMul_fx(v1.f, v2)
case *evalDecimal:
return mathMul_dx(v1, v2), nil
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid arithmetic between: %s %s", evalToSQLValue(v1), evalToSQLValue(v2))
}
func divideNumericWithError(left, right eval, precise bool) (eval, error) {
v1 := evalToNumeric(left, true)
v2 := evalToNumeric(right, true)
if v1, ok := v1.(*evalFloat); ok {
return mathDiv_fx(v1.f, v2)
}
if v2, ok := v2.(*evalFloat); ok {
v1f, ok := v1.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathDiv_fx(v1f.f, v2)
}
return mathDiv_xx(v1, v2, divPrecisionIncrement)
}
func integerDivideConvert(arg eval) evalNumeric {
if dec, ok := arg.(evalNumeric); ok {
return dec
}
if b1, ok := arg.(*evalBytes); ok {
if b1.isHexLiteral() {
hex, ok := b1.toNumericHex()
if !ok {
return newEvalDecimal(decimal.Zero, 0, 0)
}
return hex
}
if b1.isBitLiteral() {
bit, ok := b1.toNumericBit()
if !ok {
return newEvalDecimal(decimal.Zero, 0, 0)
}
return bit
}
}
return evalToDecimal(arg, 0, 0)
}
func integerDivideNumericWithError(left, right eval) (eval, error) {
v1 := integerDivideConvert(left)
v2 := integerDivideConvert(right)
switch v1 := v1.(type) {
case *evalInt64:
switch v2 := v2.(type) {
case *evalInt64:
return mathIntDiv_ii(v1, v2)
case *evalUint64:
return mathIntDiv_iu(v1, v2)
case *evalFloat:
return mathIntDiv_di(v1.toDecimal(0, 0), v2.toDecimal(0, 0))
case *evalDecimal:
return mathIntDiv_di(v1.toDecimal(0, 0), v2)
}
case *evalUint64:
switch v2 := v2.(type) {
case *evalInt64:
return mathIntDiv_ui(v1, v2)
case *evalUint64:
return mathIntDiv_uu(v1, v2)
case *evalFloat:
return mathIntDiv_du(v1.toDecimal(0, 0), v2.toDecimal(0, 0))
case *evalDecimal:
return mathIntDiv_du(v1.toDecimal(0, 0), v2)
}
case *evalFloat:
switch v2 := v2.(type) {
case *evalUint64:
return mathIntDiv_du(v1.toDecimal(0, 0), v2.toDecimal(0, 0))
default:
return mathIntDiv_di(v1.toDecimal(0, 0), v2.toDecimal(0, 0))
}
case *evalDecimal:
switch v2 := v2.(type) {
case *evalUint64:
return mathIntDiv_du(v1, v2.toDecimal(0, 0))
default:
return mathIntDiv_di(v1, v2.toDecimal(0, 0))
}
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid arithmetic between: %s %s", evalToSQLValue(v1), evalToSQLValue(v2))
}
func modNumericWithError(left, right eval, precise bool) (eval, error) {
v1 := evalToNumeric(left, true)
v2 := evalToNumeric(right, true)
switch v1 := v1.(type) {
case *evalInt64:
switch v2 := v2.(type) {
case *evalInt64:
return mathMod_ii(v1, v2)
case *evalUint64:
return mathMod_iu(v1, v2)
case *evalFloat:
v1f, ok := v1.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathMod_ff(v1f, v2)
case *evalDecimal:
return mathMod_dd(v1.toDecimal(0, 0), v2)
}
case *evalUint64:
switch v2 := v2.(type) {
case *evalInt64:
return mathMod_ui(v1, v2)
case *evalUint64:
return mathMod_uu(v1, v2)
case *evalFloat:
v1f, ok := v1.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathMod_ff(v1f, v2)
case *evalDecimal:
return mathMod_dd(v1.toDecimal(0, 0), v2)
}
case *evalDecimal:
switch v2 := v2.(type) {
case *evalFloat:
v1f, ok := v1.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathMod_ff(v1f, v2)
default:
return mathMod_dd(v1, v2.toDecimal(0, 0))
}
case *evalFloat:
v2f, ok := v2.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathMod_ff(v1, v2f)
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "invalid arithmetic between: %s %s", evalToSQLValue(v1), evalToSQLValue(v2))
}
// makeNumericAndPrioritize reorders the input parameters
// to be Float64, Decimal, Uint64, Int64.
func makeNumericAndPrioritize(left, right eval) (evalNumeric, evalNumeric) {
i1 := evalToNumeric(left, true)
i2 := evalToNumeric(right, true)
switch i1.SQLType() {
case sqltypes.Int64:
if i2.SQLType() == sqltypes.Uint64 || i2.SQLType() == sqltypes.Float64 || i2.SQLType() == sqltypes.Decimal {
return i2, i1
}
case sqltypes.Uint64:
if i2.SQLType() == sqltypes.Float64 || i2.SQLType() == sqltypes.Decimal {
return i2, i1
}
case sqltypes.Decimal:
if i2.SQLType() == sqltypes.Float64 {
return i2, i1
}
}
return i1, i2
}
func mathAdd_ii(v1, v2 int64) (eval, error) {
result, err := mathAdd_ii0(v1, v2)
return newEvalInt64(result), err
}
func mathAdd_ii0(v1, v2 int64) (int64, error) {
result := v1 + v2
if (result > v1) != (v2 > 0) {
return 0, dataOutOfRangeError(v1, v2, "BIGINT", "+")
}
return result, nil
}
func mathAdd_ui(v1 uint64, v2 int64) (*evalUint64, error) {
result, err := mathAdd_ui0(v1, v2)
return newEvalUint64(result), err
}
func mathAdd_ui0(v1 uint64, v2 int64) (uint64, error) {
result := v1 + uint64(v2)
if v2 < 0 && v1 < uint64(-v2) || v2 > 0 && (result < v1 || result < uint64(v2)) {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "+")
}
return result, nil
}
func mathAdd_uu(v1, v2 uint64) (*evalUint64, error) {
result, err := mathAdd_uu0(v1, v2)
return newEvalUint64(result), err
}
func mathAdd_uu0(v1, v2 uint64) (uint64, error) {
result := v1 + v2
if result < v1 || result < v2 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "+")
}
return result, nil
}
var errDecimalOutOfRange = vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.DataOutOfRange, "DECIMAL value is out of range")
func mathAdd_fx(v1 float64, v2 evalNumeric) (*evalFloat, error) {
v2f, ok := v2.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathAdd_ff(v1, v2f.f), nil
}
func mathAdd_ff(v1, v2 float64) *evalFloat {
return newEvalFloat(v1 + v2)
}
func mathAdd_dx(v1 *evalDecimal, v2 evalNumeric) *evalDecimal {
return mathAdd_dd(v1, v2.toDecimal(0, 0))
}
func mathAdd_dd(v1, v2 *evalDecimal) *evalDecimal {
return newEvalDecimalWithPrec(v1.dec.Add(v2.dec), max(v1.length, v2.length))
}
func mathAdd_dd0(v1, v2 *evalDecimal) {
v1.dec = v1.dec.Add(v2.dec)
v1.length = max(v1.length, v2.length)
}
func mathSub_ii(v1, v2 int64) (*evalInt64, error) {
result, err := mathSub_ii0(v1, v2)
return newEvalInt64(result), err
}
func mathSub_ii0(v1, v2 int64) (int64, error) {
result := v1 - v2
if (result < v1) != (v2 > 0) {
return 0, dataOutOfRangeError(v1, v2, "BIGINT", "-")
}
return result, nil
}
func mathSub_iu(v1 int64, v2 uint64) (*evalUint64, error) {
result, err := mathSub_iu0(v1, v2)
return newEvalUint64(result), err
}
func mathSub_iu0(v1 int64, v2 uint64) (uint64, error) {
if v1 < 0 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "-")
}
return mathSub_uu0(uint64(v1), v2)
}
func mathSub_ui(v1 uint64, v2 int64) (*evalUint64, error) {
result, err := mathSub_ui0(v1, v2)
return newEvalUint64(result), err
}
func mathSub_ui0(v1 uint64, v2 int64) (uint64, error) {
if v2 > 0 && v1 < uint64(v2) {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "-")
}
// uint - (- int) = uint + int
if v2 < 0 {
return mathAdd_uu0(v1, uint64(-v2))
}
return mathSub_uu0(v1, uint64(v2))
}
func mathSub_uu(v1, v2 uint64) (*evalUint64, error) {
result, err := mathSub_uu0(v1, v2)
return newEvalUint64(result), err
}
func mathSub_uu0(v1, v2 uint64) (uint64, error) {
result := v1 - v2
if v2 > v1 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "-")
}
return result, nil
}
func mathSub_fx(v1 float64, v2 evalNumeric) (*evalFloat, error) {
v2f, ok := v2.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathSub_ff(v1, v2f.f), nil
}
func mathSub_xf(v1 evalNumeric, v2 float64) (*evalFloat, error) {
v1f, ok := v1.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathSub_ff(v1f.f, v2), nil
}
func mathSub_ff(v1, v2 float64) *evalFloat {
return newEvalFloat(v1 - v2)
}
func mathSub_dx(v1 *evalDecimal, v2 evalNumeric) *evalDecimal {
return mathSub_dd(v1, v2.toDecimal(0, 0))
}
func mathSub_xd(v1 evalNumeric, v2 *evalDecimal) *evalDecimal {
return mathSub_dd(v1.toDecimal(0, 0), v2)
}
func mathSub_dd(v1, v2 *evalDecimal) *evalDecimal {
return newEvalDecimalWithPrec(v1.dec.Sub(v2.dec), max(v1.length, v2.length))
}
func mathSub_dd0(v1, v2 *evalDecimal) {
v1.dec = v1.dec.Sub(v2.dec)
v1.length = max(v1.length, v2.length)
}
func mathMul_ii(v1, v2 int64) (*evalInt64, error) {
result, err := mathMul_ii0(v1, v2)
return newEvalInt64(result), err
}
func mathMul_ii0(v1, v2 int64) (int64, error) {
result := v1 * v2
if v1 != 0 && result/v1 != v2 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT", "*")
}
return result, nil
}
func mathMul_ui(v1 uint64, v2 int64) (*evalUint64, error) {
result, err := mathMul_ui0(v1, v2)
return newEvalUint64(result), err
}
func mathMul_ui0(v1 uint64, v2 int64) (uint64, error) {
if v1 == 0 || v2 == 0 {
return 0, nil
}
if v2 < 0 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "*")
}
return mathMul_uu0(v1, uint64(v2))
}
func mathMul_uu(v1, v2 uint64) (*evalUint64, error) {
result, err := mathMul_uu0(v1, v2)
return newEvalUint64(result), err
}
func mathMul_uu0(v1, v2 uint64) (uint64, error) {
if v1 == 0 || v2 == 0 {
return 0, nil
}
result := v1 * v2
if result < v2 || result < v1 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "*")
}
return result, nil
}
func mathMul_fx(v1 float64, v2 evalNumeric) (eval, error) {
v2f, ok := v2.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathMul_ff(v1, v2f.f), nil
}
func mathMul_ff(v1, v2 float64) *evalFloat {
return newEvalFloat(v1 * v2)
}
func mathMul_dx(v1 *evalDecimal, v2 evalNumeric) *evalDecimal {
return mathMul_dd(v1, v2.toDecimal(0, 0))
}
func mathMul_dd(v1, v2 *evalDecimal) *evalDecimal {
return newEvalDecimalWithPrec(v1.dec.Mul(v2.dec), v1.length+v2.length)
}
func mathMul_dd0(v1, v2 *evalDecimal) {
v1.dec = v1.dec.Mul(v2.dec)
v1.length = v1.length + v2.length
}
const divPrecisionIncrement = 4
func mathDiv_xx(v1, v2 evalNumeric, incrPrecision int32) (eval, error) {
return mathDiv_dd(v1.toDecimal(0, 0), v2.toDecimal(0, 0), incrPrecision)
}
func mathDiv_dd(v1, v2 *evalDecimal, incrPrecision int32) (eval, error) {
if v2.dec.IsZero() {
return nil, nil
}
return newEvalDecimalWithPrec(v1.dec.Div(v2.dec, incrPrecision), v1.length+incrPrecision), nil
}
func mathDiv_dd0(v1, v2 *evalDecimal, incrPrecision int32) {
v1.dec = v1.dec.Div(v2.dec, incrPrecision)
v1.length = v1.length + incrPrecision
}
func mathDiv_fx(v1 float64, v2 evalNumeric) (eval, error) {
v2f, ok := v2.toFloat()
if !ok {
return nil, errDecimalOutOfRange
}
return mathDiv_ff(v1, v2f.f)
}
func mathDiv_ff(v1, v2 float64) (eval, error) {
if v2 == 0.0 {
return nil, nil
}
result, err := mathDiv_ff0(v1, v2)
return newEvalFloat(result), err
}
func mathDiv_ff0(v1, v2 float64) (float64, error) {
result := v1 / v2
if math.IsInf(result, 1) || math.IsInf(result, -1) {
return 0, dataOutOfRangeError(v1, v2, "DOUBLE", "/")
}
return result, nil
}
func mathIntDiv_ii(v1, v2 *evalInt64) (eval, error) {
if v2.i == 0 {
return nil, nil
}
result := v1.i / v2.i
return newEvalInt64(result), nil
}
func mathIntDiv_iu(v1 *evalInt64, v2 *evalUint64) (eval, error) {
if v2.u == 0 {
return nil, nil
}
result, err := mathIntDiv_iu0(v1.i, v2.u)
return newEvalUint64(result), err
}
func mathIntDiv_iu0(v1 int64, v2 uint64) (uint64, error) {
if v1 < 0 {
if v2 >= math.MaxInt64 {
// We know here that v2 is always so large the result
// must be 0.
return 0, nil
}
result := v1 / int64(v2)
if result < 0 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "DIV")
}
return uint64(result), nil
}
return uint64(v1) / v2, nil
}
func mathIntDiv_ui(v1 *evalUint64, v2 *evalInt64) (eval, error) {
if v2.i == 0 {
return nil, nil
}
result, err := mathIntDiv_ui0(v1.u, v2.i)
return newEvalUint64(result), err
}
func mathIntDiv_ui0(v1 uint64, v2 int64) (uint64, error) {
if v2 < 0 {
if v1 >= math.MaxInt64 {
// We know that v1 is always large here and with v2, the result
// must be at least -1 so we can't store this in the available range.
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "DIV")
}
// Safe to cast since we know it fits in int64 when we get here.
result := int64(v1) / v2
if result < 0 {
return 0, dataOutOfRangeError(v1, v2, "BIGINT UNSIGNED", "DIV")
}
return uint64(result), nil
}
return v1 / uint64(v2), nil
}
func mathIntDiv_uu(v1, v2 *evalUint64) (eval, error) {
if v2.u == 0 {
return nil, nil
}
return newEvalUint64(v1.u / v2.u), nil
}
func mathIntDiv_di(v1, v2 *evalDecimal) (eval, error) {
if v2.dec.IsZero() {
return nil, nil
}
result, err := mathIntDiv_di0(v1, v2)
return newEvalInt64(result), err
}
func mathIntDiv_di0(v1, v2 *evalDecimal) (int64, error) {
div, _ := v1.dec.QuoRem(v2.dec, 0)
result, ok := div.Int64()
if !ok {
return 0, dataOutOfRangeErrorDecimal(v1.dec, v2.dec, "BIGINT", "DIV")
}
return result, nil
}
func mathIntDiv_du(v1, v2 *evalDecimal) (eval, error) {
if v2.dec.IsZero() {
return nil, nil
}
result, err := mathIntDiv_du0(v1, v2)
return newEvalUint64(result), err
}
func mathIntDiv_du0(v1, v2 *evalDecimal) (uint64, error) {
div, _ := v1.dec.QuoRem(v2.dec, 0)
result, ok := div.Uint64()
if !ok {
return 0, dataOutOfRangeErrorDecimal(v1.dec, v2.dec, "BIGINT UNSIGNED", "DIV")
}
return result, nil
}
func mathMod_ii(v1, v2 *evalInt64) (eval, error) {
if v2.i == 0 {
return nil, nil
}
return newEvalInt64(v1.i % v2.i), nil
}
func mathMod_iu(v1 *evalInt64, v2 *evalUint64) (eval, error) {
if v2.u == 0 {
return nil, nil
}
return newEvalInt64(mathMod_iu0(v1.i, v2.u)), nil
}
func mathMod_iu0(v1 int64, v2 uint64) int64 {
if v1 == math.MinInt64 && v2 == math.MaxInt64+1 {
return 0
}
if v2 > math.MaxInt64 {
return v1
}
return v1 % int64(v2)
}
func mathMod_ui(v1 *evalUint64, v2 *evalInt64) (eval, error) {
if v2.i == 0 {
return nil, nil
}
result, err := mathMod_ui0(v1.u, v2.i)
return newEvalUint64(result), err
}
func mathMod_ui0(v1 uint64, v2 int64) (uint64, error) {
if v2 < 0 {
return v1 % uint64(-v2), nil
}
return v1 % uint64(v2), nil
}
func mathMod_uu(v1, v2 *evalUint64) (eval, error) {
if v2.u == 0 {
return nil, nil
}
return newEvalUint64(v1.u % v2.u), nil
}
func mathMod_ff(v1, v2 *evalFloat) (eval, error) {
if v2.f == 0.0 {
return nil, nil
}
return newEvalFloat(math.Mod(v1.f, v2.f)), nil
}
func mathMod_dd(v1, v2 *evalDecimal) (eval, error) {
if v2.dec.IsZero() {
return nil, nil
}
dec, prec := mathMod_dd0(v1, v2)
return newEvalDecimalWithPrec(dec, prec), nil
}
func mathMod_dd0(v1, v2 *evalDecimal) (decimal.Decimal, int32) {
length := v1.length
if v2.length > length {
length = v2.length
}
_, rem := v1.dec.QuoRem(v2.dec, 0)
return rem, length
}