-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparison.go
executable file
·813 lines (672 loc) · 22.8 KB
/
comparison.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
// 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"
"sync"
errors "gopkg.in/src-d/go-errors.v1"
"github.com/Rock-liyi/p2pdb-store/internal/regex"
"github.com/Rock-liyi/p2pdb-store/sql"
)
var ErrInvalidRegexp = errors.NewKind("Invalid regular expression: %s")
// Comparer implements a comparison expression.
type Comparer interface {
sql.Expression
Compare(ctx *sql.Context, row sql.Row) (int, error)
Left() sql.Expression
Right() sql.Expression
}
// ErrNilOperand ir returned if some or both of the comparison's operands is nil.
var ErrNilOperand = errors.NewKind("nil operand found in comparison")
type comparison struct {
BinaryExpression
}
func newComparison(left, right sql.Expression) comparison {
return comparison{BinaryExpression{left, right}}
}
// Compare the two given values using the types of the expressions in the comparison.
// Since both types should be equal, it does not matter which type is used, but for
// reference, the left type is always used.
func (c *comparison) Compare(ctx *sql.Context, row sql.Row) (int, error) {
left, right, err := c.evalLeftAndRight(ctx, row)
if err != nil {
return 0, err
}
if left == nil || right == nil {
return 0, ErrNilOperand.New()
}
if sql.TypesEqual(c.Left().Type(), c.Right().Type()) {
return c.Left().Type().Compare(left, right)
}
// ENUM and SET must be considered when doing comparisons, as they can match arbitrary strings to numbers based on
// their elements. For other types it seems there are other considerations, therefore we only take the type for
// ENUM and SET, and default to direct literal comparisons for all other types. Eventually we will need to make our
// comparisons context-sensitive, as all comparisons should probably be based on the column/variable if present.
// Until then, this is a workaround specifically for ENUM and SET.
var compareType sql.Type
switch c.Left().(type) {
case *GetField, *UserVar, *SystemVar, *ProcedureParam:
compareType = c.Left().Type()
default:
switch c.Right().(type) {
case *GetField, *UserVar, *SystemVar, *ProcedureParam:
compareType = c.Right().Type()
}
}
if compareType != nil {
_, isEnum := compareType.(sql.EnumType)
_, isSet := compareType.(sql.SetType)
if !isEnum && !isSet {
compareType = nil
}
}
if compareType == nil {
left, right, compareType, err = c.castLeftAndRight(left, right)
if err != nil {
return 0, err
}
}
return compareType.Compare(left, right)
}
// NullSafeCompare the two given values using the types of the expressions in the comparison.
// Since both types should be equal, it does not matter which type is used, but for
// reference, the left type is always used. Unlike Compare, this sorts nil values.
func (c *comparison) NullSafeCompare(ctx *sql.Context, row sql.Row) (int, error) {
left, right, err := c.evalLeftAndRight(ctx, row)
if err != nil {
return 0, err
}
if left == nil && right == nil {
return 0, nil
} else if left == nil {
return 1, nil
} else if right == nil {
return -1, nil
}
if sql.TypesEqual(c.Left().Type(), c.Right().Type()) {
return c.Left().Type().Compare(left, right)
}
// ENUM and SET must be considered when doing comparisons, as they can match arbitrary strings to numbers based on
// their elements. For other types it seems there are other considerations, therefore we only take the type for
// ENUM and SET, and default to direct literal comparisons for all other types. Eventually we will need to make our
// comparisons context-sensitive, as all comparisons should probably be based on the column/variable if present.
// Until then, this is a workaround specifically for ENUM and SET.
var compareType sql.Type
switch c.Left().(type) {
case *GetField, *UserVar, *SystemVar, *ProcedureParam:
compareType = c.Left().Type()
default:
switch c.Right().(type) {
case *GetField, *UserVar, *SystemVar, *ProcedureParam:
compareType = c.Right().Type()
}
}
if compareType != nil {
_, isEnum := compareType.(sql.EnumType)
_, isSet := compareType.(sql.SetType)
if !isEnum && !isSet {
compareType = nil
}
}
if compareType == nil {
left, right, compareType, err = c.castLeftAndRight(left, right)
if err != nil {
return 0, err
}
}
return compareType.Compare(left, right)
}
func (c *comparison) evalLeftAndRight(ctx *sql.Context, row sql.Row) (interface{}, interface{}, error) {
left, err := c.Left().Eval(ctx, row)
if err != nil {
return nil, nil, err
}
right, err := c.Right().Eval(ctx, row)
if err != nil {
return nil, nil, err
}
return left, right, nil
}
func (c *comparison) castLeftAndRight(left, right interface{}) (interface{}, interface{}, sql.Type, error) {
leftType := c.Left().Type()
rightType := c.Right().Type()
if sql.IsTuple(leftType) && sql.IsTuple(rightType) {
return left, right, c.Left().Type(), nil
}
if sql.IsNumber(leftType) || sql.IsNumber(rightType) {
if sql.IsDecimal(leftType) || sql.IsDecimal(rightType) {
//TODO: We need to set to the actual DECIMAL type
l, r, err := convertLeftAndRight(left, right, ConvertToDecimal)
if err != nil {
return nil, nil, nil, err
}
if sql.IsDecimal(leftType) {
return l, r, leftType, nil
} else {
return l, r, rightType, nil
}
}
if sql.IsFloat(leftType) || sql.IsFloat(rightType) {
l, r, err := convertLeftAndRight(left, right, ConvertToDouble)
if err != nil {
return nil, nil, nil, err
}
return l, r, sql.Float64, nil
}
if sql.IsSigned(leftType) || sql.IsSigned(rightType) {
l, r, err := convertLeftAndRight(left, right, ConvertToSigned)
if err != nil {
return nil, nil, nil, err
}
return l, r, sql.Int64, nil
}
l, r, err := convertLeftAndRight(left, right, ConvertToUnsigned)
if err != nil {
return nil, nil, nil, err
}
return l, r, sql.Uint64, nil
}
if sql.IsTime(leftType) || sql.IsTime(rightType) {
l, r, err := convertLeftAndRight(left, right, ConvertToDatetime)
if err != nil {
return nil, nil, nil, err
}
return l, r, sql.Datetime, nil
}
left, right, err := convertLeftAndRight(left, right, ConvertToChar)
if err != nil {
return nil, nil, nil, err
}
return left, right, sql.LongText, nil
}
func convertLeftAndRight(left, right interface{}, convertTo string) (interface{}, interface{}, error) {
l, err := convertValue(left, convertTo)
if err != nil {
return nil, nil, err
}
r, err := convertValue(right, convertTo)
if err != nil {
return nil, nil, err
}
return l, r, nil
}
// Type implements the Expression interface.
func (*comparison) Type() sql.Type {
return sql.Boolean
}
// Left implements Comparer interface
func (c *comparison) Left() sql.Expression { return c.BinaryExpression.Left }
// Right implements Comparer interface
func (c *comparison) Right() sql.Expression { return c.BinaryExpression.Right }
// Equals is a comparison that checks an expression is equal to another.
type Equals struct {
comparison
}
// NewEquals returns a new Equals expression.
func NewEquals(left sql.Expression, right sql.Expression) *Equals {
return &Equals{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (e *Equals) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := e.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result == 0, nil
}
// WithChildren implements the Expression interface.
func (e *Equals) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 2)
}
return NewEquals(children[0], children[1]), nil
}
func (e *Equals) String() string {
return fmt.Sprintf("(%s = %s)", e.Left(), e.Right())
}
func (e *Equals) DebugString() string {
return fmt.Sprintf("(%s = %s)", sql.DebugString(e.Left()), sql.DebugString(e.Right()))
}
// NullSafeEquals is a comparison that checks an expression is equal to
// another, where NULLs do not coalesce to NULL and two NULLs compare equal to
// each other.
type NullSafeEquals struct {
comparison
}
// NewNullSafeEquals returns a new NullSafeEquals expression.
func NewNullSafeEquals(left sql.Expression, right sql.Expression) *NullSafeEquals {
return &NullSafeEquals{newComparison(left, right)}
}
// Type implements the Expression interface.
func (e *NullSafeEquals) Type() sql.Type {
return sql.Int8
}
func (e *NullSafeEquals) Compare(ctx *sql.Context, row sql.Row) (int, error) {
left, right, err := e.evalLeftAndRight(ctx, row)
if err != nil {
return 0, err
}
if left == nil && right == nil {
return 0, nil
} else if left == nil {
return 1, nil
} else if right == nil {
return -1, nil
}
if sql.TypesEqual(e.Left().Type(), e.Right().Type()) {
return e.Left().Type().Compare(left, right)
}
var compareType sql.Type
left, right, compareType, err = e.castLeftAndRight(left, right)
if err != nil {
return 0, err
}
return compareType.Compare(left, right)
}
// Eval implements the Expression interface.
func (e *NullSafeEquals) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := e.Compare(ctx, row)
if err != nil {
return nil, err
}
if result == 0 {
return 1, nil
}
return 0, nil
}
// WithChildren implements the Expression interface.
func (e *NullSafeEquals) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 2)
}
return NewNullSafeEquals(children[0], children[1]), nil
}
func (e *NullSafeEquals) String() string {
return fmt.Sprintf("(%s <=> %s)", e.Left(), e.Right())
}
func (e *NullSafeEquals) DebugString() string {
return fmt.Sprintf("(%s <=> %s)", sql.DebugString(e.Left()), sql.DebugString(e.Right()))
}
// Regexp is a comparison that checks an expression matches a regexp.
type Regexp struct {
comparison
pool *sync.Pool
cached bool
once sync.Once
}
// NewRegexp creates a new Regexp expression.
func NewRegexp(left sql.Expression, right sql.Expression) *Regexp {
var cached = true
sql.Inspect(right, func(e sql.Expression) bool {
if _, ok := e.(*GetField); ok {
cached = false
}
return true
})
return &Regexp{
comparison: newComparison(left, right),
pool: nil,
cached: cached,
once: sync.Once{},
}
}
// Eval implements the Expression interface.
func (re *Regexp) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if sql.IsText(re.Left().Type()) && sql.IsText(re.Right().Type()) {
return re.compareRegexp(ctx, row)
}
result, err := re.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result == 0, nil
}
type matcherErrTuple struct {
matcher regex.DisposableMatcher
err error
}
func (re *Regexp) compareRegexp(ctx *sql.Context, row sql.Row) (interface{}, error) {
left, err := re.Left().Eval(ctx, row)
if err != nil || left == nil {
return nil, err
}
left, err = sql.LongText.Convert(left)
if err != nil {
return nil, err
}
var matcher regex.DisposableMatcher
if !re.cached {
right, rerr := re.evalRight(ctx, row)
if rerr != nil || right == nil {
return right, rerr
}
matcher, err = regex.NewDisposableMatcher(regex.Default(), *right)
} else {
re.once.Do(func() {
right, err := re.evalRight(ctx, row)
re.pool = &sync.Pool{
New: func() interface{} {
if err != nil || right == nil {
return matcherErrTuple{nil, err}
}
m, e := regex.NewDisposableMatcher(regex.Default(), *right)
return matcherErrTuple{m, e}
},
}
})
met := re.pool.Get().(matcherErrTuple)
matcher, err = met.matcher, met.err
}
if err != nil {
return nil, ErrInvalidRegexp.New(err.Error())
} else if matcher == nil {
return nil, nil
}
ok := matcher.Match(left.(string))
if !re.cached {
matcher.Dispose()
} else {
re.pool.Put(matcherErrTuple{matcher, nil})
}
return ok, nil
}
func (re *Regexp) evalRight(ctx *sql.Context, row sql.Row) (*string, error) {
right, err := re.Right().Eval(ctx, row)
if err != nil {
return nil, err
}
if right == nil {
return nil, nil
}
right, err = sql.LongText.Convert(right)
if err != nil {
return nil, err
}
s := right.(string)
return &s, nil
}
// WithChildren implements the Expression interface.
func (re *Regexp) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(re, len(children), 2)
}
return NewRegexp(children[0], children[1]), nil
}
func (re *Regexp) String() string {
return fmt.Sprintf("(%s REGEXP %s)", re.Left(), re.Right())
}
func (re *Regexp) DebugString() string {
return fmt.Sprintf("(%s REGEXP %s)", sql.DebugString(re.Left()), sql.DebugString(re.Right()))
}
// GreaterThan is a comparison that checks an expression is greater than another.
type GreaterThan struct {
comparison
}
// NewGreaterThan creates a new GreaterThan expression.
func NewGreaterThan(left sql.Expression, right sql.Expression) *GreaterThan {
return &GreaterThan{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (gt *GreaterThan) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := gt.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result == 1, nil
}
// WithChildren implements the Expression interface.
func (gt *GreaterThan) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(gt, len(children), 2)
}
return NewGreaterThan(children[0], children[1]), nil
}
func (gt *GreaterThan) String() string {
return fmt.Sprintf("(%s > %s)", gt.Left(), gt.Right())
}
func (gt *GreaterThan) DebugString() string {
return fmt.Sprintf("(%s > %s)", sql.DebugString(gt.Left()), sql.DebugString(gt.Right()))
}
// NullSafeGreaterThan is a comparison that checks an expression is greater than another. This is not directly
// accessible through the SQL language.
type NullSafeGreaterThan struct {
comparison
}
// NewNullSafeGreaterThan creates a new NullSafeGreaterThan expression.
func NewNullSafeGreaterThan(left sql.Expression, right sql.Expression) *NullSafeGreaterThan {
return &NullSafeGreaterThan{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (gt *NullSafeGreaterThan) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := gt.NullSafeCompare(ctx, row)
if err != nil {
return nil, err
}
return result == 1, nil
}
// WithChildren implements the Expression interface.
func (gt *NullSafeGreaterThan) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(gt, len(children), 2)
}
return NewNullSafeGreaterThan(children[0], children[1]), nil
}
func (gt *NullSafeGreaterThan) String() string {
return fmt.Sprintf("(%s > %s)", gt.Left(), gt.Right())
}
func (gt *NullSafeGreaterThan) DebugString() string {
return fmt.Sprintf("(%s > %s)", sql.DebugString(gt.Left()), sql.DebugString(gt.Right()))
}
// LessThan is a comparison that checks an expression is less than another.
type LessThan struct {
comparison
}
// NewLessThan creates a new LessThan expression.
func NewLessThan(left sql.Expression, right sql.Expression) *LessThan {
return &LessThan{newComparison(left, right)}
}
// Eval implements the expression interface.
func (lt *LessThan) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := lt.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result == -1, nil
}
// WithChildren implements the Expression interface.
func (lt *LessThan) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(lt, len(children), 2)
}
return NewLessThan(children[0], children[1]), nil
}
func (lt *LessThan) String() string {
return fmt.Sprintf("(%s < %s)", lt.Left(), lt.Right())
}
func (lt *LessThan) DebugString() string {
return fmt.Sprintf("(%s < %s)", sql.DebugString(lt.Left()), sql.DebugString(lt.Right()))
}
// NullSafeLessThan is a comparison that checks an expression is less than another. This is not directly accessible
// through the SQL language.
type NullSafeLessThan struct {
comparison
}
// NewNullSafeLessThan creates a new NullSafeLessThan expression.
func NewNullSafeLessThan(left sql.Expression, right sql.Expression) *NullSafeLessThan {
return &NullSafeLessThan{newComparison(left, right)}
}
// Eval implements the expression interface.
func (lt *NullSafeLessThan) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := lt.NullSafeCompare(ctx, row)
if err != nil {
return nil, err
}
return result == -1, nil
}
// WithChildren implements the Expression interface.
func (lt *NullSafeLessThan) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(lt, len(children), 2)
}
return NewNullSafeLessThan(children[0], children[1]), nil
}
func (lt *NullSafeLessThan) String() string {
return fmt.Sprintf("(%s < %s)", lt.Left(), lt.Right())
}
func (lt *NullSafeLessThan) DebugString() string {
return fmt.Sprintf("(%s < %s)", sql.DebugString(lt.Left()), sql.DebugString(lt.Right()))
}
// GreaterThanOrEqual is a comparison that checks an expression is greater or equal to
// another.
type GreaterThanOrEqual struct {
comparison
}
// NewGreaterThanOrEqual creates a new GreaterThanOrEqual
func NewGreaterThanOrEqual(left sql.Expression, right sql.Expression) *GreaterThanOrEqual {
return &GreaterThanOrEqual{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (gte *GreaterThanOrEqual) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := gte.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result > -1, nil
}
// WithChildren implements the Expression interface.
func (gte *GreaterThanOrEqual) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(gte, len(children), 2)
}
return NewGreaterThanOrEqual(children[0], children[1]), nil
}
func (gte *GreaterThanOrEqual) String() string {
return fmt.Sprintf("(%s >= %s)", gte.Left(), gte.Right())
}
func (gte *GreaterThanOrEqual) DebugString() string {
return fmt.Sprintf("(%s >= %s)", sql.DebugString(gte.Left()), sql.DebugString(gte.Right()))
}
// NullSafeGreaterThanOrEqual is a comparison that checks an expression is greater or equal to
// another. This is not directly accessible through the SQL language.
type NullSafeGreaterThanOrEqual struct {
comparison
}
// NewNullSafeGreaterThanOrEqual creates a new NullSafeGreaterThanOrEqual
func NewNullSafeGreaterThanOrEqual(left sql.Expression, right sql.Expression) *NullSafeGreaterThanOrEqual {
return &NullSafeGreaterThanOrEqual{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (gte *NullSafeGreaterThanOrEqual) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := gte.NullSafeCompare(ctx, row)
if err != nil {
return nil, err
}
return result > -1, nil
}
// WithChildren implements the Expression interface.
func (gte *NullSafeGreaterThanOrEqual) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(gte, len(children), 2)
}
return NewNullSafeGreaterThanOrEqual(children[0], children[1]), nil
}
func (gte *NullSafeGreaterThanOrEqual) String() string {
return fmt.Sprintf("(%s >= %s)", gte.Left(), gte.Right())
}
func (gte *NullSafeGreaterThanOrEqual) DebugString() string {
return fmt.Sprintf("(%s >= %s)", sql.DebugString(gte.Left()), sql.DebugString(gte.Right()))
}
// LessThanOrEqual is a comparison that checks an expression is equal or lower than
// another.
type LessThanOrEqual struct {
comparison
}
// NewLessThanOrEqual creates a LessThanOrEqual expression.
func NewLessThanOrEqual(left sql.Expression, right sql.Expression) *LessThanOrEqual {
return &LessThanOrEqual{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (lte *LessThanOrEqual) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := lte.Compare(ctx, row)
if err != nil {
if ErrNilOperand.Is(err) {
return nil, nil
}
return nil, err
}
return result < 1, nil
}
// WithChildren implements the Expression interface.
func (lte *LessThanOrEqual) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(lte, len(children), 2)
}
return NewLessThanOrEqual(children[0], children[1]), nil
}
func (lte *LessThanOrEqual) String() string {
return fmt.Sprintf("(%s <= %s)", lte.Left(), lte.Right())
}
func (lte *LessThanOrEqual) DebugString() string {
return fmt.Sprintf("(%s <= %s)", sql.DebugString(lte.Left()), sql.DebugString(lte.Right()))
}
// NullSafeLessThanOrEqual is a comparison that checks an expression is equal or lower than
// another. This is not directly accessible through the SQL language.
type NullSafeLessThanOrEqual struct {
comparison
}
// NewNullSafeLessThanOrEqual creates a NullSafeLessThanOrEqual expression.
func NewNullSafeLessThanOrEqual(left sql.Expression, right sql.Expression) *NullSafeLessThanOrEqual {
return &NullSafeLessThanOrEqual{newComparison(left, right)}
}
// Eval implements the Expression interface.
func (lte *NullSafeLessThanOrEqual) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
result, err := lte.NullSafeCompare(ctx, row)
if err != nil {
return nil, err
}
return result < 1, nil
}
// WithChildren implements the Expression interface.
func (lte *NullSafeLessThanOrEqual) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(lte, len(children), 2)
}
return NewNullSafeLessThanOrEqual(children[0], children[1]), nil
}
func (lte *NullSafeLessThanOrEqual) String() string {
return fmt.Sprintf("(%s <= %s)", lte.Left(), lte.Right())
}
func (lte *NullSafeLessThanOrEqual) DebugString() string {
return fmt.Sprintf("(%s <= %s)", sql.DebugString(lte.Left()), sql.DebugString(lte.Right()))
}
var (
// ErrUnsupportedInOperand is returned when there is an invalid righthand
// operand in an IN operator.
ErrUnsupportedInOperand = errors.NewKind("right operand in IN operation must be tuple, but is %T")
)