/
binop.go
818 lines (733 loc) · 18.4 KB
/
binop.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expressions
import (
"fmt"
"math"
"github.com/juju/errors"
"github.com/Dong-Chan/alloydb/context"
"github.com/Dong-Chan/alloydb/expression"
mysql "github.com/Dong-Chan/alloydb/mysqldef"
"github.com/Dong-Chan/alloydb/parser/opcode"
"github.com/Dong-Chan/alloydb/util/types"
)
var _ expression.Expression = (*BinaryOperation)(nil)
// BinaryOperation is for binary operation like 1 + 1, 1 - 1, etc.
type BinaryOperation struct {
// Op is the operator code for BinaryOperation.
Op opcode.Op
// L is the left expression in BinaryOperation.
L expression.Expression
// R is the right expression in BinaryOperation.
R expression.Expression
}
// NewBinaryOperation creates a binary expression with op as the operator code and
// x, y as the left and right operator expression.
func NewBinaryOperation(op opcode.Op, x, y expression.Expression) expression.Expression {
b := &BinaryOperation{op, x, y}
var ok bool
// Normalize relational comparison operation like format "ident op expr"
// if left is Ident operation, return directly
if _, ok = b.L.(*Ident); ok {
return b
}
var r expression.Expression
// left is not Ident expression, check right
if r, ok = b.R.(*Ident); !ok {
return b
}
// Normalize expr relOp indent: Ident InRelOp expr
switch b.Op {
case opcode.LT:
return &BinaryOperation{opcode.GT, r, b.L}
case opcode.LE:
return &BinaryOperation{opcode.GE, r, b.L}
case opcode.GT:
return &BinaryOperation{opcode.LT, r, b.L}
case opcode.GE:
return &BinaryOperation{opcode.LE, r, b.L}
case opcode.EQ, opcode.NE:
return &BinaryOperation{b.Op, r, b.L}
default:
return b
}
}
// IsIdentRelOpVal checks left expression is Ident expression and right is Value expression
// for relational comparison, then returns left identifier name and right value.
func (o *BinaryOperation) IsIdentRelOpVal() (bool, string, interface{}, error) {
sid := ""
id, ok := o.L.(*Ident)
if ok {
// TODO: maybe we can remove this later
if IsQualified(id.O) {
return false, "", nil, nil
}
sid = id.O
} else {
return false, "", nil, nil
}
if v, ok := o.R.(Value); ok {
switch o.Op {
case opcode.LT, opcode.LE,
opcode.GT, opcode.GE,
opcode.EQ, opcode.NE:
return true, sid, v.Val, nil
default:
return false, "", nil, nil
}
}
return false, "", nil, nil
}
// Clone implements the Expression Clone interface.
func (o *BinaryOperation) Clone() (expression.Expression, error) {
l, err := o.L.Clone()
if err != nil {
return nil, err
}
r, err := o.R.Clone()
if err != nil {
return nil, err
}
return NewBinaryOperation(o.Op, l, r), nil
}
// IsStatic implements the Expression IsStatic interface.
func (o *BinaryOperation) IsStatic() bool {
return o.L.IsStatic() && o.R.IsStatic()
}
// String implements the Expression String interface.
func (o *BinaryOperation) String() string {
return fmt.Sprintf("%s %s %s", o.L, o.Op, o.R)
}
func (o *BinaryOperation) traceErr(err error) error {
if err == nil {
return nil
}
return errors.Errorf("eval %s err: %v", o, err)
}
// Eval implements the Expression Eval interface.
func (o *BinaryOperation) Eval(ctx context.Context, args map[interface{}]interface{}) (r interface{}, err error) {
defer func() {
if e := recover(); e != nil {
r, err = nil, errors.Errorf("%v", e)
err = o.traceErr(err)
}
}()
switch o.Op {
case opcode.AndAnd, opcode.OrOr, opcode.LogicXor:
return o.evalLogicOp(ctx, args)
case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE:
return o.evalComparisonOp(ctx, args)
case opcode.RightShift, opcode.LeftShift, opcode.And, opcode.Or, opcode.Xor:
// TODO: MySQL doesn't support and not, we should remove it later.
return o.evalBitOp(ctx, args)
case opcode.Plus, opcode.Minus, opcode.Mod, opcode.Div, opcode.Mul, opcode.IntDiv:
return o.evalArithmeticOp(ctx, args)
default:
panic("should never happen")
}
}
func (o *BinaryOperation) get2(ctx context.Context, args map[interface{}]interface{}) (x, y interface{}, err error) {
x, err = o.L.Eval(ctx, args)
if err != nil {
err = o.traceErr(err)
return
}
y, err = o.R.Eval(ctx, args)
if err != nil {
err = o.traceErr(err)
return
}
x, y = types.Coerce(x, y)
return x, y, nil
}
func (o *BinaryOperation) evalAndAnd(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, err := o.L.Eval(ctx, args)
if err != nil {
return nil, o.traceErr(err)
}
if a != nil {
var x int8
x, err = types.ToBool(a)
if err != nil {
return nil, o.traceErr(err)
} else if x == 0 {
// false && any other types is false
return x, nil
}
}
b, err := o.R.Eval(ctx, args)
if err != nil {
return nil, o.traceErr(err)
}
if b != nil {
var y int8
y, err = types.ToBool(b)
if err != nil {
return nil, o.traceErr(err)
} else if y == 0 {
return y, nil
}
}
// here x and y are all not false
// if a or b is nil
if a == nil || b == nil {
return nil, nil
}
return int8(1), nil
}
func (o *BinaryOperation) evalOrOr(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, err := o.L.Eval(ctx, args)
if err != nil {
return nil, o.traceErr(err)
}
var (
x int8
y int8
)
if a != nil {
x, err = types.ToBool(a)
if err != nil {
return nil, o.traceErr(err)
} else if x == 1 {
// true || any other types is true
return x, nil
}
}
b, err := o.R.Eval(ctx, args)
if err != nil {
return nil, o.traceErr(err)
}
if b != nil {
y, err = types.ToBool(b)
if err != nil {
return nil, o.traceErr(err)
} else if y == 1 {
return y, nil
}
}
// here x and y are all not true
// if a or b is nil
if a == nil || b == nil {
return nil, nil
}
return int8(0), nil
}
func (o *BinaryOperation) evalLogicXor(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, err := o.L.Eval(ctx, args)
if err != nil || a == nil {
return nil, o.traceErr(err)
}
x, err := types.ToBool(a)
if err != nil {
return nil, o.traceErr(err)
}
b, err := o.R.Eval(ctx, args)
if err != nil || b == nil {
return nil, o.traceErr(err)
}
y, err := types.ToBool(b)
if err != nil {
return nil, o.traceErr(err)
}
if x == y {
return int8(0), nil
}
return int8(1), nil
}
func (o *BinaryOperation) errorf(format string, args ...interface{}) error {
err := errors.Errorf(format, args...)
return o.traceErr(err)
}
// Operator: &, ~, |, ^, <<, >>
// See https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html
func (o *BinaryOperation) evalBitOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
return nil, nil
}
x, err := types.ToInt64(a)
if err != nil {
return nil, o.traceErr(err)
}
y, err := types.ToInt64(b)
if err != nil {
return nil, o.traceErr(err)
}
// use a int64 for bit operator, return uint64
switch o.Op {
case opcode.And:
return uint64(x & y), nil
case opcode.Or:
return uint64(x | y), nil
case opcode.Xor:
return uint64(x ^ y), nil
case opcode.RightShift:
return uint64(x) >> uint64(y), nil
case opcode.LeftShift:
return uint64(x) << uint64(y), nil
default:
return nil, o.errorf("invalid op %v in bit operation", o.Op)
}
}
// Operator: &&, ||, logic XOR
// See https://dev.mysql.com/doc/refman/5.7/en/logical-operators.html
func (o *BinaryOperation) evalLogicOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
switch o.Op {
case opcode.AndAnd:
return o.evalAndAnd(ctx, args)
case opcode.OrOr:
return o.evalOrOr(ctx, args)
case opcode.LogicXor:
return o.evalLogicXor(ctx, args)
default:
return nil, o.errorf("invalid op %v in logic operation", o.Op)
}
}
func compareFloatString(a float64, s string) (int, error) {
// MySQL will convert string to a float point value
// MySQL use a very loose conversation, e.g, 123.abc -> 123
// we should do a trade off whether supporting this feature or using a strict mode
// now we use a strict mode
b, err := types.StrToFloat(s)
if err != nil {
return 0, err
}
return types.CompareFloat64(a, b), nil
}
func compareStringFloat(s string, a float64) (int, error) {
n, err := compareFloatString(a, s)
return -n, err
}
// See https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html
func evalCompare(a interface{}, b interface{}) (int, error) {
// TODO: support compare time type with other types
switch x := a.(type) {
case float64:
switch y := b.(type) {
case float64:
return types.CompareFloat64(x, y), nil
case string:
return compareFloatString(x, y)
}
case int64:
switch y := b.(type) {
case int64:
return types.CompareInt64(x, y), nil
case uint64:
return types.CompareInteger(x, y), nil
case string:
return compareFloatString(float64(x), y)
}
case uint64:
switch y := b.(type) {
case uint64:
return types.CompareUint64(x, y), nil
case int64:
return -types.CompareInteger(y, x), nil
case string:
return compareFloatString(float64(x), y)
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
return x.Cmp(y), nil
case string:
f, err := mysql.ConvertToDecimal(y)
if err != nil {
return 0, err
}
return x.Cmp(f), nil
}
case string:
switch y := b.(type) {
case string:
return types.CompareString(x, y), nil
case int64:
return compareStringFloat(x, float64(y))
case uint64:
return compareStringFloat(x, float64(y))
case float64:
return compareStringFloat(x, y)
case mysql.Decimal:
f, err := mysql.ConvertToDecimal(x)
if err != nil {
return 0, err
}
return f.Cmp(y), nil
case mysql.Time:
n, err := y.CompareString(x)
return -n, err
case mysql.Duration:
n, err := y.CompareString(x)
return -n, err
}
case mysql.Time:
switch y := b.(type) {
case mysql.Time:
return x.Compare(y), nil
case string:
return x.CompareString(y)
}
case mysql.Duration:
switch y := b.(type) {
case mysql.Duration:
return x.Compare(y), nil
case string:
return x.CompareString(y)
}
}
return 0, errors.Errorf("invalid compare type %T cmp %T", a, b)
}
// operator: >=, >, <=, <, !=, <>, = <=>, etc.
// see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
func (o *BinaryOperation) evalComparisonOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
//TODO: support <=> later
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
// TODO: for <=>, if a and b are both nil, return true
return nil, nil
}
n, err := evalCompare(a, b)
if err != nil {
return nil, o.traceErr(err)
}
switch o.Op {
case opcode.LT:
return n < 0, nil
case opcode.LE:
return n <= 0, nil
case opcode.GE:
return n >= 0, nil
case opcode.GT:
return n > 0, nil
case opcode.EQ:
return n == 0, nil
case opcode.NE:
return n != 0, nil
default:
return nil, o.errorf("invalid op %v in comparision operation", o.Op)
}
}
func (o *BinaryOperation) evalPlus(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
return x + y, nil
case uint64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return uint64(x) + y, nil
}
case uint64:
switch y := b.(type) {
case int64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x + uint64(y), nil
case uint64:
return x + y, nil
}
case float64:
switch y := b.(type) {
case float64:
return x + y, nil
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
return x.Add(y), nil
}
}
return types.InvOp2(a, b, opcode.Plus)
}
func (o *BinaryOperation) evalMinus(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
return x - y, nil
case uint64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return uint64(x) - y, nil
}
case uint64:
switch y := b.(type) {
case int64:
// TODO: check overflow
return x - uint64(y), nil
case uint64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x - y, nil
}
case float64:
switch y := b.(type) {
case float64:
return x - y, nil
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
return x.Sub(y), nil
}
}
return types.InvOp2(a, b, opcode.Minus)
}
func (o *BinaryOperation) evalMul(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
return x * y, nil
case uint64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow and negative number
// if a negative int64 * uint64, MySQL may throw "BIGINT UNSIGNED value is out of range" error
// we skip it now and handle it later.
return uint64(x) * y, nil
}
case uint64:
switch y := b.(type) {
case int64:
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x * uint64(y), nil
case uint64:
return x * y, nil
}
case float64:
switch y := b.(type) {
case float64:
return x * y, nil
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
return x.Mul(y), nil
}
}
return types.InvOp2(a, b, opcode.Mul)
}
const precisionIncrement int32 = 4
func (o *BinaryOperation) evalDiv(a interface{}, b interface{}) (interface{}, error) {
// MySQL support integer divison Div and division operator /
// we use opcode.Div for division operator and will use another for integer division later.
// for division operator, we will use float64 for calculation.
switch x := a.(type) {
case float64:
y, err := types.ToFloat64(b)
if err != nil {
return nil, err
}
if y == 0 {
return nil, nil
}
return x / y, nil
default:
// the scale of the result is the scale of the first operand plus
// the value of the div_precision_increment system variable (which is 4 by default)
// we will use 4 here
xa, err := types.ToDecimal(a)
if err != nil {
return nil, o.traceErr(err)
}
xb, err := types.ToDecimal(b)
if err != nil {
return nil, o.traceErr(err)
}
if f, _ := xb.Float64(); f == 0 {
// division by zero return null
return nil, nil
}
return xa.Div(xb), nil
}
}
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div
func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return x / y, nil
case uint64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return uint64(x) / y, nil
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x / uint64(y), nil
case uint64:
if y == 0 {
return nil, nil
}
return x / y, nil
}
}
// if any is none integer, use decimal to calculate
x, err := types.ToDecimal(a)
if err != nil {
return nil, o.traceErr(err)
}
y, err := types.ToDecimal(b)
if err != nil {
return nil, o.traceErr(err)
}
if f, _ := y.Float64(); f == 0 {
return nil, nil
}
return x.Div(y).IntPart(), nil
}
func (o *BinaryOperation) evalMod(a interface{}, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return x % y, nil
case uint64:
if y == 0 {
return nil, nil
} else if x < 0 {
// TODO: check overflow
return -int64(uint64(-x) % y), nil
}
// TODO: check overflow
return uint64(x) % y, nil
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
} else if y < 0 {
// TODO: check overflow
return -int64(x % uint64(-y)), nil
}
// TODO: check overflow
return x % uint64(y), nil
case uint64:
if y == 0 {
return nil, nil
}
return x % y, nil
}
case float64:
switch y := b.(type) {
case float64:
if y == 0 {
return nil, nil
}
return math.Mod(x, y), nil
}
case mysql.Decimal:
switch y := b.(type) {
case mysql.Decimal:
xf, _ := x.Float64()
yf, _ := y.Float64()
if yf == 0 {
return nil, nil
}
return math.Mod(xf, yf), nil
}
}
return types.InvOp2(a, b, opcode.Mod)
}
func (o *BinaryOperation) coerceArithmetic(a interface{}) (interface{}, error) {
switch x := a.(type) {
case string:
// MySQL will convert string to float for arithmetic operation
f, err := types.StrToFloat(x)
if err != nil {
return nil, err
}
return f, err
case mysql.Time:
// TODO: if time has no precision, return int64
return x.ToNumber(), nil
case mysql.Duration:
// TODO: if duration has no precision, return int64
return x.ToNumber(), nil
default:
return x, nil
}
}
func (o *BinaryOperation) coerceArithmetic2(a interface{}, b interface{}) (x interface{}, y interface{}, err error) {
x, err = o.coerceArithmetic(a)
if err != nil {
return
}
y, err = o.coerceArithmetic(b)
if err != nil {
return
}
x, y = types.Coerce(x, y)
return x, y, nil
}
// Operator: DIV / - % MOD + *
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_divide
func (o *BinaryOperation) evalArithmeticOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
// TODO: for <=>, if a and b are both nil, return true
return nil, nil
}
if a, b, err = o.coerceArithmetic2(a, b); err != nil {
return nil, o.traceErr(err)
}
// TODO: support logic division DIV
switch o.Op {
case opcode.Plus:
return o.evalPlus(a, b)
case opcode.Minus:
return o.evalMinus(a, b)
case opcode.Mul:
return o.evalMul(a, b)
case opcode.Div:
return o.evalDiv(a, b)
case opcode.Mod:
return o.evalMod(a, b)
case opcode.IntDiv:
return o.evalIntDiv(a, b)
default:
return nil, o.errorf("invalid op %v in arithmetic operation", o.Op)
}
}