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
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-04-12 19:17:23 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.37 2000/04/12 17:15:26 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"
|
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);
|
|
|
|
attname = get_attname(rte->relid, var->varattno);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!attname)
|
1999-12-09 06:58:56 +01:00
|
|
|
elog(ERROR, "cache lookup of attribute %d in relation %u failed",
|
|
|
|
var->varattno, rte->relid);
|
|
|
|
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
|
|
|
|
* qualifications are finalized.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
parseCheckAggregates(ParseState *pstate, Query *qry)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* Aggregates must never appear in WHERE clauses. (Note this check
|
|
|
|
* should appear first to deliver an appropriate error message;
|
2000-04-12 19:17:23 +02:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
if (contain_agg_clause(qry->qual))
|
1998-12-08 07:19:15 +01:00
|
|
|
elog(ERROR, "Aggregates not allowed in WHERE clause");
|
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
|
|
|
{
|
1999-10-07 06:23:24 +02:00
|
|
|
HeapTuple theAggTuple;
|
|
|
|
Form_pg_aggregate aggform;
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid fintype;
|
|
|
|
Oid xfn1;
|
1999-10-07 06:23:24 +02:00
|
|
|
Oid vartype;
|
1999-01-24 01:28:37 +01:00
|
|
|
Aggref *aggref;
|
1998-01-04 05:31:43 +01:00
|
|
|
bool usenulls = false;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
theAggTuple = SearchSysCacheTuple(AGGNAME,
|
|
|
|
PointerGetDatum(aggname),
|
1997-11-25 23:07:18 +01:00
|
|
|
ObjectIdGetDatum(basetype),
|
|
|
|
0, 0);
|
|
|
|
if (!HeapTupleIsValid(theAggTuple))
|
1998-12-08 07:19:15 +01:00
|
|
|
elog(ERROR, "Aggregate %s does not exist", aggname);
|
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
|
|
|
*
|
2000-04-12 19:17:23 +02: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
|
1999-10-07 06:23:24 +02:00
|
|
|
* extend the pg_aggregate table to let usenulls be specified as an
|
2000-04-12 19:17:23 +02:00
|
|
|
* attribute of user-defined aggregates. In the meantime, usenulls is
|
|
|
|
* just always set to "false".
|
1998-01-20 06:05:08 +01:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-11-25 23:07:18 +01: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-12-10 08:37:35 +01:00
|
|
|
vartype = exprType(lfirst(args));
|
1998-12-08 07:19:15 +01:00
|
|
|
if ((basetype != vartype)
|
1999-05-25 18:15:34 +02:00
|
|
|
&& (!IS_BINARY_COMPATIBLE(basetype, vartype)))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
Type tp1,
|
|
|
|
tp2;
|
|
|
|
|
|
|
|
tp1 = typeidType(basetype);
|
|
|
|
tp2 = typeidType(vartype);
|
1998-12-08 07:19:15 +01:00
|
|
|
elog(ERROR, "Aggregate type mismatch"
|
1999-05-25 18:15:34 +02:00
|
|
|
"\n\t%s() works on %s, not on %s",
|
|
|
|
aggname, typeTypeName(tp1), typeTypeName(tp2));
|
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;
|
|
|
|
aggref->aggtype = fintype;
|
1999-12-10 08:37:35 +01:00
|
|
|
aggref->target = lfirst(args);
|
1999-08-21 05:49:17 +02:00
|
|
|
aggref->usenulls = usenulls;
|
1999-12-13 02:27:21 +01:00
|
|
|
aggref->aggstar = agg_star;
|
|
|
|
aggref->aggdistinct = agg_distinct;
|
1999-12-10 08:37:35 +01:00
|
|
|
|
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)
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for all types does not exist", caller, aggname);
|
1997-11-25 23:07:18 +01:00
|
|
|
else
|
|
|
|
{
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "%s: aggregate '%s' for '%s' does not exist", caller, aggname,
|
1997-11-25 23:07:18 +01:00
|
|
|
typeidTypeName(basetypeID));
|
|
|
|
}
|
|
|
|
}
|