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
|
/*-------------------------------------------------------------------------
*
* analyze.c--
* transform the parse tree into a query tree
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.51 1997/11/26 01:11:03 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "postgres.h"
#include "access/heapam.h"
#include "nodes/makefuncs.h"
#include "nodes/memnodes.h"
#include "nodes/pg_list.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_node.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "utils/builtins.h"
#include "utils/mcxt.h"
static Query *transformStmt(ParseState *pstate, Node *stmt);
static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
static Query *transformInsertStmt(ParseState *pstate, AppendStmt *stmt);
static Query *transformIndexStmt(ParseState *pstate, IndexStmt *stmt);
static Query *transformExtendStmt(ParseState *pstate, ExtendStmt *stmt);
static Query *transformRuleStmt(ParseState *query, RuleStmt *stmt);
static Query *transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt);
static Query *transformUpdateStmt(ParseState *pstate, ReplaceStmt *stmt);
static Query *transformCursorStmt(ParseState *pstate, CursorStmt *stmt);
/*
* parse_analyze -
* analyze a list of parse trees and transform them if necessary.
*
* Returns a list of transformed parse trees. Optimizable statements are
* all transformed to Query while the rest stays the same.
*
* CALLER is responsible for freeing the QueryTreeList* returned
*/
QueryTreeList *
parse_analyze(List *pl)
{
QueryTreeList *result;
ParseState *pstate;
int i = 0;
result = malloc(sizeof(QueryTreeList));
result->len = length(pl);
result->qtrees = (Query **) malloc(result->len * sizeof(Query *));
while (pl != NIL)
{
pstate = make_parsestate();
result->qtrees[i++] = transformStmt(pstate, lfirst(pl));
pl = lnext(pl);
if (pstate->p_target_relation != NULL)
heap_close(pstate->p_target_relation);
free(pstate);
}
return result;
}
/*
* transformStmt -
* transform a Parse tree. If it is an optimizable statement, turn it
* into a Query tree.
*/
static Query *
transformStmt(ParseState *pstate, Node *parseTree)
{
Query *result = NULL;
switch (nodeTag(parseTree))
{
/*------------------------
* Non-optimizable statements
*------------------------
*/
case T_IndexStmt:
result = transformIndexStmt(pstate, (IndexStmt *) parseTree);
break;
case T_ExtendStmt:
result = transformExtendStmt(pstate, (ExtendStmt *) parseTree);
break;
case T_RuleStmt:
result = transformRuleStmt(pstate, (RuleStmt *) parseTree);
break;
case T_ViewStmt:
{
ViewStmt *n = (ViewStmt *) parseTree;
n->query = (Query *) transformStmt(pstate, (Node *) n->query);
result = makeNode(Query);
result->commandType = CMD_UTILITY;
result->utilityStmt = (Node *) n;
}
break;
case T_VacuumStmt:
{
MemoryContext oldcontext;
/*
* make sure that this Query is allocated in TopMemory
* context because vacuum spans transactions and we don't
* want to lose the vacuum Query due to end-of-transaction
* free'ing
*/
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
result = makeNode(Query);
result->commandType = CMD_UTILITY;
result->utilityStmt = (Node *) parseTree;
MemoryContextSwitchTo(oldcontext);
break;
}
case T_ExplainStmt:
{
ExplainStmt *n = (ExplainStmt *) parseTree;
result = makeNode(Query);
result->commandType = CMD_UTILITY;
n->query = transformStmt(pstate, (Node *) n->query);
result->utilityStmt = (Node *) parseTree;
}
break;
/*------------------------
* Optimizable statements
*------------------------
*/
case T_AppendStmt:
result = transformInsertStmt(pstate, (AppendStmt *) parseTree);
break;
case T_DeleteStmt:
result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
break;
case T_ReplaceStmt:
result = transformUpdateStmt(pstate, (ReplaceStmt *) parseTree);
break;
case T_CursorStmt:
result = transformCursorStmt(pstate, (CursorStmt *) parseTree);
break;
case T_RetrieveStmt:
result = transformSelectStmt(pstate, (RetrieveStmt *) parseTree);
break;
default:
/*
* other statments don't require any transformation-- just
* return the original parsetree
*/
result = makeNode(Query);
result->commandType = CMD_UTILITY;
result->utilityStmt = (Node *) parseTree;
break;
}
return result;
}
/*
* transformDeleteStmt -
* transforms a Delete Statement
*/
static Query *
transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
{
Query *qry = makeNode(Query);
qry->commandType = CMD_DELETE;
/* set up a range table */
makeRangeTable(pstate, stmt->relname, NULL);
qry->uniqueFlag = NULL;
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause);
qry->rtable = pstate->p_rtable;
qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
/* make sure we don't have aggregates in the where clause */
if (pstate->p_numAgg > 0)
parseCheckAggregates(pstate, qry);
return (Query *) qry;
}
/*
* transformInsertStmt -
* transform an Insert Statement
*/
static Query *
transformInsertStmt(ParseState *pstate, AppendStmt *stmt)
{
Query *qry = makeNode(Query); /* make a new query tree */
List *icolumns;
qry->commandType = CMD_INSERT;
pstate->p_is_insert = true;
/* set up a range table */
makeRangeTable(pstate, stmt->relname, stmt->fromClause);
qry->uniqueFlag = NULL;
/* fix the target list */
icolumns = pstate->p_insert_columns = makeTargetNames(pstate, stmt->cols);
qry->targetList = transformTargetList(pstate, stmt->targetList);
/* DEFAULT handling */
if (length(qry->targetList) < pstate->p_target_relation->rd_att->natts &&
pstate->p_target_relation->rd_att->constr &&
pstate->p_target_relation->rd_att->constr->num_defval > 0)
{
AttributeTupleForm *att = pstate->p_target_relation->rd_att->attrs;
AttrDefault *defval = pstate->p_target_relation->rd_att->constr->defval;
int ndef = pstate->p_target_relation->rd_att->constr->num_defval;
/*
* if stmt->cols == NIL then makeTargetNames returns list of all
* attrs: have to shorter icolumns list...
*/
if (stmt->cols == NIL)
{
List *extrl;
int i = length(qry->targetList);
foreach (extrl, icolumns)
{
if (--i <= 0)
break;
}
freeList (lnext(extrl));
lnext(extrl) = NIL;
}
while (ndef-- > 0)
{
List *tl;
Ident *id;
TargetEntry *te;
foreach (tl, icolumns)
{
id = (Ident *) lfirst(tl);
if (!namestrcmp(&(att[defval[ndef].adnum - 1]->attname), id->name))
break;
}
if (tl != NIL) /* something given for this attr */
continue;
/*
* Nothing given for this attr with DEFAULT expr, so
* add new TargetEntry to qry->targetList.
* Note, that we set resno to defval[ndef].adnum:
* it's what transformTargetList()->make_targetlist_expr()
* does for INSERT ... SELECT. But for INSERT ... VALUES
* pstate->p_last_resno is used. It doesn't matter for
* "normal" using (planner creates proper target list
* in preptlist.c), but may break RULEs in some way.
* It seems better to create proper target list here...
*/
te = makeNode(TargetEntry);
te->resdom = makeResdom(defval[ndef].adnum,
att[defval[ndef].adnum - 1]->atttypid,
att[defval[ndef].adnum - 1]->attlen,
pstrdup(nameout(&(att[defval[ndef].adnum - 1]->attname))),
0, 0, 0);
te->fjoin = NULL;
te->expr = (Node *) stringToNode(defval[ndef].adbin);
qry->targetList = lappend (qry->targetList, te);
}
}
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause);
/* now the range table will not change */
qry->rtable = pstate->p_rtable;
qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
if (pstate->p_numAgg > 0)
finalizeAggregates(pstate, qry);
return (Query *) qry;
}
/*
* transformIndexStmt -
* transforms the qualification of the index statement
*/
static Query *
transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
{
Query *q;
q = makeNode(Query);
q->commandType = CMD_UTILITY;
/* take care of the where clause */
stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
stmt->rangetable = pstate->p_rtable;
q->utilityStmt = (Node *) stmt;
return q;
}
/*
* transformExtendStmt -
* transform the qualifications of the Extend Index Statement
*
*/
static Query *
transformExtendStmt(ParseState *pstate, ExtendStmt *stmt)
{
Query *q;
q = makeNode(Query);
q->commandType = CMD_UTILITY;
/* take care of the where clause */
stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
stmt->rangetable = pstate->p_rtable;
q->utilityStmt = (Node *) stmt;
return q;
}
/*
* transformRuleStmt -
* transform a Create Rule Statement. The actions is a list of parse
* trees which is transformed into a list of query trees.
*/
static Query *
transformRuleStmt(ParseState *pstate, RuleStmt *stmt)
{
Query *q;
List *actions;
q = makeNode(Query);
q->commandType = CMD_UTILITY;
actions = stmt->actions;
/*
* transform each statment, like parse_analyze()
*/
while (actions != NIL)
{
/*
* NOTE: 'CURRENT' must always have a varno equal to 1 and 'NEW'
* equal to 2.
*/
addRangeTableEntry(pstate, stmt->object->relname, "*CURRENT*",
FALSE, FALSE);
addRangeTableEntry(pstate, stmt->object->relname, "*NEW*",
FALSE, FALSE);
pstate->p_last_resno = 1;
pstate->p_is_rule = true; /* for expand all */
pstate->p_numAgg = 0;
pstate->p_aggs = NULL;
lfirst(actions) = transformStmt(pstate, lfirst(actions));
actions = lnext(actions);
}
/* take care of the where clause */
stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
q->utilityStmt = (Node *) stmt;
return q;
}
/*
* transformSelectStmt -
* transforms a Select Statement
*
*/
static Query *
transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt)
{
Query *qry = makeNode(Query);
qry->commandType = CMD_SELECT;
/* set up a range table */
makeRangeTable(pstate, NULL, stmt->fromClause);
qry->uniqueFlag = stmt->unique;
qry->into = stmt->into;
qry->isPortal = FALSE;
/* fix the target list */
qry->targetList = transformTargetList(pstate, stmt->targetList);
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause);
/* check subselect clause */
if (stmt->selectClause)
elog(NOTICE, "UNION not yet supported; using first SELECT only", NULL);
/* check subselect clause */
if (stmt->havingClause)
elog(NOTICE, "HAVING not yet supported; ignore clause", NULL);
/* fix order clause */
qry->sortClause = transformSortClause(pstate,
stmt->sortClause,
qry->targetList,
qry->uniqueFlag);
/* fix group by clause */
qry->groupClause = transformGroupClause(pstate,
stmt->groupClause,
qry->targetList);
qry->rtable = pstate->p_rtable;
if (pstate->p_numAgg > 0)
finalizeAggregates(pstate, qry);
return (Query *) qry;
}
/*
* transformUpdateStmt -
* transforms an update statement
*
*/
static Query *
transformUpdateStmt(ParseState *pstate, ReplaceStmt *stmt)
{
Query *qry = makeNode(Query);
qry->commandType = CMD_UPDATE;
pstate->p_is_update = true;
/*
* the FROM clause is non-standard SQL syntax. We used to be able to
* do this with REPLACE in POSTQUEL so we keep the feature.
*/
makeRangeTable(pstate, stmt->relname, stmt->fromClause);
/* fix the target list */
qry->targetList = transformTargetList(pstate, stmt->targetList);
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause);
qry->rtable = pstate->p_rtable;
qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
/* make sure we don't have aggregates in the where clause */
if (pstate->p_numAgg > 0)
parseCheckAggregates(pstate, qry);
return (Query *) qry;
}
/*
* transformCursorStmt -
* transform a Create Cursor Statement
*
*/
static Query *
transformCursorStmt(ParseState *pstate, CursorStmt *stmt)
{
Query *qry = makeNode(Query);
/*
* in the old days, a cursor statement is a 'retrieve into portal'; If
* you change the following, make sure you also go through the code in
* various places that tests the kind of operation.
*/
qry->commandType = CMD_SELECT;
/* set up a range table */
makeRangeTable(pstate, NULL, stmt->fromClause);
qry->uniqueFlag = stmt->unique;
qry->into = stmt->portalname;
qry->isPortal = TRUE;
qry->isBinary = stmt->binary; /* internal portal */
/* fix the target list */
qry->targetList = transformTargetList(pstate, stmt->targetList);
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause);
/* fix order clause */
qry->sortClause = transformSortClause(pstate,
stmt->sortClause,
qry->targetList,
qry->uniqueFlag);
/* fix group by clause */
qry->groupClause = transformGroupClause(pstate,
stmt->groupClause,
qry->targetList);
qry->rtable = pstate->p_rtable;
if (pstate->p_numAgg > 0)
finalizeAggregates(pstate, qry);
return (Query *) qry;
}
|