*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.55 1999/11/22 17:56:17 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.56 1999/12/09 05:58:53 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_type.h"
+#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
(isnull), true, false, false))
typedef struct {
- List *groupClause;
+ Query *query;
List *targetList;
} check_subplans_for_ungrouped_vars_context;
/*
* check_subplans_for_ungrouped_vars
* Check for subplans that are being passed ungrouped variables as
- * parameters; return TRUE if any are found.
+ * parameters; generate an error message if any are found.
*
* In most contexts, ungrouped variables will be detected by the parser (see
- * parse_agg.c, exprIsAggOrGroupCol()). But that routine currently does not
- * check subplans, because the necessary info is not computed until the
+ * parse_agg.c, check_ungrouped_columns()). But that routine currently does
+ * not check subplans, because the necessary info is not computed until the
* planner runs. So we do it here, after we have processed the subplan.
* This ought to be cleaned up someday.
*
* 'clause' is the expression tree to be searched for subplans.
- * 'groupClause' is the GROUP BY list (a list of GroupClause nodes).
+ * 'query' provides the GROUP BY list and range table.
* 'targetList' is the target list that the group clauses refer to.
+ * (Is it really necessary to pass the tlist separately? Couldn't we
+ * just use the tlist found in the query node?)
*/
-bool
+void
check_subplans_for_ungrouped_vars(Node *clause,
- List *groupClause,
+ Query *query,
List *targetList)
{
check_subplans_for_ungrouped_vars_context context;
- context.groupClause = groupClause;
+ context.query = query;
context.targetList = targetList;
- return check_subplans_for_ungrouped_vars_walker(clause, &context);
+ check_subplans_for_ungrouped_vars_walker(clause, &context);
}
static bool
foreach(t, ((Expr *) node)->args)
{
Node *thisarg = lfirst(t);
- bool contained_in_group_clause = false;
+ Var *var;
+ bool contained_in_group_clause;
List *gl;
- foreach(gl, context->groupClause)
+ /*
+ * We do not care about args that are not local variables;
+ * params or outer-level vars are not our responsibility to
+ * check. (The outer-level query passing them to us needs
+ * to worry, instead.)
+ */
+ if (! IsA(thisarg, Var))
+ continue;
+ var = (Var *) thisarg;
+ if (var->varlevelsup > 0)
+ continue;
+
+ /*
+ * Else, see if it is a grouping column.
+ */
+ contained_in_group_clause = false;
+ foreach(gl, context->query->groupClause)
{
GroupClause *gcl = lfirst(gl);
Node *groupexpr;
}
if (!contained_in_group_clause)
- return true; /* found an ungrouped argument */
+ {
+ /* Found an ungrouped argument. Complain. */
+ RangeTblEntry *rte;
+ char *attname;
+
+ Assert(var->varno > 0 &&
+ var->varno <= length(context->query->rtable));
+ rte = rt_fetch(var->varno, context->query->rtable);
+ attname = get_attname(rte->relid, var->varattno);
+ if (! attname)
+ elog(ERROR, "cache lookup of attribute %d in relation %u failed",
+ var->varattno, rte->relid);
+ elog(ERROR, "Sub-SELECT uses un-GROUPed attribute %s.%s from outer query",
+ rte->refname, attname);
+ }
}
}
return expression_tree_walker(node,
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.29 1999/10/07 04:23:12 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.30 1999/12/09 05:58:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "parser/parse_agg.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
+#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
+typedef struct {
+ ParseState *pstate;
+ List *groupClauses;
+} check_ungrouped_columns_context;
+
static bool contain_agg_clause(Node *clause);
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);
+static void check_ungrouped_columns(Node *node, ParseState *pstate,
+ List *groupClauses);
+static bool check_ungrouped_columns_walker(Node *node,
+ check_ungrouped_columns_context *context);
/*
* contain_agg_clause
}
/*
- * exprIsAggOrGroupCol -
- * returns true if the expression does not contain non-group columns,
- * other than within the arguments of aggregate functions.
+ * 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.
*
* NOTE: we assume that the given clause has been transformed suitably for
* parser output. This means we can use the planner's expression_tree_walker.
* inside the subquery and converted them into a list of parameters for the
* subquery.
*/
-static bool
-exprIsAggOrGroupCol(Node *expr, List *groupClauses)
+static void
+check_ungrouped_columns(Node *node, ParseState *pstate,
+ List *groupClauses)
{
- /* 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);
+ check_ungrouped_columns_context context;
+
+ context.pstate = pstate;
+ context.groupClauses = groupClauses;
+ check_ungrouped_columns_walker(node, &context);
}
static bool
-exprIsAggOrGroupCol_walker(Node *node, List *groupClauses)
+check_ungrouped_columns_walker(Node *node,
+ check_ungrouped_columns_context *context)
{
List *gl;
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.
+ /*
+ * If we find an aggregate function, do not recurse into its arguments.
+ */
+ if (IsA(node, Aggref))
+ return false;
+ /*
+ * Check to see if subexpression as a whole matches any GROUP BY item.
* We need to do this at every recursion level so that we recognize
- * GROUPed-BY expressions.
+ * GROUPed-BY expressions before reaching variables within them.
*/
- foreach(gl, groupClauses)
+ foreach(gl, context->groupClauses)
{
if (equal(node, lfirst(gl)))
return false; /* acceptable, do not descend more */
}
- /* If we have an ungrouped Var, we have a failure --- unless it is an
+ /*
+ * 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))
{
- if (((Var *) node)->varlevelsup == 0)
- return true; /* found an ungrouped local variable */
- return false; /* outer-level Var is acceptable */
+ Var *var = (Var *) node;
+ RangeTblEntry *rte;
+ char *attname;
+
+ if (var->varlevelsup > 0)
+ return false; /* outer-level Var is acceptable */
+ /* Found an ungrouped local variable; generate error message */
+ Assert(var->varno > 0 &&
+ var->varno <= length(context->pstate->p_rtable));
+ rte = rt_fetch(var->varno, context->pstate->p_rtable);
+ attname = get_attname(rte->relid, var->varattno);
+ if (! attname)
+ 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",
+ rte->refname, attname);
}
/* Otherwise, recurse. */
- return expression_tree_walker(node, exprIsAggOrGroupCol_walker,
- (void *) groupClauses);
+ return expression_tree_walker(node, check_ungrouped_columns_walker,
+ (void *) context);
}
/*
/*
* 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.)
+ * otherwise we are likely to complain about some innocent variable
+ * in the target list, which is outright misleading if the problem
+ * is in WHERE.)
*/
if (contain_agg_clause(qry->qual))
elog(ERROR, "Aggregates not allowed in WHERE clause");
* 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...)
+ * for use by check_ungrouped_columns() (this avoids repeated scans of the
+ * targetlist within the recursive routine...)
*/
foreach(tl, qry->groupClause)
{
}
/*
- * 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.
+ * Check the targetlist and HAVING clause for ungrouped variables.
*/
- if (!exprIsAggOrGroupCol(qry->havingQual, groupClauses))
- elog(ERROR,
- "Illegal use of aggregates or non-group column in HAVING clause");
-
- /*
- * The target list can only contain aggregates, group columns and
- * functions thereof.
- */
- foreach(tl, qry->targetList)
- {
- TargetEntry *tle = lfirst(tl);
-
- if (!exprIsAggOrGroupCol(tle->expr, groupClauses))
- elog(ERROR,
- "Illegal use of aggregates or non-group column in target list");
- }
+ check_ungrouped_columns((Node *) qry->targetList, pstate, groupClauses);
+ check_ungrouped_columns((Node *) qry->havingQual, pstate, groupClauses);
/* Release the list storage (but not the pointed-to expressions!) */
freeList(groupClauses);