OSDN Git Service

Bye CursorStmt, now use SelectStmt.
[pg-rex/syncrep.git] / src / backend / parser / analyze.c
1 /*-------------------------------------------------------------------------
2  *
3  * analyze.c--
4  *        transform the parse tree into a query tree
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.63 1998/01/10 04:29:47 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <string.h>
19
20 #include "postgres.h"
21 #include "access/heapam.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/memnodes.h"
24 #include "nodes/pg_list.h"
25 #include "parser/analyze.h"
26 #include "parser/parse_agg.h"
27 #include "parser/parse_clause.h"
28 #include "parser/parse_node.h"
29 #include "parser/parse_relation.h"
30 #include "parser/parse_target.h"
31 #include "utils/builtins.h"
32 #include "utils/mcxt.h"
33
34 static Query *transformStmt(ParseState *pstate, Node *stmt);
35 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
36 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
37 static Query *transformIndexStmt(ParseState *pstate, IndexStmt *stmt);
38 static Query *transformExtendStmt(ParseState *pstate, ExtendStmt *stmt);
39 static Query *transformRuleStmt(ParseState *query, RuleStmt *stmt);
40 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
41 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
42 static Query *transformCursorStmt(ParseState *pstate, SelectStmt *stmt);
43 static Query *transformCreateStmt(ParseState *pstate, CreateStmt *stmt);
44
45 List   *extras = NIL;
46
47 /*
48  * parse_analyze -
49  *        analyze a list of parse trees and transform them if necessary.
50  *
51  * Returns a list of transformed parse trees. Optimizable statements are
52  * all transformed to Query while the rest stays the same.
53  *
54  */
55 QueryTreeList *
56 parse_analyze(List *pl)
57 {
58         QueryTreeList *result;
59         ParseState *pstate;
60         int                     i = 0;
61
62         result = malloc(sizeof(QueryTreeList));
63         result->len = length(pl);
64         result->qtrees = (Query **) malloc(result->len * sizeof(Query *));
65
66         while (pl != NIL)
67         {
68                 pstate = make_parsestate();
69                 result->qtrees[i++] = transformStmt(pstate, lfirst(pl));
70                 if (extras != NIL)
71                 {
72                         result->len += length(extras);
73                         result->qtrees = (Query **) realloc(result->qtrees, result->len * sizeof(Query *));
74                         while (extras != NIL)
75                         {
76                                 result->qtrees[i++] = transformStmt(pstate, lfirst(extras));
77                                 extras = lnext(extras);
78                         }
79                 }
80                 extras = NIL;
81                 pl = lnext(pl);
82                 if (pstate->p_target_relation != NULL)
83                         heap_close(pstate->p_target_relation);
84                 pfree(pstate);
85         }
86
87         return result;
88 }
89
90 /*
91  * transformStmt -
92  *        transform a Parse tree. If it is an optimizable statement, turn it
93  *        into a Query tree.
94  */
95 static Query *
96 transformStmt(ParseState *pstate, Node *parseTree)
97 {
98         Query      *result = NULL;
99
100         switch (nodeTag(parseTree))
101         {
102                         /*------------------------
103                          *      Non-optimizable statements
104                          *------------------------
105                          */
106                 case T_CreateStmt:
107                         result = transformCreateStmt(pstate, (CreateStmt *) parseTree);
108                         break;
109
110                 case T_IndexStmt:
111                         result = transformIndexStmt(pstate, (IndexStmt *) parseTree);
112                         break;
113
114                 case T_ExtendStmt:
115                         result = transformExtendStmt(pstate, (ExtendStmt *) parseTree);
116                         break;
117
118                 case T_RuleStmt:
119                         result = transformRuleStmt(pstate, (RuleStmt *) parseTree);
120                         break;
121
122                 case T_ViewStmt:
123                         {
124                                 ViewStmt   *n = (ViewStmt *) parseTree;
125
126                                 n->query = (Query *) transformStmt(pstate, (Node *) n->query);
127                                 result = makeNode(Query);
128                                 result->commandType = CMD_UTILITY;
129                                 result->utilityStmt = (Node *) n;
130                         }
131                         break;
132
133                 case T_VacuumStmt:
134                         {
135                                 MemoryContext oldcontext;
136
137                                 /*
138                                  * make sure that this Query is allocated in TopMemory
139                                  * context because vacuum spans transactions and we don't
140                                  * want to lose the vacuum Query due to end-of-transaction
141                                  * free'ing
142                                  */
143                                 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
144                                 result = makeNode(Query);
145                                 result->commandType = CMD_UTILITY;
146                                 result->utilityStmt = (Node *) parseTree;
147                                 MemoryContextSwitchTo(oldcontext);
148                                 break;
149
150                         }
151                 case T_ExplainStmt:
152                         {
153                                 ExplainStmt *n = (ExplainStmt *) parseTree;
154
155                                 result = makeNode(Query);
156                                 result->commandType = CMD_UTILITY;
157                                 n->query = transformStmt(pstate, (Node *) n->query);
158                                 result->utilityStmt = (Node *) parseTree;
159                         }
160                         break;
161
162                         /*------------------------
163                          *      Optimizable statements
164                          *------------------------
165                          */
166                 case T_InsertStmt:
167                         result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
168                         break;
169
170                 case T_DeleteStmt:
171                         result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
172                         break;
173
174                 case T_UpdateStmt:
175                         result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
176                         break;
177
178                 case T_SelectStmt:
179                         if (!((SelectStmt *)parseTree)->portalname)
180                                 result = transformSelectStmt(pstate, (SelectStmt *) parseTree);
181                         else
182                                 result = transformCursorStmt(pstate, (SelectStmt *) parseTree);
183                         break;
184
185                 default:
186
187                         /*
188                          * other statments don't require any transformation-- just
189                          * return the original parsetree
190                          */
191                         result = makeNode(Query);
192                         result->commandType = CMD_UTILITY;
193                         result->utilityStmt = (Node *) parseTree;
194                         break;
195         }
196         return result;
197 }
198
199 /*
200  * transformDeleteStmt -
201  *        transforms a Delete Statement
202  */
203 static Query *
204 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
205 {
206         Query      *qry = makeNode(Query);
207
208         qry->commandType = CMD_DELETE;
209
210         /* set up a range table */
211         makeRangeTable(pstate, stmt->relname, NULL);
212
213         qry->uniqueFlag = NULL;
214
215         /* fix where clause */
216         qry->qual = transformWhereClause(pstate, stmt->whereClause);
217
218         qry->rtable = pstate->p_rtable;
219         qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
220
221         /* make sure we don't have aggregates in the where clause */
222         if (pstate->p_numAgg > 0)
223                 parseCheckAggregates(pstate, qry);
224
225         return (Query *) qry;
226 }
227
228 /*
229  * transformInsertStmt -
230  *        transform an Insert Statement
231  */
232 static Query *
233 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
234 {
235         Query      *qry = makeNode(Query);      /* make a new query tree */
236         List       *icolumns;
237
238         qry->commandType = CMD_INSERT;
239         pstate->p_is_insert = true;
240
241         /* set up a range table */
242         makeRangeTable(pstate, stmt->relname, stmt->fromClause);
243
244         qry->uniqueFlag = NULL;
245
246         /* fix the target list */
247         icolumns = pstate->p_insert_columns = makeTargetNames(pstate, stmt->cols);
248         
249         qry->targetList = transformTargetList(pstate, stmt->targetList);
250         
251         /* DEFAULT handling */
252         if (length(qry->targetList) < pstate->p_target_relation->rd_att->natts &&
253                 pstate->p_target_relation->rd_att->constr &&
254                 pstate->p_target_relation->rd_att->constr->num_defval > 0)
255         {
256                 AttributeTupleForm         *att = pstate->p_target_relation->rd_att->attrs;
257                 AttrDefault                        *defval = pstate->p_target_relation->rd_att->constr->defval;
258                 int                                             ndef = pstate->p_target_relation->rd_att->constr->num_defval;
259                 
260                 /* 
261                  * if stmt->cols == NIL then makeTargetNames returns list of all 
262                  * attrs: have to shorter icolumns list...
263                  */
264                 if (stmt->cols == NIL)
265                 {
266                         List   *extrl;
267                         int             i = length(qry->targetList);
268                         
269                         foreach (extrl, icolumns)
270                         {
271                                 if (--i <= 0)
272                                         break;
273                         }
274                         freeList (lnext(extrl));
275                         lnext(extrl) = NIL;
276                 }
277                 
278                 while (ndef-- > 0)
279                 {
280                         List               *tl;
281                         Ident              *id;
282                         TargetEntry        *te;
283                         
284                         foreach (tl, icolumns)
285                         {
286                                 id = (Ident *) lfirst(tl);
287                                 if (!namestrcmp(&(att[defval[ndef].adnum - 1]->attname), id->name))
288                                         break;
289                         }
290                         if (tl != NIL)          /* something given for this attr */
291                                 continue;
292                         /* 
293                          * Nothing given for this attr with DEFAULT expr, so
294                          * add new TargetEntry to qry->targetList. 
295                          * Note, that we set resno to defval[ndef].adnum:
296                          * it's what transformTargetList()->make_targetlist_expr()
297                          * does for INSERT ... SELECT. But for INSERT ... VALUES
298                          * pstate->p_last_resno is used. It doesn't matter for 
299                          * "normal" using (planner creates proper target list
300                          * in preptlist.c), but may break RULEs in some way.
301                          * It seems better to create proper target list here...
302                          */
303                         te = makeNode(TargetEntry);
304                         te->resdom = makeResdom(defval[ndef].adnum,
305                                                                         att[defval[ndef].adnum - 1]->atttypid,
306                                                                         att[defval[ndef].adnum - 1]->attlen,
307                                                                         pstrdup(nameout(&(att[defval[ndef].adnum - 1]->attname))),
308                                                                         0, 0, 0);
309                         te->fjoin = NULL;
310                         te->expr = (Node *) stringToNode(defval[ndef].adbin);
311                         qry->targetList = lappend (qry->targetList, te);
312                 }
313         }
314         
315         /* fix where clause */
316         qry->qual = transformWhereClause(pstate, stmt->whereClause);
317
318         /* now the range table will not change */
319         qry->rtable = pstate->p_rtable;
320         qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
321
322         if (pstate->p_numAgg > 0)
323                 finalizeAggregates(pstate, qry);
324
325         return (Query *) qry;
326 }
327
328 /* makeTableName()
329  * Create a table name from a list of fields.
330  */
331 static char *
332 makeTableName(void *elem,...);
333
334 static char *
335 makeTableName(void *elem,...)
336 {
337         va_list args;
338
339         char   *name;
340         char    buf[NAMEDATALEN+1];
341
342         strcpy(buf,"");
343
344         va_start(args,elem);
345
346         name = elem;
347         while (name != NULL)
348         {
349                 /* not enough room for next part? then return nothing */
350                 if ((strlen(buf)+strlen(name)) >= (sizeof(buf)-1))
351                         return (NULL);
352
353                 if (strlen(buf) > 0) strcat(buf,"_");
354                 strcat(buf,name);
355
356                 name = va_arg(args,void *);
357         }
358
359         va_end(args);
360
361         name = palloc(strlen(buf)+1);
362         strcpy(name,buf);
363
364         return (name);
365 } /* makeTableName() */
366
367 char *
368 CreateIndexName(char *tname, char *cname, char *label, List *indices);
369
370 char *
371 CreateIndexName(char *tname, char *cname, char *label, List *indices)
372 {
373         int                     pass = 0;
374         char       *iname = NULL;
375         List       *ilist;
376         IndexStmt  *index;
377         char            name2[NAMEDATALEN+1];
378
379         /* use working storage, since we might be trying several possibilities */
380         strcpy(name2,cname);
381         while (iname == NULL)
382         {
383                 iname = makeTableName(tname, name2, label, NULL);
384                 /* unable to make a name at all? then quit */
385                 if (iname == NULL)
386                         break;
387
388 #if PARSEDEBUG
389 printf("CreateNameIndex- check %s against indices\n",iname);
390 #endif
391
392                 ilist = indices;
393                 while (ilist != NIL)
394                 {
395                         index = lfirst(ilist);
396 #if PARSEDEBUG
397 printf("CreateNameIndex- compare %s with existing index %s\n",iname,index->idxname);
398 #endif
399                         if (strcasecmp(iname,index->idxname) == 0)
400                                 break;
401
402                         ilist = lnext(ilist);
403                 }
404                 /* ran through entire list? then no name conflict found so done */
405                 if (ilist == NIL)
406                         break;
407
408                 /* the last one conflicted, so try a new name component */
409                 pfree(iname);
410                 iname = NULL;
411                 pass++;
412                 sprintf(name2, "%s_%d", cname, (pass+1));
413         }
414
415         return (iname);
416 } /* CreateIndexName() */
417
418 /*
419  * transformCreateStmt -
420  *        transforms the "create table" statement
421  *        SQL92 allows constraints to be scattered all over, so thumb through
422  *         the columns and collect all constraints into one place.
423  *        If there are any implied indices (e.g. UNIQUE or PRIMARY KEY)
424  *         then expand those into multiple IndexStmt blocks.
425  *        - thomas 1997-12-02
426  */
427 static Query *
428 transformCreateStmt(ParseState *pstate, CreateStmt *stmt)
429 {
430         Query      *q;
431         int                     have_pkey = FALSE;
432         List       *elements;
433         Node       *element;
434         List       *columns;
435         List       *dlist;
436         ColumnDef  *column;
437         List       *constraints, *clist;
438         Constraint *constraint;
439         List       *keys;
440         Ident      *key;
441         List       *ilist;
442         IndexStmt  *index;
443         IndexElem  *iparam;
444
445         q = makeNode(Query);
446         q->commandType = CMD_UTILITY;
447
448         elements = stmt->tableElts;
449         constraints = stmt->constraints;
450         columns = NIL;
451         dlist = NIL;
452
453         while (elements != NIL)
454         {
455                 element = lfirst(elements);
456                 switch (nodeTag(element))
457                 {
458                         case T_ColumnDef:
459                                 column = (ColumnDef *) element;
460 #if PARSEDEBUG
461 printf("transformCreateStmt- found column %s\n",column->colname);
462 #endif
463                                 columns = lappend(columns,column);
464                                 if (column->constraints != NIL)
465                                 {
466 #if PARSEDEBUG
467 printf("transformCreateStmt- found constraint(s) on column %s\n",column->colname);
468 #endif
469                                         clist = column->constraints;
470                                         while (clist != NIL)
471                                         {
472                                                 constraint = lfirst(clist);
473                                                 switch (constraint->contype)
474                                                 {
475                                                         case CONSTR_NOTNULL:
476 #if PARSEDEBUG
477 printf("transformCreateStmt- found NOT NULL constraint on column %s\n",column->colname);
478 #endif
479                                                                 if (column->is_not_null)
480                                                                         elog(ERROR,"CREATE TABLE/NOT NULL already specified"
481                                                                                 " for %s.%s", stmt->relname, column->colname);
482                                                                 column->is_not_null = TRUE;
483                                                                 break;
484
485                                                         case CONSTR_DEFAULT:
486 #if PARSEDEBUG
487 printf("transformCreateStmt- found DEFAULT clause on column %s\n",column->colname);
488 #endif
489                                                                 if (column->defval != NULL)
490                                                                         elog(ERROR,"CREATE TABLE/DEFAULT multiple values specified"
491                                                                                 " for %s.%s", stmt->relname, column->colname);
492                                                                 column->defval = constraint->def;
493                                                                 break;
494
495                                                         case CONSTR_PRIMARY:
496 #if PARSEDEBUG
497 printf("transformCreateStmt- found PRIMARY KEY clause on column %s\n",column->colname);
498 #endif
499                                                                 if (constraint->name == NULL)
500                                                                         constraint->name = makeTableName(stmt->relname, "pkey", NULL);
501                                                                 if (constraint->keys == NIL)
502                                                                         constraint->keys = lappend(constraint->keys, column);
503                                                                 dlist = lappend(dlist, constraint);
504                                                                 break;
505
506                                                         case CONSTR_UNIQUE:
507 #if PARSEDEBUG
508 printf("transformCreateStmt- found UNIQUE clause on column %s\n",column->colname);
509 #endif
510                                                                 if (constraint->name == NULL)
511                                                                         constraint->name = makeTableName(stmt->relname, column->colname, "key", NULL);
512                                                                 if (constraint->keys == NIL)
513                                                                         constraint->keys = lappend(constraint->keys, column);
514                                                                 dlist = lappend(dlist, constraint);
515                                                                 break;
516
517                                                         case CONSTR_CHECK:
518 #if PARSEDEBUG
519 printf("transformCreateStmt- found CHECK clause on column %s\n",column->colname);
520 #endif
521                                                                 constraints = lappend(constraints, constraint);
522                                                                 if (constraint->name == NULL)
523                                                                         constraint->name = makeTableName(stmt->relname, column->colname, NULL);
524                                                                 break;
525
526                                                         default:
527                                                                 elog(ERROR,"parser: internal error; unrecognized constraint",NULL);
528                                                                 break;
529                                                 }
530                                                 clist = lnext(clist);
531                                         }
532                                 }
533                                 break;
534
535                         case T_Constraint:
536                                 constraint = (Constraint *) element;
537 #if PARSEDEBUG
538 printf("transformCreateStmt- found constraint %s\n", ((constraint->name != NULL)? constraint->name: "(unknown)"));
539 #endif
540                                 switch (constraint->contype)
541                                 {
542                                         case CONSTR_PRIMARY:
543 #if PARSEDEBUG
544 printf("transformCreateStmt- found PRIMARY KEY clause\n");
545 #endif
546                                                 if (constraint->name == NULL)
547                                                         constraint->name = makeTableName(stmt->relname, "pkey", NULL);
548                                                 dlist = lappend(dlist, constraint);
549                                                 break;
550
551                                         case CONSTR_UNIQUE:
552 #if PARSEDEBUG
553 printf("transformCreateStmt- found UNIQUE clause\n");
554 #endif
555 #if FALSE
556                                                 if (constraint->name == NULL)
557                                                         constraint->name = makeTableName(stmt->relname, "key", NULL);
558 #endif
559                                                 dlist = lappend(dlist, constraint);
560                                                 break;
561
562                                         case CONSTR_CHECK:
563 #if PARSEDEBUG
564 printf("transformCreateStmt- found CHECK clause\n");
565 #endif
566                                                 constraints = lappend(constraints, constraint);
567                                                 break;
568
569                                         case CONSTR_NOTNULL:
570                                         case CONSTR_DEFAULT:
571                                                 elog(ERROR,"parser: internal error; illegal context for constraint",NULL);
572                                                 break;
573                                         default:
574                                                 elog(ERROR,"parser: internal error; unrecognized constraint",NULL);
575                                                 break;
576                                 }
577                                 break;
578
579                         default:
580                                 elog(ERROR,"parser: internal error; unrecognized node",NULL);
581                 }
582
583                 elements = lnext(elements);
584         }
585
586         stmt->tableElts = columns;
587         stmt->constraints = constraints;
588
589 /* Now run through the "deferred list" to complete the query transformation.
590  * For PRIMARY KEYs, mark each column as NOT NULL and create an index.
591  * For UNIQUE, create an index as for PRIMARY KEYS, but do not insist on NOT NULL.
592  *
593  * Note that this code does not currently look for all possible redundant cases
594  *  and either ignore or stop with warning. The create might fail later when
595  *  names for indices turn out to be redundant, or a user might have specified
596  *  extra useless indices which might hurt performance. - thomas 1997-12-08
597  */
598         ilist = NIL;
599         while (dlist != NIL)
600         {
601                 constraint = lfirst(dlist);
602                 if (nodeTag(constraint) != T_Constraint)
603                         elog(ERROR,"parser: internal error; unrecognized deferred node",NULL);
604
605 #if PARSEDEBUG
606 printf("transformCreateStmt- found deferred constraint %s\n",
607  ((constraint->name != NULL)? constraint->name: "(unknown)"));
608 #endif
609
610                 if (constraint->contype == CONSTR_PRIMARY)
611                         if (have_pkey)
612                                 elog(ERROR,"CREATE TABLE/PRIMARY KEY multiple primary keys"
613                                         " for table %s are not legal", stmt->relname);
614                         else 
615                                 have_pkey = TRUE;
616                 else if (constraint->contype != CONSTR_UNIQUE)
617                         elog(ERROR,"parser: internal error; unrecognized deferred constraint",NULL);
618
619 #if PARSEDEBUG
620 printf("transformCreateStmt- found deferred %s clause\n",
621  (constraint->contype == CONSTR_PRIMARY? "PRIMARY KEY": "UNIQUE"));
622 #endif
623
624                 index = makeNode(IndexStmt);
625
626                 index->unique = TRUE;
627                 if (constraint->name != NULL)
628                         index->idxname = constraint->name;
629                 else if (constraint->contype == CONSTR_PRIMARY)
630                 {
631                         if (have_pkey)
632                                 elog(ERROR,"CREATE TABLE/PRIMARY KEY multiple keys for table %s are not legal", stmt->relname);
633
634                         have_pkey = TRUE;
635                         index->idxname = makeTableName(stmt->relname, "pkey", NULL);
636                 }
637                 else
638                         index->idxname = NULL;
639
640                 index->relname = stmt->relname;
641                 index->accessMethod = "btree";
642                 index->indexParams = NIL;
643                 index->withClause = NIL;
644                 index->whereClause = NULL;
645  
646                 keys = constraint->keys;
647                 while (keys != NIL)
648                 {
649                         key = lfirst(keys);
650 #if PARSEDEBUG
651 printf("transformCreateStmt- check key %s for column match\n", key->name);
652 #endif
653                         columns = stmt->tableElts;
654                         column = NULL;
655                         while (columns != NIL)
656                         {
657                                 column = lfirst(columns);
658 #if PARSEDEBUG
659 printf("transformCreateStmt- check column %s for key match\n", column->colname);
660 #endif
661                                 if (strcasecmp(column->colname,key->name) == 0) break;
662                                 else column = NULL;
663                                 columns = lnext(columns);
664                         }
665                         if (column == NULL)
666                                 elog(ERROR,"parser: column '%s' in key does not exist",key->name);
667
668                         if (constraint->contype == CONSTR_PRIMARY)
669                         {
670 #if PARSEDEBUG
671 printf("transformCreateStmt- mark column %s as NOT NULL\n", column->colname);
672 #endif
673                                 column->is_not_null = TRUE;
674                         }
675                         iparam = makeNode(IndexElem);
676                         iparam->name = strcpy(palloc(strlen(column->colname)+1), column->colname);
677                         iparam->args = NIL;
678                         iparam->class = NULL;
679                         iparam->tname = NULL;
680                         index->indexParams = lappend(index->indexParams, iparam);
681
682                         if (index->idxname == NULL)
683                                 index->idxname = CreateIndexName(stmt->relname, iparam->name, "key", ilist);
684
685                         keys = lnext(keys);
686                 }
687
688                 if (index->idxname == NULL)
689                         elog(ERROR,"parser: unable to construct implicit index for table %s"
690                                 "; name too long", stmt->relname);
691                 else
692                         elog(NOTICE,"CREATE TABLE/%s will create implicit index %s for table %s",
693                                 ((constraint->contype == CONSTR_PRIMARY)? "PRIMARY KEY": "UNIQUE"),
694                                 index->idxname, stmt->relname);
695
696                 ilist = lappend(ilist, index);
697                 dlist = lnext(dlist);
698         }
699
700         q->utilityStmt = (Node *) stmt;
701         extras = ilist;
702
703         return q;
704 } /* transformCreateStmt() */
705
706 /*
707  * transformIndexStmt -
708  *        transforms the qualification of the index statement
709  */
710 static Query *
711 transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
712 {
713         Query      *q;
714
715         q = makeNode(Query);
716         q->commandType = CMD_UTILITY;
717
718         /* take care of the where clause */
719         stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
720         stmt->rangetable = pstate->p_rtable;
721
722         q->utilityStmt = (Node *) stmt;
723
724         return q;
725 }
726
727 /*
728  * transformExtendStmt -
729  *        transform the qualifications of the Extend Index Statement
730  *
731  */
732 static Query *
733 transformExtendStmt(ParseState *pstate, ExtendStmt *stmt)
734 {
735         Query      *q;
736
737         q = makeNode(Query);
738         q->commandType = CMD_UTILITY;
739
740         /* take care of the where clause */
741         stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
742         stmt->rangetable = pstate->p_rtable;
743
744         q->utilityStmt = (Node *) stmt;
745         return q;
746 }
747
748 /*
749  * transformRuleStmt -
750  *        transform a Create Rule Statement. The actions is a list of parse
751  *        trees which is transformed into a list of query trees.
752  */
753 static Query *
754 transformRuleStmt(ParseState *pstate, RuleStmt *stmt)
755 {
756         Query      *q;
757         List       *actions;
758
759         q = makeNode(Query);
760         q->commandType = CMD_UTILITY;
761
762         actions = stmt->actions;
763
764         /*
765          * transform each statment, like parse_analyze()
766          */
767         while (actions != NIL)
768         {
769
770                 /*
771                  * NOTE: 'CURRENT' must always have a varno equal to 1 and 'NEW'
772                  * equal to 2.
773                  */
774                 addRangeTableEntry(pstate, stmt->object->relname, "*CURRENT*",
775                                                    FALSE, FALSE);
776                 addRangeTableEntry(pstate, stmt->object->relname, "*NEW*",
777                                                    FALSE, FALSE);
778
779                 pstate->p_last_resno = 1;
780                 pstate->p_is_rule = true;               /* for expand all */
781                 pstate->p_numAgg = 0;
782                 pstate->p_aggs = NULL;
783
784                 lfirst(actions) = transformStmt(pstate, lfirst(actions));
785                 actions = lnext(actions);
786         }
787
788         /* take care of the where clause */
789         stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
790
791         q->utilityStmt = (Node *) stmt;
792         return q;
793 }
794
795
796 /*
797  * transformSelectStmt -
798  *        transforms a Select Statement
799  *
800  */
801 static Query *
802 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
803 {
804         Query      *qry = makeNode(Query);
805
806         qry->commandType = CMD_SELECT;
807
808         /* set up a range table */
809         makeRangeTable(pstate, NULL, stmt->fromClause);
810
811         qry->uniqueFlag = stmt->unique;
812
813         qry->into = stmt->into;
814         qry->isPortal = FALSE;
815
816         /* fix the target list */
817         qry->targetList = transformTargetList(pstate, stmt->targetList);
818
819         /* fix where clause */
820         qry->qual = transformWhereClause(pstate, stmt->whereClause);
821
822         /* check having clause */
823         if (stmt->havingClause)
824                 elog(NOTICE, "HAVING not yet supported; ignore clause", NULL);
825
826         /* fix order clause */
827         qry->sortClause = transformSortClause(pstate,
828                                                                                   stmt->sortClause,
829                                                                                   NIL,
830                                                                                   qry->targetList,
831                                                                                   qry->uniqueFlag);
832
833         qry->groupClause = transformGroupClause(pstate,
834                                                                                         stmt->groupClause,
835                                                                                         qry->targetList);
836         qry->rtable = pstate->p_rtable;
837
838         if (pstate->p_numAgg > 0)
839                 finalizeAggregates(pstate, qry);
840
841         qry->unionall = stmt->unionall; /* in child, so unionClause may be false */
842         qry->unionClause = transformUnionClause(stmt->unionClause, qry->targetList);
843
844         return (Query *) qry;
845 }
846
847 /*
848  * transformUpdateStmt -
849  *        transforms an update statement
850  *
851  */
852 static Query *
853 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
854 {
855         Query      *qry = makeNode(Query);
856
857         qry->commandType = CMD_UPDATE;
858         pstate->p_is_update = true;
859
860         /*
861          * the FROM clause is non-standard SQL syntax. We used to be able to
862          * do this with REPLACE in POSTQUEL so we keep the feature.
863          */
864         makeRangeTable(pstate, stmt->relname, stmt->fromClause);
865
866         /* fix the target list */
867         qry->targetList = transformTargetList(pstate, stmt->targetList);
868
869         /* fix where clause */
870         qry->qual = transformWhereClause(pstate, stmt->whereClause);
871
872         qry->rtable = pstate->p_rtable;
873         qry->resultRelation = refnameRangeTablePosn(pstate->p_rtable, stmt->relname);
874
875         if (pstate->p_numAgg > 0)
876                 finalizeAggregates(pstate, qry);
877
878         /* make sure we don't have aggregates in the where clause */
879         if (pstate->p_numAgg > 0)
880                 parseCheckAggregates(pstate, qry);
881
882         return (Query *) qry;
883 }
884
885 /*
886  * transformCursorStmt -
887  *        transform a Create Cursor Statement
888  *
889  */
890 static Query *
891 transformCursorStmt(ParseState *pstate, SelectStmt *stmt)
892 {
893         Query      *qry;
894
895         qry = transformSelectStmt(pstate, stmt);
896
897         qry->into = stmt->portalname;
898         qry->isPortal = TRUE;
899         qry->isBinary = stmt->binary;           /* internal portal */
900
901         return qry;
902 }