]> granicus.if.org Git - postgresql/blob - src/backend/parser/analyze.c
Modify the definition of window-function PARTITION BY and ORDER BY clauses
[postgresql] / src / backend / parser / analyze.c
1 /*-------------------------------------------------------------------------
2  *
3  * analyze.c
4  *        transform the raw parse tree into a query tree
5  *
6  * For optimizable statements, we are careful to obtain a suitable lock on
7  * each referenced table, and other modules of the backend preserve or
8  * re-obtain these locks before depending on the results.  It is therefore
9  * okay to do significant semantic analysis of these statements.  For
10  * utility commands, no locks are obtained here (and if they were, we could
11  * not be sure we'd still have them at execution).  Hence the general rule
12  * for utility commands is to just dump them into a Query node untransformed.
13  * DECLARE CURSOR and EXPLAIN are exceptions because they contain
14  * optimizable statements.
15  *
16  *
17  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  *      $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.390 2009/08/27 20:08:02 tgl Exp $
21  *
22  *-------------------------------------------------------------------------
23  */
24
25 #include "postgres.h"
26
27 #include "access/sysattr.h"
28 #include "catalog/pg_type.h"
29 #include "nodes/makefuncs.h"
30 #include "nodes/nodeFuncs.h"
31 #include "optimizer/var.h"
32 #include "parser/analyze.h"
33 #include "parser/parse_agg.h"
34 #include "parser/parse_clause.h"
35 #include "parser/parse_coerce.h"
36 #include "parser/parse_cte.h"
37 #include "parser/parse_oper.h"
38 #include "parser/parse_relation.h"
39 #include "parser/parse_target.h"
40 #include "parser/parsetree.h"
41 #include "rewrite/rewriteManip.h"
42 #include "utils/rel.h"
43
44
45 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
46 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
47 static List *transformInsertRow(ParseState *pstate, List *exprlist,
48                                    List *stmtcols, List *icolumns, List *attrnos);
49 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
50 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
51 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
52 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
53                                                   List **colInfo);
54 static void applyColumnNames(List *dst, List *src);
55 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
56 static List *transformReturningList(ParseState *pstate, List *returningList);
57 static Query *transformDeclareCursorStmt(ParseState *pstate,
58                                                    DeclareCursorStmt *stmt);
59 static Query *transformExplainStmt(ParseState *pstate,
60                                          ExplainStmt *stmt);
61 static void transformLockingClause(ParseState *pstate,
62                                            Query *qry, LockingClause *lc);
63 static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
64
65
66 /*
67  * parse_analyze
68  *              Analyze a raw parse tree and transform it to Query form.
69  *
70  * Optionally, information about $n parameter types can be supplied.
71  * References to $n indexes not defined by paramTypes[] are disallowed.
72  *
73  * The result is a Query node.  Optimizable statements require considerable
74  * transformation, while utility-type statements are simply hung off
75  * a dummy CMD_UTILITY Query node.
76  */
77 Query *
78 parse_analyze(Node *parseTree, const char *sourceText,
79                           Oid *paramTypes, int numParams)
80 {
81         ParseState *pstate = make_parsestate(NULL);
82         Query      *query;
83
84         Assert(sourceText != NULL); /* required as of 8.4 */
85
86         pstate->p_sourcetext = sourceText;
87         pstate->p_paramtypes = paramTypes;
88         pstate->p_numparams = numParams;
89         pstate->p_variableparams = false;
90
91         query = transformStmt(pstate, parseTree);
92
93         free_parsestate(pstate);
94
95         return query;
96 }
97
98 /*
99  * parse_analyze_varparams
100  *
101  * This variant is used when it's okay to deduce information about $n
102  * symbol datatypes from context.  The passed-in paramTypes[] array can
103  * be modified or enlarged (via repalloc).
104  */
105 Query *
106 parse_analyze_varparams(Node *parseTree, const char *sourceText,
107                                                 Oid **paramTypes, int *numParams)
108 {
109         ParseState *pstate = make_parsestate(NULL);
110         Query      *query;
111
112         Assert(sourceText != NULL); /* required as of 8.4 */
113
114         pstate->p_sourcetext = sourceText;
115         pstate->p_paramtypes = *paramTypes;
116         pstate->p_numparams = *numParams;
117         pstate->p_variableparams = true;
118
119         query = transformStmt(pstate, parseTree);
120
121         /* make sure all is well with parameter types */
122         if (pstate->p_numparams > 0)
123                 check_parameter_resolution_walker((Node *) query, pstate);
124
125         *paramTypes = pstate->p_paramtypes;
126         *numParams = pstate->p_numparams;
127
128         free_parsestate(pstate);
129
130         return query;
131 }
132
133 /*
134  * parse_sub_analyze
135  *              Entry point for recursively analyzing a sub-statement.
136  */
137 Query *
138 parse_sub_analyze(Node *parseTree, ParseState *parentParseState)
139 {
140         ParseState *pstate = make_parsestate(parentParseState);
141         Query      *query;
142
143         query = transformStmt(pstate, parseTree);
144
145         free_parsestate(pstate);
146
147         return query;
148 }
149
150 /*
151  * transformStmt -
152  *        transform a Parse tree into a Query tree.
153  */
154 Query *
155 transformStmt(ParseState *pstate, Node *parseTree)
156 {
157         Query      *result;
158
159         switch (nodeTag(parseTree))
160         {
161                         /*
162                          * Optimizable statements
163                          */
164                 case T_InsertStmt:
165                         result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
166                         break;
167
168                 case T_DeleteStmt:
169                         result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
170                         break;
171
172                 case T_UpdateStmt:
173                         result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
174                         break;
175
176                 case T_SelectStmt:
177                         {
178                                 SelectStmt *n = (SelectStmt *) parseTree;
179
180                                 if (n->valuesLists)
181                                         result = transformValuesClause(pstate, n);
182                                 else if (n->op == SETOP_NONE)
183                                         result = transformSelectStmt(pstate, n);
184                                 else
185                                         result = transformSetOperationStmt(pstate, n);
186                         }
187                         break;
188
189                         /*
190                          * Special cases
191                          */
192                 case T_DeclareCursorStmt:
193                         result = transformDeclareCursorStmt(pstate,
194                                                                                         (DeclareCursorStmt *) parseTree);
195                         break;
196
197                 case T_ExplainStmt:
198                         result = transformExplainStmt(pstate,
199                                                                                   (ExplainStmt *) parseTree);
200                         break;
201
202                 default:
203
204                         /*
205                          * other statements don't require any transformation; just return
206                          * the original parsetree with a Query node plastered on top.
207                          */
208                         result = makeNode(Query);
209                         result->commandType = CMD_UTILITY;
210                         result->utilityStmt = (Node *) parseTree;
211                         break;
212         }
213
214         /* Mark as original query until we learn differently */
215         result->querySource = QSRC_ORIGINAL;
216         result->canSetTag = true;
217
218         return result;
219 }
220
221 /*
222  * analyze_requires_snapshot
223  *              Returns true if a snapshot must be set before doing parse analysis
224  *              on the given raw parse tree.
225  *
226  * Classification here should match transformStmt(); but we also have to
227  * allow a NULL input (for Parse/Bind of an empty query string).
228  */
229 bool
230 analyze_requires_snapshot(Node *parseTree)
231 {
232         bool            result;
233
234         if (parseTree == NULL)
235                 return false;
236
237         switch (nodeTag(parseTree))
238         {
239                         /*
240                          * Optimizable statements
241                          */
242                 case T_InsertStmt:
243                 case T_DeleteStmt:
244                 case T_UpdateStmt:
245                 case T_SelectStmt:
246                         result = true;
247                         break;
248
249                         /*
250                          * Special cases
251                          */
252                 case T_DeclareCursorStmt:
253                         /* yes, because it's analyzed just like SELECT */
254                         result = true;
255                         break;
256
257                 case T_ExplainStmt:
258
259                         /*
260                          * We only need a snapshot in varparams case, but it doesn't seem
261                          * worth complicating this function's API to distinguish that.
262                          */
263                         result = true;
264                         break;
265
266                 default:
267                         /* utility statements don't have any active parse analysis */
268                         result = false;
269                         break;
270         }
271
272         return result;
273 }
274
275 /*
276  * transformDeleteStmt -
277  *        transforms a Delete Statement
278  */
279 static Query *
280 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
281 {
282         Query      *qry = makeNode(Query);
283         Node       *qual;
284
285         qry->commandType = CMD_DELETE;
286
287         /* set up range table with just the result rel */
288         qry->resultRelation = setTargetTable(pstate, stmt->relation,
289                                                                   interpretInhOption(stmt->relation->inhOpt),
290                                                                                  true,
291                                                                                  ACL_DELETE);
292
293         qry->distinctClause = NIL;
294
295         /*
296          * The USING clause is non-standard SQL syntax, and is equivalent in
297          * functionality to the FROM list that can be specified for UPDATE. The
298          * USING keyword is used rather than FROM because FROM is already a
299          * keyword in the DELETE syntax.
300          */
301         transformFromClause(pstate, stmt->usingClause);
302
303         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
304
305         qry->returningList = transformReturningList(pstate, stmt->returningList);
306
307         /* done building the range table and jointree */
308         qry->rtable = pstate->p_rtable;
309         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
310
311         qry->hasSubLinks = pstate->p_hasSubLinks;
312         qry->hasAggs = pstate->p_hasAggs;
313         if (pstate->p_hasAggs)
314                 parseCheckAggregates(pstate, qry);
315         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
316         if (pstate->p_hasWindowFuncs)
317                 parseCheckWindowFuncs(pstate, qry);
318
319         return qry;
320 }
321
322 /*
323  * transformInsertStmt -
324  *        transform an Insert Statement
325  */
326 static Query *
327 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
328 {
329         Query      *qry = makeNode(Query);
330         SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
331         List       *exprList = NIL;
332         bool            isGeneralSelect;
333         List       *sub_rtable;
334         List       *sub_relnamespace;
335         List       *sub_varnamespace;
336         List       *icolumns;
337         List       *attrnos;
338         RangeTblEntry *rte;
339         RangeTblRef *rtr;
340         ListCell   *icols;
341         ListCell   *attnos;
342         ListCell   *lc;
343
344         qry->commandType = CMD_INSERT;
345         pstate->p_is_insert = true;
346
347         /*
348          * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
349          * VALUES list, or general SELECT input.  We special-case VALUES, both for
350          * efficiency and so we can handle DEFAULT specifications.
351          */
352         isGeneralSelect = (selectStmt && selectStmt->valuesLists == NIL);
353
354         /*
355          * If a non-nil rangetable/namespace was passed in, and we are doing
356          * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
357          * SELECT.      This can only happen if we are inside a CREATE RULE, and in
358          * that case we want the rule's OLD and NEW rtable entries to appear as
359          * part of the SELECT's rtable, not as outer references for it.  (Kluge!)
360          * The SELECT's joinlist is not affected however.  We must do this before
361          * adding the target table to the INSERT's rtable.
362          */
363         if (isGeneralSelect)
364         {
365                 sub_rtable = pstate->p_rtable;
366                 pstate->p_rtable = NIL;
367                 sub_relnamespace = pstate->p_relnamespace;
368                 pstate->p_relnamespace = NIL;
369                 sub_varnamespace = pstate->p_varnamespace;
370                 pstate->p_varnamespace = NIL;
371                 /* There can't be any outer WITH to worry about */
372                 Assert(pstate->p_ctenamespace == NIL);
373         }
374         else
375         {
376                 sub_rtable = NIL;               /* not used, but keep compiler quiet */
377                 sub_relnamespace = NIL;
378                 sub_varnamespace = NIL;
379         }
380
381         /*
382          * Must get write lock on INSERT target table before scanning SELECT, else
383          * we will grab the wrong kind of initial lock if the target table is also
384          * mentioned in the SELECT part.  Note that the target table is not added
385          * to the joinlist or namespace.
386          */
387         qry->resultRelation = setTargetTable(pstate, stmt->relation,
388                                                                                  false, false, ACL_INSERT);
389
390         /* Validate stmt->cols list, or build default list if no list given */
391         icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
392         Assert(list_length(icolumns) == list_length(attrnos));
393
394         /*
395          * Determine which variant of INSERT we have.
396          */
397         if (selectStmt == NULL)
398         {
399                 /*
400                  * We have INSERT ... DEFAULT VALUES.  We can handle this case by
401                  * emitting an empty targetlist --- all columns will be defaulted when
402                  * the planner expands the targetlist.
403                  */
404                 exprList = NIL;
405         }
406         else if (isGeneralSelect)
407         {
408                 /*
409                  * We make the sub-pstate a child of the outer pstate so that it can
410                  * see any Param definitions supplied from above.  Since the outer
411                  * pstate's rtable and namespace are presently empty, there are no
412                  * side-effects of exposing names the sub-SELECT shouldn't be able to
413                  * see.
414                  */
415                 ParseState *sub_pstate = make_parsestate(pstate);
416                 Query      *selectQuery;
417
418                 /*
419                  * Process the source SELECT.
420                  *
421                  * It is important that this be handled just like a standalone SELECT;
422                  * otherwise the behavior of SELECT within INSERT might be different
423                  * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
424                  * bugs of just that nature...)
425                  */
426                 sub_pstate->p_rtable = sub_rtable;
427                 sub_pstate->p_joinexprs = NIL;  /* sub_rtable has no joins */
428                 sub_pstate->p_relnamespace = sub_relnamespace;
429                 sub_pstate->p_varnamespace = sub_varnamespace;
430
431                 selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
432
433                 free_parsestate(sub_pstate);
434
435                 /* The grammar should have produced a SELECT, but it might have INTO */
436                 if (!IsA(selectQuery, Query) ||
437                         selectQuery->commandType != CMD_SELECT ||
438                         selectQuery->utilityStmt != NULL)
439                         elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
440                 if (selectQuery->intoClause)
441                         ereport(ERROR,
442                                         (errcode(ERRCODE_SYNTAX_ERROR),
443                                          errmsg("INSERT ... SELECT cannot specify INTO"),
444                                          parser_errposition(pstate,
445                                                    exprLocation((Node *) selectQuery->intoClause))));
446
447                 /*
448                  * Make the source be a subquery in the INSERT's rangetable, and add
449                  * it to the INSERT's joinlist.
450                  */
451                 rte = addRangeTableEntryForSubquery(pstate,
452                                                                                         selectQuery,
453                                                                                         makeAlias("*SELECT*", NIL),
454                                                                                         false);
455                 rtr = makeNode(RangeTblRef);
456                 /* assume new rte is at end */
457                 rtr->rtindex = list_length(pstate->p_rtable);
458                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
459                 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
460
461                 /*----------
462                  * Generate an expression list for the INSERT that selects all the
463                  * non-resjunk columns from the subquery.  (INSERT's tlist must be
464                  * separate from the subquery's tlist because we may add columns,
465                  * insert datatype coercions, etc.)
466                  *
467                  * HACK: unknown-type constants and params in the SELECT's targetlist
468                  * are copied up as-is rather than being referenced as subquery
469                  * outputs.  This is to ensure that when we try to coerce them to
470                  * the target column's datatype, the right things happen (see
471                  * special cases in coerce_type).  Otherwise, this fails:
472                  *              INSERT INTO foo SELECT 'bar', ... FROM baz
473                  *----------
474                  */
475                 exprList = NIL;
476                 foreach(lc, selectQuery->targetList)
477                 {
478                         TargetEntry *tle = (TargetEntry *) lfirst(lc);
479                         Expr       *expr;
480
481                         if (tle->resjunk)
482                                 continue;
483                         if (tle->expr &&
484                                 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
485                                 exprType((Node *) tle->expr) == UNKNOWNOID)
486                                 expr = tle->expr;
487                         else
488                         {
489                                 Var                *var = makeVar(rtr->rtindex,
490                                                                                   tle->resno,
491                                                                                   exprType((Node *) tle->expr),
492                                                                                   exprTypmod((Node *) tle->expr),
493                                                                                   0);
494
495                                 var->location = exprLocation((Node *) tle->expr);
496                                 expr = (Expr *) var;
497                         }
498                         exprList = lappend(exprList, expr);
499                 }
500
501                 /* Prepare row for assignment to target table */
502                 exprList = transformInsertRow(pstate, exprList,
503                                                                           stmt->cols,
504                                                                           icolumns, attrnos);
505         }
506         else if (list_length(selectStmt->valuesLists) > 1)
507         {
508                 /*
509                  * Process INSERT ... VALUES with multiple VALUES sublists. We
510                  * generate a VALUES RTE holding the transformed expression lists, and
511                  * build up a targetlist containing Vars that reference the VALUES
512                  * RTE.
513                  */
514                 List       *exprsLists = NIL;
515                 int                     sublist_length = -1;
516
517                 /* process the WITH clause */
518                 if (selectStmt->withClause)
519                 {
520                         qry->hasRecursive = selectStmt->withClause->recursive;
521                         qry->cteList = transformWithClause(pstate, selectStmt->withClause);
522                 }
523
524                 foreach(lc, selectStmt->valuesLists)
525                 {
526                         List       *sublist = (List *) lfirst(lc);
527
528                         /* Do basic expression transformation (same as a ROW() expr) */
529                         sublist = transformExpressionList(pstate, sublist);
530
531                         /*
532                          * All the sublists must be the same length, *after*
533                          * transformation (which might expand '*' into multiple items).
534                          * The VALUES RTE can't handle anything different.
535                          */
536                         if (sublist_length < 0)
537                         {
538                                 /* Remember post-transformation length of first sublist */
539                                 sublist_length = list_length(sublist);
540                         }
541                         else if (sublist_length != list_length(sublist))
542                         {
543                                 ereport(ERROR,
544                                                 (errcode(ERRCODE_SYNTAX_ERROR),
545                                                  errmsg("VALUES lists must all be the same length"),
546                                                  parser_errposition(pstate,
547                                                                                         exprLocation((Node *) sublist))));
548                         }
549
550                         /* Prepare row for assignment to target table */
551                         sublist = transformInsertRow(pstate, sublist,
552                                                                                  stmt->cols,
553                                                                                  icolumns, attrnos);
554
555                         exprsLists = lappend(exprsLists, sublist);
556                 }
557
558                 /*
559                  * There mustn't have been any table references in the expressions,
560                  * else strange things would happen, like Cartesian products of those
561                  * tables with the VALUES list ...
562                  */
563                 if (pstate->p_joinlist != NIL)
564                         ereport(ERROR,
565                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
566                                          errmsg("VALUES must not contain table references"),
567                                          parser_errposition(pstate,
568                                                           locate_var_of_level((Node *) exprsLists, 0))));
569
570                 /*
571                  * Another thing we can't currently support is NEW/OLD references in
572                  * rules --- seems we'd need something like SQL99's LATERAL construct
573                  * to ensure that the values would be available while evaluating the
574                  * VALUES RTE.  This is a shame.  FIXME
575                  */
576                 if (list_length(pstate->p_rtable) != 1 &&
577                         contain_vars_of_level((Node *) exprsLists, 0))
578                         ereport(ERROR,
579                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
580                                          errmsg("VALUES must not contain OLD or NEW references"),
581                                          errhint("Use SELECT ... UNION ALL ... instead."),
582                                          parser_errposition(pstate,
583                                                           locate_var_of_level((Node *) exprsLists, 0))));
584
585                 /*
586                  * Generate the VALUES RTE
587                  */
588                 rte = addRangeTableEntryForValues(pstate, exprsLists, NULL, true);
589                 rtr = makeNode(RangeTblRef);
590                 /* assume new rte is at end */
591                 rtr->rtindex = list_length(pstate->p_rtable);
592                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
593                 pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
594
595                 /*
596                  * Generate list of Vars referencing the RTE
597                  */
598                 expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
599         }
600         else
601         {
602                 /*----------
603                  * Process INSERT ... VALUES with a single VALUES sublist.
604                  * We treat this separately for efficiency and for historical
605                  * compatibility --- specifically, allowing table references,
606                  * such as
607                  *                      INSERT INTO foo VALUES(bar.*)
608                  *
609                  * The sublist is just computed directly as the Query's targetlist,
610                  * with no VALUES RTE.  So it works just like SELECT without FROM.
611                  *----------
612                  */
613                 List       *valuesLists = selectStmt->valuesLists;
614
615                 Assert(list_length(valuesLists) == 1);
616
617                 /* process the WITH clause */
618                 if (selectStmt->withClause)
619                 {
620                         qry->hasRecursive = selectStmt->withClause->recursive;
621                         qry->cteList = transformWithClause(pstate, selectStmt->withClause);
622                 }
623
624                 /* Do basic expression transformation (same as a ROW() expr) */
625                 exprList = transformExpressionList(pstate,
626                                                                                    (List *) linitial(valuesLists));
627
628                 /* Prepare row for assignment to target table */
629                 exprList = transformInsertRow(pstate, exprList,
630                                                                           stmt->cols,
631                                                                           icolumns, attrnos);
632         }
633
634         /*
635          * Generate query's target list using the computed list of expressions.
636          * Also, mark all the target columns as needing insert permissions.
637          */
638         rte = pstate->p_target_rangetblentry;
639         qry->targetList = NIL;
640         icols = list_head(icolumns);
641         attnos = list_head(attrnos);
642         foreach(lc, exprList)
643         {
644                 Expr       *expr = (Expr *) lfirst(lc);
645                 ResTarget  *col;
646                 AttrNumber      attr_num;
647                 TargetEntry *tle;
648
649                 col = (ResTarget *) lfirst(icols);
650                 Assert(IsA(col, ResTarget));
651                 attr_num = (AttrNumber) lfirst_int(attnos);
652
653                 tle = makeTargetEntry(expr,
654                                                           attr_num,
655                                                           col->name,
656                                                           false);
657                 qry->targetList = lappend(qry->targetList, tle);
658
659                 rte->modifiedCols = bms_add_member(rte->modifiedCols,
660                                                           attr_num - FirstLowInvalidHeapAttributeNumber);
661
662                 icols = lnext(icols);
663                 attnos = lnext(attnos);
664         }
665
666         /*
667          * If we have a RETURNING clause, we need to add the target relation to
668          * the query namespace before processing it, so that Var references in
669          * RETURNING will work.  Also, remove any namespace entries added in a
670          * sub-SELECT or VALUES list.
671          */
672         if (stmt->returningList)
673         {
674                 pstate->p_relnamespace = NIL;
675                 pstate->p_varnamespace = NIL;
676                 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
677                                           false, true, true);
678                 qry->returningList = transformReturningList(pstate,
679                                                                                                         stmt->returningList);
680         }
681
682         /* done building the range table and jointree */
683         qry->rtable = pstate->p_rtable;
684         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
685
686         qry->hasSubLinks = pstate->p_hasSubLinks;
687         /* aggregates not allowed (but subselects are okay) */
688         if (pstate->p_hasAggs)
689                 ereport(ERROR,
690                                 (errcode(ERRCODE_GROUPING_ERROR),
691                                  errmsg("cannot use aggregate function in VALUES"),
692                                  parser_errposition(pstate,
693                                                                         locate_agg_of_level((Node *) qry, 0))));
694         if (pstate->p_hasWindowFuncs)
695                 ereport(ERROR,
696                                 (errcode(ERRCODE_WINDOWING_ERROR),
697                                  errmsg("cannot use window function in VALUES"),
698                                  parser_errposition(pstate,
699                                                                         locate_windowfunc((Node *) qry))));
700
701         return qry;
702 }
703
704 /*
705  * Prepare an INSERT row for assignment to the target table.
706  *
707  * The row might be either a VALUES row, or variables referencing a
708  * sub-SELECT output.
709  */
710 static List *
711 transformInsertRow(ParseState *pstate, List *exprlist,
712                                    List *stmtcols, List *icolumns, List *attrnos)
713 {
714         List       *result;
715         ListCell   *lc;
716         ListCell   *icols;
717         ListCell   *attnos;
718
719         /*
720          * Check length of expr list.  It must not have more expressions than
721          * there are target columns.  We allow fewer, but only if no explicit
722          * columns list was given (the remaining columns are implicitly
723          * defaulted).  Note we must check this *after* transformation because
724          * that could expand '*' into multiple items.
725          */
726         if (list_length(exprlist) > list_length(icolumns))
727                 ereport(ERROR,
728                                 (errcode(ERRCODE_SYNTAX_ERROR),
729                                  errmsg("INSERT has more expressions than target columns"),
730                                  parser_errposition(pstate,
731                                                                         exprLocation(list_nth(exprlist,
732                                                                                                   list_length(icolumns))))));
733         if (stmtcols != NIL &&
734                 list_length(exprlist) < list_length(icolumns))
735                 ereport(ERROR,
736                                 (errcode(ERRCODE_SYNTAX_ERROR),
737                                  errmsg("INSERT has more target columns than expressions"),
738                                  parser_errposition(pstate,
739                                                                         exprLocation(list_nth(icolumns,
740                                                                                                   list_length(exprlist))))));
741
742         /*
743          * Prepare columns for assignment to target table.
744          */
745         result = NIL;
746         icols = list_head(icolumns);
747         attnos = list_head(attrnos);
748         foreach(lc, exprlist)
749         {
750                 Expr       *expr = (Expr *) lfirst(lc);
751                 ResTarget  *col;
752
753                 col = (ResTarget *) lfirst(icols);
754                 Assert(IsA(col, ResTarget));
755
756                 expr = transformAssignedExpr(pstate, expr,
757                                                                          col->name,
758                                                                          lfirst_int(attnos),
759                                                                          col->indirection,
760                                                                          col->location);
761
762                 result = lappend(result, expr);
763
764                 icols = lnext(icols);
765                 attnos = lnext(attnos);
766         }
767
768         return result;
769 }
770
771
772 /*
773  * transformSelectStmt -
774  *        transforms a Select Statement
775  *
776  * Note: this covers only cases with no set operations and no VALUES lists;
777  * see below for the other cases.
778  */
779 static Query *
780 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
781 {
782         Query      *qry = makeNode(Query);
783         Node       *qual;
784         ListCell   *l;
785
786         qry->commandType = CMD_SELECT;
787
788         /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
789         pstate->p_locking_clause = stmt->lockingClause;
790
791         /* make WINDOW info available for window functions, too */
792         pstate->p_windowdefs = stmt->windowClause;
793
794         /* process the WITH clause */
795         if (stmt->withClause)
796         {
797                 qry->hasRecursive = stmt->withClause->recursive;
798                 qry->cteList = transformWithClause(pstate, stmt->withClause);
799         }
800
801         /* process the FROM clause */
802         transformFromClause(pstate, stmt->fromClause);
803
804         /* transform targetlist */
805         qry->targetList = transformTargetList(pstate, stmt->targetList);
806
807         /* mark column origins */
808         markTargetListOrigins(pstate, qry->targetList);
809
810         /* transform WHERE */
811         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
812
813         /*
814          * Initial processing of HAVING clause is just like WHERE clause.
815          */
816         qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
817                                                                                    "HAVING");
818
819         /*
820          * Transform sorting/grouping stuff.  Do ORDER BY first because both
821          * transformGroupClause and transformDistinctClause need the results. Note
822          * that these functions can also change the targetList, so it's passed to
823          * them by reference.
824          */
825         qry->sortClause = transformSortClause(pstate,
826                                                                                   stmt->sortClause,
827                                                                                   &qry->targetList,
828                                                                                   true /* fix unknowns */,
829                                                                                   false /* not window function */);
830
831         qry->groupClause = transformGroupClause(pstate,
832                                                                                         stmt->groupClause,
833                                                                                         &qry->targetList,
834                                                                                         qry->sortClause,
835                                                                                         false /* not window function */);
836
837         if (stmt->distinctClause == NIL)
838         {
839                 qry->distinctClause = NIL;
840                 qry->hasDistinctOn = false;
841         }
842         else if (linitial(stmt->distinctClause) == NULL)
843         {
844                 /* We had SELECT DISTINCT */
845                 qry->distinctClause = transformDistinctClause(pstate,
846                                                                                                           &qry->targetList,
847                                                                                                           qry->sortClause);
848                 qry->hasDistinctOn = false;
849         }
850         else
851         {
852                 /* We had SELECT DISTINCT ON */
853                 qry->distinctClause = transformDistinctOnClause(pstate,
854                                                                                                                 stmt->distinctClause,
855                                                                                                                 &qry->targetList,
856                                                                                                                 qry->sortClause);
857                 qry->hasDistinctOn = true;
858         }
859
860         /* transform LIMIT */
861         qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
862                                                                                         "OFFSET");
863         qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
864                                                                                    "LIMIT");
865
866         /* transform window clauses after we have seen all window functions */
867         qry->windowClause = transformWindowDefinitions(pstate,
868                                                                                                    pstate->p_windowdefs,
869                                                                                                    &qry->targetList);
870
871         /* handle any SELECT INTO/CREATE TABLE AS spec */
872         if (stmt->intoClause)
873         {
874                 qry->intoClause = stmt->intoClause;
875                 if (stmt->intoClause->colNames)
876                         applyColumnNames(qry->targetList, stmt->intoClause->colNames);
877         }
878
879         qry->rtable = pstate->p_rtable;
880         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
881
882         qry->hasSubLinks = pstate->p_hasSubLinks;
883         qry->hasAggs = pstate->p_hasAggs;
884         if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
885                 parseCheckAggregates(pstate, qry);
886         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
887         if (pstate->p_hasWindowFuncs)
888                 parseCheckWindowFuncs(pstate, qry);
889
890         foreach(l, stmt->lockingClause)
891         {
892                 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
893         }
894
895         return qry;
896 }
897
898 /*
899  * transformValuesClause -
900  *        transforms a VALUES clause that's being used as a standalone SELECT
901  *
902  * We build a Query containing a VALUES RTE, rather as if one had written
903  *                      SELECT * FROM (VALUES ...)
904  */
905 static Query *
906 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
907 {
908         Query      *qry = makeNode(Query);
909         List       *exprsLists = NIL;
910         List      **colexprs = NULL;
911         Oid                *coltypes = NULL;
912         int                     sublist_length = -1;
913         List       *newExprsLists;
914         RangeTblEntry *rte;
915         RangeTblRef *rtr;
916         ListCell   *lc;
917         ListCell   *lc2;
918         int                     i;
919
920         qry->commandType = CMD_SELECT;
921
922         /* Most SELECT stuff doesn't apply in a VALUES clause */
923         Assert(stmt->distinctClause == NIL);
924         Assert(stmt->targetList == NIL);
925         Assert(stmt->fromClause == NIL);
926         Assert(stmt->whereClause == NULL);
927         Assert(stmt->groupClause == NIL);
928         Assert(stmt->havingClause == NULL);
929         Assert(stmt->windowClause == NIL);
930         Assert(stmt->op == SETOP_NONE);
931
932         /* process the WITH clause */
933         if (stmt->withClause)
934         {
935                 qry->hasRecursive = stmt->withClause->recursive;
936                 qry->cteList = transformWithClause(pstate, stmt->withClause);
937         }
938
939         /*
940          * For each row of VALUES, transform the raw expressions and gather type
941          * information.  This is also a handy place to reject DEFAULT nodes, which
942          * the grammar allows for simplicity.
943          */
944         foreach(lc, stmt->valuesLists)
945         {
946                 List       *sublist = (List *) lfirst(lc);
947
948                 /* Do basic expression transformation (same as a ROW() expr) */
949                 sublist = transformExpressionList(pstate, sublist);
950
951                 /*
952                  * All the sublists must be the same length, *after* transformation
953                  * (which might expand '*' into multiple items).  The VALUES RTE can't
954                  * handle anything different.
955                  */
956                 if (sublist_length < 0)
957                 {
958                         /* Remember post-transformation length of first sublist */
959                         sublist_length = list_length(sublist);
960                         /* and allocate arrays for per-column info */
961                         colexprs = (List **) palloc0(sublist_length * sizeof(List *));
962                         coltypes = (Oid *) palloc0(sublist_length * sizeof(Oid));
963                 }
964                 else if (sublist_length != list_length(sublist))
965                 {
966                         ereport(ERROR,
967                                         (errcode(ERRCODE_SYNTAX_ERROR),
968                                          errmsg("VALUES lists must all be the same length"),
969                                          parser_errposition(pstate,
970                                                                                 exprLocation((Node *) sublist))));
971                 }
972
973                 exprsLists = lappend(exprsLists, sublist);
974
975                 /* Check for DEFAULT and build per-column expression lists */
976                 i = 0;
977                 foreach(lc2, sublist)
978                 {
979                         Node       *col = (Node *) lfirst(lc2);
980
981                         if (IsA(col, SetToDefault))
982                                 ereport(ERROR,
983                                                 (errcode(ERRCODE_SYNTAX_ERROR),
984                                                  errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
985                                                  parser_errposition(pstate, exprLocation(col))));
986                         colexprs[i] = lappend(colexprs[i], col);
987                         i++;
988                 }
989         }
990
991         /*
992          * Now resolve the common types of the columns, and coerce everything to
993          * those types.
994          */
995         for (i = 0; i < sublist_length; i++)
996         {
997                 coltypes[i] = select_common_type(pstate, colexprs[i], "VALUES", NULL);
998         }
999
1000         newExprsLists = NIL;
1001         foreach(lc, exprsLists)
1002         {
1003                 List       *sublist = (List *) lfirst(lc);
1004                 List       *newsublist = NIL;
1005
1006                 i = 0;
1007                 foreach(lc2, sublist)
1008                 {
1009                         Node       *col = (Node *) lfirst(lc2);
1010
1011                         col = coerce_to_common_type(pstate, col, coltypes[i], "VALUES");
1012                         newsublist = lappend(newsublist, col);
1013                         i++;
1014                 }
1015
1016                 newExprsLists = lappend(newExprsLists, newsublist);
1017         }
1018
1019         /*
1020          * Generate the VALUES RTE
1021          */
1022         rte = addRangeTableEntryForValues(pstate, newExprsLists, NULL, true);
1023         rtr = makeNode(RangeTblRef);
1024         /* assume new rte is at end */
1025         rtr->rtindex = list_length(pstate->p_rtable);
1026         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1027         pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
1028         pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
1029
1030         /*
1031          * Generate a targetlist as though expanding "*"
1032          */
1033         Assert(pstate->p_next_resno == 1);
1034         qry->targetList = expandRelAttrs(pstate, rte, rtr->rtindex, 0, -1);
1035
1036         /*
1037          * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
1038          * VALUES, so cope.
1039          */
1040         qry->sortClause = transformSortClause(pstate,
1041                                                                                   stmt->sortClause,
1042                                                                                   &qry->targetList,
1043                                                                                   true /* fix unknowns */,
1044                                                                                   false /* not window function */);
1045
1046         qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1047                                                                                         "OFFSET");
1048         qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1049                                                                                    "LIMIT");
1050
1051         if (stmt->lockingClause)
1052                 ereport(ERROR,
1053                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1054                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
1055
1056         /* handle any CREATE TABLE AS spec */
1057         if (stmt->intoClause)
1058         {
1059                 qry->intoClause = stmt->intoClause;
1060                 if (stmt->intoClause->colNames)
1061                         applyColumnNames(qry->targetList, stmt->intoClause->colNames);
1062         }
1063
1064         /*
1065          * There mustn't have been any table references in the expressions, else
1066          * strange things would happen, like Cartesian products of those tables
1067          * with the VALUES list.  We have to check this after parsing ORDER BY et
1068          * al since those could insert more junk.
1069          */
1070         if (list_length(pstate->p_joinlist) != 1)
1071                 ereport(ERROR,
1072                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1073                                  errmsg("VALUES must not contain table references"),
1074                                  parser_errposition(pstate,
1075                                                    locate_var_of_level((Node *) newExprsLists, 0))));
1076
1077         /*
1078          * Another thing we can't currently support is NEW/OLD references in rules
1079          * --- seems we'd need something like SQL99's LATERAL construct to ensure
1080          * that the values would be available while evaluating the VALUES RTE.
1081          * This is a shame.  FIXME
1082          */
1083         if (list_length(pstate->p_rtable) != 1 &&
1084                 contain_vars_of_level((Node *) newExprsLists, 0))
1085                 ereport(ERROR,
1086                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1087                                  errmsg("VALUES must not contain OLD or NEW references"),
1088                                  errhint("Use SELECT ... UNION ALL ... instead."),
1089                                  parser_errposition(pstate,
1090                                                    locate_var_of_level((Node *) newExprsLists, 0))));
1091
1092         qry->rtable = pstate->p_rtable;
1093         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1094
1095         qry->hasSubLinks = pstate->p_hasSubLinks;
1096         /* aggregates not allowed (but subselects are okay) */
1097         if (pstate->p_hasAggs)
1098                 ereport(ERROR,
1099                                 (errcode(ERRCODE_GROUPING_ERROR),
1100                                  errmsg("cannot use aggregate function in VALUES"),
1101                                  parser_errposition(pstate,
1102                                                    locate_agg_of_level((Node *) newExprsLists, 0))));
1103         if (pstate->p_hasWindowFuncs)
1104                 ereport(ERROR,
1105                                 (errcode(ERRCODE_WINDOWING_ERROR),
1106                                  errmsg("cannot use window function in VALUES"),
1107                                  parser_errposition(pstate,
1108                                                                 locate_windowfunc((Node *) newExprsLists))));
1109
1110         return qry;
1111 }
1112
1113 /*
1114  * transformSetOperationStmt -
1115  *        transforms a set-operations tree
1116  *
1117  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
1118  * structure to it.  We must transform each leaf SELECT and build up a top-
1119  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
1120  * The tree of set operations is converted into the setOperations field of
1121  * the top-level Query.
1122  */
1123 static Query *
1124 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
1125 {
1126         Query      *qry = makeNode(Query);
1127         SelectStmt *leftmostSelect;
1128         int                     leftmostRTI;
1129         Query      *leftmostQuery;
1130         SetOperationStmt *sostmt;
1131         List       *socolinfo;
1132         List       *intoColNames = NIL;
1133         List       *sortClause;
1134         Node       *limitOffset;
1135         Node       *limitCount;
1136         List       *lockingClause;
1137         Node       *node;
1138         ListCell   *left_tlist,
1139                            *lct,
1140                            *lcm,
1141                            *l;
1142         List       *targetvars,
1143                            *targetnames,
1144                            *sv_relnamespace,
1145                            *sv_varnamespace;
1146         int                     sv_rtable_length;
1147         RangeTblEntry *jrte;
1148         int                     tllen;
1149
1150         qry->commandType = CMD_SELECT;
1151
1152         /*
1153          * Find leftmost leaf SelectStmt; extract the one-time-only items from it
1154          * and from the top-level node.
1155          */
1156         leftmostSelect = stmt->larg;
1157         while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
1158                 leftmostSelect = leftmostSelect->larg;
1159         Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
1160                    leftmostSelect->larg == NULL);
1161         if (leftmostSelect->intoClause)
1162         {
1163                 qry->intoClause = leftmostSelect->intoClause;
1164                 intoColNames = leftmostSelect->intoClause->colNames;
1165         }
1166
1167         /* clear this to prevent complaints in transformSetOperationTree() */
1168         leftmostSelect->intoClause = NULL;
1169
1170         /*
1171          * These are not one-time, exactly, but we want to process them here and
1172          * not let transformSetOperationTree() see them --- else it'll just
1173          * recurse right back here!
1174          */
1175         sortClause = stmt->sortClause;
1176         limitOffset = stmt->limitOffset;
1177         limitCount = stmt->limitCount;
1178         lockingClause = stmt->lockingClause;
1179
1180         stmt->sortClause = NIL;
1181         stmt->limitOffset = NULL;
1182         stmt->limitCount = NULL;
1183         stmt->lockingClause = NIL;
1184
1185         /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1186         if (lockingClause)
1187                 ereport(ERROR,
1188                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1189                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1190
1191         /* process the WITH clause */
1192         if (stmt->withClause)
1193         {
1194                 qry->hasRecursive = stmt->withClause->recursive;
1195                 qry->cteList = transformWithClause(pstate, stmt->withClause);
1196         }
1197
1198         /*
1199          * Recursively transform the components of the tree.
1200          */
1201         sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
1202                                                                                                                         &socolinfo);
1203         Assert(sostmt && IsA(sostmt, SetOperationStmt));
1204         qry->setOperations = (Node *) sostmt;
1205
1206         /*
1207          * Re-find leftmost SELECT (now it's a sub-query in rangetable)
1208          */
1209         node = sostmt->larg;
1210         while (node && IsA(node, SetOperationStmt))
1211                 node = ((SetOperationStmt *) node)->larg;
1212         Assert(node && IsA(node, RangeTblRef));
1213         leftmostRTI = ((RangeTblRef *) node)->rtindex;
1214         leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
1215         Assert(leftmostQuery != NULL);
1216
1217         /*
1218          * Generate dummy targetlist for outer query using column names of
1219          * leftmost select and common datatypes of topmost set operation. Also
1220          * make lists of the dummy vars and their names for use in parsing ORDER
1221          * BY.
1222          *
1223          * Note: we use leftmostRTI as the varno of the dummy variables. It
1224          * shouldn't matter too much which RT index they have, as long as they
1225          * have one that corresponds to a real RT entry; else funny things may
1226          * happen when the tree is mashed by rule rewriting.
1227          */
1228         qry->targetList = NIL;
1229         targetvars = NIL;
1230         targetnames = NIL;
1231         left_tlist = list_head(leftmostQuery->targetList);
1232
1233         forboth(lct, sostmt->colTypes, lcm, sostmt->colTypmods)
1234         {
1235                 Oid                     colType = lfirst_oid(lct);
1236                 int32           colTypmod = lfirst_int(lcm);
1237                 TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
1238                 char       *colName;
1239                 TargetEntry *tle;
1240                 Var                *var;
1241
1242                 Assert(!lefttle->resjunk);
1243                 colName = pstrdup(lefttle->resname);
1244                 var = makeVar(leftmostRTI,
1245                                           lefttle->resno,
1246                                           colType,
1247                                           colTypmod,
1248                                           0);
1249                 var->location = exprLocation((Node *) lefttle->expr);
1250                 tle = makeTargetEntry((Expr *) var,
1251                                                           (AttrNumber) pstate->p_next_resno++,
1252                                                           colName,
1253                                                           false);
1254                 qry->targetList = lappend(qry->targetList, tle);
1255                 targetvars = lappend(targetvars, var);
1256                 targetnames = lappend(targetnames, makeString(colName));
1257                 left_tlist = lnext(left_tlist);
1258         }
1259
1260         /*
1261          * As a first step towards supporting sort clauses that are expressions
1262          * using the output columns, generate a varnamespace entry that makes the
1263          * output columns visible.      A Join RTE node is handy for this, since we
1264          * can easily control the Vars generated upon matches.
1265          *
1266          * Note: we don't yet do anything useful with such cases, but at least
1267          * "ORDER BY upper(foo)" will draw the right error message rather than
1268          * "foo not found".
1269          */
1270         sv_rtable_length = list_length(pstate->p_rtable);
1271
1272         jrte = addRangeTableEntryForJoin(pstate,
1273                                                                          targetnames,
1274                                                                          JOIN_INNER,
1275                                                                          targetvars,
1276                                                                          NULL,
1277                                                                          false);
1278
1279         sv_relnamespace = pstate->p_relnamespace;
1280         pstate->p_relnamespace = NIL;           /* no qualified names allowed */
1281
1282         sv_varnamespace = pstate->p_varnamespace;
1283         pstate->p_varnamespace = list_make1(jrte);
1284
1285         /*
1286          * For now, we don't support resjunk sort clauses on the output of a
1287          * setOperation tree --- you can only use the SQL92-spec options of
1288          * selecting an output column by name or number.  Enforce by checking that
1289          * transformSortClause doesn't add any items to tlist.
1290          */
1291         tllen = list_length(qry->targetList);
1292
1293         qry->sortClause = transformSortClause(pstate,
1294                                                                                   sortClause,
1295                                                                                   &qry->targetList,
1296                                                                                   false /* no unknowns expected */,
1297                                                                                   false /* not window function */);
1298
1299         pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
1300         pstate->p_relnamespace = sv_relnamespace;
1301         pstate->p_varnamespace = sv_varnamespace;
1302
1303         if (tllen != list_length(qry->targetList))
1304                 ereport(ERROR,
1305                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1306                                  errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
1307                                  errdetail("Only result column names can be used, not expressions or functions."),
1308                                  errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
1309                                  parser_errposition(pstate,
1310                                                    exprLocation(list_nth(qry->targetList, tllen)))));
1311
1312         qry->limitOffset = transformLimitClause(pstate, limitOffset,
1313                                                                                         "OFFSET");
1314         qry->limitCount = transformLimitClause(pstate, limitCount,
1315                                                                                    "LIMIT");
1316
1317         /*
1318          * Handle SELECT INTO/CREATE TABLE AS.
1319          *
1320          * Any column names from CREATE TABLE AS need to be attached to both the
1321          * top level and the leftmost subquery.  We do not do this earlier because
1322          * we do *not* want sortClause processing to be affected.
1323          */
1324         if (intoColNames)
1325         {
1326                 applyColumnNames(qry->targetList, intoColNames);
1327                 applyColumnNames(leftmostQuery->targetList, intoColNames);
1328         }
1329
1330         qry->rtable = pstate->p_rtable;
1331         qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1332
1333         qry->hasSubLinks = pstate->p_hasSubLinks;
1334         qry->hasAggs = pstate->p_hasAggs;
1335         if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
1336                 parseCheckAggregates(pstate, qry);
1337         qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1338         if (pstate->p_hasWindowFuncs)
1339                 parseCheckWindowFuncs(pstate, qry);
1340
1341         foreach(l, lockingClause)
1342         {
1343                 transformLockingClause(pstate, qry, (LockingClause *) lfirst(l));
1344         }
1345
1346         return qry;
1347 }
1348
1349 /*
1350  * transformSetOperationTree
1351  *              Recursively transform leaves and internal nodes of a set-op tree
1352  *
1353  * In addition to returning the transformed node, we return a list of
1354  * expression nodes showing the type, typmod, and location (for error messages)
1355  * of each output column of the set-op node.  This is used only during the
1356  * internal recursion of this function.  At the upper levels we use
1357  * SetToDefault nodes for this purpose, since they carry exactly the fields
1358  * needed, but any other expression node type would do as well.
1359  */
1360 static Node *
1361 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
1362                                                   List **colInfo)
1363 {
1364         bool            isLeaf;
1365
1366         Assert(stmt && IsA(stmt, SelectStmt));
1367
1368         /*
1369          * Validity-check both leaf and internal SELECTs for disallowed ops.
1370          */
1371         if (stmt->intoClause)
1372                 ereport(ERROR,
1373                                 (errcode(ERRCODE_SYNTAX_ERROR),
1374                                  errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
1375                                  parser_errposition(pstate,
1376                                                                   exprLocation((Node *) stmt->intoClause))));
1377
1378         /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
1379         if (stmt->lockingClause)
1380                 ereport(ERROR,
1381                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1382                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1383
1384         /*
1385          * If an internal node of a set-op tree has ORDER BY, UPDATE, or LIMIT
1386          * clauses attached, we need to treat it like a leaf node to generate an
1387          * independent sub-Query tree.  Otherwise, it can be represented by a
1388          * SetOperationStmt node underneath the parent Query.
1389          */
1390         if (stmt->op == SETOP_NONE)
1391         {
1392                 Assert(stmt->larg == NULL && stmt->rarg == NULL);
1393                 isLeaf = true;
1394         }
1395         else
1396         {
1397                 Assert(stmt->larg != NULL && stmt->rarg != NULL);
1398                 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
1399                         stmt->lockingClause)
1400                         isLeaf = true;
1401                 else
1402                         isLeaf = false;
1403         }
1404
1405         if (isLeaf)
1406         {
1407                 /* Process leaf SELECT */
1408                 Query      *selectQuery;
1409                 char            selectName[32];
1410                 RangeTblEntry *rte;
1411                 RangeTblRef *rtr;
1412                 ListCell   *tl;
1413
1414                 /*
1415                  * Transform SelectStmt into a Query.
1416                  *
1417                  * Note: previously transformed sub-queries don't affect the parsing
1418                  * of this sub-query, because they are not in the toplevel pstate's
1419                  * namespace list.
1420                  */
1421                 selectQuery = parse_sub_analyze((Node *) stmt, pstate);
1422
1423                 /*
1424                  * Check for bogus references to Vars on the current query level (but
1425                  * upper-level references are okay). Normally this can't happen
1426                  * because the namespace will be empty, but it could happen if we are
1427                  * inside a rule.
1428                  */
1429                 if (pstate->p_relnamespace || pstate->p_varnamespace)
1430                 {
1431                         if (contain_vars_of_level((Node *) selectQuery, 1))
1432                                 ereport(ERROR,
1433                                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1434                                                  errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
1435                                                  parser_errposition(pstate,
1436                                                          locate_var_of_level((Node *) selectQuery, 1))));
1437                 }
1438
1439                 /*
1440                  * Extract a list of the result expressions for upper-level checking.
1441                  */
1442                 *colInfo = NIL;
1443                 foreach(tl, selectQuery->targetList)
1444                 {
1445                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
1446
1447                         if (!tle->resjunk)
1448                                 *colInfo = lappend(*colInfo, tle->expr);
1449                 }
1450
1451                 /*
1452                  * Make the leaf query be a subquery in the top-level rangetable.
1453                  */
1454                 snprintf(selectName, sizeof(selectName), "*SELECT* %d",
1455                                  list_length(pstate->p_rtable) + 1);
1456                 rte = addRangeTableEntryForSubquery(pstate,
1457                                                                                         selectQuery,
1458                                                                                         makeAlias(selectName, NIL),
1459                                                                                         false);
1460
1461                 /*
1462                  * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
1463                  */
1464                 rtr = makeNode(RangeTblRef);
1465                 /* assume new rte is at end */
1466                 rtr->rtindex = list_length(pstate->p_rtable);
1467                 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
1468                 return (Node *) rtr;
1469         }
1470         else
1471         {
1472                 /* Process an internal node (set operation node) */
1473                 SetOperationStmt *op = makeNode(SetOperationStmt);
1474                 List       *lcolinfo;
1475                 List       *rcolinfo;
1476                 ListCell   *lci;
1477                 ListCell   *rci;
1478                 const char *context;
1479
1480                 context = (stmt->op == SETOP_UNION ? "UNION" :
1481                                    (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
1482                                         "EXCEPT"));
1483
1484                 op->op = stmt->op;
1485                 op->all = stmt->all;
1486
1487                 /*
1488                  * Recursively transform the child nodes.
1489                  */
1490                 op->larg = transformSetOperationTree(pstate, stmt->larg,
1491                                                                                          &lcolinfo);
1492                 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
1493                                                                                          &rcolinfo);
1494
1495                 /*
1496                  * Verify that the two children have the same number of non-junk
1497                  * columns, and determine the types of the merged output columns.
1498                  */
1499                 if (list_length(lcolinfo) != list_length(rcolinfo))
1500                         ereport(ERROR,
1501                                         (errcode(ERRCODE_SYNTAX_ERROR),
1502                                  errmsg("each %s query must have the same number of columns",
1503                                                 context),
1504                                          parser_errposition(pstate,
1505                                                                                 exprLocation((Node *) rcolinfo))));
1506
1507                 *colInfo = NIL;
1508                 op->colTypes = NIL;
1509                 op->colTypmods = NIL;
1510                 op->groupClauses = NIL;
1511                 forboth(lci, lcolinfo, rci, rcolinfo)
1512                 {
1513                         Node       *lcolinfo = (Node *) lfirst(lci);
1514                         Node       *rcolinfo = (Node *) lfirst(rci);
1515                         Oid                     lcoltype = exprType(lcolinfo);
1516                         Oid                     rcoltype = exprType(rcolinfo);
1517                         int32           lcoltypmod = exprTypmod(lcolinfo);
1518                         int32           rcoltypmod = exprTypmod(rcolinfo);
1519                         Node       *bestexpr;
1520                         SetToDefault *rescolinfo;
1521                         Oid                     rescoltype;
1522                         int32           rescoltypmod;
1523
1524                         /* select common type, same as CASE et al */
1525                         rescoltype = select_common_type(pstate,
1526                                                                                         list_make2(lcolinfo, rcolinfo),
1527                                                                                         context,
1528                                                                                         &bestexpr);
1529                         /* if same type and same typmod, use typmod; else default */
1530                         if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
1531                                 rescoltypmod = lcoltypmod;
1532                         else
1533                                 rescoltypmod = -1;
1534
1535                         /* verify the coercions are actually possible */
1536                         (void) coerce_to_common_type(pstate, lcolinfo,
1537                                                                                  rescoltype, context);
1538                         (void) coerce_to_common_type(pstate, rcolinfo,
1539                                                                                  rescoltype, context);
1540
1541                         /* emit results */
1542                         rescolinfo = makeNode(SetToDefault);
1543                         rescolinfo->typeId = rescoltype;
1544                         rescolinfo->typeMod = rescoltypmod;
1545                         rescolinfo->location = exprLocation(bestexpr);
1546                         *colInfo = lappend(*colInfo, rescolinfo);
1547
1548                         op->colTypes = lappend_oid(op->colTypes, rescoltype);
1549                         op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
1550
1551                         /*
1552                          * For all cases except UNION ALL, identify the grouping operators
1553                          * (and, if available, sorting operators) that will be used to
1554                          * eliminate duplicates.
1555                          */
1556                         if (op->op != SETOP_UNION || !op->all)
1557                         {
1558                                 SortGroupClause *grpcl = makeNode(SortGroupClause);
1559                                 Oid                     sortop;
1560                                 Oid                     eqop;
1561                                 ParseCallbackState pcbstate;
1562
1563                                 setup_parser_errposition_callback(&pcbstate, pstate,
1564                                                                                                   rescolinfo->location);
1565
1566                                 /* determine the eqop and optional sortop */
1567                                 get_sort_group_operators(rescoltype,
1568                                                                                  false, true, false,
1569                                                                                  &sortop, &eqop, NULL);
1570
1571                                 cancel_parser_errposition_callback(&pcbstate);
1572
1573                                 /* we don't have a tlist yet, so can't assign sortgrouprefs */
1574                                 grpcl->tleSortGroupRef = 0;
1575                                 grpcl->eqop = eqop;
1576                                 grpcl->sortop = sortop;
1577                                 grpcl->nulls_first = false;             /* OK with or without sortop */
1578
1579                                 op->groupClauses = lappend(op->groupClauses, grpcl);
1580                         }
1581                 }
1582
1583                 return (Node *) op;
1584         }
1585 }
1586
1587 /*
1588  * Attach column names from a ColumnDef list to a TargetEntry list
1589  * (for CREATE TABLE AS)
1590  */
1591 static void
1592 applyColumnNames(List *dst, List *src)
1593 {
1594         ListCell   *dst_item;
1595         ListCell   *src_item;
1596
1597         src_item = list_head(src);
1598
1599         foreach(dst_item, dst)
1600         {
1601                 TargetEntry *d = (TargetEntry *) lfirst(dst_item);
1602                 ColumnDef  *s;
1603
1604                 /* junk targets don't count */
1605                 if (d->resjunk)
1606                         continue;
1607
1608                 /* fewer ColumnDefs than target entries is OK */
1609                 if (src_item == NULL)
1610                         break;
1611
1612                 s = (ColumnDef *) lfirst(src_item);
1613                 src_item = lnext(src_item);
1614
1615                 d->resname = pstrdup(s->colname);
1616         }
1617
1618         /* more ColumnDefs than target entries is not OK */
1619         if (src_item != NULL)
1620                 ereport(ERROR,
1621                                 (errcode(ERRCODE_SYNTAX_ERROR),
1622                                  errmsg("CREATE TABLE AS specifies too many column names")));
1623 }
1624
1625
1626 /*
1627  * transformUpdateStmt -
1628  *        transforms an update statement
1629  */
1630 static Query *
1631 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
1632 {
1633         Query      *qry = makeNode(Query);
1634         RangeTblEntry *target_rte;
1635         Node       *qual;
1636         ListCell   *origTargetList;
1637         ListCell   *tl;
1638
1639         qry->commandType = CMD_UPDATE;
1640         pstate->p_is_update = true;
1641
1642         qry->resultRelation = setTargetTable(pstate, stmt->relation,
1643                                                                   interpretInhOption(stmt->relation->inhOpt),
1644                                                                                  true,
1645                                                                                  ACL_UPDATE);
1646
1647         /*
1648          * the FROM clause is non-standard SQL syntax. We used to be able to do
1649          * this with REPLACE in POSTQUEL so we keep the feature.
1650          */
1651         transformFromClause(pstate, stmt->fromClause);
1652
1653         qry->targetList = transformTargetList(pstate, stmt->targetList);
1654
1655         qual = transformWhereClause(pstate, stmt->whereClause, "WHERE");
1656
1657         qry->returningList = transformReturningList(pstate, stmt->returningList);
1658
1659         qry->rtable = pstate->p_rtable;
1660         qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1661
1662         qry->hasSubLinks = pstate->p_hasSubLinks;
1663
1664         /*
1665          * Top-level aggregates are simply disallowed in UPDATE, per spec. (From
1666          * an implementation point of view, this is forced because the implicit
1667          * ctid reference would otherwise be an ungrouped variable.)
1668          */
1669         if (pstate->p_hasAggs)
1670                 ereport(ERROR,
1671                                 (errcode(ERRCODE_GROUPING_ERROR),
1672                                  errmsg("cannot use aggregate function in UPDATE"),
1673                                  parser_errposition(pstate,
1674                                                                         locate_agg_of_level((Node *) qry, 0))));
1675         if (pstate->p_hasWindowFuncs)
1676                 ereport(ERROR,
1677                                 (errcode(ERRCODE_WINDOWING_ERROR),
1678                                  errmsg("cannot use window function in UPDATE"),
1679                                  parser_errposition(pstate,
1680                                                                         locate_windowfunc((Node *) qry))));
1681
1682         /*
1683          * Now we are done with SELECT-like processing, and can get on with
1684          * transforming the target list to match the UPDATE target columns.
1685          */
1686
1687         /* Prepare to assign non-conflicting resnos to resjunk attributes */
1688         if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
1689                 pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
1690
1691         /* Prepare non-junk columns for assignment to target table */
1692         target_rte = pstate->p_target_rangetblentry;
1693         origTargetList = list_head(stmt->targetList);
1694
1695         foreach(tl, qry->targetList)
1696         {
1697                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1698                 ResTarget  *origTarget;
1699                 int                     attrno;
1700
1701                 if (tle->resjunk)
1702                 {
1703                         /*
1704                          * Resjunk nodes need no additional processing, but be sure they
1705                          * have resnos that do not match any target columns; else rewriter
1706                          * or planner might get confused.  They don't need a resname
1707                          * either.
1708                          */
1709                         tle->resno = (AttrNumber) pstate->p_next_resno++;
1710                         tle->resname = NULL;
1711                         continue;
1712                 }
1713                 if (origTargetList == NULL)
1714                         elog(ERROR, "UPDATE target count mismatch --- internal error");
1715                 origTarget = (ResTarget *) lfirst(origTargetList);
1716                 Assert(IsA(origTarget, ResTarget));
1717
1718                 attrno = attnameAttNum(pstate->p_target_relation,
1719                                                            origTarget->name, true);
1720                 if (attrno == InvalidAttrNumber)
1721                         ereport(ERROR,
1722                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
1723                                          errmsg("column \"%s\" of relation \"%s\" does not exist",
1724                                                         origTarget->name,
1725                                                  RelationGetRelationName(pstate->p_target_relation)),
1726                                          parser_errposition(pstate, origTarget->location)));
1727
1728                 updateTargetListEntry(pstate, tle, origTarget->name,
1729                                                           attrno,
1730                                                           origTarget->indirection,
1731                                                           origTarget->location);
1732
1733                 /* Mark the target column as requiring update permissions */
1734                 target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
1735                                                                 attrno - FirstLowInvalidHeapAttributeNumber);
1736
1737                 origTargetList = lnext(origTargetList);
1738         }
1739         if (origTargetList != NULL)
1740                 elog(ERROR, "UPDATE target count mismatch --- internal error");
1741
1742         return qry;
1743 }
1744
1745 /*
1746  * transformReturningList -
1747  *      handle a RETURNING clause in INSERT/UPDATE/DELETE
1748  */
1749 static List *
1750 transformReturningList(ParseState *pstate, List *returningList)
1751 {
1752         List       *rlist;
1753         int                     save_next_resno;
1754         bool            save_hasAggs;
1755         bool            save_hasWindowFuncs;
1756         int                     length_rtable;
1757
1758         if (returningList == NIL)
1759                 return NIL;                             /* nothing to do */
1760
1761         /*
1762          * We need to assign resnos starting at one in the RETURNING list. Save
1763          * and restore the main tlist's value of p_next_resno, just in case
1764          * someone looks at it later (probably won't happen).
1765          */
1766         save_next_resno = pstate->p_next_resno;
1767         pstate->p_next_resno = 1;
1768
1769         /* save other state so that we can detect disallowed stuff */
1770         save_hasAggs = pstate->p_hasAggs;
1771         pstate->p_hasAggs = false;
1772         save_hasWindowFuncs = pstate->p_hasWindowFuncs;
1773         pstate->p_hasWindowFuncs = false;
1774         length_rtable = list_length(pstate->p_rtable);
1775
1776         /* transform RETURNING identically to a SELECT targetlist */
1777         rlist = transformTargetList(pstate, returningList);
1778
1779         /* check for disallowed stuff */
1780
1781         /* aggregates not allowed (but subselects are okay) */
1782         if (pstate->p_hasAggs)
1783                 ereport(ERROR,
1784                                 (errcode(ERRCODE_GROUPING_ERROR),
1785                                  errmsg("cannot use aggregate function in RETURNING"),
1786                                  parser_errposition(pstate,
1787                                                                         locate_agg_of_level((Node *) rlist, 0))));
1788         if (pstate->p_hasWindowFuncs)
1789                 ereport(ERROR,
1790                                 (errcode(ERRCODE_WINDOWING_ERROR),
1791                                  errmsg("cannot use window function in RETURNING"),
1792                                  parser_errposition(pstate,
1793                                                                         locate_windowfunc((Node *) rlist))));
1794
1795         /* no new relation references please */
1796         if (list_length(pstate->p_rtable) != length_rtable)
1797         {
1798                 int                     vlocation = -1;
1799                 int                     relid;
1800
1801                 /* try to locate such a reference to point to */
1802                 for (relid = length_rtable + 1; relid <= list_length(pstate->p_rtable); relid++)
1803                 {
1804                         vlocation = locate_var_of_relation((Node *) rlist, relid, 0);
1805                         if (vlocation >= 0)
1806                                 break;
1807                 }
1808                 ereport(ERROR,
1809                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1810                         errmsg("RETURNING cannot contain references to other relations"),
1811                                  parser_errposition(pstate, vlocation)));
1812         }
1813
1814         /* mark column origins */
1815         markTargetListOrigins(pstate, rlist);
1816
1817         /* restore state */
1818         pstate->p_next_resno = save_next_resno;
1819         pstate->p_hasAggs = save_hasAggs;
1820         pstate->p_hasWindowFuncs = save_hasWindowFuncs;
1821
1822         return rlist;
1823 }
1824
1825
1826 /*
1827  * transformDeclareCursorStmt -
1828  *      transform a DECLARE CURSOR Statement
1829  *
1830  * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
1831  * significantly different from a SELECT) as far as parsing/rewriting/planning
1832  * are concerned, but it's not passed to the executor and so in that sense is
1833  * a utility statement.  We transform it into a Query exactly as if it were
1834  * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
1835  * field to carry the cursor name and options.
1836  */
1837 static Query *
1838 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
1839 {
1840         Query      *result;
1841
1842         /*
1843          * Don't allow both SCROLL and NO SCROLL to be specified
1844          */
1845         if ((stmt->options & CURSOR_OPT_SCROLL) &&
1846                 (stmt->options & CURSOR_OPT_NO_SCROLL))
1847                 ereport(ERROR,
1848                                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1849                                  errmsg("cannot specify both SCROLL and NO SCROLL")));
1850
1851         result = transformStmt(pstate, stmt->query);
1852
1853         /* Grammar should not have allowed anything but SELECT */
1854         if (!IsA(result, Query) ||
1855                 result->commandType != CMD_SELECT ||
1856                 result->utilityStmt != NULL)
1857                 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
1858
1859         /* But we must explicitly disallow DECLARE CURSOR ... SELECT INTO */
1860         if (result->intoClause)
1861                 ereport(ERROR,
1862                                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1863                                  errmsg("DECLARE CURSOR cannot specify INTO"),
1864                                  parser_errposition(pstate,
1865                                                                 exprLocation((Node *) result->intoClause))));
1866
1867         /* FOR UPDATE and WITH HOLD are not compatible */
1868         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
1869                 ereport(ERROR,
1870                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1871                                  errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
1872                                  errdetail("Holdable cursors must be READ ONLY.")));
1873
1874         /* FOR UPDATE and SCROLL are not compatible */
1875         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
1876                 ereport(ERROR,
1877                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1878                 errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
1879                                  errdetail("Scrollable cursors must be READ ONLY.")));
1880
1881         /* FOR UPDATE and INSENSITIVE are not compatible */
1882         if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
1883                 ereport(ERROR,
1884                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1885                                  errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
1886                                  errdetail("Insensitive cursors must be READ ONLY.")));
1887
1888         /* We won't need the raw querytree any more */
1889         stmt->query = NULL;
1890
1891         result->utilityStmt = (Node *) stmt;
1892
1893         return result;
1894 }
1895
1896
1897 /*
1898  * transformExplainStmt -
1899  *      transform an EXPLAIN Statement
1900  *
1901  * EXPLAIN is just like other utility statements in that we emit it as a
1902  * CMD_UTILITY Query node with no transformation of the raw parse tree.
1903  * However, if p_variableparams is set, it could be that the client is
1904  * expecting us to resolve parameter types in something like
1905  *              EXPLAIN SELECT * FROM tab WHERE col = $1
1906  * To deal with such cases, we run parse analysis and throw away the result;
1907  * this is a bit grotty but not worth contorting the rest of the system for.
1908  * (The approach we use for DECLARE CURSOR won't work because the statement
1909  * being explained isn't necessarily a SELECT, and in particular might rewrite
1910  * to multiple parsetrees.)
1911  */
1912 static Query *
1913 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
1914 {
1915         Query      *result;
1916
1917         if (pstate->p_variableparams)
1918         {
1919                 /* Since parse analysis scribbles on its input, copy the tree first! */
1920                 (void) transformStmt(pstate, copyObject(stmt->query));
1921         }
1922
1923         /* Now return the untransformed command as a utility Query */
1924         result = makeNode(Query);
1925         result->commandType = CMD_UTILITY;
1926         result->utilityStmt = (Node *) stmt;
1927
1928         return result;
1929 }
1930
1931
1932 /* exported so planner can check again after rewriting, query pullup, etc */
1933 void
1934 CheckSelectLocking(Query *qry)
1935 {
1936         if (qry->setOperations)
1937                 ereport(ERROR,
1938                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1939                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1940         if (qry->distinctClause != NIL)
1941                 ereport(ERROR,
1942                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1943                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with DISTINCT clause")));
1944         if (qry->groupClause != NIL)
1945                 ereport(ERROR,
1946                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1947                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with GROUP BY clause")));
1948         if (qry->havingQual != NULL)
1949                 ereport(ERROR,
1950                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1951                 errmsg("SELECT FOR UPDATE/SHARE is not allowed with HAVING clause")));
1952         if (qry->hasAggs)
1953                 ereport(ERROR,
1954                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1955                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with aggregate functions")));
1956         if (qry->hasWindowFuncs)
1957                 ereport(ERROR,
1958                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1959                                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with window functions")));
1960 }
1961
1962 /*
1963  * Transform a FOR UPDATE/SHARE clause
1964  *
1965  * This basically involves replacing names by integer relids.
1966  *
1967  * NB: if you need to change this, see also markQueryForLocking()
1968  * in rewriteHandler.c, and isLockedRel() in parse_relation.c.
1969  */
1970 static void
1971 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc)
1972 {
1973         List       *lockedRels = lc->lockedRels;
1974         ListCell   *l;
1975         ListCell   *rt;
1976         Index           i;
1977         LockingClause *allrels;
1978
1979         CheckSelectLocking(qry);
1980
1981         /* make a clause we can pass down to subqueries to select all rels */
1982         allrels = makeNode(LockingClause);
1983         allrels->lockedRels = NIL;      /* indicates all rels */
1984         allrels->forUpdate = lc->forUpdate;
1985         allrels->noWait = lc->noWait;
1986
1987         if (lockedRels == NIL)
1988         {
1989                 /* all regular tables used in query */
1990                 i = 0;
1991                 foreach(rt, qry->rtable)
1992                 {
1993                         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
1994
1995                         ++i;
1996                         switch (rte->rtekind)
1997                         {
1998                                 case RTE_RELATION:
1999                                         applyLockingClause(qry, i, lc->forUpdate, lc->noWait);
2000                                         rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2001                                         break;
2002                                 case RTE_SUBQUERY:
2003
2004                                         /*
2005                                          * FOR UPDATE/SHARE of subquery is propagated to all of
2006                                          * subquery's rels
2007                                          */
2008                                         transformLockingClause(pstate, rte->subquery, allrels);
2009                                         break;
2010                                 case RTE_CTE:
2011                                         {
2012                                                 /*
2013                                                  * We allow FOR UPDATE/SHARE of a WITH query to be
2014                                                  * propagated into the WITH, but it doesn't seem very
2015                                                  * sane to allow this for a reference to an
2016                                                  * outer-level WITH.  And it definitely wouldn't work
2017                                                  * for a self-reference, since we're not done
2018                                                  * analyzing the CTE anyway.
2019                                                  */
2020                                                 CommonTableExpr *cte;
2021
2022                                                 if (rte->ctelevelsup > 0 || rte->self_reference)
2023                                                         ereport(ERROR,
2024                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2025                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query")));
2026                                                 cte = GetCTEForRTE(pstate, rte, -1);
2027                                                 /* should be analyzed by now */
2028                                                 Assert(IsA(cte->ctequery, Query));
2029                                                 transformLockingClause(pstate,
2030                                                                                            (Query *) cte->ctequery,
2031                                                                                            allrels);
2032                                         }
2033                                         break;
2034                                 default:
2035                                         /* ignore JOIN, SPECIAL, FUNCTION RTEs */
2036                                         break;
2037                         }
2038                 }
2039         }
2040         else
2041         {
2042                 /* just the named tables */
2043                 foreach(l, lockedRels)
2044                 {
2045                         RangeVar   *thisrel = (RangeVar *) lfirst(l);
2046
2047                         /* For simplicity we insist on unqualified alias names here */
2048                         if (thisrel->catalogname || thisrel->schemaname)
2049                                 ereport(ERROR,
2050                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2051                                                  errmsg("SELECT FOR UPDATE/SHARE must specify unqualified relation names"),
2052                                                  parser_errposition(pstate, thisrel->location)));
2053
2054                         i = 0;
2055                         foreach(rt, qry->rtable)
2056                         {
2057                                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2058
2059                                 ++i;
2060                                 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
2061                                 {
2062                                         switch (rte->rtekind)
2063                                         {
2064                                                 case RTE_RELATION:
2065                                                         applyLockingClause(qry, i,
2066                                                                                            lc->forUpdate, lc->noWait);
2067                                                         rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
2068                                                         break;
2069                                                 case RTE_SUBQUERY:
2070
2071                                                         /*
2072                                                          * FOR UPDATE/SHARE of subquery is propagated to
2073                                                          * all of subquery's rels
2074                                                          */
2075                                                         transformLockingClause(pstate, rte->subquery, allrels);
2076                                                         break;
2077                                                 case RTE_JOIN:
2078                                                         ereport(ERROR,
2079                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2080                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a join"),
2081                                                          parser_errposition(pstate, thisrel->location)));
2082                                                         break;
2083                                                 case RTE_SPECIAL:
2084                                                         ereport(ERROR,
2085                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2086                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to NEW or OLD"),
2087                                                          parser_errposition(pstate, thisrel->location)));
2088                                                         break;
2089                                                 case RTE_FUNCTION:
2090                                                         ereport(ERROR,
2091                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2092                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to a function"),
2093                                                          parser_errposition(pstate, thisrel->location)));
2094                                                         break;
2095                                                 case RTE_VALUES:
2096                                                         ereport(ERROR,
2097                                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2098                                                                          errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES"),
2099                                                          parser_errposition(pstate, thisrel->location)));
2100                                                         break;
2101                                                 case RTE_CTE:
2102                                                         {
2103                                                                 /*
2104                                                                  * We allow FOR UPDATE/SHARE of a WITH query
2105                                                                  * to be propagated into the WITH, but it
2106                                                                  * doesn't seem very sane to allow this for a
2107                                                                  * reference to an outer-level WITH.  And it
2108                                                                  * definitely wouldn't work for a
2109                                                                  * self-reference, since we're not done
2110                                                                  * analyzing the CTE anyway.
2111                                                                  */
2112                                                                 CommonTableExpr *cte;
2113
2114                                                                 if (rte->ctelevelsup > 0 || rte->self_reference)
2115                                                                         ereport(ERROR,
2116                                                                          (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2117                                                                           errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query"),
2118                                                                           parser_errposition(pstate, thisrel->location)));
2119                                                                 cte = GetCTEForRTE(pstate, rte, -1);
2120                                                                 /* should be analyzed by now */
2121                                                                 Assert(IsA(cte->ctequery, Query));
2122                                                                 transformLockingClause(pstate,
2123                                                                                                          (Query *) cte->ctequery,
2124                                                                                                            allrels);
2125                                                         }
2126                                                         break;
2127                                                 default:
2128                                                         elog(ERROR, "unrecognized RTE type: %d",
2129                                                                  (int) rte->rtekind);
2130                                                         break;
2131                                         }
2132                                         break;          /* out of foreach loop */
2133                                 }
2134                         }
2135                         if (rt == NULL)
2136                                 ereport(ERROR,
2137                                                 (errcode(ERRCODE_UNDEFINED_TABLE),
2138                                                  errmsg("relation \"%s\" in FOR UPDATE/SHARE clause not found in FROM clause",
2139                                                                 thisrel->relname),
2140                                                  parser_errposition(pstate, thisrel->location)));
2141                 }
2142         }
2143 }
2144
2145 /*
2146  * Record locking info for a single rangetable item
2147  */
2148 void
2149 applyLockingClause(Query *qry, Index rtindex, bool forUpdate, bool noWait)
2150 {
2151         RowMarkClause *rc;
2152
2153         /* Check for pre-existing entry for same rtindex */
2154         if ((rc = get_rowmark(qry, rtindex)) != NULL)
2155         {
2156                 /*
2157                  * If the same RTE is specified both FOR UPDATE and FOR SHARE, treat
2158                  * it as FOR UPDATE.  (Reasonable, since you can't take both a shared
2159                  * and exclusive lock at the same time; it'll end up being exclusive
2160                  * anyway.)
2161                  *
2162                  * We also consider that NOWAIT wins if it's specified both ways. This
2163                  * is a bit more debatable but raising an error doesn't seem helpful.
2164                  * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
2165                  * internally contains a plain FOR UPDATE spec.)
2166                  */
2167                 rc->forUpdate |= forUpdate;
2168                 rc->noWait |= noWait;
2169                 return;
2170         }
2171
2172         /* Make a new RowMarkClause */
2173         rc = makeNode(RowMarkClause);
2174         rc->rti = rtindex;
2175         rc->prti = rtindex;
2176         rc->forUpdate = forUpdate;
2177         rc->noWait = noWait;
2178         rc->isParent = false;
2179         qry->rowMarks = lappend(qry->rowMarks, rc);
2180 }
2181
2182
2183 /*
2184  * Traverse a fully-analyzed tree to verify that parameter symbols
2185  * match their types.  We need this because some Params might still
2186  * be UNKNOWN, if there wasn't anything to force their coercion,
2187  * and yet other instances seen later might have gotten coerced.
2188  */
2189 static bool
2190 check_parameter_resolution_walker(Node *node, ParseState *pstate)
2191 {
2192         if (node == NULL)
2193                 return false;
2194         if (IsA(node, Param))
2195         {
2196                 Param      *param = (Param *) node;
2197
2198                 if (param->paramkind == PARAM_EXTERN)
2199                 {
2200                         int                     paramno = param->paramid;
2201
2202                         if (paramno <= 0 || /* shouldn't happen, but... */
2203                                 paramno > pstate->p_numparams)
2204                                 ereport(ERROR,
2205                                                 (errcode(ERRCODE_UNDEFINED_PARAMETER),
2206                                                  errmsg("there is no parameter $%d", paramno),
2207                                                  parser_errposition(pstate, param->location)));
2208
2209                         if (param->paramtype != pstate->p_paramtypes[paramno - 1])
2210                                 ereport(ERROR,
2211                                                 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
2212                                          errmsg("could not determine data type of parameter $%d",
2213                                                         paramno),
2214                                                  parser_errposition(pstate, param->location)));
2215                 }
2216                 return false;
2217         }
2218         if (IsA(node, Query))
2219         {
2220                 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
2221                 return query_tree_walker((Query *) node,
2222                                                                  check_parameter_resolution_walker,
2223                                                                  (void *) pstate, 0);
2224         }
2225         return expression_tree_walker(node, check_parameter_resolution_walker,
2226                                                                   (void *) pstate);
2227 }