LCOV - code coverage report
Current view: top level - src/backend/parser - parse_agg.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 530 664 79.8 %
Date: 2019-11-13 21:06:57 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13