Skip to content

Commit 31a0f1d

Browse files
committed
Fix oversight in check_ungrouped_columns optimization that avoids
unnecessary checks for complex grouping expressions: we cannot check whether the expressions are simple Vars until after we apply flatten_join_alias_vars, because in the case of FULL JOIN that routine can introduce non-Var expressions. Per example from Joel Knight.
1 parent 4405b3e commit 31a0f1d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/backend/parser/parse_agg.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.60 2003/11/29 19:51:51 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_agg.c,v 1.61 2004/01/28 07:46:44 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -98,7 +98,7 @@ void
9898
parseCheckAggregates(ParseState *pstate, Query *qry)
9999
{
100100
List *groupClauses = NIL;
101-
bool have_non_var_grouping = false;
101+
bool have_non_var_grouping;
102102
List *lst;
103103
bool hasJoinRTEs;
104104
Node *clause;
@@ -127,9 +127,7 @@ parseCheckAggregates(ParseState *pstate, Query *qry)
127127
* No aggregates allowed in GROUP BY clauses, either.
128128
*
129129
* While we are at it, build a list of the acceptable GROUP BY
130-
* expressions for use by check_ungrouped_columns() (this avoids
131-
* repeated scans of the targetlist within the recursive routine...).
132-
* And detect whether any of the expressions aren't simple Vars.
130+
* expressions for use by check_ungrouped_columns().
133131
*/
134132
foreach(lst, qry->groupClause)
135133
{
@@ -144,8 +142,6 @@ parseCheckAggregates(ParseState *pstate, Query *qry)
144142
(errcode(ERRCODE_GROUPING_ERROR),
145143
errmsg("aggregates not allowed in GROUP BY clause")));
146144
groupClauses = lcons(expr, groupClauses);
147-
if (!IsA(expr, Var))
148-
have_non_var_grouping = true;
149145
}
150146

151147
/*
@@ -170,6 +166,21 @@ parseCheckAggregates(ParseState *pstate, Query *qry)
170166
groupClauses = (List *) flatten_join_alias_vars(qry,
171167
(Node *) groupClauses);
172168

169+
/*
170+
* Detect whether any of the grouping expressions aren't simple Vars;
171+
* if they're all Vars then we don't have to work so hard in the
172+
* recursive scans. (Note we have to flatten aliases before this.)
173+
*/
174+
have_non_var_grouping = false;
175+
foreach(lst, groupClauses)
176+
{
177+
if (!IsA((Node *) lfirst(lst), Var))
178+
{
179+
have_non_var_grouping = true;
180+
break;
181+
}
182+
}
183+
173184
/*
174185
* Check the targetlist and HAVING clause for ungrouped variables.
175186
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy