LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_clause.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 93.5 % 1054 985 3 18 44 4 22 653 63 247 42 696 1 24
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 41 41 38 3 40 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 94.7 % 57 54 3 54
Legend: Lines: hit not hit (120,180] days: 100.0 % 4 4 4
(180,240] days: 100.0 % 5 5 2 2 1 3
(240..) days: 93.3 % 988 922 18 44 4 22 651 3 246 40 655
Function coverage date bins:
(60,120] days: 100.0 % 3 3 3
(240..) days: 48.7 % 78 38 38 40

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

Generated by: LCOV version v1.16-55-g56c0a2a