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

Generated by: LCOV version 2.0-1