]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_agg.c
03a765e96efabb80cf87bf1e221547ca37df4708
[postgresql] / src / backend / parser / parse_agg.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_agg.c
4  *        handle aggregates in parser
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.79 2008/01/01 19:45:50 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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"
25
26
27 typedef struct
28 {
29         ParseState *pstate;
30         List       *groupClauses;
31         bool            have_non_var_grouping;
32         int                     sublevels_up;
33 } check_ungrouped_columns_context;
34
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);
39
40
41 /*
42  * transformAggregateCall -
43  *              Finish initial transformation of an aggregate call
44  *
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
49  * pstate level.
50  */
51 void
52 transformAggregateCall(ParseState *pstate, Aggref *agg)
53 {
54         int                     min_varlevel;
55
56         /*
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.
60          */
61         min_varlevel = find_minimum_var_level((Node *) agg->args);
62
63         /*
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.
67          */
68         if (min_varlevel == 0)
69         {
70                 if (checkExprHasAggs((Node *) agg->args))
71                         ereport(ERROR,
72                                         (errcode(ERRCODE_GROUPING_ERROR),
73                                          errmsg("aggregate function calls cannot be nested")));
74         }
75
76         if (min_varlevel < 0)
77                 min_varlevel = 0;
78         agg->agglevelsup = min_varlevel;
79
80         /* Mark the correct pstate as having aggregates */
81         while (min_varlevel-- > 0)
82                 pstate = pstate->parentParseState;
83         pstate->p_hasAggs = true;
84 }
85
86
87 /*
88  * parseCheckAggregates
89  *      Check for aggregates where they shouldn't be and improper grouping.
90  *
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.
95  */
96 void
97 parseCheckAggregates(ParseState *pstate, Query *qry)
98 {
99         List       *groupClauses = NIL;
100         bool            have_non_var_grouping;
101         ListCell   *l;
102         bool            hasJoinRTEs;
103         PlannerInfo *root;
104         Node       *clause;
105
106         /* This should only be called if we found aggregates or grouping */
107         Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
108
109         /*
110          * Aggregates must never appear in WHERE or JOIN/ON clauses.
111          *
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.)
116          */
117         if (checkExprHasAggs(qry->jointree->quals))
118                 ereport(ERROR,
119                                 (errcode(ERRCODE_GROUPING_ERROR),
120                                  errmsg("aggregates not allowed in WHERE clause")));
121         if (checkExprHasAggs((Node *) qry->jointree->fromlist))
122                 ereport(ERROR,
123                                 (errcode(ERRCODE_GROUPING_ERROR),
124                                  errmsg("aggregates not allowed in JOIN conditions")));
125
126         /*
127          * No aggregates allowed in GROUP BY clauses, either.
128          *
129          * While we are at it, build a list of the acceptable GROUP BY expressions
130          * for use by check_ungrouped_columns().
131          */
132         foreach(l, qry->groupClause)
133         {
134                 GroupClause *grpcl = (GroupClause *) lfirst(l);
135                 Node       *expr;
136
137                 expr = get_sortgroupclause_expr(grpcl, qry->targetList);
138                 if (expr == NULL)
139                         continue;                       /* probably cannot happen */
140                 if (checkExprHasAggs(expr))
141                         ereport(ERROR,
142                                         (errcode(ERRCODE_GROUPING_ERROR),
143                                          errmsg("aggregates not allowed in GROUP BY clause")));
144                 groupClauses = lcons(expr, groupClauses);
145         }
146
147         /*
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.
152          */
153         hasJoinRTEs = false;
154         foreach(l, pstate->p_rtable)
155         {
156                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
157
158                 if (rte->rtekind == RTE_JOIN)
159                 {
160                         hasJoinRTEs = true;
161                         break;
162                 }
163         }
164
165         /*
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
168          * mostly dummy.
169          */
170         if (hasJoinRTEs)
171         {
172                 root = makeNode(PlannerInfo);
173                 root->parse = qry;
174                 root->planner_cxt = CurrentMemoryContext;
175                 root->hasJoinRTEs = true;
176
177                 groupClauses = (List *) flatten_join_alias_vars(root,
178                                                                                                           (Node *) groupClauses);
179         }
180         else
181                 root = NULL;                    /* keep compiler quiet */
182
183         /*
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.)
187          */
188         have_non_var_grouping = false;
189         foreach(l, groupClauses)
190         {
191                 if (!IsA((Node *) lfirst(l), Var))
192                 {
193                         have_non_var_grouping = true;
194                         break;
195                 }
196         }
197
198         /*
199          * Check the targetlist and HAVING clause for ungrouped variables.
200          */
201         clause = (Node *) qry->targetList;
202         if (hasJoinRTEs)
203                 clause = flatten_join_alias_vars(root, clause);
204         check_ungrouped_columns(clause, pstate,
205                                                         groupClauses, have_non_var_grouping);
206
207         clause = (Node *) qry->havingQual;
208         if (hasJoinRTEs)
209                 clause = flatten_join_alias_vars(root, clause);
210         check_ungrouped_columns(clause, pstate,
211                                                         groupClauses, have_non_var_grouping);
212 }
213
214
215 /*
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
220  *        if any are found.
221  *
222  * NOTE: we assume that the given clause has been transformed suitably for
223  * parser output.  This means we can use expression_tree_walker.
224  *
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:
228  *              SELECT
229  *                      (SELECT x FROM bar where y = (foo.a + foo.b))
230  *              FROM foo
231  *              GROUP BY a + 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.
235  */
236 static void
237 check_ungrouped_columns(Node *node, ParseState *pstate,
238                                                 List *groupClauses, bool have_non_var_grouping)
239 {
240         check_ungrouped_columns_context context;
241
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);
247 }
248
249 static bool
250 check_ungrouped_columns_walker(Node *node,
251                                                            check_ungrouped_columns_context *context)
252 {
253         ListCell   *gl;
254
255         if (node == NULL)
256                 return false;
257         if (IsA(node, Const) ||
258                 IsA(node, Param))
259                 return false;                   /* constants are always acceptable */
260
261         /*
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.
268          */
269         if (IsA(node, Aggref) &&
270                 (int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
271                 return false;
272
273         /*
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.
279          */
280         if (context->have_non_var_grouping && context->sublevels_up == 0)
281         {
282                 foreach(gl, context->groupClauses)
283                 {
284                         if (equal(node, lfirst(gl)))
285                                 return false;   /* acceptable, do not descend more */
286                 }
287         }
288
289         /*
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...)
294          */
295         if (IsA(node, Var))
296         {
297                 Var                *var = (Var *) node;
298                 RangeTblEntry *rte;
299                 char       *attname;
300
301                 if (var->varlevelsup != context->sublevels_up)
302                         return false;           /* it's not local to my query, ignore */
303
304                 /*
305                  * Check for a match, if we didn't do it above.
306                  */
307                 if (!context->have_non_var_grouping || context->sublevels_up != 0)
308                 {
309                         foreach(gl, context->groupClauses)
310                         {
311                                 Var                *gvar = (Var *) lfirst(gl);
312
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 */
318                         }
319                 }
320
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)
327                         ereport(ERROR,
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)));
331                 else
332                         ereport(ERROR,
333                                         (errcode(ERRCODE_GROUPING_ERROR),
334                                          errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
335                                                         rte->eref->aliasname, attname)));
336
337         }
338
339         if (IsA(node, Query))
340         {
341                 /* Recurse into subselects */
342                 bool            result;
343
344                 context->sublevels_up++;
345                 result = query_tree_walker((Query *) node,
346                                                                    check_ungrouped_columns_walker,
347                                                                    (void *) context,
348                                                                    0);
349                 context->sublevels_up--;
350                 return result;
351         }
352         return expression_tree_walker(node, check_ungrouped_columns_walker,
353                                                                   (void *) context);
354 }
355
356 /*
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.)
363  *
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).
367  *
368  * transfn_oid and finalfn_oid identify the funcs to be called; the latter
369  * may be InvalidOid.
370  *
371  * Pointers to the constructed trees are returned into *transfnexpr and
372  * *finalfnexpr.  The latter is set to NULL if there's no finalfn.
373  */
374 void
375 build_aggregate_fnexprs(Oid *agg_input_types,
376                                                 int agg_num_inputs,
377                                                 Oid agg_state_type,
378                                                 Oid agg_result_type,
379                                                 Oid transfn_oid,
380                                                 Oid finalfn_oid,
381                                                 Expr **transfnexpr,
382                                                 Expr **finalfnexpr)
383 {
384         Param      *argp;
385         List       *args;
386         int                     i;
387
388         /*
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.
393          */
394         argp = makeNode(Param);
395         argp->paramkind = PARAM_EXEC;
396         argp->paramid = -1;
397         argp->paramtype = agg_state_type;
398         argp->paramtypmod = -1;
399
400         args = list_make1(argp);
401
402         for (i = 0; i < agg_num_inputs; i++)
403         {
404                 argp = makeNode(Param);
405                 argp->paramkind = PARAM_EXEC;
406                 argp->paramid = -1;
407                 argp->paramtype = agg_input_types[i];
408                 argp->paramtypmod = -1;
409                 args = lappend(args, argp);
410         }
411
412         *transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
413                                                                                  agg_state_type,
414                                                                                  args,
415                                                                                  COERCE_DONTCARE);
416
417         /* see if we have a final function */
418         if (!OidIsValid(finalfn_oid))
419         {
420                 *finalfnexpr = NULL;
421                 return;
422         }
423
424         /*
425          * Build expr tree for final function
426          */
427         argp = makeNode(Param);
428         argp->paramkind = PARAM_EXEC;
429         argp->paramid = -1;
430         argp->paramtype = agg_state_type;
431         argp->paramtypmod = -1;
432         args = list_make1(argp);
433
434         *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
435                                                                                  agg_result_type,
436                                                                                  args,
437                                                                                  COERCE_DONTCARE);
438 }