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

Generated by: LCOV version 1.14