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

Generated by: LCOV version 2.0-1