forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
1674 lines (1581 loc) · 41.2 KB
/
analyze.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Cockroach 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 sql
import (
"fmt"
"reflect"
"regexp"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
)
// analyzeExpr analyzes and simplifies an expression, returning a list of
// expressions of a list of expressions. The top-level list contains
// disjunctions (a.k.a OR expressions). The second-level contains conjunctions
// (a.k.a. AND expressions). For example:
//
// (a AND b) OR c -> [[a, b], c]
//
// Expression analysis should only be performed on the WHERE clause of SELECT
// statements. The returned expressions are not guaranteed to be semantically
// identical to the original. In particular, expressions that return NULL might
// be transformed into expressions that return false. This is ok in the context
// of WHERE clauses where we care about not-true for filtering
// purposes. Additionally, expressions that analysis does not handle will be
// transformed into true. The caller is required to use the original expression
// (which will be unchanged by analyzeExpr) for filtering.
//
// Returns false for equivalent if the resulting expressions are not equivalent
// to the originals. This occurs for expressions which are currently not
// handled by simplification (they are replaced by "true").
func analyzeExpr(
evalCtx *parser.EvalContext, e parser.TypedExpr,
) (exprs []parser.TypedExprs, equivalent bool) {
e, equivalent = simplifyExpr(evalCtx, e)
orExprs := splitOrExpr(evalCtx, e, nil)
results := make([]parser.TypedExprs, len(orExprs))
for i := range orExprs {
results[i] = splitAndExpr(evalCtx, orExprs[i], nil)
}
return results, equivalent
}
// splitOrExpr flattens a tree of OR expressions returning all of the child
// expressions as a list. Any non-OR expression is returned as a single element
// in the list.
//
// a OR b OR c OR d -> [a, b, c, d]
func splitOrExpr(
evalCtx *parser.EvalContext, e parser.TypedExpr, exprs parser.TypedExprs,
) parser.TypedExprs {
switch t := e.(type) {
case *parser.OrExpr:
return splitOrExpr(evalCtx, t.TypedRight(), splitOrExpr(evalCtx, t.TypedLeft(), exprs))
}
return append(exprs, e)
}
// splitAndExpr flattens a tree of AND expressions returning all of the child
// expressions as a list. Any non-AND expression is returned as a single
// element in the list.
//
// a AND b AND c AND d -> [a, b, c, d]
func splitAndExpr(
evalCtx *parser.EvalContext, e parser.TypedExpr, exprs parser.TypedExprs,
) parser.TypedExprs {
switch t := e.(type) {
case *parser.AndExpr:
return splitAndExpr(evalCtx, t.TypedRight(), splitAndExpr(evalCtx, t.TypedLeft(), exprs))
}
return append(exprs, e)
}
// joinOrExprs performs the inverse operation of splitOrExpr, joining
// together the individual expressions using OrExpr nodes.
func joinOrExprs(exprs parser.TypedExprs) parser.TypedExpr {
return joinExprs(exprs, func(left, right parser.TypedExpr) parser.TypedExpr {
return parser.NewTypedOrExpr(left, right)
})
}
// joinAndExprs performs the inverse operation of splitAndExpr, joining
// together the individual expressions using AndExpr nodes.
func joinAndExprs(exprs parser.TypedExprs) parser.TypedExpr {
return joinExprs(exprs, func(left, right parser.TypedExpr) parser.TypedExpr {
return parser.NewTypedAndExpr(left, right)
})
}
func joinExprs(
exprs parser.TypedExprs, joinExprsFn func(left, right parser.TypedExpr) parser.TypedExpr,
) parser.TypedExpr {
switch len(exprs) {
case 0:
return nil
case 1:
return exprs[0]
default:
a := joinExprsFn(exprs[len(exprs)-2], exprs[len(exprs)-1])
for i := len(exprs) - 3; i >= 0; i-- {
a = joinExprsFn(exprs[i], a)
}
return a
}
}
// simplifyExpr transforms an expression such that it contains only expressions
// involving IndexedVars that can be used for index selection. If an expression is
// encountered that cannot be used for index selection (e.g. "func(val)") that
// part of the expression tree is considered to evaluate to true, possibly
// rendering the entire expression as true. Additionally, various
// normalizations are performed on comparison expressions. For example:
//
// (a < 1 AND a < 2) -> (a < 1)
// (a < 1 AND a > 2) -> false
// (a > 1 OR a < 2) -> true
// (a > 1 OR func(b)) -> true
//
// Note that simplification is not normalization. Normalization as performed by
// parser.NormalizeExpr returns an expression that is equivalent to the
// original. Simplification can return an expression with parts of the
// expression tree stripped out.
//
// Returns false for equivalent if the resulting expression is not equivalent
// to the original. This occurs for expressions which are currently not handled
// by simplification.
func simplifyExpr(
evalCtx *parser.EvalContext, e parser.TypedExpr,
) (simplified parser.TypedExpr, equivalent bool) {
if e == parser.DNull {
return e, true
}
switch t := e.(type) {
case *parser.NotExpr:
return simplifyNotExpr(evalCtx, t)
case *parser.AndExpr:
return simplifyAndExpr(evalCtx, t)
case *parser.OrExpr:
return simplifyOrExpr(evalCtx, t)
case *parser.ComparisonExpr:
return simplifyComparisonExpr(evalCtx, t)
case *parser.IndexedVar, *parser.DBool:
return e, true
}
// We don't know how to simplify expressions that fall through to here, so
// consider this part of the expression true.
return parser.MakeDBool(true), false
}
func simplifyNotExpr(evalCtx *parser.EvalContext, n *parser.NotExpr) (parser.TypedExpr, bool) {
if n.Expr == parser.DNull {
return parser.DNull, true
}
switch t := n.Expr.(type) {
case *parser.ComparisonExpr:
op := t.Operator
switch op {
case parser.EQ:
op = parser.NE
case parser.NE:
op = parser.EQ
case parser.GT:
op = parser.LE
case parser.GE:
op = parser.LT
case parser.LT:
op = parser.GE
case parser.LE:
op = parser.GT
case parser.In:
op = parser.NotIn
case parser.NotIn:
op = parser.In
case parser.Like:
op = parser.NotLike
case parser.NotLike:
op = parser.Like
case parser.ILike:
op = parser.NotILike
case parser.NotILike:
op = parser.ILike
case parser.SimilarTo:
op = parser.NotSimilarTo
case parser.NotSimilarTo:
op = parser.SimilarTo
case parser.RegMatch:
op = parser.NotRegMatch
case parser.RegIMatch:
op = parser.NotRegIMatch
default:
return parser.MakeDBool(true), false
}
return simplifyExpr(evalCtx, parser.NewTypedComparisonExpr(
op,
t.TypedLeft(),
t.TypedRight(),
))
case *parser.AndExpr:
// De Morgan's Law: NOT (a AND b) -> (NOT a) OR (NOT b)
return simplifyExpr(evalCtx, parser.NewTypedOrExpr(
parser.NewTypedNotExpr(t.TypedLeft()),
parser.NewTypedNotExpr(t.TypedRight()),
))
case *parser.OrExpr:
// De Morgan's Law: NOT (a OR b) -> (NOT a) AND (NOT b)
return simplifyExpr(evalCtx, parser.NewTypedAndExpr(
parser.NewTypedNotExpr(t.TypedLeft()),
parser.NewTypedNotExpr(t.TypedRight()),
))
}
return parser.MakeDBool(true), false
}
func isKnownTrue(e parser.TypedExpr) bool {
if e == parser.DNull {
return false
}
if b, ok := e.(*parser.DBool); ok {
return bool(*b)
}
return false
}
func isKnownFalseOrNull(e parser.TypedExpr) bool {
if e == parser.DNull {
return true
}
if b, ok := e.(*parser.DBool); ok {
return !bool(*b)
}
return false
}
func simplifyAndExpr(evalCtx *parser.EvalContext, n *parser.AndExpr) (parser.TypedExpr, bool) {
// a AND b AND c AND d -> [a, b, c, d]
equivalent := true
exprs := splitAndExpr(evalCtx, n, nil)
for i := range exprs {
var equiv bool
exprs[i], equiv = simplifyExpr(evalCtx, exprs[i])
if !equiv {
equivalent = false
}
if isKnownFalseOrNull(exprs[i]) {
return parser.MakeDBool(false), equivalent
}
}
// Simplifying exprs might have transformed one of the elements into an AND
// expression.
texprs, exprs := exprs, nil
for _, e := range texprs {
exprs = splitAndExpr(evalCtx, e, exprs)
}
// Loop over the expressions looking for simplifications.
//
// TODO(pmattis): This is O(n^2) in the number of expressions. Could be
// optimized by sorting the expressions based on the variables they contain.
outer:
for i := len(exprs) - 1; i >= 0; i-- {
for j := i - 1; j >= 0; j-- {
var equiv bool
exprs[j], exprs[i], equiv = simplifyOneAndExpr(evalCtx, exprs[j], exprs[i])
if !equiv {
equivalent = false
}
if isKnownFalseOrNull(exprs[j]) {
return exprs[j], equivalent
}
if isKnownTrue(exprs[i]) {
exprs[i] = nil
}
if exprs[i] == nil {
// We found a simplification. Strip off the expression that is now nil
// and continue the outer loop.
n := len(exprs) - 1
exprs[i] = exprs[n]
exprs = exprs[:n]
continue outer
}
}
}
// Reform the AND expressions.
return joinAndExprs(exprs), equivalent
}
func simplifyOneAndExpr(
evalCtx *parser.EvalContext, left, right parser.TypedExpr,
) (parser.TypedExpr, parser.TypedExpr, bool) {
lcmp, ok := left.(*parser.ComparisonExpr)
if !ok {
return left, right, true
}
rcmp, ok := right.(*parser.ComparisonExpr)
if !ok {
return left, right, true
}
lcmpLeft, lcmpRight := lcmp.TypedLeft(), lcmp.TypedRight()
rcmpLeft, rcmpRight := rcmp.TypedLeft(), rcmp.TypedRight()
if !isDatum(lcmpRight) || !isDatum(rcmpRight) {
return parser.MakeDBool(true), nil, false
}
if !varEqual(lcmpLeft, rcmpLeft) {
return left, right, true
}
if lcmp.Operator == parser.IsNot || rcmp.Operator == parser.IsNot {
switch lcmp.Operator {
case parser.EQ, parser.GT, parser.GE, parser.LT, parser.LE, parser.In:
if rcmpRight == parser.DNull {
// a <cmp> x AND a IS NOT NULL
return left, nil, true
}
case parser.Is:
if lcmpRight == parser.DNull && rcmpRight == parser.DNull {
// a IS NULL AND a IS NOT NULL
return parser.MakeDBool(false), nil, true
}
case parser.IsNot:
if lcmpRight == parser.DNull {
switch rcmp.Operator {
case parser.EQ, parser.GT, parser.GE, parser.LT, parser.LE, parser.In:
// a IS NOT NULL AND a <cmp> x
return right, nil, true
case parser.Is:
if rcmpRight == parser.DNull {
// a IS NOT NULL AND a IS NULL
return parser.MakeDBool(false), nil, true
}
case parser.IsNot:
if rcmpRight == parser.DNull {
// a IS NOT NULL AND a IS NOT NULL
return left, nil, true
}
}
}
}
return left, right, true
}
if lcmp.Operator == parser.In || rcmp.Operator == parser.In {
left, right = simplifyOneAndInExpr(evalCtx, lcmp, rcmp)
return left, right, true
}
if reflect.TypeOf(lcmpRight) != reflect.TypeOf(rcmpRight) {
allowCmp := false
switch lcmp.Operator {
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE:
switch rcmp.Operator {
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE:
// Break, permitting heterogeneous comparison.
allowCmp = true
}
}
if !allowCmp {
if lcmp.Operator == parser.Is && lcmpRight == parser.DNull {
// a IS NULL AND a <cmp> x
return parser.MakeDBool(false), nil, true
}
if rcmp.Operator == parser.Is && rcmpRight == parser.DNull {
// a <cmp> x AND a IS NULL
return parser.MakeDBool(false), nil, true
}
// Note that "a IS NULL and a IS NULL" cannot happen here because
// "reflect.TypeOf(NULL) == reflect.TypeOf(NULL)".
return left, right, true
}
}
ldatum := lcmpRight.(parser.Datum)
rdatum := rcmpRight.(parser.Datum)
cmp := ldatum.Compare(evalCtx, rdatum)
// Determine which expression to use when either expression (left or right)
// is valid as a return value but their types are different. The reason
// to prefer a comparison between a column value and a datum of the same
// type is that it makes index constraint construction easier.
either := lcmp
if !ldatum.ResolvedType().Equivalent(rdatum.ResolvedType()) {
switch ta := lcmpLeft.(type) {
case *parser.IndexedVar:
if ta.ResolvedType().Equivalent(rdatum.ResolvedType()) {
either = rcmp
}
}
}
// TODO(pmattis): Figure out how to generate this logic.
switch lcmp.Operator {
case parser.EQ:
switch rcmp.Operator {
case parser.EQ:
// a = x AND a = y
if cmp == 0 {
// x = y
return either, nil, true
}
return parser.MakeDBool(false), nil, true
case parser.NE:
// a = x AND a != y
if cmp == 0 {
// x = y
return parser.MakeDBool(false), nil, true
}
return left, nil, true
case parser.GT, parser.GE:
// a = x AND (a > y OR a >= y)
if cmp == -1 || (cmp == 0 && rcmp.Operator == parser.GT) {
// x < y OR x = y
return parser.MakeDBool(false), nil, true
}
return left, nil, true
case parser.LT, parser.LE:
// a = x AND (a < y OR a <= y)
if cmp == 1 || (cmp == 0 && rcmp.Operator == parser.LT) {
// x > y OR x = y
return parser.MakeDBool(false), nil, true
}
return left, nil, true
}
case parser.NE:
switch rcmp.Operator {
case parser.EQ:
// a != x AND a = y
if cmp == 0 {
// x = y
return parser.MakeDBool(false), nil, true
}
return right, nil, true
case parser.NE:
// a != x AND a != y
if cmp == 0 {
// x = y
return either, nil, true
}
return left, right, true
case parser.GT:
// a != x AND a > y
return right, nil, cmp <= 0
case parser.LT:
// a != x AND a < y
return right, nil, cmp >= 0
case parser.GE:
// a != x AND a >= y
if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.GT,
rcmpLeft,
either.TypedRight(),
), nil, true
}
// x != y
return right, nil, cmp == -1
case parser.LE:
// a != x AND a <= y
if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.LT,
rcmpLeft,
either.TypedRight(),
), nil, true
}
// x != y
return right, nil, cmp == +1
}
case parser.GT:
switch rcmp.Operator {
case parser.EQ:
// a > x AND a = y
if cmp != -1 {
// x >= y
return parser.MakeDBool(false), nil, true
}
// x < y
return right, nil, true
case parser.NE:
// a > x AND a != y
return left, nil, cmp >= 0
case parser.GT, parser.GE:
// a > x AND (a > y OR a >= y)
if cmp != -1 {
// x >= y
return left, nil, true
}
// x < y
return right, nil, true
case parser.LT, parser.LE:
// a > x AND (a < y OR a <= y)
if cmp == -1 {
// x < y
return left, right, true
}
// x >= y
return parser.MakeDBool(false), nil, true
}
case parser.GE:
switch rcmp.Operator {
case parser.EQ:
// a >= x AND a = y
if cmp == 1 {
// x > y
return parser.MakeDBool(false), nil, true
}
// x <= y
return right, nil, true
case parser.NE:
// a >= x AND x != y
if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.GT,
lcmpLeft,
either.TypedRight(),
), nil, true
}
// x != y
return left, nil, cmp == +1
case parser.GT, parser.GE:
// a >= x AND (a > y OR a >= y)
if cmp == -1 || (cmp == 0 && rcmp.Operator == parser.GT) {
// x < y
return right, nil, true
}
// x >= y
return left, nil, true
case parser.LT:
// a >= x AND a < y
if cmp == -1 {
// x < y
return left, right, true
}
// x >= y
return parser.MakeDBool(false), nil, true
case parser.LE:
// a >= x AND a <= y
if cmp == -1 {
// x < y
return left, right, true
} else if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.EQ,
lcmpLeft,
either.TypedRight(),
), nil, true
}
// x > y
return parser.MakeDBool(false), nil, true
}
case parser.LT:
switch rcmp.Operator {
case parser.EQ:
// a < x AND a = y
if cmp != 1 {
// x <= y
return parser.MakeDBool(false), nil, true
}
// x > y
return right, nil, true
case parser.NE:
// a < x AND a != y
return left, nil, cmp <= 0
case parser.GT, parser.GE:
// a < x AND (a > y OR a >= y)
if cmp == 1 {
// x > y
return left, right, true
}
// x <= y
return parser.MakeDBool(false), nil, true
case parser.LT, parser.LE:
// a < x AND (a < y OR a <= y)
if cmp != 1 {
// x <= y
return left, nil, true
}
// x > y
return right, nil, true
}
case parser.LE:
switch rcmp.Operator {
case parser.EQ:
// a <= x AND a = y
if cmp == -1 {
// x < y
return parser.MakeDBool(false), nil, true
}
// x >= y
return right, nil, true
case parser.NE:
// a <= x AND a != y
if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.LT,
lcmpLeft,
either.TypedRight(),
), nil, true
}
// x != y
return left, nil, cmp == -1
case parser.GT:
// a <= x AND a > y
if cmp == 1 {
// x > y
return left, right, true
}
return parser.MakeDBool(false), nil, true
case parser.GE:
// a <= x AND a >= y
if cmp == +1 {
// x > y
return left, right, true
} else if cmp == 0 {
// x = y
return parser.NewTypedComparisonExpr(
parser.EQ,
lcmpLeft,
either.TypedRight(),
), nil, true
}
// x < y
return parser.MakeDBool(false), nil, true
case parser.LT, parser.LE:
// a <= x AND (a > y OR a >= y)
if cmp == 1 || (cmp == 0 && rcmp.Operator == parser.LT) {
// x > y
return right, nil, true
}
// x <= y
return left, nil, true
}
case parser.Is:
switch rcmp.Operator {
case parser.Is:
if lcmpRight == parser.DNull && rcmpRight == parser.DNull {
// a IS NULL AND a IS NULL
return left, nil, true
}
}
}
return parser.MakeDBool(true), nil, false
}
func simplifyOneAndInExpr(
evalCtx *parser.EvalContext, left, right *parser.ComparisonExpr,
) (parser.TypedExpr, parser.TypedExpr) {
if left.Operator != parser.In && right.Operator != parser.In {
panic(fmt.Sprintf("IN expression required: %s vs %s", left, right))
}
origLeft, origRight := left, right
switch left.Operator {
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE, parser.Is:
switch right.Operator {
case parser.In:
left, right = right, left
}
fallthrough
case parser.In:
ltuple := left.Right.(*parser.DTuple)
ltuple.AssertSorted()
values := ltuple.D
switch right.Operator {
case parser.Is:
if right.Right == parser.DNull {
return parser.MakeDBool(false), nil
}
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE:
// Our tuple will be sorted (see simplifyComparisonExpr). Binary search
// for the right datum.
datum := right.Right.(parser.Datum)
i, found := ltuple.SearchSorted(evalCtx, datum)
switch right.Operator {
case parser.EQ:
if found {
return right, nil
}
return parser.MakeDBool(false), nil
case parser.NE:
if found {
if len(values) < 2 {
return parser.MakeDBool(false), nil
}
values = remove(values, i)
}
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(values...).SetSorted(),
), nil
case parser.GT:
if i < len(values) {
if found {
values = values[i+1:]
} else {
values = values[i:]
}
if len(values) > 0 {
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(values...).SetSorted(),
), nil
}
}
return parser.MakeDBool(false), nil
case parser.GE:
if i < len(values) {
values = values[i:]
if len(values) > 0 {
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(values...).SetSorted(),
), nil
}
}
return parser.MakeDBool(false), nil
case parser.LT:
if i < len(values) {
if i == 0 {
return parser.MakeDBool(false), nil
}
values = values[:i]
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(values...).SetSorted(),
), nil
}
return left, nil
case parser.LE:
if i < len(values) {
if found {
i++
}
if i == 0 {
return parser.MakeDBool(false), nil
}
values = values[:i]
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(values...).SetSorted(),
), nil
}
return left, nil
}
case parser.In:
// Both of our tuples are sorted. Intersect the lists.
rtuple := right.Right.(*parser.DTuple)
intersection := intersectSorted(evalCtx, values, rtuple.D)
if len(intersection) == 0 {
return parser.MakeDBool(false), nil
}
return parser.NewTypedComparisonExpr(
parser.In,
left.TypedLeft(),
parser.NewDTuple(intersection...).SetSorted(),
), nil
}
}
return origLeft, origRight
}
func simplifyOrExpr(evalCtx *parser.EvalContext, n *parser.OrExpr) (parser.TypedExpr, bool) {
// a OR b OR c OR d -> [a, b, c, d]
equivalent := true
exprs := splitOrExpr(evalCtx, n, nil)
for i := range exprs {
var equiv bool
exprs[i], equiv = simplifyExpr(evalCtx, exprs[i])
if !equiv {
equivalent = false
}
if isKnownTrue(exprs[i]) {
return exprs[i], equivalent
}
}
// Simplifying exprs might have transformed one of the elements into an OR
// expression.
texprs, exprs := exprs, nil
for _, e := range texprs {
exprs = splitOrExpr(evalCtx, e, exprs)
}
// Loop over the expressions looking for simplifications.
//
// TODO(pmattis): This is O(n^2) in the number of expressions. Could be
// optimized by sorting the expressions based on the variables they contain.
outer:
for i := len(exprs) - 1; i >= 0; i-- {
for j := i - 1; j >= 0; j-- {
var equiv bool
exprs[j], exprs[i], equiv = simplifyOneOrExpr(evalCtx, exprs[j], exprs[i])
if !equiv {
equivalent = false
}
if isKnownTrue(exprs[j]) {
return exprs[j], equivalent
}
if isKnownFalseOrNull(exprs[i]) {
exprs[i] = nil
}
if exprs[i] == nil {
// We found a simplification. Strip off the expression that is now nil
// and continue the outer loop.
n := len(exprs) - 1
exprs[i] = exprs[n]
exprs = exprs[:n]
continue outer
}
}
}
// Reform the OR expressions.
return joinOrExprs(exprs), equivalent
}
func simplifyOneOrExpr(
evalCtx *parser.EvalContext, left, right parser.TypedExpr,
) (parser.TypedExpr, parser.TypedExpr, bool) {
lcmp, ok := left.(*parser.ComparisonExpr)
if !ok {
return left, right, true
}
rcmp, ok := right.(*parser.ComparisonExpr)
if !ok {
return left, right, true
}
lcmpLeft, lcmpRight := lcmp.TypedLeft(), lcmp.TypedRight()
rcmpLeft, rcmpRight := rcmp.TypedLeft(), rcmp.TypedRight()
if !isDatum(lcmpRight) || !isDatum(rcmpRight) {
return parser.MakeDBool(true), nil, false
}
if !varEqual(lcmpLeft, rcmpLeft) {
return left, right, true
}
if lcmp.Operator == parser.IsNot || rcmp.Operator == parser.IsNot {
switch lcmp.Operator {
case parser.Is:
if lcmpRight == parser.DNull && rcmpRight == parser.DNull {
// a IS NULL OR a IS NOT NULL
return parser.MakeDBool(true), nil, true
}
case parser.IsNot:
if lcmpRight == parser.DNull {
switch rcmp.Operator {
case parser.Is:
if rcmpRight == parser.DNull {
// a IS NOT NULL OR a IS NULL
return parser.MakeDBool(true), nil, true
}
case parser.IsNot:
if rcmpRight == parser.DNull {
// a IS NOT NULL OR a IS NOT NULL
return left, nil, true
}
}
}
}
return left, right, true
}
if lcmp.Operator == parser.In || rcmp.Operator == parser.In {
left, right = simplifyOneOrInExpr(evalCtx, lcmp, rcmp)
return left, right, true
}
if reflect.TypeOf(lcmpRight) != reflect.TypeOf(rcmpRight) {
allowCmp := false
switch lcmp.Operator {
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE:
switch rcmp.Operator {
case parser.EQ, parser.NE, parser.GT, parser.GE, parser.LT, parser.LE:
// Break, permitting heterogeneous comparison.
allowCmp = true
}
}
if !allowCmp {
// If the types of the left and right datums are different, no
// simplification is possible.
return left, right, true
}
}
ldatum := lcmpRight.(parser.Datum)
rdatum := rcmpRight.(parser.Datum)
cmp := ldatum.Compare(evalCtx, rdatum)
// Determine which expression to use when either expression (left or right)
// is valid as a return value but their types are different. The reason
// to prefer a comparison between a column value and a datum of the same
// type is that it makes index constraint construction easier.
either := lcmp
if !ldatum.ResolvedType().Equivalent(rdatum.ResolvedType()) {
switch ta := lcmpLeft.(type) {
case *parser.IndexedVar:
if ta.ResolvedType().Equivalent(rdatum.ResolvedType()) {
either = rcmp
}
}
}
// TODO(pmattis): Figure out how to generate this logic.
switch lcmp.Operator {
case parser.EQ:
switch rcmp.Operator {
case parser.EQ:
// a = x OR a = y
if cmp == 0 {
// x = y
return either, nil, true
} else if cmp == 1 {
// x > y
ldatum, rdatum = rdatum, ldatum
}
return parser.NewTypedComparisonExpr(
parser.In,
lcmpLeft,
parser.NewDTuple(ldatum, rdatum).SetSorted(),
), nil, true
case parser.NE:
// a = x OR a != y
if cmp == 0 {
// x = y
return makeIsNotNull(lcmpLeft), nil, true
}
return right, nil, true
case parser.GT:
// a = x OR a > y
if cmp == 1 {
// x > y OR x = y
return right, nil, true
} else if cmp == 0 {
return parser.NewTypedComparisonExpr(
parser.GE,
lcmpLeft,
either.TypedRight(),
), nil, true
}
return left, right, true
case parser.GE:
// a = x OR a >= y
if cmp != -1 {
// x >= y
return right, nil, true
}
return left, right, true
case parser.LT:
// a = x OR a < y
if cmp == -1 {
// x < y OR x = y
return right, nil, true
} else if cmp == 0 {
return parser.NewTypedComparisonExpr(