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