Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit505b518

Browse files
committed
Detect case of invalid use of GROUP BY when there are no
aggregate functions, as inselect a, b from foo group by a;The ungrouped reference to b is not kosher, but formerly we neglected tocheck this unless there was an aggregate function somewhere in the query.
1 parent57455fc commit505b518

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

‎src/backend/parser/analyze.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: analyze.c,v 1.106 1999/05/17 17:03:27 momjian Exp $
8+
* $Id: analyze.c,v 1.107 1999/05/23 21:41:14 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -373,7 +373,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
373373
qry->uniqueFlag);
374374

375375
qry->hasAggs=pstate->p_hasAggs;
376-
if (pstate->p_hasAggs)
376+
if (pstate->p_hasAggs||qry->groupClause)
377377
parseCheckAggregates(pstate,qry);
378378

379379
/*
@@ -997,7 +997,7 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
997997
qry->rtable=pstate->p_rtable;
998998

999999
qry->hasAggs=pstate->p_hasAggs;
1000-
if (pstate->p_hasAggs)
1000+
if (pstate->p_hasAggs||qry->groupClause)
10011001
parseCheckAggregates(pstate,qry);
10021002

10031003
/*

‎src/backend/parser/parse_agg.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.19 1999/05/12 15:01:48 wieck Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.20 1999/05/23 21:41:14 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -173,28 +173,34 @@ tleIsAggOrGroupCol(TargetEntry *tle, List *groupClause, List *tlist)
173173
}
174174

175175
/*
176-
* parseCheckAggregates -
177-
* this should really be done earlier but the current grammar
178-
* cannot differentiate functions from aggregates. So we have do check
179-
* here when the target list and the qualifications are finalized.
176+
* parseCheckAggregates
177+
*Check for aggregates where they shouldn't be and improper grouping.
178+
*
179+
*Ideally this should be done earlier, but it's difficult to distinguish
180+
*aggregates from plain functions at the grammar level. So instead we
181+
*check here. This function should be called after the target list and
182+
*qualifications are finalized.
180183
*/
181184
void
182185
parseCheckAggregates(ParseState*pstate,Query*qry)
183186
{
184187
List*tl;
185188

186-
Assert(pstate->p_hasAggs);
189+
/* This should only be called if we found aggregates or grouping */
190+
Assert(pstate->p_hasAggs||qry->groupClause);
187191

188192
/*
189-
* aggregates never appear in WHERE clauses. (we have to check where
190-
* clause first because if there is an aggregate, the check for
191-
* non-group column in target list may fail.)
193+
* Aggregates must never appear in WHERE clauses.
194+
* (Note this check should appear first to deliver an appropriate
195+
* error message; otherwise we are likely to generate the generic
196+
* "illegal use of aggregates in target list" message, which is
197+
* outright misleading if the problem is in WHERE.)
192198
*/
193199
if (contain_agg_clause(qry->qual))
194200
elog(ERROR,"Aggregates not allowed in WHERE clause");
195201

196202
/*
197-
*the target list can only contain aggregates, group columns and
203+
*The target list can only contain aggregates, group columns and
198204
* functions thereof.
199205
*/
200206
foreach(tl,qry->targetList)
@@ -214,7 +220,6 @@ parseCheckAggregates(ParseState *pstate, Query *qry)
214220
if (!exprIsAggOrGroupCol(qry->havingQual,qry->groupClause,qry->targetList))
215221
elog(ERROR,
216222
"Illegal use of aggregates or non-group column in HAVING clause");
217-
return;
218223
}
219224

220225

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp