-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.go
executable file
·602 lines (509 loc) · 12.9 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
// Copyright 2020-2021 Dolthub, Inc.
//
// 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 expression
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/dolthub/vitess/go/vt/sqlparser"
errors "gopkg.in/src-d/go-errors.v1"
"github.com/Rock-liyi/p2pdb-store/sql"
)
var (
// errUnableToCast means that we could not find common type for two arithemtic objects
errUnableToCast = errors.NewKind("Unable to cast between types: %T, %T")
// errUnableToEval means that we could not evaluate an expression
errUnableToEval = errors.NewKind("Unable to evaluate an expression: %v %s %v")
)
// Arithmetic expressions (+, -, *, /, ...)
type Arithmetic struct {
BinaryExpression
Op string
}
// NewArithmetic creates a new Arithmetic sql.Expression.
func NewArithmetic(left, right sql.Expression, op string) *Arithmetic {
return &Arithmetic{BinaryExpression{Left: left, Right: right}, op}
}
// NewPlus creates a new Arithmetic + sql.Expression.
func NewPlus(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.PlusStr)
}
func NewIncrement(left sql.Expression) *Arithmetic {
one := NewLiteral(sql.NumericUnaryValue(left.Type()), left.Type())
return NewArithmetic(left, one, sqlparser.PlusStr)
}
// NewMinus creates a new Arithmetic - sql.Expression.
func NewMinus(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.MinusStr)
}
// NewMult creates a new Arithmetic * sql.Expression.
func NewMult(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.MultStr)
}
// NewDiv creates a new Arithmetic / sql.Expression.
func NewDiv(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.DivStr)
}
// NewShiftLeft creates a new Arithmetic << sql.Expression.
func NewShiftLeft(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.ShiftLeftStr)
}
// NewShiftRight creates a new Arithmetic >> sql.Expression.
func NewShiftRight(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.ShiftRightStr)
}
// NewBitAnd creates a new Arithmetic & sql.Expression.
func NewBitAnd(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.BitAndStr)
}
// NewBitOr creates a new Arithmetic | sql.Expression.
func NewBitOr(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.BitOrStr)
}
// NewBitXor creates a new Arithmetic ^ sql.Expression.
func NewBitXor(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.BitXorStr)
}
// NewIntDiv creates a new Arithmetic div sql.Expression.
func NewIntDiv(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.IntDivStr)
}
// NewMod creates a new Arithmetic % sql.Expression.
func NewMod(left, right sql.Expression) *Arithmetic {
return NewArithmetic(left, right, sqlparser.ModStr)
}
func (a *Arithmetic) String() string {
return fmt.Sprintf("(%s %s %s)", a.Left, a.Op, a.Right)
}
func (a *Arithmetic) DebugString() string {
return fmt.Sprintf("(%s %s %s)", sql.DebugString(a.Left), a.Op, sql.DebugString(a.Right))
}
// IsNullable implements the sql.Expression interface.
func (a *Arithmetic) IsNullable() bool {
if a.Type() == sql.Timestamp || a.Type() == sql.Datetime {
return true
}
return a.BinaryExpression.IsNullable()
}
// Type returns the greatest type for given operation.
func (a *Arithmetic) Type() sql.Type {
switch strings.ToLower(a.Op) {
case sqlparser.PlusStr, sqlparser.MinusStr, sqlparser.MultStr, sqlparser.DivStr:
if isInterval(a.Left) || isInterval(a.Right) {
return sql.Datetime
}
if sql.IsTime(a.Left.Type()) && sql.IsTime(a.Right.Type()) {
return sql.Int64
}
if sql.IsInteger(a.Left.Type()) && sql.IsInteger(a.Right.Type()) {
if sql.IsUnsigned(a.Left.Type()) && sql.IsUnsigned(a.Right.Type()) {
return sql.Uint64
}
return sql.Int64
}
return sql.Float64
case sqlparser.ShiftLeftStr, sqlparser.ShiftRightStr:
return sql.Uint64
case sqlparser.BitAndStr, sqlparser.BitOrStr, sqlparser.BitXorStr, sqlparser.IntDivStr, sqlparser.ModStr:
if sql.IsUnsigned(a.Left.Type()) && sql.IsUnsigned(a.Right.Type()) {
return sql.Uint64
}
return sql.Int64
}
return sql.Float64
}
func isInterval(expr sql.Expression) bool {
_, ok := expr.(*Interval)
return ok
}
// WithChildren implements the Expression interface.
func (a *Arithmetic) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 2)
}
return NewArithmetic(children[0], children[1], a.Op), nil
}
// Eval implements the Expression interface.
func (a *Arithmetic) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
lval, rval, err := a.evalLeftRight(ctx, row)
if err != nil {
return nil, err
}
if lval == nil || rval == nil {
return nil, nil
}
lval, rval, err = a.convertLeftRight(lval, rval)
if err != nil {
return nil, err
}
switch strings.ToLower(a.Op) {
case sqlparser.PlusStr:
return plus(lval, rval)
case sqlparser.MinusStr:
return minus(lval, rval)
case sqlparser.MultStr:
return mult(lval, rval)
case sqlparser.DivStr:
return div(lval, rval)
case sqlparser.BitAndStr:
return bitAnd(lval, rval)
case sqlparser.BitOrStr:
return bitOr(lval, rval)
case sqlparser.BitXorStr:
return bitXor(lval, rval)
case sqlparser.ShiftLeftStr:
return shiftLeft(lval, rval)
case sqlparser.ShiftRightStr:
return shiftRight(lval, rval)
case sqlparser.IntDivStr:
return intDiv(lval, rval)
case sqlparser.ModStr:
return mod(lval, rval)
}
return nil, errUnableToEval.New(lval, a.Op, rval)
}
func (a *Arithmetic) evalLeftRight(ctx *sql.Context, row sql.Row) (interface{}, interface{}, error) {
var lval, rval interface{}
var err error
if i, ok := a.Left.(*Interval); ok {
lval, err = i.EvalDelta(ctx, row)
if err != nil {
return nil, nil, err
}
} else {
lval, err = a.Left.Eval(ctx, row)
if err != nil {
return nil, nil, err
}
}
if i, ok := a.Right.(*Interval); ok {
rval, err = i.EvalDelta(ctx, row)
if err != nil {
return nil, nil, err
}
} else {
rval, err = a.Right.Eval(ctx, row)
if err != nil {
return nil, nil, err
}
}
return lval, rval, nil
}
func (a *Arithmetic) convertLeftRight(left interface{}, right interface{}) (interface{}, interface{}, error) {
var err error
typ := a.Type()
if i, ok := left.(*TimeDelta); ok {
left = i
} else {
left, err = typ.Convert(left)
if err != nil {
return nil, nil, err
}
}
if i, ok := right.(*TimeDelta); ok {
right = i
} else {
right, err = typ.Convert(right)
if err != nil {
return nil, nil, err
}
}
return left, right, nil
}
func plus(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l + r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l + r, nil
}
case float64:
switch r := rval.(type) {
case float64:
return l + r, nil
}
case time.Time:
switch r := rval.(type) {
case *TimeDelta:
return sql.ValidateTime(r.Add(l)), nil
case time.Time:
return l.Unix() + r.Unix(), nil
}
case *TimeDelta:
switch r := rval.(type) {
case time.Time:
return sql.ValidateTime(l.Add(r)), nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func minus(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l - r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l - r, nil
}
case float64:
switch r := rval.(type) {
case float64:
return l - r, nil
}
case time.Time:
switch r := rval.(type) {
case *TimeDelta:
return sql.ValidateTime(r.Sub(l)), nil
case time.Time:
return l.Unix() - r.Unix(), nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func mult(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l * r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l * r, nil
}
case float64:
switch r := rval.(type) {
case float64:
return l * r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func div(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
if r == 0 {
return sql.Null, nil
}
return l / r, nil
}
case int64:
switch r := rval.(type) {
case int64:
if r == 0 {
return sql.Null, nil
}
return l / r, nil
}
case float64:
switch r := rval.(type) {
case float64:
if r == 0 {
return sql.Null, nil
}
return l / r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func bitAnd(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l & r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l & r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func bitOr(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l | r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l | r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func bitXor(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l ^ r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l ^ r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func shiftLeft(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l << r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func shiftRight(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l >> r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func intDiv(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
if r == 0 {
return sql.Null, nil
}
return uint64(l / r), nil
}
case int64:
switch r := rval.(type) {
case int64:
if r == 0 {
return sql.Null, nil
}
return int64(l / r), nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
func mod(lval, rval interface{}) (interface{}, error) {
switch l := lval.(type) {
case uint64:
switch r := rval.(type) {
case uint64:
return l % r, nil
}
case int64:
switch r := rval.(type) {
case int64:
return l % r, nil
}
}
return nil, errUnableToCast.New(lval, rval)
}
// UnaryMinus is an unary minus operator.
type UnaryMinus struct {
UnaryExpression
}
// NewUnaryMinus creates a new UnaryMinus expression node.
func NewUnaryMinus(child sql.Expression) *UnaryMinus {
return &UnaryMinus{UnaryExpression{Child: child}}
}
// Eval implements the sql.Expression interface.
func (e *UnaryMinus) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
child, err := e.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if child == nil {
return nil, nil
}
if !sql.IsNumber(e.Child.Type()) {
child, err = sql.Float64.Convert(child)
if err != nil {
child = 0.0
}
}
switch n := child.(type) {
case float64:
return -n, nil
case float32:
return -n, nil
case int:
return -n, nil
case int8:
return -n, nil
case int16:
return -n, nil
case int32:
return -n, nil
case int64:
return -n, nil
case uint:
return -int(n), nil
case uint8:
return -int8(n), nil
case uint16:
return -int16(n), nil
case uint32:
return -int32(n), nil
case uint64:
return -int64(n), nil
default:
return nil, sql.ErrInvalidType.New(reflect.TypeOf(n))
}
}
// Type implements the sql.Expression interface.
func (e *UnaryMinus) Type() sql.Type {
typ := e.Child.Type()
if !sql.IsNumber(typ) {
return sql.Float64
}
if typ == sql.Uint32 {
return sql.Int32
}
if typ == sql.Uint64 {
return sql.Int64
}
return e.Child.Type()
}
func (e *UnaryMinus) String() string {
return fmt.Sprintf("-%s", e.Child)
}
// WithChildren implements the Expression interface.
func (e *UnaryMinus) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
}
return NewUnaryMinus(children[0]), nil
}