1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-12 21:01:52 +03:00

Fix assorted missing logic for GroupingFunc nodes.

The planner needs to treat GroupingFunc like Aggref for many purposes,
in particular with respect to processing of the argument expressions,
which are not to be evaluated at runtime.  A few places hadn't gotten
that memo, notably including subselect.c's processing of outer-level
aggregates.  This resulted in assertion failures or wrong plans for
cases in which a GROUPING() construct references an outer aggregation
level.

Also fix missing special cases for GroupingFunc in cost_qual_eval
(resulting in wrong cost estimates for GROUPING(), although it's
not clear that that would affect plan shapes in practice) and in
ruleutils.c (resulting in excess parentheses in pretty-print mode).

Per bug #17088 from Yaoguang Chen.  Back-patch to all supported
branches.

Richard Guo, Tom Lane

Discussion: https://postgr.es/m/17088-e33882b387de7f5c@postgresql.org
This commit is contained in:
Tom Lane
2022-03-21 17:44:29 -04:00
parent d8d378d510
commit 69c88e2fb0
6 changed files with 88 additions and 14 deletions

View File

@ -7466,12 +7466,13 @@ get_parameter(Param *param, deparse_context *context)
context->varprefix = true;
/*
* A Param's expansion is typically a Var, Aggref, or upper-level
* Param, which wouldn't need extra parentheses. Otherwise, insert
* parens to ensure the expression looks atomic.
* A Param's expansion is typically a Var, Aggref, GroupingFunc, or
* upper-level Param, which wouldn't need extra parentheses.
* Otherwise, insert parens to ensure the expression looks atomic.
*/
need_paren = !(IsA(expr, Var) ||
IsA(expr, Aggref) ||
IsA(expr, GroupingFunc) ||
IsA(expr, Param));
if (need_paren)
appendStringInfoChar(context->buf, '(');
@ -7553,6 +7554,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
case T_NextValueExpr:
case T_NullIfExpr:
case T_Aggref:
case T_GroupingFunc:
case T_WindowFunc:
case T_FuncExpr:
/* function-like: name(..) or name[..] */
@ -7669,6 +7671,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
case T_XmlExpr: /* own parentheses */
case T_NullIfExpr: /* other separators */
case T_Aggref: /* own parentheses */
case T_GroupingFunc: /* own parentheses */
case T_WindowFunc: /* own parentheses */
case T_CaseExpr: /* other separators */
return true;
@ -7719,6 +7722,7 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
case T_XmlExpr: /* own parentheses */
case T_NullIfExpr: /* other separators */
case T_Aggref: /* own parentheses */
case T_GroupingFunc: /* own parentheses */
case T_WindowFunc: /* own parentheses */
case T_CaseExpr: /* other separators */
return true;