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 */
181184void
182185parseCheckAggregates (ParseState * pstate ,Query * qry )
183186{
184187List * 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 */
193199if (contain_agg_clause (qry -> qual ))
194200elog (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 */
200206foreach (tl ,qry -> targetList )
@@ -214,7 +220,6 @@ parseCheckAggregates(ParseState *pstate, Query *qry)
214220if (!exprIsAggOrGroupCol (qry -> havingQual ,qry -> groupClause ,qry -> targetList ))
215221elog (ERROR ,
216222"Illegal use of aggregates or non-group column in HAVING clause" );
217- return ;
218223}
219224
220225