LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_agg.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 75.5 % 799 603 1 22 69 104 18 360 17 208 74 371 2
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 23 23 22 1 23
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 94.4 % 18 17 1 17
Legend: Lines: hit not hit (240..) days: 75.0 % 781 586 22 69 104 18 360 208 74 356
Function coverage date bins:
(60,120] days: 100.0 % 1 1 1
(240..) days: 50.0 % 44 22 22 22

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * parse_agg.c
                                  4                 :  *    handle aggregates and window functions in parser
                                  5                 :  *
                                  6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
                                  7                 :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :  *
                                  9                 :  *
                                 10                 :  * IDENTIFICATION
                                 11                 :  *    src/backend/parser/parse_agg.c
                                 12                 :  *
                                 13                 :  *-------------------------------------------------------------------------
                                 14                 :  */
                                 15                 : #include "postgres.h"
                                 16                 : 
                                 17                 : #include "access/htup_details.h"
                                 18                 : #include "catalog/pg_aggregate.h"
                                 19                 : #include "catalog/pg_constraint.h"
                                 20                 : #include "catalog/pg_type.h"
                                 21                 : #include "nodes/makefuncs.h"
                                 22                 : #include "nodes/nodeFuncs.h"
                                 23                 : #include "optimizer/optimizer.h"
                                 24                 : #include "parser/parse_agg.h"
                                 25                 : #include "parser/parse_clause.h"
                                 26                 : #include "parser/parse_coerce.h"
                                 27                 : #include "parser/parse_expr.h"
                                 28                 : #include "parser/parsetree.h"
                                 29                 : #include "rewrite/rewriteManip.h"
                                 30                 : #include "utils/builtins.h"
                                 31                 : #include "utils/lsyscache.h"
                                 32                 : #include "utils/syscache.h"
                                 33                 : 
                                 34                 : typedef struct
                                 35                 : {
                                 36                 :     ParseState *pstate;
                                 37                 :     int         min_varlevel;
                                 38                 :     int         min_agglevel;
                                 39                 :     int         sublevels_up;
                                 40                 : } check_agg_arguments_context;
                                 41                 : 
                                 42                 : typedef struct
                                 43                 : {
                                 44                 :     ParseState *pstate;
                                 45                 :     Query      *qry;
                                 46                 :     bool        hasJoinRTEs;
                                 47                 :     List       *groupClauses;
                                 48                 :     List       *groupClauseCommonVars;
                                 49                 :     bool        have_non_var_grouping;
                                 50                 :     List      **func_grouped_rels;
                                 51                 :     int         sublevels_up;
                                 52                 :     bool        in_agg_direct_args;
                                 53                 : } check_ungrouped_columns_context;
                                 54                 : 
                                 55                 : static int  check_agg_arguments(ParseState *pstate,
                                 56                 :                                 List *directargs,
                                 57                 :                                 List *args,
                                 58                 :                                 Expr *filter);
                                 59                 : static bool check_agg_arguments_walker(Node *node,
                                 60                 :                                        check_agg_arguments_context *context);
                                 61                 : static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
                                 62                 :                                     List *groupClauses, List *groupClauseCommonVars,
                                 63                 :                                     bool have_non_var_grouping,
                                 64                 :                                     List **func_grouped_rels);
                                 65                 : static bool check_ungrouped_columns_walker(Node *node,
                                 66                 :                                            check_ungrouped_columns_context *context);
                                 67                 : static void finalize_grouping_exprs(Node *node, ParseState *pstate, Query *qry,
                                 68                 :                                     List *groupClauses, bool hasJoinRTEs,
                                 69                 :                                     bool have_non_var_grouping);
                                 70                 : static bool finalize_grouping_exprs_walker(Node *node,
                                 71                 :                                            check_ungrouped_columns_context *context);
                                 72                 : static void check_agglevels_and_constraints(ParseState *pstate, Node *expr);
                                 73                 : static List *expand_groupingset_node(GroupingSet *gs);
                                 74                 : static Node *make_agg_arg(Oid argtype, Oid argcollation);
                                 75                 : 
                                 76                 : 
                                 77                 : /*
                                 78                 :  * transformAggregateCall -
                                 79                 :  *      Finish initial transformation of an aggregate call
                                 80                 :  *
                                 81                 :  * parse_func.c has recognized the function as an aggregate, and has set up
                                 82                 :  * all the fields of the Aggref except aggargtypes, aggdirectargs, args,
                                 83                 :  * aggorder, aggdistinct and agglevelsup.  The passed-in args list has been
                                 84                 :  * through standard expression transformation and type coercion to match the
                                 85                 :  * agg's declared arg types, while the passed-in aggorder list hasn't been
                                 86                 :  * transformed at all.
                                 87                 :  *
                                 88                 :  * Here we separate the args list into direct and aggregated args, storing the
                                 89                 :  * former in agg->aggdirectargs and the latter in agg->args.  The regular
                                 90                 :  * args, but not the direct args, are converted into a targetlist by inserting
                                 91                 :  * TargetEntry nodes.  We then transform the aggorder and agg_distinct
                                 92                 :  * specifications to produce lists of SortGroupClause nodes for agg->aggorder
                                 93                 :  * and agg->aggdistinct.  (For a regular aggregate, this might result in
                                 94                 :  * adding resjunk expressions to the targetlist; but for ordered-set
                                 95                 :  * aggregates the aggorder list will always be one-to-one with the aggregated
                                 96                 :  * args.)
                                 97                 :  *
                                 98                 :  * We must also determine which query level the aggregate actually belongs to,
                                 99                 :  * set agglevelsup accordingly, and mark p_hasAggs true in the corresponding
                                100                 :  * pstate level.
                                101                 :  */
                                102                 : void
 4771 tgl                       103 GIC       22722 : transformAggregateCall(ParseState *pstate, Aggref *agg,
 4771 tgl                       104 ECB             :                        List *args, List *aggorder, bool agg_distinct)
                                105                 : {
 2487 tgl                       106 GIC       22722 :     List       *argtypes = NIL;
 3394 tgl                       107 CBC       22722 :     List       *tlist = NIL;
                                108           22722 :     List       *torder = NIL;
 4790 bruce                     109           22722 :     List       *tdistinct = NIL;
 3394 tgl                       110           22722 :     AttrNumber  attno = 1;
 4790 bruce                     111 ECB             :     int         save_next_resno;
                                112                 :     ListCell   *lc;
                                113                 : 
                                114                 :     /*
                                115                 :      * Before separating the args into direct and aggregated args, make a list
                                116                 :      * of their data type OIDs for use later.
                                117                 :      */
 2487 tgl                       118 GIC       42192 :     foreach(lc, args)
 2487 tgl                       119 ECB             :     {
 2487 tgl                       120 GIC       19470 :         Expr       *arg = (Expr *) lfirst(lc);
 2487 tgl                       121 ECB             : 
 2487 tgl                       122 GIC       19470 :         argtypes = lappend_oid(argtypes, exprType((Node *) arg));
 2487 tgl                       123 ECB             :     }
 2487 tgl                       124 GIC       22722 :     agg->aggargtypes = argtypes;
 2487 tgl                       125 ECB             : 
 3394 tgl                       126 GIC       22722 :     if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
 4863 tgl                       127 ECB             :     {
                                128                 :         /*
                                129                 :          * For an ordered-set agg, the args list includes direct args and
                                130                 :          * aggregated args; we must split them apart.
                                131                 :          */
 3394 tgl                       132 GIC         159 :         int         numDirectArgs = list_length(args) - list_length(aggorder);
 3394 tgl                       133 ECB             :         List       *aargs;
                                134                 :         ListCell   *lc2;
                                135                 : 
 3394 tgl                       136 GIC         159 :         Assert(numDirectArgs >= 0);
 4863 tgl                       137 ECB             : 
 3394 tgl                       138 GIC         159 :         aargs = list_copy_tail(args, numDirectArgs);
 3394 tgl                       139 CBC         159 :         agg->aggdirectargs = list_truncate(args, numDirectArgs);
 4863 tgl                       140 ECB             : 
                                141                 :         /*
                                142                 :          * Build a tlist from the aggregated args, and make a sortlist entry
                                143                 :          * for each one.  Note that the expressions in the SortBy nodes are
                                144                 :          * ignored (they are the raw versions of the transformed args); we are
                                145                 :          * just looking at the sort information in the SortBy nodes.
                                146                 :          */
 3394 tgl                       147 GIC         345 :         forboth(lc, aargs, lc2, aggorder)
 3394 tgl                       148 ECB             :         {
 3394 tgl                       149 GIC         186 :             Expr       *arg = (Expr *) lfirst(lc);
 3394 tgl                       150 CBC         186 :             SortBy     *sortby = (SortBy *) lfirst(lc2);
 3394 tgl                       151 ECB             :             TargetEntry *tle;
                                152                 : 
                                153                 :             /* We don't bother to assign column names to the entries */
 3394 tgl                       154 GIC         186 :             tle = makeTargetEntry(arg, attno++, NULL, false);
 3394 tgl                       155 CBC         186 :             tlist = lappend(tlist, tle);
 3394 tgl                       156 ECB             : 
 3394 tgl                       157 GIC         186 :             torder = addTargetToSortList(pstate, tle,
 2265 tgl                       158 ECB             :                                          torder, tlist, sortby);
                                159                 :         }
                                160                 : 
                                161                 :         /* Never any DISTINCT in an ordered-set agg */
 3394 tgl                       162 GIC         159 :         Assert(!agg_distinct);
 3394 tgl                       163 ECB             :     }
                                164                 :     else
                                165                 :     {
                                166                 :         /* Regular aggregate, so it has no direct args */
 3394 tgl                       167 GIC       22563 :         agg->aggdirectargs = NIL;
 3394 tgl                       168 ECB             : 
                                169                 :         /*
                                170                 :          * Transform the plain list of Exprs into a targetlist.
                                171                 :          */
 3394 tgl                       172 GIC       41664 :         foreach(lc, args)
 3394 tgl                       173 ECB             :         {
 3394 tgl                       174 GIC       19101 :             Expr       *arg = (Expr *) lfirst(lc);
 3394 tgl                       175 ECB             :             TargetEntry *tle;
                                176                 : 
                                177                 :             /* We don't bother to assign column names to the entries */
 3394 tgl                       178 GIC       19101 :             tle = makeTargetEntry(arg, attno++, NULL, false);
 3394 tgl                       179 CBC       19101 :             tlist = lappend(tlist, tle);
 3394 tgl                       180 ECB             :         }
                                181                 : 
                                182                 :         /*
                                183                 :          * If we have an ORDER BY, transform it.  This will add columns to the
                                184                 :          * tlist if they appear in ORDER BY but weren't already in the arg
                                185                 :          * list.  They will be marked resjunk = true so we can tell them apart
                                186                 :          * from regular aggregate arguments later.
                                187                 :          *
                                188                 :          * We need to mess with p_next_resno since it will be used to number
                                189                 :          * any new targetlist entries.
                                190                 :          */
 3394 tgl                       191 GIC       22563 :         save_next_resno = pstate->p_next_resno;
 3394 tgl                       192 CBC       22563 :         pstate->p_next_resno = attno;
 3394 tgl                       193 ECB             : 
 3394 tgl                       194 GIC       22563 :         torder = transformSortClause(pstate,
 3394 tgl                       195 ECB             :                                      aggorder,
                                196                 :                                      &tlist,
                                197                 :                                      EXPR_KIND_ORDER_BY,
                                198                 :                                      true /* force SQL99 rules */ );
                                199                 : 
                                200                 :         /*
                                201                 :          * If we have DISTINCT, transform that to produce a distinctList.
                                202                 :          */
 3394 tgl                       203 GIC       22563 :         if (agg_distinct)
 4863 tgl                       204 ECB             :         {
 3394 tgl                       205 GIC         251 :             tdistinct = transformDistinctClause(pstate, &tlist, torder, true);
 4863 tgl                       206 ECB             : 
                                207                 :             /*
                                208                 :              * Remove this check if executor support for hashed distinct for
                                209                 :              * aggregates is ever added.
                                210                 :              */
 3394 tgl                       211 GIC         544 :             foreach(lc, tdistinct)
 4863 tgl                       212 ECB             :             {
 3394 tgl                       213 GIC         311 :                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
 3394 tgl                       214 ECB             : 
 3394 tgl                       215 GIC         311 :                 if (!OidIsValid(sortcl->sortop))
 3394 tgl                       216 ECB             :                 {
 3394 tgl                       217 UIC           0 :                     Node       *expr = get_sortgroupclause_expr(sortcl, tlist);
 3394 tgl                       218 EUB             : 
 3394 tgl                       219 UIC           0 :                     ereport(ERROR,
 3394 tgl                       220 EUB             :                             (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                221                 :                              errmsg("could not identify an ordering operator for type %s",
                                222                 :                                     format_type_be(exprType(expr))),
                                223                 :                              errdetail("Aggregates with DISTINCT must be able to sort their inputs."),
                                224                 :                              parser_errposition(pstate, exprLocation(expr))));
                                225                 :                 }
                                226                 :             }
                                227                 :         }
                                228                 : 
 3394 tgl                       229 GIC       22545 :         pstate->p_next_resno = save_next_resno;
 4863 tgl                       230 ECB             :     }
                                231                 : 
                                232                 :     /* Update the Aggref with the transformation results */
 4863 tgl                       233 GIC       22704 :     agg->args = tlist;
 4863 tgl                       234 CBC       22704 :     agg->aggorder = torder;
                                235           22704 :     agg->aggdistinct = tdistinct;
 4863 tgl                       236 ECB             : 
 2885 andres                    237 GIC       22704 :     check_agglevels_and_constraints(pstate, (Node *) agg);
 2885 andres                    238 CBC       22635 : }
 2885 andres                    239 ECB             : 
                                240                 : /*
                                241                 :  * transformGroupingFunc
                                242                 :  *      Transform a GROUPING expression
                                243                 :  *
                                244                 :  * GROUPING() behaves very like an aggregate.  Processing of levels and nesting
                                245                 :  * is done as for aggregates.  We set p_hasAggs for these expressions too.
                                246                 :  */
                                247                 : Node *
 2885 andres                    248 GIC         157 : transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
 2885 andres                    249 ECB             : {
                                250                 :     ListCell   *lc;
 2885 andres                    251 GIC         157 :     List       *args = p->args;
 2885 andres                    252 CBC         157 :     List       *result_list = NIL;
                                253             157 :     GroupingFunc *result = makeNode(GroupingFunc);
 2885 andres                    254 ECB             : 
 2885 andres                    255 GIC         157 :     if (list_length(args) > 31)
 2885 andres                    256 LBC           0 :         ereport(ERROR,
 2885 andres                    257 EUB             :                 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
                                258                 :                  errmsg("GROUPING must have fewer than 32 arguments"),
                                259                 :                  parser_errposition(pstate, p->location)));
                                260                 : 
 2885 andres                    261 GIC         412 :     foreach(lc, args)
 2885 andres                    262 ECB             :     {
                                263                 :         Node       *current_result;
                                264                 : 
 2878 bruce                     265 GIC         255 :         current_result = transformExpr(pstate, (Node *) lfirst(lc), pstate->p_expr_kind);
 2885 andres                    266 ECB             : 
                                267                 :         /* acceptability of expressions is checked later */
                                268                 : 
 2885 andres                    269 GIC         255 :         result_list = lappend(result_list, current_result);
 2885 andres                    270 ECB             :     }
                                271                 : 
 2885 andres                    272 GIC         157 :     result->args = result_list;
 2885 andres                    273 CBC         157 :     result->location = p->location;
 2885 andres                    274 ECB             : 
 2885 andres                    275 GIC         157 :     check_agglevels_and_constraints(pstate, (Node *) result);
 2885 andres                    276 ECB             : 
 2885 andres                    277 GIC         157 :     return (Node *) result;
 2885 andres                    278 ECB             : }
                                279                 : 
                                280                 : /*
                                281                 :  * Aggregate functions and grouping operations (which are combined in the spec
                                282                 :  * as <set function specification>) are very similar with regard to level and
                                283                 :  * nesting restrictions (though we allow a lot more things than the spec does).
                                284                 :  * Centralise those restrictions here.
                                285                 :  */
                                286                 : static void
 2885 andres                    287 GIC       22861 : check_agglevels_and_constraints(ParseState *pstate, Node *expr)
 2885 andres                    288 ECB             : {
 2885 andres                    289 GIC       22861 :     List       *directargs = NIL;
 2885 andres                    290 CBC       22861 :     List       *args = NIL;
                                291           22861 :     Expr       *filter = NULL;
 2885 andres                    292 ECB             :     int         min_varlevel;
 2885 andres                    293 GIC       22861 :     int         location = -1;
 2885 andres                    294 ECB             :     Index      *p_levelsup;
                                295                 :     const char *err;
                                296                 :     bool        errkind;
 2885 andres                    297 GIC       22861 :     bool        isAgg = IsA(expr, Aggref);
 2885 andres                    298 ECB             : 
 2885 andres                    299 GIC       22861 :     if (isAgg)
 2885 andres                    300 ECB             :     {
 2878 bruce                     301 GIC       22704 :         Aggref     *agg = (Aggref *) expr;
 2885 andres                    302 ECB             : 
 2885 andres                    303 GIC       22704 :         directargs = agg->aggdirectargs;
 2885 andres                    304 CBC       22704 :         args = agg->args;
                                305           22704 :         filter = agg->aggfilter;
                                306           22704 :         location = agg->location;
                                307           22704 :         p_levelsup = &agg->agglevelsup;
 2885 andres                    308 ECB             :     }
                                309                 :     else
                                310                 :     {
 2885 andres                    311 GIC         157 :         GroupingFunc *grp = (GroupingFunc *) expr;
 2885 andres                    312 ECB             : 
 2885 andres                    313 GIC         157 :         args = grp->args;
 2885 andres                    314 CBC         157 :         location = grp->location;
                                315             157 :         p_levelsup = &grp->agglevelsup;
 2885 andres                    316 ECB             :     }
                                317                 : 
                                318                 :     /*
                                319                 :      * Check the arguments to compute the aggregate's level and detect
                                320                 :      * improper nesting.
                                321                 :      */
 3394 tgl                       322 GIC       22861 :     min_varlevel = check_agg_arguments(pstate,
 2885 andres                    323 ECB             :                                        directargs,
                                324                 :                                        args,
                                325                 :                                        filter);
                                326                 : 
 2885 andres                    327 GIC       22837 :     *p_levelsup = min_varlevel;
 3894 tgl                       328 ECB             : 
                                329                 :     /* Mark the correct pstate level as having aggregates */
 3894 tgl                       330 GIC       22926 :     while (min_varlevel-- > 0)
 3894 tgl                       331 CBC          89 :         pstate = pstate->parentParseState;
                                332           22837 :     pstate->p_hasAggs = true;
 7247 tgl                       333 ECB             : 
                                334                 :     /*
                                335                 :      * Check to see if the aggregate function is in an invalid place within
                                336                 :      * its aggregation query.
                                337                 :      *
                                338                 :      * For brevity we support two schemes for reporting an error here: set
                                339                 :      * "err" to a custom message, or set "errkind" true if the error context
                                340                 :      * is sufficiently identified by what ParseExprKindName will return, *and*
                                341                 :      * what it will return is just a SQL keyword.  (Otherwise, use a custom
                                342                 :      * message to avoid creating translation problems.)
                                343                 :      */
 3894 tgl                       344 GIC       22837 :     err = NULL;
 3894 tgl                       345 CBC       22837 :     errkind = false;
                                346           22837 :     switch (pstate->p_expr_kind)
 7247 tgl                       347 ECB             :     {
 3894 tgl                       348 UIC           0 :         case EXPR_KIND_NONE:
 3894 tgl                       349 UBC           0 :             Assert(false);      /* can't happen */
 3894 tgl                       350 EUB             :             break;
 3894 tgl                       351 UIC           0 :         case EXPR_KIND_OTHER:
 2878 bruce                     352 EUB             : 
                                353                 :             /*
                                354                 :              * Accept aggregate/grouping here; caller must throw error if
                                355                 :              * wanted
                                356                 :              */
 3894 tgl                       357 UIC           0 :             break;
 3894 tgl                       358 UBC           0 :         case EXPR_KIND_JOIN_ON:
 3894 tgl                       359 EUB             :         case EXPR_KIND_JOIN_USING:
 2885 andres                    360 UIC           0 :             if (isAgg)
 2885 andres                    361 UBC           0 :                 err = _("aggregate functions are not allowed in JOIN conditions");
 2885 andres                    362 EUB             :             else
 2885 andres                    363 UIC           0 :                 err = _("grouping operations are not allowed in JOIN conditions");
 2885 andres                    364 EUB             : 
 3894 tgl                       365 UIC           0 :             break;
 3894 tgl                       366 GBC          12 :         case EXPR_KIND_FROM_SUBSELECT:
 3894 tgl                       367 ECB             :             /* Should only be possible in a LATERAL subquery */
 3894 tgl                       368 GIC          12 :             Assert(pstate->p_lateral_active);
 2878 bruce                     369 ECB             : 
                                370                 :             /*
                                371                 :              * Aggregate/grouping scope rules make it worth being explicit
                                372                 :              * here
                                373                 :              */
 2885 andres                    374 GIC          12 :             if (isAgg)
 2885 andres                    375 CBC          12 :                 err = _("aggregate functions are not allowed in FROM clause of their own query level");
 2885 andres                    376 ECB             :             else
 2885 andres                    377 UIC           0 :                 err = _("grouping operations are not allowed in FROM clause of their own query level");
 2885 andres                    378 EUB             : 
 3894 tgl                       379 GIC          12 :             break;
 3894 tgl                       380 LBC           0 :         case EXPR_KIND_FROM_FUNCTION:
 2885 andres                    381 UBC           0 :             if (isAgg)
                                382               0 :                 err = _("aggregate functions are not allowed in functions in FROM");
 2885 andres                    383 EUB             :             else
 2885 andres                    384 UIC           0 :                 err = _("grouping operations are not allowed in functions in FROM");
 2885 andres                    385 EUB             : 
 3894 tgl                       386 UIC           0 :             break;
 3894 tgl                       387 GBC           6 :         case EXPR_KIND_WHERE:
 3894 tgl                       388 CBC           6 :             errkind = true;
 2811 mail                      389               6 :             break;
                                390               3 :         case EXPR_KIND_POLICY:
                                391               3 :             if (isAgg)
                                392               3 :                 err = _("aggregate functions are not allowed in policy expressions");
 2811 mail                      393 ECB             :             else
 2811 mail                      394 UIC           0 :                 err = _("grouping operations are not allowed in policy expressions");
 2811 mail                      395 EUB             : 
 3894 tgl                       396 GIC           3 :             break;
 3894 tgl                       397 CBC         265 :         case EXPR_KIND_HAVING:
 3894 tgl                       398 ECB             :             /* okay */
 3894 tgl                       399 GIC         265 :             break;
 3554 noah                      400 CBC           6 :         case EXPR_KIND_FILTER:
                                401               6 :             errkind = true;
                                402               6 :             break;
 3894 tgl                       403 LBC           0 :         case EXPR_KIND_WINDOW_PARTITION:
 3894 tgl                       404 EUB             :             /* okay */
 3894 tgl                       405 UIC           0 :             break;
 3894 tgl                       406 GBC           6 :         case EXPR_KIND_WINDOW_ORDER:
 3894 tgl                       407 ECB             :             /* okay */
 3894 tgl                       408 GIC           6 :             break;
 3894 tgl                       409 LBC           0 :         case EXPR_KIND_WINDOW_FRAME_RANGE:
 2885 andres                    410 UBC           0 :             if (isAgg)
                                411               0 :                 err = _("aggregate functions are not allowed in window RANGE");
 2885 andres                    412 EUB             :             else
 2885 andres                    413 UIC           0 :                 err = _("grouping operations are not allowed in window RANGE");
 2885 andres                    414 EUB             : 
 3894 tgl                       415 UIC           0 :             break;
 3894 tgl                       416 UBC           0 :         case EXPR_KIND_WINDOW_FRAME_ROWS:
 2885 andres                    417               0 :             if (isAgg)
                                418               0 :                 err = _("aggregate functions are not allowed in window ROWS");
 2885 andres                    419 EUB             :             else
 2885 andres                    420 UIC           0 :                 err = _("grouping operations are not allowed in window ROWS");
 2885 andres                    421 EUB             : 
 1887 tgl                       422 UIC           0 :             break;
 1887 tgl                       423 UBC           0 :         case EXPR_KIND_WINDOW_FRAME_GROUPS:
                                424               0 :             if (isAgg)
                                425               0 :                 err = _("aggregate functions are not allowed in window GROUPS");
 1887 tgl                       426 EUB             :             else
 1887 tgl                       427 UIC           0 :                 err = _("grouping operations are not allowed in window GROUPS");
 1887 tgl                       428 EUB             : 
 3894 tgl                       429 UIC           0 :             break;
 3894 tgl                       430 GBC       22487 :         case EXPR_KIND_SELECT_TARGET:
 3894 tgl                       431 ECB             :             /* okay */
 3894 tgl                       432 GIC       22487 :             break;
 3894 tgl                       433 LBC           0 :         case EXPR_KIND_INSERT_TARGET:
 3894 tgl                       434 EUB             :         case EXPR_KIND_UPDATE_SOURCE:
                                435                 :         case EXPR_KIND_UPDATE_TARGET:
 3894 tgl                       436 UIC           0 :             errkind = true;
  377 alvherre                  437 UBC           0 :             break;
                                438               0 :         case EXPR_KIND_MERGE_WHEN:
                                439               0 :             if (isAgg)
                                440               0 :                 err = _("aggregate functions are not allowed in MERGE WHEN conditions");
  377 alvherre                  441 EUB             :             else
  377 alvherre                  442 UIC           0 :                 err = _("grouping operations are not allowed in MERGE WHEN conditions");
  377 alvherre                  443 EUB             : 
 3894 tgl                       444 UIC           0 :             break;
 3894 tgl                       445 UBC           0 :         case EXPR_KIND_GROUP_BY:
                                446               0 :             errkind = true;
                                447               0 :             break;
 3894 tgl                       448 GBC          34 :         case EXPR_KIND_ORDER_BY:
 3894 tgl                       449 ECB             :             /* okay */
 3894 tgl                       450 GIC          34 :             break;
 3894 tgl                       451 LBC           0 :         case EXPR_KIND_DISTINCT_ON:
 3894 tgl                       452 EUB             :             /* okay */
 3894 tgl                       453 UIC           0 :             break;
 3894 tgl                       454 UBC           0 :         case EXPR_KIND_LIMIT:
 3894 tgl                       455 EUB             :         case EXPR_KIND_OFFSET:
 3894 tgl                       456 UIC           0 :             errkind = true;
 3894 tgl                       457 UBC           0 :             break;
                                458               0 :         case EXPR_KIND_RETURNING:
                                459               0 :             errkind = true;
                                460               0 :             break;
                                461               0 :         case EXPR_KIND_VALUES:
 2274 tgl                       462 EUB             :         case EXPR_KIND_VALUES_SINGLE:
 3894 tgl                       463 UIC           0 :             errkind = true;
 3894 tgl                       464 UBC           0 :             break;
                                465               0 :         case EXPR_KIND_CHECK_CONSTRAINT:
 3894 tgl                       466 EUB             :         case EXPR_KIND_DOMAIN_CHECK:
 2885 andres                    467 UIC           0 :             if (isAgg)
 2885 andres                    468 UBC           0 :                 err = _("aggregate functions are not allowed in check constraints");
 2885 andres                    469 EUB             :             else
 2885 andres                    470 UIC           0 :                 err = _("grouping operations are not allowed in check constraints");
 2885 andres                    471 EUB             : 
 3894 tgl                       472 UIC           0 :             break;
 3894 tgl                       473 GBC           3 :         case EXPR_KIND_COLUMN_DEFAULT:
 3894 tgl                       474 ECB             :         case EXPR_KIND_FUNCTION_DEFAULT:
                                475                 : 
 2885 andres                    476 GIC           3 :             if (isAgg)
 2885 andres                    477 CBC           3 :                 err = _("aggregate functions are not allowed in DEFAULT expressions");
 2885 andres                    478 ECB             :             else
 2885 andres                    479 UIC           0 :                 err = _("grouping operations are not allowed in DEFAULT expressions");
 2885 andres                    480 EUB             : 
 3894 tgl                       481 GIC           3 :             break;
 3894 tgl                       482 LBC           0 :         case EXPR_KIND_INDEX_EXPRESSION:
 2885 andres                    483 UBC           0 :             if (isAgg)
                                484               0 :                 err = _("aggregate functions are not allowed in index expressions");
 2885 andres                    485 EUB             :             else
 2885 andres                    486 UIC           0 :                 err = _("grouping operations are not allowed in index expressions");
 2885 andres                    487 EUB             : 
 3894 tgl                       488 UIC           0 :             break;
 3894 tgl                       489 UBC           0 :         case EXPR_KIND_INDEX_PREDICATE:
 2885 andres                    490               0 :             if (isAgg)
                                491               0 :                 err = _("aggregate functions are not allowed in index predicates");
 2885 andres                    492 EUB             :             else
 2885 andres                    493 UIC           0 :                 err = _("grouping operations are not allowed in index predicates");
 2885 andres                    494 EUB             : 
  744 tomas.vondra              495 UIC           0 :             break;
  744 tomas.vondra              496 UBC           0 :         case EXPR_KIND_STATS_EXPRESSION:
                                497               0 :             if (isAgg)
                                498               0 :                 err = _("aggregate functions are not allowed in statistics expressions");
  744 tomas.vondra              499 EUB             :             else
  744 tomas.vondra              500 UIC           0 :                 err = _("grouping operations are not allowed in statistics expressions");
  744 tomas.vondra              501 EUB             : 
 3894 tgl                       502 UIC           0 :             break;
 3894 tgl                       503 UBC           0 :         case EXPR_KIND_ALTER_COL_TRANSFORM:
 2885 andres                    504               0 :             if (isAgg)
                                505               0 :                 err = _("aggregate functions are not allowed in transform expressions");
 2885 andres                    506 EUB             :             else
 2885 andres                    507 UIC           0 :                 err = _("grouping operations are not allowed in transform expressions");
 2885 andres                    508 EUB             : 
 3894 tgl                       509 UIC           0 :             break;
 3894 tgl                       510 UBC           0 :         case EXPR_KIND_EXECUTE_PARAMETER:
 2885 andres                    511               0 :             if (isAgg)
                                512               0 :                 err = _("aggregate functions are not allowed in EXECUTE parameters");
 2885 andres                    513 EUB             :             else
 2885 andres                    514 UIC           0 :                 err = _("grouping operations are not allowed in EXECUTE parameters");
 2885 andres                    515 EUB             : 
 3894 tgl                       516 UIC           0 :             break;
 3894 tgl                       517 UBC           0 :         case EXPR_KIND_TRIGGER_WHEN:
 2885 andres                    518               0 :             if (isAgg)
                                519               0 :                 err = _("aggregate functions are not allowed in trigger WHEN conditions");
 2885 andres                    520 EUB             :             else
 2885 andres                    521 UIC           0 :                 err = _("grouping operations are not allowed in trigger WHEN conditions");
 2885 andres                    522 EUB             : 
 1535 peter                     523 UIC           0 :             break;
 1535 peter                     524 GBC           6 :         case EXPR_KIND_PARTITION_BOUND:
 1535 peter                     525 CBC           6 :             if (isAgg)
                                526               6 :                 err = _("aggregate functions are not allowed in partition bound");
 1535 peter                     527 ECB             :             else
 1535 peter                     528 UIC           0 :                 err = _("grouping operations are not allowed in partition bound");
 1535 peter                     529 EUB             : 
 3894 tgl                       530 GIC           6 :             break;
 2314 rhaas                     531 CBC           3 :         case EXPR_KIND_PARTITION_EXPRESSION:
                                532               3 :             if (isAgg)
 1884 tgl                       533               3 :                 err = _("aggregate functions are not allowed in partition key expressions");
 2314 rhaas                     534 ECB             :             else
 1884 tgl                       535 UIC           0 :                 err = _("grouping operations are not allowed in partition key expressions");
 2314 rhaas                     536 EUB             : 
 2314 rhaas                     537 GIC           3 :             break;
 1471 peter                     538 CBC           3 :         case EXPR_KIND_GENERATED_COLUMN:
 1471 peter                     539 ECB             : 
 1471 peter                     540 GIC           3 :             if (isAgg)
 1471 peter                     541 CBC           3 :                 err = _("aggregate functions are not allowed in column generation expressions");
 1471 peter                     542 ECB             :             else
 1471 peter                     543 UIC           0 :                 err = _("grouping operations are not allowed in column generation expressions");
 1471 peter                     544 EUB             : 
 1471 peter                     545 GIC           3 :             break;
 7247 tgl                       546 ECB             : 
 1884 tgl                       547 UIC           0 :         case EXPR_KIND_CALL_ARGUMENT:
 1956 peter_e                   548 UBC           0 :             if (isAgg)
                                549               0 :                 err = _("aggregate functions are not allowed in CALL arguments");
 1956 peter_e                   550 EUB             :             else
 1956 peter_e                   551 UIC           0 :                 err = _("grouping operations are not allowed in CALL arguments");
 1956 peter_e                   552 EUB             : 
 1956 peter_e                   553 UIC           0 :             break;
 1956 peter_e                   554 EUB             : 
 1541 tomas.vondra              555 GIC           3 :         case EXPR_KIND_COPY_WHERE:
 1541 tomas.vondra              556 CBC           3 :             if (isAgg)
                                557               3 :                 err = _("aggregate functions are not allowed in COPY FROM WHERE conditions");
 1541 tomas.vondra              558 ECB             :             else
 1541 tomas.vondra              559 UIC           0 :                 err = _("grouping operations are not allowed in COPY FROM WHERE conditions");
 1541 tomas.vondra              560 EUB             : 
 1541 tomas.vondra              561 GIC           3 :             break;
 1541 tomas.vondra              562 ECB             : 
  797 peter                     563 UIC           0 :         case EXPR_KIND_CYCLE_MARK:
  797 peter                     564 UBC           0 :             errkind = true;
                                565               0 :             break;
  797 peter                     566 EUB             : 
                                567                 :             /*
                                568                 :              * There is intentionally no default: case here, so that the
                                569                 :              * compiler will warn if we add a new ParseExprKind without
                                570                 :              * extending this switch.  If we do see an unrecognized value at
                                571                 :              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
                                572                 :              * which is sane anyway.
                                573                 :              */
                                574                 :     }
                                575                 : 
 3894 tgl                       576 GIC       22837 :     if (err)
 5215 tgl                       577 CBC          33 :         ereport(ERROR,
 5215 tgl                       578 ECB             :                 (errcode(ERRCODE_GROUPING_ERROR),
                                579                 :                  errmsg_internal("%s", err),
                                580                 :                  parser_errposition(pstate, location)));
                                581                 : 
 3894 tgl                       582 GIC       22804 :     if (errkind)
 2885 andres                    583 ECB             :     {
 2885 andres                    584 GIC          12 :         if (isAgg)
 2885 andres                    585 ECB             :             /* translator: %s is name of a SQL construct, eg GROUP BY */
 2885 andres                    586 GIC          12 :             err = _("aggregate functions are not allowed in %s");
 2885 andres                    587 ECB             :         else
                                588                 :             /* translator: %s is name of a SQL construct, eg GROUP BY */
 2885 andres                    589 UIC           0 :             err = _("grouping operations are not allowed in %s");
 2885 andres                    590 EUB             : 
 3894 tgl                       591 GIC          12 :         ereport(ERROR,
 3894 tgl                       592 ECB             :                 (errcode(ERRCODE_GROUPING_ERROR),
                                593                 :                  errmsg_internal(err,
                                594                 :                                  ParseExprKindName(pstate->p_expr_kind)),
                                595                 :                  parser_errposition(pstate, location)));
                                596                 :     }
 3894 tgl                       597 GIC       22792 : }
 5215 tgl                       598 ECB             : 
                                599                 : /*
                                600                 :  * check_agg_arguments
                                601                 :  *    Scan the arguments of an aggregate function to determine the
                                602                 :  *    aggregate's semantic level (zero is the current select's level,
                                603                 :  *    one is its parent, etc).
                                604                 :  *
                                605                 :  * The aggregate's level is the same as the level of the lowest-level variable
                                606                 :  * or aggregate in its aggregated arguments (including any ORDER BY columns)
                                607                 :  * or filter expression; or if it contains no variables at all, we presume it
                                608                 :  * to be local.
                                609                 :  *
                                610                 :  * Vars/Aggs in direct arguments are *not* counted towards determining the
                                611                 :  * agg's level, as those arguments aren't evaluated per-row but only
                                612                 :  * per-group, and so in some sense aren't really agg arguments.  However,
                                613                 :  * this can mean that we decide an agg is upper-level even when its direct
                                614                 :  * args contain lower-level Vars/Aggs, and that case has to be disallowed.
                                615                 :  * (This is a little strange, but the SQL standard seems pretty definite that
                                616                 :  * direct args are not to be considered when setting the agg's level.)
                                617                 :  *
                                618                 :  * We also take this opportunity to detect any aggregates or window functions
                                619                 :  * nested within the arguments.  We can throw error immediately if we find
                                620                 :  * a window function.  Aggregates are a bit trickier because it's only an
                                621                 :  * error if the inner aggregate is of the same semantic level as the outer,
                                622                 :  * which we can't know until we finish scanning the arguments.
                                623                 :  */
                                624                 : static int
 3394 tgl                       625 GIC       22861 : check_agg_arguments(ParseState *pstate,
 3394 tgl                       626 ECB             :                     List *directargs,
                                627                 :                     List *args,
                                628                 :                     Expr *filter)
                                629                 : {
                                630                 :     int         agglevel;
                                631                 :     check_agg_arguments_context context;
                                632                 : 
 3894 tgl                       633 GIC       22861 :     context.pstate = pstate;
 3894 tgl                       634 CBC       22861 :     context.min_varlevel = -1;  /* signifies nothing found yet */
                                635           22861 :     context.min_agglevel = -1;
                                636           22861 :     context.sublevels_up = 0;
 3894 tgl                       637 ECB             : 
  599 tgl                       638 GIC       22861 :     (void) check_agg_arguments_walker((Node *) args, &context);
  599 tgl                       639 CBC       22858 :     (void) check_agg_arguments_walker((Node *) filter, &context);
 3554 noah                      640 ECB             : 
                                641                 :     /*
                                642                 :      * If we found no vars nor aggs at all, it's a level-zero aggregate;
                                643                 :      * otherwise, its level is the minimum of vars or aggs.
                                644                 :      */
 3894 tgl                       645 GIC       22858 :     if (context.min_varlevel < 0)
 3894 tgl                       646 ECB             :     {
 3894 tgl                       647 GIC        5832 :         if (context.min_agglevel < 0)
 3394 tgl                       648 CBC        5832 :             agglevel = 0;
 3394 tgl                       649 ECB             :         else
 3394 tgl                       650 UIC           0 :             agglevel = context.min_agglevel;
 3894 tgl                       651 EUB             :     }
 3894 tgl                       652 GIC       17026 :     else if (context.min_agglevel < 0)
 3894 tgl                       653 CBC       17008 :         agglevel = context.min_varlevel;
 3894 tgl                       654 ECB             :     else
 3894 tgl                       655 GIC          18 :         agglevel = Min(context.min_varlevel, context.min_agglevel);
 3894 tgl                       656 ECB             : 
                                657                 :     /*
                                658                 :      * If there's a nested aggregate of the same semantic level, complain.
                                659                 :      */
 3894 tgl                       660 GIC       22858 :     if (agglevel == context.min_agglevel)
 3394 tgl                       661 ECB             :     {
                                662                 :         int         aggloc;
                                663                 : 
 3394 tgl                       664 GIC          15 :         aggloc = locate_agg_of_level((Node *) args, agglevel);
 3394 tgl                       665 CBC          15 :         if (aggloc < 0)
                                666               6 :             aggloc = locate_agg_of_level((Node *) filter, agglevel);
 3897                           667              15 :         ereport(ERROR,
 3897 tgl                       668 ECB             :                 (errcode(ERRCODE_GROUPING_ERROR),
                                669                 :                  errmsg("aggregate function calls cannot be nested"),
                                670                 :                  parser_errposition(pstate, aggloc)));
                                671                 :     }
                                672                 : 
                                673                 :     /*
                                674                 :      * Now check for vars/aggs in the direct arguments, and throw error if
                                675                 :      * needed.  Note that we allow a Var of the agg's semantic level, but not
                                676                 :      * an Agg of that level.  In principle such Aggs could probably be
                                677                 :      * supported, but it would create an ordering dependency among the
                                678                 :      * aggregates at execution time.  Since the case appears neither to be
                                679                 :      * required by spec nor particularly useful, we just treat it as a
                                680                 :      * nested-aggregate situation.
                                681                 :      */
 3394 tgl                       682 GIC       22843 :     if (directargs)
 3394 tgl                       683 ECB             :     {
 3394 tgl                       684 GIC         156 :         context.min_varlevel = -1;
 3394 tgl                       685 CBC         156 :         context.min_agglevel = -1;
  599                           686             156 :         (void) check_agg_arguments_walker((Node *) directargs, &context);
 3394                           687             156 :         if (context.min_varlevel >= 0 && context.min_varlevel < agglevel)
                                688               3 :             ereport(ERROR,
 3394 tgl                       689 ECB             :                     (errcode(ERRCODE_GROUPING_ERROR),
                                690                 :                      errmsg("outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
                                691                 :                      parser_errposition(pstate,
                                692                 :                                         locate_var_of_level((Node *) directargs,
                                693                 :                                                             context.min_varlevel))));
 3394 tgl                       694 GIC         153 :         if (context.min_agglevel >= 0 && context.min_agglevel <= agglevel)
 3394 tgl                       695 CBC           3 :             ereport(ERROR,
 3394 tgl                       696 ECB             :                     (errcode(ERRCODE_GROUPING_ERROR),
                                697                 :                      errmsg("aggregate function calls cannot be nested"),
                                698                 :                      parser_errposition(pstate,
                                699                 :                                         locate_agg_of_level((Node *) directargs,
                                700                 :                                                             context.min_agglevel))));
                                701                 :     }
 3894 tgl                       702 GIC       22837 :     return agglevel;
 3894 tgl                       703 ECB             : }
                                704                 : 
                                705                 : static bool
 3894 tgl                       706 GIC       98009 : check_agg_arguments_walker(Node *node,
 3894 tgl                       707 ECB             :                            check_agg_arguments_context *context)
                                708                 : {
 3894 tgl                       709 GIC       98009 :     if (node == NULL)
 3894 tgl                       710 CBC       28443 :         return false;
                                711           69566 :     if (IsA(node, Var))
 3894 tgl                       712 ECB             :     {
 3894 tgl                       713 GIC       19344 :         int         varlevelsup = ((Var *) node)->varlevelsup;
 3894 tgl                       714 ECB             : 
                                715                 :         /* convert levelsup to frame of reference of original query */
 3894 tgl                       716 GIC       19344 :         varlevelsup -= context->sublevels_up;
 3894 tgl                       717 ECB             :         /* ignore local vars of subqueries */
 3894 tgl                       718 GIC       19344 :         if (varlevelsup >= 0)
 3894 tgl                       719 ECB             :         {
 3894 tgl                       720 GIC       19309 :             if (context->min_varlevel < 0 ||
 3894 tgl                       721 CBC        2253 :                 context->min_varlevel > varlevelsup)
                                722           17071 :                 context->min_varlevel = varlevelsup;
 3894 tgl                       723 ECB             :         }
 3894 tgl                       724 GIC       19344 :         return false;
 3894 tgl                       725 ECB             :     }
 3894 tgl                       726 GIC       50222 :     if (IsA(node, Aggref))
 3894 tgl                       727 ECB             :     {
 3894 tgl                       728 GIC          24 :         int         agglevelsup = ((Aggref *) node)->agglevelsup;
 3894 tgl                       729 ECB             : 
                                730                 :         /* convert levelsup to frame of reference of original query */
 3894 tgl                       731 GIC          24 :         agglevelsup -= context->sublevels_up;
 3894 tgl                       732 ECB             :         /* ignore local aggs of subqueries */
 3894 tgl                       733 GIC          24 :         if (agglevelsup >= 0)
 3894 tgl                       734 ECB             :         {
 3894 tgl                       735 GIC          21 :             if (context->min_agglevel < 0 ||
 3894 tgl                       736 LBC           0 :                 context->min_agglevel > agglevelsup)
 3894 tgl                       737 GBC          21 :                 context->min_agglevel = agglevelsup;
 3894 tgl                       738 ECB             :         }
                                739                 :         /* Continue and descend into subtree */
                                740                 :     }
 2885 andres                    741 GIC       50222 :     if (IsA(node, GroupingFunc))
 2885 andres                    742 ECB             :     {
 2885 andres                    743 UIC           0 :         int         agglevelsup = ((GroupingFunc *) node)->agglevelsup;
 2885 andres                    744 EUB             : 
                                745                 :         /* convert levelsup to frame of reference of original query */
 2885 andres                    746 UIC           0 :         agglevelsup -= context->sublevels_up;
 2885 andres                    747 EUB             :         /* ignore local aggs of subqueries */
 2885 andres                    748 UIC           0 :         if (agglevelsup >= 0)
 2885 andres                    749 EUB             :         {
 2885 andres                    750 UIC           0 :             if (context->min_agglevel < 0 ||
 2885 andres                    751 UBC           0 :                 context->min_agglevel > agglevelsup)
                                752               0 :                 context->min_agglevel = agglevelsup;
 2885 andres                    753 EUB             :         }
                                754                 :         /* Continue and descend into subtree */
                                755                 :     }
                                756                 : 
                                757                 :     /*
                                758                 :      * SRFs and window functions can be rejected immediately, unless we are
                                759                 :      * within a sub-select within the aggregate's arguments; in that case
                                760                 :      * they're OK.
                                761                 :      */
 2112 tgl                       762 GIC       50222 :     if (context->sublevels_up == 0)
 2112 tgl                       763 ECB             :     {
 1058 tgl                       764 GIC       50028 :         if ((IsA(node, FuncExpr) && ((FuncExpr *) node)->funcretset) ||
 1058 tgl                       765 CBC       50025 :             (IsA(node, OpExpr) && ((OpExpr *) node)->opretset))
 2112                           766               3 :             ereport(ERROR,
 2112 tgl                       767 ECB             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                768                 :                      errmsg("aggregate function calls cannot contain set-returning function calls"),
                                769                 :                      errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
                                770                 :                      parser_errposition(context->pstate, exprLocation(node))));
 2112 tgl                       771 GIC       50025 :         if (IsA(node, WindowFunc))
 2112 tgl                       772 LBC           0 :             ereport(ERROR,
 2112 tgl                       773 EUB             :                     (errcode(ERRCODE_GROUPING_ERROR),
                                774                 :                      errmsg("aggregate function calls cannot contain window function calls"),
                                775                 :                      parser_errposition(context->pstate,
                                776                 :                                         ((WindowFunc *) node)->location)));
                                777                 :     }
 3894 tgl                       778 GIC       50219 :     if (IsA(node, Query))
 3894 tgl                       779 ECB             :     {
                                780                 :         /* Recurse into subselects */
                                781                 :         bool        result;
                                782                 : 
 3894 tgl                       783 GIC          30 :         context->sublevels_up++;
 3894 tgl                       784 CBC          30 :         result = query_tree_walker((Query *) node,
 3894 tgl                       785 ECB             :                                    check_agg_arguments_walker,
                                786                 :                                    (void *) context,
                                787                 :                                    0);
 3894 tgl                       788 GIC          30 :         context->sublevels_up--;
 3894 tgl                       789 CBC          30 :         return result;
 3894 tgl                       790 ECB             :     }
                                791                 : 
 3894 tgl                       792 GIC       50189 :     return expression_tree_walker(node,
 3894 tgl                       793 ECB             :                                   check_agg_arguments_walker,
                                794                 :                                   (void *) context);
                                795                 : }
                                796                 : 
                                797                 : /*
                                798                 :  * transformWindowFuncCall -
                                799                 :  *      Finish initial transformation of a window function call
                                800                 :  *
                                801                 :  * parse_func.c has recognized the function as a window function, and has set
                                802                 :  * up all the fields of the WindowFunc except winref.  Here we must (1) add
                                803                 :  * the WindowDef to the pstate (if not a duplicate of one already present) and
                                804                 :  * set winref to link to it; and (2) mark p_hasWindowFuncs true in the pstate.
                                805                 :  * Unlike aggregates, only the most closely nested pstate level need be
                                806                 :  * considered --- there are no "outer window functions" per SQL spec.
                                807                 :  */
                                808                 : void
 5215 tgl                       809 GIC        1725 : transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
 5215 tgl                       810 ECB             :                         WindowDef *windef)
                                811                 : {
                                812                 :     const char *err;
                                813                 :     bool        errkind;
                                814                 : 
                                815                 :     /*
                                816                 :      * A window function call can't contain another one (but aggs are OK). XXX
                                817                 :      * is this required by spec, or just an unimplemented feature?
                                818                 :      *
                                819                 :      * Note: we don't need to check the filter expression here, because the
                                820                 :      * context checks done below and in transformAggregateCall would have
                                821                 :      * already rejected any window funcs or aggs within the filter.
                                822                 :      */
 5215 tgl                       823 GIC        2064 :     if (pstate->p_hasWindowFuncs &&
 3894 tgl                       824 CBC         339 :         contain_windowfuncs((Node *) wfunc->args))
 5215 tgl                       825 LBC           0 :         ereport(ERROR,
 5215 tgl                       826 EUB             :                 (errcode(ERRCODE_WINDOWING_ERROR),
                                827                 :                  errmsg("window function calls cannot be nested"),
                                828                 :                  parser_errposition(pstate,
                                829                 :                                     locate_windowfunc((Node *) wfunc->args))));
                                830                 : 
                                831                 :     /*
                                832                 :      * Check to see if the window function is in an invalid place within the
                                833                 :      * query.
                                834                 :      *
                                835                 :      * For brevity we support two schemes for reporting an error here: set
                                836                 :      * "err" to a custom message, or set "errkind" true if the error context
                                837                 :      * is sufficiently identified by what ParseExprKindName will return, *and*
                                838                 :      * what it will return is just a SQL keyword.  (Otherwise, use a custom
                                839                 :      * message to avoid creating translation problems.)
                                840                 :      */
 3894 tgl                       841 GIC        1725 :     err = NULL;
 3894 tgl                       842 CBC        1725 :     errkind = false;
                                843            1725 :     switch (pstate->p_expr_kind)
 3894 tgl                       844 ECB             :     {
 3894 tgl                       845 UIC           0 :         case EXPR_KIND_NONE:
 3894 tgl                       846 UBC           0 :             Assert(false);      /* can't happen */
 3894 tgl                       847 EUB             :             break;
 3894 tgl                       848 UIC           0 :         case EXPR_KIND_OTHER:
 3894 tgl                       849 EUB             :             /* Accept window func here; caller must throw error if wanted */
 3894 tgl                       850 UIC           0 :             break;
 3894 tgl                       851 GBC           3 :         case EXPR_KIND_JOIN_ON:
 3894 tgl                       852 ECB             :         case EXPR_KIND_JOIN_USING:
 3894 tgl                       853 GIC           3 :             err = _("window functions are not allowed in JOIN conditions");
 3894 tgl                       854 CBC           3 :             break;
 3894 tgl                       855 LBC           0 :         case EXPR_KIND_FROM_SUBSELECT:
 3894 tgl                       856 EUB             :             /* can't get here, but just in case, throw an error */
 3894 tgl                       857 UIC           0 :             errkind = true;
 3894 tgl                       858 UBC           0 :             break;
                                859               0 :         case EXPR_KIND_FROM_FUNCTION:
                                860               0 :             err = _("window functions are not allowed in functions in FROM");
                                861               0 :             break;
 3894 tgl                       862 GBC           6 :         case EXPR_KIND_WHERE:
 3894 tgl                       863 CBC           6 :             errkind = true;
                                864               6 :             break;
 2811 mail                      865 LBC           0 :         case EXPR_KIND_POLICY:
 2811 mail                      866 UBC           0 :             err = _("window functions are not allowed in policy expressions");
                                867               0 :             break;
 3894 tgl                       868               0 :         case EXPR_KIND_HAVING:
                                869               0 :             errkind = true;
                                870               0 :             break;
 3554 noah                      871               0 :         case EXPR_KIND_FILTER:
                                872               0 :             errkind = true;
                                873               0 :             break;
 3894 tgl                       874 GBC           3 :         case EXPR_KIND_WINDOW_PARTITION:
 3894 tgl                       875 ECB             :         case EXPR_KIND_WINDOW_ORDER:
                                876                 :         case EXPR_KIND_WINDOW_FRAME_RANGE:
                                877                 :         case EXPR_KIND_WINDOW_FRAME_ROWS:
                                878                 :         case EXPR_KIND_WINDOW_FRAME_GROUPS:
 3894 tgl                       879 GIC           3 :             err = _("window functions are not allowed in window definitions");
 3894 tgl                       880 CBC           3 :             break;
                                881            1698 :         case EXPR_KIND_SELECT_TARGET:
 3894 tgl                       882 ECB             :             /* okay */
 3894 tgl                       883 GIC        1698 :             break;
 3894 tgl                       884 LBC           0 :         case EXPR_KIND_INSERT_TARGET:
 3894 tgl                       885 EUB             :         case EXPR_KIND_UPDATE_SOURCE:
                                886                 :         case EXPR_KIND_UPDATE_TARGET:
 3894 tgl                       887 UIC           0 :             errkind = true;
 3894 tgl                       888 UBC           0 :             break;
  377 alvherre                  889               0 :         case EXPR_KIND_MERGE_WHEN:
                                890               0 :             err = _("window functions are not allowed in MERGE WHEN conditions");
                                891               0 :             break;
 3894 tgl                       892               0 :         case EXPR_KIND_GROUP_BY:
                                893               0 :             errkind = true;
                                894               0 :             break;
 3894 tgl                       895 GBC           3 :         case EXPR_KIND_ORDER_BY:
 3894 tgl                       896 ECB             :             /* okay */
 3894 tgl                       897 GIC           3 :             break;
 3894 tgl                       898 LBC           0 :         case EXPR_KIND_DISTINCT_ON:
 3894 tgl                       899 EUB             :             /* okay */
 3894 tgl                       900 UIC           0 :             break;
 3894 tgl                       901 UBC           0 :         case EXPR_KIND_LIMIT:
 3894 tgl                       902 EUB             :         case EXPR_KIND_OFFSET:
 3894 tgl                       903 UIC           0 :             errkind = true;
 3894 tgl                       904 UBC           0 :             break;
 3894 tgl                       905 GBC           3 :         case EXPR_KIND_RETURNING:
 3894 tgl                       906 CBC           3 :             errkind = true;
                                907               3 :             break;
 3894 tgl                       908 LBC           0 :         case EXPR_KIND_VALUES:
 2274 tgl                       909 EUB             :         case EXPR_KIND_VALUES_SINGLE:
 3894 tgl                       910 UIC           0 :             errkind = true;
 3894 tgl                       911 UBC           0 :             break;
                                912               0 :         case EXPR_KIND_CHECK_CONSTRAINT:
 3894 tgl                       913 EUB             :         case EXPR_KIND_DOMAIN_CHECK:
 3746 peter_e                   914 UIC           0 :             err = _("window functions are not allowed in check constraints");
 3894 tgl                       915 UBC           0 :             break;
                                916               0 :         case EXPR_KIND_COLUMN_DEFAULT:
 3894 tgl                       917 EUB             :         case EXPR_KIND_FUNCTION_DEFAULT:
 3894 tgl                       918 UIC           0 :             err = _("window functions are not allowed in DEFAULT expressions");
 3894 tgl                       919 UBC           0 :             break;
                                920               0 :         case EXPR_KIND_INDEX_EXPRESSION:
                                921               0 :             err = _("window functions are not allowed in index expressions");
                                922               0 :             break;
  744 tomas.vondra              923               0 :         case EXPR_KIND_STATS_EXPRESSION:
                                924               0 :             err = _("window functions are not allowed in statistics expressions");
                                925               0 :             break;
 3894 tgl                       926               0 :         case EXPR_KIND_INDEX_PREDICATE:
                                927               0 :             err = _("window functions are not allowed in index predicates");
                                928               0 :             break;
                                929               0 :         case EXPR_KIND_ALTER_COL_TRANSFORM:
                                930               0 :             err = _("window functions are not allowed in transform expressions");
                                931               0 :             break;
                                932               0 :         case EXPR_KIND_EXECUTE_PARAMETER:
                                933               0 :             err = _("window functions are not allowed in EXECUTE parameters");
                                934               0 :             break;
                                935               0 :         case EXPR_KIND_TRIGGER_WHEN:
                                936               0 :             err = _("window functions are not allowed in trigger WHEN conditions");
                                937               0 :             break;
 1535 peter                     938               0 :         case EXPR_KIND_PARTITION_BOUND:
                                939               0 :             err = _("window functions are not allowed in partition bound");
                                940               0 :             break;
 2314 rhaas                     941 GBC           3 :         case EXPR_KIND_PARTITION_EXPRESSION:
 1884 tgl                       942 CBC           3 :             err = _("window functions are not allowed in partition key expressions");
 2314 rhaas                     943               3 :             break;
 1884 tgl                       944 LBC           0 :         case EXPR_KIND_CALL_ARGUMENT:
 1956 peter_e                   945 UBC           0 :             err = _("window functions are not allowed in CALL arguments");
                                946               0 :             break;
 1541 tomas.vondra              947 GBC           3 :         case EXPR_KIND_COPY_WHERE:
 1541 tomas.vondra              948 CBC           3 :             err = _("window functions are not allowed in COPY FROM WHERE conditions");
                                949               3 :             break;
 1471 peter                     950               3 :         case EXPR_KIND_GENERATED_COLUMN:
                                951               3 :             err = _("window functions are not allowed in column generation expressions");
                                952               3 :             break;
  797 peter                     953 LBC           0 :         case EXPR_KIND_CYCLE_MARK:
  797 peter                     954 UBC           0 :             errkind = true;
                                955               0 :             break;
 3894 tgl                       956 EUB             : 
                                957                 :             /*
                                958                 :              * There is intentionally no default: case here, so that the
                                959                 :              * compiler will warn if we add a new ParseExprKind without
                                960                 :              * extending this switch.  If we do see an unrecognized value at
                                961                 :              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
                                962                 :              * which is sane anyway.
                                963                 :              */
                                964                 :     }
 3894 tgl                       965 GIC        1725 :     if (err)
 3894 tgl                       966 CBC          15 :         ereport(ERROR,
 3894 tgl                       967 ECB             :                 (errcode(ERRCODE_WINDOWING_ERROR),
                                968                 :                  errmsg_internal("%s", err),
                                969                 :                  parser_errposition(pstate, wfunc->location)));
 3894 tgl                       970 GIC        1710 :     if (errkind)
 3894 tgl                       971 CBC           9 :         ereport(ERROR,
 3894 tgl                       972 ECB             :                 (errcode(ERRCODE_WINDOWING_ERROR),
                                973                 :         /* translator: %s is name of a SQL construct, eg GROUP BY */
                                974                 :                  errmsg("window functions are not allowed in %s",
                                975                 :                         ParseExprKindName(pstate->p_expr_kind)),
                                976                 :                  parser_errposition(pstate, wfunc->location)));
                                977                 : 
                                978                 :     /*
                                979                 :      * If the OVER clause just specifies a window name, find that WINDOW
                                980                 :      * clause (which had better be present).  Otherwise, try to match all the
                                981                 :      * properties of the OVER clause, and make a new entry in the p_windowdefs
                                982                 :      * list if no luck.
                                983                 :      */
 5212 tgl                       984 GIC        1701 :     if (windef->name)
 5215 tgl                       985 ECB             :     {
 5215 tgl                       986 GIC         321 :         Index       winref = 0;
 5215 tgl                       987 ECB             :         ListCell   *lc;
                                988                 : 
 5212 tgl                       989 GIC         321 :         Assert(windef->refname == NULL &&
 5212 tgl                       990 ECB             :                windef->partitionClause == NIL &&
                                991                 :                windef->orderClause == NIL &&
                                992                 :                windef->frameOptions == FRAMEOPTION_DEFAULTS);
                                993                 : 
 5215 tgl                       994 GIC         324 :         foreach(lc, pstate->p_windowdefs)
 5215 tgl                       995 ECB             :         {
 5050 bruce                     996 GIC         324 :             WindowDef  *refwin = (WindowDef *) lfirst(lc);
 5215 tgl                       997 ECB             : 
 5215 tgl                       998 GIC         324 :             winref++;
 5212 tgl                       999 CBC         324 :             if (refwin->name && strcmp(refwin->name, windef->name) == 0)
 5215 tgl                      1000 ECB             :             {
 5215 tgl                      1001 GIC         321 :                 wfunc->winref = winref;
 5215 tgl                      1002 CBC         321 :                 break;
 5215 tgl                      1003 ECB             :             }
                               1004                 :         }
 5215 tgl                      1005 GIC         321 :         if (lc == NULL)         /* didn't find it? */
 5215 tgl                      1006 LBC           0 :             ereport(ERROR,
 5215 tgl                      1007 EUB             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                               1008                 :                      errmsg("window \"%s\" does not exist", windef->name),
                               1009                 :                      parser_errposition(pstate, windef->location)));
                               1010                 :     }
                               1011                 :     else
                               1012                 :     {
 5215 tgl                      1013 GIC        1380 :         Index       winref = 0;
 5215 tgl                      1014 ECB             :         ListCell   *lc;
                               1015                 : 
 5215 tgl                      1016 GIC        1539 :         foreach(lc, pstate->p_windowdefs)
 5215 tgl                      1017 ECB             :         {
 5050 bruce                    1018 GIC         261 :             WindowDef  *refwin = (WindowDef *) lfirst(lc);
 5215 tgl                      1019 ECB             : 
 5215 tgl                      1020 GIC         261 :             winref++;
 5215 tgl                      1021 CBC         261 :             if (refwin->refname && windef->refname &&
 5212 tgl                      1022 LBC           0 :                 strcmp(refwin->refname, windef->refname) == 0)
 5050 bruce                    1023 EUB             :                  /* matched on refname */ ;
 5215 tgl                      1024 GIC         261 :             else if (!refwin->refname && !windef->refname)
 5050 bruce                    1025 ECB             :                  /* matched, no refname */ ;
                               1026                 :             else
 5215 tgl                      1027 GIC          12 :                 continue;
                               1028                 : 
                               1029                 :             /*
                               1030                 :              * Also see similar de-duplication code in optimize_window_clauses
                               1031                 :              */
 5215 tgl                      1032 CBC         453 :             if (equal(refwin->partitionClause, windef->partitionClause) &&
 5212 tgl                      1033 GIC         204 :                 equal(refwin->orderClause, windef->orderClause) &&
 4804                          1034             276 :                 refwin->frameOptions == windef->frameOptions &&
                               1035             204 :                 equal(refwin->startOffset, windef->startOffset) &&
                               1036             102 :                 equal(refwin->endOffset, windef->endOffset))
 5215 tgl                      1037 ECB             :             {
                               1038                 :                 /* found a duplicate window specification */
 5215 tgl                      1039 CBC         102 :                 wfunc->winref = winref;
                               1040             102 :                 break;
 5215 tgl                      1041 ECB             :             }
                               1042                 :         }
 5215 tgl                      1043 GIC        1380 :         if (lc == NULL)         /* didn't find it? */
 5215 tgl                      1044 ECB             :         {
 5215 tgl                      1045 CBC        1278 :             pstate->p_windowdefs = lappend(pstate->p_windowdefs, windef);
 5215 tgl                      1046 GIC        1278 :             wfunc->winref = list_length(pstate->p_windowdefs);
                               1047                 :         }
 5215 tgl                      1048 ECB             :     }
                               1049                 : 
 5215 tgl                      1050 CBC        1701 :     pstate->p_hasWindowFuncs = true;
                               1051            1701 : }
                               1052                 : 
                               1053                 : /*
                               1054                 :  * parseCheckAggregates
 7247 tgl                      1055 ECB             :  *  Check for aggregates where they shouldn't be and improper grouping.
 3894                          1056                 :  *  This function should be called after the target list and qualifications
                               1057                 :  *  are finalized.
                               1058                 :  *
                               1059                 :  *  Misplaced aggregates are now mostly detected in transformAggregateCall,
                               1060                 :  *  but it seems more robust to check for aggregates in recursive queries
                               1061                 :  *  only after everything is finalized.  In any case it's hard to detect
                               1062                 :  *  improper grouping on-the-fly, so we have to make another pass over the
                               1063                 :  *  query for that.
                               1064                 :  */
                               1065                 : void
 7247 tgl                      1066 GIC       18322 : parseCheckAggregates(ParseState *pstate, Query *qry)
                               1067                 : {
 2878 bruce                    1068           18322 :     List       *gset_common = NIL;
 7247 tgl                      1069           18322 :     List       *groupClauses = NIL;
 2885 andres                   1070           18322 :     List       *groupClauseCommonVars = NIL;
 7011 tgl                      1071 ECB             :     bool        have_non_var_grouping;
 4628 tgl                      1072 GIC       18322 :     List       *func_grouped_rels = NIL;
 6892 neilc                    1073 ECB             :     ListCell   *l;
 7247 tgl                      1074                 :     bool        hasJoinRTEs;
 5300                          1075                 :     bool        hasSelfRefRTEs;
                               1076                 :     Node       *clause;
 7247                          1077                 : 
                               1078                 :     /* This should only be called if we found aggregates or grouping */
 2885 andres                   1079 GIC       18322 :     Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual || qry->groupingSets);
                               1080                 : 
                               1081                 :     /*
                               1082                 :      * If we have grouping sets, expand them and find the intersection of all
                               1083                 :      * sets.
 2885 andres                   1084 ECB             :      */
 2885 andres                   1085 GIC       18322 :     if (qry->groupingSets)
                               1086                 :     {
                               1087                 :         /*
                               1088                 :          * The limit of 4096 is arbitrary and exists simply to avoid resource
                               1089                 :          * issues from pathological constructs.
 2885 andres                   1090 ECB             :          */
  752 tomas.vondra             1091 GIC         382 :         List       *gsets = expand_grouping_sets(qry->groupingSets, qry->groupDistinct, 4096);
                               1092                 : 
 2885 andres                   1093             382 :         if (!gsets)
 2885 andres                   1094 UIC           0 :             ereport(ERROR,
                               1095                 :                     (errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
 2701 peter_e                  1096 ECB             :                      errmsg("too many grouping sets present (maximum 4096)"),
                               1097                 :                      parser_errposition(pstate,
 2885 andres                   1098                 :                                         qry->groupClause
 2118 tgl                      1099 EUB             :                                         ? exprLocation((Node *) qry->groupClause)
                               1100                 :                                         : exprLocation((Node *) qry->groupingSets))));
                               1101                 : 
                               1102                 :         /*
                               1103                 :          * The intersection will often be empty, so help things along by
                               1104                 :          * seeding the intersect with the smallest set.
                               1105                 :          */
 2885 andres                   1106 GIC         382 :         gset_common = linitial(gsets);
                               1107                 : 
                               1108             382 :         if (gset_common)
                               1109                 :         {
  923 tgl                      1110             218 :             for_each_from(l, gsets, 1)
 2885 andres                   1111 ECB             :             {
 2885 andres                   1112 GIC         149 :                 gset_common = list_intersection_int(gset_common, lfirst(l));
 2885 andres                   1113 CBC         149 :                 if (!gset_common)
 2885 andres                   1114 GIC          68 :                     break;
 2885 andres                   1115 ECB             :             }
                               1116                 :         }
                               1117                 : 
                               1118                 :         /*
                               1119                 :          * If there was only one grouping set in the expansion, AND if the
                               1120                 :          * groupClause is non-empty (meaning that the grouping set is not
                               1121                 :          * empty either), then we can ditch the grouping set and pretend we
                               1122                 :          * just had a normal GROUP BY.
                               1123                 :          */
 2885 andres                   1124 GIC         382 :         if (list_length(gsets) == 1 && qry->groupClause)
                               1125              12 :             qry->groupingSets = NIL;
                               1126                 :     }
                               1127                 : 
                               1128                 :     /*
 5300 tgl                      1129 ECB             :      * Scan the range table to see if there are JOIN or self-reference CTE
                               1130                 :      * entries.  We'll need this info below.
                               1131                 :      */
 5300 tgl                      1132 GIC       18322 :     hasJoinRTEs = hasSelfRefRTEs = false;
                               1133           42202 :     foreach(l, pstate->p_rtable)
                               1134                 :     {
                               1135           23880 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
                               1136                 : 
 5300 tgl                      1137 CBC       23880 :         if (rte->rtekind == RTE_JOIN)
                               1138            2094 :             hasJoinRTEs = true;
 5300 tgl                      1139 GIC       21786 :         else if (rte->rtekind == RTE_CTE && rte->self_reference)
 5300 tgl                      1140 CBC           6 :             hasSelfRefRTEs = true;
                               1141                 :     }
 5300 tgl                      1142 ECB             : 
 7247                          1143                 :     /*
 3894                          1144                 :      * Build a list of the acceptable GROUP BY expressions for use by
                               1145                 :      * check_ungrouped_columns().
                               1146                 :      *
                               1147                 :      * We get the TLE, not just the expr, because GROUPING wants to know the
                               1148                 :      * sortgroupref.
                               1149                 :      */
 6892 neilc                    1150 GIC       23290 :     foreach(l, qry->groupClause)
                               1151                 :     {
 5363 tgl                      1152            4968 :         SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
                               1153                 :         TargetEntry *expr;
                               1154                 : 
 2885 andres                   1155 CBC        4968 :         expr = get_sortgroupclause_tle(grpcl, qry->targetList);
 7247 tgl                      1156 GIC        4968 :         if (expr == NULL)
 7247 tgl                      1157 LBC           0 :             continue;           /* probably cannot happen */
                               1158                 : 
 1362 tgl                      1159 GIC        4968 :         groupClauses = lappend(groupClauses, expr);
 7247 tgl                      1160 ECB             :     }
                               1161                 : 
 7247 tgl                      1162 EUB             :     /*
                               1163                 :      * If there are join alias vars involved, we have to flatten them to the
 6385 bruce                    1164 ECB             :      * underlying vars, so that aliased and unaliased vars will be correctly
                               1165                 :      * taken as equal.  We can skip the expense of doing this if no rangetable
                               1166                 :      * entries are RTE_JOIN kind.
                               1167                 :      */
 7247 tgl                      1168 GIC       18322 :     if (hasJoinRTEs)
   69 tgl                      1169 GNC        1431 :         groupClauses = (List *) flatten_join_alias_vars(NULL, qry,
                               1170                 :                                                         (Node *) groupClauses);
                               1171                 : 
                               1172                 :     /*
 6385 bruce                    1173 ECB             :      * Detect whether any of the grouping expressions aren't simple Vars; if
                               1174                 :      * they're all Vars then we don't have to work so hard in the recursive
                               1175                 :      * scans.  (Note we have to flatten aliases before this.)
                               1176                 :      *
                               1177                 :      * Track Vars that are included in all grouping sets separately in
                               1178                 :      * groupClauseCommonVars, since these are the only ones we can use to
                               1179                 :      * check for functional dependencies.
                               1180                 :      */
 7011 tgl                      1181 GIC       18322 :     have_non_var_grouping = false;
 6892 neilc                    1182           23290 :     foreach(l, groupClauses)
                               1183                 :     {
 2885 andres                   1184            4968 :         TargetEntry *tle = lfirst(l);
                               1185                 : 
 2885 andres                   1186 CBC        4968 :         if (!IsA(tle->expr, Var))
 7011 tgl                      1187 ECB             :         {
 7011 tgl                      1188 GIC         615 :             have_non_var_grouping = true;
 2885 andres                   1189 ECB             :         }
 2885 andres                   1190 GIC        5067 :         else if (!qry->groupingSets ||
 2885 andres                   1191 CBC         714 :                  list_member_int(gset_common, tle->ressortgroupref))
                               1192                 :         {
                               1193            3696 :             groupClauseCommonVars = lappend(groupClauseCommonVars, tle->expr);
                               1194                 :         }
 7011 tgl                      1195 ECB             :     }
                               1196                 : 
                               1197                 :     /*
 7247                          1198                 :      * Check the targetlist and HAVING clause for ungrouped variables.
                               1199                 :      *
                               1200                 :      * Note: because we check resjunk tlist elements as well as regular ones,
                               1201                 :      * this will also find ungrouped variables that came from ORDER BY and
                               1202                 :      * WINDOW clauses.  For that matter, it's also going to examine the
                               1203                 :      * grouping expressions themselves --- but they'll all pass the test ...
                               1204                 :      *
                               1205                 :      * We also finalize GROUPING expressions, but for that we need to traverse
                               1206                 :      * the original (unflattened) clause in order to modify nodes.
                               1207                 :      */
 7247 tgl                      1208 GIC       18322 :     clause = (Node *) qry->targetList;
 2885 andres                   1209           18322 :     finalize_grouping_exprs(clause, pstate, qry,
                               1210                 :                             groupClauses, hasJoinRTEs,
                               1211                 :                             have_non_var_grouping);
 7247 tgl                      1212           18319 :     if (hasJoinRTEs)
   69 tgl                      1213 GNC        1431 :         clause = flatten_join_alias_vars(NULL, qry, clause);
 4628 tgl                      1214 CBC       18319 :     check_ungrouped_columns(clause, pstate, qry,
                               1215                 :                             groupClauses, groupClauseCommonVars,
                               1216                 :                             have_non_var_grouping,
 4628 tgl                      1217 ECB             :                             &func_grouped_rels);
 7247                          1218                 : 
 7247 tgl                      1219 CBC       18277 :     clause = (Node *) qry->havingQual;
 2885 andres                   1220 GIC       18277 :     finalize_grouping_exprs(clause, pstate, qry,
                               1221                 :                             groupClauses, hasJoinRTEs,
                               1222                 :                             have_non_var_grouping);
 7247 tgl                      1223           18277 :     if (hasJoinRTEs)
   69 tgl                      1224 GNC        1419 :         clause = flatten_join_alias_vars(NULL, qry, clause);
 4628 tgl                      1225 CBC       18277 :     check_ungrouped_columns(clause, pstate, qry,
                               1226                 :                             groupClauses, groupClauseCommonVars,
                               1227                 :                             have_non_var_grouping,
 4628 tgl                      1228 ECB             :                             &func_grouped_rels);
 5300                          1229                 : 
                               1230                 :     /*
                               1231                 :      * Per spec, aggregates can't appear in a recursive term.
                               1232                 :      */
 5300 tgl                      1233 GIC       18274 :     if (pstate->p_hasAggs && hasSelfRefRTEs)
                               1234               6 :         ereport(ERROR,
                               1235                 :                 (errcode(ERRCODE_INVALID_RECURSION),
                               1236                 :                  errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
                               1237                 :                  parser_errposition(pstate,
 5300 tgl                      1238 ECB             :                                     locate_agg_of_level((Node *) qry, 0))));
 7247 tgl                      1239 CBC       18268 : }
                               1240                 : 
                               1241                 : /*
                               1242                 :  * check_ungrouped_columns -
                               1243                 :  *    Scan the given expression tree for ungrouped variables (variables
 8522 tgl                      1244 ECB             :  *    that are not listed in the groupClauses list and are not within
                               1245                 :  *    the arguments of aggregate functions).  Emit a suitable error message
                               1246                 :  *    if any are found.
                               1247                 :  *
                               1248                 :  * NOTE: we assume that the given clause has been transformed suitably for
                               1249                 :  * parser output.  This means we can use expression_tree_walker.
                               1250                 :  *
                               1251                 :  * NOTE: we recognize grouping expressions in the main query, but only
                               1252                 :  * grouping Vars in subqueries.  For example, this will be rejected,
                               1253                 :  * although it could be allowed:
                               1254                 :  *      SELECT
                               1255                 :  *          (SELECT x FROM bar where y = (foo.a + foo.b))
                               1256                 :  *      FROM foo
                               1257                 :  *      GROUP BY a + b;
                               1258                 :  * The difficulty is the need to account for different sublevels_up.
                               1259                 :  * This appears to require a whole custom version of equal(), which is
                               1260                 :  * way more pain than the feature seems worth.
                               1261                 :  */
                               1262                 : static void
 4628 tgl                      1263 GIC       36596 : check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
                               1264                 :                         List *groupClauses, List *groupClauseCommonVars,
                               1265                 :                         bool have_non_var_grouping,
                               1266                 :                         List **func_grouped_rels)
                               1267                 : {
 8397 bruce                    1268 ECB             :     check_ungrouped_columns_context context;
                               1269                 : 
 8522 tgl                      1270 GIC       36596 :     context.pstate = pstate;
 4628                          1271           36596 :     context.qry = qry;
 1531                          1272           36596 :     context.hasJoinRTEs = false;    /* assume caller flattened join Vars */
 8522                          1273           36596 :     context.groupClauses = groupClauses;
 2885 andres                   1274           36596 :     context.groupClauseCommonVars = groupClauseCommonVars;
 7387 tgl                      1275 CBC       36596 :     context.have_non_var_grouping = have_non_var_grouping;
 4628                          1276           36596 :     context.func_grouped_rels = func_grouped_rels;
 7387                          1277           36596 :     context.sublevels_up = 0;
 3394                          1278           36596 :     context.in_agg_direct_args = false;
 8522                          1279           36596 :     check_ungrouped_columns_walker(node, &context);
 9266 bruce                    1280           36551 : }
 9266 bruce                    1281 ECB             : 
 9265                          1282                 : static bool
 8522 tgl                      1283 CBC      152878 : check_ungrouped_columns_walker(Node *node,
 8522 tgl                      1284 ECB             :                                check_ungrouped_columns_context *context)
 9266 bruce                    1285                 : {
                               1286                 :     ListCell   *gl;
                               1287                 : 
 8695 tgl                      1288 CBC      152878 :     if (node == NULL)
 8695 tgl                      1289 GIC       42934 :         return false;
 7387                          1290          109944 :     if (IsA(node, Const) ||
                               1291          108144 :         IsA(node, Param))
 8695                          1292            1838 :         return false;           /* constants are always acceptable */
 8397 bruce                    1293 ECB             : 
 3394 tgl                      1294 CBC      108106 :     if (IsA(node, Aggref))
 3394 tgl                      1295 ECB             :     {
 3394 tgl                      1296 CBC       22880 :         Aggref     *agg = (Aggref *) node;
 3394 tgl                      1297 ECB             : 
 3394 tgl                      1298 GIC       22880 :         if ((int) agg->agglevelsup == context->sublevels_up)
 3394 tgl                      1299 ECB             :         {
                               1300                 :             /*
                               1301                 :              * If we find an aggregate call of the original level, do not
                               1302                 :              * recurse into its normal arguments, ORDER BY arguments, or
                               1303                 :              * filter; ungrouped vars there are not an error.  But we should
                               1304                 :              * check direct arguments as though they weren't in an aggregate.
                               1305                 :              * We set a special flag in the context to help produce a useful
                               1306                 :              * error message for ungrouped vars in direct arguments.
                               1307                 :              */
                               1308                 :             bool        result;
                               1309                 : 
 3394 tgl                      1310 GIC       22877 :             Assert(!context->in_agg_direct_args);
                               1311           22877 :             context->in_agg_direct_args = true;
                               1312           22877 :             result = check_ungrouped_columns_walker((Node *) agg->aggdirectargs,
                               1313                 :                                                     context);
                               1314           22874 :             context->in_agg_direct_args = false;
 3394 tgl                      1315 CBC       22874 :             return result;
 3394 tgl                      1316 ECB             :         }
                               1317                 : 
                               1318                 :         /*
                               1319                 :          * We can skip recursing into aggregates of higher levels altogether,
                               1320                 :          * since they could not possibly contain Vars of concern to us (see
                               1321                 :          * transformAggregateCall).  We do need to look at aggregates of lower
                               1322                 :          * levels, however.
                               1323                 :          */
 3394 tgl                      1324 GIC           3 :         if ((int) agg->agglevelsup > context->sublevels_up)
 3394 tgl                      1325 UIC           0 :             return false;
                               1326                 :     }
                               1327                 : 
 2885 andres                   1328 GIC       85229 :     if (IsA(node, GroupingFunc))
 2885 andres                   1329 ECB             :     {
 2885 andres                   1330 GBC         174 :         GroupingFunc *grp = (GroupingFunc *) node;
                               1331                 : 
                               1332                 :         /* handled GroupingFunc separately, no need to recheck at this level */
 2885 andres                   1333 ECB             : 
 2885 andres                   1334 GIC         174 :         if ((int) grp->agglevelsup >= context->sublevels_up)
 2885 andres                   1335 CBC         158 :             return false;
                               1336                 :     }
                               1337                 : 
                               1338                 :     /*
 6385 bruce                    1339 ECB             :      * If we have any GROUP BY items that are not simple Vars, check to see if
                               1340                 :      * subexpression as a whole matches any GROUP BY item. We need to do this
                               1341                 :      * at every recursion level so that we recognize GROUPed-BY expressions
                               1342                 :      * before reaching variables within them. But this only works at the outer
                               1343                 :      * query level, as noted above.
                               1344                 :      */
 7387 tgl                      1345 GIC       85071 :     if (context->have_non_var_grouping && context->sublevels_up == 0)
                               1346                 :     {
                               1347            8580 :         foreach(gl, context->groupClauses)
                               1348                 :         {
 2885 andres                   1349            6482 :             TargetEntry *tle = lfirst(gl);
 2885 andres                   1350 ECB             : 
 2885 andres                   1351 GIC        6482 :             if (equal(node, tle->expr))
 7387 tgl                      1352 CBC         933 :                 return false;   /* acceptable, do not descend more */
                               1353                 :         }
 9266 bruce                    1354 ECB             :     }
                               1355                 : 
 8522 tgl                      1356                 :     /*
 7387                          1357                 :      * If we have an ungrouped Var of the original query level, we have a
                               1358                 :      * failure.  Vars below the original query level are not a problem, and
                               1359                 :      * neither are Vars from above it.  (If such Vars are ungrouped as far as
                               1360                 :      * their own query level is concerned, that's someone else's problem...)
                               1361                 :      */
 8695 tgl                      1362 GIC       84138 :     if (IsA(node, Var))
                               1363                 :     {
 8397 bruce                    1364           13268 :         Var        *var = (Var *) node;
                               1365                 :         RangeTblEntry *rte;
                               1366                 :         char       *attname;
 8522 tgl                      1367 ECB             : 
 7387 tgl                      1368 GIC       13268 :         if (var->varlevelsup != context->sublevels_up)
 7387 tgl                      1369 CBC         168 :             return false;       /* it's not local to my query, ignore */
                               1370                 : 
                               1371                 :         /*
                               1372                 :          * Check for a match, if we didn't do it above.
 7387 tgl                      1373 ECB             :          */
 7387 tgl                      1374 CBC       13100 :         if (!context->have_non_var_grouping || context->sublevels_up != 0)
                               1375                 :         {
 7387 tgl                      1376 GIC       16309 :             foreach(gl, context->groupClauses)
                               1377                 :             {
 2885 andres                   1378           16142 :                 Var        *gvar = (Var *) ((TargetEntry *) lfirst(gl))->expr;
 7387 tgl                      1379 ECB             : 
 7387 tgl                      1380 GIC       16142 :                 if (IsA(gvar, Var) &&
 7387 tgl                      1381 CBC       16142 :                     gvar->varno == var->varno &&
 7387 tgl                      1382 GIC       14771 :                     gvar->varattno == var->varattno &&
 7387 tgl                      1383 CBC       12930 :                     gvar->varlevelsup == 0)
 2118 tgl                      1384 GIC       12930 :                     return false;   /* acceptable, we're okay */
 7387 tgl                      1385 ECB             :             }
                               1386                 :         }
                               1387                 : 
 4628                          1388                 :         /*
                               1389                 :          * Check whether the Var is known functionally dependent on the GROUP
                               1390                 :          * BY columns.  If so, we can allow the Var to be used, because the
                               1391                 :          * grouping is really a no-op for this table.  However, this deduction
                               1392                 :          * depends on one or more constraints of the table, so we have to add
                               1393                 :          * those constraints to the query's constraintDeps list, because it's
                               1394                 :          * not semantically valid anymore if the constraint(s) get dropped.
                               1395                 :          * (Therefore, this check must be the last-ditch effort before raising
                               1396                 :          * error: we don't want to add dependencies unnecessarily.)
                               1397                 :          *
                               1398                 :          * Because this is a pretty expensive check, and will have the same
                               1399                 :          * outcome for all columns of a table, we remember which RTEs we've
                               1400                 :          * already proven functional dependency for in the func_grouped_rels
                               1401                 :          * list.  This test also prevents us from adding duplicate entries to
                               1402                 :          * the constraintDeps list.
                               1403                 :          */
 4628 tgl                      1404 GIC         170 :         if (list_member_int(*context->func_grouped_rels, var->varno))
 4382 bruce                    1405              69 :             return false;       /* previously proven acceptable */
                               1406                 : 
 8522 tgl                      1407             101 :         Assert(var->varno > 0 &&
                               1408                 :                (int) var->varno <= list_length(context->pstate->p_rtable));
 8522 tgl                      1409 CBC         101 :         rte = rt_fetch(var->varno, context->pstate->p_rtable);
 4628                          1410             101 :         if (rte->rtekind == RTE_RELATION)
                               1411                 :         {
                               1412              98 :             if (check_functional_grouping(rte->relid,
 4628 tgl                      1413 GIC          98 :                                           var->varno,
 4628 tgl                      1414 ECB             :                                           0,
 2885 andres                   1415                 :                                           context->groupClauseCommonVars,
 4628 tgl                      1416 GIC          98 :                                           &context->qry->constraintDeps))
 4628 tgl                      1417 ECB             :             {
 4628 tgl                      1418 CBC         112 :                 *context->func_grouped_rels =
 4628 tgl                      1419 GIC          56 :                     lappend_int(*context->func_grouped_rels, var->varno);
 4382 bruce                    1420              56 :                 return false;   /* acceptable */
 4628 tgl                      1421 ECB             :             }
                               1422                 :         }
                               1423                 : 
                               1424                 :         /* Found an ungrouped local variable; generate error message */
 8231 tgl                      1425 CBC          45 :         attname = get_rte_attribute_name(rte, var->varattno);
 7387 tgl                      1426 GIC          45 :         if (context->sublevels_up == 0)
 7204                          1427              45 :             ereport(ERROR,
                               1428                 :                     (errcode(ERRCODE_GROUPING_ERROR),
                               1429                 :                      errmsg("column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
 5333 tgl                      1430 ECB             :                             rte->eref->aliasname, attname),
 3394                          1431                 :                      context->in_agg_direct_args ?
                               1432                 :                      errdetail("Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
                               1433                 :                      parser_errposition(context->pstate, var->location)));
                               1434                 :         else
 7204 tgl                      1435 UIC           0 :             ereport(ERROR,
                               1436                 :                     (errcode(ERRCODE_GROUPING_ERROR),
                               1437                 :                      errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
                               1438                 :                             rte->eref->aliasname, attname),
                               1439                 :                      parser_errposition(context->pstate, var->location)));
 7387 tgl                      1440 EUB             :     }
                               1441                 : 
 7387 tgl                      1442 GIC       70870 :     if (IsA(node, Query))
                               1443                 :     {
                               1444                 :         /* Recurse into subselects */
                               1445                 :         bool        result;
                               1446                 : 
 7387 tgl                      1447 CBC         155 :         context->sublevels_up++;
 7387 tgl                      1448 GIC         155 :         result = query_tree_walker((Query *) node,
                               1449                 :                                    check_ungrouped_columns_walker,
                               1450                 :                                    (void *) context,
                               1451                 :                                    0);
 7387 tgl                      1452 CBC         155 :         context->sublevels_up--;
                               1453             155 :         return result;
                               1454                 :     }
 8522 tgl                      1455 GIC       70715 :     return expression_tree_walker(node, check_ungrouped_columns_walker,
                               1456                 :                                   (void *) context);
 9266 bruce                    1457 ECB             : }
 7222 tgl                      1458                 : 
                               1459                 : /*
 2885 andres                   1460                 :  * finalize_grouping_exprs -
                               1461                 :  *    Scan the given expression tree for GROUPING() and related calls,
                               1462                 :  *    and validate and process their arguments.
                               1463                 :  *
                               1464                 :  * This is split out from check_ungrouped_columns above because it needs
                               1465                 :  * to modify the nodes (which it does in-place, not via a mutator) while
                               1466                 :  * check_ungrouped_columns may see only a copy of the original thanks to
                               1467                 :  * flattening of join alias vars. So here, we flatten each individual
                               1468                 :  * GROUPING argument as we see it before comparing it.
                               1469                 :  */
                               1470                 : static void
 2885 andres                   1471 GIC       36599 : finalize_grouping_exprs(Node *node, ParseState *pstate, Query *qry,
                               1472                 :                         List *groupClauses, bool hasJoinRTEs,
                               1473                 :                         bool have_non_var_grouping)
                               1474                 : {
                               1475                 :     check_ungrouped_columns_context context;
 2885 andres                   1476 ECB             : 
 2885 andres                   1477 GIC       36599 :     context.pstate = pstate;
                               1478           36599 :     context.qry = qry;
 1531 tgl                      1479           36599 :     context.hasJoinRTEs = hasJoinRTEs;
 2885 andres                   1480           36599 :     context.groupClauses = groupClauses;
                               1481           36599 :     context.groupClauseCommonVars = NIL;
 2885 andres                   1482 CBC       36599 :     context.have_non_var_grouping = have_non_var_grouping;
                               1483           36599 :     context.func_grouped_rels = NULL;
                               1484           36599 :     context.sublevels_up = 0;
                               1485           36599 :     context.in_agg_direct_args = false;
                               1486           36599 :     finalize_grouping_exprs_walker(node, &context);
                               1487           36596 : }
 2885 andres                   1488 ECB             : 
                               1489                 : static bool
 2885 andres                   1490 CBC      154706 : finalize_grouping_exprs_walker(Node *node,
 2885 andres                   1491 ECB             :                                check_ungrouped_columns_context *context)
                               1492                 : {
                               1493                 :     ListCell   *gl;
                               1494                 : 
 2885 andres                   1495 CBC      154706 :     if (node == NULL)
 2885 andres                   1496 GIC       42977 :         return false;
                               1497          111729 :     if (IsA(node, Const) ||
                               1498          109340 :         IsA(node, Param))
                               1499            2433 :         return false;           /* constants are always acceptable */
 2885 andres                   1500 ECB             : 
 2885 andres                   1501 CBC      109296 :     if (IsA(node, Aggref))
 2885 andres                   1502 ECB             :     {
 2885 andres                   1503 CBC       22883 :         Aggref     *agg = (Aggref *) node;
 2885 andres                   1504 ECB             : 
 2885 andres                   1505 GIC       22883 :         if ((int) agg->agglevelsup == context->sublevels_up)
 2885 andres                   1506 ECB             :         {
                               1507                 :             /*
                               1508                 :              * If we find an aggregate call of the original level, do not
                               1509                 :              * recurse into its normal arguments, ORDER BY arguments, or
                               1510                 :              * filter; GROUPING exprs of this level are not allowed there. But
                               1511                 :              * check direct arguments as though they weren't in an aggregate.
                               1512                 :              */
                               1513                 :             bool        result;
                               1514                 : 
 2885 andres                   1515 GIC       22880 :             Assert(!context->in_agg_direct_args);
                               1516           22880 :             context->in_agg_direct_args = true;
                               1517           22880 :             result = finalize_grouping_exprs_walker((Node *) agg->aggdirectargs,
                               1518                 :                                                     context);
                               1519           22880 :             context->in_agg_direct_args = false;
 2885 andres                   1520 CBC       22880 :             return result;
 2885 andres                   1521 ECB             :         }
                               1522                 : 
                               1523                 :         /*
                               1524                 :          * We can skip recursing into aggregates of higher levels altogether,
                               1525                 :          * since they could not possibly contain exprs of concern to us (see
                               1526                 :          * transformAggregateCall).  We do need to look at aggregates of lower
                               1527                 :          * levels, however.
                               1528                 :          */
 2885 andres                   1529 GIC           3 :         if ((int) agg->agglevelsup > context->sublevels_up)
 2885 andres                   1530 UIC           0 :             return false;
                               1531                 :     }
                               1532                 : 
 2885 andres                   1533 GIC       86416 :     if (IsA(node, GroupingFunc))
 2885 andres                   1534 ECB             :     {
 2885 andres                   1535 GBC         177 :         GroupingFunc *grp = (GroupingFunc *) node;
                               1536                 : 
                               1537                 :         /*
 2878 bruce                    1538 ECB             :          * We only need to check GroupingFunc nodes at the exact level to
                               1539                 :          * which they belong, since they cannot mix levels in arguments.
 2885 andres                   1540                 :          */
                               1541                 : 
 2885 andres                   1542 GIC         177 :         if ((int) grp->agglevelsup == context->sublevels_up)
                               1543                 :         {
                               1544                 :             ListCell   *lc;
 2878 bruce                    1545             157 :             List       *ref_list = NIL;
                               1546                 : 
 2885 andres                   1547 CBC         406 :             foreach(lc, grp->args)
                               1548                 :             {
 2878 bruce                    1549 GIC         252 :                 Node       *expr = lfirst(lc);
 2878 bruce                    1550 CBC         252 :                 Index       ref = 0;
                               1551                 : 
 1531 tgl                      1552             252 :                 if (context->hasJoinRTEs)
   69 tgl                      1553 GNC          24 :                     expr = flatten_join_alias_vars(NULL, context->qry, expr);
 2885 andres                   1554 ECB             : 
                               1555                 :                 /*
                               1556                 :                  * Each expression must match a grouping entry at the current
                               1557                 :                  * query level. Unlike the general expression case, we don't
                               1558                 :                  * allow functional dependencies or outer references.
                               1559                 :                  */
                               1560                 : 
 2885 andres                   1561 GIC         252 :                 if (IsA(expr, Var))
                               1562                 :                 {
 2878 bruce                    1563             246 :                     Var        *var = (Var *) expr;
                               1564                 : 
 2885 andres                   1565             246 :                     if (var->varlevelsup == context->sublevels_up)
 2885 andres                   1566 ECB             :                     {
 2885 andres                   1567 GIC         362 :                         foreach(gl, context->groupClauses)
 2885 andres                   1568 ECB             :                         {
 2885 andres                   1569 GIC         359 :                             TargetEntry *tle = lfirst(gl);
 2885 andres                   1570 CBC         359 :                             Var        *gvar = (Var *) tle->expr;
                               1571                 : 
                               1572             359 :                             if (IsA(gvar, Var) &&
 2885 andres                   1573 GIC         359 :                                 gvar->varno == var->varno &&
 2885 andres                   1574 CBC         353 :                                 gvar->varattno == var->varattno &&
                               1575             243 :                                 gvar->varlevelsup == 0)
                               1576                 :                             {
                               1577             243 :                                 ref = tle->ressortgroupref;
                               1578             243 :                                 break;
 2885 andres                   1579 ECB             :                             }
                               1580                 :                         }
                               1581                 :                     }
                               1582                 :                 }
 2885 andres                   1583 CBC           6 :                 else if (context->have_non_var_grouping &&
 2885 andres                   1584 GIC           6 :                          context->sublevels_up == 0)
                               1585                 :                 {
                               1586              12 :                     foreach(gl, context->groupClauses)
                               1587                 :                     {
 2885 andres                   1588 CBC          12 :                         TargetEntry *tle = lfirst(gl);
 2885 andres                   1589 ECB             : 
 2885 andres                   1590 GIC          12 :                         if (equal(expr, tle->expr))
 2885 andres                   1591 ECB             :                         {
 2885 andres                   1592 GIC           6 :                             ref = tle->ressortgroupref;
 2885 andres                   1593 CBC           6 :                             break;
                               1594                 :                         }
 2885 andres                   1595 ECB             :                     }
                               1596                 :                 }
                               1597                 : 
 2885 andres                   1598 CBC         252 :                 if (ref == 0)
 2885 andres                   1599 GIC           3 :                     ereport(ERROR,
                               1600                 :                             (errcode(ERRCODE_GROUPING_ERROR),
                               1601                 :                              errmsg("arguments to GROUPING must be grouping expressions of the associated query level"),
                               1602                 :                              parser_errposition(context->pstate,
 2885 andres                   1603 ECB             :                                                 exprLocation(expr))));
                               1604                 : 
 2885 andres                   1605 GIC         249 :                 ref_list = lappend_int(ref_list, ref);
                               1606                 :             }
                               1607                 : 
                               1608             154 :             grp->refs = ref_list;
                               1609                 :         }
 2885 andres                   1610 ECB             : 
 2885 andres                   1611 GIC         174 :         if ((int) grp->agglevelsup > context->sublevels_up)
                               1612               4 :             return false;
 2885 andres                   1613 ECB             :     }
                               1614                 : 
 2885 andres                   1615 GIC       86409 :     if (IsA(node, Query))
 2885 andres                   1616 ECB             :     {
                               1617                 :         /* Recurse into subselects */
                               1618                 :         bool        result;
                               1619                 : 
 2885 andres                   1620 CBC         157 :         context->sublevels_up++;
 2885 andres                   1621 GIC         157 :         result = query_tree_walker((Query *) node,
                               1622                 :                                    finalize_grouping_exprs_walker,
                               1623                 :                                    (void *) context,
                               1624                 :                                    0);
 2885 andres                   1625 CBC         157 :         context->sublevels_up--;
                               1626             157 :         return result;
                               1627                 :     }
 2885 andres                   1628 GIC       86252 :     return expression_tree_walker(node, finalize_grouping_exprs_walker,
                               1629                 :                                   (void *) context);
 2885 andres                   1630 ECB             : }
                               1631                 : 
                               1632                 : 
                               1633                 : /*
                               1634                 :  * Given a GroupingSet node, expand it and return a list of lists.
                               1635                 :  *
                               1636                 :  * For EMPTY nodes, return a list of one empty list.
                               1637                 :  *
                               1638                 :  * For SIMPLE nodes, return a list of one list, which is the node content.
                               1639                 :  *
                               1640                 :  * For CUBE and ROLLUP nodes, return a list of the expansions.
                               1641                 :  *
                               1642                 :  * For SET nodes, recursively expand contained CUBE and ROLLUP.
                               1643                 :  */
                               1644                 : static List *
 2885 andres                   1645 GIC        1769 : expand_groupingset_node(GroupingSet *gs)
                               1646                 : {
 2878 bruce                    1647            1769 :     List       *result = NIL;
                               1648                 : 
 2885 andres                   1649            1769 :     switch (gs->kind)
 2885 andres                   1650 ECB             :     {
 2885 andres                   1651 GIC         216 :         case GROUPING_SET_EMPTY:
 2885 andres                   1652 CBC         216 :             result = list_make1(NIL);
 2885 andres                   1653 GIC         216 :             break;
 2885 andres                   1654 ECB             : 
 2885 andres                   1655 GIC         764 :         case GROUPING_SET_SIMPLE:
 2885 andres                   1656 CBC         764 :             result = list_make1(gs->content);
                               1657             764 :             break;
 2885 andres                   1658 ECB             : 
 2885 andres                   1659 GIC         241 :         case GROUPING_SET_ROLLUP:
 2885 andres                   1660 ECB             :             {
 2885 andres                   1661 CBC         241 :                 List       *rollup_val = gs->content;
 2885 andres                   1662 ECB             :                 ListCell   *lc;
 2885 andres                   1663 GIC         241 :                 int         curgroup_size = list_length(gs->content);
 2885 andres                   1664 ECB             : 
 2885 andres                   1665 GIC         647 :                 while (curgroup_size > 0)
 2885 andres                   1666 ECB             :                 {
 2878 bruce                    1667 GIC         406 :                     List       *current_result = NIL;
 2878 bruce                    1668 CBC         406 :                     int         i = curgroup_size;
                               1669                 : 
 2885 andres                   1670             571 :                     foreach(lc, rollup_val)
                               1671                 :                     {
                               1672             571 :                         GroupingSet *gs_current = (GroupingSet *) lfirst(lc);
 2885 andres                   1673 ECB             : 
 2885 andres                   1674 GIC         571 :                         Assert(gs_current->kind == GROUPING_SET_SIMPLE);
 2885 andres                   1675 ECB             : 
 1336 tgl                      1676 GIC         571 :                         current_result = list_concat(current_result,
 1336 tgl                      1677 CBC         571 :                                                      gs_current->content);
                               1678                 : 
 2885 andres                   1679 ECB             :                         /* If we are done with making the current group, break */
 2885 andres                   1680 GIC         571 :                         if (--i == 0)
 2885 andres                   1681 CBC         406 :                             break;
 2885 andres                   1682 ECB             :                     }
                               1683                 : 
 2885 andres                   1684 GIC         406 :                     result = lappend(result, current_result);
 2885 andres                   1685 CBC         406 :                     --curgroup_size;
 2885 andres                   1686 ECB             :                 }
                               1687                 : 
 2885 andres                   1688 GIC         241 :                 result = lappend(result, NIL);
 2885 andres                   1689 ECB             :             }
 2885 andres                   1690 CBC         241 :             break;
                               1691                 : 
 2885 andres                   1692 GIC         184 :         case GROUPING_SET_CUBE:
 2885 andres                   1693 ECB             :             {
 2878 bruce                    1694 GIC         184 :                 List       *cube_list = gs->content;
 2878 bruce                    1695 CBC         184 :                 int         number_bits = list_length(cube_list);
                               1696                 :                 uint32      num_sets;
 2878 bruce                    1697 ECB             :                 uint32      i;
                               1698                 : 
 2885 andres                   1699                 :                 /* parser should cap this much lower */
 2885 andres                   1700 CBC         184 :                 Assert(number_bits < 31);
                               1701                 : 
 2885 andres                   1702 GIC         184 :                 num_sets = (1U << number_bits);
                               1703                 : 
                               1704            1020 :                 for (i = 0; i < num_sets; i++)
 2885 andres                   1705 ECB             :                 {
 2878 bruce                    1706 GIC         836 :                     List       *current_result = NIL;
 2878 bruce                    1707 ECB             :                     ListCell   *lc;
 2878 bruce                    1708 GIC         836 :                     uint32      mask = 1U;
 2885 andres                   1709 ECB             : 
 2885 andres                   1710 GIC        2776 :                     foreach(lc, cube_list)
 2885 andres                   1711 ECB             :                     {
 2885 andres                   1712 GIC        1940 :                         GroupingSet *gs_current = (GroupingSet *) lfirst(lc);
 2885 andres                   1713 ECB             : 
 2885 andres                   1714 GIC        1940 :                         Assert(gs_current->kind == GROUPING_SET_SIMPLE);
 2885 andres                   1715 ECB             : 
 2885 andres                   1716 GIC        1940 :                         if (mask & i)
 1336 tgl                      1717 CBC         970 :                             current_result = list_concat(current_result,
 1336 tgl                      1718 GIC         970 :                                                          gs_current->content);
 2885 andres                   1719 ECB             : 
 2885 andres                   1720 GIC        1940 :                         mask <<= 1;
 2885 andres                   1721 ECB             :                     }
                               1722                 : 
 2885 andres                   1723 CBC         836 :                     result = lappend(result, current_result);
                               1724                 :                 }
 2885 andres                   1725 ECB             :             }
 2885 andres                   1726 GIC         184 :             break;
                               1727                 : 
 2885 andres                   1728 CBC         364 :         case GROUPING_SET_SETS:
                               1729                 :             {
                               1730                 :                 ListCell   *lc;
 2885 andres                   1731 ECB             : 
 2885 andres                   1732 GIC        1320 :                 foreach(lc, gs->content)
 2885 andres                   1733 ECB             :                 {
 2878 bruce                    1734 GIC         956 :                     List       *current_result = expand_groupingset_node(lfirst(lc));
                               1735                 : 
 2885 andres                   1736             956 :                     result = list_concat(result, current_result);
 2885 andres                   1737 ECB             :                 }
                               1738                 :             }
 2885 andres                   1739 CBC         364 :             break;
                               1740                 :     }
 2885 andres                   1741 ECB             : 
 2885 andres                   1742 GIC        1769 :     return result;
                               1743                 : }
 2885 andres                   1744 ECB             : 
                               1745                 : /* list_sort comparator to sort sub-lists by length */
                               1746                 : static int
 1363 tgl                      1747 CBC        2715 : cmp_list_len_asc(const ListCell *a, const ListCell *b)
                               1748                 : {
 1363 tgl                      1749 GIC        2715 :     int         la = list_length((const List *) lfirst(a));
                               1750            2715 :     int         lb = list_length((const List *) lfirst(b));
                               1751                 : 
 2885 andres                   1752 CBC        2715 :     return (la > lb) ? 1 : (la == lb) ? 0 : -1;
                               1753                 : }
 2885 andres                   1754 ECB             : 
  752 tomas.vondra             1755                 : /* list_sort comparator to sort sub-lists by length and contents */
                               1756                 : static int
  752 tomas.vondra             1757 CBC         160 : cmp_list_len_contents_asc(const ListCell *a, const ListCell *b)
                               1758                 : {
  697 tgl                      1759 GIC         160 :     int         res = cmp_list_len_asc(a, b);
                               1760                 : 
  752 tomas.vondra             1761             160 :     if (res == 0)
  752 tomas.vondra             1762 ECB             :     {
  697 tgl                      1763 GIC          48 :         List       *la = (List *) lfirst(a);
  697 tgl                      1764 CBC          48 :         List       *lb = (List *) lfirst(b);
                               1765                 :         ListCell   *lca;
  697 tgl                      1766 ECB             :         ListCell   *lcb;
                               1767                 : 
  752 tomas.vondra             1768 CBC         112 :         forboth(lca, la, lcb, lb)
  752 tomas.vondra             1769 ECB             :         {
  697 tgl                      1770 GIC          80 :             int         va = lfirst_int(lca);
                               1771              80 :             int         vb = lfirst_int(lcb);
                               1772                 : 
  752 tomas.vondra             1773 CBC          80 :             if (va > vb)
  752 tomas.vondra             1774 GIC          16 :                 return 1;
  752 tomas.vondra             1775 CBC          72 :             if (va < vb)
                               1776               8 :                 return -1;
                               1777                 :         }
  752 tomas.vondra             1778 ECB             :     }
                               1779                 : 
  752 tomas.vondra             1780 CBC         144 :     return res;
  752 tomas.vondra             1781 ECB             : }
                               1782                 : 
                               1783                 : /*
                               1784                 :  * Expand a groupingSets clause to a flat list of grouping sets.
 2885 andres                   1785                 :  * The returned list is sorted by length, shortest sets first.
                               1786                 :  *
                               1787                 :  * This is mainly for the planner, but we use it here too to do
                               1788                 :  * some consistency checks.
                               1789                 :  */
                               1790                 : List *
  752 tomas.vondra             1791 GIC         749 : expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
                               1792                 : {
 2885 andres                   1793             749 :     List       *expanded_groups = NIL;
 2878 bruce                    1794             749 :     List       *result = NIL;
 2885 andres                   1795             749 :     double      numsets = 1;
 2885 andres                   1796 ECB             :     ListCell   *lc;
                               1797                 : 
 2885 andres                   1798 CBC         749 :     if (groupingSets == NIL)
 2885 andres                   1799 LBC           0 :         return NIL;
 2885 andres                   1800 ECB             : 
 2885 andres                   1801 GIC        1562 :     foreach(lc, groupingSets)
                               1802                 :     {
 2878 bruce                    1803 CBC         813 :         List       *current_result = NIL;
 2885 andres                   1804 GBC         813 :         GroupingSet *gs = lfirst(lc);
                               1805                 : 
 2885 andres                   1806 CBC         813 :         current_result = expand_groupingset_node(gs);
                               1807                 : 
                               1808             813 :         Assert(current_result != NIL);
 2885 andres                   1809 ECB             : 
 2885 andres                   1810 GIC         813 :         numsets *= list_length(current_result);
 2885 andres                   1811 ECB             : 
 2885 andres                   1812 GIC         813 :         if (limit >= 0 && numsets > limit)
 2885 andres                   1813 LBC           0 :             return NIL;
                               1814                 : 
 2885 andres                   1815 CBC         813 :         expanded_groups = lappend(expanded_groups, current_result);
                               1816                 :     }
 2885 andres                   1817 ECB             : 
 2885 andres                   1818 EUB             :     /*
                               1819                 :      * Do cartesian product between sublists of expanded_groups. While at it,
 2878 bruce                    1820 ECB             :      * remove any duplicate elements from individual grouping sets (we must
                               1821                 :      * NOT change the number of sets though)
                               1822                 :      */
                               1823                 : 
 2885 andres                   1824 GIC        3068 :     foreach(lc, (List *) linitial(expanded_groups))
                               1825                 :     {
                               1826            2319 :         result = lappend(result, list_union_int(NIL, (List *) lfirst(lc)));
                               1827                 :     }
                               1828                 : 
  923 tgl                      1829 CBC         813 :     for_each_from(lc, expanded_groups, 1)
                               1830                 :     {
 2885 andres                   1831              64 :         List       *p = lfirst(lc);
 2885 andres                   1832 GIC          64 :         List       *new_result = NIL;
                               1833                 :         ListCell   *lc2;
 2885 andres                   1834 ECB             : 
 2885 andres                   1835 GIC         202 :         foreach(lc2, result)
 2885 andres                   1836 ECB             :         {
 2885 andres                   1837 CBC         138 :             List       *q = lfirst(lc2);
                               1838                 :             ListCell   *lc3;
                               1839                 : 
                               1840             480 :             foreach(lc3, p)
                               1841                 :             {
                               1842             342 :                 new_result = lappend(new_result,
 2885 andres                   1843 GIC         342 :                                      list_union_int(q, (List *) lfirst(lc3)));
                               1844                 :             }
 2885 andres                   1845 ECB             :         }
 2885 andres                   1846 GIC          64 :         result = new_result;
 2885 andres                   1847 ECB             :     }
                               1848                 : 
                               1849                 :     /* Now sort the lists by length and deduplicate if necessary */
  752 tomas.vondra             1850 GIC         749 :     if (!groupDistinct || list_length(result) < 2)
  752 tomas.vondra             1851 CBC         741 :         list_sort(result, cmp_list_len_asc);
                               1852                 :     else
                               1853                 :     {
                               1854                 :         ListCell   *cell;
  752 tomas.vondra             1855 ECB             :         List       *prev;
                               1856                 : 
                               1857                 :         /* Sort each groupset individually */
  752 tomas.vondra             1858 GIC          80 :         foreach(cell, result)
                               1859              72 :             list_sort(lfirst(cell), list_int_cmp);
                               1860                 : 
                               1861                 :         /* Now sort the list of groupsets by length and contents */
                               1862               8 :         list_sort(result, cmp_list_len_contents_asc);
  752 tomas.vondra             1863 ECB             : 
                               1864                 :         /* Finally, remove duplicates */
  657 drowley                  1865 GIC           8 :         prev = linitial(result);
  752 tomas.vondra             1866              72 :         for_each_from(cell, result, 1)
  752 tomas.vondra             1867 ECB             :         {
  752 tomas.vondra             1868 GIC          64 :             if (equal(lfirst(cell), prev))
      tgl                      1869              32 :                 result = foreach_delete_current(result, cell);
  752 tomas.vondra             1870 ECB             :             else
  752 tomas.vondra             1871 CBC          32 :                 prev = lfirst(cell);
                               1872                 :         }
  752 tomas.vondra             1873 ECB             :     }
 2885 andres                   1874                 : 
 2885 andres                   1875 GIC         749 :     return result;
 2885 andres                   1876 ECB             : }
                               1877                 : 
                               1878                 : /*
                               1879                 :  * get_aggregate_argtypes
 3394 tgl                      1880                 :  *  Identify the specific datatypes passed to an aggregate call.
                               1881                 :  *
                               1882                 :  * Given an Aggref, extract the actual datatypes of the input arguments.
                               1883                 :  * The input datatypes are reported in a way that matches up with the
                               1884                 :  * aggregate's declaration, ie, any ORDER BY columns attached to a plain
                               1885                 :  * aggregate are ignored, but we report both direct and aggregated args of
                               1886                 :  * an ordered-set aggregate.
                               1887                 :  *
                               1888                 :  * Datatypes are returned into inputTypes[], which must reference an array
                               1889                 :  * of length FUNC_MAX_ARGS.
                               1890                 :  *
                               1891                 :  * The function result is the number of actual arguments.
                               1892                 :  */
                               1893                 : int
 3394 tgl                      1894 GIC       41599 : get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
                               1895                 : {
                               1896           41599 :     int         numArguments = 0;
                               1897                 :     ListCell   *lc;
                               1898                 : 
 2487 tgl                      1899 CBC       41599 :     Assert(list_length(aggref->aggargtypes) <= FUNC_MAX_ARGS);
                               1900                 : 
                               1901           75480 :     foreach(lc, aggref->aggargtypes)
                               1902                 :     {
 2487 tgl                      1903 GIC       33881 :         inputTypes[numArguments++] = lfirst_oid(lc);
 3394 tgl                      1904 ECB             :     }
                               1905                 : 
 3394 tgl                      1906 CBC       41599 :     return numArguments;
                               1907                 : }
 3394 tgl                      1908 ECB             : 
                               1909                 : /*
                               1910                 :  * resolve_aggregate_transtype
                               1911                 :  *  Identify the transition state value's datatype for an aggregate call.
                               1912                 :  *
                               1913                 :  * This function resolves a polymorphic aggregate's state datatype.
                               1914                 :  * It must be passed the aggtranstype from the aggregate's catalog entry,
                               1915                 :  * as well as the actual argument types extracted by get_aggregate_argtypes.
                               1916                 :  * (We could fetch pg_aggregate.aggtranstype internally, but all existing
                               1917                 :  * callers already have the value at hand, so we make them pass it.)
                               1918                 :  */
                               1919                 : Oid
 3394 tgl                      1920 GIC       19193 : resolve_aggregate_transtype(Oid aggfuncid,
                               1921                 :                             Oid aggtranstype,
                               1922                 :                             Oid *inputTypes,
                               1923                 :                             int numArguments)
                               1924                 : {
 3394 tgl                      1925 ECB             :     /* resolve actual type of transition state, if polymorphic */
 3394 tgl                      1926 GIC       19193 :     if (IsPolymorphicType(aggtranstype))
                               1927                 :     {
                               1928                 :         /* have to fetch the agg's declared input types... */
                               1929                 :         Oid        *declaredArgTypes;
                               1930                 :         int         agg_nargs;
 3394 tgl                      1931 ECB             : 
 3394 tgl                      1932 GIC         266 :         (void) get_func_signature(aggfuncid, &declaredArgTypes, &agg_nargs);
                               1933                 : 
                               1934                 :         /*
                               1935                 :          * VARIADIC ANY aggs could have more actual than declared args, but
                               1936                 :          * such extra args can't affect polymorphic type resolution.
 3394 tgl                      1937 ECB             :          */
 3394 tgl                      1938 GIC         266 :         Assert(agg_nargs <= numArguments);
                               1939                 : 
                               1940             266 :         aggtranstype = enforce_generic_type_consistency(inputTypes,
                               1941                 :                                                         declaredArgTypes,
                               1942                 :                                                         agg_nargs,
 3394 tgl                      1943 ECB             :                                                         aggtranstype,
                               1944                 :                                                         false);
 3394 tgl                      1945 CBC         266 :         pfree(declaredArgTypes);
                               1946                 :     }
 3394 tgl                      1947 GIC       19193 :     return aggtranstype;
                               1948                 : }
                               1949                 : 
                               1950                 : /*
                               1951                 :  * agg_args_support_sendreceive
                               1952                 :  *      Returns true if all non-byval of aggref's arg types have send and
                               1953                 :  *      receive functions.
                               1954                 :  */
                               1955                 : bool
   76 drowley                  1956 GNC        5758 : agg_args_support_sendreceive(Aggref *aggref)
                               1957                 : {
                               1958                 :     ListCell   *lc;
                               1959                 : 
                               1960           11213 :     foreach(lc, aggref->args)
                               1961                 :     {
                               1962                 :         HeapTuple   typeTuple;
                               1963                 :         Form_pg_type pt;
                               1964            5758 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               1965            5758 :         Oid         type = exprType((Node *) tle->expr);
                               1966                 : 
                               1967            5758 :         typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
                               1968            5758 :         if (!HeapTupleIsValid(typeTuple))
   76 drowley                  1969 UNC           0 :             elog(ERROR, "cache lookup failed for type %u", type);
                               1970                 : 
   76 drowley                  1971 GNC        5758 :         pt = (Form_pg_type) GETSTRUCT(typeTuple);
                               1972                 : 
                               1973            5758 :         if (!pt->typbyval &&
                               1974            5719 :             (!OidIsValid(pt->typsend) || !OidIsValid(pt->typreceive)))
                               1975                 :         {
                               1976             303 :             ReleaseSysCache(typeTuple);
                               1977             303 :             return false;
                               1978                 :         }
                               1979            5455 :         ReleaseSysCache(typeTuple);
                               1980                 :     }
                               1981            5455 :     return true;
                               1982                 : }
                               1983                 : 
 7222 tgl                      1984 ECB             : /*
                               1985                 :  * Create an expression tree for the transition function of an aggregate.
 2805 heikki.linnakangas       1986                 :  * This is needed so that polymorphic functions can be used within an
                               1987                 :  * aggregate --- without the expression tree, such functions would not know
                               1988                 :  * the datatypes they are supposed to use.  (The trees will never actually
                               1989                 :  * be executed, however, so we can skimp a bit on correctness.)
                               1990                 :  *
                               1991                 :  * agg_input_types and agg_state_type identifies the input types of the
                               1992                 :  * aggregate.  These should be resolved to actual types (ie, none should
                               1993                 :  * ever be ANYELEMENT etc).
                               1994                 :  * agg_input_collation is the aggregate function's input collation.
 7222 tgl                      1995                 :  *
                               1996                 :  * For an ordered-set aggregate, remember that agg_input_types describes
                               1997                 :  * the direct arguments followed by the aggregated arguments.
                               1998                 :  *
 2805 heikki.linnakangas       1999                 :  * transfn_oid and invtransfn_oid identify the funcs to be called; the
                               2000                 :  * latter may be InvalidOid, however if invtransfn_oid is set then
                               2001                 :  * transfn_oid must also be set.
                               2002                 :  *
  644 drowley                  2003                 :  * transfn_oid may also be passed as the aggcombinefn when the *transfnexpr is
                               2004                 :  * to be used for a combine aggregate phase.  We expect invtransfn_oid to be
                               2005                 :  * InvalidOid in this case since there is no such thing as an inverse
                               2006                 :  * combinefn.
                               2007                 :  *
 3284 tgl                      2008 EUB             :  * Pointers to the constructed trees are returned into *transfnexpr,
                               2009                 :  * *invtransfnexpr. If there is no invtransfn, the respective pointer is set
 2805 heikki.linnakangas       2010 ECB             :  * to NULL.  Since use of the invtransfn is optional, NULL may be passed for
                               2011                 :  * invtransfnexpr.
 7222 tgl                      2012                 :  */
                               2013                 : void
 2805 heikki.linnakangas       2014 GIC       22835 : build_aggregate_transfn_expr(Oid *agg_input_types,
 2805 heikki.linnakangas       2015 ECB             :                              int agg_num_inputs,
                               2016                 :                              int agg_num_direct_inputs,
                               2017                 :                              bool agg_variadic,
                               2018                 :                              Oid agg_state_type,
                               2019                 :                              Oid agg_input_collation,
                               2020                 :                              Oid transfn_oid,
                               2021                 :                              Oid invtransfn_oid,
                               2022                 :                              Expr **transfnexpr,
                               2023                 :                              Expr **invtransfnexpr)
                               2024                 : {
                               2025                 :     List       *args;
                               2026                 :     FuncExpr   *fexpr;
                               2027                 :     int         i;
                               2028                 : 
                               2029                 :     /*
                               2030                 :      * Build arg list to use in the transfn FuncExpr node.
                               2031                 :      */
 2482 tgl                      2032 GIC       22835 :     args = list_make1(make_agg_arg(agg_state_type, agg_input_collation));
                               2033                 : 
 3394                          2034           41889 :     for (i = agg_num_direct_inputs; i < agg_num_inputs; i++)
                               2035                 :     {
 2482                          2036           19054 :         args = lappend(args,
                               2037           19054 :                        make_agg_arg(agg_input_types[i], agg_input_collation));
                               2038                 :     }
                               2039                 : 
 3505                          2040           22835 :     fexpr = makeFuncExpr(transfn_oid,
                               2041                 :                          agg_state_type,
                               2042                 :                          args,
                               2043                 :                          InvalidOid,
                               2044                 :                          agg_input_collation,
                               2045                 :                          COERCE_EXPLICIT_CALL);
                               2046           22835 :     fexpr->funcvariadic = agg_variadic;
                               2047           22835 :     *transfnexpr = (Expr *) fexpr;
                               2048                 : 
                               2049                 :     /*
                               2050                 :      * Build invtransfn expression if requested, with same args as transfn
                               2051                 :      */
 3284                          2052           22835 :     if (invtransfnexpr != NULL)
 3284 tgl                      2053 ECB             :     {
 3284 tgl                      2054 GIC         669 :         if (OidIsValid(invtransfn_oid))
                               2055                 :         {
                               2056             584 :             fexpr = makeFuncExpr(invtransfn_oid,
                               2057                 :                                  agg_state_type,
                               2058                 :                                  args,
                               2059                 :                                  InvalidOid,
                               2060                 :                                  agg_input_collation,
                               2061                 :                                  COERCE_EXPLICIT_CALL);
                               2062             584 :             fexpr->funcvariadic = agg_variadic;
                               2063             584 :             *invtransfnexpr = (Expr *) fexpr;
                               2064                 :         }
                               2065                 :         else
                               2066              85 :             *invtransfnexpr = NULL;
                               2067                 :     }
 2805 heikki.linnakangas       2068           22835 : }
                               2069                 : 
                               2070                 : /*
 2567 rhaas                    2071 ECB             :  * Like build_aggregate_transfn_expr, but creates an expression tree for the
                               2072                 :  * serialization function of an aggregate.
                               2073                 :  */
                               2074                 : void
 2482 tgl                      2075 CBC         168 : build_aggregate_serialfn_expr(Oid serialfn_oid,
 2567 rhaas                    2076 ECB             :                               Expr **serialfnexpr)
                               2077                 : {
                               2078                 :     List       *args;
                               2079                 :     FuncExpr   *fexpr;
                               2080                 : 
                               2081                 :     /* serialfn always takes INTERNAL and returns BYTEA */
 2482 tgl                      2082 GIC         168 :     args = list_make1(make_agg_arg(INTERNALOID, InvalidOid));
                               2083                 : 
 2567 rhaas                    2084             168 :     fexpr = makeFuncExpr(serialfn_oid,
 2482 tgl                      2085 ECB             :                          BYTEAOID,
 2567 rhaas                    2086                 :                          args,
                               2087                 :                          InvalidOid,
                               2088                 :                          InvalidOid,
                               2089                 :                          COERCE_EXPLICIT_CALL);
 2567 rhaas                    2090 GIC         168 :     *serialfnexpr = (Expr *) fexpr;
 2567 rhaas                    2091 CBC         168 : }
                               2092                 : 
 2482 tgl                      2093 ECB             : /*
                               2094                 :  * Like build_aggregate_transfn_expr, but creates an expression tree for the
                               2095                 :  * deserialization function of an aggregate.
                               2096                 :  */
                               2097                 : void
 2482 tgl                      2098 GIC          60 : build_aggregate_deserialfn_expr(Oid deserialfn_oid,
                               2099                 :                                 Expr **deserialfnexpr)
                               2100                 : {
 2482 tgl                      2101 ECB             :     List       *args;
                               2102                 :     FuncExpr   *fexpr;
                               2103                 : 
                               2104                 :     /* deserialfn always takes BYTEA, INTERNAL and returns INTERNAL */
 2482 tgl                      2105 CBC          60 :     args = list_make2(make_agg_arg(BYTEAOID, InvalidOid),
                               2106                 :                       make_agg_arg(INTERNALOID, InvalidOid));
 2482 tgl                      2107 ECB             : 
 2482 tgl                      2108 GIC          60 :     fexpr = makeFuncExpr(deserialfn_oid,
                               2109                 :                          INTERNALOID,
                               2110                 :                          args,
                               2111                 :                          InvalidOid,
                               2112                 :                          InvalidOid,
                               2113                 :                          COERCE_EXPLICIT_CALL);
 2482 tgl                      2114 CBC          60 :     *deserialfnexpr = (Expr *) fexpr;
 2482 tgl                      2115 GIC          60 : }
                               2116                 : 
                               2117                 : /*
                               2118                 :  * Like build_aggregate_transfn_expr, but creates an expression tree for the
                               2119                 :  * final function of an aggregate, rather than the transition function.
                               2120                 :  */
 2805 heikki.linnakangas       2121 ECB             : void
 2805 heikki.linnakangas       2122 GIC       10294 : build_aggregate_finalfn_expr(Oid *agg_input_types,
 2805 heikki.linnakangas       2123 ECB             :                              int num_finalfn_inputs,
                               2124                 :                              Oid agg_state_type,
                               2125                 :                              Oid agg_result_type,
                               2126                 :                              Oid agg_input_collation,
                               2127                 :                              Oid finalfn_oid,
                               2128                 :                              Expr **finalfnexpr)
                               2129                 : {
                               2130                 :     List       *args;
                               2131                 :     int         i;
                               2132                 : 
                               2133                 :     /*
                               2134                 :      * Build expr tree for final function
                               2135                 :      */
 2482 tgl                      2136 GIC       10294 :     args = list_make1(make_agg_arg(agg_state_type, agg_input_collation));
 7222 tgl                      2137 ECB             : 
                               2138                 :     /* finalfn may take additional args, which match agg's input types */
 3273 tgl                      2139 GIC       17066 :     for (i = 0; i < num_finalfn_inputs - 1; i++)
                               2140                 :     {
 2482                          2141            6772 :         args = lappend(args,
                               2142            6772 :                        make_agg_arg(agg_input_types[i], agg_input_collation));
                               2143                 :     }
 3394 tgl                      2144 ECB             : 
 7188 bruce                    2145 GIC       10294 :     *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
                               2146                 :                                          agg_result_type,
 7188 bruce                    2147 ECB             :                                          args,
                               2148                 :                                          InvalidOid,
                               2149                 :                                          agg_input_collation,
                               2150                 :                                          COERCE_EXPLICIT_CALL);
                               2151                 :     /* finalfn is currently never treated as variadic */
 7222 tgl                      2152 GIC       10294 : }
 2482 tgl                      2153 ECB             : 
                               2154                 : /*
                               2155                 :  * Convenience function to build dummy argument expressions for aggregates.
                               2156                 :  *
                               2157                 :  * We really only care that an aggregate support function can discover its
                               2158                 :  * actual argument types at runtime using get_fn_expr_argtype(), so it's okay
                               2159                 :  * to use Param nodes that don't correspond to any real Param.
                               2160                 :  */
                               2161                 : static Node *
 2482 tgl                      2162 GIC       59243 : make_agg_arg(Oid argtype, Oid argcollation)
                               2163                 : {
                               2164           59243 :     Param      *argp = makeNode(Param);
                               2165                 : 
                               2166           59243 :     argp->paramkind = PARAM_EXEC;
                               2167           59243 :     argp->paramid = -1;
                               2168           59243 :     argp->paramtype = argtype;
                               2169           59243 :     argp->paramtypmod = -1;
                               2170           59243 :     argp->paramcollid = argcollation;
                               2171           59243 :     argp->location = -1;
                               2172           59243 :     return (Node *) argp;
                               2173                 : }
        

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