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
|
/*-------------------------------------------------------------------------
*
* prepqual.c--
* Routines for preprocessing the parse tree qualification
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepqual.c,v 1.1.1.1 1996/07/09 06:21:38 scrappy Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "nodes/pg_list.h"
#include "nodes/makefuncs.h"
#include "optimizer/internal.h"
#include "optimizer/clauses.h"
#include "optimizer/prep.h"
#include "utils/lsyscache.h"
static Expr *pull_args(Expr *qual);
static List *pull_ors(List *orlist);
static List *pull_ands(List *andlist);
static Expr *find_nots(Expr *qual);
static Expr *push_nots(Expr *qual);
static Expr *normalize(Expr *qual);
static List *or_normalize(List *orlist);
static List *distribute_args(List *item, List *args);
static List *qualcleanup(Expr *qual);
static List *remove_ands(Expr *qual);
static List *remove_duplicates(List *list);
/*
* preprocess-qualification--
* Driver routine for modifying the parse tree qualification.
*
* Returns the new base qualification and the existential qualification
* in existentialQualPtr.
*
* XXX right now, update_clauses() does nothing so
* preprocess-qualification simply converts the qual in conjunctive
* normal form (see cnfify() below )
*/
List *
preprocess_qualification(Expr *qual, List *tlist, List **existentialQualPtr)
{
List *cnf_qual = cnfify(qual, true);
/*
List *existential_qual =
update_clauses(intCons(_query_result_relation_,
update_relations(tlist)),
cnf_qual,
_query_command_type_);
if (existential_qual) {
*existentialQualPtr = existential_qual;
return set_difference(cnf_qual, existential_qual);
} else {
*existentialQualPtr = NIL;
return cnf_qual;
}
*/
/* update_clauses() is not working right now */
*existentialQualPtr = NIL;
return cnf_qual;
}
/*****************************************************************************
*
* CNF CONVERSION ROUTINES
*
* NOTES:
* The basic algorithms for normalizing the qualification are taken
* from ingres/source/qrymod/norml.c
*
* Remember that the initial qualification may consist of ARBITRARY
* combinations of clauses. In addition, before this routine is called,
* the qualification will contain explicit "AND"s.
*
*****************************************************************************/
/*
* cnfify--
* Convert a qualification to conjunctive normal form by applying
* successive normalizations.
*
* Returns the modified qualification with an extra level of nesting.
*
* If 'removeAndFlag' is true then it removes the explicit ANDs.
*
* NOTE: this routine is called by the planner (removeAndFlag = true)
* and from the rule manager (removeAndFlag = false).
*
*/
List *
cnfify(Expr *qual, bool removeAndFlag)
{
Expr *newqual = NULL;
if (qual != NULL) {
newqual = find_nots(pull_args(qual));
newqual = normalize(pull_args(newqual));
newqual = (Expr*)qualcleanup(pull_args(newqual));
newqual = pull_args(newqual);;
if (removeAndFlag) {
if(and_clause((Node*)newqual))
newqual=(Expr*)remove_ands(newqual);
else
newqual=(Expr*)remove_ands(make_andclause(lcons(newqual,NIL)));
}
}
else if (qual!=NULL)
newqual = (Expr*)lcons(qual, NIL);
return (List*)(newqual);
}
/*
* pull-args--
* Given a qualification, eliminate nested 'and' and 'or' clauses.
*
* Returns the modified qualification.
*
*/
static Expr *
pull_args(Expr *qual)
{
if (qual==NULL)
return (NULL);
if (is_opclause((Node*)qual)) {
return(make_clause(qual->opType, qual->oper,
lcons(pull_args((Expr*)get_leftop(qual)),
lcons(pull_args((Expr*)get_rightop(qual)),
NIL))));
} else if (and_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach (temp, qual->args)
t_list = lappend (t_list, pull_args(lfirst(temp)));
return (make_andclause (pull_ands (t_list)));
}else if (or_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach (temp, qual->args)
t_list = lappend (t_list, pull_args(lfirst(temp)));
return (make_orclause (pull_ors (t_list)));
} else if (not_clause((Node*)qual)) {
return (make_notclause (pull_args (get_notclausearg (qual))));
} else {
return (qual);
}
}
/*
* pull-ors--
* Pull the arguments of an 'or' clause nested within another 'or'
* clause up into the argument list of the parent.
*
* Returns the modified list.
*/
static List *
pull_ors(List *orlist)
{
if (orlist==NIL)
return (NIL);
if (or_clause(lfirst(orlist))) {
List *args = ((Expr*)lfirst(orlist))->args;
return (pull_ors(nconc(copyObject((Node*)args),
copyObject((Node*)lnext(orlist)))));
} else {
return (lcons(lfirst(orlist), pull_ors(lnext(orlist))));
}
}
/*
* pull-ands--
* Pull the arguments of an 'and' clause nested within another 'and'
* clause up into the argument list of the parent.
*
* Returns the modified list.
*/
static List *
pull_ands(List *andlist)
{
if (andlist==NIL)
return (NIL);
if (and_clause (lfirst(andlist))) {
List *args = ((Expr*)lfirst(andlist))->args;
return (pull_ands(nconc(copyObject((Node*)args),
copyObject((Node*)lnext(andlist)))));
} else {
return (lcons(lfirst(andlist), pull_ands(lnext(andlist))));
}
}
/*
* find-nots--
* Traverse the qualification, looking for 'not's to take care of.
* For 'not' clauses, remove the 'not' and push it down to the clauses'
* descendants.
* For all other clause types, simply recurse.
*
* Returns the modified qualification.
*
*/
static Expr *
find_nots(Expr *qual)
{
if (qual==NULL)
return (NULL);
if (is_opclause((Node*)qual)) {
return (make_clause(qual->opType, qual->oper,
lcons(find_nots((Expr*)get_leftop(qual)),
lcons(find_nots((Expr*)get_rightop(qual)),
NIL))));
} else if (and_clause ((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach (temp, qual->args) {
t_list = lappend(t_list,find_nots(lfirst(temp)));
}
return (make_andclause(t_list));
} else if (or_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach (temp, qual->args) {
t_list = lappend(t_list,find_nots(lfirst(temp)));
}
return (make_orclause (t_list));
} else if (not_clause((Node*)qual))
return (push_nots(get_notclausearg (qual)));
else
return (qual);
}
/*
* push-nots--
* Negate the descendants of a 'not' clause.
*
* Returns the modified qualification.
*
*/
static Expr *
push_nots(Expr *qual)
{
if (qual==NULL)
return (NULL);
/*
* Negate an operator clause if possible:
* ("NOT" (< A B)) => (> A B)
* Otherwise, retain the clause as it is (the 'not' can't be pushed
* down any farther).
*/
if (is_opclause((Node*)qual)) {
Oper *oper = (Oper*)((Expr*)qual)->oper;
Oid negator = get_negator(oper->opno);
if(negator) {
Oper *op = (Oper*) makeOper(negator,
InvalidOid,
oper->opresulttype,
0, NULL);
op->op_fcache = (FunctionCache *) NULL;
return
(make_opclause(op, get_leftop(qual), get_rightop(qual)));
} else {
return (make_notclause(qual));
}
} else if (and_clause((Node*)qual)) {
/* Apply DeMorgan's Laws:
* ("NOT" ("AND" A B)) => ("OR" ("NOT" A) ("NOT" B))
* ("NOT" ("OR" A B)) => ("AND" ("NOT" A) ("NOT" B))
* i.e., continue negating down through the clause's descendants.
*/
List *temp = NIL;
List *t_list = NIL;
foreach(temp, qual->args) {
t_list = lappend(t_list,push_nots(lfirst(temp)));
}
return (make_orclause (t_list));
} else if (or_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach(temp, qual->args) {
t_list = lappend(t_list,push_nots(lfirst(temp)));
}
return (make_andclause (t_list));
} else if (not_clause((Node*)qual))
/* Another 'not' cancels this 'not', so eliminate the 'not' and
* stop negating this branch.
*/
return (find_nots (get_notclausearg (qual)));
else
/* We don't know how to negate anything else, place a 'not' at this
* level.
*/
return (make_notclause (qual));
}
/*
* normalize--
* Given a qualification tree with the 'not's pushed down, convert it
* to a tree in CNF by repeatedly applying the rule:
* ("OR" A ("AND" B C)) => ("AND" ("OR" A B) ("OR" A C))
* bottom-up.
* Note that 'or' clauses will always be turned into 'and' clauses.
*
* Returns the modified qualification.
*
*/
static Expr *
normalize(Expr *qual)
{
if (qual==NULL)
return (NULL);
if (is_opclause((Node*)qual)) {
Expr *expr = (Expr*)qual;
return (make_clause(expr->opType, expr->oper,
lcons(normalize((Expr*)get_leftop(qual)),
lcons(normalize((Expr*)get_rightop(qual)),
NIL))));
} else if (and_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
foreach (temp, qual->args) {
t_list = lappend(t_list,normalize(lfirst(temp)));
}
return (make_andclause (t_list));
} else if (or_clause((Node*)qual)) {
/* XXX - let form, maybe incorrect */
List *orlist = NIL;
List *temp = NIL;
bool has_andclause = FALSE;
foreach(temp, qual->args) {
orlist = lappend(orlist,normalize(lfirst(temp)));
}
foreach (temp, orlist) {
if (and_clause (lfirst(temp))) {
has_andclause = TRUE;
break;
}
}
if (has_andclause == TRUE)
return (make_andclause(or_normalize(orlist)));
else
return (make_orclause(orlist));
} else if (not_clause((Node*)qual))
return (make_notclause (normalize (get_notclausearg (qual))));
else
return (qual);
}
/*
* or-normalize--
* Given a list of exprs which are 'or'ed together, distribute any
* 'and' clauses.
*
* Returns the modified list.
*
*/
static List *
or_normalize(List *orlist)
{
List *distributable = NIL;
List *new_orlist = NIL;
List *temp = NIL;
if (orlist==NIL)
return NIL;
foreach(temp, orlist) {
if (and_clause(lfirst(temp)))
distributable = lfirst(temp);
}
if (distributable)
new_orlist = LispRemove(distributable,orlist);
if(new_orlist) {
return
(or_normalize(lcons(distribute_args(lfirst(new_orlist),
((Expr*)distributable)->args),
lnext(new_orlist))));
}else {
return (orlist);
}
}
/*
* distribute-args--
* Create new 'or' clauses by or'ing 'item' with each element of 'args'.
* E.g.: (distribute-args A ("AND" B C)) => ("AND" ("OR" A B) ("OR" A C))
*
* Returns an 'and' clause.
*
*/
static List *
distribute_args(List *item, List *args)
{
List *or_list = NIL;
List *n_list = NIL;
List *temp = NIL;
List *t_list = NIL;
if (args==NULL)
return (item);
foreach (temp,args) {
n_list = or_normalize(pull_ors(lcons(item,
lcons(lfirst(temp),NIL))));
or_list = (List*)make_orclause(n_list);
t_list = lappend(t_list,or_list);
}
return ((List*)make_andclause(t_list));
}
/*
* qualcleanup--
* Fix up a qualification by removing duplicate entries (left over from
* normalization), and by removing 'and' and 'or' clauses which have only
* one valid expr (e.g., ("AND" A) => A).
*
* Returns the modified qualfication.
*
*/
static List *
qualcleanup(Expr *qual)
{
if (qual==NULL)
return (NIL);
if (is_opclause((Node*)qual)) {
return ((List*)make_clause(qual->opType, qual->oper,
lcons(qualcleanup((Expr*)get_leftop(qual)),
lcons(qualcleanup((Expr*)get_rightop(qual)),
NIL))));
} else if (and_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
List *new_and_args = NIL;
foreach(temp, qual->args)
t_list = lappend(t_list,qualcleanup(lfirst(temp)));
new_and_args = remove_duplicates(t_list);
if(length (new_and_args) > 1)
return ((List*)make_andclause(new_and_args));
else
return (lfirst(new_and_args));
}
else if (or_clause((Node*)qual)) {
List *temp = NIL;
List *t_list = NIL;
List *new_or_args = NIL;
foreach (temp, qual->args)
t_list = lappend(t_list,qualcleanup(lfirst(temp)));
new_or_args = remove_duplicates(t_list);
if(length (new_or_args) > 1)
return ((List*)make_orclause (new_or_args));
else
return (lfirst (new_or_args));
} else if (not_clause((Node*)qual))
return ((List*)make_notclause((Expr*)qualcleanup((Expr*)get_notclausearg(qual))));
else
return ((List*)qual);
}
/*
* remove-ands--
* Remove the explicit "AND"s from the qualification:
* ("AND" A B) => (A B)
*
* RETURNS : qual
* MODIFIES: qual
*/
static List *
remove_ands(Expr *qual)
{
List *t_list = NIL;
if (qual==NULL)
return (NIL);
if (is_opclause((Node*)qual)) {
return ((List*)make_clause(qual->opType, qual->oper,
lcons(remove_ands((Expr*)get_leftop(qual)),
lcons(remove_ands((Expr*)get_rightop(qual)),
NIL))));
} else if (and_clause((Node*)qual)) {
List *temp = NIL;
foreach (temp, qual->args)
t_list = lappend(t_list,remove_ands(lfirst(temp)));
return(t_list);
} else if (or_clause((Node*)qual)) {
List *temp = NIL;
foreach (temp, qual->args)
t_list = lappend(t_list,remove_ands(lfirst(temp)));
return ((List*)make_orclause((List*)t_list));
} else if (not_clause((Node*)qual)) {
return ((List*)make_notclause((Expr*)remove_ands((Expr*)get_notclausearg (qual))));
} else {
return ((List*)qual);
}
}
/*****************************************************************************
*
* EXISTENTIAL QUALIFICATIONS
*
*****************************************************************************/
/*
* update-relations--
* Returns the range table indices (i.e., varnos) for all relations which
* are referenced in the target list.
*
*/
#if 0
static List *
update_relations(List *tlist)
{
return(NIL);
}
#endif
/*****************************************************************************
*
*
*
*****************************************************************************/
static List *
remove_duplicates(List *list)
{
List *i;
List *j;
List *result = NIL;
bool there_exists_duplicate = false;
if (length(list) == 1)
return(list);
foreach (i, list) {
if (i != NIL) {
foreach (j, lnext(i)) {
if (equal(lfirst(i), lfirst(j)))
there_exists_duplicate = true;
}
if (!there_exists_duplicate)
result = lappend(result, lfirst(i));
there_exists_duplicate = false;
}
}
return(result);
}
|