1997-11-25 22:07:18 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* parse_agg.c
|
1997-11-25 22:07:18 +00:00
|
|
|
* handle aggregates in parser
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-10-07 04:23:24 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.29 1999/10/07 04:23:12 tgl Exp $
|
1997-11-25 22:07:18 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "catalog/pg_aggregate.h"
|
|
|
|
#include "optimizer/clauses.h"
|
1999-08-16 02:10:13 +00:00
|
|
|
#include "optimizer/tlist.h"
|
1997-11-25 22:07:18 +00:00
|
|
|
#include "parser/parse_agg.h"
|
1998-12-08 06:19:15 +00:00
|
|
|
#include "parser/parse_coerce.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "parser/parse_expr.h"
|
1998-01-04 04:31:43 +00:00
|
|
|
#include "utils/lsyscache.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "utils/syscache.h"
|
1997-11-25 22:07:18 +00:00
|
|
|
|
1997-11-26 03:43:18 +00:00
|
|
|
static bool contain_agg_clause(Node *clause);
|
1999-06-19 03:48:31 +00:00
|
|
|
static bool contain_agg_clause_walker(Node *node, void *context);
|
|
|
|
static bool exprIsAggOrGroupCol(Node *expr, List *groupClauses);
|
|
|
|
static bool exprIsAggOrGroupCol_walker(Node *node, List *groupClauses);
|
1997-11-26 03:43:18 +00:00
|
|
|
|
1997-11-25 22:07:18 +00:00
|
|
|
/*
|
1999-02-13 23:22:53 +00:00
|
|
|
* contain_agg_clause
|
1999-06-19 03:48:31 +00:00
|
|
|
* Recursively find aggref nodes within a clause.
|
1997-11-25 22:07:18 +00:00
|
|
|
*
|
|
|
|
* Returns true if any aggregate found.
|
1999-06-19 03:48:31 +00:00
|
|
|
*
|
|
|
|
* NOTE: we assume that the given clause has been transformed suitably for
|
1999-06-21 01:18:02 +00:00
|
|
|
* parser output. This means we can use the planner's expression_tree_walker.
|
1997-11-25 22:07:18 +00:00
|
|
|
*/
|
1997-11-26 03:43:18 +00:00
|
|
|
static bool
|
1997-11-25 22:07:18 +00:00
|
|
|
contain_agg_clause(Node *clause)
|
|
|
|
{
|
1999-06-19 03:48:31 +00:00
|
|
|
return contain_agg_clause_walker(clause, NULL);
|
|
|
|
}
|
1997-11-25 22:07:18 +00:00
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
static bool
|
|
|
|
contain_agg_clause_walker(Node *node, void *context)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Aggref))
|
|
|
|
return true; /* abort the tree traversal and return true */
|
|
|
|
return expression_tree_walker(node, contain_agg_clause_walker, context);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exprIsAggOrGroupCol -
|
1999-06-19 03:48:31 +00:00
|
|
|
* returns true if the expression does not contain non-group columns,
|
|
|
|
* other than within the arguments of aggregate functions.
|
|
|
|
*
|
|
|
|
* NOTE: we assume that the given clause has been transformed suitably for
|
1999-06-21 01:18:02 +00:00
|
|
|
* parser output. This means we can use the planner's expression_tree_walker.
|
1999-06-19 03:48:31 +00:00
|
|
|
*
|
1999-06-21 01:18:02 +00: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
|
|
|
|
* that appear as outer-level variables within a subquery. That case seems
|
|
|
|
* 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 22:07:18 +00:00
|
|
|
*/
|
1997-11-26 03:43:18 +00:00
|
|
|
static bool
|
1999-06-19 03:48:31 +00:00
|
|
|
exprIsAggOrGroupCol(Node *expr, List *groupClauses)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
1999-06-19 03:48:31 +00:00
|
|
|
/* My walker returns TRUE if it finds a subexpression that is NOT
|
|
|
|
* acceptable (since we can abort the recursion at that point).
|
|
|
|
* So, invert its result.
|
|
|
|
*/
|
|
|
|
return ! exprIsAggOrGroupCol_walker(expr, groupClauses);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
1997-11-26 03:43:18 +00:00
|
|
|
static bool
|
1999-06-19 03:48:31 +00:00
|
|
|
exprIsAggOrGroupCol_walker(Node *node, List *groupClauses)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
|
|
|
List *gl;
|
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Aggref))
|
|
|
|
return false; /* OK; do not examine argument of aggregate */
|
|
|
|
if (IsA(node, Const) || IsA(node, Param))
|
|
|
|
return false; /* constants are always acceptable */
|
|
|
|
/* Now check to see if expression as a whole matches any GROUP BY item.
|
|
|
|
* We need to do this at every recursion level so that we recognize
|
|
|
|
* GROUPed-BY expressions.
|
|
|
|
*/
|
|
|
|
foreach(gl, groupClauses)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
1999-06-19 03:48:31 +00:00
|
|
|
if (equal(node, lfirst(gl)))
|
|
|
|
return false; /* acceptable, do not descend more */
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
1999-06-19 03:48:31 +00:00
|
|
|
/* If we have an ungrouped Var, we have a failure --- unless it is an
|
|
|
|
* outer-level Var. In that case it's a constant as far as this query
|
|
|
|
* 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...)
|
|
|
|
*/
|
|
|
|
if (IsA(node, Var))
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
1999-06-19 03:48:31 +00:00
|
|
|
if (((Var *) node)->varlevelsup == 0)
|
|
|
|
return true; /* found an ungrouped local variable */
|
|
|
|
return false; /* outer-level Var is acceptable */
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
1999-06-19 03:48:31 +00:00
|
|
|
/* Otherwise, recurse. */
|
|
|
|
return expression_tree_walker(node, exprIsAggOrGroupCol_walker,
|
|
|
|
(void *) groupClauses);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-23 21:41:14 +00: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
|
|
|
|
* qualifications are finalized.
|
1997-11-25 22:07:18 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
parseCheckAggregates(ParseState *pstate, Query *qry)
|
|
|
|
{
|
1999-06-19 03:48:31 +00:00
|
|
|
List *groupClauses = NIL;
|
1997-11-25 22:07:18 +00:00
|
|
|
List *tl;
|
|
|
|
|
1999-10-07 04:23:24 +00:00
|
|
|
/* This should only be called if we found aggregates, GROUP, or HAVING */
|
|
|
|
Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
|
1997-11-25 22:07:18 +00:00
|
|
|
|
|
|
|
/*
|
1999-05-25 16:15:34 +00:00
|
|
|
* Aggregates must never appear in WHERE clauses. (Note this check
|
|
|
|
* should appear first to deliver an appropriate error message;
|
|
|
|
* otherwise we are likely to generate the generic "illegal use of
|
|
|
|
* aggregates in target list" message, which is outright misleading if
|
|
|
|
* the problem is in WHERE.)
|
1997-11-25 22:07:18 +00:00
|
|
|
*/
|
|
|
|
if (contain_agg_clause(qry->qual))
|
1998-12-08 06:19:15 +00:00
|
|
|
elog(ERROR, "Aggregates not allowed in WHERE clause");
|
1997-11-25 22:07:18 +00:00
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
/*
|
|
|
|
* No aggregates allowed in GROUP BY clauses, either.
|
|
|
|
*
|
|
|
|
* While we are at it, build a list of the acceptable GROUP BY expressions
|
|
|
|
* for use by exprIsAggOrGroupCol() (this avoids repeated scans of the
|
|
|
|
* targetlist within the recursive routines...)
|
|
|
|
*/
|
|
|
|
foreach(tl, qry->groupClause)
|
|
|
|
{
|
|
|
|
GroupClause *grpcl = lfirst(tl);
|
|
|
|
Node *expr;
|
|
|
|
|
1999-08-21 03:49:17 +00:00
|
|
|
expr = get_sortgroupclause_expr(grpcl, qry->targetList);
|
1999-06-19 03:48:31 +00:00
|
|
|
if (contain_agg_clause(expr))
|
|
|
|
elog(ERROR, "Aggregates not allowed in GROUP BY clause");
|
|
|
|
groupClauses = lcons(expr, groupClauses);
|
|
|
|
}
|
|
|
|
|
1999-10-07 04:23:24 +00:00
|
|
|
/*
|
|
|
|
* The expression specified in the HAVING clause can only contain
|
|
|
|
* aggregates, group columns and functions thereof. As with WHERE,
|
|
|
|
* we want to point the finger at HAVING before the target list.
|
|
|
|
*/
|
|
|
|
if (!exprIsAggOrGroupCol(qry->havingQual, groupClauses))
|
|
|
|
elog(ERROR,
|
|
|
|
"Illegal use of aggregates or non-group column in HAVING clause");
|
|
|
|
|
1997-11-25 22:07:18 +00:00
|
|
|
/*
|
1999-05-23 21:41:14 +00:00
|
|
|
* The target list can only contain aggregates, group columns and
|
1997-11-25 22:07:18 +00:00
|
|
|
* functions thereof.
|
|
|
|
*/
|
|
|
|
foreach(tl, qry->targetList)
|
|
|
|
{
|
|
|
|
TargetEntry *tle = lfirst(tl);
|
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
if (!exprIsAggOrGroupCol(tle->expr, groupClauses))
|
1998-01-05 03:35:55 +00:00
|
|
|
elog(ERROR,
|
1998-12-08 06:19:15 +00:00
|
|
|
"Illegal use of aggregates or non-group column in target list");
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
/* Release the list storage (but not the pointed-to expressions!) */
|
|
|
|
freeList(groupClauses);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-24 00:28:37 +00:00
|
|
|
Aggref *
|
1998-01-04 04:31:43 +00:00
|
|
|
ParseAgg(ParseState *pstate, char *aggname, Oid basetype,
|
1998-02-26 04:46:47 +00:00
|
|
|
List *target, int precedence)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
1999-10-07 04:23:24 +00:00
|
|
|
HeapTuple theAggTuple;
|
|
|
|
Form_pg_aggregate aggform;
|
1997-11-25 22:07:18 +00:00
|
|
|
Oid fintype;
|
|
|
|
Oid xfn1;
|
1999-10-07 04:23:24 +00:00
|
|
|
Oid vartype;
|
1999-01-24 00:28:37 +00:00
|
|
|
Aggref *aggref;
|
1998-01-04 04:31:43 +00:00
|
|
|
bool usenulls = false;
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1998-08-19 02:04:17 +00:00
|
|
|
theAggTuple = SearchSysCacheTuple(AGGNAME,
|
|
|
|
PointerGetDatum(aggname),
|
1997-11-25 22:07:18 +00:00
|
|
|
ObjectIdGetDatum(basetype),
|
|
|
|
0, 0);
|
|
|
|
if (!HeapTupleIsValid(theAggTuple))
|
1998-12-08 06:19:15 +00:00
|
|
|
elog(ERROR, "Aggregate %s does not exist", aggname);
|
1997-11-25 22:07:18 +00:00
|
|
|
|
1998-01-04 04:31:43 +00:00
|
|
|
/*
|
1999-10-07 04:23:24 +00:00
|
|
|
* There used to be a really ugly hack for count(*) here.
|
1998-01-04 04:31:43 +00:00
|
|
|
*
|
1999-10-07 04:23:24 +00: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 05:05:08 +00:00
|
|
|
*
|
1999-10-07 04:23:24 +00:00
|
|
|
* Since "1" never evaluates as null, we currently have no need of
|
|
|
|
* the "usenulls" flag, but it should be kept around; in fact, we should
|
|
|
|
* extend the pg_aggregate table to let usenulls be specified as an
|
|
|
|
* attribute of user-defined aggregates.
|
1998-01-20 05:05:08 +00:00
|
|
|
*/
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1997-11-25 22:07:18 +00:00
|
|
|
aggform = (Form_pg_aggregate) GETSTRUCT(theAggTuple);
|
|
|
|
fintype = aggform->aggfinaltype;
|
|
|
|
xfn1 = aggform->aggtransfn1;
|
|
|
|
|
|
|
|
/* only aggregates with transfn1 need a base type */
|
|
|
|
if (OidIsValid(xfn1))
|
|
|
|
{
|
|
|
|
basetype = aggform->aggbasetype;
|
1999-04-29 01:13:13 +00:00
|
|
|
vartype = exprType(lfirst(target));
|
1998-12-08 06:19:15 +00:00
|
|
|
if ((basetype != vartype)
|
1999-05-25 16:15:34 +00:00
|
|
|
&& (!IS_BINARY_COMPATIBLE(basetype, vartype)))
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
|
|
|
Type tp1,
|
|
|
|
tp2;
|
|
|
|
|
|
|
|
tp1 = typeidType(basetype);
|
|
|
|
tp2 = typeidType(vartype);
|
1998-12-08 06:19:15 +00:00
|
|
|
elog(ERROR, "Aggregate type mismatch"
|
1999-05-25 16:15:34 +00:00
|
|
|
"\n\t%s() works on %s, not on %s",
|
|
|
|
aggname, typeTypeName(tp1), typeTypeName(tp2));
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-24 00:28:37 +00:00
|
|
|
aggref = makeNode(Aggref);
|
|
|
|
aggref->aggname = pstrdup(aggname);
|
|
|
|
aggref->basetype = aggform->aggbasetype;
|
|
|
|
aggref->aggtype = fintype;
|
|
|
|
aggref->target = lfirst(target);
|
1999-08-21 03:49:17 +00:00
|
|
|
aggref->usenulls = usenulls;
|
1998-09-01 04:40:42 +00:00
|
|
|
|
1998-01-15 19:00:16 +00:00
|
|
|
pstate->p_hasAggs = true;
|
|
|
|
|
1999-01-24 00:28:37 +00:00
|
|
|
return aggref;
|
1997-11-25 22:07:18 +00: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)
|
1998-01-05 03:35:55 +00:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for all types does not exist", caller, aggname);
|
1997-11-25 22:07:18 +00:00
|
|
|
else
|
|
|
|
{
|
1998-01-05 03:35:55 +00:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for '%s' does not exist", caller, aggname,
|
1997-11-25 22:07:18 +00:00
|
|
|
typeidTypeName(basetypeID));
|
|
|
|
}
|
|
|
|
}
|