Header And Logo

PostgreSQL
| The world's most advanced open source database.

parse_agg.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * parse_agg.c
00004  *    handle aggregates and window functions in parser
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/parser/parse_agg.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #include "postgres.h"
00016 
00017 #include "catalog/pg_constraint.h"
00018 #include "nodes/makefuncs.h"
00019 #include "nodes/nodeFuncs.h"
00020 #include "optimizer/tlist.h"
00021 #include "parser/parse_agg.h"
00022 #include "parser/parse_clause.h"
00023 #include "parser/parse_expr.h"
00024 #include "parser/parsetree.h"
00025 #include "rewrite/rewriteManip.h"
00026 #include "utils/builtins.h"
00027 
00028 
00029 typedef struct
00030 {
00031     ParseState *pstate;
00032     int         min_varlevel;
00033     int         min_agglevel;
00034     int         sublevels_up;
00035 } check_agg_arguments_context;
00036 
00037 typedef struct
00038 {
00039     ParseState *pstate;
00040     Query      *qry;
00041     List       *groupClauses;
00042     bool        have_non_var_grouping;
00043     List      **func_grouped_rels;
00044     int         sublevels_up;
00045 } check_ungrouped_columns_context;
00046 
00047 static int  check_agg_arguments(ParseState *pstate, List *args);
00048 static bool check_agg_arguments_walker(Node *node,
00049                            check_agg_arguments_context *context);
00050 static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
00051                         List *groupClauses, bool have_non_var_grouping,
00052                         List **func_grouped_rels);
00053 static bool check_ungrouped_columns_walker(Node *node,
00054                                check_ungrouped_columns_context *context);
00055 
00056 
00057 /*
00058  * transformAggregateCall -
00059  *      Finish initial transformation of an aggregate call
00060  *
00061  * parse_func.c has recognized the function as an aggregate, and has set up
00062  * all the fields of the Aggref except args, aggorder, aggdistinct and
00063  * agglevelsup.  The passed-in args list has been through standard expression
00064  * transformation, while the passed-in aggorder list hasn't been transformed
00065  * at all.
00066  *
00067  * Here we convert the args list into a targetlist by inserting TargetEntry
00068  * nodes, and then transform the aggorder and agg_distinct specifications to
00069  * produce lists of SortGroupClause nodes.  (That might also result in adding
00070  * resjunk expressions to the targetlist.)
00071  *
00072  * We must also determine which query level the aggregate actually belongs to,
00073  * set agglevelsup accordingly, and mark p_hasAggs true in the corresponding
00074  * pstate level.
00075  */
00076 void
00077 transformAggregateCall(ParseState *pstate, Aggref *agg,
00078                        List *args, List *aggorder, bool agg_distinct)
00079 {
00080     List       *tlist;
00081     List       *torder;
00082     List       *tdistinct = NIL;
00083     AttrNumber  attno;
00084     int         save_next_resno;
00085     int         min_varlevel;
00086     ListCell   *lc;
00087     const char *err;
00088     bool        errkind;
00089 
00090     /*
00091      * Transform the plain list of Exprs into a targetlist.  We don't bother
00092      * to assign column names to the entries.
00093      */
00094     tlist = NIL;
00095     attno = 1;
00096     foreach(lc, args)
00097     {
00098         Expr       *arg = (Expr *) lfirst(lc);
00099         TargetEntry *tle = makeTargetEntry(arg, attno++, NULL, false);
00100 
00101         tlist = lappend(tlist, tle);
00102     }
00103 
00104     /*
00105      * If we have an ORDER BY, transform it.  This will add columns to the
00106      * tlist if they appear in ORDER BY but weren't already in the arg list.
00107      * They will be marked resjunk = true so we can tell them apart from
00108      * regular aggregate arguments later.
00109      *
00110      * We need to mess with p_next_resno since it will be used to number any
00111      * new targetlist entries.
00112      */
00113     save_next_resno = pstate->p_next_resno;
00114     pstate->p_next_resno = attno;
00115 
00116     torder = transformSortClause(pstate,
00117                                  aggorder,
00118                                  &tlist,
00119                                  EXPR_KIND_ORDER_BY,
00120                                  true /* fix unknowns */ ,
00121                                  true /* force SQL99 rules */ );
00122 
00123     /*
00124      * If we have DISTINCT, transform that to produce a distinctList.
00125      */
00126     if (agg_distinct)
00127     {
00128         tdistinct = transformDistinctClause(pstate, &tlist, torder, true);
00129 
00130         /*
00131          * Remove this check if executor support for hashed distinct for
00132          * aggregates is ever added.
00133          */
00134         foreach(lc, tdistinct)
00135         {
00136             SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
00137 
00138             if (!OidIsValid(sortcl->sortop))
00139             {
00140                 Node       *expr = get_sortgroupclause_expr(sortcl, tlist);
00141 
00142                 ereport(ERROR,
00143                         (errcode(ERRCODE_UNDEFINED_FUNCTION),
00144                 errmsg("could not identify an ordering operator for type %s",
00145                        format_type_be(exprType(expr))),
00146                          errdetail("Aggregates with DISTINCT must be able to sort their inputs."),
00147                          parser_errposition(pstate, exprLocation(expr))));
00148             }
00149         }
00150     }
00151 
00152     /* Update the Aggref with the transformation results */
00153     agg->args = tlist;
00154     agg->aggorder = torder;
00155     agg->aggdistinct = tdistinct;
00156 
00157     pstate->p_next_resno = save_next_resno;
00158 
00159     /*
00160      * Check the arguments to compute the aggregate's level and detect
00161      * improper nesting.
00162      */
00163     min_varlevel = check_agg_arguments(pstate, agg->args);
00164     agg->agglevelsup = min_varlevel;
00165 
00166     /* Mark the correct pstate level as having aggregates */
00167     while (min_varlevel-- > 0)
00168         pstate = pstate->parentParseState;
00169     pstate->p_hasAggs = true;
00170 
00171     /*
00172      * Check to see if the aggregate function is in an invalid place within
00173      * its aggregation query.
00174      *
00175      * For brevity we support two schemes for reporting an error here: set
00176      * "err" to a custom message, or set "errkind" true if the error context
00177      * is sufficiently identified by what ParseExprKindName will return, *and*
00178      * what it will return is just a SQL keyword.  (Otherwise, use a custom
00179      * message to avoid creating translation problems.)
00180      */
00181     err = NULL;
00182     errkind = false;
00183     switch (pstate->p_expr_kind)
00184     {
00185         case EXPR_KIND_NONE:
00186             Assert(false);      /* can't happen */
00187             break;
00188         case EXPR_KIND_OTHER:
00189             /* Accept aggregate here; caller must throw error if wanted */
00190             break;
00191         case EXPR_KIND_JOIN_ON:
00192         case EXPR_KIND_JOIN_USING:
00193             err = _("aggregate functions are not allowed in JOIN conditions");
00194             break;
00195         case EXPR_KIND_FROM_SUBSELECT:
00196             /* Should only be possible in a LATERAL subquery */
00197             Assert(pstate->p_lateral_active);
00198             /* Aggregate scope rules make it worth being explicit here */
00199             err = _("aggregate functions are not allowed in FROM clause of their own query level");
00200             break;
00201         case EXPR_KIND_FROM_FUNCTION:
00202             err = _("aggregate functions are not allowed in functions in FROM");
00203             break;
00204         case EXPR_KIND_WHERE:
00205             errkind = true;
00206             break;
00207         case EXPR_KIND_HAVING:
00208             /* okay */
00209             break;
00210         case EXPR_KIND_WINDOW_PARTITION:
00211             /* okay */
00212             break;
00213         case EXPR_KIND_WINDOW_ORDER:
00214             /* okay */
00215             break;
00216         case EXPR_KIND_WINDOW_FRAME_RANGE:
00217             err = _("aggregate functions are not allowed in window RANGE");
00218             break;
00219         case EXPR_KIND_WINDOW_FRAME_ROWS:
00220             err = _("aggregate functions are not allowed in window ROWS");
00221             break;
00222         case EXPR_KIND_SELECT_TARGET:
00223             /* okay */
00224             break;
00225         case EXPR_KIND_INSERT_TARGET:
00226         case EXPR_KIND_UPDATE_SOURCE:
00227         case EXPR_KIND_UPDATE_TARGET:
00228             errkind = true;
00229             break;
00230         case EXPR_KIND_GROUP_BY:
00231             errkind = true;
00232             break;
00233         case EXPR_KIND_ORDER_BY:
00234             /* okay */
00235             break;
00236         case EXPR_KIND_DISTINCT_ON:
00237             /* okay */
00238             break;
00239         case EXPR_KIND_LIMIT:
00240         case EXPR_KIND_OFFSET:
00241             errkind = true;
00242             break;
00243         case EXPR_KIND_RETURNING:
00244             errkind = true;
00245             break;
00246         case EXPR_KIND_VALUES:
00247             errkind = true;
00248             break;
00249         case EXPR_KIND_CHECK_CONSTRAINT:
00250         case EXPR_KIND_DOMAIN_CHECK:
00251             err = _("aggregate functions are not allowed in check constraints");
00252             break;
00253         case EXPR_KIND_COLUMN_DEFAULT:
00254         case EXPR_KIND_FUNCTION_DEFAULT:
00255             err = _("aggregate functions are not allowed in DEFAULT expressions");
00256             break;
00257         case EXPR_KIND_INDEX_EXPRESSION:
00258             err = _("aggregate functions are not allowed in index expressions");
00259             break;
00260         case EXPR_KIND_INDEX_PREDICATE:
00261             err = _("aggregate functions are not allowed in index predicates");
00262             break;
00263         case EXPR_KIND_ALTER_COL_TRANSFORM:
00264             err = _("aggregate functions are not allowed in transform expressions");
00265             break;
00266         case EXPR_KIND_EXECUTE_PARAMETER:
00267             err = _("aggregate functions are not allowed in EXECUTE parameters");
00268             break;
00269         case EXPR_KIND_TRIGGER_WHEN:
00270             err = _("aggregate functions are not allowed in trigger WHEN conditions");
00271             break;
00272 
00273             /*
00274              * There is intentionally no default: case here, so that the
00275              * compiler will warn if we add a new ParseExprKind without
00276              * extending this switch.  If we do see an unrecognized value at
00277              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
00278              * which is sane anyway.
00279              */
00280     }
00281     if (err)
00282         ereport(ERROR,
00283                 (errcode(ERRCODE_GROUPING_ERROR),
00284                  errmsg_internal("%s", err),
00285                  parser_errposition(pstate, agg->location)));
00286     if (errkind)
00287         ereport(ERROR,
00288                 (errcode(ERRCODE_GROUPING_ERROR),
00289                  /* translator: %s is name of a SQL construct, eg GROUP BY */
00290                  errmsg("aggregate functions are not allowed in %s",
00291                         ParseExprKindName(pstate->p_expr_kind)),
00292                  parser_errposition(pstate, agg->location)));
00293 }
00294 
00295 /*
00296  * check_agg_arguments
00297  *    Scan the arguments of an aggregate function to determine the
00298  *    aggregate's semantic level (zero is the current select's level,
00299  *    one is its parent, etc).
00300  *
00301  * The aggregate's level is the same as the level of the lowest-level variable
00302  * or aggregate in its arguments; or if it contains no variables at all, we
00303  * presume it to be local.
00304  *
00305  * We also take this opportunity to detect any aggregates or window functions
00306  * nested within the arguments.  We can throw error immediately if we find
00307  * a window function.  Aggregates are a bit trickier because it's only an
00308  * error if the inner aggregate is of the same semantic level as the outer,
00309  * which we can't know until we finish scanning the arguments.
00310  */
00311 static int
00312 check_agg_arguments(ParseState *pstate, List *args)
00313 {
00314     int         agglevel;
00315     check_agg_arguments_context context;
00316 
00317     context.pstate = pstate;
00318     context.min_varlevel = -1;  /* signifies nothing found yet */
00319     context.min_agglevel = -1;
00320     context.sublevels_up = 0;
00321 
00322     (void) expression_tree_walker((Node *) args,
00323                                   check_agg_arguments_walker,
00324                                   (void *) &context);
00325 
00326     /*
00327      * If we found no vars nor aggs at all, it's a level-zero aggregate;
00328      * otherwise, its level is the minimum of vars or aggs.
00329      */
00330     if (context.min_varlevel < 0)
00331     {
00332         if (context.min_agglevel < 0)
00333             return 0;
00334         agglevel = context.min_agglevel;
00335     }
00336     else if (context.min_agglevel < 0)
00337         agglevel = context.min_varlevel;
00338     else
00339         agglevel = Min(context.min_varlevel, context.min_agglevel);
00340 
00341     /*
00342      * If there's a nested aggregate of the same semantic level, complain.
00343      */
00344     if (agglevel == context.min_agglevel)
00345         ereport(ERROR,
00346                 (errcode(ERRCODE_GROUPING_ERROR),
00347                  errmsg("aggregate function calls cannot be nested"),
00348                  parser_errposition(pstate,
00349                                     locate_agg_of_level((Node *) args,
00350                                                         agglevel))));
00351 
00352     return agglevel;
00353 }
00354 
00355 static bool
00356 check_agg_arguments_walker(Node *node,
00357                            check_agg_arguments_context *context)
00358 {
00359     if (node == NULL)
00360         return false;
00361     if (IsA(node, Var))
00362     {
00363         int         varlevelsup = ((Var *) node)->varlevelsup;
00364 
00365         /* convert levelsup to frame of reference of original query */
00366         varlevelsup -= context->sublevels_up;
00367         /* ignore local vars of subqueries */
00368         if (varlevelsup >= 0)
00369         {
00370             if (context->min_varlevel < 0 ||
00371                 context->min_varlevel > varlevelsup)
00372                 context->min_varlevel = varlevelsup;
00373         }
00374         return false;
00375     }
00376     if (IsA(node, Aggref))
00377     {
00378         int         agglevelsup = ((Aggref *) node)->agglevelsup;
00379 
00380         /* convert levelsup to frame of reference of original query */
00381         agglevelsup -= context->sublevels_up;
00382         /* ignore local aggs of subqueries */
00383         if (agglevelsup >= 0)
00384         {
00385             if (context->min_agglevel < 0 ||
00386                 context->min_agglevel > agglevelsup)
00387                 context->min_agglevel = agglevelsup;
00388         }
00389         /* no need to examine args of the inner aggregate */
00390         return false;
00391     }
00392     /* We can throw error on sight for a window function */
00393     if (IsA(node, WindowFunc))
00394         ereport(ERROR,
00395                 (errcode(ERRCODE_GROUPING_ERROR),
00396                  errmsg("aggregate function calls cannot contain window function calls"),
00397                  parser_errposition(context->pstate,
00398                                     ((WindowFunc *) node)->location)));
00399     if (IsA(node, Query))
00400     {
00401         /* Recurse into subselects */
00402         bool        result;
00403 
00404         context->sublevels_up++;
00405         result = query_tree_walker((Query *) node,
00406                                    check_agg_arguments_walker,
00407                                    (void *) context,
00408                                    0);
00409         context->sublevels_up--;
00410         return result;
00411     }
00412     return expression_tree_walker(node,
00413                                   check_agg_arguments_walker,
00414                                   (void *) context);
00415 }
00416 
00417 /*
00418  * transformWindowFuncCall -
00419  *      Finish initial transformation of a window function call
00420  *
00421  * parse_func.c has recognized the function as a window function, and has set
00422  * up all the fields of the WindowFunc except winref.  Here we must (1) add
00423  * the WindowDef to the pstate (if not a duplicate of one already present) and
00424  * set winref to link to it; and (2) mark p_hasWindowFuncs true in the pstate.
00425  * Unlike aggregates, only the most closely nested pstate level need be
00426  * considered --- there are no "outer window functions" per SQL spec.
00427  */
00428 void
00429 transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
00430                         WindowDef *windef)
00431 {
00432     const char *err;
00433     bool        errkind;
00434 
00435     /*
00436      * A window function call can't contain another one (but aggs are OK). XXX
00437      * is this required by spec, or just an unimplemented feature?
00438      */
00439     if (pstate->p_hasWindowFuncs &&
00440         contain_windowfuncs((Node *) wfunc->args))
00441         ereport(ERROR,
00442                 (errcode(ERRCODE_WINDOWING_ERROR),
00443                  errmsg("window function calls cannot be nested"),
00444                  parser_errposition(pstate,
00445                                   locate_windowfunc((Node *) wfunc->args))));
00446 
00447     /*
00448      * Check to see if the window function is in an invalid place within the
00449      * query.
00450      *
00451      * For brevity we support two schemes for reporting an error here: set
00452      * "err" to a custom message, or set "errkind" true if the error context
00453      * is sufficiently identified by what ParseExprKindName will return, *and*
00454      * what it will return is just a SQL keyword.  (Otherwise, use a custom
00455      * message to avoid creating translation problems.)
00456      */
00457     err = NULL;
00458     errkind = false;
00459     switch (pstate->p_expr_kind)
00460     {
00461         case EXPR_KIND_NONE:
00462             Assert(false);      /* can't happen */
00463             break;
00464         case EXPR_KIND_OTHER:
00465             /* Accept window func here; caller must throw error if wanted */
00466             break;
00467         case EXPR_KIND_JOIN_ON:
00468         case EXPR_KIND_JOIN_USING:
00469             err = _("window functions are not allowed in JOIN conditions");
00470             break;
00471         case EXPR_KIND_FROM_SUBSELECT:
00472             /* can't get here, but just in case, throw an error */
00473             errkind = true;
00474             break;
00475         case EXPR_KIND_FROM_FUNCTION:
00476             err = _("window functions are not allowed in functions in FROM");
00477             break;
00478         case EXPR_KIND_WHERE:
00479             errkind = true;
00480             break;
00481         case EXPR_KIND_HAVING:
00482             errkind = true;
00483             break;
00484         case EXPR_KIND_WINDOW_PARTITION:
00485         case EXPR_KIND_WINDOW_ORDER:
00486         case EXPR_KIND_WINDOW_FRAME_RANGE:
00487         case EXPR_KIND_WINDOW_FRAME_ROWS:
00488             err = _("window functions are not allowed in window definitions");
00489             break;
00490         case EXPR_KIND_SELECT_TARGET:
00491             /* okay */
00492             break;
00493         case EXPR_KIND_INSERT_TARGET:
00494         case EXPR_KIND_UPDATE_SOURCE:
00495         case EXPR_KIND_UPDATE_TARGET:
00496             errkind = true;
00497             break;
00498         case EXPR_KIND_GROUP_BY:
00499             errkind = true;
00500             break;
00501         case EXPR_KIND_ORDER_BY:
00502             /* okay */
00503             break;
00504         case EXPR_KIND_DISTINCT_ON:
00505             /* okay */
00506             break;
00507         case EXPR_KIND_LIMIT:
00508         case EXPR_KIND_OFFSET:
00509             errkind = true;
00510             break;
00511         case EXPR_KIND_RETURNING:
00512             errkind = true;
00513             break;
00514         case EXPR_KIND_VALUES:
00515             errkind = true;
00516             break;
00517         case EXPR_KIND_CHECK_CONSTRAINT:
00518         case EXPR_KIND_DOMAIN_CHECK:
00519             err = _("window functions are not allowed in check constraints");
00520             break;
00521         case EXPR_KIND_COLUMN_DEFAULT:
00522         case EXPR_KIND_FUNCTION_DEFAULT:
00523             err = _("window functions are not allowed in DEFAULT expressions");
00524             break;
00525         case EXPR_KIND_INDEX_EXPRESSION:
00526             err = _("window functions are not allowed in index expressions");
00527             break;
00528         case EXPR_KIND_INDEX_PREDICATE:
00529             err = _("window functions are not allowed in index predicates");
00530             break;
00531         case EXPR_KIND_ALTER_COL_TRANSFORM:
00532             err = _("window functions are not allowed in transform expressions");
00533             break;
00534         case EXPR_KIND_EXECUTE_PARAMETER:
00535             err = _("window functions are not allowed in EXECUTE parameters");
00536             break;
00537         case EXPR_KIND_TRIGGER_WHEN:
00538             err = _("window functions are not allowed in trigger WHEN conditions");
00539             break;
00540 
00541             /*
00542              * There is intentionally no default: case here, so that the
00543              * compiler will warn if we add a new ParseExprKind without
00544              * extending this switch.  If we do see an unrecognized value at
00545              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
00546              * which is sane anyway.
00547              */
00548     }
00549     if (err)
00550         ereport(ERROR,
00551                 (errcode(ERRCODE_WINDOWING_ERROR),
00552                  errmsg_internal("%s", err),
00553                  parser_errposition(pstate, wfunc->location)));
00554     if (errkind)
00555         ereport(ERROR,
00556                 (errcode(ERRCODE_WINDOWING_ERROR),
00557                  /* translator: %s is name of a SQL construct, eg GROUP BY */
00558                  errmsg("window functions are not allowed in %s",
00559                         ParseExprKindName(pstate->p_expr_kind)),
00560                  parser_errposition(pstate, wfunc->location)));
00561 
00562     /*
00563      * If the OVER clause just specifies a window name, find that WINDOW
00564      * clause (which had better be present).  Otherwise, try to match all the
00565      * properties of the OVER clause, and make a new entry in the p_windowdefs
00566      * list if no luck.
00567      */
00568     if (windef->name)
00569     {
00570         Index       winref = 0;
00571         ListCell   *lc;
00572 
00573         Assert(windef->refname == NULL &&
00574                windef->partitionClause == NIL &&
00575                windef->orderClause == NIL &&
00576                windef->frameOptions == FRAMEOPTION_DEFAULTS);
00577 
00578         foreach(lc, pstate->p_windowdefs)
00579         {
00580             WindowDef  *refwin = (WindowDef *) lfirst(lc);
00581 
00582             winref++;
00583             if (refwin->name && strcmp(refwin->name, windef->name) == 0)
00584             {
00585                 wfunc->winref = winref;
00586                 break;
00587             }
00588         }
00589         if (lc == NULL)         /* didn't find it? */
00590             ereport(ERROR,
00591                     (errcode(ERRCODE_UNDEFINED_OBJECT),
00592                      errmsg("window \"%s\" does not exist", windef->name),
00593                      parser_errposition(pstate, windef->location)));
00594     }
00595     else
00596     {
00597         Index       winref = 0;
00598         ListCell   *lc;
00599 
00600         foreach(lc, pstate->p_windowdefs)
00601         {
00602             WindowDef  *refwin = (WindowDef *) lfirst(lc);
00603 
00604             winref++;
00605             if (refwin->refname && windef->refname &&
00606                 strcmp(refwin->refname, windef->refname) == 0)
00607                  /* matched on refname */ ;
00608             else if (!refwin->refname && !windef->refname)
00609                  /* matched, no refname */ ;
00610             else
00611                 continue;
00612             if (equal(refwin->partitionClause, windef->partitionClause) &&
00613                 equal(refwin->orderClause, windef->orderClause) &&
00614                 refwin->frameOptions == windef->frameOptions &&
00615                 equal(refwin->startOffset, windef->startOffset) &&
00616                 equal(refwin->endOffset, windef->endOffset))
00617             {
00618                 /* found a duplicate window specification */
00619                 wfunc->winref = winref;
00620                 break;
00621             }
00622         }
00623         if (lc == NULL)         /* didn't find it? */
00624         {
00625             pstate->p_windowdefs = lappend(pstate->p_windowdefs, windef);
00626             wfunc->winref = list_length(pstate->p_windowdefs);
00627         }
00628     }
00629 
00630     pstate->p_hasWindowFuncs = true;
00631 }
00632 
00633 /*
00634  * parseCheckAggregates
00635  *  Check for aggregates where they shouldn't be and improper grouping.
00636  *  This function should be called after the target list and qualifications
00637  *  are finalized.
00638  *
00639  *  Misplaced aggregates are now mostly detected in transformAggregateCall,
00640  *  but it seems more robust to check for aggregates in recursive queries
00641  *  only after everything is finalized.  In any case it's hard to detect
00642  *  improper grouping on-the-fly, so we have to make another pass over the
00643  *  query for that.
00644  */
00645 void
00646 parseCheckAggregates(ParseState *pstate, Query *qry)
00647 {
00648     List       *groupClauses = NIL;
00649     bool        have_non_var_grouping;
00650     List       *func_grouped_rels = NIL;
00651     ListCell   *l;
00652     bool        hasJoinRTEs;
00653     bool        hasSelfRefRTEs;
00654     PlannerInfo *root;
00655     Node       *clause;
00656 
00657     /* This should only be called if we found aggregates or grouping */
00658     Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
00659 
00660     /*
00661      * Scan the range table to see if there are JOIN or self-reference CTE
00662      * entries.  We'll need this info below.
00663      */
00664     hasJoinRTEs = hasSelfRefRTEs = false;
00665     foreach(l, pstate->p_rtable)
00666     {
00667         RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
00668 
00669         if (rte->rtekind == RTE_JOIN)
00670             hasJoinRTEs = true;
00671         else if (rte->rtekind == RTE_CTE && rte->self_reference)
00672             hasSelfRefRTEs = true;
00673     }
00674 
00675     /*
00676      * Build a list of the acceptable GROUP BY expressions for use by
00677      * check_ungrouped_columns().
00678      */
00679     foreach(l, qry->groupClause)
00680     {
00681         SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
00682         Node       *expr;
00683 
00684         expr = get_sortgroupclause_expr(grpcl, qry->targetList);
00685         if (expr == NULL)
00686             continue;           /* probably cannot happen */
00687         groupClauses = lcons(expr, groupClauses);
00688     }
00689 
00690     /*
00691      * If there are join alias vars involved, we have to flatten them to the
00692      * underlying vars, so that aliased and unaliased vars will be correctly
00693      * taken as equal.  We can skip the expense of doing this if no rangetable
00694      * entries are RTE_JOIN kind. We use the planner's flatten_join_alias_vars
00695      * routine to do the flattening; it wants a PlannerInfo root node, which
00696      * fortunately can be mostly dummy.
00697      */
00698     if (hasJoinRTEs)
00699     {
00700         root = makeNode(PlannerInfo);
00701         root->parse = qry;
00702         root->planner_cxt = CurrentMemoryContext;
00703         root->hasJoinRTEs = true;
00704 
00705         groupClauses = (List *) flatten_join_alias_vars(root,
00706                                                       (Node *) groupClauses);
00707     }
00708     else
00709         root = NULL;            /* keep compiler quiet */
00710 
00711     /*
00712      * Detect whether any of the grouping expressions aren't simple Vars; if
00713      * they're all Vars then we don't have to work so hard in the recursive
00714      * scans.  (Note we have to flatten aliases before this.)
00715      */
00716     have_non_var_grouping = false;
00717     foreach(l, groupClauses)
00718     {
00719         if (!IsA((Node *) lfirst(l), Var))
00720         {
00721             have_non_var_grouping = true;
00722             break;
00723         }
00724     }
00725 
00726     /*
00727      * Check the targetlist and HAVING clause for ungrouped variables.
00728      *
00729      * Note: because we check resjunk tlist elements as well as regular ones,
00730      * this will also find ungrouped variables that came from ORDER BY and
00731      * WINDOW clauses.  For that matter, it's also going to examine the
00732      * grouping expressions themselves --- but they'll all pass the test ...
00733      */
00734     clause = (Node *) qry->targetList;
00735     if (hasJoinRTEs)
00736         clause = flatten_join_alias_vars(root, clause);
00737     check_ungrouped_columns(clause, pstate, qry,
00738                             groupClauses, have_non_var_grouping,
00739                             &func_grouped_rels);
00740 
00741     clause = (Node *) qry->havingQual;
00742     if (hasJoinRTEs)
00743         clause = flatten_join_alias_vars(root, clause);
00744     check_ungrouped_columns(clause, pstate, qry,
00745                             groupClauses, have_non_var_grouping,
00746                             &func_grouped_rels);
00747 
00748     /*
00749      * Per spec, aggregates can't appear in a recursive term.
00750      */
00751     if (pstate->p_hasAggs && hasSelfRefRTEs)
00752         ereport(ERROR,
00753                 (errcode(ERRCODE_INVALID_RECURSION),
00754                  errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
00755                  parser_errposition(pstate,
00756                                     locate_agg_of_level((Node *) qry, 0))));
00757 }
00758 
00759 /*
00760  * check_ungrouped_columns -
00761  *    Scan the given expression tree for ungrouped variables (variables
00762  *    that are not listed in the groupClauses list and are not within
00763  *    the arguments of aggregate functions).  Emit a suitable error message
00764  *    if any are found.
00765  *
00766  * NOTE: we assume that the given clause has been transformed suitably for
00767  * parser output.  This means we can use expression_tree_walker.
00768  *
00769  * NOTE: we recognize grouping expressions in the main query, but only
00770  * grouping Vars in subqueries.  For example, this will be rejected,
00771  * although it could be allowed:
00772  *      SELECT
00773  *          (SELECT x FROM bar where y = (foo.a + foo.b))
00774  *      FROM foo
00775  *      GROUP BY a + b;
00776  * The difficulty is the need to account for different sublevels_up.
00777  * This appears to require a whole custom version of equal(), which is
00778  * way more pain than the feature seems worth.
00779  */
00780 static void
00781 check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
00782                         List *groupClauses, bool have_non_var_grouping,
00783                         List **func_grouped_rels)
00784 {
00785     check_ungrouped_columns_context context;
00786 
00787     context.pstate = pstate;
00788     context.qry = qry;
00789     context.groupClauses = groupClauses;
00790     context.have_non_var_grouping = have_non_var_grouping;
00791     context.func_grouped_rels = func_grouped_rels;
00792     context.sublevels_up = 0;
00793     check_ungrouped_columns_walker(node, &context);
00794 }
00795 
00796 static bool
00797 check_ungrouped_columns_walker(Node *node,
00798                                check_ungrouped_columns_context *context)
00799 {
00800     ListCell   *gl;
00801 
00802     if (node == NULL)
00803         return false;
00804     if (IsA(node, Const) ||
00805         IsA(node, Param))
00806         return false;           /* constants are always acceptable */
00807 
00808     /*
00809      * If we find an aggregate call of the original level, do not recurse into
00810      * its arguments; ungrouped vars in the arguments are not an error. We can
00811      * also skip looking at the arguments of aggregates of higher levels,
00812      * since they could not possibly contain Vars that are of concern to us
00813      * (see transformAggregateCall).  We do need to look into the arguments of
00814      * aggregates of lower levels, however.
00815      */
00816     if (IsA(node, Aggref) &&
00817         (int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
00818         return false;
00819 
00820     /*
00821      * If we have any GROUP BY items that are not simple Vars, check to see if
00822      * subexpression as a whole matches any GROUP BY item. We need to do this
00823      * at every recursion level so that we recognize GROUPed-BY expressions
00824      * before reaching variables within them. But this only works at the outer
00825      * query level, as noted above.
00826      */
00827     if (context->have_non_var_grouping && context->sublevels_up == 0)
00828     {
00829         foreach(gl, context->groupClauses)
00830         {
00831             if (equal(node, lfirst(gl)))
00832                 return false;   /* acceptable, do not descend more */
00833         }
00834     }
00835 
00836     /*
00837      * If we have an ungrouped Var of the original query level, we have a
00838      * failure.  Vars below the original query level are not a problem, and
00839      * neither are Vars from above it.  (If such Vars are ungrouped as far as
00840      * their own query level is concerned, that's someone else's problem...)
00841      */
00842     if (IsA(node, Var))
00843     {
00844         Var        *var = (Var *) node;
00845         RangeTblEntry *rte;
00846         char       *attname;
00847 
00848         if (var->varlevelsup != context->sublevels_up)
00849             return false;       /* it's not local to my query, ignore */
00850 
00851         /*
00852          * Check for a match, if we didn't do it above.
00853          */
00854         if (!context->have_non_var_grouping || context->sublevels_up != 0)
00855         {
00856             foreach(gl, context->groupClauses)
00857             {
00858                 Var        *gvar = (Var *) lfirst(gl);
00859 
00860                 if (IsA(gvar, Var) &&
00861                     gvar->varno == var->varno &&
00862                     gvar->varattno == var->varattno &&
00863                     gvar->varlevelsup == 0)
00864                     return false;       /* acceptable, we're okay */
00865             }
00866         }
00867 
00868         /*
00869          * Check whether the Var is known functionally dependent on the GROUP
00870          * BY columns.  If so, we can allow the Var to be used, because the
00871          * grouping is really a no-op for this table.  However, this deduction
00872          * depends on one or more constraints of the table, so we have to add
00873          * those constraints to the query's constraintDeps list, because it's
00874          * not semantically valid anymore if the constraint(s) get dropped.
00875          * (Therefore, this check must be the last-ditch effort before raising
00876          * error: we don't want to add dependencies unnecessarily.)
00877          *
00878          * Because this is a pretty expensive check, and will have the same
00879          * outcome for all columns of a table, we remember which RTEs we've
00880          * already proven functional dependency for in the func_grouped_rels
00881          * list.  This test also prevents us from adding duplicate entries to
00882          * the constraintDeps list.
00883          */
00884         if (list_member_int(*context->func_grouped_rels, var->varno))
00885             return false;       /* previously proven acceptable */
00886 
00887         Assert(var->varno > 0 &&
00888                (int) var->varno <= list_length(context->pstate->p_rtable));
00889         rte = rt_fetch(var->varno, context->pstate->p_rtable);
00890         if (rte->rtekind == RTE_RELATION)
00891         {
00892             if (check_functional_grouping(rte->relid,
00893                                           var->varno,
00894                                           0,
00895                                           context->groupClauses,
00896                                           &context->qry->constraintDeps))
00897             {
00898                 *context->func_grouped_rels =
00899                     lappend_int(*context->func_grouped_rels, var->varno);
00900                 return false;   /* acceptable */
00901             }
00902         }
00903 
00904         /* Found an ungrouped local variable; generate error message */
00905         attname = get_rte_attribute_name(rte, var->varattno);
00906         if (context->sublevels_up == 0)
00907             ereport(ERROR,
00908                     (errcode(ERRCODE_GROUPING_ERROR),
00909                      errmsg("column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
00910                             rte->eref->aliasname, attname),
00911                      parser_errposition(context->pstate, var->location)));
00912         else
00913             ereport(ERROR,
00914                     (errcode(ERRCODE_GROUPING_ERROR),
00915                      errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
00916                             rte->eref->aliasname, attname),
00917                      parser_errposition(context->pstate, var->location)));
00918     }
00919 
00920     if (IsA(node, Query))
00921     {
00922         /* Recurse into subselects */
00923         bool        result;
00924 
00925         context->sublevels_up++;
00926         result = query_tree_walker((Query *) node,
00927                                    check_ungrouped_columns_walker,
00928                                    (void *) context,
00929                                    0);
00930         context->sublevels_up--;
00931         return result;
00932     }
00933     return expression_tree_walker(node, check_ungrouped_columns_walker,
00934                                   (void *) context);
00935 }
00936 
00937 /*
00938  * Create expression trees for the transition and final functions
00939  * of an aggregate.  These are needed so that polymorphic functions
00940  * can be used within an aggregate --- without the expression trees,
00941  * such functions would not know the datatypes they are supposed to use.
00942  * (The trees will never actually be executed, however, so we can skimp
00943  * a bit on correctness.)
00944  *
00945  * agg_input_types, agg_state_type, agg_result_type identify the input,
00946  * transition, and result types of the aggregate.  These should all be
00947  * resolved to actual types (ie, none should ever be ANYELEMENT etc).
00948  * agg_input_collation is the aggregate function's input collation.
00949  *
00950  * transfn_oid and finalfn_oid identify the funcs to be called; the latter
00951  * may be InvalidOid.
00952  *
00953  * Pointers to the constructed trees are returned into *transfnexpr and
00954  * *finalfnexpr.  The latter is set to NULL if there's no finalfn.
00955  */
00956 void
00957 build_aggregate_fnexprs(Oid *agg_input_types,
00958                         int agg_num_inputs,
00959                         Oid agg_state_type,
00960                         Oid agg_result_type,
00961                         Oid agg_input_collation,
00962                         Oid transfn_oid,
00963                         Oid finalfn_oid,
00964                         Expr **transfnexpr,
00965                         Expr **finalfnexpr)
00966 {
00967     Param      *argp;
00968     List       *args;
00969     int         i;
00970 
00971     /*
00972      * Build arg list to use in the transfn FuncExpr node. We really only care
00973      * that transfn can discover the actual argument types at runtime using
00974      * get_fn_expr_argtype(), so it's okay to use Param nodes that don't
00975      * correspond to any real Param.
00976      */
00977     argp = makeNode(Param);
00978     argp->paramkind = PARAM_EXEC;
00979     argp->paramid = -1;
00980     argp->paramtype = agg_state_type;
00981     argp->paramtypmod = -1;
00982     argp->paramcollid = agg_input_collation;
00983     argp->location = -1;
00984 
00985     args = list_make1(argp);
00986 
00987     for (i = 0; i < agg_num_inputs; i++)
00988     {
00989         argp = makeNode(Param);
00990         argp->paramkind = PARAM_EXEC;
00991         argp->paramid = -1;
00992         argp->paramtype = agg_input_types[i];
00993         argp->paramtypmod = -1;
00994         argp->paramcollid = agg_input_collation;
00995         argp->location = -1;
00996         args = lappend(args, argp);
00997     }
00998 
00999     *transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
01000                                          agg_state_type,
01001                                          args,
01002                                          InvalidOid,
01003                                          agg_input_collation,
01004                                          COERCE_EXPLICIT_CALL);
01005 
01006     /* see if we have a final function */
01007     if (!OidIsValid(finalfn_oid))
01008     {
01009         *finalfnexpr = NULL;
01010         return;
01011     }
01012 
01013     /*
01014      * Build expr tree for final function
01015      */
01016     argp = makeNode(Param);
01017     argp->paramkind = PARAM_EXEC;
01018     argp->paramid = -1;
01019     argp->paramtype = agg_state_type;
01020     argp->paramtypmod = -1;
01021     argp->paramcollid = agg_input_collation;
01022     argp->location = -1;
01023     args = list_make1(argp);
01024 
01025     *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
01026                                          agg_result_type,
01027                                          args,
01028                                          InvalidOid,
01029                                          agg_input_collation,
01030                                          COERCE_EXPLICIT_CALL);
01031 }