LCOV - code coverage report
Current view: top level - src/backend/parser - parse_clause.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 845 936 90.3 %
Date: 2017-09-29 15:12:54 Functions: 43 43 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * parse_clause.c
       4             :  *    handle clauses in parser
       5             :  *
       6             :  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/parser/parse_clause.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : 
      16             : #include "postgres.h"
      17             : 
      18             : #include "miscadmin.h"
      19             : 
      20             : #include "access/heapam.h"
      21             : #include "access/tsmapi.h"
      22             : #include "catalog/catalog.h"
      23             : #include "catalog/heap.h"
      24             : #include "catalog/pg_am.h"
      25             : #include "catalog/pg_collation.h"
      26             : #include "catalog/pg_constraint_fn.h"
      27             : #include "catalog/pg_type.h"
      28             : #include "commands/defrem.h"
      29             : #include "nodes/makefuncs.h"
      30             : #include "nodes/nodeFuncs.h"
      31             : #include "optimizer/tlist.h"
      32             : #include "optimizer/var.h"
      33             : #include "parser/analyze.h"
      34             : #include "parser/parsetree.h"
      35             : #include "parser/parser.h"
      36             : #include "parser/parse_clause.h"
      37             : #include "parser/parse_coerce.h"
      38             : #include "parser/parse_collate.h"
      39             : #include "parser/parse_expr.h"
      40             : #include "parser/parse_func.h"
      41             : #include "parser/parse_oper.h"
      42             : #include "parser/parse_relation.h"
      43             : #include "parser/parse_target.h"
      44             : #include "parser/parse_type.h"
      45             : #include "rewrite/rewriteManip.h"
      46             : #include "utils/guc.h"
      47             : #include "utils/lsyscache.h"
      48             : #include "utils/rel.h"
      49             : 
      50             : 
      51             : /* Convenience macro for the most common makeNamespaceItem() case */
      52             : #define makeDefaultNSItem(rte)  makeNamespaceItem(rte, true, true, false, true)
      53             : 
      54             : static void extractRemainingColumns(List *common_colnames,
      55             :                         List *src_colnames, List *src_colvars,
      56             :                         List **res_colnames, List **res_colvars);
      57             : static Node *transformJoinUsingClause(ParseState *pstate,
      58             :                          RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
      59             :                          List *leftVars, List *rightVars);
      60             : static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
      61             :                       List *namespace);
      62             : static RangeTblEntry *getRTEForSpecialRelationTypes(ParseState *pstate,
      63             :                               RangeVar *rv);
      64             : static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
      65             : static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
      66             :                       CommonTableExpr *cte, Index levelsup);
      67             : static RangeTblEntry *transformENRReference(ParseState *pstate, RangeVar *r);
      68             : static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
      69             :                         RangeSubselect *r);
      70             : static RangeTblEntry *transformRangeFunction(ParseState *pstate,
      71             :                        RangeFunction *r);
      72             : static RangeTblEntry *transformRangeTableFunc(ParseState *pstate,
      73             :                         RangeTableFunc *t);
      74             : static TableSampleClause *transformRangeTableSample(ParseState *pstate,
      75             :                           RangeTableSample *rts);
      76             : static Node *transformFromClauseItem(ParseState *pstate, Node *n,
      77             :                         RangeTblEntry **top_rte, int *top_rti,
      78             :                         List **namespace);
      79             : static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
      80             :                    Var *l_colvar, Var *r_colvar);
      81             : static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
      82             :                   bool rel_visible, bool cols_visible,
      83             :                   bool lateral_only, bool lateral_ok);
      84             : static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
      85             : static void setNamespaceLateralState(List *namespace,
      86             :                          bool lateral_only, bool lateral_ok);
      87             : static void checkExprIsVarFree(ParseState *pstate, Node *n,
      88             :                    const char *constructName);
      89             : static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
      90             :                          List **tlist, ParseExprKind exprKind);
      91             : static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
      92             :                          List **tlist, ParseExprKind exprKind);
      93             : static int get_matching_location(int sortgroupref,
      94             :                       List *sortgrouprefs, List *exprs);
      95             : static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
      96             :                           Relation heapRel);
      97             : static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
      98             :                      List *grouplist, List *targetlist, int location);
      99             : static WindowClause *findWindowClause(List *wclist, const char *name);
     100             : static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
     101             :                      Node *clause);
     102             : 
     103             : 
     104             : /*
     105             :  * transformFromClause -
     106             :  *    Process the FROM clause and add items to the query's range table,
     107             :  *    joinlist, and namespace.
     108             :  *
     109             :  * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
     110             :  * lists were initialized to NIL when the pstate was created.
     111             :  * We will add onto any entries already present --- this is needed for rule
     112             :  * processing, as well as for UPDATE and DELETE.
     113             :  */
     114             : void
     115       25492 : transformFromClause(ParseState *pstate, List *frmList)
     116             : {
     117             :     ListCell   *fl;
     118             : 
     119             :     /*
     120             :      * The grammar will have produced a list of RangeVars, RangeSubselects,
     121             :      * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
     122             :      * entries to the rtable), check for duplicate refnames, and then add it
     123             :      * to the joinlist and namespace.
     124             :      *
     125             :      * Note we must process the items left-to-right for proper handling of
     126             :      * LATERAL references.
     127             :      */
     128       39892 :     foreach(fl, frmList)
     129             :     {
     130       14460 :         Node       *n = lfirst(fl);
     131             :         RangeTblEntry *rte;
     132             :         int         rtindex;
     133             :         List       *namespace;
     134             : 
     135       14460 :         n = transformFromClauseItem(pstate, n,
     136             :                                     &rte,
     137             :                                     &rtindex,
     138             :                                     &namespace);
     139             : 
     140       14401 :         checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
     141             : 
     142             :         /* Mark the new namespace items as visible only to LATERAL */
     143       14400 :         setNamespaceLateralState(namespace, true, true);
     144             : 
     145       14400 :         pstate->p_joinlist = lappend(pstate->p_joinlist, n);
     146       14400 :         pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
     147             :     }
     148             : 
     149             :     /*
     150             :      * We're done parsing the FROM list, so make all namespace items
     151             :      * unconditionally visible.  Note that this will also reset lateral_only
     152             :      * for any namespace items that were already present when we were called;
     153             :      * but those should have been that way already.
     154             :      */
     155       25432 :     setNamespaceLateralState(pstate->p_namespace, false, true);
     156       25432 : }
     157             : 
     158             : /*
     159             :  * setTargetTable
     160             :  *    Add the target relation of INSERT/UPDATE/DELETE to the range table,
     161             :  *    and make the special links to it in the ParseState.
     162             :  *
     163             :  *    We also open the target relation and acquire a write lock on it.
     164             :  *    This must be done before processing the FROM list, in case the target
     165             :  *    is also mentioned as a source relation --- we want to be sure to grab
     166             :  *    the write lock before any read lock.
     167             :  *
     168             :  *    If alsoSource is true, add the target to the query's joinlist and
     169             :  *    namespace.  For INSERT, we don't want the target to be joined to;
     170             :  *    it's a destination of tuples, not a source.   For UPDATE/DELETE,
     171             :  *    we do need to scan or join the target.  (NOTE: we do not bother
     172             :  *    to check for namespace conflict; we assume that the namespace was
     173             :  *    initially empty in these cases.)
     174             :  *
     175             :  *    Finally, we mark the relation as requiring the permissions specified
     176             :  *    by requiredPerms.
     177             :  *
     178             :  *    Returns the rangetable index of the target relation.
     179             :  */
     180             : int
     181        4551 : setTargetTable(ParseState *pstate, RangeVar *relation,
     182             :                bool inh, bool alsoSource, AclMode requiredPerms)
     183             : {
     184             :     RangeTblEntry *rte;
     185             :     int         rtindex;
     186             : 
     187             :     /* So far special relations are immutable; so they cannot be targets. */
     188        4551 :     rte = getRTEForSpecialRelationTypes(pstate, relation);
     189        4551 :     if (rte != NULL)
     190           2 :         ereport(ERROR,
     191             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     192             :                  errmsg("relation \"%s\" cannot be the target of a modifying statement",
     193             :                         relation->relname)));
     194             : 
     195             :     /* Close old target; this could only happen for multi-action rules */
     196        4549 :     if (pstate->p_target_relation != NULL)
     197           0 :         heap_close(pstate->p_target_relation, NoLock);
     198             : 
     199             :     /*
     200             :      * Open target rel and grab suitable lock (which we will hold till end of
     201             :      * transaction).
     202             :      *
     203             :      * free_parsestate() will eventually do the corresponding heap_close(),
     204             :      * but *not* release the lock.
     205             :      */
     206        4549 :     pstate->p_target_relation = parserOpenTable(pstate, relation,
     207             :                                                 RowExclusiveLock);
     208             : 
     209             :     /*
     210             :      * Now build an RTE.
     211             :      */
     212        4547 :     rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
     213             :                                         relation->alias, inh, false);
     214        4547 :     pstate->p_target_rangetblentry = rte;
     215             : 
     216             :     /* assume new rte is at end */
     217        4547 :     rtindex = list_length(pstate->p_rtable);
     218        4547 :     Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
     219             : 
     220             :     /*
     221             :      * Override addRangeTableEntry's default ACL_SELECT permissions check, and
     222             :      * instead mark target table as requiring exactly the specified
     223             :      * permissions.
     224             :      *
     225             :      * If we find an explicit reference to the rel later during parse
     226             :      * analysis, we will add the ACL_SELECT bit back again; see
     227             :      * markVarForSelectPriv and its callers.
     228             :      */
     229        4547 :     rte->requiredPerms = requiredPerms;
     230             : 
     231             :     /*
     232             :      * If UPDATE/DELETE, add table to joinlist and namespace.
     233             :      *
     234             :      * Note: some callers know that they can find the new ParseNamespaceItem
     235             :      * at the end of the pstate->p_namespace list.  This is a bit ugly but not
     236             :      * worth complicating this function's signature for.
     237             :      */
     238        4547 :     if (alsoSource)
     239         879 :         addRTEtoQuery(pstate, rte, true, true, true);
     240             : 
     241        4547 :     return rtindex;
     242             : }
     243             : 
     244             : /*
     245             :  * Given a relation-options list (of DefElems), return true iff the specified
     246             :  * table/result set should be created with OIDs. This needs to be done after
     247             :  * parsing the query string because the return value can depend upon the
     248             :  * default_with_oids GUC var.
     249             :  *
     250             :  * In some situations, we want to reject an OIDS option even if it's present.
     251             :  * That's (rather messily) handled here rather than reloptions.c, because that
     252             :  * code explicitly punts checking for oids to here.
     253             :  */
     254             : bool
     255        3760 : interpretOidsOption(List *defList, bool allowOids)
     256             : {
     257             :     ListCell   *cell;
     258             : 
     259             :     /* Scan list to see if OIDS was included */
     260        3840 :     foreach(cell, defList)
     261             :     {
     262         187 :         DefElem    *def = (DefElem *) lfirst(cell);
     263             : 
     264         367 :         if (def->defnamespace == NULL &&
     265         180 :             pg_strcasecmp(def->defname, "oids") == 0)
     266             :         {
     267         107 :             if (!allowOids)
     268           0 :                 ereport(ERROR,
     269             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     270             :                          errmsg("unrecognized parameter \"%s\"",
     271             :                                 def->defname)));
     272         107 :             return defGetBoolean(def);
     273             :         }
     274             :     }
     275             : 
     276             :     /* Force no-OIDS result if caller disallows OIDS. */
     277        3653 :     if (!allowOids)
     278         645 :         return false;
     279             : 
     280             :     /* OIDS option was not specified, so use default. */
     281        3008 :     return default_with_oids;
     282             : }
     283             : 
     284             : /*
     285             :  * Extract all not-in-common columns from column lists of a source table
     286             :  */
     287             : static void
     288        3802 : extractRemainingColumns(List *common_colnames,
     289             :                         List *src_colnames, List *src_colvars,
     290             :                         List **res_colnames, List **res_colvars)
     291             : {
     292        3802 :     List       *new_colnames = NIL;
     293        3802 :     List       *new_colvars = NIL;
     294             :     ListCell   *lnames,
     295             :                *lvars;
     296             : 
     297        3802 :     Assert(list_length(src_colnames) == list_length(src_colvars));
     298             : 
     299       59578 :     forboth(lnames, src_colnames, lvars, src_colvars)
     300             :     {
     301       55776 :         char       *colname = strVal(lfirst(lnames));
     302       55776 :         bool        match = false;
     303             :         ListCell   *cnames;
     304             : 
     305       56544 :         foreach(cnames, common_colnames)
     306             :         {
     307        1014 :             char       *ccolname = strVal(lfirst(cnames));
     308             : 
     309        1014 :             if (strcmp(colname, ccolname) == 0)
     310             :             {
     311         246 :                 match = true;
     312         246 :                 break;
     313             :             }
     314             :         }
     315             : 
     316       55776 :         if (!match)
     317             :         {
     318       55530 :             new_colnames = lappend(new_colnames, lfirst(lnames));
     319       55530 :             new_colvars = lappend(new_colvars, lfirst(lvars));
     320             :         }
     321             :     }
     322             : 
     323        3802 :     *res_colnames = new_colnames;
     324        3802 :     *res_colvars = new_colvars;
     325        3802 : }
     326             : 
     327             : /* transformJoinUsingClause()
     328             :  *    Build a complete ON clause from a partially-transformed USING list.
     329             :  *    We are given lists of nodes representing left and right match columns.
     330             :  *    Result is a transformed qualification expression.
     331             :  */
     332             : static Node *
     333         103 : transformJoinUsingClause(ParseState *pstate,
     334             :                          RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
     335             :                          List *leftVars, List *rightVars)
     336             : {
     337             :     Node       *result;
     338         103 :     List       *andargs = NIL;
     339             :     ListCell   *lvars,
     340             :                *rvars;
     341             : 
     342             :     /*
     343             :      * We cheat a little bit here by building an untransformed operator tree
     344             :      * whose leaves are the already-transformed Vars.  This requires collusion
     345             :      * from transformExpr(), which normally could be expected to complain
     346             :      * about already-transformed subnodes.  However, this does mean that we
     347             :      * have to mark the columns as requiring SELECT privilege for ourselves;
     348             :      * transformExpr() won't do it.
     349             :      */
     350         226 :     forboth(lvars, leftVars, rvars, rightVars)
     351             :     {
     352         123 :         Var        *lvar = (Var *) lfirst(lvars);
     353         123 :         Var        *rvar = (Var *) lfirst(rvars);
     354             :         A_Expr     *e;
     355             : 
     356             :         /* Require read access to the join variables */
     357         123 :         markVarForSelectPriv(pstate, lvar, leftRTE);
     358         123 :         markVarForSelectPriv(pstate, rvar, rightRTE);
     359             : 
     360             :         /* Now create the lvar = rvar join condition */
     361         123 :         e = makeSimpleA_Expr(AEXPR_OP, "=",
     362         123 :                              (Node *) copyObject(lvar), (Node *) copyObject(rvar),
     363             :                              -1);
     364             : 
     365             :         /* Prepare to combine into an AND clause, if multiple join columns */
     366         123 :         andargs = lappend(andargs, e);
     367             :     }
     368             : 
     369             :     /* Only need an AND if there's more than one join column */
     370         103 :     if (list_length(andargs) == 1)
     371          86 :         result = (Node *) linitial(andargs);
     372             :     else
     373          17 :         result = (Node *) makeBoolExpr(AND_EXPR, andargs, -1);
     374             : 
     375             :     /*
     376             :      * Since the references are already Vars, and are certainly from the input
     377             :      * relations, we don't have to go through the same pushups that
     378             :      * transformJoinOnClause() does.  Just invoke transformExpr() to fix up
     379             :      * the operators, and we're done.
     380             :      */
     381         103 :     result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
     382             : 
     383         103 :     result = coerce_to_boolean(pstate, result, "JOIN/USING");
     384             : 
     385         103 :     return result;
     386             : }
     387             : 
     388             : /* transformJoinOnClause()
     389             :  *    Transform the qual conditions for JOIN/ON.
     390             :  *    Result is a transformed qualification expression.
     391             :  */
     392             : static Node *
     393        1776 : transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
     394             : {
     395             :     Node       *result;
     396             :     List       *save_namespace;
     397             : 
     398             :     /*
     399             :      * The namespace that the join expression should see is just the two
     400             :      * subtrees of the JOIN plus any outer references from upper pstate
     401             :      * levels.  Temporarily set this pstate's namespace accordingly.  (We need
     402             :      * not check for refname conflicts, because transformFromClauseItem()
     403             :      * already did.)  All namespace items are marked visible regardless of
     404             :      * LATERAL state.
     405             :      */
     406        1776 :     setNamespaceLateralState(namespace, false, true);
     407             : 
     408        1776 :     save_namespace = pstate->p_namespace;
     409        1776 :     pstate->p_namespace = namespace;
     410             : 
     411        1776 :     result = transformWhereClause(pstate, j->quals,
     412             :                                   EXPR_KIND_JOIN_ON, "JOIN/ON");
     413             : 
     414        1773 :     pstate->p_namespace = save_namespace;
     415             : 
     416        1773 :     return result;
     417             : }
     418             : 
     419             : /*
     420             :  * transformTableEntry --- transform a RangeVar (simple relation reference)
     421             :  */
     422             : static RangeTblEntry *
     423       13987 : transformTableEntry(ParseState *pstate, RangeVar *r)
     424             : {
     425             :     RangeTblEntry *rte;
     426             : 
     427             :     /* We need only build a range table entry */
     428       13987 :     rte = addRangeTableEntry(pstate, r, r->alias, r->inh, true);
     429             : 
     430       13974 :     return rte;
     431             : }
     432             : 
     433             : /*
     434             :  * transformCTEReference --- transform a RangeVar that references a common
     435             :  * table expression (ie, a sub-SELECT defined in a WITH clause)
     436             :  */
     437             : static RangeTblEntry *
     438         206 : transformCTEReference(ParseState *pstate, RangeVar *r,
     439             :                       CommonTableExpr *cte, Index levelsup)
     440             : {
     441             :     RangeTblEntry *rte;
     442             : 
     443         206 :     rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r, true);
     444             : 
     445         205 :     return rte;
     446             : }
     447             : 
     448             : /*
     449             :  * transformENRReference --- transform a RangeVar that references an ephemeral
     450             :  * named relation
     451             :  */
     452             : static RangeTblEntry *
     453          45 : transformENRReference(ParseState *pstate, RangeVar *r)
     454             : {
     455             :     RangeTblEntry *rte;
     456             : 
     457          45 :     rte = addRangeTableEntryForENR(pstate, r, true);
     458             : 
     459          45 :     return rte;
     460             : }
     461             : 
     462             : /*
     463             :  * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
     464             :  */
     465             : static RangeTblEntry *
     466         812 : transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
     467             : {
     468             :     Query      *query;
     469             :     RangeTblEntry *rte;
     470             : 
     471             :     /*
     472             :      * We require user to supply an alias for a subselect, per SQL92. To relax
     473             :      * this, we'd have to be prepared to gin up a unique alias for an
     474             :      * unlabeled subselect.  (This is just elog, not ereport, because the
     475             :      * grammar should have enforced it already.  It'd probably be better to
     476             :      * report the error here, but we don't have a good error location here.)
     477             :      */
     478         812 :     if (r->alias == NULL)
     479           0 :         elog(ERROR, "subquery in FROM must have an alias");
     480             : 
     481             :     /*
     482             :      * Set p_expr_kind to show this parse level is recursing to a subselect.
     483             :      * We can't be nested within any expression, so don't need save-restore
     484             :      * logic here.
     485             :      */
     486         812 :     Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
     487         812 :     pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
     488             : 
     489             :     /*
     490             :      * If the subselect is LATERAL, make lateral_only names of this level
     491             :      * visible to it.  (LATERAL can't nest within a single pstate level, so we
     492             :      * don't need save/restore logic here.)
     493             :      */
     494         812 :     Assert(!pstate->p_lateral_active);
     495         812 :     pstate->p_lateral_active = r->lateral;
     496             : 
     497             :     /*
     498             :      * Analyze and transform the subquery.
     499             :      */
     500         812 :     query = parse_sub_analyze(r->subquery, pstate, NULL,
     501         812 :                               isLockedRefname(pstate, r->alias->aliasname),
     502             :                               true);
     503             : 
     504             :     /* Restore state */
     505         795 :     pstate->p_lateral_active = false;
     506         795 :     pstate->p_expr_kind = EXPR_KIND_NONE;
     507             : 
     508             :     /*
     509             :      * Check that we got a SELECT.  Anything else should be impossible given
     510             :      * restrictions of the grammar, but check anyway.
     511             :      */
     512        1590 :     if (!IsA(query, Query) ||
     513         795 :         query->commandType != CMD_SELECT)
     514           0 :         elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
     515             : 
     516             :     /*
     517             :      * OK, build an RTE for the subquery.
     518             :      */
     519         795 :     rte = addRangeTableEntryForSubquery(pstate,
     520             :                                         query,
     521             :                                         r->alias,
     522         795 :                                         r->lateral,
     523             :                                         true);
     524             : 
     525         795 :     return rte;
     526             : }
     527             : 
     528             : 
     529             : /*
     530             :  * transformRangeFunction --- transform a function call appearing in FROM
     531             :  */
     532             : static RangeTblEntry *
     533        1294 : transformRangeFunction(ParseState *pstate, RangeFunction *r)
     534             : {
     535        1294 :     List       *funcexprs = NIL;
     536        1294 :     List       *funcnames = NIL;
     537        1294 :     List       *coldeflists = NIL;
     538             :     bool        is_lateral;
     539             :     RangeTblEntry *rte;
     540             :     ListCell   *lc;
     541             : 
     542             :     /*
     543             :      * We make lateral_only names of this level visible, whether or not the
     544             :      * RangeFunction is explicitly marked LATERAL.  This is needed for SQL
     545             :      * spec compliance in the case of UNNEST(), and seems useful on
     546             :      * convenience grounds for all functions in FROM.
     547             :      *
     548             :      * (LATERAL can't nest within a single pstate level, so we don't need
     549             :      * save/restore logic here.)
     550             :      */
     551        1294 :     Assert(!pstate->p_lateral_active);
     552        1294 :     pstate->p_lateral_active = true;
     553             : 
     554             :     /*
     555             :      * Transform the raw expressions.
     556             :      *
     557             :      * While transforming, also save function names for possible use as alias
     558             :      * and column names.  We use the same transformation rules as for a SELECT
     559             :      * output expression.  For a FuncCall node, the result will be the
     560             :      * function name, but it is possible for the grammar to hand back other
     561             :      * node types.
     562             :      *
     563             :      * We have to get this info now, because FigureColname only works on raw
     564             :      * parsetrees.  Actually deciding what to do with the names is left up to
     565             :      * addRangeTableEntryForFunction.
     566             :      *
     567             :      * Likewise, collect column definition lists if there were any.  But
     568             :      * complain if we find one here and the RangeFunction has one too.
     569             :      */
     570        2608 :     foreach(lc, r->functions)
     571             :     {
     572        1325 :         List       *pair = (List *) lfirst(lc);
     573             :         Node       *fexpr;
     574             :         List       *coldeflist;
     575             :         Node       *newfexpr;
     576             :         Node       *last_srf;
     577             : 
     578             :         /* Disassemble the function-call/column-def-list pairs */
     579        1325 :         Assert(list_length(pair) == 2);
     580        1325 :         fexpr = (Node *) linitial(pair);
     581        1325 :         coldeflist = (List *) lsecond(pair);
     582             : 
     583             :         /*
     584             :          * If we find a function call unnest() with more than one argument and
     585             :          * no special decoration, transform it into separate unnest() calls on
     586             :          * each argument.  This is a kluge, for sure, but it's less nasty than
     587             :          * other ways of implementing the SQL-standard UNNEST() syntax.
     588             :          *
     589             :          * If there is any decoration (including a coldeflist), we don't
     590             :          * transform, which probably means a no-such-function error later.  We
     591             :          * could alternatively throw an error right now, but that doesn't seem
     592             :          * tremendously helpful.  If someone is using any such decoration,
     593             :          * then they're not using the SQL-standard syntax, and they're more
     594             :          * likely expecting an un-tweaked function call.
     595             :          *
     596             :          * Note: the transformation changes a non-schema-qualified unnest()
     597             :          * function name into schema-qualified pg_catalog.unnest().  This
     598             :          * choice is also a bit debatable, but it seems reasonable to force
     599             :          * use of built-in unnest() when we make this transformation.
     600             :          */
     601        1325 :         if (IsA(fexpr, FuncCall))
     602             :         {
     603        1320 :             FuncCall   *fc = (FuncCall *) fexpr;
     604             : 
     605        2229 :             if (list_length(fc->funcname) == 1 &&
     606         942 :                 strcmp(strVal(linitial(fc->funcname)), "unnest") == 0 &&
     607          40 :                 list_length(fc->args) > 1 &&
     608          14 :                 fc->agg_order == NIL &&
     609          14 :                 fc->agg_filter == NULL &&
     610          14 :                 !fc->agg_star &&
     611          14 :                 !fc->agg_distinct &&
     612          14 :                 !fc->func_variadic &&
     613          14 :                 fc->over == NULL &&
     614             :                 coldeflist == NIL)
     615             :             {
     616             :                 ListCell   *lc;
     617             : 
     618          26 :                 foreach(lc, fc->args)
     619             :                 {
     620          19 :                     Node       *arg = (Node *) lfirst(lc);
     621             :                     FuncCall   *newfc;
     622             : 
     623          19 :                     last_srf = pstate->p_last_srf;
     624             : 
     625          19 :                     newfc = makeFuncCall(SystemFuncName("unnest"),
     626             :                                          list_make1(arg),
     627             :                                          fc->location);
     628             : 
     629          19 :                     newfexpr = transformExpr(pstate, (Node *) newfc,
     630             :                                              EXPR_KIND_FROM_FUNCTION);
     631             : 
     632             :                     /* nodeFunctionscan.c requires SRFs to be at top level */
     633          38 :                     if (pstate->p_last_srf != last_srf &&
     634          19 :                         pstate->p_last_srf != newfexpr)
     635           0 :                         ereport(ERROR,
     636             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     637             :                                  errmsg("set-returning functions must appear at top level of FROM"),
     638             :                                  parser_errposition(pstate,
     639             :                                                     exprLocation(pstate->p_last_srf))));
     640             : 
     641          19 :                     funcexprs = lappend(funcexprs, newfexpr);
     642             : 
     643          19 :                     funcnames = lappend(funcnames,
     644          19 :                                         FigureColname((Node *) newfc));
     645             : 
     646             :                     /* coldeflist is empty, so no error is possible */
     647             : 
     648          19 :                     coldeflists = lappend(coldeflists, coldeflist);
     649             :                 }
     650           7 :                 continue;       /* done with this function item */
     651             :             }
     652             :         }
     653             : 
     654             :         /* normal case ... */
     655        1318 :         last_srf = pstate->p_last_srf;
     656             : 
     657        1318 :         newfexpr = transformExpr(pstate, fexpr,
     658             :                                  EXPR_KIND_FROM_FUNCTION);
     659             : 
     660             :         /* nodeFunctionscan.c requires SRFs to be at top level */
     661        2317 :         if (pstate->p_last_srf != last_srf &&
     662        1009 :             pstate->p_last_srf != newfexpr)
     663           1 :             ereport(ERROR,
     664             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     665             :                      errmsg("set-returning functions must appear at top level of FROM"),
     666             :                      parser_errposition(pstate,
     667             :                                         exprLocation(pstate->p_last_srf))));
     668             : 
     669        1307 :         funcexprs = lappend(funcexprs, newfexpr);
     670             : 
     671        1307 :         funcnames = lappend(funcnames,
     672        1307 :                             FigureColname(fexpr));
     673             : 
     674        1307 :         if (coldeflist && r->coldeflist)
     675           0 :             ereport(ERROR,
     676             :                     (errcode(ERRCODE_SYNTAX_ERROR),
     677             :                      errmsg("multiple column definition lists are not allowed for the same function"),
     678             :                      parser_errposition(pstate,
     679             :                                         exprLocation((Node *) r->coldeflist))));
     680             : 
     681        1307 :         coldeflists = lappend(coldeflists, coldeflist);
     682             :     }
     683             : 
     684        1283 :     pstate->p_lateral_active = false;
     685             : 
     686             :     /*
     687             :      * We must assign collations now so that the RTE exposes correct collation
     688             :      * info for Vars created from it.
     689             :      */
     690        1283 :     assign_list_collations(pstate, funcexprs);
     691             : 
     692             :     /*
     693             :      * Install the top-level coldeflist if there was one (we already checked
     694             :      * that there was no conflicting per-function coldeflist).
     695             :      *
     696             :      * We only allow this when there's a single function (even after UNNEST
     697             :      * expansion) and no WITH ORDINALITY.  The reason for the latter
     698             :      * restriction is that it's not real clear whether the ordinality column
     699             :      * should be in the coldeflist, and users are too likely to make mistakes
     700             :      * in one direction or the other.  Putting the coldeflist inside ROWS
     701             :      * FROM() is much clearer in this case.
     702             :      */
     703        1283 :     if (r->coldeflist)
     704             :     {
     705          38 :         if (list_length(funcexprs) != 1)
     706             :         {
     707           0 :             if (r->is_rowsfrom)
     708           0 :                 ereport(ERROR,
     709             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     710             :                          errmsg("ROWS FROM() with multiple functions cannot have a column definition list"),
     711             :                          errhint("Put a separate column definition list for each function inside ROWS FROM()."),
     712             :                          parser_errposition(pstate,
     713             :                                             exprLocation((Node *) r->coldeflist))));
     714             :             else
     715           0 :                 ereport(ERROR,
     716             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     717             :                          errmsg("UNNEST() with multiple arguments cannot have a column definition list"),
     718             :                          errhint("Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one."),
     719             :                          parser_errposition(pstate,
     720             :                                             exprLocation((Node *) r->coldeflist))));
     721             :         }
     722          38 :         if (r->ordinality)
     723           0 :             ereport(ERROR,
     724             :                     (errcode(ERRCODE_SYNTAX_ERROR),
     725             :                      errmsg("WITH ORDINALITY cannot be used with a column definition list"),
     726             :                      errhint("Put the column definition list inside ROWS FROM()."),
     727             :                      parser_errposition(pstate,
     728             :                                         exprLocation((Node *) r->coldeflist))));
     729             : 
     730          38 :         coldeflists = list_make1(r->coldeflist);
     731             :     }
     732             : 
     733             :     /*
     734             :      * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
     735             :      * there are any lateral cross-references in it.
     736             :      */
     737        1283 :     is_lateral = r->lateral || contain_vars_of_level((Node *) funcexprs, 0);
     738             : 
     739             :     /*
     740             :      * OK, build an RTE for the function.
     741             :      */
     742        1283 :     rte = addRangeTableEntryForFunction(pstate,
     743             :                                         funcnames, funcexprs, coldeflists,
     744             :                                         r, is_lateral, true);
     745             : 
     746        1280 :     return rte;
     747             : }
     748             : 
     749             : /*
     750             :  * transformRangeTableFunc -
     751             :  *          Transform a raw RangeTableFunc into TableFunc.
     752             :  *
     753             :  * Transform the namespace clauses, the document-generating expression, the
     754             :  * row-generating expression, the column-generating expressions, and the
     755             :  * default value expressions.
     756             :  */
     757             : static RangeTblEntry *
     758          28 : transformRangeTableFunc(ParseState *pstate, RangeTableFunc *rtf)
     759             : {
     760          28 :     TableFunc  *tf = makeNode(TableFunc);
     761             :     const char *constructName;
     762             :     Oid         docType;
     763             :     RangeTblEntry *rte;
     764             :     bool        is_lateral;
     765             :     ListCell   *col;
     766             :     char      **names;
     767             :     int         colno;
     768             : 
     769             :     /* Currently only XMLTABLE is supported */
     770          28 :     constructName = "XMLTABLE";
     771          28 :     docType = XMLOID;
     772             : 
     773             :     /*
     774             :      * We make lateral_only names of this level visible, whether or not the
     775             :      * RangeTableFunc is explicitly marked LATERAL.  This is needed for SQL
     776             :      * spec compliance and seems useful on convenience grounds for all
     777             :      * functions in FROM.
     778             :      *
     779             :      * (LATERAL can't nest within a single pstate level, so we don't need
     780             :      * save/restore logic here.)
     781             :      */
     782          28 :     Assert(!pstate->p_lateral_active);
     783          28 :     pstate->p_lateral_active = true;
     784             : 
     785             :     /* Transform and apply typecast to the row-generating expression ... */
     786          28 :     Assert(rtf->rowexpr != NULL);
     787          28 :     tf->rowexpr = coerce_to_specific_type(pstate,
     788             :                                           transformExpr(pstate, rtf->rowexpr, EXPR_KIND_FROM_FUNCTION),
     789             :                                           TEXTOID,
     790             :                                           constructName);
     791          28 :     assign_expr_collations(pstate, tf->rowexpr);
     792             : 
     793             :     /* ... and to the document itself */
     794          28 :     Assert(rtf->docexpr != NULL);
     795          28 :     tf->docexpr = coerce_to_specific_type(pstate,
     796             :                                           transformExpr(pstate, rtf->docexpr, EXPR_KIND_FROM_FUNCTION),
     797             :                                           docType,
     798             :                                           constructName);
     799          20 :     assign_expr_collations(pstate, tf->docexpr);
     800             : 
     801             :     /* undef ordinality column number */
     802          20 :     tf->ordinalitycol = -1;
     803             : 
     804             : 
     805          20 :     names = palloc(sizeof(char *) * list_length(rtf->columns));
     806             : 
     807          20 :     colno = 0;
     808         109 :     foreach(col, rtf->columns)
     809             :     {
     810          89 :         RangeTableFuncCol *rawc = (RangeTableFuncCol *) lfirst(col);
     811             :         Oid         typid;
     812             :         int32       typmod;
     813             :         Node       *colexpr;
     814             :         Node       *coldefexpr;
     815             :         int         j;
     816             : 
     817          89 :         tf->colnames = lappend(tf->colnames,
     818          89 :                                makeString(pstrdup(rawc->colname)));
     819             : 
     820             :         /*
     821             :          * Determine the type and typmod for the new column. FOR ORDINALITY
     822             :          * columns are INTEGER per spec; the others are user-specified.
     823             :          */
     824          89 :         if (rawc->for_ordinality)
     825             :         {
     826          10 :             if (tf->ordinalitycol != -1)
     827           0 :                 ereport(ERROR,
     828             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     829             :                          errmsg("only one FOR ORDINALITY column is allowed"),
     830             :                          parser_errposition(pstate, rawc->location)));
     831             : 
     832          10 :             typid = INT4OID;
     833          10 :             typmod = -1;
     834          10 :             tf->ordinalitycol = colno;
     835             :         }
     836             :         else
     837             :         {
     838          79 :             if (rawc->typeName->setof)
     839           0 :                 ereport(ERROR,
     840             :                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
     841             :                          errmsg("column \"%s\" cannot be declared SETOF",
     842             :                                 rawc->colname),
     843             :                          parser_errposition(pstate, rawc->location)));
     844             : 
     845          79 :             typenameTypeIdAndMod(pstate, rawc->typeName,
     846             :                                  &typid, &typmod);
     847             :         }
     848             : 
     849          89 :         tf->coltypes = lappend_oid(tf->coltypes, typid);
     850          89 :         tf->coltypmods = lappend_int(tf->coltypmods, typmod);
     851          89 :         tf->colcollations = lappend_oid(tf->colcollations,
     852          89 :                                         type_is_collatable(typid) ? DEFAULT_COLLATION_OID : InvalidOid);
     853             : 
     854             :         /* Transform the PATH and DEFAULT expressions */
     855          89 :         if (rawc->colexpr)
     856             :         {
     857          65 :             colexpr = coerce_to_specific_type(pstate,
     858             :                                               transformExpr(pstate, rawc->colexpr,
     859             :                                                             EXPR_KIND_FROM_FUNCTION),
     860             :                                               TEXTOID,
     861             :                                               constructName);
     862          65 :             assign_expr_collations(pstate, colexpr);
     863             :         }
     864             :         else
     865          24 :             colexpr = NULL;
     866             : 
     867          89 :         if (rawc->coldefexpr)
     868             :         {
     869           9 :             coldefexpr = coerce_to_specific_type_typmod(pstate,
     870             :                                                         transformExpr(pstate, rawc->coldefexpr,
     871             :                                                                       EXPR_KIND_FROM_FUNCTION),
     872             :                                                         typid, typmod,
     873             :                                                         constructName);
     874           9 :             assign_expr_collations(pstate, coldefexpr);
     875             :         }
     876             :         else
     877          80 :             coldefexpr = NULL;
     878             : 
     879          89 :         tf->colexprs = lappend(tf->colexprs, colexpr);
     880          89 :         tf->coldefexprs = lappend(tf->coldefexprs, coldefexpr);
     881             : 
     882          89 :         if (rawc->is_not_null)
     883           9 :             tf->notnulls = bms_add_member(tf->notnulls, colno);
     884             : 
     885             :         /* make sure column names are unique */
     886         334 :         for (j = 0; j < colno; j++)
     887         245 :             if (strcmp(names[j], rawc->colname) == 0)
     888           0 :                 ereport(ERROR,
     889             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     890             :                          errmsg("column name \"%s\" is not unique",
     891             :                                 rawc->colname),
     892             :                          parser_errposition(pstate, rawc->location)));
     893          89 :         names[colno] = rawc->colname;
     894             : 
     895          89 :         colno++;
     896             :     }
     897          20 :     pfree(names);
     898             : 
     899             :     /* Namespaces, if any, also need to be transformed */
     900          20 :     if (rtf->namespaces != NIL)
     901             :     {
     902             :         ListCell   *ns;
     903             :         ListCell   *lc2;
     904           0 :         List       *ns_uris = NIL;
     905           0 :         List       *ns_names = NIL;
     906           0 :         bool        default_ns_seen = false;
     907             : 
     908           0 :         foreach(ns, rtf->namespaces)
     909             :         {
     910           0 :             ResTarget  *r = (ResTarget *) lfirst(ns);
     911             :             Node       *ns_uri;
     912             : 
     913           0 :             Assert(IsA(r, ResTarget));
     914           0 :             ns_uri = transformExpr(pstate, r->val, EXPR_KIND_FROM_FUNCTION);
     915           0 :             ns_uri = coerce_to_specific_type(pstate, ns_uri,
     916             :                                              TEXTOID, constructName);
     917           0 :             assign_expr_collations(pstate, ns_uri);
     918           0 :             ns_uris = lappend(ns_uris, ns_uri);
     919             : 
     920             :             /* Verify consistency of name list: no dupes, only one DEFAULT */
     921           0 :             if (r->name != NULL)
     922             :             {
     923           0 :                 foreach(lc2, ns_names)
     924             :                 {
     925           0 :                     char       *name = strVal(lfirst(lc2));
     926             : 
     927           0 :                     if (name == NULL)
     928           0 :                         continue;
     929           0 :                     if (strcmp(name, r->name) == 0)
     930           0 :                         ereport(ERROR,
     931             :                                 (errcode(ERRCODE_SYNTAX_ERROR),
     932             :                                  errmsg("namespace name \"%s\" is not unique",
     933             :                                         name),
     934             :                                  parser_errposition(pstate, r->location)));
     935             :                 }
     936             :             }
     937             :             else
     938             :             {
     939           0 :                 if (default_ns_seen)
     940           0 :                     ereport(ERROR,
     941             :                             (errcode(ERRCODE_SYNTAX_ERROR),
     942             :                              errmsg("only one default namespace is allowed"),
     943             :                              parser_errposition(pstate, r->location)));
     944           0 :                 default_ns_seen = true;
     945             :             }
     946             : 
     947             :             /* Note the string may be NULL */
     948           0 :             ns_names = lappend(ns_names, makeString(r->name));
     949             :         }
     950             : 
     951           0 :         tf->ns_uris = ns_uris;
     952           0 :         tf->ns_names = ns_names;
     953             :     }
     954             : 
     955          20 :     tf->location = rtf->location;
     956             : 
     957          20 :     pstate->p_lateral_active = false;
     958             : 
     959             :     /*
     960             :      * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
     961             :      * there are any lateral cross-references in it.
     962             :      */
     963          20 :     is_lateral = rtf->lateral || contain_vars_of_level((Node *) tf, 0);
     964             : 
     965          20 :     rte = addRangeTableEntryForTableFunc(pstate,
     966             :                                          tf, rtf->alias, is_lateral, true);
     967             : 
     968          20 :     return rte;
     969             : }
     970             : 
     971             : /*
     972             :  * transformRangeTableSample --- transform a TABLESAMPLE clause
     973             :  *
     974             :  * Caller has already transformed rts->relation, we just have to validate
     975             :  * the remaining fields and create a TableSampleClause node.
     976             :  */
     977             : static TableSampleClause *
     978          31 : transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
     979             : {
     980             :     TableSampleClause *tablesample;
     981             :     Oid         handlerOid;
     982             :     Oid         funcargtypes[1];
     983             :     TsmRoutine *tsm;
     984             :     List       *fargs;
     985             :     ListCell   *larg,
     986             :                *ltyp;
     987             : 
     988             :     /*
     989             :      * To validate the sample method name, look up the handler function, which
     990             :      * has the same name, one dummy INTERNAL argument, and a result type of
     991             :      * tsm_handler.  (Note: tablesample method names are not schema-qualified
     992             :      * in the SQL standard; but since they are just functions to us, we allow
     993             :      * schema qualification to resolve any potential ambiguity.)
     994             :      */
     995          31 :     funcargtypes[0] = INTERNALOID;
     996             : 
     997          31 :     handlerOid = LookupFuncName(rts->method, 1, funcargtypes, true);
     998             : 
     999             :     /* we want error to complain about no-such-method, not no-such-function */
    1000          31 :     if (!OidIsValid(handlerOid))
    1001           1 :         ereport(ERROR,
    1002             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    1003             :                  errmsg("tablesample method %s does not exist",
    1004             :                         NameListToString(rts->method)),
    1005             :                  parser_errposition(pstate, rts->location)));
    1006             : 
    1007             :     /* check that handler has correct return type */
    1008          30 :     if (get_func_rettype(handlerOid) != TSM_HANDLEROID)
    1009           0 :         ereport(ERROR,
    1010             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1011             :                  errmsg("function %s must return type %s",
    1012             :                         NameListToString(rts->method), "tsm_handler"),
    1013             :                  parser_errposition(pstate, rts->location)));
    1014             : 
    1015             :     /* OK, run the handler to get TsmRoutine, for argument type info */
    1016          30 :     tsm = GetTsmRoutine(handlerOid);
    1017             : 
    1018          30 :     tablesample = makeNode(TableSampleClause);
    1019          30 :     tablesample->tsmhandler = handlerOid;
    1020             : 
    1021             :     /* check user provided the expected number of arguments */
    1022          30 :     if (list_length(rts->args) != list_length(tsm->parameterTypes))
    1023           0 :         ereport(ERROR,
    1024             :                 (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
    1025             :                  errmsg_plural("tablesample method %s requires %d argument, not %d",
    1026             :                                "tablesample method %s requires %d arguments, not %d",
    1027             :                                list_length(tsm->parameterTypes),
    1028             :                                NameListToString(rts->method),
    1029             :                                list_length(tsm->parameterTypes),
    1030             :                                list_length(rts->args)),
    1031             :                  parser_errposition(pstate, rts->location)));
    1032             : 
    1033             :     /*
    1034             :      * Transform the arguments, typecasting them as needed.  Note we must also
    1035             :      * assign collations now, because assign_query_collations() doesn't
    1036             :      * examine any substructure of RTEs.
    1037             :      */
    1038          30 :     fargs = NIL;
    1039          60 :     forboth(larg, rts->args, ltyp, tsm->parameterTypes)
    1040             :     {
    1041          30 :         Node       *arg = (Node *) lfirst(larg);
    1042          30 :         Oid         argtype = lfirst_oid(ltyp);
    1043             : 
    1044          30 :         arg = transformExpr(pstate, arg, EXPR_KIND_FROM_FUNCTION);
    1045          30 :         arg = coerce_to_specific_type(pstate, arg, argtype, "TABLESAMPLE");
    1046          30 :         assign_expr_collations(pstate, arg);
    1047          30 :         fargs = lappend(fargs, arg);
    1048             :     }
    1049          30 :     tablesample->args = fargs;
    1050             : 
    1051             :     /* Process REPEATABLE (seed) */
    1052          30 :     if (rts->repeatable != NULL)
    1053             :     {
    1054             :         Node       *arg;
    1055             : 
    1056          14 :         if (!tsm->repeatable_across_queries)
    1057           0 :             ereport(ERROR,
    1058             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1059             :                      errmsg("tablesample method %s does not support REPEATABLE",
    1060             :                             NameListToString(rts->method)),
    1061             :                      parser_errposition(pstate, rts->location)));
    1062             : 
    1063          14 :         arg = transformExpr(pstate, rts->repeatable, EXPR_KIND_FROM_FUNCTION);
    1064          14 :         arg = coerce_to_specific_type(pstate, arg, FLOAT8OID, "REPEATABLE");
    1065          14 :         assign_expr_collations(pstate, arg);
    1066          14 :         tablesample->repeatable = (Expr *) arg;
    1067             :     }
    1068             :     else
    1069          16 :         tablesample->repeatable = NULL;
    1070             : 
    1071          30 :     return tablesample;
    1072             : }
    1073             : 
    1074             : 
    1075             : static RangeTblEntry *
    1076       12945 : getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
    1077             : {
    1078             :     CommonTableExpr *cte;
    1079             :     Index       levelsup;
    1080       12945 :     RangeTblEntry *rte = NULL;
    1081             : 
    1082       12945 :     cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
    1083       12945 :     if (cte)
    1084         206 :         rte = transformCTEReference(pstate, rv, cte, levelsup);
    1085       12944 :     if (!rte && scanNameSpaceForENR(pstate, rv->relname))
    1086          45 :         rte = transformENRReference(pstate, rv);
    1087             : 
    1088       12944 :     return rte;
    1089             : }
    1090             : 
    1091             : /*
    1092             :  * transformFromClauseItem -
    1093             :  *    Transform a FROM-clause item, adding any required entries to the
    1094             :  *    range table list being built in the ParseState, and return the
    1095             :  *    transformed item ready to include in the joinlist.  Also build a
    1096             :  *    ParseNamespaceItem list describing the names exposed by this item.
    1097             :  *    This routine can recurse to handle SQL92 JOIN expressions.
    1098             :  *
    1099             :  * The function return value is the node to add to the jointree (a
    1100             :  * RangeTblRef or JoinExpr).  Additional output parameters are:
    1101             :  *
    1102             :  * *top_rte: receives the RTE corresponding to the jointree item.
    1103             :  * (We could extract this from the function return node, but it saves cycles
    1104             :  * to pass it back separately.)
    1105             :  *
    1106             :  * *top_rti: receives the rangetable index of top_rte.  (Ditto.)
    1107             :  *
    1108             :  * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
    1109             :  * as table/column names by this item.  (The lateral_only flags in these items
    1110             :  * are indeterminate and should be explicitly set by the caller before use.)
    1111             :  */
    1112             : static Node *
    1113       18313 : transformFromClauseItem(ParseState *pstate, Node *n,
    1114             :                         RangeTblEntry **top_rte, int *top_rti,
    1115             :                         List **namespace)
    1116             : {
    1117       18313 :     if (IsA(n, RangeVar))
    1118             :     {
    1119             :         /* Plain relation reference, or perhaps a CTE reference */
    1120       14236 :         RangeVar   *rv = (RangeVar *) n;
    1121             :         RangeTblRef *rtr;
    1122       14236 :         RangeTblEntry *rte = NULL;
    1123             :         int         rtindex;
    1124             : 
    1125             :         /*
    1126             :          * if it is an unqualified name, it might be a CTE or tuplestore
    1127             :          * reference
    1128             :          */
    1129       14236 :         if (!rv->schemaname)
    1130        8394 :             rte = getRTEForSpecialRelationTypes(pstate, rv);
    1131             : 
    1132             :         /* if not found above, must be a table reference */
    1133       14235 :         if (!rte)
    1134       13987 :             rte = transformTableEntry(pstate, rv);
    1135             : 
    1136             :         /* assume new rte is at end */
    1137       14222 :         rtindex = list_length(pstate->p_rtable);
    1138       14222 :         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
    1139       14222 :         *top_rte = rte;
    1140       14222 :         *top_rti = rtindex;
    1141       14222 :         *namespace = list_make1(makeDefaultNSItem(rte));
    1142       14222 :         rtr = makeNode(RangeTblRef);
    1143       14222 :         rtr->rtindex = rtindex;
    1144       14222 :         return (Node *) rtr;
    1145             :     }
    1146        4077 :     else if (IsA(n, RangeSubselect))
    1147             :     {
    1148             :         /* sub-SELECT is like a plain relation */
    1149             :         RangeTblRef *rtr;
    1150             :         RangeTblEntry *rte;
    1151             :         int         rtindex;
    1152             : 
    1153         812 :         rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
    1154             :         /* assume new rte is at end */
    1155         795 :         rtindex = list_length(pstate->p_rtable);
    1156         795 :         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
    1157         795 :         *top_rte = rte;
    1158         795 :         *top_rti = rtindex;
    1159         795 :         *namespace = list_make1(makeDefaultNSItem(rte));
    1160         795 :         rtr = makeNode(RangeTblRef);
    1161         795 :         rtr->rtindex = rtindex;
    1162         795 :         return (Node *) rtr;
    1163             :     }
    1164        3265 :     else if (IsA(n, RangeFunction))
    1165             :     {
    1166             :         /* function is like a plain relation */
    1167             :         RangeTblRef *rtr;
    1168             :         RangeTblEntry *rte;
    1169             :         int         rtindex;
    1170             : 
    1171        1294 :         rte = transformRangeFunction(pstate, (RangeFunction *) n);
    1172             :         /* assume new rte is at end */
    1173        1280 :         rtindex = list_length(pstate->p_rtable);
    1174        1280 :         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
    1175        1280 :         *top_rte = rte;
    1176        1280 :         *top_rti = rtindex;
    1177        1280 :         *namespace = list_make1(makeDefaultNSItem(rte));
    1178        1280 :         rtr = makeNode(RangeTblRef);
    1179        1280 :         rtr->rtindex = rtindex;
    1180        1280 :         return (Node *) rtr;
    1181             :     }
    1182        1971 :     else if (IsA(n, RangeTableFunc))
    1183             :     {
    1184             :         /* table function is like a plain relation */
    1185             :         RangeTblRef *rtr;
    1186             :         RangeTblEntry *rte;
    1187             :         int         rtindex;
    1188             : 
    1189          28 :         rte = transformRangeTableFunc(pstate, (RangeTableFunc *) n);
    1190             :         /* assume new rte is at end */
    1191          20 :         rtindex = list_length(pstate->p_rtable);
    1192          20 :         Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
    1193          20 :         *top_rte = rte;
    1194          20 :         *top_rti = rtindex;
    1195          20 :         *namespace = list_make1(makeDefaultNSItem(rte));
    1196          20 :         rtr = makeNode(RangeTblRef);
    1197          20 :         rtr->rtindex = rtindex;
    1198          20 :         return (Node *) rtr;
    1199             :     }
    1200        1943 :     else if (IsA(n, RangeTableSample))
    1201             :     {
    1202             :         /* TABLESAMPLE clause (wrapping some other valid FROM node) */
    1203          33 :         RangeTableSample *rts = (RangeTableSample *) n;
    1204             :         Node       *rel;
    1205             :         RangeTblRef *rtr;
    1206             :         RangeTblEntry *rte;
    1207             : 
    1208             :         /* Recursively transform the contained relation */
    1209          33 :         rel = transformFromClauseItem(pstate, rts->relation,
    1210             :                                       top_rte, top_rti, namespace);
    1211             :         /* Currently, grammar could only return a RangeVar as contained rel */
    1212          33 :         rtr = castNode(RangeTblRef, rel);
    1213          33 :         rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
    1214             :         /* We only support this on plain relations and matviews */
    1215          36 :         if (rte->relkind != RELKIND_RELATION &&
    1216           6 :             rte->relkind != RELKIND_MATVIEW &&
    1217           3 :             rte->relkind != RELKIND_PARTITIONED_TABLE)
    1218           2 :             ereport(ERROR,
    1219             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1220             :                      errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
    1221             :                      parser_errposition(pstate, exprLocation(rts->relation))));
    1222             : 
    1223             :         /* Transform TABLESAMPLE details and attach to the RTE */
    1224          31 :         rte->tablesample = transformRangeTableSample(pstate, rts);
    1225          30 :         return (Node *) rtr;
    1226             :     }
    1227        1910 :     else if (IsA(n, JoinExpr))
    1228             :     {
    1229             :         /* A newfangled join expression */
    1230        1910 :         JoinExpr   *j = (JoinExpr *) n;
    1231             :         RangeTblEntry *l_rte;
    1232             :         RangeTblEntry *r_rte;
    1233             :         int         l_rtindex;
    1234             :         int         r_rtindex;
    1235             :         List       *l_namespace,
    1236             :                    *r_namespace,
    1237             :                    *my_namespace,
    1238             :                    *l_colnames,
    1239             :                    *r_colnames,
    1240             :                    *res_colnames,
    1241             :                    *l_colvars,
    1242             :                    *r_colvars,
    1243             :                    *res_colvars;
    1244             :         bool        lateral_ok;
    1245             :         int         sv_namespace_length;
    1246             :         RangeTblEntry *rte;
    1247             :         int         k;
    1248             : 
    1249             :         /*
    1250             :          * Recursively process the left subtree, then the right.  We must do
    1251             :          * it in this order for correct visibility of LATERAL references.
    1252             :          */
    1253        1910 :         j->larg = transformFromClauseItem(pstate, j->larg,
    1254             :                                           &l_rte,
    1255             :                                           &l_rtindex,
    1256             :                                           &l_namespace);
    1257             : 
    1258             :         /*
    1259             :          * Make the left-side RTEs available for LATERAL access within the
    1260             :          * right side, by temporarily adding them to the pstate's namespace
    1261             :          * list.  Per SQL:2008, if the join type is not INNER or LEFT then the
    1262             :          * left-side names must still be exposed, but it's an error to
    1263             :          * reference them.  (Stupid design, but that's what it says.)  Hence,
    1264             :          * we always push them into the namespace, but mark them as not
    1265             :          * lateral_ok if the jointype is wrong.
    1266             :          *
    1267             :          * Notice that we don't require the merged namespace list to be
    1268             :          * conflict-free.  See the comments for scanNameSpaceForRefname().
    1269             :          *
    1270             :          * NB: this coding relies on the fact that list_concat is not
    1271             :          * destructive to its second argument.
    1272             :          */
    1273        1910 :         lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
    1274        1910 :         setNamespaceLateralState(l_namespace, true, lateral_ok);
    1275             : 
    1276        1910 :         sv_namespace_length = list_length(pstate->p_namespace);
    1277        1910 :         pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
    1278             : 
    1279             :         /* And now we can process the RHS */
    1280        1910 :         j->rarg = transformFromClauseItem(pstate, j->rarg,
    1281             :                                           &r_rte,
    1282             :                                           &r_rtindex,
    1283             :                                           &r_namespace);
    1284             : 
    1285             :         /* Remove the left-side RTEs from the namespace list again */
    1286        1904 :         pstate->p_namespace = list_truncate(pstate->p_namespace,
    1287             :                                             sv_namespace_length);
    1288             : 
    1289             :         /*
    1290             :          * Check for conflicting refnames in left and right subtrees. Must do
    1291             :          * this because higher levels will assume I hand back a self-
    1292             :          * consistent namespace list.
    1293             :          */
    1294        1904 :         checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
    1295             : 
    1296             :         /*
    1297             :          * Generate combined namespace info for possible use below.
    1298             :          */
    1299        1904 :         my_namespace = list_concat(l_namespace, r_namespace);
    1300             : 
    1301             :         /*
    1302             :          * Extract column name and var lists from both subtrees
    1303             :          *
    1304             :          * Note: expandRTE returns new lists, safe for me to modify
    1305             :          */
    1306        1904 :         expandRTE(l_rte, l_rtindex, 0, -1, false,
    1307             :                   &l_colnames, &l_colvars);
    1308        1904 :         expandRTE(r_rte, r_rtindex, 0, -1, false,
    1309             :                   &r_colnames, &r_colvars);
    1310             : 
    1311             :         /*
    1312             :          * Natural join does not explicitly specify columns; must generate
    1313             :          * columns to join. Need to run through the list of columns from each
    1314             :          * table or join result and match up the column names. Use the first
    1315             :          * table, and check every column in the second table for a match.
    1316             :          * (We'll check that the matches were unique later on.) The result of
    1317             :          * this step is a list of column names just like an explicitly-written
    1318             :          * USING list.
    1319             :          */
    1320        1904 :         if (j->isNatural)
    1321             :         {
    1322          40 :             List       *rlist = NIL;
    1323             :             ListCell   *lx,
    1324             :                        *rx;
    1325             : 
    1326          40 :             Assert(j->usingClause == NIL);   /* shouldn't have USING() too */
    1327             : 
    1328         182 :             foreach(lx, l_colnames)
    1329             :             {
    1330         142 :                 char       *l_colname = strVal(lfirst(lx));
    1331         142 :                 Value      *m_name = NULL;
    1332             : 
    1333         397 :                 foreach(rx, r_colnames)
    1334             :                 {
    1335         303 :                     char       *r_colname = strVal(lfirst(rx));
    1336             : 
    1337         303 :                     if (strcmp(l_colname, r_colname) == 0)
    1338             :                     {
    1339          48 :                         m_name = makeString(l_colname);
    1340          48 :                         break;
    1341             :                     }
    1342             :                 }
    1343             : 
    1344             :                 /* matched a right column? then keep as join column... */
    1345         142 :                 if (m_name != NULL)
    1346          48 :                     rlist = lappend(rlist, m_name);
    1347             :             }
    1348             : 
    1349          40 :             j->usingClause = rlist;
    1350             :         }
    1351             : 
    1352             :         /*
    1353             :          * Now transform the join qualifications, if any.
    1354             :          */
    1355        1904 :         res_colnames = NIL;
    1356        1904 :         res_colvars = NIL;
    1357             : 
    1358        1904 :         if (j->usingClause)
    1359             :         {
    1360             :             /*
    1361             :              * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
    1362             :              * the list into an explicit ON-condition, and generate a list of
    1363             :              * merged result columns.
    1364             :              */
    1365         103 :             List       *ucols = j->usingClause;
    1366         103 :             List       *l_usingvars = NIL;
    1367         103 :             List       *r_usingvars = NIL;
    1368             :             ListCell   *ucol;
    1369             : 
    1370         103 :             Assert(j->quals == NULL);    /* shouldn't have ON() too */
    1371             : 
    1372         226 :             foreach(ucol, ucols)
    1373             :             {
    1374         123 :                 char       *u_colname = strVal(lfirst(ucol));
    1375             :                 ListCell   *col;
    1376             :                 int         ndx;
    1377         123 :                 int         l_index = -1;
    1378         123 :                 int         r_index = -1;
    1379             :                 Var        *l_colvar,
    1380             :                            *r_colvar;
    1381             : 
    1382             :                 /* Check for USING(foo,foo) */
    1383         147 :                 foreach(col, res_colnames)
    1384             :                 {
    1385          24 :                     char       *res_colname = strVal(lfirst(col));
    1386             : 
    1387          24 :                     if (strcmp(res_colname, u_colname) == 0)
    1388           0 :                         ereport(ERROR,
    1389             :                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
    1390             :                                  errmsg("column name \"%s\" appears more than once in USING clause",
    1391             :                                         u_colname)));
    1392             :                 }
    1393             : 
    1394             :                 /* Find it in left input */
    1395         123 :                 ndx = 0;
    1396         681 :                 foreach(col, l_colnames)
    1397             :                 {
    1398         558 :                     char       *l_colname = strVal(lfirst(col));
    1399             : 
    1400         558 :                     if (strcmp(l_colname, u_colname) == 0)
    1401             :                     {
    1402         123 :                         if (l_index >= 0)
    1403           0 :                             ereport(ERROR,
    1404             :                                     (errcode(ERRCODE_AMBIGUOUS_COLUMN),
    1405             :                                      errmsg("common column name \"%s\" appears more than once in left table",
    1406             :                                             u_colname)));
    1407         123 :                         l_index = ndx;
    1408             :                     }
    1409         558 :                     ndx++;
    1410             :                 }
    1411         123 :                 if (l_index < 0)
    1412           0 :                     ereport(ERROR,
    1413             :                             (errcode(ERRCODE_UNDEFINED_COLUMN),
    1414             :                              errmsg("column \"%s\" specified in USING clause does not exist in left table",
    1415             :                                     u_colname)));
    1416             : 
    1417             :                 /* Find it in right input */
    1418         123 :                 ndx = 0;
    1419         627 :                 foreach(col, r_colnames)
    1420             :                 {
    1421         504 :                     char       *r_colname = strVal(lfirst(col));
    1422             : 
    1423         504 :                     if (strcmp(r_colname, u_colname) == 0)
    1424             :                     {
    1425         123 :                         if (r_index >= 0)
    1426           0 :                             ereport(ERROR,
    1427             :                                     (errcode(ERRCODE_AMBIGUOUS_COLUMN),
    1428             :                                      errmsg("common column name \"%s\" appears more than once in right table",
    1429             :                                             u_colname)));
    1430         123 :                         r_index = ndx;
    1431             :                     }
    1432         504 :                     ndx++;
    1433             :                 }
    1434         123 :                 if (r_index < 0)
    1435           0 :                     ereport(ERROR,
    1436             :                             (errcode(ERRCODE_UNDEFINED_COLUMN),
    1437             :                              errmsg("column \"%s\" specified in USING clause does not exist in right table",
    1438             :                                     u_colname)));
    1439             : 
    1440         123 :                 l_colvar = list_nth(l_colvars, l_index);
    1441         123 :                 l_usingvars = lappend(l_usingvars, l_colvar);
    1442         123 :                 r_colvar = list_nth(r_colvars, r_index);
    1443         123 :                 r_usingvars = lappend(r_usingvars, r_colvar);
    1444             : 
    1445         123 :                 res_colnames = lappend(res_colnames, lfirst(ucol));
    1446         123 :                 res_colvars = lappend(res_colvars,
    1447         123 :                                       buildMergedJoinVar(pstate,
    1448             :                                                          j->jointype,
    1449             :                                                          l_colvar,
    1450             :                                                          r_colvar));
    1451             :             }
    1452             : 
    1453         103 :             j->quals = transformJoinUsingClause(pstate,
    1454             :                                                 l_rte,
    1455             :                                                 r_rte,
    1456             :                                                 l_usingvars,
    1457             :                                                 r_usingvars);
    1458             :         }
    1459        1801 :         else if (j->quals)
    1460             :         {
    1461             :             /* User-written ON-condition; transform it */
    1462        1776 :             j->quals = transformJoinOnClause(pstate, j, my_namespace);
    1463             :         }
    1464             :         else
    1465             :         {
    1466             :             /* CROSS JOIN: no quals */
    1467             :         }
    1468             : 
    1469             :         /* Add remaining columns from each side to the output columns */
    1470        1901 :         extractRemainingColumns(res_colnames,
    1471             :                                 l_colnames, l_colvars,
    1472             :                                 &l_colnames, &l_colvars);
    1473        1901 :         extractRemainingColumns(res_colnames,
    1474             :                                 r_colnames, r_colvars,
    1475             :                                 &r_colnames, &r_colvars);
    1476        1901 :         res_colnames = list_concat(res_colnames, l_colnames);
    1477        1901 :         res_colvars = list_concat(res_colvars, l_colvars);
    1478        1901 :         res_colnames = list_concat(res_colnames, r_colnames);
    1479        1901 :         res_colvars = list_concat(res_colvars, r_colvars);
    1480             : 
    1481             :         /*
    1482             :          * Check alias (AS clause), if any.
    1483             :          */
    1484        1901 :         if (j->alias)
    1485             :         {
    1486          10 :             if (j->alias->colnames != NIL)
    1487             :             {
    1488           3 :                 if (list_length(j->alias->colnames) > list_length(res_colnames))
    1489           0 :                     ereport(ERROR,
    1490             :                             (errcode(ERRCODE_SYNTAX_ERROR),
    1491             :                              errmsg("column alias list for \"%s\" has too many entries",
    1492             :                                     j->alias->aliasname)));
    1493             :             }
    1494             :         }
    1495             : 
    1496             :         /*
    1497             :          * Now build an RTE for the result of the join
    1498             :          */
    1499        1901 :         rte = addRangeTableEntryForJoin(pstate,
    1500             :                                         res_colnames,
    1501             :                                         j->jointype,
    1502             :                                         res_colvars,
    1503             :                                         j->alias,
    1504             :                                         true);
    1505             : 
    1506             :         /* assume new rte is at end */
    1507        1901 :         j->rtindex = list_length(pstate->p_rtable);
    1508        1901 :         Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
    1509             : 
    1510        1901 :         *top_rte = rte;
    1511        1901 :         *top_rti = j->rtindex;
    1512             : 
    1513             :         /* make a matching link to the JoinExpr for later use */
    1514        5554 :         for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
    1515        3653 :             pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
    1516        1901 :         pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
    1517        1901 :         Assert(list_length(pstate->p_joinexprs) == j->rtindex);
    1518             : 
    1519             :         /*
    1520             :          * Prepare returned namespace list.  If the JOIN has an alias then it
    1521             :          * hides the contained RTEs completely; otherwise, the contained RTEs
    1522             :          * are still visible as table names, but are not visible for
    1523             :          * unqualified column-name access.
    1524             :          *
    1525             :          * Note: if there are nested alias-less JOINs, the lower-level ones
    1526             :          * will remain in the list although they have neither p_rel_visible
    1527             :          * nor p_cols_visible set.  We could delete such list items, but it's
    1528             :          * unclear that it's worth expending cycles to do so.
    1529             :          */
    1530        1901 :         if (j->alias != NULL)
    1531          10 :             my_namespace = NIL;
    1532             :         else
    1533        1891 :             setNamespaceColumnVisibility(my_namespace, false);
    1534             : 
    1535             :         /*
    1536             :          * The join RTE itself is always made visible for unqualified column
    1537             :          * names.  It's visible as a relation name only if it has an alias.
    1538             :          */
    1539        1901 :         *namespace = lappend(my_namespace,
    1540        1901 :                              makeNamespaceItem(rte,
    1541        1901 :                                                (j->alias != NULL),
    1542             :                                                true,
    1543             :                                                false,
    1544             :                                                true));
    1545             : 
    1546        1901 :         return (Node *) j;
    1547             :     }
    1548             :     else
    1549           0 :         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
    1550             :     return NULL;                /* can't get here, keep compiler quiet */
    1551             : }
    1552             : 
    1553             : /*
    1554             :  * buildMergedJoinVar -
    1555             :  *    generate a suitable replacement expression for a merged join column
    1556             :  */
    1557             : static Node *
    1558         123 : buildMergedJoinVar(ParseState *pstate, JoinType jointype,
    1559             :                    Var *l_colvar, Var *r_colvar)
    1560             : {
    1561             :     Oid         outcoltype;
    1562             :     int32       outcoltypmod;
    1563             :     Node       *l_node,
    1564             :                *r_node,
    1565             :                *res_node;
    1566             : 
    1567             :     /*
    1568             :      * Choose output type if input types are dissimilar.
    1569             :      */
    1570         123 :     outcoltype = l_colvar->vartype;
    1571         123 :     outcoltypmod = l_colvar->vartypmod;
    1572         123 :     if (outcoltype != r_colvar->vartype)
    1573             :     {
    1574          11 :         outcoltype = select_common_type(pstate,
    1575             :                                         list_make2(l_colvar, r_colvar),
    1576             :                                         "JOIN/USING",
    1577             :                                         NULL);
    1578          11 :         outcoltypmod = -1;      /* ie, unknown */
    1579             :     }
    1580         112 :     else if (outcoltypmod != r_colvar->vartypmod)
    1581             :     {
    1582             :         /* same type, but not same typmod */
    1583           0 :         outcoltypmod = -1;      /* ie, unknown */
    1584             :     }
    1585             : 
    1586             :     /*
    1587             :      * Insert coercion functions if needed.  Note that a difference in typmod
    1588             :      * can only happen if input has typmod but outcoltypmod is -1. In that
    1589             :      * case we insert a RelabelType to clearly mark that result's typmod is
    1590             :      * not same as input.  We never need coerce_type_typmod.
    1591             :      */
    1592         123 :     if (l_colvar->vartype != outcoltype)
    1593           6 :         l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
    1594             :                              outcoltype, outcoltypmod,
    1595             :                              COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
    1596         117 :     else if (l_colvar->vartypmod != outcoltypmod)
    1597           0 :         l_node = (Node *) makeRelabelType((Expr *) l_colvar,
    1598             :                                           outcoltype, outcoltypmod,
    1599             :                                           InvalidOid,   /* fixed below */
    1600             :                                           COERCE_IMPLICIT_CAST);
    1601             :     else
    1602         117 :         l_node = (Node *) l_colvar;
    1603             : 
    1604         123 :     if (r_colvar->vartype != outcoltype)
    1605           5 :         r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
    1606             :                              outcoltype, outcoltypmod,
    1607             :                              COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
    1608         118 :     else if (r_colvar->vartypmod != outcoltypmod)
    1609           0 :         r_node = (Node *) makeRelabelType((Expr *) r_colvar,
    1610             :                                           outcoltype, outcoltypmod,
    1611             :                                           InvalidOid,   /* fixed below */
    1612             :                                           COERCE_IMPLICIT_CAST);
    1613             :     else
    1614         118 :         r_node = (Node *) r_colvar;
    1615             : 
    1616             :     /*
    1617             :      * Choose what to emit
    1618             :      */
    1619         123 :     switch (jointype)
    1620             :     {
    1621             :         case JOIN_INNER:
    1622             : 
    1623             :             /*
    1624             :              * We can use either var; prefer non-coerced one if available.
    1625             :              */
    1626          80 :             if (IsA(l_node, Var))
    1627          75 :                 res_node = l_node;
    1628           5 :             else if (IsA(r_node, Var))
    1629           5 :                 res_node = r_node;
    1630             :             else
    1631           0 :                 res_node = l_node;
    1632          80 :             break;
    1633             :         case JOIN_LEFT:
    1634             :             /* Always use left var */
    1635          18 :             res_node = l_node;
    1636          18 :             break;
    1637             :         case JOIN_RIGHT:
    1638             :             /* Always use right var */
    1639           2 :             res_node = r_node;
    1640           2 :             break;
    1641             :         case JOIN_FULL:
    1642             :             {
    1643             :                 /*
    1644             :                  * Here we must build a COALESCE expression to ensure that the
    1645             :                  * join output is non-null if either input is.
    1646             :                  */
    1647          23 :                 CoalesceExpr *c = makeNode(CoalesceExpr);
    1648             : 
    1649          23 :                 c->coalescetype = outcoltype;
    1650             :                 /* coalescecollid will get set below */
    1651          23 :                 c->args = list_make2(l_node, r_node);
    1652          23 :                 c->location = -1;
    1653          23 :                 res_node = (Node *) c;
    1654          23 :                 break;
    1655             :             }
    1656             :         default:
    1657           0 :             elog(ERROR, "unrecognized join type: %d", (int) jointype);
    1658             :             res_node = NULL;    /* keep compiler quiet */
    1659             :             break;
    1660             :     }
    1661             : 
    1662             :     /*
    1663             :      * Apply assign_expr_collations to fix up the collation info in the
    1664             :      * coercion and CoalesceExpr nodes, if we made any.  This must be done now
    1665             :      * so that the join node's alias vars show correct collation info.
    1666             :      */
    1667         123 :     assign_expr_collations(pstate, res_node);
    1668             : 
    1669         123 :     return res_node;
    1670             : }
    1671             : 
    1672             : /*
    1673             :  * makeNamespaceItem -
    1674             :  *    Convenience subroutine to construct a ParseNamespaceItem.
    1675             :  */
    1676             : static ParseNamespaceItem *
    1677       18218 : makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
    1678             :                   bool lateral_only, bool lateral_ok)
    1679             : {
    1680             :     ParseNamespaceItem *nsitem;
    1681             : 
    1682       18218 :     nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
    1683       18218 :     nsitem->p_rte = rte;
    1684       18218 :     nsitem->p_rel_visible = rel_visible;
    1685       18218 :     nsitem->p_cols_visible = cols_visible;
    1686       18218 :     nsitem->p_lateral_only = lateral_only;
    1687       18218 :     nsitem->p_lateral_ok = lateral_ok;
    1688       18218 :     return nsitem;
    1689             : }
    1690             : 
    1691             : /*
    1692             :  * setNamespaceColumnVisibility -
    1693             :  *    Convenience subroutine to update cols_visible flags in a namespace list.
    1694             :  */
    1695             : static void
    1696        1891 : setNamespaceColumnVisibility(List *namespace, bool cols_visible)
    1697             : {
    1698             :     ListCell   *lc;
    1699             : 
    1700        6343 :     foreach(lc, namespace)
    1701             :     {
    1702        4452 :         ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
    1703             : 
    1704        4452 :         nsitem->p_cols_visible = cols_visible;
    1705             :     }
    1706        1891 : }
    1707             : 
    1708             : /*
    1709             :  * setNamespaceLateralState -
    1710             :  *    Convenience subroutine to update LATERAL flags in a namespace list.
    1711             :  */
    1712             : static void
    1713       43518 : setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
    1714             : {
    1715             :     ListCell   *lc;
    1716             : 
    1717       87534 :     foreach(lc, namespace)
    1718             :     {
    1719       44016 :         ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
    1720             : 
    1721       44016 :         nsitem->p_lateral_only = lateral_only;
    1722       44016 :         nsitem->p_lateral_ok = lateral_ok;
    1723             :     }
    1724       43518 : }
    1725             : 
    1726             : 
    1727             : /*
    1728             :  * transformWhereClause -
    1729             :  *    Transform the qualification and make sure it is of type boolean.
    1730             :  *    Used for WHERE and allied clauses.
    1731             :  *
    1732             :  * constructName does not affect the semantics, but is used in error messages
    1733             :  */
    1734             : Node *
    1735       51378 : transformWhereClause(ParseState *pstate, Node *clause,
    1736             :                      ParseExprKind exprKind, const char *constructName)
    1737             : {
    1738             :     Node       *qual;
    1739             : 
    1740       51378 :     if (clause == NULL)
    1741       40944 :         return NULL;
    1742             : 
    1743       10434 :     qual = transformExpr(pstate, clause, exprKind);
    1744             : 
    1745       10411 :     qual = coerce_to_boolean(pstate, qual, constructName);
    1746             : 
    1747       10411 :     return qual;
    1748             : }
    1749             : 
    1750             : 
    1751             : /*
    1752             :  * transformLimitClause -
    1753             :  *    Transform the expression and make sure it is of type bigint.
    1754             :  *    Used for LIMIT and allied clauses.
    1755             :  *
    1756             :  * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
    1757             :  * rather than int4 as before.
    1758             :  *
    1759             :  * constructName does not affect the semantics, but is used in error messages
    1760             :  */
    1761             : Node *
    1762       49860 : transformLimitClause(ParseState *pstate, Node *clause,
    1763             :                      ParseExprKind exprKind, const char *constructName)
    1764             : {
    1765             :     Node       *qual;
    1766             : 
    1767       49860 :     if (clause == NULL)
    1768       49622 :         return NULL;
    1769             : 
    1770         238 :     qual = transformExpr(pstate, clause, exprKind);
    1771             : 
    1772         237 :     qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
    1773             : 
    1774             :     /* LIMIT can't refer to any variables of the current query */
    1775         237 :     checkExprIsVarFree(pstate, qual, constructName);
    1776             : 
    1777         237 :     return qual;
    1778             : }
    1779             : 
    1780             : /*
    1781             :  * checkExprIsVarFree
    1782             :  *      Check that given expr has no Vars of the current query level
    1783             :  *      (aggregates and window functions should have been rejected already).
    1784             :  *
    1785             :  * This is used to check expressions that have to have a consistent value
    1786             :  * across all rows of the query, such as a LIMIT.  Arguably it should reject
    1787             :  * volatile functions, too, but we don't do that --- whatever value the
    1788             :  * function gives on first execution is what you get.
    1789             :  *
    1790             :  * constructName does not affect the semantics, but is used in error messages
    1791             :  */
    1792             : static void
    1793         257 : checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
    1794             : {
    1795         257 :     if (contain_vars_of_level(n, 0))
    1796             :     {
    1797           0 :         ereport(ERROR,
    1798             :                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    1799             :         /* translator: %s is name of a SQL construct, eg LIMIT */
    1800             :                  errmsg("argument of %s must not contain variables",
    1801             :                         constructName),
    1802             :                  parser_errposition(pstate,
    1803             :                                     locate_var_of_level(n, 0))));
    1804             :     }
    1805         257 : }
    1806             : 
    1807             : 
    1808             : /*
    1809             :  * checkTargetlistEntrySQL92 -
    1810             :  *    Validate a targetlist entry found by findTargetlistEntrySQL92
    1811             :  *
    1812             :  * When we select a pre-existing tlist entry as a result of syntax such
    1813             :  * as "GROUP BY 1", we have to make sure it is acceptable for use in the
    1814             :  * indicated clause type; transformExpr() will have treated it as a regular
    1815             :  * targetlist item.
    1816             :  */
    1817             : static void
    1818        2647 : checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
    1819             :                           ParseExprKind exprKind)
    1820             : {
    1821        2647 :     switch (exprKind)
    1822             :     {
    1823             :         case EXPR_KIND_GROUP_BY:
    1824             :             /* reject aggregates and window functions */
    1825          41 :             if (pstate->p_hasAggs &&
    1826          20 :                 contain_aggs_of_level((Node *) tle->expr, 0))
    1827           0 :                 ereport(ERROR,
    1828             :                         (errcode(ERRCODE_GROUPING_ERROR),
    1829             :                 /* translator: %s is name of a SQL construct, eg GROUP BY */
    1830             :                          errmsg("aggregate functions are not allowed in %s",
    1831             :                                 ParseExprKindName(exprKind)),
    1832             :                          parser_errposition(pstate,
    1833             :                                             locate_agg_of_level((Node *) tle->expr, 0))));
    1834          23 :             if (pstate->p_hasWindowFuncs &&
    1835           2 :                 contain_windowfuncs((Node *) tle->expr))
    1836           1 :                 ereport(ERROR,
    1837             :                         (errcode(ERRCODE_WINDOWING_ERROR),
    1838             :                 /* translator: %s is name of a SQL construct, eg GROUP BY */
    1839             :                          errmsg("window functions are not allowed in %s",
    1840             :                                 ParseExprKindName(exprKind)),
    1841             :                          parser_errposition(pstate,
    1842             :                                             locate_windowfunc((Node *) tle->expr))));
    1843          20 :             break;
    1844             :         case EXPR_KIND_ORDER_BY:
    1845             :             /* no extra checks needed */
    1846        2612 :             break;
    1847             :         case EXPR_KIND_DISTINCT_ON:
    1848             :             /* no extra checks needed */
    1849          14 :             break;
    1850             :         default:
    1851           0 :             elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
    1852             :             break;
    1853             :     }
    1854        2646 : }
    1855             : 
    1856             : /*
    1857             :  *  findTargetlistEntrySQL92 -
    1858             :  *    Returns the targetlist entry matching the given (untransformed) node.
    1859             :  *    If no matching entry exists, one is created and appended to the target
    1860             :  *    list as a "resjunk" node.
    1861             :  *
    1862             :  * This function supports the old SQL92 ORDER BY interpretation, where the
    1863             :  * expression is an output column name or number.  If we fail to find a
    1864             :  * match of that sort, we fall through to the SQL99 rules.  For historical
    1865             :  * reasons, Postgres also allows this interpretation for GROUP BY, though
    1866             :  * the standard never did.  However, for GROUP BY we prefer a SQL99 match.
    1867             :  * This function is *not* used for WINDOW definitions.
    1868             :  *
    1869             :  * node     the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
    1870             :  * tlist    the target list (passed by reference so we can append to it)
    1871             :  * exprKind identifies clause type being processed
    1872             :  */
    1873             : static TargetEntry *
    1874        4074 : findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
    1875             :                          ParseExprKind exprKind)
    1876             : {
    1877             :     ListCell   *tl;
    1878             : 
    1879             :     /*----------
    1880             :      * Handle two special cases as mandated by the SQL92 spec:
    1881             :      *
    1882             :      * 1. Bare ColumnName (no qualifier or subscripts)
    1883             :      *    For a bare identifier, we search for a matching column name
    1884             :      *    in the existing target list.  Multiple matches are an error
    1885             :      *    unless they refer to identical values; for example,
    1886             :      *    we allow  SELECT a, a FROM table ORDER BY a
    1887             :      *    but not   SELECT a AS b, b FROM table ORDER BY b
    1888             :      *    If no match is found, we fall through and treat the identifier
    1889             :      *    as an expression.
    1890             :      *    For GROUP BY, it is incorrect to match the grouping item against
    1891             :      *    targetlist entries: according to SQL92, an identifier in GROUP BY
    1892             :      *    is a reference to a column name exposed by FROM, not to a target
    1893             :      *    list column.  However, many implementations (including pre-7.0
    1894             :      *    PostgreSQL) accept this anyway.  So for GROUP BY, we look first
    1895             :      *    to see if the identifier matches any FROM column name, and only
    1896             :      *    try for a targetlist name if it doesn't.  This ensures that we
    1897             :      *    adhere to the spec in the case where the name could be both.
    1898             :      *    DISTINCT ON isn't in the standard, so we can do what we like there;
    1899             :      *    we choose to make it work like ORDER BY, on the rather flimsy
    1900             :      *    grounds that ordinary DISTINCT works on targetlist entries.
    1901             :      *
    1902             :      * 2. IntegerConstant
    1903             :      *    This means to use the n'th item in the existing target list.
    1904             :      *    Note that it would make no sense to order/group/distinct by an
    1905             :      *    actual constant, so this does not create a conflict with SQL99.
    1906             :      *    GROUP BY column-number is not allowed by SQL92, but since
    1907             :      *    the standard has no other behavior defined for this syntax,
    1908             :      *    we may as well accept this common extension.
    1909             :      *
    1910             :      * Note that pre-existing resjunk targets must not be used in either case,
    1911             :      * since the user didn't write them in his SELECT list.
    1912             :      *
    1913             :      * If neither special case applies, fall through to treat the item as
    1914             :      * an expression per SQL99.
    1915             :      *----------
    1916             :      */
    1917        5950 :     if (IsA(node, ColumnRef) &&
    1918        3294 :         list_length(((ColumnRef *) node)->fields) == 1 &&
    1919        1418 :         IsA(linitial(((ColumnRef *) node)->fields), String))
    1920             :     {
    1921        1418 :         char       *name = strVal(linitial(((ColumnRef *) node)->fields));
    1922        1418 :         int         location = ((ColumnRef *) node)->location;
    1923             : 
    1924        1418 :         if (exprKind == EXPR_KIND_GROUP_BY)
    1925             :         {
    1926             :             /*
    1927             :              * In GROUP BY, we must prefer a match against a FROM-clause
    1928             :              * column to one against the targetlist.  Look to see if there is
    1929             :              * a matching column.  If so, fall through to use SQL99 rules.
    1930             :              * NOTE: if name could refer ambiguously to more than one column
    1931             :              * name exposed by FROM, colNameToVar will ereport(ERROR). That's
    1932             :              * just what we want here.
    1933             :              *
    1934             :              * Small tweak for 7.4.3: ignore matches in upper query levels.
    1935             :              * This effectively changes the search order for bare names to (1)
    1936             :              * local FROM variables, (2) local targetlist aliases, (3) outer
    1937             :              * FROM variables, whereas before it was (1) (3) (2). SQL92 and
    1938             :              * SQL99 do not allow GROUPing BY an outer reference, so this
    1939             :              * breaks no cases that are legal per spec, and it seems a more
    1940             :              * self-consistent behavior.
    1941             :              */
    1942         417 :             if (colNameToVar(pstate, name, true, location) != NULL)
    1943         410 :                 name = NULL;
    1944             :         }
    1945             : 
    1946        1418 :         if (name != NULL)
    1947             :         {
    1948        1008 :             TargetEntry *target_result = NULL;
    1949             : 
    1950        4150 :             foreach(tl, *tlist)
    1951             :             {
    1952        3142 :                 TargetEntry *tle = (TargetEntry *) lfirst(tl);
    1953             : 
    1954        6271 :                 if (!tle->resjunk &&
    1955        3129 :                     strcmp(tle->resname, name) == 0)
    1956             :                 {
    1957         763 :                     if (target_result != NULL)
    1958             :                     {
    1959           1 :                         if (!equal(target_result->expr, tle->expr))
    1960           0 :                             ereport(ERROR,
    1961             :                                     (errcode(ERRCODE_AMBIGUOUS_COLUMN),
    1962             : 
    1963             :                             /*------
    1964             :                               translator: first %s is name of a SQL construct, eg ORDER BY */
    1965             :                                      errmsg("%s \"%s\" is ambiguous",
    1966             :                                             ParseExprKindName(exprKind),
    1967             :                                             name),
    1968             :                                      parser_errposition(pstate, location)));
    1969             :                     }
    1970             :                     else
    1971         762 :                         target_result = tle;
    1972             :                     /* Stay in loop to check for ambiguity */
    1973             :                 }
    1974             :             }
    1975        1008 :             if (target_result != NULL)
    1976             :             {
    1977             :                 /* return the first match, after suitable validation */
    1978         762 :                 checkTargetlistEntrySQL92(pstate, target_result, exprKind);
    1979         762 :                 return target_result;
    1980             :             }
    1981             :         }
    1982             :     }
    1983        3312 :     if (IsA(node, A_Const))
    1984             :     {
    1985        1886 :         Value      *val = &((A_Const *) node)->val;
    1986        1886 :         int         location = ((A_Const *) node)->location;
    1987        1886 :         int         targetlist_pos = 0;
    1988             :         int         target_pos;
    1989             : 
    1990        1886 :         if (!IsA(val, Integer))
    1991           0 :             ereport(ERROR,
    1992             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    1993             :             /* translator: %s is name of a SQL construct, eg ORDER BY */
    1994             :                      errmsg("non-integer constant in %s",
    1995             :                             ParseExprKindName(exprKind)),
    1996             :                      parser_errposition(pstate, location)));
    1997             : 
    1998        1886 :         target_pos = intVal(val);
    1999        2951 :         foreach(tl, *tlist)
    2000             :         {
    2001        2950 :             TargetEntry *tle = (TargetEntry *) lfirst(tl);
    2002             : 
    2003        2950 :             if (!tle->resjunk)
    2004             :             {
    2005        2950 :                 if (++targetlist_pos == target_pos)
    2006             :                 {
    2007             :                     /* return the unique match, after suitable validation */
    2008        1885 :                     checkTargetlistEntrySQL92(pstate, tle, exprKind);
    2009        1884 :                     return tle;
    2010             :                 }
    2011             :             }
    2012             :         }
    2013           1 :         ereport(ERROR,
    2014             :                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    2015             :         /* translator: %s is name of a SQL construct, eg ORDER BY */
    2016             :                  errmsg("%s position %d is not in select list",
    2017             :                         ParseExprKindName(exprKind), target_pos),
    2018             :                  parser_errposition(pstate, location)));
    2019             :     }
    2020             : 
    2021             :     /*
    2022             :      * Otherwise, we have an expression, so process it per SQL99 rules.
    2023             :      */
    2024        1426 :     return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
    2025             : }
    2026             : 
    2027             : /*
    2028             :  *  findTargetlistEntrySQL99 -
    2029             :  *    Returns the targetlist entry matching the given (untransformed) node.
    2030             :  *    If no matching entry exists, one is created and appended to the target
    2031             :  *    list as a "resjunk" node.
    2032             :  *
    2033             :  * This function supports the SQL99 interpretation, wherein the expression
    2034             :  * is just an ordinary expression referencing input column names.
    2035             :  *
    2036             :  * node     the ORDER BY, GROUP BY, etc expression to be matched
    2037             :  * tlist    the target list (passed by reference so we can append to it)
    2038             :  * exprKind identifies clause type being processed
    2039             :  */
    2040             : static TargetEntry *
    2041        1680 : findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
    2042             :                          ParseExprKind exprKind)
    2043             : {
    2044             :     TargetEntry *target_result;
    2045             :     ListCell   *tl;
    2046             :     Node       *expr;
    2047             : 
    2048             :     /*
    2049             :      * Convert the untransformed node to a transformed expression, and search
    2050             :      * for a match in the tlist.  NOTE: it doesn't really matter whether there
    2051             :      * is more than one match.  Also, we are willing to match an existing
    2052             :      * resjunk target here, though the SQL92 cases above must ignore resjunk
    2053             :      * targets.
    2054             :      */
    2055        1680 :     expr = transformExpr(pstate, node, exprKind);
    2056             : 
    2057        4946 :     foreach(tl, *tlist)
    2058             :     {
    2059        4136 :         TargetEntry *tle = (TargetEntry *) lfirst(tl);
    2060             :         Node       *texpr;
    2061             : 
    2062             :         /*
    2063             :          * Ignore any implicit cast on the existing tlist expression.
    2064             :          *
    2065             :          * This essentially allows the ORDER/GROUP/etc item to adopt the same
    2066             :          * datatype previously selected for a textually-equivalent tlist item.
    2067             :          * There can't be any implicit cast at top level in an ordinary SELECT
    2068             :          * tlist at this stage, but the case does arise with ORDER BY in an
    2069             :          * aggregate function.
    2070             :          */
    2071        4136 :         texpr = strip_implicit_coercions((Node *) tle->expr);
    2072             : 
    2073        4136 :         if (equal(expr, texpr))
    2074         861 :             return tle;
    2075             :     }
    2076             : 
    2077             :     /*
    2078             :      * If no matches, construct a new target entry which is appended to the
    2079             :      * end of the target list.  This target is given resjunk = TRUE so that it
    2080             :      * will not be projected into the final tuple.
    2081             :      */
    2082         810 :     target_result = transformTargetEntry(pstate, node, expr, exprKind,
    2083             :                                          NULL, true);
    2084             : 
    2085         810 :     *tlist = lappend(*tlist, target_result);
    2086             : 
    2087         810 :     return target_result;
    2088             : }
    2089             : 
    2090             : /*-------------------------------------------------------------------------
    2091             :  * Flatten out parenthesized sublists in grouping lists, and some cases
    2092             :  * of nested grouping sets.
    2093             :  *
    2094             :  * Inside a grouping set (ROLLUP, CUBE, or GROUPING SETS), we expect the
    2095             :  * content to be nested no more than 2 deep: i.e. ROLLUP((a,b),(c,d)) is
    2096             :  * ok, but ROLLUP((a,(b,c)),d) is flattened to ((a,b,c),d), which we then
    2097             :  * (later) normalize to ((a,b,c),(d)).
    2098             :  *
    2099             :  * CUBE or ROLLUP can be nested inside GROUPING SETS (but not the reverse),
    2100             :  * and we leave that alone if we find it. But if we see GROUPING SETS inside
    2101             :  * GROUPING SETS, we can flatten and normalize as follows:
    2102             :  *   GROUPING SETS (a, (b,c), GROUPING SETS ((c,d),(e)), (f,g))
    2103             :  * becomes
    2104             :  *   GROUPING SETS ((a), (b,c), (c,d), (e), (f,g))
    2105             :  *
    2106             :  * This is per the spec's syntax transformations, but these are the only such
    2107             :  * transformations we do in parse analysis, so that queries retain the
    2108             :  * originally specified grouping set syntax for CUBE and ROLLUP as much as
    2109             :  * possible when deparsed. (Full expansion of the result into a list of
    2110             :  * grouping sets is left to the planner.)
    2111             :  *
    2112             :  * When we're done, the resulting list should contain only these possible
    2113             :  * elements:
    2114             :  *   - an expression
    2115             :  *   - a CUBE or ROLLUP with a list of expressions nested 2 deep
    2116             :  *   - a GROUPING SET containing any of:
    2117             :  *      - expression lists
    2118             :  *      - empty grouping sets
    2119             :  *      - CUBE or ROLLUP nodes with lists nested 2 deep
    2120             :  * The return is a new list, but doesn't deep-copy the old nodes except for
    2121             :  * GroupingSet nodes.
    2122             :  *
    2123             :  * As a side effect, flag whether the list has any GroupingSet nodes.
    2124             :  *-------------------------------------------------------------------------
    2125             :  */
    2126             : static Node *
    2127       25103 : flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
    2128             : {
    2129             :     /* just in case of pathological input */
    2130       25103 :     check_stack_depth();
    2131             : 
    2132       25103 :     if (expr == (Node *) NIL)
    2133       23921 :         return (Node *) NIL;
    2134             : 
    2135        1182 :     switch (expr->type)
    2136             :     {
    2137             :         case T_RowExpr:
    2138             :             {
    2139          47 :                 RowExpr    *r = (RowExpr *) expr;
    2140             : 
    2141          47 :                 if (r->row_format == COERCE_IMPLICIT_CAST)
    2142          47 :                     return flatten_grouping_sets((Node *) r->args,
    2143             :                                                  false, NULL);
    2144             :             }
    2145           0 :             break;
    2146             :         case T_GroupingSet:
    2147             :             {
    2148         146 :                 GroupingSet *gset = (GroupingSet *) expr;
    2149             :                 ListCell   *l2;
    2150         146 :                 List       *result_set = NIL;
    2151             : 
    2152         146 :                 if (hasGroupingSets)
    2153          97 :                     *hasGroupingSets = true;
    2154             : 
    2155             :                 /*
    2156             :                  * at the top level, we skip over all empty grouping sets; the
    2157             :                  * caller can supply the canonical GROUP BY () if nothing is
    2158             :                  * left.
    2159             :                  */
    2160             : 
    2161         146 :                 if (toplevel && gset->kind == GROUPING_SET_EMPTY)
    2162           3 :                     return (Node *) NIL;
    2163             : 
    2164         378 :                 foreach(l2, gset->content)
    2165             :                 {
    2166         235 :                     Node       *n1 = lfirst(l2);
    2167         235 :                     Node       *n2 = flatten_grouping_sets(n1, false, NULL);
    2168             : 
    2169         284 :                     if (IsA(n1, GroupingSet) &&
    2170          49 :                         ((GroupingSet *) n1)->kind == GROUPING_SET_SETS)
    2171             :                     {
    2172          16 :                         result_set = list_concat(result_set, (List *) n2);
    2173             :                     }
    2174             :                     else
    2175         219 :                         result_set = lappend(result_set, n2);
    2176             :                 }
    2177             : 
    2178             :                 /*
    2179             :                  * At top level, keep the grouping set node; but if we're in a
    2180             :                  * nested grouping set, then we need to concat the flattened
    2181             :                  * result into the outer list if it's simply nested.
    2182             :                  */
    2183             : 
    2184         143 :                 if (toplevel || (gset->kind != GROUPING_SET_SETS))
    2185             :                 {
    2186         127 :                     return (Node *) makeGroupingSet(gset->kind, result_set, gset->location);
    2187             :                 }
    2188             :                 else
    2189          16 :                     return (Node *) result_set;
    2190             :             }
    2191             :         case T_List:
    2192             :             {
    2193         396 :                 List       *result = NIL;
    2194             :                 ListCell   *l;
    2195             : 
    2196         947 :                 foreach(l, (List *) expr)
    2197             :                 {
    2198         551 :                     Node       *n = flatten_grouping_sets(lfirst(l), toplevel, hasGroupingSets);
    2199             : 
    2200         551 :                     if (n != (Node *) NIL)
    2201             :                     {
    2202         548 :                         if (IsA(n, List))
    2203           7 :                             result = list_concat(result, (List *) n);
    2204             :                         else
    2205         541 :                             result = lappend(result, n);
    2206             :                     }
    2207             :                 }
    2208             : 
    2209         396 :                 return (Node *) result;
    2210             :             }
    2211             :         default:
    2212         593 :             break;
    2213             :     }
    2214             : 
    2215         593 :     return expr;
    2216             : }
    2217             : 
    2218             : /*
    2219             :  * Transform a single expression within a GROUP BY clause or grouping set.
    2220             :  *
    2221             :  * The expression is added to the targetlist if not already present, and to the
    2222             :  * flatresult list (which will become the groupClause) if not already present
    2223             :  * there.  The sortClause is consulted for operator and sort order hints.
    2224             :  *
    2225             :  * Returns the ressortgroupref of the expression.
    2226             :  *
    2227             :  * flatresult   reference to flat list of SortGroupClause nodes
    2228             :  * seen_local   bitmapset of sortgrouprefs already seen at the local level
    2229             :  * pstate       ParseState
    2230             :  * gexpr        node to transform
    2231             :  * targetlist   reference to TargetEntry list
    2232             :  * sortClause   ORDER BY clause (SortGroupClause nodes)
    2233             :  * exprKind     expression kind
    2234             :  * useSQL99     SQL99 rather than SQL92 syntax
    2235             :  * toplevel     false if within any grouping set
    2236             :  */
    2237             : static Index
    2238         593 : transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local,
    2239             :                          ParseState *pstate, Node *gexpr,
    2240             :                          List **targetlist, List *sortClause,
    2241             :                          ParseExprKind exprKind, bool useSQL99, bool toplevel)
    2242             : {
    2243             :     TargetEntry *tle;
    2244         593 :     bool        found = false;
    2245             : 
    2246         593 :     if (useSQL99)
    2247          48 :         tle = findTargetlistEntrySQL99(pstate, gexpr,
    2248             :                                        targetlist, exprKind);
    2249             :     else
    2250         545 :         tle = findTargetlistEntrySQL92(pstate, gexpr,
    2251             :                                        targetlist, exprKind);
    2252             : 
    2253         589 :     if (tle->ressortgroupref > 0)
    2254             :     {
    2255             :         ListCell   *sl;
    2256             : 
    2257             :         /*
    2258             :          * Eliminate duplicates (GROUP BY x, x) but only at local level.
    2259             :          * (Duplicates in grouping sets can affect the number of returned
    2260             :          * rows, so can't be dropped indiscriminately.)
    2261             :          *
    2262             :          * Since we don't care about anything except the sortgroupref, we can
    2263             :          * use a bitmapset rather than scanning lists.
    2264             :          */
    2265         184 :         if (bms_is_member(tle->ressortgroupref, seen_local))
    2266           2 :             return 0;
    2267             : 
    2268             :         /*
    2269             :          * If we're already in the flat clause list, we don't need to consider
    2270             :          * adding ourselves again.
    2271             :          */
    2272         182 :         found = targetIsInSortList(tle, InvalidOid, *flatresult);
    2273         182 :         if (found)
    2274          27 :             return tle->ressortgroupref;
    2275             : 
    2276             :         /*
    2277             :          * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
    2278             :          * info from the (first) matching ORDER BY item.  This means that if
    2279             :          * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
    2280             :          * GROUP BY operation silently takes on the equality semantics implied
    2281             :          * by the ORDER BY.  There are two reasons to do this: it improves the
    2282             :          * odds that we can implement both GROUP BY and ORDER BY with a single
    2283             :          * sort step, and it allows the user to choose the equality semantics
    2284             :          * used by GROUP BY, should she be working with a datatype that has
    2285             :          * more than one equality operator.
    2286             :          *
    2287             :          * If we're in a grouping set, though, we force our requested ordering
    2288             :          * to be NULLS LAST, because if we have any hope of using a sorted agg
    2289             :          * for the job, we're going to be tacking on generated NULL values
    2290             :          * after the corresponding groups. If the user demands nulls first,
    2291             :          * another sort step is going to be inevitable, but that's the
    2292             :          * planner's problem.
    2293             :          */
    2294             : 
    2295         215 :         foreach(sl, sortClause)
    2296             :         {
    2297         207 :             SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
    2298             : 
    2299         207 :             if (sc->tleSortGroupRef == tle->ressortgroupref)
    2300             :             {
    2301         147 :                 SortGroupClause *grpc = copyObject(sc);
    2302             : 
    2303         147 :                 if (!toplevel)
    2304          47 :                     grpc->nulls_first = false;
    2305         147 :                 *flatresult = lappend(*flatresult, grpc);
    2306         147 :                 found = true;
    2307         147 :                 break;
    2308             :             }
    2309             :         }
    2310             :     }
    2311             : 
    2312             :     /*
    2313             :      * If no match in ORDER BY, just add it to the result using default
    2314             :      * sort/group semantics.
    2315             :      */
    2316         560 :     if (!found)
    2317         413 :         *flatresult = addTargetToGroupList(pstate, tle,
    2318             :                                            *flatresult, *targetlist,
    2319             :                                            exprLocation(gexpr));
    2320             : 
    2321             :     /*
    2322             :      * _something_ must have assigned us a sortgroupref by now...
    2323             :      */
    2324             : 
    2325         560 :     return tle->ressortgroupref;
    2326             : }
    2327             : 
    2328             : /*
    2329             :  * Transform a list of expressions within a GROUP BY clause or grouping set.
    2330             :  *
    2331             :  * The list of expressions belongs to a single clause within which duplicates
    2332             :  * can be safely eliminated.
    2333             :  *
    2334             :  * Returns an integer list of ressortgroupref values.
    2335             :  *
    2336             :  * flatresult   reference to flat list of SortGroupClause nodes
    2337             :  * pstate       ParseState
    2338             :  * list         nodes to transform
    2339             :  * targetlist   reference to TargetEntry list
    2340             :  * sortClause   ORDER BY clause (SortGroupClause nodes)
    2341             :  * exprKind     expression kind
    2342             :  * useSQL99     SQL99 rather than SQL92 syntax
    2343             :  * toplevel     false if within any grouping set
    2344             :  */
    2345             : static List *
    2346          40 : transformGroupClauseList(List **flatresult,
    2347             :                          ParseState *pstate, List *list,
    2348             :                          List **targetlist, List *sortClause,
    2349             :                          ParseExprKind exprKind, bool useSQL99, bool toplevel)
    2350             : {
    2351          40 :     Bitmapset  *seen_local = NULL;
    2352          40 :     List       *result = NIL;
    2353             :     ListCell   *gl;
    2354             : 
    2355         123 :     foreach(gl, list)
    2356             :     {
    2357          83 :         Node       *gexpr = (Node *) lfirst(gl);
    2358             : 
    2359          83 :         Index       ref = transformGroupClauseExpr(flatresult,
    2360             :                                                    seen_local,
    2361             :                                                    pstate,
    2362             :                                                    gexpr,
    2363             :                                                    targetlist,
    2364             :                                                    sortClause,
    2365             :                                                    exprKind,
    2366             :                                                    useSQL99,
    2367             :                                                    toplevel);
    2368             : 
    2369          83 :         if (ref > 0)
    2370             :         {
    2371          81 :             seen_local = bms_add_member(seen_local, ref);
    2372          81 :             result = lappend_int(result, ref);
    2373             :         }
    2374             :     }
    2375             : 
    2376          40 :     return result;
    2377             : }
    2378             : 
    2379             : /*
    2380             :  * Transform a grouping set and (recursively) its content.
    2381             :  *
    2382             :  * The grouping set might be a GROUPING SETS node with other grouping sets
    2383             :  * inside it, but SETS within SETS have already been flattened out before
    2384             :  * reaching here.
    2385             :  *
    2386             :  * Returns the transformed node, which now contains SIMPLE nodes with lists
    2387             :  * of ressortgrouprefs rather than expressions.
    2388             :  *
    2389             :  * flatresult   reference to flat list of SortGroupClause nodes
    2390             :  * pstate       ParseState
    2391             :  * gset         grouping set to transform
    2392             :  * targetlist   reference to TargetEntry list
    2393             :  * sortClause   ORDER BY clause (SortGroupClause nodes)
    2394             :  * exprKind     expression kind
    2395             :  * useSQL99     SQL99 rather than SQL92 syntax
    2396             :  * toplevel     false if within any grouping set
    2397             :  */
    2398             : static Node *
    2399         127 : transformGroupingSet(List **flatresult,
    2400             :                      ParseState *pstate, GroupingSet *gset,
    2401             :                      List **targetlist, List *sortClause,
    2402             :                      ParseExprKind exprKind, bool useSQL99, bool toplevel)
    2403             : {
    2404             :     ListCell   *gl;
    2405         127 :     List       *content = NIL;
    2406             : 
    2407         127 :     Assert(toplevel || gset->kind != GROUPING_SET_SETS);
    2408             : 
    2409         346 :     foreach(gl, gset->content)
    2410             :     {
    2411         219 :         Node       *n = lfirst(gl);
    2412             : 
    2413         219 :         if (IsA(n, List))
    2414             :         {
    2415          40 :             List       *l = transformGroupClauseList(flatresult,
    2416             :                                                      pstate, (List *) n,
    2417             :                                                      targetlist, sortClause,
    2418             :                                                      exprKind, useSQL99, false);
    2419             : 
    2420          40 :             content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
    2421             :                                                        l,
    2422             :                                                        exprLocation(n)));
    2423             :         }
    2424         179 :         else if (IsA(n, GroupingSet))
    2425             :         {
    2426          33 :             GroupingSet *gset2 = (GroupingSet *) lfirst(gl);
    2427             : 
    2428          33 :             content = lappend(content, transformGroupingSet(flatresult,
    2429             :                                                             pstate, gset2,
    2430             :                                                             targetlist, sortClause,
    2431             :                                                             exprKind, useSQL99, false));
    2432             :         }
    2433             :         else
    2434             :         {
    2435         146 :             Index       ref = transformGroupClauseExpr(flatresult,
    2436             :                                                        NULL,
    2437             :                                                        pstate,
    2438             :                                                        n,
    2439             :                                                        targetlist,
    2440             :                                                        sortClause,
    2441             :                                                        exprKind,
    2442             :                                                        useSQL99,
    2443             :                                                        false);
    2444             : 
    2445         146 :             content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
    2446             :                                                        list_make1_int(ref),
    2447             :                                                        exprLocation(n)));
    2448             :         }
    2449             :     }
    2450             : 
    2451             :     /* Arbitrarily cap the size of CUBE, which has exponential growth */
    2452         127 :     if (gset->kind == GROUPING_SET_CUBE)
    2453             :     {
    2454          23 :         if (list_length(content) > 12)
    2455           0 :             ereport(ERROR,
    2456             :                     (errcode(ERRCODE_TOO_MANY_COLUMNS),
    2457             :                      errmsg("CUBE is limited to 12 elements"),
    2458             :                      parser_errposition(pstate, gset->location)));
    2459             :     }
    2460             : 
    2461         127 :     return (Node *) makeGroupingSet(gset->kind, content, gset->location);
    2462             : }
    2463             : 
    2464             : 
    2465             : /*
    2466             :  * transformGroupClause -
    2467             :  *    transform a GROUP BY clause
    2468             :  *
    2469             :  * GROUP BY items will be added to the targetlist (as resjunk columns)
    2470             :  * if not already present, so the targetlist must be passed by reference.
    2471             :  *
    2472             :  * This is also used for window PARTITION BY clauses (which act almost the
    2473             :  * same, but are always interpreted per SQL99 rules).
    2474             :  *
    2475             :  * Grouping sets make this a lot more complex than it was. Our goal here is
    2476             :  * twofold: we make a flat list of SortGroupClause nodes referencing each
    2477             :  * distinct expression used for grouping, with those expressions added to the
    2478             :  * targetlist if needed. At the same time, we build the groupingSets tree,
    2479             :  * which stores only ressortgrouprefs as integer lists inside GroupingSet nodes
    2480             :  * (possibly nested, but limited in depth: a GROUPING_SET_SETS node can contain
    2481             :  * nested SIMPLE, CUBE or ROLLUP nodes, but not more sets - we flatten that
    2482             :  * out; while CUBE and ROLLUP can contain only SIMPLE nodes).
    2483             :  *
    2484             :  * We skip much of the hard work if there are no grouping sets.
    2485             :  *
    2486             :  * One subtlety is that the groupClause list can end up empty while the
    2487             :  * groupingSets list is not; this happens if there are only empty grouping
    2488             :  * sets, or an explicit GROUP BY (). This has the same effect as specifying
    2489             :  * aggregates or a HAVING clause with no GROUP BY; the output is one row per
    2490             :  * grouping set even if the input is empty.
    2491             :  *
    2492             :  * Returns the transformed (flat) groupClause.
    2493             :  *
    2494             :  * pstate       ParseState
    2495             :  * grouplist    clause to transform
    2496             :  * groupingSets reference to list to contain the grouping set tree
    2497             :  * targetlist   reference to TargetEntry list
    2498             :  * sortClause   ORDER BY clause (SortGroupClause nodes)
    2499             :  * exprKind     expression kind
    2500             :  * useSQL99     SQL99 rather than SQL92 syntax
    2501             :  */
    2502             : List *
    2503       24270 : transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets,
    2504             :                      List **targetlist, List *sortClause,
    2505             :                      ParseExprKind exprKind, bool useSQL99)
    2506             : {
    2507       24270 :     List       *result = NIL;
    2508             :     List       *flat_grouplist;
    2509       24270 :     List       *gsets = NIL;
    2510             :     ListCell   *gl;
    2511       24270 :     bool        hasGroupingSets = false;
    2512       24270 :     Bitmapset  *seen_local = NULL;
    2513             : 
    2514             :     /*
    2515             :      * Recursively flatten implicit RowExprs. (Technically this is only needed
    2516             :      * for GROUP BY, per the syntax rules for grouping sets, but we do it
    2517             :      * anyway.)
    2518             :      */
    2519       24270 :     flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
    2520             :                                                     true,
    2521             :                                                     &hasGroupingSets);
    2522             : 
    2523             :     /*
    2524             :      * If the list is now empty, but hasGroupingSets is true, it's because we
    2525             :      * elided redundant empty grouping sets. Restore a single empty grouping
    2526             :      * set to leave a canonical form: GROUP BY ()
    2527             :      */
    2528             : 
    2529       24270 :     if (flat_grouplist == NIL && hasGroupingSets)
    2530             :     {
    2531           3 :         flat_grouplist = list_make1(makeGroupingSet(GROUPING_SET_EMPTY,
    2532             :                                                     NIL,
    2533             :                                                     exprLocation((Node *) grouplist)));
    2534             :     }
    2535             : 
    2536       24727 :     foreach(gl, flat_grouplist)
    2537             :     {
    2538         461 :         Node       *gexpr = (Node *) lfirst(gl);
    2539             : 
    2540         461 :         if (IsA(gexpr, GroupingSet))
    2541             :         {
    2542          97 :             GroupingSet *gset = (GroupingSet *) gexpr;
    2543             : 
    2544          97 :             switch (gset->kind)
    2545             :             {
    2546             :                 case GROUPING_SET_EMPTY:
    2547           3 :                     gsets = lappend(gsets, gset);
    2548           3 :                     break;
    2549             :                 case GROUPING_SET_SIMPLE:
    2550             :                     /* can't happen */
    2551           0 :                     Assert(false);
    2552             :                     break;
    2553             :                 case GROUPING_SET_SETS:
    2554             :                 case GROUPING_SET_CUBE:
    2555             :                 case GROUPING_SET_ROLLUP:
    2556          94 :                     gsets = lappend(gsets,
    2557          94 :                                     transformGroupingSet(&result,
    2558             :                                                          pstate, gset,
    2559             :                                                          targetlist, sortClause,
    2560             :                                                          exprKind, useSQL99, true));
    2561          94 :                     break;
    2562             :             }
    2563             :         }
    2564             :         else
    2565             :         {
    2566         364 :             Index       ref = transformGroupClauseExpr(&result, seen_local,
    2567             :                                                        pstate, gexpr,
    2568             :                                                        targetlist, sortClause,
    2569             :                                                        exprKind, useSQL99, true);
    2570             : 
    2571         360 :             if (ref > 0)
    2572             :             {
    2573         360 :                 seen_local = bms_add_member(seen_local, ref);
    2574         360 :                 if (hasGroupingSets)
    2575           4 :                     gsets = lappend(gsets,
    2576           4 :                                     makeGroupingSet(GROUPING_SET_SIMPLE,
    2577             :                                                     list_make1_int(ref),
    2578             :                                                     exprLocation(gexpr)));
    2579             :             }
    2580             :         }
    2581             :     }
    2582             : 
    2583             :     /* parser should prevent this */
    2584       24266 :     Assert(gsets == NIL || groupingSets != NULL);
    2585             : 
    2586       24266 :     if (groupingSets)
    2587       24122 :         *groupingSets = gsets;
    2588             : 
    2589       24266 :     return result;
    2590             : }
    2591             : 
    2592             : /*
    2593             :  * transformSortClause -
    2594             :  *    transform an ORDER BY clause
    2595             :  *
    2596             :  * ORDER BY items will be added to the targetlist (as resjunk columns)
    2597             :  * if not already present, so the targetlist must be passed by reference.
    2598             :  *
    2599             :  * This is also used for window and aggregate ORDER BY clauses (which act
    2600             :  * almost the same, but are always interpreted per SQL99 rules).
    2601             :  */
    2602             : List *
    2603       27644 : transformSortClause(ParseState *pstate,
    2604             :                     List *orderlist,
    2605             :                     List **targetlist,
    2606             :                     ParseExprKind exprKind,
    2607             :                     bool useSQL99)
    2608             : {
    2609       27644 :     List       *sortlist = NIL;
    2610             :     ListCell   *olitem;
    2611             : 
    2612       31357 :     foreach(olitem, orderlist)
    2613             :     {
    2614        3720 :         SortBy     *sortby = (SortBy *) lfirst(olitem);
    2615             :         TargetEntry *tle;
    2616             : 
    2617        3720 :         if (useSQL99)
    2618         206 :             tle = findTargetlistEntrySQL99(pstate, sortby->node,
    2619             :                                            targetlist, exprKind);
    2620             :         else
    2621        3514 :             tle = findTargetlistEntrySQL92(pstate, sortby->node,
    2622             :                                            targetlist, exprKind);
    2623             : 
    2624        3714 :         sortlist = addTargetToSortList(pstate, tle,
    2625             :                                        sortlist, *targetlist, sortby);
    2626             :     }
    2627             : 
    2628       27637 :     return sortlist;
    2629             : }
    2630             : 
    2631             : /*
    2632             :  * transformWindowDefinitions -
    2633             :  *      transform window definitions (WindowDef to WindowClause)
    2634             :  */
    2635             : List *
    2636       24119 : transformWindowDefinitions(ParseState *pstate,
    2637             :                            List *windowdefs,
    2638             :                            List **targetlist)
    2639             : {
    2640       24119 :     List       *result = NIL;
    2641       24119 :     Index       winref = 0;
    2642             :     ListCell   *lc;
    2643             : 
    2644       24263 :     foreach(lc, windowdefs)
    2645             :     {
    2646         146 :         WindowDef  *windef = (WindowDef *) lfirst(lc);
    2647         146 :         WindowClause *refwc = NULL;
    2648             :         List       *partitionClause;
    2649             :         List       *orderClause;
    2650             :         WindowClause *wc;
    2651             : 
    2652         146 :         winref++;
    2653             : 
    2654             :         /*
    2655             :          * Check for duplicate window names.
    2656             :          */
    2657         165 :         if (windef->name &&
    2658          19 :             findWindowClause(result, windef->name) != NULL)
    2659           1 :             ereport(ERROR,
    2660             :                     (errcode(ERRCODE_WINDOWING_ERROR),
    2661             :                      errmsg("window \"%s\" is already defined", windef->name),
    2662             :                      parser_errposition(pstate, windef->location)));
    2663             : 
    2664             :         /*
    2665             :          * If it references a previous window, look that up.
    2666             :          */
    2667         145 :         if (windef->refname)
    2668             :         {
    2669           1 :             refwc = findWindowClause(result, windef->refname);
    2670           1 :             if (refwc == NULL)
    2671           0 :                 ereport(ERROR,
    2672             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    2673             :                          errmsg("window \"%s\" does not exist",
    2674             :                                 windef->refname),
    2675             :                          parser_errposition(pstate, windef->location)));
    2676             :         }
    2677             : 
    2678             :         /*
    2679             :          * Transform PARTITION and ORDER specs, if any.  These are treated
    2680             :          * almost exactly like top-level GROUP BY and ORDER BY clauses,
    2681             :          * including the special handling of nondefault operator semantics.
    2682             :          */
    2683         145 :         orderClause = transformSortClause(pstate,
    2684             :                                           windef->orderClause,
    2685             :                                           targetlist,
    2686             :                                           EXPR_KIND_WINDOW_ORDER,
    2687             :                                           true /* force SQL99 rules */ );
    2688         144 :         partitionClause = transformGroupClause(pstate,
    2689             :                                                windef->partitionClause,
    2690             :                                                NULL,
    2691             :                                                targetlist,
    2692             :                                                orderClause,
    2693             :                                                EXPR_KIND_WINDOW_PARTITION,
    2694             :                                                true /* force SQL99 rules */ );
    2695             : 
    2696             :         /*
    2697             :          * And prepare the new WindowClause.
    2698             :          */
    2699         144 :         wc = makeNode(WindowClause);
    2700         144 :         wc->name = windef->name;
    2701         144 :         wc->refname = windef->refname;
    2702             : 
    2703             :         /*
    2704             :          * Per spec, a windowdef that references a previous one copies the
    2705             :          * previous partition clause (and mustn't specify its own).  It can
    2706             :          * specify its own ordering clause, but only if the previous one had
    2707             :          * none.  It always specifies its own frame clause, and the previous
    2708             :          * one must not have a frame clause.  Yeah, it's bizarre that each of
    2709             :          * these cases works differently, but SQL:2008 says so; see 7.11
    2710             :          * <window clause> syntax rule 10 and general rule 1.  The frame
    2711             :          * clause rule is especially bizarre because it makes "OVER foo"
    2712             :          * different from "OVER (foo)", and requires the latter to throw an
    2713             :          * error if foo has a nondefault frame clause.  Well, ours not to
    2714             :          * reason why, but we do go out of our way to throw a useful error
    2715             :          * message for such cases.
    2716             :          */
    2717         144 :         if (refwc)
    2718             :         {
    2719           1 :             if (partitionClause)
    2720           0 :                 ereport(ERROR,
    2721             :                         (errcode(ERRCODE_WINDOWING_ERROR),
    2722             :                          errmsg("cannot override PARTITION BY clause of window \"%s\"",
    2723             :                                 windef->refname),
    2724             :                          parser_errposition(pstate, windef->location)));
    2725           1 :             wc->partitionClause = copyObject(refwc->partitionClause);
    2726             :         }
    2727             :         else
    2728         143 :             wc->partitionClause = partitionClause;
    2729         144 :         if (refwc)
    2730             :         {
    2731           1 :             if (orderClause && refwc->orderClause)
    2732           0 :                 ereport(ERROR,
    2733             :                         (errcode(ERRCODE_WINDOWING_ERROR),
    2734             :                          errmsg("cannot override ORDER BY clause of window \"%s\"",
    2735             :                                 windef->refname),
    2736             :                          parser_errposition(pstate, windef->location)));
    2737           1 :             if (orderClause)
    2738             :             {
    2739           0 :                 wc->orderClause = orderClause;
    2740           0 :                 wc->copiedOrder = false;
    2741             :             }
    2742             :             else
    2743             :             {
    2744           1 :                 wc->orderClause = copyObject(refwc->orderClause);
    2745           1 :                 wc->copiedOrder = true;
    2746             :             }
    2747             :         }
    2748             :         else
    2749             :         {
    2750         143 :             wc->orderClause = orderClause;
    2751         143 :             wc->copiedOrder = false;
    2752             :         }
    2753         144 :         if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
    2754             :         {
    2755             :             /*
    2756             :              * Use this message if this is a WINDOW clause, or if it's an OVER
    2757             :              * clause that includes ORDER BY or framing clauses.  (We already
    2758             :              * rejected PARTITION BY above, so no need to check that.)
    2759             :              */
    2760           0 :             if (windef->name ||
    2761           0 :                 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
    2762           0 :                 ereport(ERROR,
    2763             :                         (errcode(ERRCODE_WINDOWING_ERROR),
    2764             :                          errmsg("cannot copy window \"%s\" because it has a frame clause",
    2765             :                                 windef->refname),
    2766             :                          parser_errposition(pstate, windef->location)));
    2767             :             /* Else this clause is just OVER (foo), so say this: */
    2768           0 :             ereport(ERROR,
    2769             :                     (errcode(ERRCODE_WINDOWING_ERROR),
    2770             :                      errmsg("cannot copy window \"%s\" because it has a frame clause",
    2771             :                             windef->refname),
    2772             :                      errhint("Omit the parentheses in this OVER clause."),
    2773             :                      parser_errposition(pstate, windef->location)));
    2774             :         }
    2775         144 :         wc->frameOptions = windef->frameOptions;
    2776             :         /* Process frame offset expressions */
    2777         144 :         wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
    2778             :                                                windef->startOffset);
    2779         144 :         wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
    2780             :                                              windef->endOffset);
    2781         144 :         wc->winref = winref;
    2782             : 
    2783         144 :         result = lappend(result, wc);
    2784             :     }
    2785             : 
    2786       24117 :     return result;
    2787             : }
    2788             : 
    2789             : /*
    2790             :  * transformDistinctClause -
    2791             :  *    transform a DISTINCT clause
    2792             :  *
    2793             :  * Since we may need to add items to the query's targetlist, that list
    2794             :  * is passed by reference.
    2795             :  *
    2796             :  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
    2797             :  * possible into the distinctClause.  This avoids a possible need to re-sort,
    2798             :  * and allows the user to choose the equality semantics used by DISTINCT,
    2799             :  * should she be working with a datatype that has more than one equality
    2800             :  * operator.
    2801             :  *
    2802             :  * is_agg is true if we are transforming an aggregate(DISTINCT ...)
    2803             :  * function call.  This does not affect any behavior, only the phrasing
    2804             :  * of error messages.
    2805             :  */
    2806             : List *
    2807         104 : transformDistinctClause(ParseState *pstate,
    2808             :                         List **targetlist, List *sortClause, bool is_agg)
    2809             : {
    2810         104 :     List       *result = NIL;
    2811             :     ListCell   *slitem;
    2812             :     ListCell   *tlitem;
    2813             : 
    2814             :     /*
    2815             :      * The distinctClause should consist of all ORDER BY items followed by all
    2816             :      * other non-resjunk targetlist items.  There must not be any resjunk
    2817             :      * ORDER BY items --- that would imply that we are sorting by a value that
    2818             :      * isn't necessarily unique within a DISTINCT group, so the results
    2819             :      * wouldn't be well-defined.  This construction ensures we follow the rule
    2820             :      * that sortClause and distinctClause match; in fact the sortClause will
    2821             :      * always be a prefix of distinctClause.
    2822             :      *
    2823             :      * Note a corner case: the same TLE could be in the ORDER BY list multiple
    2824             :      * times with different sortops.  We have to include it in the
    2825             :      * distinctClause the same way to preserve the prefix property. The net
    2826             :      * effect will be that the TLE value will be made unique according to both
    2827             :      * sortops.
    2828             :      */
    2829         175 :     foreach(slitem, sortClause)
    2830             :     {
    2831          77 :         SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
    2832          77 :         TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
    2833             : 
    2834          77 :         if (tle->resjunk)
    2835           6 :             ereport(ERROR,
    2836             :                     (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    2837             :                      is_agg ?
    2838             :                      errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
    2839             :                      errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
    2840             :                      parser_errposition(pstate,
    2841             :                                         exprLocation((Node *) tle->expr))));
    2842          71 :         result = lappend(result, copyObject(scl));
    2843             :     }
    2844             : 
    2845             :     /*
    2846             :      * Now add any remaining non-resjunk tlist items, using default sort/group
    2847             :      * semantics for their data types.
    2848             :      */
    2849         285 :     foreach(tlitem, *targetlist)
    2850             :     {
    2851         187 :         TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
    2852             : 
    2853         187 :         if (tle->resjunk)
    2854           0 :             continue;           /* ignore junk */
    2855         187 :         result = addTargetToGroupList(pstate, tle,
    2856             :                                       result, *targetlist,
    2857         187 :                                       exprLocation((Node *) tle->expr));
    2858             :     }
    2859             : 
    2860             :     /*
    2861             :      * Complain if we found nothing to make DISTINCT.  Returning an empty list
    2862             :      * would cause the parsed Query to look like it didn't have DISTINCT, with
    2863             :      * results that would probably surprise the user.  Note: this case is
    2864             :      * presently impossible for aggregates because of grammar restrictions,
    2865             :      * but we check anyway.
    2866             :      */
    2867          98 :     if (result == NIL)
    2868           0 :         ereport(ERROR,
    2869             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    2870             :                  is_agg ?
    2871             :                  errmsg("an aggregate with DISTINCT must have at least one argument") :
    2872             :                  errmsg("SELECT DISTINCT must have at least one column")));
    2873             : 
    2874          98 :     return result;
    2875             : }
    2876             : 
    2877             : /*
    2878             :  * transformDistinctOnClause -
    2879             :  *    transform a DISTINCT ON clause
    2880             :  *
    2881             :  * Since we may need to add items to the query's targetlist, that list
    2882             :  * is passed by reference.
    2883             :  *
    2884             :  * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
    2885             :  * possible into the distinctClause.  This avoids a possible need to re-sort,
    2886             :  * and allows the user to choose the equality semantics used by DISTINCT,
    2887             :  * should she be working with a datatype that has more than one equality
    2888             :  * operator.
    2889             :  */
    2890             : List *
    2891          11 : transformDistinctOnClause(ParseState *pstate, List *distinctlist,
    2892             :                           List **targetlist, List *sortClause)
    2893             : {
    2894          11 :     List       *result = NIL;
    2895          11 :     List       *sortgrouprefs = NIL;
    2896             :     bool        skipped_sortitem;
    2897             :     ListCell   *lc;
    2898             :     ListCell   *lc2;
    2899             : 
    2900             :     /*
    2901             :      * Add all the DISTINCT ON expressions to the tlist (if not already
    2902             :      * present, they are added as resjunk items).  Assign sortgroupref numbers
    2903             :      * to them, and make a list of these numbers.  (NB: we rely below on the
    2904             :      * sortgrouprefs list being one-for-one with the original distinctlist.
    2905             :      * Also notice that we could have duplicate DISTINCT ON expressions and
    2906             :      * hence duplicate entries in sortgrouprefs.)
    2907             :      */
    2908          25 :     foreach(lc, distinctlist)
    2909             :     {
    2910          15 :         Node       *dexpr = (Node *) lfirst(lc);
    2911             :         int         sortgroupref;
    2912             :         TargetEntry *tle;
    2913             : 
    2914          15 :         tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
    2915             :                                        EXPR_KIND_DISTINCT_ON);
    2916          14 :         sortgroupref = assignSortGroupRef(tle, *targetlist);
    2917          14 :         sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
    2918             :     }
    2919             : 
    2920             :     /*
    2921             :      * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
    2922             :      * semantics from ORDER BY items that match DISTINCT ON items, and also
    2923             :      * adopt their column sort order.  We insist that the distinctClause and
    2924             :      * sortClause match, so throw error if we find the need to add any more
    2925             :      * distinctClause items after we've skipped an ORDER BY item that wasn't
    2926             :      * in DISTINCT ON.
    2927             :      */
    2928          10 :     skipped_sortitem = false;
    2929          28 :     foreach(lc, sortClause)
    2930             :     {
    2931          19 :         SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
    2932             : 
    2933          19 :         if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
    2934             :         {
    2935          11 :             if (skipped_sortitem)
    2936           1 :                 ereport(ERROR,
    2937             :                         (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    2938             :                          errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
    2939             :                          parser_errposition(pstate,
    2940             :                                             get_matching_location(scl->tleSortGroupRef,
    2941             :                                                                   sortgrouprefs,
    2942             :                                                                   distinctlist))));
    2943             :             else
    2944          10 :                 result = lappend(result, copyObject(scl));
    2945             :         }
    2946             :         else
    2947           8 :             skipped_sortitem = true;
    2948             :     }
    2949             : 
    2950             :     /*
    2951             :      * Now add any remaining DISTINCT ON items, using default sort/group
    2952             :      * semantics for their data types.  (Note: this is pretty questionable; if
    2953             :      * the ORDER BY list doesn't include all the DISTINCT ON items and more
    2954             :      * besides, you certainly aren't using DISTINCT ON in the intended way,
    2955             :      * and you probably aren't going to get consistent results.  It might be
    2956             :      * better to throw an error or warning here.  But historically we've
    2957             :      * allowed it, so keep doing so.)
    2958             :      */
    2959          21 :     forboth(lc, distinctlist, lc2, sortgrouprefs)
    2960             :     {
    2961          12 :         Node       *dexpr = (Node *) lfirst(lc);
    2962          12 :         int         sortgroupref = lfirst_int(lc2);
    2963          12 :         TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
    2964             : 
    2965          12 :         if (targetIsInSortList(tle, InvalidOid, result))
    2966           9 :             continue;           /* already in list (with some semantics) */
    2967           3 :         if (skipped_sortitem)
    2968           0 :             ereport(ERROR,
    2969             :                     (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    2970             :                      errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
    2971             :                      parser_errposition(pstate, exprLocation(dexpr))));
    2972           3 :         result = addTargetToGroupList(pstate, tle,
    2973             :                                       result, *targetlist,
    2974             :                                       exprLocation(dexpr));
    2975             :     }
    2976             : 
    2977             :     /*
    2978             :      * An empty result list is impossible here because of grammar
    2979             :      * restrictions.
    2980             :      */
    2981           9 :     Assert(result != NIL);
    2982             : 
    2983           9 :     return result;
    2984             : }
    2985             : 
    2986             : /*
    2987             :  * get_matching_location
    2988             :  *      Get the exprLocation of the exprs member corresponding to the
    2989             :  *      (first) member of sortgrouprefs that equals sortgroupref.
    2990             :  *
    2991             :  * This is used so that we can point at a troublesome DISTINCT ON entry.
    2992             :  * (Note that we need to use the original untransformed DISTINCT ON list
    2993             :  * item, as whatever TLE it corresponds to will very possibly have a
    2994             :  * parse location pointing to some matching entry in the SELECT list
    2995             :  * or ORDER BY list.)
    2996             :  */
    2997             : static int
    2998           1 : get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
    2999             : {
    3000             :     ListCell   *lcs;
    3001             :     ListCell   *lce;
    3002             : 
    3003           2 :     forboth(lcs, sortgrouprefs, lce, exprs)
    3004             :     {
    3005           2 :         if (lfirst_int(lcs) == sortgroupref)
    3006           1 :             return exprLocation((Node *) lfirst(lce));
    3007             :     }
    3008             :     /* if no match, caller blew it */
    3009           0 :     elog(ERROR, "get_matching_location: no matching sortgroupref");
    3010             :     return -1;                  /* keep compiler quiet */
    3011             : }
    3012             : 
    3013             : /*
    3014             :  * resolve_unique_index_expr
    3015             :  *      Infer a unique index from a list of indexElems, for ON
    3016             :  *      CONFLICT clause
    3017             :  *
    3018             :  * Perform parse analysis of expressions and columns appearing within ON
    3019             :  * CONFLICT clause.  During planning, the returned list of expressions is used
    3020             :  * to infer which unique index to use.
    3021             :  */
    3022             : static List *
    3023         158 : resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
    3024             :                           Relation heapRel)
    3025             : {
    3026         158 :     List       *result = NIL;
    3027             :     ListCell   *l;
    3028             : 
    3029         361 :     foreach(l, infer->indexElems)
    3030             :     {
    3031         204 :         IndexElem  *ielem = (IndexElem *) lfirst(l);
    3032         204 :         InferenceElem *pInfer = makeNode(InferenceElem);
    3033             :         Node       *parse;
    3034             : 
    3035             :         /*
    3036             :          * Raw grammar re-uses CREATE INDEX infrastructure for unique index
    3037             :          * inference clause, and so will accept opclasses by name and so on.
    3038             :          *
    3039             :          * Make no attempt to match ASC or DESC ordering or NULLS FIRST/NULLS
    3040             :          * LAST ordering, since those are not significant for inference
    3041             :          * purposes (any unique index matching the inference specification in
    3042             :          * other regards is accepted indifferently).  Actively reject this as
    3043             :          * wrong-headed.
    3044             :          */
    3045         204 :         if (ielem->ordering != SORTBY_DEFAULT)
    3046           0 :             ereport(ERROR,
    3047             :                     (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    3048             :                      errmsg("ASC/DESC is not allowed in ON CONFLICT clause"),
    3049             :                      parser_errposition(pstate,
    3050             :                                         exprLocation((Node *) infer))));
    3051         204 :         if (ielem->nulls_ordering != SORTBY_NULLS_DEFAULT)
    3052           0 :             ereport(ERROR,
    3053             :                     (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    3054             :                      errmsg("NULLS FIRST/LAST is not allowed in ON CONFLICT clause"),
    3055             :                      parser_errposition(pstate,
    3056             :                                         exprLocation((Node *) infer))));
    3057             : 
    3058         204 :         if (!ielem->expr)
    3059             :         {
    3060             :             /* Simple index attribute */
    3061             :             ColumnRef  *n;
    3062             : 
    3063             :             /*
    3064             :              * Grammar won't have built raw expression for us in event of
    3065             :              * plain column reference.  Create one directly, and perform
    3066             :              * expression transformation.  Planner expects this, and performs
    3067             :              * its own normalization for the purposes of matching against
    3068             :              * pg_index.
    3069             :              */
    3070         181 :             n = makeNode(ColumnRef);
    3071         181 :             n->fields = list_make1(makeString(ielem->name));
    3072             :             /* Location is approximately that of inference specification */
    3073         181 :             n->location = infer->location;
    3074         181 :             parse = (Node *) n;
    3075             :         }
    3076             :         else
    3077             :         {
    3078             :             /* Do parse transformation of the raw expression */
    3079          23 :             parse = (Node *) ielem->expr;
    3080             :         }
    3081             : 
    3082             :         /*
    3083             :          * transformExpr() should have already rejected subqueries,
    3084             :          * aggregates, and window functions, based on the EXPR_KIND_ for an
    3085             :          * index expression.  Expressions returning sets won't have been
    3086             :          * rejected, but don't bother doing so here; there should be no
    3087             :          * available expression unique index to match any such expression
    3088             :          * against anyway.
    3089             :          */
    3090         204 :         pInfer->expr = transformExpr(pstate, parse, EXPR_KIND_INDEX_EXPRESSION);
    3091             : 
    3092             :         /* Perform lookup of collation and operator class as required */
    3093         203 :         if (!ielem->collation)
    3094         196 :             pInfer->infercollid = InvalidOid;
    3095             :         else
    3096           7 :             pInfer->infercollid = LookupCollation(pstate, ielem->collation,
    3097           7 :                                                   exprLocation(pInfer->expr));
    3098             : 
    3099         203 :         if (!ielem->opclass)
    3100         196 :             pInfer->inferopclass = InvalidOid;
    3101             :         else
    3102           7 :             pInfer->inferopclass = get_opclass_oid(BTREE_AM_OID,
    3103             :                                                    ielem->opclass, false);
    3104             : 
    3105         203 :         result = lappend(result, pInfer);
    3106             :     }
    3107             : 
    3108         157 :     return result;
    3109             : }
    3110             : 
    3111             : /*
    3112             :  * transformOnConflictArbiter -
    3113             :  *      transform arbiter expressions in an ON CONFLICT clause.
    3114             :  *
    3115             :  * Transformed expressions used to infer one unique index relation to serve as
    3116             :  * an ON CONFLICT arbiter.  Partial unique indexes may be inferred using WHERE
    3117             :  * clause from inference specification clause.
    3118             :  */
    3119             : void
    3120         172 : transformOnConflictArbiter(ParseState *pstate,
    3121             :                            OnConflictClause *onConflictClause,
    3122             :                            List **arbiterExpr, Node **arbiterWhere,
    3123             :                            Oid *constraint)
    3124             : {
    3125         172 :     InferClause *infer = onConflictClause->infer;
    3126             : 
    3127         172 :     *arbiterExpr = NIL;
    3128         172 :     *arbiterWhere = NULL;
    3129         172 :     *constraint = InvalidOid;
    3130             : 
    3131         172 :     if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
    3132           1 :         ereport(ERROR,
    3133             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    3134             :                  errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
    3135             :                  errhint("For example, ON CONFLICT (column_name)."),
    3136             :                  parser_errposition(pstate,
    3137             :                                     exprLocation((Node *) onConflictClause))));
    3138             : 
    3139             :     /*
    3140             :      * To simplify certain aspects of its design, speculative insertion into
    3141             :      * system catalogs is disallowed
    3142             :      */
    3143         171 :     if (IsCatalogRelation(pstate->p_target_relation))
    3144           0 :         ereport(ERROR,
    3145             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3146             :                  errmsg("ON CONFLICT is not supported with system catalog tables"),
    3147             :                  parser_errposition(pstate,
    3148             :                                     exprLocation((Node *) onConflictClause))));
    3149             : 
    3150             :     /* Same applies to table used by logical decoding as catalog table */
    3151         171 :     if (RelationIsUsedAsCatalogTable(pstate->p_target_relation))
    3152           0 :         ereport(ERROR,
    3153             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3154             :                  errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
    3155             :                         RelationGetRelationName(pstate->p_target_relation)),
    3156             :                  parser_errposition(pstate,
    3157             :                                     exprLocation((Node *) onConflictClause))));
    3158             : 
    3159             :     /* ON CONFLICT DO NOTHING does not require an inference clause */
    3160         171 :     if (infer)
    3161             :     {
    3162             :         List       *save_namespace;
    3163             : 
    3164             :         /*
    3165             :          * While we process the arbiter expressions, accept only non-qualified
    3166             :          * references to the target table. Hide any other relations.
    3167             :          */
    3168         163 :         save_namespace = pstate->p_namespace;
    3169         163 :         pstate->p_namespace = NIL;
    3170         163 :         addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
    3171             :                       false, false, true);
    3172             : 
    3173         163 :         if (infer->indexElems)
    3174         158 :             *arbiterExpr = resolve_unique_index_expr(pstate, infer,
    3175             :                                                      pstate->p_target_relation);
    3176             : 
    3177             :         /*
    3178             :          * Handling inference WHERE clause (for partial unique index
    3179             :          * inference)
    3180             :          */
    3181         162 :         if (infer->whereClause)
    3182           7 :             *arbiterWhere = transformExpr(pstate, infer->whereClause,
    3183             :                                           EXPR_KIND_INDEX_PREDICATE);
    3184             : 
    3185         162 :         pstate->p_namespace = save_namespace;
    3186             : 
    3187         162 :         if (infer->conname)
    3188           5 :             *constraint = get_relation_constraint_oid(RelationGetRelid(pstate->p_target_relation),
    3189           5 :                                                       infer->conname, false);
    3190             :     }
    3191             : 
    3192             :     /*
    3193             :      * It's convenient to form a list of expressions based on the
    3194             :      * representation used by CREATE INDEX, since the same restrictions are
    3195             :      * appropriate (e.g. on subqueries).  However, from here on, a dedicated
    3196             :      * primnode representation is used for inference elements, and so
    3197             :      * assign_query_collations() can be trusted to do the right thing with the
    3198             :      * post parse analysis query tree inference clause representation.
    3199             :      */
    3200         170 : }
    3201             : 
    3202             : /*
    3203             :  * addTargetToSortList
    3204             :  *      If the given targetlist entry isn't already in the SortGroupClause
    3205             :  *      list, add it to the end of the list, using the given sort ordering
    3206             :  *      info.
    3207             :  *
    3208             :  * Returns the updated SortGroupClause list.
    3209             :  */
    3210             : List *
    3211        3750 : addTargetToSortList(ParseState *pstate, TargetEntry *tle,
    3212             :                     List *sortlist, List *targetlist, SortBy *sortby)
    3213             : {
    3214        3750 :     Oid         restype = exprType((Node *) tle->expr);
    3215             :     Oid         sortop;
    3216             :     Oid         eqop;
    3217             :     bool        hashable;
    3218             :     bool        reverse;
    3219             :     int         location;
    3220             :     ParseCallbackState pcbstate;
    3221             : 
    3222             :     /* if tlist item is an UNKNOWN literal, change it to TEXT */
    3223        3750 :     if (restype == UNKNOWNOID)
    3224             :     {
    3225           0 :         tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
    3226             :                                          restype, TEXTOID, -1,
    3227             :                                          COERCION_IMPLICIT,
    3228             :                                          COERCE_IMPLICIT_CAST,
    3229             :                                          -1);
    3230           0 :         restype = TEXTOID;
    3231             :     }
    3232             : 
    3233             :     /*
    3234             :      * Rather than clutter the API of get_sort_group_operators and the other
    3235             :      * functions we're about to use, make use of error context callback to
    3236             :      * mark any error reports with a parse position.  We point to the operator
    3237             :      * location if present, else to the expression being sorted.  (NB: use the
    3238             :      * original untransformed expression here; the TLE entry might well point
    3239             :      * at a duplicate expression in the regular SELECT list.)
    3240             :      */
    3241        3750 :     location = sortby->location;
    3242        3750 :     if (location < 0)
    3243        3718 :         location = exprLocation(sortby->node);
    3244        3750 :     setup_parser_errposition_callback(&pcbstate, pstate, location);
    3245             : 
    3246             :     /* determine the sortop, eqop, and directionality */
    3247        3750 :     switch (sortby->sortby_dir)
    3248             :     {
    3249             :         case SORTBY_DEFAULT:
    3250             :         case SORTBY_ASC:
    3251        3603 :             get_sort_group_operators(restype,
    3252             :                                      true, true, false,
    3253             :                                      &sortop, &eqop, NULL,
    3254             :                                      &hashable);
    3255        3602 :             reverse = false;
    3256        3602 :             break;
    3257             :         case SORTBY_DESC:
    3258         115 :             get_sort_group_operators(restype,
    3259             :                                      false, true, true,
    3260             :                                      NULL, &eqop, &sortop,
    3261             :                                      &hashable);
    3262         115 :             reverse = true;
    3263         115 :             break;
    3264             :         case SORTBY_USING:
    3265          32 :             Assert(sortby->useOp != NIL);
    3266          32 :             sortop = compatible_oper_opid(sortby->useOp,
    3267             :                                           restype,
    3268             :                                           restype,
    3269             :                                           false);
    3270             : 
    3271             :             /*
    3272             :              * Verify it's a valid ordering operator, fetch the corresponding
    3273             :              * equality operator, and determine whether to consider it like
    3274             :              * ASC or DESC.
    3275             :              */
    3276          32 :             eqop = get_equality_op_for_ordering_op(sortop, &reverse);
    3277          32 :             if (!OidIsValid(eqop))
    3278           0 :                 ereport(ERROR,
    3279             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    3280             :                          errmsg("operator %s is not a valid ordering operator",
    3281             :                                 strVal(llast(sortby->useOp))),
    3282             :                          errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
    3283             : 
    3284             :             /*
    3285             :              * Also see if the equality operator is hashable.
    3286             :              */
    3287          32 :             hashable = op_hashjoinable(eqop, restype);
    3288          32 :             break;
    3289             :         default:
    3290           0 :             elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
    3291             :             sortop = InvalidOid;    /* keep compiler quiet */
    3292             :             eqop = InvalidOid;
    3293             :             hashable = false;
    3294             :             reverse = false;
    3295             :             break;
    3296             :     }
    3297             : 
    3298        3749 :     cancel_parser_errposition_callback(&pcbstate);
    3299             : 
    3300             :     /* avoid making duplicate sortlist entries */
    3301        3749 :     if (!targetIsInSortList(tle, sortop, sortlist))
    3302             :     {
    3303        3749 :         SortGroupClause *sortcl = makeNode(SortGroupClause);
    3304             : 
    3305        3749 :         sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
    3306             : 
    3307        3749 :         sortcl->eqop = eqop;
    3308        3749 :         sortcl->sortop = sortop;
    3309        3749 :         sortcl->hashable = hashable;
    3310             : 
    3311        3749 :         switch (sortby->sortby_nulls)
    3312             :         {
    3313             :             case SORTBY_NULLS_DEFAULT:
    3314             :                 /* NULLS FIRST is default for DESC; other way for ASC */
    3315        3734 :                 sortcl->nulls_first = reverse;
    3316        3734 :                 break;
    3317             :             case SORTBY_NULLS_FIRST:
    3318           8 :                 sortcl->nulls_first = true;
    3319           8 :                 break;
    3320             :             case SORTBY_NULLS_LAST:
    3321           7 :                 sortcl->nulls_first = false;
    3322           7 :                 break;
    3323             :             default:
    3324           0 :                 elog(ERROR, "unrecognized sortby_nulls: %d",
    3325             :                      sortby->sortby_nulls);
    3326             :                 break;
    3327             :         }
    3328             : 
    3329        3749 :         sortlist = lappend(sortlist, sortcl);
    3330             :     }
    3331             : 
    3332        3749 :     return sortlist;
    3333             : }
    3334             : 
    3335             : /*
    3336             :  * addTargetToGroupList
    3337             :  *      If the given targetlist entry isn't already in the SortGroupClause
    3338             :  *      list, add it to the end of the list, using default sort/group
    3339             :  *      semantics.
    3340             :  *
    3341             :  * This is very similar to addTargetToSortList, except that we allow the
    3342             :  * case where only a grouping (equality) operator can be found, and that
    3343             :  * the TLE is considered "already in the list" if it appears there with any
    3344             :  * sorting semantics.
    3345             :  *
    3346             :  * location is the parse location to be fingered in event of trouble.  Note
    3347             :  * that we can't rely on exprLocation(tle->expr), because that might point
    3348             :  * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
    3349             :  * to report such a location.
    3350             :  *
    3351             :  * Returns the updated SortGroupClause list.
    3352             :  */
    3353             : static List *
    3354         603 : addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
    3355             :                      List *grouplist, List *targetlist, int location)
    3356             : {
    3357         603 :     Oid         restype = exprType((Node *) tle->expr);
    3358             : 
    3359             :     /* if tlist item is an UNKNOWN literal, change it to TEXT */
    3360         603 :     if (restype == UNKNOWNOID)
    3361             :     {
    3362           0 :         tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
    3363             :                                          restype, TEXTOID, -1,
    3364             :                                          COERCION_IMPLICIT,
    3365             :                                          COERCE_IMPLICIT_CAST,
    3366             :                                          -1);
    3367           0 :         restype = TEXTOID;
    3368             :     }
    3369             : 
    3370             :     /* avoid making duplicate grouplist entries */
    3371         603 :     if (!targetIsInSortList(tle, InvalidOid, grouplist))
    3372             :     {
    3373         536 :         SortGroupClause *grpcl = makeNode(SortGroupClause);
    3374             :         Oid         sortop;
    3375             :         Oid         eqop;
    3376             :         bool        hashable;
    3377             :         ParseCallbackState pcbstate;
    3378             : 
    3379         536 :         setup_parser_errposition_callback(&pcbstate, pstate, location);
    3380             : 
    3381             :         /* determine the eqop and optional sortop */
    3382         536 :         get_sort_group_operators(restype,
    3383             :                                  false, true, false,
    3384             :                                  &sortop, &eqop, NULL,
    3385             :                                  &hashable);
    3386             : 
    3387         536 :         cancel_parser_errposition_callback(&pcbstate);
    3388             : 
    3389         536 :         grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
    3390         536 :         grpcl->eqop = eqop;
    3391         536 :         grpcl->sortop = sortop;
    3392         536 :         grpcl->nulls_first = false; /* OK with or without sortop */
    3393         536 :         grpcl->hashable = hashable;
    3394             : 
    3395         536 :         grouplist = lappend(grouplist, grpcl);
    3396             :     }
    3397             : 
    3398         603 :     return grouplist;
    3399             : }
    3400             : 
    3401             : /*
    3402             :  * assignSortGroupRef
    3403             :  *    Assign the targetentry an unused ressortgroupref, if it doesn't
    3404             :  *    already have one.  Return the assigned or pre-existing refnumber.
    3405             :  *
    3406             :  * 'tlist' is the targetlist containing (or to contain) the given targetentry.
    3407             :  */
    3408             : Index
    3409        4453 : assignSortGroupRef(TargetEntry *tle, List *tlist)
    3410             : {
    3411             :     Index       maxRef;
    3412             :     ListCell   *l;
    3413             : 
    3414        4453 :     if (tle->ressortgroupref)    /* already has one? */
    3415          47 :         return tle->ressortgroupref;
    3416             : 
    3417             :     /* easiest way to pick an unused refnumber: max used + 1 */
    3418        4406 :     maxRef = 0;
    3419       21434 :     foreach(l, tlist)
    3420             :     {
    3421       17028 :         Index       ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
    3422             : 
    3423       17028 :         if (ref > maxRef)
    3424        1367 :             maxRef = ref;
    3425             :     }
    3426        4406 :     tle->ressortgroupref = maxRef + 1;
    3427        4406 :     return tle->ressortgroupref;
    3428             : }
    3429             : 
    3430             : /*
    3431             :  * targetIsInSortList
    3432             :  *      Is the given target item already in the sortlist?
    3433             :  *      If sortop is not InvalidOid, also test for a match to the sortop.
    3434             :  *
    3435             :  * It is not an oversight that this function ignores the nulls_first flag.
    3436             :  * We check sortop when determining if an ORDER BY item is redundant with
    3437             :  * earlier ORDER BY items, because it's conceivable that "ORDER BY
    3438             :  * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
    3439             :  * values that < considers equal.  We need not check nulls_first
    3440             :  * however, because a lower-order column with the same sortop but
    3441             :  * opposite nulls direction is redundant.  Also, we can consider
    3442             :  * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
    3443             :  *
    3444             :  * Works for both ordering and grouping lists (sortop would normally be
    3445             :  * InvalidOid when considering grouping).  Note that the main reason we need
    3446             :  * this routine (and not just a quick test for nonzeroness of ressortgroupref)
    3447             :  * is that a TLE might be in only one of the lists.
    3448             :  */
    3449             : bool
    3450        4585 : targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
    3451             : {
    3452        4585 :     Index       ref = tle->ressortgroupref;
    3453             :     ListCell   *l;
    3454             : 
    3455             :     /* no need to scan list if tle has no marker */
    3456        4585 :     if (ref == 0)
    3457        4268 :         return false;
    3458             : 
    3459         396 :     foreach(l, sortList)
    3460             :     {
    3461         201 :         SortGroupClause *scl = (SortGroupClause *) lfirst(l);
    3462             : 
    3463         201 :         if (scl->tleSortGroupRef == ref &&
    3464           0 :             (sortop == InvalidOid ||
    3465           0 :              sortop == scl->sortop ||
    3466           0 :              sortop == get_commutator(scl->sortop)))
    3467         122 :             return true;
    3468             :     }
    3469         195 :     return false;
    3470             : }
    3471             : 
    3472             : /*
    3473             :  * findWindowClause
    3474             :  *      Find the named WindowClause in the list, or return NULL if not there
    3475             :  */
    3476             : static WindowClause *
    3477          20 : findWindowClause(List *wclist, const char *name)
    3478             : {
    3479             :     ListCell   *l;
    3480             : 
    3481          21 :     foreach(l, wclist)
    3482             :     {
    3483           3 :         WindowClause *wc = (WindowClause *) lfirst(l);
    3484             : 
    3485           3 :         if (wc->name && strcmp(wc->name, name) == 0)
    3486           2 :             return wc;
    3487             :     }
    3488             : 
    3489          18 :     return NULL;
    3490             : }
    3491             : 
    3492             : /*
    3493             :  * transformFrameOffset
    3494             :  *      Process a window frame offset expression
    3495             :  */
    3496             : static Node *
    3497         288 : transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
    3498             : {
    3499         288 :     const char *constructName = NULL;
    3500             :     Node       *node;
    3501             : 
    3502             :     /* Quick exit if no offset expression */
    3503         288 :     if (clause == NULL)
    3504         268 :         return NULL;
    3505             : 
    3506          20 :     if (frameOptions & FRAMEOPTION_ROWS)
    3507             :     {
    3508             :         /* Transform the raw expression tree */
    3509          20 :         node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
    3510             : 
    3511             :         /*
    3512             :          * Like LIMIT clause, simply coerce to int8
    3513             :          */
    3514          20 :         constructName = "ROWS";
    3515          20 :         node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
    3516             :     }
    3517           0 :     else if (frameOptions & FRAMEOPTION_RANGE)
    3518             :     {
    3519             :         /* Transform the raw expression tree */
    3520           0 :         node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
    3521             : 
    3522             :         /*
    3523             :          * this needs a lot of thought to decide how to support in the context
    3524             :          * of Postgres' extensible datatype framework
    3525             :          */
    3526           0 :         constructName = "RANGE";
    3527             :         /* error was already thrown by gram.y, this is just a backstop */
    3528           0 :         elog(ERROR, "window frame with value offset is not implemented");
    3529             :     }
    3530             :     else
    3531             :     {
    3532           0 :         Assert(false);
    3533             :         node = NULL;
    3534             :     }
    3535             : 
    3536             :     /* Disallow variables in frame offsets */
    3537          20 :     checkExprIsVarFree(pstate, node, constructName);
    3538             : 
    3539          20 :     return node;
    3540             : }

Generated by: LCOV version 1.11