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

Generated by: LCOV version 1.14