1 /*-------------------------------------------------------------------------
4 * handle aggregates in parser
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.79 2008/01/01 19:45:50 momjian Exp $
13 *-------------------------------------------------------------------------
17 #include "nodes/makefuncs.h"
18 #include "optimizer/clauses.h"
19 #include "optimizer/tlist.h"
20 #include "optimizer/var.h"
21 #include "parser/parse_agg.h"
22 #include "parser/parsetree.h"
23 #include "rewrite/rewriteManip.h"
24 #include "utils/lsyscache.h"
31 bool have_non_var_grouping;
33 } check_ungrouped_columns_context;
35 static void check_ungrouped_columns(Node *node, ParseState *pstate,
36 List *groupClauses, bool have_non_var_grouping);
37 static bool check_ungrouped_columns_walker(Node *node,
38 check_ungrouped_columns_context *context);
42 * transformAggregateCall -
43 * Finish initial transformation of an aggregate call
45 * parse_func.c has recognized the function as an aggregate, and has set
46 * up all the fields of the Aggref except agglevelsup. Here we must
47 * determine which query level the aggregate actually belongs to, set
48 * agglevelsup accordingly, and mark p_hasAggs true in the corresponding
52 transformAggregateCall(ParseState *pstate, Aggref *agg)
57 * The aggregate's level is the same as the level of the lowest-level
58 * variable or aggregate in its arguments; or if it contains no variables
59 * at all, we presume it to be local.
61 min_varlevel = find_minimum_var_level((Node *) agg->args);
64 * An aggregate can't directly contain another aggregate call of the same
65 * level (though outer aggs are okay). We can skip this check if we
66 * didn't find any local vars or aggs.
68 if (min_varlevel == 0)
70 if (checkExprHasAggs((Node *) agg->args))
72 (errcode(ERRCODE_GROUPING_ERROR),
73 errmsg("aggregate function calls cannot be nested")));
78 agg->agglevelsup = min_varlevel;
80 /* Mark the correct pstate as having aggregates */
81 while (min_varlevel-- > 0)
82 pstate = pstate->parentParseState;
83 pstate->p_hasAggs = true;
88 * parseCheckAggregates
89 * Check for aggregates where they shouldn't be and improper grouping.
91 * Ideally this should be done earlier, but it's difficult to distinguish
92 * aggregates from plain functions at the grammar level. So instead we
93 * check here. This function should be called after the target list and
94 * qualifications are finalized.
97 parseCheckAggregates(ParseState *pstate, Query *qry)
99 List *groupClauses = NIL;
100 bool have_non_var_grouping;
106 /* This should only be called if we found aggregates or grouping */
107 Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
110 * Aggregates must never appear in WHERE or JOIN/ON clauses.
112 * (Note this check should appear first to deliver an appropriate error
113 * message; otherwise we are likely to complain about some innocent
114 * variable in the target list, which is outright misleading if the
115 * problem is in WHERE.)
117 if (checkExprHasAggs(qry->jointree->quals))
119 (errcode(ERRCODE_GROUPING_ERROR),
120 errmsg("aggregates not allowed in WHERE clause")));
121 if (checkExprHasAggs((Node *) qry->jointree->fromlist))
123 (errcode(ERRCODE_GROUPING_ERROR),
124 errmsg("aggregates not allowed in JOIN conditions")));
127 * No aggregates allowed in GROUP BY clauses, either.
129 * While we are at it, build a list of the acceptable GROUP BY expressions
130 * for use by check_ungrouped_columns().
132 foreach(l, qry->groupClause)
134 GroupClause *grpcl = (GroupClause *) lfirst(l);
137 expr = get_sortgroupclause_expr(grpcl, qry->targetList);
139 continue; /* probably cannot happen */
140 if (checkExprHasAggs(expr))
142 (errcode(ERRCODE_GROUPING_ERROR),
143 errmsg("aggregates not allowed in GROUP BY clause")));
144 groupClauses = lcons(expr, groupClauses);
148 * If there are join alias vars involved, we have to flatten them to the
149 * underlying vars, so that aliased and unaliased vars will be correctly
150 * taken as equal. We can skip the expense of doing this if no rangetable
151 * entries are RTE_JOIN kind.
154 foreach(l, pstate->p_rtable)
156 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
158 if (rte->rtekind == RTE_JOIN)
166 * We use the planner's flatten_join_alias_vars routine to do the
167 * flattening; it wants a PlannerInfo root node, which fortunately can be
172 root = makeNode(PlannerInfo);
174 root->planner_cxt = CurrentMemoryContext;
175 root->hasJoinRTEs = true;
177 groupClauses = (List *) flatten_join_alias_vars(root,
178 (Node *) groupClauses);
181 root = NULL; /* keep compiler quiet */
184 * Detect whether any of the grouping expressions aren't simple Vars; if
185 * they're all Vars then we don't have to work so hard in the recursive
186 * scans. (Note we have to flatten aliases before this.)
188 have_non_var_grouping = false;
189 foreach(l, groupClauses)
191 if (!IsA((Node *) lfirst(l), Var))
193 have_non_var_grouping = true;
199 * Check the targetlist and HAVING clause for ungrouped variables.
201 clause = (Node *) qry->targetList;
203 clause = flatten_join_alias_vars(root, clause);
204 check_ungrouped_columns(clause, pstate,
205 groupClauses, have_non_var_grouping);
207 clause = (Node *) qry->havingQual;
209 clause = flatten_join_alias_vars(root, clause);
210 check_ungrouped_columns(clause, pstate,
211 groupClauses, have_non_var_grouping);
216 * check_ungrouped_columns -
217 * Scan the given expression tree for ungrouped variables (variables
218 * that are not listed in the groupClauses list and are not within
219 * the arguments of aggregate functions). Emit a suitable error message
222 * NOTE: we assume that the given clause has been transformed suitably for
223 * parser output. This means we can use expression_tree_walker.
225 * NOTE: we recognize grouping expressions in the main query, but only
226 * grouping Vars in subqueries. For example, this will be rejected,
227 * although it could be allowed:
229 * (SELECT x FROM bar where y = (foo.a + foo.b))
232 * The difficulty is the need to account for different sublevels_up.
233 * This appears to require a whole custom version of equal(), which is
234 * way more pain than the feature seems worth.
237 check_ungrouped_columns(Node *node, ParseState *pstate,
238 List *groupClauses, bool have_non_var_grouping)
240 check_ungrouped_columns_context context;
242 context.pstate = pstate;
243 context.groupClauses = groupClauses;
244 context.have_non_var_grouping = have_non_var_grouping;
245 context.sublevels_up = 0;
246 check_ungrouped_columns_walker(node, &context);
250 check_ungrouped_columns_walker(Node *node,
251 check_ungrouped_columns_context *context)
257 if (IsA(node, Const) ||
259 return false; /* constants are always acceptable */
262 * If we find an aggregate call of the original level, do not recurse into
263 * its arguments; ungrouped vars in the arguments are not an error. We can
264 * also skip looking at the arguments of aggregates of higher levels,
265 * since they could not possibly contain Vars that are of concern to us
266 * (see transformAggregateCall). We do need to look into the arguments of
267 * aggregates of lower levels, however.
269 if (IsA(node, Aggref) &&
270 (int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
274 * If we have any GROUP BY items that are not simple Vars, check to see if
275 * subexpression as a whole matches any GROUP BY item. We need to do this
276 * at every recursion level so that we recognize GROUPed-BY expressions
277 * before reaching variables within them. But this only works at the outer
278 * query level, as noted above.
280 if (context->have_non_var_grouping && context->sublevels_up == 0)
282 foreach(gl, context->groupClauses)
284 if (equal(node, lfirst(gl)))
285 return false; /* acceptable, do not descend more */
290 * If we have an ungrouped Var of the original query level, we have a
291 * failure. Vars below the original query level are not a problem, and
292 * neither are Vars from above it. (If such Vars are ungrouped as far as
293 * their own query level is concerned, that's someone else's problem...)
297 Var *var = (Var *) node;
301 if (var->varlevelsup != context->sublevels_up)
302 return false; /* it's not local to my query, ignore */
305 * Check for a match, if we didn't do it above.
307 if (!context->have_non_var_grouping || context->sublevels_up != 0)
309 foreach(gl, context->groupClauses)
311 Var *gvar = (Var *) lfirst(gl);
313 if (IsA(gvar, Var) &&
314 gvar->varno == var->varno &&
315 gvar->varattno == var->varattno &&
316 gvar->varlevelsup == 0)
317 return false; /* acceptable, we're okay */
321 /* Found an ungrouped local variable; generate error message */
322 Assert(var->varno > 0 &&
323 (int) var->varno <= list_length(context->pstate->p_rtable));
324 rte = rt_fetch(var->varno, context->pstate->p_rtable);
325 attname = get_rte_attribute_name(rte, var->varattno);
326 if (context->sublevels_up == 0)
328 (errcode(ERRCODE_GROUPING_ERROR),
329 errmsg("column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
330 rte->eref->aliasname, attname)));
333 (errcode(ERRCODE_GROUPING_ERROR),
334 errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
335 rte->eref->aliasname, attname)));
339 if (IsA(node, Query))
341 /* Recurse into subselects */
344 context->sublevels_up++;
345 result = query_tree_walker((Query *) node,
346 check_ungrouped_columns_walker,
349 context->sublevels_up--;
352 return expression_tree_walker(node, check_ungrouped_columns_walker,
357 * Create expression trees for the transition and final functions
358 * of an aggregate. These are needed so that polymorphic functions
359 * can be used within an aggregate --- without the expression trees,
360 * such functions would not know the datatypes they are supposed to use.
361 * (The trees will never actually be executed, however, so we can skimp
362 * a bit on correctness.)
364 * agg_input_types, agg_state_type, agg_result_type identify the input,
365 * transition, and result types of the aggregate. These should all be
366 * resolved to actual types (ie, none should ever be ANYELEMENT etc).
368 * transfn_oid and finalfn_oid identify the funcs to be called; the latter
371 * Pointers to the constructed trees are returned into *transfnexpr and
372 * *finalfnexpr. The latter is set to NULL if there's no finalfn.
375 build_aggregate_fnexprs(Oid *agg_input_types,
389 * Build arg list to use in the transfn FuncExpr node. We really only care
390 * that transfn can discover the actual argument types at runtime using
391 * get_fn_expr_argtype(), so it's okay to use Param nodes that don't
392 * correspond to any real Param.
394 argp = makeNode(Param);
395 argp->paramkind = PARAM_EXEC;
397 argp->paramtype = agg_state_type;
398 argp->paramtypmod = -1;
400 args = list_make1(argp);
402 for (i = 0; i < agg_num_inputs; i++)
404 argp = makeNode(Param);
405 argp->paramkind = PARAM_EXEC;
407 argp->paramtype = agg_input_types[i];
408 argp->paramtypmod = -1;
409 args = lappend(args, argp);
412 *transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
417 /* see if we have a final function */
418 if (!OidIsValid(finalfn_oid))
425 * Build expr tree for final function
427 argp = makeNode(Param);
428 argp->paramkind = PARAM_EXEC;
430 argp->paramtype = agg_state_type;
431 argp->paramtypmod = -1;
432 args = list_make1(argp);
434 *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,