1997-11-25 23:07:18 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* parse_agg.c
|
1997-11-25 23:07:18 +01:00
|
|
|
* handle aggregates in parser
|
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-10-25 07:50:21 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.46 2001/10/25 05:49:36 momjian Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "catalog/pg_aggregate.h"
|
|
|
|
#include "optimizer/clauses.h"
|
1999-08-16 04:10:13 +02:00
|
|
|
#include "optimizer/tlist.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_agg.h"
|
1998-12-08 07:19:15 +01:00
|
|
|
#include "parser/parse_coerce.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_expr.h"
|
1999-12-09 06:58:56 +01:00
|
|
|
#include "parser/parsetree.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "parser/parse_type.h"
|
2001-08-09 20:28:18 +02:00
|
|
|
#include "utils/builtins.h"
|
1998-01-04 05:31:43 +01:00
|
|
|
#include "utils/lsyscache.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/syscache.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
1999-12-09 06:58:56 +01:00
|
|
|
ParseState *pstate;
|
|
|
|
List *groupClauses;
|
|
|
|
} check_ungrouped_columns_context;
|
|
|
|
|
|
|
|
static void check_ungrouped_columns(Node *node, ParseState *pstate,
|
2000-04-12 19:17:23 +02:00
|
|
|
List *groupClauses);
|
1999-12-09 06:58:56 +01:00
|
|
|
static bool check_ungrouped_columns_walker(Node *node,
|
2000-04-12 19:17:23 +02:00
|
|
|
check_ungrouped_columns_context *context);
|
1997-11-26 04:43:18 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
1999-12-09 06:58:56 +01:00
|
|
|
* check_ungrouped_columns -
|
|
|
|
* Scan the given expression tree for ungrouped variables (variables
|
|
|
|
* that are not listed in the groupClauses list and are not within
|
|
|
|
* the arguments of aggregate functions). Emit a suitable error message
|
|
|
|
* if any are found.
|
1999-06-19 05:48:31 +02:00
|
|
|
*
|
|
|
|
* NOTE: we assume that the given clause has been transformed suitably for
|
1999-06-21 03:18:02 +02:00
|
|
|
* parser output. This means we can use the planner's expression_tree_walker.
|
1999-06-19 05:48:31 +02:00
|
|
|
*
|
1999-06-21 03:18:02 +02:00
|
|
|
* NOTE: in the case of a SubLink, expression_tree_walker does not descend
|
|
|
|
* into the subquery. This means we will fail to detect ungrouped columns
|
2000-04-12 19:17:23 +02:00
|
|
|
* that appear as outer-level variables within a subquery. That case seems
|
1999-06-21 03:18:02 +02:00
|
|
|
* unreasonably hard to handle here. Instead, we expect the planner to check
|
|
|
|
* for ungrouped columns after it's found all the outer-level references
|
|
|
|
* inside the subquery and converted them into a list of parameters for the
|
|
|
|
* subquery.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1999-12-09 06:58:56 +01:00
|
|
|
static void
|
|
|
|
check_ungrouped_columns(Node *node, ParseState *pstate,
|
|
|
|
List *groupClauses)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
check_ungrouped_columns_context context;
|
1999-12-09 06:58:56 +01:00
|
|
|
|
|
|
|
context.pstate = pstate;
|
|
|
|
context.groupClauses = groupClauses;
|
|
|
|
check_ungrouped_columns_walker(node, &context);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
1997-11-26 04:43:18 +01:00
|
|
|
static bool
|
1999-12-09 06:58:56 +01:00
|
|
|
check_ungrouped_columns_walker(Node *node,
|
|
|
|
check_ungrouped_columns_context *context)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
List *gl;
|
|
|
|
|
1999-06-19 05:48:31 +02:00
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
2000-04-12 19:17:23 +02:00
|
|
|
if (IsA(node, Const) ||IsA(node, Param))
|
1999-06-19 05:48:31 +02:00
|
|
|
return false; /* constants are always acceptable */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-12-09 06:58:56 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* If we find an aggregate function, do not recurse into its
|
|
|
|
* arguments.
|
1999-12-09 06:58:56 +01:00
|
|
|
*/
|
|
|
|
if (IsA(node, Aggref))
|
|
|
|
return false;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-12-09 06:58:56 +01:00
|
|
|
/*
|
|
|
|
* Check to see if subexpression as a whole matches any GROUP BY item.
|
1999-06-19 05:48:31 +02:00
|
|
|
* We need to do this at every recursion level so that we recognize
|
1999-12-09 06:58:56 +01:00
|
|
|
* GROUPed-BY expressions before reaching variables within them.
|
1999-06-19 05:48:31 +02:00
|
|
|
*/
|
1999-12-09 06:58:56 +01:00
|
|
|
foreach(gl, context->groupClauses)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-06-19 05:48:31 +02:00
|
|
|
if (equal(node, lfirst(gl)))
|
|
|
|
return false; /* acceptable, do not descend more */
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-12-09 06:58:56 +01:00
|
|
|
/*
|
|
|
|
* If we have an ungrouped Var, we have a failure --- unless it is an
|
1999-06-19 05:48:31 +02:00
|
|
|
* outer-level Var. In that case it's a constant as far as this query
|
2000-04-12 19:17:23 +02:00
|
|
|
* level is concerned, and we can accept it. (If it's ungrouped as
|
|
|
|
* far as the upper query is concerned, that's someone else's
|
|
|
|
* problem...)
|
1999-06-19 05:48:31 +02:00
|
|
|
*/
|
|
|
|
if (IsA(node, Var))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Var *var = (Var *) node;
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
char *attname;
|
1999-12-09 06:58:56 +01:00
|
|
|
|
|
|
|
if (var->varlevelsup > 0)
|
|
|
|
return false; /* outer-level Var is acceptable */
|
|
|
|
/* Found an ungrouped local variable; generate error message */
|
|
|
|
Assert(var->varno > 0 &&
|
2000-03-17 03:36:41 +01:00
|
|
|
(int) var->varno <= length(context->pstate->p_rtable));
|
1999-12-09 06:58:56 +01:00
|
|
|
rte = rt_fetch(var->varno, context->pstate->p_rtable);
|
2000-09-25 20:14:55 +02:00
|
|
|
attname = get_rte_attribute_name(rte, var->varattno);
|
1999-12-09 06:58:56 +01:00
|
|
|
elog(ERROR, "Attribute %s.%s must be GROUPed or used in an aggregate function",
|
2000-03-15 00:06:59 +01:00
|
|
|
rte->eref->relname, attname);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
1999-06-19 05:48:31 +02:00
|
|
|
/* Otherwise, recurse. */
|
1999-12-09 06:58:56 +01:00
|
|
|
return expression_tree_walker(node, check_ungrouped_columns_walker,
|
|
|
|
(void *) context);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-23 23:41:14 +02:00
|
|
|
* parseCheckAggregates
|
|
|
|
* Check for aggregates where they shouldn't be and improper grouping.
|
|
|
|
*
|
|
|
|
* Ideally this should be done earlier, but it's difficult to distinguish
|
|
|
|
* aggregates from plain functions at the grammar level. So instead we
|
|
|
|
* check here. This function should be called after the target list and
|
2000-09-29 20:21:41 +02:00
|
|
|
* qualifications are finalized. BUT: in some cases we want to call this
|
|
|
|
* routine before we've assembled the joinlist and qual into a FromExpr.
|
|
|
|
* So, rather than looking at qry->jointree, look at pstate->p_joinlist
|
|
|
|
* and the explicitly-passed qual.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
|
|
|
void
|
2000-09-29 20:21:41 +02:00
|
|
|
parseCheckAggregates(ParseState *pstate, Query *qry, Node *qual)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-06-19 05:48:31 +02:00
|
|
|
List *groupClauses = NIL;
|
1997-11-25 23:07:18 +01:00
|
|
|
List *tl;
|
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
/* This should only be called if we found aggregates, GROUP, or HAVING */
|
|
|
|
Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* Aggregates must never appear in WHERE or JOIN/ON clauses.
|
|
|
|
*
|
|
|
|
* (Note this check should appear first to deliver an appropriate error
|
|
|
|
* message; otherwise we are likely to complain about some innocent
|
|
|
|
* variable in the target list, which is outright misleading if the
|
|
|
|
* problem is in WHERE.)
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
if (contain_agg_clause(qual))
|
1998-12-08 07:19:15 +01:00
|
|
|
elog(ERROR, "Aggregates not allowed in WHERE clause");
|
2000-09-29 20:21:41 +02:00
|
|
|
if (contain_agg_clause((Node *) pstate->p_joinlist))
|
2000-09-12 23:07:18 +02:00
|
|
|
elog(ERROR, "Aggregates not allowed in JOIN conditions");
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-06-19 05:48:31 +02:00
|
|
|
/*
|
|
|
|
* No aggregates allowed in GROUP BY clauses, either.
|
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* While we are at it, build a list of the acceptable GROUP BY
|
|
|
|
* expressions for use by check_ungrouped_columns() (this avoids
|
|
|
|
* repeated scans of the targetlist within the recursive routine...)
|
1999-06-19 05:48:31 +02:00
|
|
|
*/
|
|
|
|
foreach(tl, qry->groupClause)
|
|
|
|
{
|
|
|
|
GroupClause *grpcl = lfirst(tl);
|
2000-04-12 19:17:23 +02:00
|
|
|
Node *expr;
|
1999-06-19 05:48:31 +02:00
|
|
|
|
1999-08-21 05:49:17 +02:00
|
|
|
expr = get_sortgroupclause_expr(grpcl, qry->targetList);
|
1999-06-19 05:48:31 +02:00
|
|
|
if (contain_agg_clause(expr))
|
|
|
|
elog(ERROR, "Aggregates not allowed in GROUP BY clause");
|
|
|
|
groupClauses = lcons(expr, groupClauses);
|
|
|
|
}
|
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
/*
|
1999-12-09 06:58:56 +01:00
|
|
|
* Check the targetlist and HAVING clause for ungrouped variables.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1999-12-09 06:58:56 +01:00
|
|
|
check_ungrouped_columns((Node *) qry->targetList, pstate, groupClauses);
|
|
|
|
check_ungrouped_columns((Node *) qry->havingQual, pstate, groupClauses);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-06-19 05:48:31 +02:00
|
|
|
/* Release the list storage (but not the pointed-to expressions!) */
|
|
|
|
freeList(groupClauses);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-24 01:28:37 +01:00
|
|
|
Aggref *
|
1998-01-04 05:31:43 +01:00
|
|
|
ParseAgg(ParseState *pstate, char *aggname, Oid basetype,
|
1999-12-10 08:37:35 +01:00
|
|
|
List *args, bool agg_star, bool agg_distinct,
|
|
|
|
int precedence)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
HeapTuple aggtuple;
|
1999-10-07 06:23:24 +02:00
|
|
|
Form_pg_aggregate aggform;
|
1999-01-24 01:28:37 +01:00
|
|
|
Aggref *aggref;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
aggtuple = SearchSysCache(AGGNAME,
|
|
|
|
PointerGetDatum(aggname),
|
|
|
|
ObjectIdGetDatum(basetype),
|
|
|
|
0, 0);
|
2000-07-17 05:05:41 +02:00
|
|
|
/* shouldn't happen --- caller should have checked already */
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!HeapTupleIsValid(aggtuple))
|
2000-07-17 05:05:41 +02:00
|
|
|
agg_error("ParseAgg", aggname, basetype);
|
2000-11-16 23:30:52 +01:00
|
|
|
aggform = (Form_pg_aggregate) GETSTRUCT(aggtuple);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-01-04 05:31:43 +01:00
|
|
|
/*
|
1999-10-07 06:23:24 +02:00
|
|
|
* There used to be a really ugly hack for count(*) here.
|
1998-01-04 05:31:43 +01:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* It's gone. Now, the grammar transforms count(*) into count(1), which
|
|
|
|
* does the right thing. (It didn't use to do the right thing,
|
|
|
|
* because the optimizer had the wrong ideas about semantics of
|
|
|
|
* queries without explicit variables. Fixed as of Oct 1999 --- tgl.)
|
1998-01-20 06:05:08 +01:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* We assume caller has already checked that given args are compatible
|
|
|
|
* with the agg's basetype.
|
|
|
|
*/
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-01-24 01:28:37 +01:00
|
|
|
aggref = makeNode(Aggref);
|
|
|
|
aggref->aggname = pstrdup(aggname);
|
|
|
|
aggref->basetype = aggform->aggbasetype;
|
2000-07-17 05:05:41 +02:00
|
|
|
aggref->aggtype = aggform->aggfinaltype;
|
1999-12-10 08:37:35 +01:00
|
|
|
aggref->target = lfirst(args);
|
1999-12-13 02:27:21 +01:00
|
|
|
aggref->aggstar = agg_star;
|
|
|
|
aggref->aggdistinct = agg_distinct;
|
1999-12-10 08:37:35 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(aggtuple);
|
|
|
|
|
1998-01-15 20:00:16 +01:00
|
|
|
pstate->p_hasAggs = true;
|
|
|
|
|
1999-01-24 01:28:37 +01:00
|
|
|
return aggref;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error message when aggregate lookup fails that gives details of the
|
|
|
|
* basetype
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
agg_error(char *caller, char *aggname, Oid basetypeID)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* basetypeID that is Invalid (zero) means aggregate over all types.
|
|
|
|
* (count)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (basetypeID == InvalidOid)
|
2000-07-17 05:05:41 +02:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for all types does not exist",
|
|
|
|
caller, aggname);
|
1997-11-25 23:07:18 +01:00
|
|
|
else
|
2001-08-09 20:28:18 +02:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for type %s does not exist",
|
|
|
|
caller, aggname, format_type_be(basetypeID));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|