Header And Logo

PostgreSQL
| The world's most advanced open source database.

analyze.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * analyze.c
00004  *    transform the raw parse tree into a query tree
00005  *
00006  * For optimizable statements, we are careful to obtain a suitable lock on
00007  * each referenced table, and other modules of the backend preserve or
00008  * re-obtain these locks before depending on the results.  It is therefore
00009  * okay to do significant semantic analysis of these statements.  For
00010  * utility commands, no locks are obtained here (and if they were, we could
00011  * not be sure we'd still have them at execution).  Hence the general rule
00012  * for utility commands is to just dump them into a Query node untransformed.
00013  * DECLARE CURSOR, EXPLAIN, and CREATE TABLE AS are exceptions because they
00014  * contain optimizable statements, which we should transform.
00015  *
00016  *
00017  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00018  * Portions Copyright (c) 1994, Regents of the University of California
00019  *
00020  *  src/backend/parser/analyze.c
00021  *
00022  *-------------------------------------------------------------------------
00023  */
00024 
00025 #include "postgres.h"
00026 
00027 #include "access/sysattr.h"
00028 #include "catalog/pg_type.h"
00029 #include "miscadmin.h"
00030 #include "nodes/makefuncs.h"
00031 #include "nodes/nodeFuncs.h"
00032 #include "optimizer/var.h"
00033 #include "parser/analyze.h"
00034 #include "parser/parse_agg.h"
00035 #include "parser/parse_clause.h"
00036 #include "parser/parse_coerce.h"
00037 #include "parser/parse_collate.h"
00038 #include "parser/parse_cte.h"
00039 #include "parser/parse_oper.h"
00040 #include "parser/parse_param.h"
00041 #include "parser/parse_relation.h"
00042 #include "parser/parse_target.h"
00043 #include "parser/parsetree.h"
00044 #include "rewrite/rewriteManip.h"
00045 #include "utils/rel.h"
00046 
00047 
00048 /* Hook for plugins to get control at end of parse analysis */
00049 post_parse_analyze_hook_type post_parse_analyze_hook = NULL;
00050 
00051 static Query *transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt);
00052 static Query *transformInsertStmt(ParseState *pstate, InsertStmt *stmt);
00053 static List *transformInsertRow(ParseState *pstate, List *exprlist,
00054                    List *stmtcols, List *icolumns, List *attrnos);
00055 static int  count_rowexpr_columns(ParseState *pstate, Node *expr);
00056 static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt);
00057 static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt);
00058 static Query *transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt);
00059 static Node *transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
00060                           bool isTopLevel, List **targetlist);
00061 static void determineRecursiveColTypes(ParseState *pstate,
00062                            Node *larg, List *nrtargetlist);
00063 static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt);
00064 static List *transformReturningList(ParseState *pstate, List *returningList);
00065 static Query *transformDeclareCursorStmt(ParseState *pstate,
00066                            DeclareCursorStmt *stmt);
00067 static Query *transformExplainStmt(ParseState *pstate,
00068                      ExplainStmt *stmt);
00069 static Query *transformCreateTableAsStmt(ParseState *pstate,
00070                            CreateTableAsStmt *stmt);
00071 static void transformLockingClause(ParseState *pstate, Query *qry,
00072                        LockingClause *lc, bool pushedDown);
00073 
00074 
00075 /*
00076  * parse_analyze
00077  *      Analyze a raw parse tree and transform it to Query form.
00078  *
00079  * Optionally, information about $n parameter types can be supplied.
00080  * References to $n indexes not defined by paramTypes[] are disallowed.
00081  *
00082  * The result is a Query node.  Optimizable statements require considerable
00083  * transformation, while utility-type statements are simply hung off
00084  * a dummy CMD_UTILITY Query node.
00085  */
00086 Query *
00087 parse_analyze(Node *parseTree, const char *sourceText,
00088               Oid *paramTypes, int numParams)
00089 {
00090     ParseState *pstate = make_parsestate(NULL);
00091     Query      *query;
00092 
00093     Assert(sourceText != NULL); /* required as of 8.4 */
00094 
00095     pstate->p_sourcetext = sourceText;
00096 
00097     if (numParams > 0)
00098         parse_fixed_parameters(pstate, paramTypes, numParams);
00099 
00100     query = transformTopLevelStmt(pstate, parseTree);
00101 
00102     if (post_parse_analyze_hook)
00103         (*post_parse_analyze_hook) (pstate, query);
00104 
00105     free_parsestate(pstate);
00106 
00107     return query;
00108 }
00109 
00110 /*
00111  * parse_analyze_varparams
00112  *
00113  * This variant is used when it's okay to deduce information about $n
00114  * symbol datatypes from context.  The passed-in paramTypes[] array can
00115  * be modified or enlarged (via repalloc).
00116  */
00117 Query *
00118 parse_analyze_varparams(Node *parseTree, const char *sourceText,
00119                         Oid **paramTypes, int *numParams)
00120 {
00121     ParseState *pstate = make_parsestate(NULL);
00122     Query      *query;
00123 
00124     Assert(sourceText != NULL); /* required as of 8.4 */
00125 
00126     pstate->p_sourcetext = sourceText;
00127 
00128     parse_variable_parameters(pstate, paramTypes, numParams);
00129 
00130     query = transformTopLevelStmt(pstate, parseTree);
00131 
00132     /* make sure all is well with parameter types */
00133     check_variable_parameters(pstate, query);
00134 
00135     if (post_parse_analyze_hook)
00136         (*post_parse_analyze_hook) (pstate, query);
00137 
00138     free_parsestate(pstate);
00139 
00140     return query;
00141 }
00142 
00143 /*
00144  * parse_sub_analyze
00145  *      Entry point for recursively analyzing a sub-statement.
00146  */
00147 Query *
00148 parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
00149                   CommonTableExpr *parentCTE,
00150                   bool locked_from_parent)
00151 {
00152     ParseState *pstate = make_parsestate(parentParseState);
00153     Query      *query;
00154 
00155     pstate->p_parent_cte = parentCTE;
00156     pstate->p_locked_from_parent = locked_from_parent;
00157 
00158     query = transformStmt(pstate, parseTree);
00159 
00160     free_parsestate(pstate);
00161 
00162     return query;
00163 }
00164 
00165 /*
00166  * transformTopLevelStmt -
00167  *    transform a Parse tree into a Query tree.
00168  *
00169  * The only thing we do here that we don't do in transformStmt() is to
00170  * convert SELECT ... INTO into CREATE TABLE AS.  Since utility statements
00171  * aren't allowed within larger statements, this is only allowed at the top
00172  * of the parse tree, and so we only try it before entering the recursive
00173  * transformStmt() processing.
00174  */
00175 Query *
00176 transformTopLevelStmt(ParseState *pstate, Node *parseTree)
00177 {
00178     if (IsA(parseTree, SelectStmt))
00179     {
00180         SelectStmt *stmt = (SelectStmt *) parseTree;
00181 
00182         /* If it's a set-operation tree, drill down to leftmost SelectStmt */
00183         while (stmt && stmt->op != SETOP_NONE)
00184             stmt = stmt->larg;
00185         Assert(stmt && IsA(stmt, SelectStmt) &&stmt->larg == NULL);
00186 
00187         if (stmt->intoClause)
00188         {
00189             CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
00190 
00191             ctas->query = parseTree;
00192             ctas->into = stmt->intoClause;
00193             ctas->relkind = OBJECT_TABLE;
00194             ctas->is_select_into = true;
00195 
00196             /*
00197              * Remove the intoClause from the SelectStmt.  This makes it safe
00198              * for transformSelectStmt to complain if it finds intoClause set
00199              * (implying that the INTO appeared in a disallowed place).
00200              */
00201             stmt->intoClause = NULL;
00202 
00203             parseTree = (Node *) ctas;
00204         }
00205     }
00206 
00207     return transformStmt(pstate, parseTree);
00208 }
00209 
00210 /*
00211  * transformStmt -
00212  *    recursively transform a Parse tree into a Query tree.
00213  */
00214 Query *
00215 transformStmt(ParseState *pstate, Node *parseTree)
00216 {
00217     Query      *result;
00218 
00219     switch (nodeTag(parseTree))
00220     {
00221             /*
00222              * Optimizable statements
00223              */
00224         case T_InsertStmt:
00225             result = transformInsertStmt(pstate, (InsertStmt *) parseTree);
00226             break;
00227 
00228         case T_DeleteStmt:
00229             result = transformDeleteStmt(pstate, (DeleteStmt *) parseTree);
00230             break;
00231 
00232         case T_UpdateStmt:
00233             result = transformUpdateStmt(pstate, (UpdateStmt *) parseTree);
00234             break;
00235 
00236         case T_SelectStmt:
00237             {
00238                 SelectStmt *n = (SelectStmt *) parseTree;
00239 
00240                 if (n->valuesLists)
00241                     result = transformValuesClause(pstate, n);
00242                 else if (n->op == SETOP_NONE)
00243                     result = transformSelectStmt(pstate, n);
00244                 else
00245                     result = transformSetOperationStmt(pstate, n);
00246             }
00247             break;
00248 
00249             /*
00250              * Special cases
00251              */
00252         case T_DeclareCursorStmt:
00253             result = transformDeclareCursorStmt(pstate,
00254                                             (DeclareCursorStmt *) parseTree);
00255             break;
00256 
00257         case T_ExplainStmt:
00258             result = transformExplainStmt(pstate,
00259                                           (ExplainStmt *) parseTree);
00260             break;
00261 
00262         case T_CreateTableAsStmt:
00263             result = transformCreateTableAsStmt(pstate,
00264                                             (CreateTableAsStmt *) parseTree);
00265             break;
00266 
00267         default:
00268 
00269             /*
00270              * other statements don't require any transformation; just return
00271              * the original parsetree with a Query node plastered on top.
00272              */
00273             result = makeNode(Query);
00274             result->commandType = CMD_UTILITY;
00275             result->utilityStmt = (Node *) parseTree;
00276             break;
00277     }
00278 
00279     /* Mark as original query until we learn differently */
00280     result->querySource = QSRC_ORIGINAL;
00281     result->canSetTag = true;
00282 
00283     return result;
00284 }
00285 
00286 /*
00287  * analyze_requires_snapshot
00288  *      Returns true if a snapshot must be set before doing parse analysis
00289  *      on the given raw parse tree.
00290  *
00291  * Classification here should match transformStmt(); but we also have to
00292  * allow a NULL input (for Parse/Bind of an empty query string).
00293  */
00294 bool
00295 analyze_requires_snapshot(Node *parseTree)
00296 {
00297     bool        result;
00298 
00299     if (parseTree == NULL)
00300         return false;
00301 
00302     switch (nodeTag(parseTree))
00303     {
00304             /*
00305              * Optimizable statements
00306              */
00307         case T_InsertStmt:
00308         case T_DeleteStmt:
00309         case T_UpdateStmt:
00310         case T_SelectStmt:
00311             result = true;
00312             break;
00313 
00314             /*
00315              * Special cases
00316              */
00317         case T_DeclareCursorStmt:
00318             /* yes, because it's analyzed just like SELECT */
00319             result = true;
00320             break;
00321 
00322         case T_ExplainStmt:
00323         case T_CreateTableAsStmt:
00324             /* yes, because we must analyze the contained statement */
00325             result = true;
00326             break;
00327 
00328         default:
00329             /* other utility statements don't have any real parse analysis */
00330             result = false;
00331             break;
00332     }
00333 
00334     return result;
00335 }
00336 
00337 /*
00338  * transformDeleteStmt -
00339  *    transforms a Delete Statement
00340  */
00341 static Query *
00342 transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
00343 {
00344     Query      *qry = makeNode(Query);
00345     Node       *qual;
00346 
00347     qry->commandType = CMD_DELETE;
00348 
00349     /* process the WITH clause independently of all else */
00350     if (stmt->withClause)
00351     {
00352         qry->hasRecursive = stmt->withClause->recursive;
00353         qry->cteList = transformWithClause(pstate, stmt->withClause);
00354         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
00355     }
00356 
00357     /* set up range table with just the result rel */
00358     qry->resultRelation = setTargetTable(pstate, stmt->relation,
00359                                   interpretInhOption(stmt->relation->inhOpt),
00360                                          true,
00361                                          ACL_DELETE);
00362 
00363     qry->distinctClause = NIL;
00364 
00365     /*
00366      * The USING clause is non-standard SQL syntax, and is equivalent in
00367      * functionality to the FROM list that can be specified for UPDATE. The
00368      * USING keyword is used rather than FROM because FROM is already a
00369      * keyword in the DELETE syntax.
00370      */
00371     transformFromClause(pstate, stmt->usingClause);
00372 
00373     qual = transformWhereClause(pstate, stmt->whereClause,
00374                                 EXPR_KIND_WHERE, "WHERE");
00375 
00376     qry->returningList = transformReturningList(pstate, stmt->returningList);
00377 
00378     /* done building the range table and jointree */
00379     qry->rtable = pstate->p_rtable;
00380     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
00381 
00382     qry->hasSubLinks = pstate->p_hasSubLinks;
00383     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
00384     qry->hasAggs = pstate->p_hasAggs;
00385     if (pstate->p_hasAggs)
00386         parseCheckAggregates(pstate, qry);
00387 
00388     assign_query_collations(pstate, qry);
00389 
00390     return qry;
00391 }
00392 
00393 /*
00394  * transformInsertStmt -
00395  *    transform an Insert Statement
00396  */
00397 static Query *
00398 transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
00399 {
00400     Query      *qry = makeNode(Query);
00401     SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
00402     List       *exprList = NIL;
00403     bool        isGeneralSelect;
00404     List       *sub_rtable;
00405     List       *sub_namespace;
00406     List       *icolumns;
00407     List       *attrnos;
00408     RangeTblEntry *rte;
00409     RangeTblRef *rtr;
00410     ListCell   *icols;
00411     ListCell   *attnos;
00412     ListCell   *lc;
00413 
00414     /* There can't be any outer WITH to worry about */
00415     Assert(pstate->p_ctenamespace == NIL);
00416 
00417     qry->commandType = CMD_INSERT;
00418     pstate->p_is_insert = true;
00419 
00420     /* process the WITH clause independently of all else */
00421     if (stmt->withClause)
00422     {
00423         qry->hasRecursive = stmt->withClause->recursive;
00424         qry->cteList = transformWithClause(pstate, stmt->withClause);
00425         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
00426     }
00427 
00428     /*
00429      * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
00430      * VALUES list, or general SELECT input.  We special-case VALUES, both for
00431      * efficiency and so we can handle DEFAULT specifications.
00432      *
00433      * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
00434      * VALUES clause.  If we have any of those, treat it as a general SELECT;
00435      * so it will work, but you can't use DEFAULT items together with those.
00436      */
00437     isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
00438                                       selectStmt->sortClause != NIL ||
00439                                       selectStmt->limitOffset != NULL ||
00440                                       selectStmt->limitCount != NULL ||
00441                                       selectStmt->lockingClause != NIL ||
00442                                       selectStmt->withClause != NULL));
00443 
00444     /*
00445      * If a non-nil rangetable/namespace was passed in, and we are doing
00446      * INSERT/SELECT, arrange to pass the rangetable/namespace down to the
00447      * SELECT.  This can only happen if we are inside a CREATE RULE, and in
00448      * that case we want the rule's OLD and NEW rtable entries to appear as
00449      * part of the SELECT's rtable, not as outer references for it.  (Kluge!)
00450      * The SELECT's joinlist is not affected however.  We must do this before
00451      * adding the target table to the INSERT's rtable.
00452      */
00453     if (isGeneralSelect)
00454     {
00455         sub_rtable = pstate->p_rtable;
00456         pstate->p_rtable = NIL;
00457         sub_namespace = pstate->p_namespace;
00458         pstate->p_namespace = NIL;
00459     }
00460     else
00461     {
00462         sub_rtable = NIL;       /* not used, but keep compiler quiet */
00463         sub_namespace = NIL;
00464     }
00465 
00466     /*
00467      * Must get write lock on INSERT target table before scanning SELECT, else
00468      * we will grab the wrong kind of initial lock if the target table is also
00469      * mentioned in the SELECT part.  Note that the target table is not added
00470      * to the joinlist or namespace.
00471      */
00472     qry->resultRelation = setTargetTable(pstate, stmt->relation,
00473                                          false, false, ACL_INSERT);
00474 
00475     /* Validate stmt->cols list, or build default list if no list given */
00476     icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
00477     Assert(list_length(icolumns) == list_length(attrnos));
00478 
00479     /*
00480      * Determine which variant of INSERT we have.
00481      */
00482     if (selectStmt == NULL)
00483     {
00484         /*
00485          * We have INSERT ... DEFAULT VALUES.  We can handle this case by
00486          * emitting an empty targetlist --- all columns will be defaulted when
00487          * the planner expands the targetlist.
00488          */
00489         exprList = NIL;
00490     }
00491     else if (isGeneralSelect)
00492     {
00493         /*
00494          * We make the sub-pstate a child of the outer pstate so that it can
00495          * see any Param definitions supplied from above.  Since the outer
00496          * pstate's rtable and namespace are presently empty, there are no
00497          * side-effects of exposing names the sub-SELECT shouldn't be able to
00498          * see.
00499          */
00500         ParseState *sub_pstate = make_parsestate(pstate);
00501         Query      *selectQuery;
00502 
00503         /*
00504          * Process the source SELECT.
00505          *
00506          * It is important that this be handled just like a standalone SELECT;
00507          * otherwise the behavior of SELECT within INSERT might be different
00508          * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
00509          * bugs of just that nature...)
00510          */
00511         sub_pstate->p_rtable = sub_rtable;
00512         sub_pstate->p_joinexprs = NIL;  /* sub_rtable has no joins */
00513         sub_pstate->p_namespace = sub_namespace;
00514 
00515         selectQuery = transformStmt(sub_pstate, stmt->selectStmt);
00516 
00517         free_parsestate(sub_pstate);
00518 
00519         /* The grammar should have produced a SELECT */
00520         if (!IsA(selectQuery, Query) ||
00521             selectQuery->commandType != CMD_SELECT ||
00522             selectQuery->utilityStmt != NULL)
00523             elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
00524 
00525         /*
00526          * Make the source be a subquery in the INSERT's rangetable, and add
00527          * it to the INSERT's joinlist.
00528          */
00529         rte = addRangeTableEntryForSubquery(pstate,
00530                                             selectQuery,
00531                                             makeAlias("*SELECT*", NIL),
00532                                             false,
00533                                             false);
00534         rtr = makeNode(RangeTblRef);
00535         /* assume new rte is at end */
00536         rtr->rtindex = list_length(pstate->p_rtable);
00537         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
00538         pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
00539 
00540         /*----------
00541          * Generate an expression list for the INSERT that selects all the
00542          * non-resjunk columns from the subquery.  (INSERT's tlist must be
00543          * separate from the subquery's tlist because we may add columns,
00544          * insert datatype coercions, etc.)
00545          *
00546          * HACK: unknown-type constants and params in the SELECT's targetlist
00547          * are copied up as-is rather than being referenced as subquery
00548          * outputs.  This is to ensure that when we try to coerce them to
00549          * the target column's datatype, the right things happen (see
00550          * special cases in coerce_type).  Otherwise, this fails:
00551          *      INSERT INTO foo SELECT 'bar', ... FROM baz
00552          *----------
00553          */
00554         exprList = NIL;
00555         foreach(lc, selectQuery->targetList)
00556         {
00557             TargetEntry *tle = (TargetEntry *) lfirst(lc);
00558             Expr       *expr;
00559 
00560             if (tle->resjunk)
00561                 continue;
00562             if (tle->expr &&
00563                 (IsA(tle->expr, Const) ||IsA(tle->expr, Param)) &&
00564                 exprType((Node *) tle->expr) == UNKNOWNOID)
00565                 expr = tle->expr;
00566             else
00567             {
00568                 Var        *var = makeVarFromTargetEntry(rtr->rtindex, tle);
00569 
00570                 var->location = exprLocation((Node *) tle->expr);
00571                 expr = (Expr *) var;
00572             }
00573             exprList = lappend(exprList, expr);
00574         }
00575 
00576         /* Prepare row for assignment to target table */
00577         exprList = transformInsertRow(pstate, exprList,
00578                                       stmt->cols,
00579                                       icolumns, attrnos);
00580     }
00581     else if (list_length(selectStmt->valuesLists) > 1)
00582     {
00583         /*
00584          * Process INSERT ... VALUES with multiple VALUES sublists. We
00585          * generate a VALUES RTE holding the transformed expression lists, and
00586          * build up a targetlist containing Vars that reference the VALUES
00587          * RTE.
00588          */
00589         List       *exprsLists = NIL;
00590         List       *collations = NIL;
00591         int         sublist_length = -1;
00592         bool        lateral = false;
00593         int         i;
00594 
00595         Assert(selectStmt->intoClause == NULL);
00596 
00597         foreach(lc, selectStmt->valuesLists)
00598         {
00599             List       *sublist = (List *) lfirst(lc);
00600 
00601             /* Do basic expression transformation (same as a ROW() expr) */
00602             sublist = transformExpressionList(pstate, sublist, EXPR_KIND_VALUES);
00603 
00604             /*
00605              * All the sublists must be the same length, *after*
00606              * transformation (which might expand '*' into multiple items).
00607              * The VALUES RTE can't handle anything different.
00608              */
00609             if (sublist_length < 0)
00610             {
00611                 /* Remember post-transformation length of first sublist */
00612                 sublist_length = list_length(sublist);
00613             }
00614             else if (sublist_length != list_length(sublist))
00615             {
00616                 ereport(ERROR,
00617                         (errcode(ERRCODE_SYNTAX_ERROR),
00618                          errmsg("VALUES lists must all be the same length"),
00619                          parser_errposition(pstate,
00620                                             exprLocation((Node *) sublist))));
00621             }
00622 
00623             /* Prepare row for assignment to target table */
00624             sublist = transformInsertRow(pstate, sublist,
00625                                          stmt->cols,
00626                                          icolumns, attrnos);
00627 
00628             /*
00629              * We must assign collations now because assign_query_collations
00630              * doesn't process rangetable entries.  We just assign all the
00631              * collations independently in each row, and don't worry about
00632              * whether they are consistent vertically.  The outer INSERT query
00633              * isn't going to care about the collations of the VALUES columns,
00634              * so it's not worth the effort to identify a common collation for
00635              * each one here.  (But note this does have one user-visible
00636              * consequence: INSERT ... VALUES won't complain about conflicting
00637              * explicit COLLATEs in a column, whereas the same VALUES
00638              * construct in another context would complain.)
00639              */
00640             assign_list_collations(pstate, sublist);
00641 
00642             exprsLists = lappend(exprsLists, sublist);
00643         }
00644 
00645         /*
00646          * Although we don't really need collation info, let's just make sure
00647          * we provide a correctly-sized list in the VALUES RTE.
00648          */
00649         for (i = 0; i < sublist_length; i++)
00650             collations = lappend_oid(collations, InvalidOid);
00651 
00652         /*
00653          * Ordinarily there can't be any current-level Vars in the expression
00654          * lists, because the namespace was empty ... but if we're inside
00655          * CREATE RULE, then NEW/OLD references might appear.  In that case we
00656          * have to mark the VALUES RTE as LATERAL.
00657          */
00658         if (list_length(pstate->p_rtable) != 1 &&
00659             contain_vars_of_level((Node *) exprsLists, 0))
00660             lateral = true;
00661 
00662         /*
00663          * Generate the VALUES RTE
00664          */
00665         rte = addRangeTableEntryForValues(pstate, exprsLists, collations,
00666                                           NULL, lateral, true);
00667         rtr = makeNode(RangeTblRef);
00668         /* assume new rte is at end */
00669         rtr->rtindex = list_length(pstate->p_rtable);
00670         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
00671         pstate->p_joinlist = lappend(pstate->p_joinlist, rtr);
00672 
00673         /*
00674          * Generate list of Vars referencing the RTE
00675          */
00676         expandRTE(rte, rtr->rtindex, 0, -1, false, NULL, &exprList);
00677     }
00678     else
00679     {
00680         /*
00681          * Process INSERT ... VALUES with a single VALUES sublist.  We treat
00682          * this case separately for efficiency.  The sublist is just computed
00683          * directly as the Query's targetlist, with no VALUES RTE.  So it
00684          * works just like a SELECT without any FROM.
00685          */
00686         List       *valuesLists = selectStmt->valuesLists;
00687 
00688         Assert(list_length(valuesLists) == 1);
00689         Assert(selectStmt->intoClause == NULL);
00690 
00691         /* Do basic expression transformation (same as a ROW() expr) */
00692         exprList = transformExpressionList(pstate,
00693                                            (List *) linitial(valuesLists),
00694                                            EXPR_KIND_VALUES);
00695 
00696         /* Prepare row for assignment to target table */
00697         exprList = transformInsertRow(pstate, exprList,
00698                                       stmt->cols,
00699                                       icolumns, attrnos);
00700     }
00701 
00702     /*
00703      * Generate query's target list using the computed list of expressions.
00704      * Also, mark all the target columns as needing insert permissions.
00705      */
00706     rte = pstate->p_target_rangetblentry;
00707     qry->targetList = NIL;
00708     icols = list_head(icolumns);
00709     attnos = list_head(attrnos);
00710     foreach(lc, exprList)
00711     {
00712         Expr       *expr = (Expr *) lfirst(lc);
00713         ResTarget  *col;
00714         AttrNumber  attr_num;
00715         TargetEntry *tle;
00716 
00717         col = (ResTarget *) lfirst(icols);
00718         Assert(IsA(col, ResTarget));
00719         attr_num = (AttrNumber) lfirst_int(attnos);
00720 
00721         tle = makeTargetEntry(expr,
00722                               attr_num,
00723                               col->name,
00724                               false);
00725         qry->targetList = lappend(qry->targetList, tle);
00726 
00727         rte->modifiedCols = bms_add_member(rte->modifiedCols,
00728                               attr_num - FirstLowInvalidHeapAttributeNumber);
00729 
00730         icols = lnext(icols);
00731         attnos = lnext(attnos);
00732     }
00733 
00734     /*
00735      * If we have a RETURNING clause, we need to add the target relation to
00736      * the query namespace before processing it, so that Var references in
00737      * RETURNING will work.  Also, remove any namespace entries added in a
00738      * sub-SELECT or VALUES list.
00739      */
00740     if (stmt->returningList)
00741     {
00742         pstate->p_namespace = NIL;
00743         addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
00744                       false, true, true);
00745         qry->returningList = transformReturningList(pstate,
00746                                                     stmt->returningList);
00747     }
00748 
00749     /* done building the range table and jointree */
00750     qry->rtable = pstate->p_rtable;
00751     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
00752 
00753     qry->hasSubLinks = pstate->p_hasSubLinks;
00754 
00755     assign_query_collations(pstate, qry);
00756 
00757     return qry;
00758 }
00759 
00760 /*
00761  * Prepare an INSERT row for assignment to the target table.
00762  *
00763  * The row might be either a VALUES row, or variables referencing a
00764  * sub-SELECT output.
00765  */
00766 static List *
00767 transformInsertRow(ParseState *pstate, List *exprlist,
00768                    List *stmtcols, List *icolumns, List *attrnos)
00769 {
00770     List       *result;
00771     ListCell   *lc;
00772     ListCell   *icols;
00773     ListCell   *attnos;
00774 
00775     /*
00776      * Check length of expr list.  It must not have more expressions than
00777      * there are target columns.  We allow fewer, but only if no explicit
00778      * columns list was given (the remaining columns are implicitly
00779      * defaulted).  Note we must check this *after* transformation because
00780      * that could expand '*' into multiple items.
00781      */
00782     if (list_length(exprlist) > list_length(icolumns))
00783         ereport(ERROR,
00784                 (errcode(ERRCODE_SYNTAX_ERROR),
00785                  errmsg("INSERT has more expressions than target columns"),
00786                  parser_errposition(pstate,
00787                                     exprLocation(list_nth(exprlist,
00788                                                   list_length(icolumns))))));
00789     if (stmtcols != NIL &&
00790         list_length(exprlist) < list_length(icolumns))
00791     {
00792         /*
00793          * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
00794          * where the user accidentally created a RowExpr instead of separate
00795          * columns.  Add a suitable hint if that seems to be the problem,
00796          * because the main error message is quite misleading for this case.
00797          * (If there's no stmtcols, you'll get something about data type
00798          * mismatch, which is less misleading so we don't worry about giving a
00799          * hint in that case.)
00800          */
00801         ereport(ERROR,
00802                 (errcode(ERRCODE_SYNTAX_ERROR),
00803                  errmsg("INSERT has more target columns than expressions"),
00804                  ((list_length(exprlist) == 1 &&
00805                    count_rowexpr_columns(pstate, linitial(exprlist)) ==
00806                    list_length(icolumns)) ?
00807                   errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
00808                  parser_errposition(pstate,
00809                                     exprLocation(list_nth(icolumns,
00810                                                   list_length(exprlist))))));
00811     }
00812 
00813     /*
00814      * Prepare columns for assignment to target table.
00815      */
00816     result = NIL;
00817     icols = list_head(icolumns);
00818     attnos = list_head(attrnos);
00819     foreach(lc, exprlist)
00820     {
00821         Expr       *expr = (Expr *) lfirst(lc);
00822         ResTarget  *col;
00823 
00824         col = (ResTarget *) lfirst(icols);
00825         Assert(IsA(col, ResTarget));
00826 
00827         expr = transformAssignedExpr(pstate, expr,
00828                                      EXPR_KIND_INSERT_TARGET,
00829                                      col->name,
00830                                      lfirst_int(attnos),
00831                                      col->indirection,
00832                                      col->location);
00833 
00834         result = lappend(result, expr);
00835 
00836         icols = lnext(icols);
00837         attnos = lnext(attnos);
00838     }
00839 
00840     return result;
00841 }
00842 
00843 /*
00844  * count_rowexpr_columns -
00845  *    get number of columns contained in a ROW() expression;
00846  *    return -1 if expression isn't a RowExpr or a Var referencing one.
00847  *
00848  * This is currently used only for hint purposes, so we aren't terribly
00849  * tense about recognizing all possible cases.  The Var case is interesting
00850  * because that's what we'll get in the INSERT ... SELECT (...) case.
00851  */
00852 static int
00853 count_rowexpr_columns(ParseState *pstate, Node *expr)
00854 {
00855     if (expr == NULL)
00856         return -1;
00857     if (IsA(expr, RowExpr))
00858         return list_length(((RowExpr *) expr)->args);
00859     if (IsA(expr, Var))
00860     {
00861         Var        *var = (Var *) expr;
00862         AttrNumber  attnum = var->varattno;
00863 
00864         if (attnum > 0 && var->vartype == RECORDOID)
00865         {
00866             RangeTblEntry *rte;
00867 
00868             rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
00869             if (rte->rtekind == RTE_SUBQUERY)
00870             {
00871                 /* Subselect-in-FROM: examine sub-select's output expr */
00872                 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
00873                                                     attnum);
00874 
00875                 if (ste == NULL || ste->resjunk)
00876                     return -1;
00877                 expr = (Node *) ste->expr;
00878                 if (IsA(expr, RowExpr))
00879                     return list_length(((RowExpr *) expr)->args);
00880             }
00881         }
00882     }
00883     return -1;
00884 }
00885 
00886 
00887 /*
00888  * transformSelectStmt -
00889  *    transforms a Select Statement
00890  *
00891  * Note: this covers only cases with no set operations and no VALUES lists;
00892  * see below for the other cases.
00893  */
00894 static Query *
00895 transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
00896 {
00897     Query      *qry = makeNode(Query);
00898     Node       *qual;
00899     ListCell   *l;
00900 
00901     qry->commandType = CMD_SELECT;
00902 
00903     /* process the WITH clause independently of all else */
00904     if (stmt->withClause)
00905     {
00906         qry->hasRecursive = stmt->withClause->recursive;
00907         qry->cteList = transformWithClause(pstate, stmt->withClause);
00908         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
00909     }
00910 
00911     /* Complain if we get called from someplace where INTO is not allowed */
00912     if (stmt->intoClause)
00913         ereport(ERROR,
00914                 (errcode(ERRCODE_SYNTAX_ERROR),
00915                  errmsg("SELECT ... INTO is not allowed here"),
00916                  parser_errposition(pstate,
00917                                   exprLocation((Node *) stmt->intoClause))));
00918 
00919     /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
00920     pstate->p_locking_clause = stmt->lockingClause;
00921 
00922     /* make WINDOW info available for window functions, too */
00923     pstate->p_windowdefs = stmt->windowClause;
00924 
00925     /* process the FROM clause */
00926     transformFromClause(pstate, stmt->fromClause);
00927 
00928     /* transform targetlist */
00929     qry->targetList = transformTargetList(pstate, stmt->targetList,
00930                                           EXPR_KIND_SELECT_TARGET);
00931 
00932     /* mark column origins */
00933     markTargetListOrigins(pstate, qry->targetList);
00934 
00935     /* transform WHERE */
00936     qual = transformWhereClause(pstate, stmt->whereClause,
00937                                 EXPR_KIND_WHERE, "WHERE");
00938 
00939     /* initial processing of HAVING clause is much like WHERE clause */
00940     qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
00941                                            EXPR_KIND_HAVING, "HAVING");
00942 
00943     /*
00944      * Transform sorting/grouping stuff.  Do ORDER BY first because both
00945      * transformGroupClause and transformDistinctClause need the results. Note
00946      * that these functions can also change the targetList, so it's passed to
00947      * them by reference.
00948      */
00949     qry->sortClause = transformSortClause(pstate,
00950                                           stmt->sortClause,
00951                                           &qry->targetList,
00952                                           EXPR_KIND_ORDER_BY,
00953                                           true /* fix unknowns */ ,
00954                                           false /* allow SQL92 rules */ );
00955 
00956     qry->groupClause = transformGroupClause(pstate,
00957                                             stmt->groupClause,
00958                                             &qry->targetList,
00959                                             qry->sortClause,
00960                                             EXPR_KIND_GROUP_BY,
00961                                             false /* allow SQL92 rules */ );
00962 
00963     if (stmt->distinctClause == NIL)
00964     {
00965         qry->distinctClause = NIL;
00966         qry->hasDistinctOn = false;
00967     }
00968     else if (linitial(stmt->distinctClause) == NULL)
00969     {
00970         /* We had SELECT DISTINCT */
00971         qry->distinctClause = transformDistinctClause(pstate,
00972                                                       &qry->targetList,
00973                                                       qry->sortClause,
00974                                                       false);
00975         qry->hasDistinctOn = false;
00976     }
00977     else
00978     {
00979         /* We had SELECT DISTINCT ON */
00980         qry->distinctClause = transformDistinctOnClause(pstate,
00981                                                         stmt->distinctClause,
00982                                                         &qry->targetList,
00983                                                         qry->sortClause);
00984         qry->hasDistinctOn = true;
00985     }
00986 
00987     /* transform LIMIT */
00988     qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
00989                                             EXPR_KIND_OFFSET, "OFFSET");
00990     qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
00991                                            EXPR_KIND_LIMIT, "LIMIT");
00992 
00993     /* transform window clauses after we have seen all window functions */
00994     qry->windowClause = transformWindowDefinitions(pstate,
00995                                                    pstate->p_windowdefs,
00996                                                    &qry->targetList);
00997 
00998     qry->rtable = pstate->p_rtable;
00999     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
01000 
01001     qry->hasSubLinks = pstate->p_hasSubLinks;
01002     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
01003     qry->hasAggs = pstate->p_hasAggs;
01004     if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
01005         parseCheckAggregates(pstate, qry);
01006 
01007     foreach(l, stmt->lockingClause)
01008     {
01009         transformLockingClause(pstate, qry,
01010                                (LockingClause *) lfirst(l), false);
01011     }
01012 
01013     assign_query_collations(pstate, qry);
01014 
01015     return qry;
01016 }
01017 
01018 /*
01019  * transformValuesClause -
01020  *    transforms a VALUES clause that's being used as a standalone SELECT
01021  *
01022  * We build a Query containing a VALUES RTE, rather as if one had written
01023  *          SELECT * FROM (VALUES ...) AS "*VALUES*"
01024  */
01025 static Query *
01026 transformValuesClause(ParseState *pstate, SelectStmt *stmt)
01027 {
01028     Query      *qry = makeNode(Query);
01029     List       *exprsLists;
01030     List       *collations;
01031     List      **colexprs = NULL;
01032     int         sublist_length = -1;
01033     bool        lateral = false;
01034     RangeTblEntry *rte;
01035     int         rtindex;
01036     ListCell   *lc;
01037     ListCell   *lc2;
01038     int         i;
01039 
01040     qry->commandType = CMD_SELECT;
01041 
01042     /* Most SELECT stuff doesn't apply in a VALUES clause */
01043     Assert(stmt->distinctClause == NIL);
01044     Assert(stmt->intoClause == NULL);
01045     Assert(stmt->targetList == NIL);
01046     Assert(stmt->fromClause == NIL);
01047     Assert(stmt->whereClause == NULL);
01048     Assert(stmt->groupClause == NIL);
01049     Assert(stmt->havingClause == NULL);
01050     Assert(stmt->windowClause == NIL);
01051     Assert(stmt->op == SETOP_NONE);
01052 
01053     /* process the WITH clause independently of all else */
01054     if (stmt->withClause)
01055     {
01056         qry->hasRecursive = stmt->withClause->recursive;
01057         qry->cteList = transformWithClause(pstate, stmt->withClause);
01058         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
01059     }
01060 
01061     /*
01062      * For each row of VALUES, transform the raw expressions.  This is also a
01063      * handy place to reject DEFAULT nodes, which the grammar allows for
01064      * simplicity.
01065      *
01066      * Note that the intermediate representation we build is column-organized
01067      * not row-organized.  That simplifies the type and collation processing
01068      * below.
01069      */
01070     foreach(lc, stmt->valuesLists)
01071     {
01072         List       *sublist = (List *) lfirst(lc);
01073 
01074         /* Do basic expression transformation (same as a ROW() expr) */
01075         sublist = transformExpressionList(pstate, sublist, EXPR_KIND_VALUES);
01076 
01077         /*
01078          * All the sublists must be the same length, *after* transformation
01079          * (which might expand '*' into multiple items).  The VALUES RTE can't
01080          * handle anything different.
01081          */
01082         if (sublist_length < 0)
01083         {
01084             /* Remember post-transformation length of first sublist */
01085             sublist_length = list_length(sublist);
01086             /* and allocate array for per-column lists */
01087             colexprs = (List **) palloc0(sublist_length * sizeof(List *));
01088         }
01089         else if (sublist_length != list_length(sublist))
01090         {
01091             ereport(ERROR,
01092                     (errcode(ERRCODE_SYNTAX_ERROR),
01093                      errmsg("VALUES lists must all be the same length"),
01094                      parser_errposition(pstate,
01095                                         exprLocation((Node *) sublist))));
01096         }
01097 
01098         /* Check for DEFAULT and build per-column expression lists */
01099         i = 0;
01100         foreach(lc2, sublist)
01101         {
01102             Node       *col = (Node *) lfirst(lc2);
01103 
01104             if (IsA(col, SetToDefault))
01105                 ereport(ERROR,
01106                         (errcode(ERRCODE_SYNTAX_ERROR),
01107                          errmsg("DEFAULT can only appear in a VALUES list within INSERT"),
01108                          parser_errposition(pstate, exprLocation(col))));
01109             colexprs[i] = lappend(colexprs[i], col);
01110             i++;
01111         }
01112 
01113         /* Release sub-list's cells to save memory */
01114         list_free(sublist);
01115     }
01116 
01117     /*
01118      * Now resolve the common types of the columns, and coerce everything to
01119      * those types.  Then identify the common collation, if any, of each
01120      * column.
01121      *
01122      * We must do collation processing now because (1) assign_query_collations
01123      * doesn't process rangetable entries, and (2) we need to label the VALUES
01124      * RTE with column collations for use in the outer query.  We don't
01125      * consider conflict of implicit collations to be an error here; instead
01126      * the column will just show InvalidOid as its collation, and you'll get a
01127      * failure later if that results in failure to resolve a collation.
01128      *
01129      * Note we modify the per-column expression lists in-place.
01130      */
01131     collations = NIL;
01132     for (i = 0; i < sublist_length; i++)
01133     {
01134         Oid         coltype;
01135         Oid         colcoll;
01136 
01137         coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
01138 
01139         foreach(lc, colexprs[i])
01140         {
01141             Node       *col = (Node *) lfirst(lc);
01142 
01143             col = coerce_to_common_type(pstate, col, coltype, "VALUES");
01144             lfirst(lc) = (void *) col;
01145         }
01146 
01147         colcoll = select_common_collation(pstate, colexprs[i], true);
01148 
01149         collations = lappend_oid(collations, colcoll);
01150     }
01151 
01152     /*
01153      * Finally, rearrange the coerced expressions into row-organized lists.
01154      */
01155     exprsLists = NIL;
01156     foreach(lc, colexprs[0])
01157     {
01158         Node       *col = (Node *) lfirst(lc);
01159         List       *sublist;
01160 
01161         sublist = list_make1(col);
01162         exprsLists = lappend(exprsLists, sublist);
01163     }
01164     list_free(colexprs[0]);
01165     for (i = 1; i < sublist_length; i++)
01166     {
01167         forboth(lc, colexprs[i], lc2, exprsLists)
01168         {
01169             Node       *col = (Node *) lfirst(lc);
01170             List       *sublist = lfirst(lc2);
01171 
01172             /* sublist pointer in exprsLists won't need adjustment */
01173             (void) lappend(sublist, col);
01174         }
01175         list_free(colexprs[i]);
01176     }
01177 
01178     /*
01179      * Ordinarily there can't be any current-level Vars in the expression
01180      * lists, because the namespace was empty ... but if we're inside CREATE
01181      * RULE, then NEW/OLD references might appear.  In that case we have to
01182      * mark the VALUES RTE as LATERAL.
01183      */
01184     if (pstate->p_rtable != NIL &&
01185         contain_vars_of_level((Node *) exprsLists, 0))
01186         lateral = true;
01187 
01188     /*
01189      * Generate the VALUES RTE
01190      */
01191     rte = addRangeTableEntryForValues(pstate, exprsLists, collations,
01192                                       NULL, lateral, true);
01193     addRTEtoQuery(pstate, rte, true, true, true);
01194 
01195     /* assume new rte is at end */
01196     rtindex = list_length(pstate->p_rtable);
01197     Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
01198 
01199     /*
01200      * Generate a targetlist as though expanding "*"
01201      */
01202     Assert(pstate->p_next_resno == 1);
01203     qry->targetList = expandRelAttrs(pstate, rte, rtindex, 0, -1);
01204 
01205     /*
01206      * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
01207      * VALUES, so cope.
01208      */
01209     qry->sortClause = transformSortClause(pstate,
01210                                           stmt->sortClause,
01211                                           &qry->targetList,
01212                                           EXPR_KIND_ORDER_BY,
01213                                           true /* fix unknowns */ ,
01214                                           false /* allow SQL92 rules */ );
01215 
01216     qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
01217                                             EXPR_KIND_OFFSET, "OFFSET");
01218     qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
01219                                            EXPR_KIND_LIMIT, "LIMIT");
01220 
01221     if (stmt->lockingClause)
01222         ereport(ERROR,
01223                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01224              errmsg("SELECT FOR UPDATE/SHARE cannot be applied to VALUES")));
01225 
01226     qry->rtable = pstate->p_rtable;
01227     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
01228 
01229     qry->hasSubLinks = pstate->p_hasSubLinks;
01230 
01231     assign_query_collations(pstate, qry);
01232 
01233     return qry;
01234 }
01235 
01236 /*
01237  * transformSetOperationStmt -
01238  *    transforms a set-operations tree
01239  *
01240  * A set-operation tree is just a SELECT, but with UNION/INTERSECT/EXCEPT
01241  * structure to it.  We must transform each leaf SELECT and build up a top-
01242  * level Query that contains the leaf SELECTs as subqueries in its rangetable.
01243  * The tree of set operations is converted into the setOperations field of
01244  * the top-level Query.
01245  */
01246 static Query *
01247 transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
01248 {
01249     Query      *qry = makeNode(Query);
01250     SelectStmt *leftmostSelect;
01251     int         leftmostRTI;
01252     Query      *leftmostQuery;
01253     SetOperationStmt *sostmt;
01254     List       *sortClause;
01255     Node       *limitOffset;
01256     Node       *limitCount;
01257     List       *lockingClause;
01258     WithClause *withClause;
01259     Node       *node;
01260     ListCell   *left_tlist,
01261                *lct,
01262                *lcm,
01263                *lcc,
01264                *l;
01265     List       *targetvars,
01266                *targetnames,
01267                *sv_namespace;
01268     int         sv_rtable_length;
01269     RangeTblEntry *jrte;
01270     int         tllen;
01271 
01272     qry->commandType = CMD_SELECT;
01273 
01274     /*
01275      * Find leftmost leaf SelectStmt.  We currently only need to do this in
01276      * order to deliver a suitable error message if there's an INTO clause
01277      * there, implying the set-op tree is in a context that doesn't allow
01278      * INTO.  (transformSetOperationTree would throw error anyway, but it
01279      * seems worth the trouble to throw a different error for non-leftmost
01280      * INTO, so we produce that error in transformSetOperationTree.)
01281      */
01282     leftmostSelect = stmt->larg;
01283     while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
01284         leftmostSelect = leftmostSelect->larg;
01285     Assert(leftmostSelect && IsA(leftmostSelect, SelectStmt) &&
01286            leftmostSelect->larg == NULL);
01287     if (leftmostSelect->intoClause)
01288         ereport(ERROR,
01289                 (errcode(ERRCODE_SYNTAX_ERROR),
01290                  errmsg("SELECT ... INTO is not allowed here"),
01291                  parser_errposition(pstate,
01292                         exprLocation((Node *) leftmostSelect->intoClause))));
01293 
01294     /*
01295      * We need to extract ORDER BY and other top-level clauses here and not
01296      * let transformSetOperationTree() see them --- else it'll just recurse
01297      * right back here!
01298      */
01299     sortClause = stmt->sortClause;
01300     limitOffset = stmt->limitOffset;
01301     limitCount = stmt->limitCount;
01302     lockingClause = stmt->lockingClause;
01303     withClause = stmt->withClause;
01304 
01305     stmt->sortClause = NIL;
01306     stmt->limitOffset = NULL;
01307     stmt->limitCount = NULL;
01308     stmt->lockingClause = NIL;
01309     stmt->withClause = NULL;
01310 
01311     /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
01312     if (lockingClause)
01313         ereport(ERROR,
01314                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01315                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
01316 
01317     /* Process the WITH clause independently of all else */
01318     if (withClause)
01319     {
01320         qry->hasRecursive = withClause->recursive;
01321         qry->cteList = transformWithClause(pstate, withClause);
01322         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
01323     }
01324 
01325     /*
01326      * Recursively transform the components of the tree.
01327      */
01328     sostmt = (SetOperationStmt *) transformSetOperationTree(pstate, stmt,
01329                                                             true,
01330                                                             NULL);
01331     Assert(sostmt && IsA(sostmt, SetOperationStmt));
01332     qry->setOperations = (Node *) sostmt;
01333 
01334     /*
01335      * Re-find leftmost SELECT (now it's a sub-query in rangetable)
01336      */
01337     node = sostmt->larg;
01338     while (node && IsA(node, SetOperationStmt))
01339         node = ((SetOperationStmt *) node)->larg;
01340     Assert(node && IsA(node, RangeTblRef));
01341     leftmostRTI = ((RangeTblRef *) node)->rtindex;
01342     leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
01343     Assert(leftmostQuery != NULL);
01344 
01345     /*
01346      * Generate dummy targetlist for outer query using column names of
01347      * leftmost select and common datatypes/collations of topmost set
01348      * operation.  Also make lists of the dummy vars and their names for use
01349      * in parsing ORDER BY.
01350      *
01351      * Note: we use leftmostRTI as the varno of the dummy variables. It
01352      * shouldn't matter too much which RT index they have, as long as they
01353      * have one that corresponds to a real RT entry; else funny things may
01354      * happen when the tree is mashed by rule rewriting.
01355      */
01356     qry->targetList = NIL;
01357     targetvars = NIL;
01358     targetnames = NIL;
01359     left_tlist = list_head(leftmostQuery->targetList);
01360 
01361     forthree(lct, sostmt->colTypes,
01362              lcm, sostmt->colTypmods,
01363              lcc, sostmt->colCollations)
01364     {
01365         Oid         colType = lfirst_oid(lct);
01366         int32       colTypmod = lfirst_int(lcm);
01367         Oid         colCollation = lfirst_oid(lcc);
01368         TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
01369         char       *colName;
01370         TargetEntry *tle;
01371         Var        *var;
01372 
01373         Assert(!lefttle->resjunk);
01374         colName = pstrdup(lefttle->resname);
01375         var = makeVar(leftmostRTI,
01376                       lefttle->resno,
01377                       colType,
01378                       colTypmod,
01379                       colCollation,
01380                       0);
01381         var->location = exprLocation((Node *) lefttle->expr);
01382         tle = makeTargetEntry((Expr *) var,
01383                               (AttrNumber) pstate->p_next_resno++,
01384                               colName,
01385                               false);
01386         qry->targetList = lappend(qry->targetList, tle);
01387         targetvars = lappend(targetvars, var);
01388         targetnames = lappend(targetnames, makeString(colName));
01389         left_tlist = lnext(left_tlist);
01390     }
01391 
01392     /*
01393      * As a first step towards supporting sort clauses that are expressions
01394      * using the output columns, generate a namespace entry that makes the
01395      * output columns visible.  A Join RTE node is handy for this, since we
01396      * can easily control the Vars generated upon matches.
01397      *
01398      * Note: we don't yet do anything useful with such cases, but at least
01399      * "ORDER BY upper(foo)" will draw the right error message rather than
01400      * "foo not found".
01401      */
01402     sv_rtable_length = list_length(pstate->p_rtable);
01403 
01404     jrte = addRangeTableEntryForJoin(pstate,
01405                                      targetnames,
01406                                      JOIN_INNER,
01407                                      targetvars,
01408                                      NULL,
01409                                      false);
01410 
01411     sv_namespace = pstate->p_namespace;
01412     pstate->p_namespace = NIL;
01413 
01414     /* add jrte to column namespace only */
01415     addRTEtoQuery(pstate, jrte, false, false, true);
01416 
01417     /*
01418      * For now, we don't support resjunk sort clauses on the output of a
01419      * setOperation tree --- you can only use the SQL92-spec options of
01420      * selecting an output column by name or number.  Enforce by checking that
01421      * transformSortClause doesn't add any items to tlist.
01422      */
01423     tllen = list_length(qry->targetList);
01424 
01425     qry->sortClause = transformSortClause(pstate,
01426                                           sortClause,
01427                                           &qry->targetList,
01428                                           EXPR_KIND_ORDER_BY,
01429                                           false /* no unknowns expected */ ,
01430                                           false /* allow SQL92 rules */ );
01431 
01432     /* restore namespace, remove jrte from rtable */
01433     pstate->p_namespace = sv_namespace;
01434     pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
01435 
01436     if (tllen != list_length(qry->targetList))
01437         ereport(ERROR,
01438                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01439                  errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
01440                  errdetail("Only result column names can be used, not expressions or functions."),
01441                  errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
01442                  parser_errposition(pstate,
01443                            exprLocation(list_nth(qry->targetList, tllen)))));
01444 
01445     qry->limitOffset = transformLimitClause(pstate, limitOffset,
01446                                             EXPR_KIND_OFFSET, "OFFSET");
01447     qry->limitCount = transformLimitClause(pstate, limitCount,
01448                                            EXPR_KIND_LIMIT, "LIMIT");
01449 
01450     qry->rtable = pstate->p_rtable;
01451     qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
01452 
01453     qry->hasSubLinks = pstate->p_hasSubLinks;
01454     qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
01455     qry->hasAggs = pstate->p_hasAggs;
01456     if (pstate->p_hasAggs || qry->groupClause || qry->havingQual)
01457         parseCheckAggregates(pstate, qry);
01458 
01459     foreach(l, lockingClause)
01460     {
01461         transformLockingClause(pstate, qry,
01462                                (LockingClause *) lfirst(l), false);
01463     }
01464 
01465     assign_query_collations(pstate, qry);
01466 
01467     return qry;
01468 }
01469 
01470 /*
01471  * transformSetOperationTree
01472  *      Recursively transform leaves and internal nodes of a set-op tree
01473  *
01474  * In addition to returning the transformed node, if targetlist isn't NULL
01475  * then we return a list of its non-resjunk TargetEntry nodes.  For a leaf
01476  * set-op node these are the actual targetlist entries; otherwise they are
01477  * dummy entries created to carry the type, typmod, collation, and location
01478  * (for error messages) of each output column of the set-op node.  This info
01479  * is needed only during the internal recursion of this function, so outside
01480  * callers pass NULL for targetlist.  Note: the reason for passing the
01481  * actual targetlist entries of a leaf node is so that upper levels can
01482  * replace UNKNOWN Consts with properly-coerced constants.
01483  */
01484 static Node *
01485 transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
01486                           bool isTopLevel, List **targetlist)
01487 {
01488     bool        isLeaf;
01489 
01490     Assert(stmt && IsA(stmt, SelectStmt));
01491 
01492     /* Guard against stack overflow due to overly complex set-expressions */
01493     check_stack_depth();
01494 
01495     /*
01496      * Validity-check both leaf and internal SELECTs for disallowed ops.
01497      */
01498     if (stmt->intoClause)
01499         ereport(ERROR,
01500                 (errcode(ERRCODE_SYNTAX_ERROR),
01501                  errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
01502                  parser_errposition(pstate,
01503                                   exprLocation((Node *) stmt->intoClause))));
01504 
01505     /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
01506     if (stmt->lockingClause)
01507         ereport(ERROR,
01508                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01509                  errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
01510 
01511     /*
01512      * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
01513      * or WITH clauses attached, we need to treat it like a leaf node to
01514      * generate an independent sub-Query tree.  Otherwise, it can be
01515      * represented by a SetOperationStmt node underneath the parent Query.
01516      */
01517     if (stmt->op == SETOP_NONE)
01518     {
01519         Assert(stmt->larg == NULL && stmt->rarg == NULL);
01520         isLeaf = true;
01521     }
01522     else
01523     {
01524         Assert(stmt->larg != NULL && stmt->rarg != NULL);
01525         if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
01526             stmt->lockingClause || stmt->withClause)
01527             isLeaf = true;
01528         else
01529             isLeaf = false;
01530     }
01531 
01532     if (isLeaf)
01533     {
01534         /* Process leaf SELECT */
01535         Query      *selectQuery;
01536         char        selectName[32];
01537         RangeTblEntry *rte PG_USED_FOR_ASSERTS_ONLY;
01538         RangeTblRef *rtr;
01539         ListCell   *tl;
01540 
01541         /*
01542          * Transform SelectStmt into a Query.
01543          *
01544          * Note: previously transformed sub-queries don't affect the parsing
01545          * of this sub-query, because they are not in the toplevel pstate's
01546          * namespace list.
01547          */
01548         selectQuery = parse_sub_analyze((Node *) stmt, pstate, NULL, false);
01549 
01550         /*
01551          * Check for bogus references to Vars on the current query level (but
01552          * upper-level references are okay). Normally this can't happen
01553          * because the namespace will be empty, but it could happen if we are
01554          * inside a rule.
01555          */
01556         if (pstate->p_namespace)
01557         {
01558             if (contain_vars_of_level((Node *) selectQuery, 1))
01559                 ereport(ERROR,
01560                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
01561                          errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
01562                          parser_errposition(pstate,
01563                              locate_var_of_level((Node *) selectQuery, 1))));
01564         }
01565 
01566         /*
01567          * Extract a list of the non-junk TLEs for upper-level processing.
01568          */
01569         if (targetlist)
01570         {
01571             *targetlist = NIL;
01572             foreach(tl, selectQuery->targetList)
01573             {
01574                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
01575 
01576                 if (!tle->resjunk)
01577                     *targetlist = lappend(*targetlist, tle);
01578             }
01579         }
01580 
01581         /*
01582          * Make the leaf query be a subquery in the top-level rangetable.
01583          */
01584         snprintf(selectName, sizeof(selectName), "*SELECT* %d",
01585                  list_length(pstate->p_rtable) + 1);
01586         rte = addRangeTableEntryForSubquery(pstate,
01587                                             selectQuery,
01588                                             makeAlias(selectName, NIL),
01589                                             false,
01590                                             false);
01591 
01592         /*
01593          * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
01594          */
01595         rtr = makeNode(RangeTblRef);
01596         /* assume new rte is at end */
01597         rtr->rtindex = list_length(pstate->p_rtable);
01598         Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
01599         return (Node *) rtr;
01600     }
01601     else
01602     {
01603         /* Process an internal node (set operation node) */
01604         SetOperationStmt *op = makeNode(SetOperationStmt);
01605         List       *ltargetlist;
01606         List       *rtargetlist;
01607         ListCell   *ltl;
01608         ListCell   *rtl;
01609         const char *context;
01610 
01611         context = (stmt->op == SETOP_UNION ? "UNION" :
01612                    (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
01613                     "EXCEPT"));
01614 
01615         op->op = stmt->op;
01616         op->all = stmt->all;
01617 
01618         /*
01619          * Recursively transform the left child node.
01620          */
01621         op->larg = transformSetOperationTree(pstate, stmt->larg,
01622                                              false,
01623                                              &ltargetlist);
01624 
01625         /*
01626          * If we are processing a recursive union query, now is the time to
01627          * examine the non-recursive term's output columns and mark the
01628          * containing CTE as having those result columns.  We should do this
01629          * only at the topmost setop of the CTE, of course.
01630          */
01631         if (isTopLevel &&
01632             pstate->p_parent_cte &&
01633             pstate->p_parent_cte->cterecursive)
01634             determineRecursiveColTypes(pstate, op->larg, ltargetlist);
01635 
01636         /*
01637          * Recursively transform the right child node.
01638          */
01639         op->rarg = transformSetOperationTree(pstate, stmt->rarg,
01640                                              false,
01641                                              &rtargetlist);
01642 
01643         /*
01644          * Verify that the two children have the same number of non-junk
01645          * columns, and determine the types of the merged output columns.
01646          */
01647         if (list_length(ltargetlist) != list_length(rtargetlist))
01648             ereport(ERROR,
01649                     (errcode(ERRCODE_SYNTAX_ERROR),
01650                  errmsg("each %s query must have the same number of columns",
01651                         context),
01652                      parser_errposition(pstate,
01653                                         exprLocation((Node *) rtargetlist))));
01654 
01655         if (targetlist)
01656             *targetlist = NIL;
01657         op->colTypes = NIL;
01658         op->colTypmods = NIL;
01659         op->colCollations = NIL;
01660         op->groupClauses = NIL;
01661         forboth(ltl, ltargetlist, rtl, rtargetlist)
01662         {
01663             TargetEntry *ltle = (TargetEntry *) lfirst(ltl);
01664             TargetEntry *rtle = (TargetEntry *) lfirst(rtl);
01665             Node       *lcolnode = (Node *) ltle->expr;
01666             Node       *rcolnode = (Node *) rtle->expr;
01667             Oid         lcoltype = exprType(lcolnode);
01668             Oid         rcoltype = exprType(rcolnode);
01669             int32       lcoltypmod = exprTypmod(lcolnode);
01670             int32       rcoltypmod = exprTypmod(rcolnode);
01671             Node       *bestexpr;
01672             int         bestlocation;
01673             Oid         rescoltype;
01674             int32       rescoltypmod;
01675             Oid         rescolcoll;
01676 
01677             /* select common type, same as CASE et al */
01678             rescoltype = select_common_type(pstate,
01679                                             list_make2(lcolnode, rcolnode),
01680                                             context,
01681                                             &bestexpr);
01682             bestlocation = exprLocation(bestexpr);
01683             /* if same type and same typmod, use typmod; else default */
01684             if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
01685                 rescoltypmod = lcoltypmod;
01686             else
01687                 rescoltypmod = -1;
01688 
01689             /*
01690              * Verify the coercions are actually possible.  If not, we'd fail
01691              * later anyway, but we want to fail now while we have sufficient
01692              * context to produce an error cursor position.
01693              *
01694              * For all non-UNKNOWN-type cases, we verify coercibility but we
01695              * don't modify the child's expression, for fear of changing the
01696              * child query's semantics.
01697              *
01698              * If a child expression is an UNKNOWN-type Const or Param, we
01699              * want to replace it with the coerced expression.  This can only
01700              * happen when the child is a leaf set-op node.  It's safe to
01701              * replace the expression because if the child query's semantics
01702              * depended on the type of this output column, it'd have already
01703              * coerced the UNKNOWN to something else.  We want to do this
01704              * because (a) we want to verify that a Const is valid for the
01705              * target type, or resolve the actual type of an UNKNOWN Param,
01706              * and (b) we want to avoid unnecessary discrepancies between the
01707              * output type of the child query and the resolved target type.
01708              * Such a discrepancy would disable optimization in the planner.
01709              *
01710              * If it's some other UNKNOWN-type node, eg a Var, we do nothing
01711              * (knowing that coerce_to_common_type would fail).  The planner
01712              * is sometimes able to fold an UNKNOWN Var to a constant before
01713              * it has to coerce the type, so failing now would just break
01714              * cases that might work.
01715              */
01716             if (lcoltype != UNKNOWNOID)
01717                 lcolnode = coerce_to_common_type(pstate, lcolnode,
01718                                                  rescoltype, context);
01719             else if (IsA(lcolnode, Const) ||
01720                      IsA(lcolnode, Param))
01721             {
01722                 lcolnode = coerce_to_common_type(pstate, lcolnode,
01723                                                  rescoltype, context);
01724                 ltle->expr = (Expr *) lcolnode;
01725             }
01726 
01727             if (rcoltype != UNKNOWNOID)
01728                 rcolnode = coerce_to_common_type(pstate, rcolnode,
01729                                                  rescoltype, context);
01730             else if (IsA(rcolnode, Const) ||
01731                      IsA(rcolnode, Param))
01732             {
01733                 rcolnode = coerce_to_common_type(pstate, rcolnode,
01734                                                  rescoltype, context);
01735                 rtle->expr = (Expr *) rcolnode;
01736             }
01737 
01738             /*
01739              * Select common collation.  A common collation is required for
01740              * all set operators except UNION ALL; see SQL:2008 7.13 <query
01741              * expression> Syntax Rule 15c.  (If we fail to identify a common
01742              * collation for a UNION ALL column, the curCollations element
01743              * will be set to InvalidOid, which may result in a runtime error
01744              * if something at a higher query level wants to use the column's
01745              * collation.)
01746              */
01747             rescolcoll = select_common_collation(pstate,
01748                                               list_make2(lcolnode, rcolnode),
01749                                          (op->op == SETOP_UNION && op->all));
01750 
01751             /* emit results */
01752             op->colTypes = lappend_oid(op->colTypes, rescoltype);
01753             op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
01754             op->colCollations = lappend_oid(op->colCollations, rescolcoll);
01755 
01756             /*
01757              * For all cases except UNION ALL, identify the grouping operators
01758              * (and, if available, sorting operators) that will be used to
01759              * eliminate duplicates.
01760              */
01761             if (op->op != SETOP_UNION || !op->all)
01762             {
01763                 SortGroupClause *grpcl = makeNode(SortGroupClause);
01764                 Oid         sortop;
01765                 Oid         eqop;
01766                 bool        hashable;
01767                 ParseCallbackState pcbstate;
01768 
01769                 setup_parser_errposition_callback(&pcbstate, pstate,
01770                                                   bestlocation);
01771 
01772                 /* determine the eqop and optional sortop */
01773                 get_sort_group_operators(rescoltype,
01774                                          false, true, false,
01775                                          &sortop, &eqop, NULL,
01776                                          &hashable);
01777 
01778                 cancel_parser_errposition_callback(&pcbstate);
01779 
01780                 /* we don't have a tlist yet, so can't assign sortgrouprefs */
01781                 grpcl->tleSortGroupRef = 0;
01782                 grpcl->eqop = eqop;
01783                 grpcl->sortop = sortop;
01784                 grpcl->nulls_first = false;     /* OK with or without sortop */
01785                 grpcl->hashable = hashable;
01786 
01787                 op->groupClauses = lappend(op->groupClauses, grpcl);
01788             }
01789 
01790             /*
01791              * Construct a dummy tlist entry to return.  We use a SetToDefault
01792              * node for the expression, since it carries exactly the fields
01793              * needed, but any other expression node type would do as well.
01794              */
01795             if (targetlist)
01796             {
01797                 SetToDefault *rescolnode = makeNode(SetToDefault);
01798                 TargetEntry *restle;
01799 
01800                 rescolnode->typeId = rescoltype;
01801                 rescolnode->typeMod = rescoltypmod;
01802                 rescolnode->collation = rescolcoll;
01803                 rescolnode->location = bestlocation;
01804                 restle = makeTargetEntry((Expr *) rescolnode,
01805                                          0,     /* no need to set resno */
01806                                          NULL,
01807                                          false);
01808                 *targetlist = lappend(*targetlist, restle);
01809             }
01810         }
01811 
01812         return (Node *) op;
01813     }
01814 }
01815 
01816 /*
01817  * Process the outputs of the non-recursive term of a recursive union
01818  * to set up the parent CTE's columns
01819  */
01820 static void
01821 determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
01822 {
01823     Node       *node;
01824     int         leftmostRTI;
01825     Query      *leftmostQuery;
01826     List       *targetList;
01827     ListCell   *left_tlist;
01828     ListCell   *nrtl;
01829     int         next_resno;
01830 
01831     /*
01832      * Find leftmost leaf SELECT
01833      */
01834     node = larg;
01835     while (node && IsA(node, SetOperationStmt))
01836         node = ((SetOperationStmt *) node)->larg;
01837     Assert(node && IsA(node, RangeTblRef));
01838     leftmostRTI = ((RangeTblRef *) node)->rtindex;
01839     leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
01840     Assert(leftmostQuery != NULL);
01841 
01842     /*
01843      * Generate dummy targetlist using column names of leftmost select and
01844      * dummy result expressions of the non-recursive term.
01845      */
01846     targetList = NIL;
01847     left_tlist = list_head(leftmostQuery->targetList);
01848     next_resno = 1;
01849 
01850     foreach(nrtl, nrtargetlist)
01851     {
01852         TargetEntry *nrtle = (TargetEntry *) lfirst(nrtl);
01853         TargetEntry *lefttle = (TargetEntry *) lfirst(left_tlist);
01854         char       *colName;
01855         TargetEntry *tle;
01856 
01857         Assert(!lefttle->resjunk);
01858         colName = pstrdup(lefttle->resname);
01859         tle = makeTargetEntry(nrtle->expr,
01860                               next_resno++,
01861                               colName,
01862                               false);
01863         targetList = lappend(targetList, tle);
01864         left_tlist = lnext(left_tlist);
01865     }
01866 
01867     /* Now build CTE's output column info using dummy targetlist */
01868     analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
01869 }
01870 
01871 
01872 /*
01873  * transformUpdateStmt -
01874  *    transforms an update statement
01875  */
01876 static Query *
01877 transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
01878 {
01879     Query      *qry = makeNode(Query);
01880     RangeTblEntry *target_rte;
01881     Node       *qual;
01882     ListCell   *origTargetList;
01883     ListCell   *tl;
01884 
01885     qry->commandType = CMD_UPDATE;
01886     pstate->p_is_update = true;
01887 
01888     /* process the WITH clause independently of all else */
01889     if (stmt->withClause)
01890     {
01891         qry->hasRecursive = stmt->withClause->recursive;
01892         qry->cteList = transformWithClause(pstate, stmt->withClause);
01893         qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
01894     }
01895 
01896     qry->resultRelation = setTargetTable(pstate, stmt->relation,
01897                                   interpretInhOption(stmt->relation->inhOpt),
01898                                          true,
01899                                          ACL_UPDATE);
01900 
01901     /*
01902      * the FROM clause is non-standard SQL syntax. We used to be able to do
01903      * this with REPLACE in POSTQUEL so we keep the feature.
01904      */
01905     transformFromClause(pstate, stmt->fromClause);
01906 
01907     qry->targetList = transformTargetList(pstate, stmt->targetList,
01908                                           EXPR_KIND_UPDATE_SOURCE);
01909 
01910     qual = transformWhereClause(pstate, stmt->whereClause,
01911                                 EXPR_KIND_WHERE, "WHERE");
01912 
01913     qry->returningList = transformReturningList(pstate, stmt->returningList);
01914 
01915     qry->rtable = pstate->p_rtable;
01916     qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
01917 
01918     qry->hasSubLinks = pstate->p_hasSubLinks;
01919 
01920     /*
01921      * Now we are done with SELECT-like processing, and can get on with
01922      * transforming the target list to match the UPDATE target columns.
01923      */
01924 
01925     /* Prepare to assign non-conflicting resnos to resjunk attributes */
01926     if (pstate->p_next_resno <= pstate->p_target_relation->rd_rel->relnatts)
01927         pstate->p_next_resno = pstate->p_target_relation->rd_rel->relnatts + 1;
01928 
01929     /* Prepare non-junk columns for assignment to target table */
01930     target_rte = pstate->p_target_rangetblentry;
01931     origTargetList = list_head(stmt->targetList);
01932 
01933     foreach(tl, qry->targetList)
01934     {
01935         TargetEntry *tle = (TargetEntry *) lfirst(tl);
01936         ResTarget  *origTarget;
01937         int         attrno;
01938 
01939         if (tle->resjunk)
01940         {
01941             /*
01942              * Resjunk nodes need no additional processing, but be sure they
01943              * have resnos that do not match any target columns; else rewriter
01944              * or planner might get confused.  They don't need a resname
01945              * either.
01946              */
01947             tle->resno = (AttrNumber) pstate->p_next_resno++;
01948             tle->resname = NULL;
01949             continue;
01950         }
01951         if (origTargetList == NULL)
01952             elog(ERROR, "UPDATE target count mismatch --- internal error");
01953         origTarget = (ResTarget *) lfirst(origTargetList);
01954         Assert(IsA(origTarget, ResTarget));
01955 
01956         attrno = attnameAttNum(pstate->p_target_relation,
01957                                origTarget->name, true);
01958         if (attrno == InvalidAttrNumber)
01959             ereport(ERROR,
01960                     (errcode(ERRCODE_UNDEFINED_COLUMN),
01961                      errmsg("column \"%s\" of relation \"%s\" does not exist",
01962                             origTarget->name,
01963                          RelationGetRelationName(pstate->p_target_relation)),
01964                      parser_errposition(pstate, origTarget->location)));
01965 
01966         updateTargetListEntry(pstate, tle, origTarget->name,
01967                               attrno,
01968                               origTarget->indirection,
01969                               origTarget->location);
01970 
01971         /* Mark the target column as requiring update permissions */
01972         target_rte->modifiedCols = bms_add_member(target_rte->modifiedCols,
01973                                 attrno - FirstLowInvalidHeapAttributeNumber);
01974 
01975         origTargetList = lnext(origTargetList);
01976     }
01977     if (origTargetList != NULL)
01978         elog(ERROR, "UPDATE target count mismatch --- internal error");
01979 
01980     assign_query_collations(pstate, qry);
01981 
01982     return qry;
01983 }
01984 
01985 /*
01986  * transformReturningList -
01987  *  handle a RETURNING clause in INSERT/UPDATE/DELETE
01988  */
01989 static List *
01990 transformReturningList(ParseState *pstate, List *returningList)
01991 {
01992     List       *rlist;
01993     int         save_next_resno;
01994 
01995     if (returningList == NIL)
01996         return NIL;             /* nothing to do */
01997 
01998     /*
01999      * We need to assign resnos starting at one in the RETURNING list. Save
02000      * and restore the main tlist's value of p_next_resno, just in case
02001      * someone looks at it later (probably won't happen).
02002      */
02003     save_next_resno = pstate->p_next_resno;
02004     pstate->p_next_resno = 1;
02005 
02006     /* transform RETURNING identically to a SELECT targetlist */
02007     rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING);
02008 
02009     /* mark column origins */
02010     markTargetListOrigins(pstate, rlist);
02011 
02012     /* restore state */
02013     pstate->p_next_resno = save_next_resno;
02014 
02015     return rlist;
02016 }
02017 
02018 
02019 /*
02020  * transformDeclareCursorStmt -
02021  *  transform a DECLARE CURSOR Statement
02022  *
02023  * DECLARE CURSOR is a hybrid case: it's an optimizable statement (in fact not
02024  * significantly different from a SELECT) as far as parsing/rewriting/planning
02025  * are concerned, but it's not passed to the executor and so in that sense is
02026  * a utility statement.  We transform it into a Query exactly as if it were
02027  * a SELECT, then stick the original DeclareCursorStmt into the utilityStmt
02028  * field to carry the cursor name and options.
02029  */
02030 static Query *
02031 transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
02032 {
02033     Query      *result;
02034 
02035     /*
02036      * Don't allow both SCROLL and NO SCROLL to be specified
02037      */
02038     if ((stmt->options & CURSOR_OPT_SCROLL) &&
02039         (stmt->options & CURSOR_OPT_NO_SCROLL))
02040         ereport(ERROR,
02041                 (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
02042                  errmsg("cannot specify both SCROLL and NO SCROLL")));
02043 
02044     result = transformStmt(pstate, stmt->query);
02045 
02046     /* Grammar should not have allowed anything but SELECT */
02047     if (!IsA(result, Query) ||
02048         result->commandType != CMD_SELECT ||
02049         result->utilityStmt != NULL)
02050         elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
02051 
02052     /*
02053      * We also disallow data-modifying WITH in a cursor.  (This could be
02054      * allowed, but the semantics of when the updates occur might be
02055      * surprising.)
02056      */
02057     if (result->hasModifyingCTE)
02058         ereport(ERROR,
02059                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02060                  errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
02061 
02062     /* FOR UPDATE and WITH HOLD are not compatible */
02063     if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
02064         ereport(ERROR,
02065                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02066                  errmsg("DECLARE CURSOR WITH HOLD ... FOR UPDATE/SHARE is not supported"),
02067                  errdetail("Holdable cursors must be READ ONLY.")));
02068 
02069     /* FOR UPDATE and SCROLL are not compatible */
02070     if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
02071         ereport(ERROR,
02072                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02073         errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
02074                  errdetail("Scrollable cursors must be READ ONLY.")));
02075 
02076     /* FOR UPDATE and INSENSITIVE are not compatible */
02077     if (result->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
02078         ereport(ERROR,
02079                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02080                  errmsg("DECLARE INSENSITIVE CURSOR ... FOR UPDATE/SHARE is not supported"),
02081                  errdetail("Insensitive cursors must be READ ONLY.")));
02082 
02083     /* We won't need the raw querytree any more */
02084     stmt->query = NULL;
02085 
02086     result->utilityStmt = (Node *) stmt;
02087 
02088     return result;
02089 }
02090 
02091 
02092 /*
02093  * transformExplainStmt -
02094  *  transform an EXPLAIN Statement
02095  *
02096  * EXPLAIN is like other utility statements in that we emit it as a
02097  * CMD_UTILITY Query node; however, we must first transform the contained
02098  * query.  We used to postpone that until execution, but it's really necessary
02099  * to do it during the normal parse analysis phase to ensure that side effects
02100  * of parser hooks happen at the expected time.
02101  */
02102 static Query *
02103 transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
02104 {
02105     Query      *result;
02106 
02107     /* transform contained query, allowing SELECT INTO */
02108     stmt->query = (Node *) transformTopLevelStmt(pstate, stmt->query);
02109 
02110     /* represent the command as a utility Query */
02111     result = makeNode(Query);
02112     result->commandType = CMD_UTILITY;
02113     result->utilityStmt = (Node *) stmt;
02114 
02115     return result;
02116 }
02117 
02118 
02119 /*
02120  * transformCreateTableAsStmt -
02121  *  transform a CREATE TABLE AS, SELECT ... INTO, or CREATE MATERIALIZED VIEW
02122  *  Statement
02123  *
02124  * As with EXPLAIN, transform the contained statement now.
02125  */
02126 static Query *
02127 transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
02128 {
02129     Query      *result;
02130     Query      *query;
02131 
02132     /* transform contained query */
02133     query = transformStmt(pstate, stmt->query);
02134     stmt->query = (Node *) query;
02135 
02136     /* additional work needed for CREATE MATERIALIZED VIEW */
02137     if (stmt->relkind == OBJECT_MATVIEW)
02138     {
02139         /*
02140          * Prohibit a data-modifying CTE in the query used to create a
02141          * materialized view. It's not sufficiently clear what the user would
02142          * want to happen if the MV is refreshed or incrementally maintained.
02143          */
02144         if (query->hasModifyingCTE)
02145             ereport(ERROR,
02146                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02147                      errmsg("materialized views must not use data-modifying statements in WITH")));
02148 
02149         /*
02150          * Check whether any temporary database objects are used in the
02151          * creation query. It would be hard to refresh data or incrementally
02152          * maintain it if a source disappeared.
02153          */
02154         if (isQueryUsingTempRelation(query))
02155             ereport(ERROR,
02156                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02157                      errmsg("materialized views must not use temporary tables or views")));
02158 
02159         /*
02160          * A materialized view would either need to save parameters for use in
02161          * maintaining/loading the data or prohibit them entirely.  The latter
02162          * seems safer and more sane.
02163          */
02164         if (query_contains_extern_params(query))
02165             ereport(ERROR,
02166                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02167                      errmsg("materialized views may not be defined using bound parameters")));
02168 
02169         /*
02170          * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
02171          * for purposes of creating the view's ON SELECT rule.  We stash that
02172          * in the IntoClause because that's where intorel_startup() can
02173          * conveniently get it from.
02174          */
02175         stmt->into->viewQuery = copyObject(query);
02176     }
02177 
02178     /* represent the command as a utility Query */
02179     result = makeNode(Query);
02180     result->commandType = CMD_UTILITY;
02181     result->utilityStmt = (Node *) stmt;
02182 
02183     return result;
02184 }
02185 
02186 
02187 /*
02188  * Check for features that are not supported with FOR [KEY] UPDATE/SHARE.
02189  *
02190  * exported so planner can check again after rewriting, query pullup, etc
02191  */
02192 void
02193 CheckSelectLocking(Query *qry)
02194 {
02195     if (qry->setOperations)
02196         ereport(ERROR,
02197                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02198                  errmsg("row-level locks are not allowed with UNION/INTERSECT/EXCEPT")));
02199     if (qry->distinctClause != NIL)
02200         ereport(ERROR,
02201                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02202                  errmsg("row-level locks are not allowed with DISTINCT clause")));
02203     if (qry->groupClause != NIL)
02204         ereport(ERROR,
02205                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02206                  errmsg("row-level locks are not allowed with GROUP BY clause")));
02207     if (qry->havingQual != NULL)
02208         ereport(ERROR,
02209                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02210         errmsg("row-level locks are not allowed with HAVING clause")));
02211     if (qry->hasAggs)
02212         ereport(ERROR,
02213                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02214                  errmsg("row-level locks are not allowed with aggregate functions")));
02215     if (qry->hasWindowFuncs)
02216         ereport(ERROR,
02217                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02218                  errmsg("row-level locks are not allowed with window functions")));
02219     if (expression_returns_set((Node *) qry->targetList))
02220         ereport(ERROR,
02221                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02222                  errmsg("row-level locks are not allowed with set-returning functions in the target list")));
02223 }
02224 
02225 /*
02226  * Transform a FOR [KEY] UPDATE/SHARE clause
02227  *
02228  * This basically involves replacing names by integer relids.
02229  *
02230  * NB: if you need to change this, see also markQueryForLocking()
02231  * in rewriteHandler.c, and isLockedRefname() in parse_relation.c.
02232  */
02233 static void
02234 transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
02235                        bool pushedDown)
02236 {
02237     List       *lockedRels = lc->lockedRels;
02238     ListCell   *l;
02239     ListCell   *rt;
02240     Index       i;
02241     LockingClause *allrels;
02242 
02243     CheckSelectLocking(qry);
02244 
02245     /* make a clause we can pass down to subqueries to select all rels */
02246     allrels = makeNode(LockingClause);
02247     allrels->lockedRels = NIL;  /* indicates all rels */
02248     allrels->strength = lc->strength;
02249     allrels->noWait = lc->noWait;
02250 
02251     if (lockedRels == NIL)
02252     {
02253         /* all regular tables used in query */
02254         i = 0;
02255         foreach(rt, qry->rtable)
02256         {
02257             RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
02258 
02259             ++i;
02260             switch (rte->rtekind)
02261             {
02262                 case RTE_RELATION:
02263                     applyLockingClause(qry, i,
02264                                        lc->strength, lc->noWait, pushedDown);
02265                     rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
02266                     break;
02267                 case RTE_SUBQUERY:
02268                     applyLockingClause(qry, i,
02269                                        lc->strength, lc->noWait, pushedDown);
02270 
02271                     /*
02272                      * FOR UPDATE/SHARE of subquery is propagated to all of
02273                      * subquery's rels, too.  We could do this later (based on
02274                      * the marking of the subquery RTE) but it is convenient
02275                      * to have local knowledge in each query level about which
02276                      * rels need to be opened with RowShareLock.
02277                      */
02278                     transformLockingClause(pstate, rte->subquery,
02279                                            allrels, true);
02280                     break;
02281                 default:
02282                     /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
02283                     break;
02284             }
02285         }
02286     }
02287     else
02288     {
02289         /* just the named tables */
02290         foreach(l, lockedRels)
02291         {
02292             RangeVar   *thisrel = (RangeVar *) lfirst(l);
02293 
02294             /* For simplicity we insist on unqualified alias names here */
02295             if (thisrel->catalogname || thisrel->schemaname)
02296                 ereport(ERROR,
02297                         (errcode(ERRCODE_SYNTAX_ERROR),
02298                          errmsg("row-level locks must specify unqualified relation names"),
02299                          parser_errposition(pstate, thisrel->location)));
02300 
02301             i = 0;
02302             foreach(rt, qry->rtable)
02303             {
02304                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
02305 
02306                 ++i;
02307                 if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
02308                 {
02309                     switch (rte->rtekind)
02310                     {
02311                         case RTE_RELATION:
02312                             applyLockingClause(qry, i,
02313                                                lc->strength, lc->noWait,
02314                                                pushedDown);
02315                             rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
02316                             break;
02317                         case RTE_SUBQUERY:
02318                             applyLockingClause(qry, i,
02319                                                lc->strength, lc->noWait,
02320                                                pushedDown);
02321                             /* see comment above */
02322                             transformLockingClause(pstate, rte->subquery,
02323                                                    allrels, true);
02324                             break;
02325                         case RTE_JOIN:
02326                             ereport(ERROR,
02327                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02328                                      errmsg("row-level locks cannot be applied to a join"),
02329                              parser_errposition(pstate, thisrel->location)));
02330                             break;
02331                         case RTE_FUNCTION:
02332                             ereport(ERROR,
02333                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02334                                      errmsg("row-level locks cannot be applied to a function"),
02335                              parser_errposition(pstate, thisrel->location)));
02336                             break;
02337                         case RTE_VALUES:
02338                             ereport(ERROR,
02339                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02340                                      errmsg("row-level locks cannot be applied to VALUES"),
02341                              parser_errposition(pstate, thisrel->location)));
02342                             break;
02343                         case RTE_CTE:
02344                             ereport(ERROR,
02345                                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
02346                                      errmsg("row-level locks cannot be applied to a WITH query"),
02347                              parser_errposition(pstate, thisrel->location)));
02348                             break;
02349                         default:
02350                             elog(ERROR, "unrecognized RTE type: %d",
02351                                  (int) rte->rtekind);
02352                             break;
02353                     }
02354                     break;      /* out of foreach loop */
02355                 }
02356             }
02357             if (rt == NULL)
02358                 ereport(ERROR,
02359                         (errcode(ERRCODE_UNDEFINED_TABLE),
02360                          errmsg("relation \"%s\" in row-level lock clause not found in FROM clause",
02361                                 thisrel->relname),
02362                          parser_errposition(pstate, thisrel->location)));
02363         }
02364     }
02365 }
02366 
02367 /*
02368  * Record locking info for a single rangetable item
02369  */
02370 void
02371 applyLockingClause(Query *qry, Index rtindex,
02372                    LockClauseStrength strength, bool noWait, bool pushedDown)
02373 {
02374     RowMarkClause *rc;
02375 
02376     /* If it's an explicit clause, make sure hasForUpdate gets set */
02377     if (!pushedDown)
02378         qry->hasForUpdate = true;
02379 
02380     /* Check for pre-existing entry for same rtindex */
02381     if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
02382     {
02383         /*
02384          * If the same RTE is specified for more than one locking strength,
02385          * treat is as the strongest.  (Reasonable, since you can't take both a
02386          * shared and exclusive lock at the same time; it'll end up being
02387          * exclusive anyway.)
02388          *
02389          * We also consider that NOWAIT wins if it's specified both ways. This
02390          * is a bit more debatable but raising an error doesn't seem helpful.
02391          * (Consider for instance SELECT FOR UPDATE NOWAIT from a view that
02392          * internally contains a plain FOR UPDATE spec.)
02393          *
02394          * And of course pushedDown becomes false if any clause is explicit.
02395          */
02396         rc->strength = Max(rc->strength, strength);
02397         rc->noWait |= noWait;
02398         rc->pushedDown &= pushedDown;
02399         return;
02400     }
02401 
02402     /* Make a new RowMarkClause */
02403     rc = makeNode(RowMarkClause);
02404     rc->rti = rtindex;
02405     rc->strength = strength;
02406     rc->noWait = noWait;
02407     rc->pushedDown = pushedDown;
02408     qry->rowMarks = lappend(qry->rowMarks, rc);
02409 }