This repository was archived by the owner on Apr 24, 2025. It is now read-only.
forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredicate_rewriting.go
476 lines (416 loc) · 10.4 KB
/
predicate_rewriting.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
/*
Copyright 2022 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 sqlparser
import "slices"
// RewritePredicate walks the input AST and rewrites any boolean logic into a simpler form
// This simpler form is CNF plus logic for extracting predicates from OR, plus logic for turning ORs into IN
func RewritePredicate(ast SQLNode) SQLNode {
original := CloneSQLNode(ast)
// Beware: converting to CNF in this loop might cause exponential formula growth.
// We bail out early to prevent going overboard.
for loop := 0; loop < 15; loop++ {
exprChanged := false
stopOnChange := func(SQLNode, SQLNode) bool {
return !exprChanged
}
ast = SafeRewrite(ast, stopOnChange, func(cursor *Cursor) bool {
e, isExpr := cursor.node.(Expr)
if !isExpr {
return true
}
rewritten, changed := simplifyExpression(e)
if changed {
exprChanged = true
cursor.Replace(rewritten)
}
return !exprChanged
})
if !exprChanged {
return ast
}
}
return original
}
func simplifyExpression(expr Expr) (Expr, bool) {
switch expr := expr.(type) {
case *NotExpr:
return simplifyNot(expr)
case *OrExpr:
return simplifyOr(expr)
case *XorExpr:
return simplifyXor(expr)
case *AndExpr:
return simplifyAnd(expr)
}
return expr, false
}
func simplifyNot(expr *NotExpr) (Expr, bool) {
switch child := expr.Expr.(type) {
case *NotExpr:
return child.Expr, true
case *OrExpr:
// not(or(a,b)) => and(not(a),not(b))
return &AndExpr{Right: &NotExpr{Expr: child.Right}, Left: &NotExpr{Expr: child.Left}}, true
case *AndExpr:
// not(and(a,b)) => or(not(a), not(b))
return &OrExpr{Right: &NotExpr{Expr: child.Right}, Left: &NotExpr{Expr: child.Left}}, true
}
return expr, false
}
func simplifyOr(expr *OrExpr) (Expr, bool) {
res, rewritten := distinctOr(expr)
if rewritten {
return res, true
}
or := expr
// first we search for ANDs and see how they can be simplified
land, lok := or.Left.(*AndExpr)
rand, rok := or.Right.(*AndExpr)
if lok && rok {
// (<> AND <>) OR (<> AND <>)
// or(and(T1,T2), and(T2, T3)) => and(T1, or(T2, T2))
var a, b, c Expr
switch {
case Equals.Expr(land.Left, rand.Left):
a, b, c = land.Left, land.Right, rand.Right
return &AndExpr{Left: a, Right: &OrExpr{Left: b, Right: c}}, true
case Equals.Expr(land.Left, rand.Right):
a, b, c = land.Left, land.Right, rand.Left
return &AndExpr{Left: a, Right: &OrExpr{Left: b, Right: c}}, true
case Equals.Expr(land.Right, rand.Left):
a, b, c = land.Right, land.Left, rand.Right
return &AndExpr{Left: a, Right: &OrExpr{Left: b, Right: c}}, true
case Equals.Expr(land.Right, rand.Right):
a, b, c = land.Right, land.Left, rand.Left
return &AndExpr{Left: a, Right: &OrExpr{Left: b, Right: c}}, true
}
}
// (<> AND <>) OR <>
if lok {
// Simplification
if Equals.Expr(or.Right, land.Left) || Equals.Expr(or.Right, land.Right) {
// or(and(a,b), c) => c where c=a or c=b
return or.Right, true
}
// Distribution Law
// or(c, and(a,b)) => and(or(c,a), or(c,b))
return &AndExpr{
Left: &OrExpr{
Left: land.Left,
Right: or.Right,
},
Right: &OrExpr{
Left: land.Right,
Right: or.Right,
},
}, true
}
// <> OR (<> AND <>)
if rok {
// Simplification
if Equals.Expr(or.Left, rand.Left) || Equals.Expr(or.Left, rand.Right) {
// or(a,and(b,c)) => a
return or.Left, true
}
// Distribution Law
// or(and(a,b), c) => and(or(c,a), or(c,b))
return &AndExpr{
Left: &OrExpr{Left: or.Left, Right: rand.Left},
Right: &OrExpr{Left: or.Left, Right: rand.Right},
}, true
}
// next, we want to try to turn multiple ORs into an IN when possible
lftCmp, lok := or.Left.(*ComparisonExpr)
rgtCmp, rok := or.Right.(*ComparisonExpr)
if lok && rok {
newExpr, rewritten := tryTurningOrIntoIn(lftCmp, rgtCmp)
if rewritten {
// or(a=x,a=y) => in(a,[x,y])
return newExpr, true
}
}
// Try to make distinct
result, changed := distinctOr(expr)
if changed {
return result, true
}
return result, false
}
func simplifyXor(expr *XorExpr) (Expr, bool) {
// xor(a,b) => and(or(a,b), not(and(a,b))
return &AndExpr{
Left: &OrExpr{Left: expr.Left, Right: expr.Right},
Right: &NotExpr{Expr: &AndExpr{Left: expr.Left, Right: expr.Right}},
}, true
}
func simplifyAnd(expr *AndExpr) (Expr, bool) {
res, rewritten := distinctAnd(expr)
if rewritten {
return res, true
}
and := expr
if or, ok := and.Left.(*OrExpr); ok {
// Simplification
// and(or(a,b),c) => c when c=a or c=b
if Equals.Expr(or.Left, and.Right) {
return and.Right, true
}
if Equals.Expr(or.Right, and.Right) {
return and.Right, true
}
}
if or, ok := and.Right.(*OrExpr); ok {
// Simplification
if Equals.Expr(or.Left, and.Left) {
return and.Left, true
}
if Equals.Expr(or.Right, and.Left) {
return and.Left, true
}
}
return expr, false
}
// ExtractINFromOR rewrites the OR expression into an IN clause.
// Each side of each ORs has to be an equality comparison expression and the column names have to
// match for all sides of each comparison.
// This rewriter takes a query that looks like this WHERE a = 1 and b = 11 or a = 2 and b = 12 or a = 3 and b = 13
// And rewrite that to WHERE (a, b) IN ((1,11), (2,12), (3,13))
func ExtractINFromOR(expr *OrExpr) []Expr {
var varNames []*ColName
var values []Exprs
orSlice := orToSlice(expr)
for _, expr := range orSlice {
andSlice := andToSlice(expr)
if len(andSlice) == 0 {
return nil
}
var currentVarNames []*ColName
var currentValues []Expr
for _, comparisonExpr := range andSlice {
if comparisonExpr.Operator != EqualOp {
return nil
}
var colName *ColName
if left, ok := comparisonExpr.Left.(*ColName); ok {
colName = left
currentValues = append(currentValues, comparisonExpr.Right)
}
if right, ok := comparisonExpr.Right.(*ColName); ok {
if colName != nil {
return nil
}
colName = right
currentValues = append(currentValues, comparisonExpr.Left)
}
if colName == nil {
return nil
}
currentVarNames = append(currentVarNames, colName)
}
if len(varNames) == 0 {
varNames = currentVarNames
} else if !slices.EqualFunc(varNames, currentVarNames, func(col1, col2 *ColName) bool { return col1.Equal(col2) }) {
return nil
}
values = append(values, currentValues)
}
var nameTuple ValTuple
for _, name := range varNames {
nameTuple = append(nameTuple, name)
}
var valueTuple ValTuple
for _, value := range values {
valueTuple = append(valueTuple, ValTuple(value))
}
return []Expr{&ComparisonExpr{
Operator: InOp,
Left: nameTuple,
Right: valueTuple,
}}
}
func orToSlice(expr *OrExpr) []Expr {
var exprs []Expr
handleOrSide := func(e Expr) {
switch e := e.(type) {
case *OrExpr:
exprs = append(exprs, orToSlice(e)...)
default:
exprs = append(exprs, e)
}
}
handleOrSide(expr.Left)
handleOrSide(expr.Right)
return exprs
}
func andToSlice(expr Expr) []*ComparisonExpr {
var andExpr *AndExpr
switch expr := expr.(type) {
case *AndExpr:
andExpr = expr
case *ComparisonExpr:
return []*ComparisonExpr{expr}
default:
return nil
}
var exprs []*ComparisonExpr
handleAndSide := func(e Expr) bool {
switch e := e.(type) {
case *AndExpr:
slice := andToSlice(e)
if slice == nil {
return false
}
exprs = append(exprs, slice...)
case *ComparisonExpr:
exprs = append(exprs, e)
default:
return false
}
return true
}
if !handleAndSide(andExpr.Left) {
return nil
}
if !handleAndSide(andExpr.Right) {
return nil
}
return exprs
}
func tryTurningOrIntoIn(l, r *ComparisonExpr) (Expr, bool) {
// looks for A = X OR A = Y and turns them into A IN (X, Y)
col, ok := l.Left.(*ColName)
if !ok || !Equals.Expr(col, r.Left) {
return nil, false
}
var tuple ValTuple
switch l.Operator {
case EqualOp:
tuple = ValTuple{l.Right}
case InOp:
lft, ok := l.Right.(ValTuple)
if !ok {
return nil, false
}
tuple = lft
default:
return nil, false
}
switch r.Operator {
case EqualOp:
tuple = append(tuple, r.Right)
case InOp:
lft, ok := r.Right.(ValTuple)
if !ok {
return nil, false
}
tuple = append(tuple, lft...)
default:
return nil, false
}
return &ComparisonExpr{
Operator: InOp,
Left: col,
Right: uniquefy(tuple),
}, true
}
func uniquefy(tuple ValTuple) (output ValTuple) {
outer:
for _, expr := range tuple {
for _, seen := range output {
if Equals.Expr(expr, seen) {
continue outer
}
}
output = append(output, expr)
}
return
}
func distinctOr(in *OrExpr) (result Expr, changed bool) {
todo := []*OrExpr{in}
var leaves []Expr
for len(todo) > 0 {
curr := todo[0]
todo = todo[1:]
addAnd := func(in Expr) {
and, ok := in.(*OrExpr)
if ok {
todo = append(todo, and)
} else {
leaves = append(leaves, in)
}
}
addAnd(curr.Left)
addAnd(curr.Right)
}
var predicates []Expr
outer1:
for _, curr := range leaves {
for _, alreadyIn := range predicates {
if Equals.Expr(alreadyIn, curr) {
changed = true
continue outer1
}
}
predicates = append(predicates, curr)
}
if !changed {
return in, false
}
for i, curr := range predicates {
if i == 0 {
result = curr
continue
}
result = &OrExpr{Left: result, Right: curr}
}
return
}
func distinctAnd(in *AndExpr) (result Expr, changed bool) {
todo := []*AndExpr{in}
var leaves []Expr
for len(todo) > 0 {
curr := todo[0]
todo = todo[1:]
addExpr := func(in Expr) {
if and, ok := in.(*AndExpr); ok {
todo = append(todo, and)
} else {
leaves = append(leaves, in)
}
}
addExpr(curr.Left)
addExpr(curr.Right)
}
var predicates []Expr
outer1:
for _, curr := range leaves {
for _, alreadyIn := range predicates {
if Equals.Expr(alreadyIn, curr) {
changed = true
continue outer1
}
}
predicates = append(predicates, curr)
}
if !changed {
return in, false
}
for i, curr := range predicates {
if i == 0 {
result = curr
continue
}
result = &AndExpr{Left: result, Right: curr}
}
return AndExpressions(leaves...), true
}