LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_clause.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 93.7 % 1061 994 67 994
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 41 41 41
Baseline: lcov-20250906-005545-baseline Branches: 73.5 % 944 694 250 694
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 5 5 5
(360..) days: 93.7 % 1056 989 67 989
Function coverage date bins:
(360..) days: 100.0 % 41 41 41
Branch coverage date bins:
(360..) days: 73.5 % 944 694 250 694

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

Generated by: LCOV version 2.4-beta