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

Generated by: LCOV version 1.14