1997-11-25 22:07:18 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* parse_agg.c
|
2008-12-28 18:54:01 +00:00
|
|
|
* handle aggregates and window functions in parser
|
1997-11-25 22:07:18 +00:00
|
|
|
*
|
2013-01-01 17:15:01 -05:00
|
|
|
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 22:07:18 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/parser/parse_agg.c
|
1997-11-25 22:07:18 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2002-04-11 20:00:18 +00:00
|
|
|
|
2010-08-07 02:44:09 +00:00
|
|
|
#include "catalog/pg_constraint.h"
|
2003-07-01 19:10:53 +00:00
|
|
|
#include "nodes/makefuncs.h"
|
2008-08-25 22:42:34 +00:00
|
|
|
#include "nodes/nodeFuncs.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"
|
2009-12-15 17:57:48 +00:00
|
|
|
#include "parser/parse_clause.h"
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
#include "parser/parse_expr.h"
|
1999-12-09 05:58:56 +00:00
|
|
|
#include "parser/parsetree.h"
|
2003-06-06 15:04:03 +00:00
|
|
|
#include "rewrite/rewriteManip.h"
|
2009-12-15 17:57:48 +00:00
|
|
|
#include "utils/builtins.h"
|
2002-04-11 20:00:18 +00:00
|
|
|
|
1997-11-25 22:07:18 +00:00
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ParseState *pstate;
|
|
|
|
int min_varlevel;
|
|
|
|
int min_agglevel;
|
|
|
|
int sublevels_up;
|
|
|
|
} check_agg_arguments_context;
|
|
|
|
|
2000-04-12 17:17:23 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
1999-12-09 05:58:56 +00:00
|
|
|
ParseState *pstate;
|
2010-08-07 02:44:09 +00:00
|
|
|
Query *qry;
|
1999-12-09 05:58:56 +00:00
|
|
|
List *groupClauses;
|
2003-01-17 03:25:04 +00:00
|
|
|
bool have_non_var_grouping;
|
2010-08-07 02:44:09 +00:00
|
|
|
List **func_grouped_rels;
|
2003-01-17 03:25:04 +00:00
|
|
|
int sublevels_up;
|
1999-12-09 05:58:56 +00:00
|
|
|
} check_ungrouped_columns_context;
|
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
static int check_agg_arguments(ParseState *pstate, List *args);
|
|
|
|
static bool check_agg_arguments_walker(Node *node,
|
|
|
|
check_agg_arguments_context *context);
|
2010-08-07 02:44:09 +00:00
|
|
|
static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
|
|
|
|
List *groupClauses, bool have_non_var_grouping,
|
|
|
|
List **func_grouped_rels);
|
1999-12-09 05:58:56 +00:00
|
|
|
static bool check_ungrouped_columns_walker(Node *node,
|
2000-04-12 17:17:23 +00:00
|
|
|
check_ungrouped_columns_context *context);
|
1997-11-26 03:43:18 +00:00
|
|
|
|
2003-06-06 15:04:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* transformAggregateCall -
|
|
|
|
* Finish initial transformation of an aggregate call
|
|
|
|
*
|
2010-03-17 16:52:38 +00:00
|
|
|
* parse_func.c has recognized the function as an aggregate, and has set up
|
|
|
|
* all the fields of the Aggref except args, aggorder, aggdistinct and
|
|
|
|
* agglevelsup. The passed-in args list has been through standard expression
|
|
|
|
* transformation, while the passed-in aggorder list hasn't been transformed
|
|
|
|
* at all.
|
2009-12-15 17:57:48 +00:00
|
|
|
*
|
|
|
|
* Here we convert the args list into a targetlist by inserting TargetEntry
|
|
|
|
* nodes, and then transform the aggorder and agg_distinct specifications to
|
2010-02-26 02:01:40 +00:00
|
|
|
* produce lists of SortGroupClause nodes. (That might also result in adding
|
2009-12-15 17:57:48 +00:00
|
|
|
* resjunk expressions to the targetlist.)
|
|
|
|
*
|
|
|
|
* We must also determine which query level the aggregate actually belongs to,
|
|
|
|
* set agglevelsup accordingly, and mark p_hasAggs true in the corresponding
|
2003-06-06 15:04:03 +00:00
|
|
|
* pstate level.
|
|
|
|
*/
|
|
|
|
void
|
2010-03-17 16:52:38 +00:00
|
|
|
transformAggregateCall(ParseState *pstate, Aggref *agg,
|
|
|
|
List *args, List *aggorder, bool agg_distinct)
|
2003-06-06 15:04:03 +00:00
|
|
|
{
|
2010-02-26 02:01:40 +00:00
|
|
|
List *tlist;
|
|
|
|
List *torder;
|
|
|
|
List *tdistinct = NIL;
|
|
|
|
AttrNumber attno;
|
|
|
|
int save_next_resno;
|
2003-06-06 15:04:03 +00:00
|
|
|
int min_varlevel;
|
2009-12-15 17:57:48 +00:00
|
|
|
ListCell *lc;
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
const char *err;
|
|
|
|
bool errkind;
|
2009-12-15 17:57:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Transform the plain list of Exprs into a targetlist. We don't bother
|
|
|
|
* to assign column names to the entries.
|
|
|
|
*/
|
|
|
|
tlist = NIL;
|
|
|
|
attno = 1;
|
2010-03-17 16:52:38 +00:00
|
|
|
foreach(lc, args)
|
2009-12-15 17:57:48 +00:00
|
|
|
{
|
2010-02-26 02:01:40 +00:00
|
|
|
Expr *arg = (Expr *) lfirst(lc);
|
2009-12-15 17:57:48 +00:00
|
|
|
TargetEntry *tle = makeTargetEntry(arg, attno++, NULL, false);
|
|
|
|
|
|
|
|
tlist = lappend(tlist, tle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have an ORDER BY, transform it. This will add columns to the
|
|
|
|
* tlist if they appear in ORDER BY but weren't already in the arg list.
|
|
|
|
* They will be marked resjunk = true so we can tell them apart from
|
|
|
|
* regular aggregate arguments later.
|
|
|
|
*
|
|
|
|
* We need to mess with p_next_resno since it will be used to number any
|
|
|
|
* new targetlist entries.
|
|
|
|
*/
|
|
|
|
save_next_resno = pstate->p_next_resno;
|
|
|
|
pstate->p_next_resno = attno;
|
|
|
|
|
|
|
|
torder = transformSortClause(pstate,
|
2010-03-17 16:52:38 +00:00
|
|
|
aggorder,
|
2009-12-15 17:57:48 +00:00
|
|
|
&tlist,
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
EXPR_KIND_ORDER_BY,
|
2010-02-26 02:01:40 +00:00
|
|
|
true /* fix unknowns */ ,
|
|
|
|
true /* force SQL99 rules */ );
|
2009-12-15 17:57:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have DISTINCT, transform that to produce a distinctList.
|
|
|
|
*/
|
|
|
|
if (agg_distinct)
|
|
|
|
{
|
|
|
|
tdistinct = transformDistinctClause(pstate, &tlist, torder, true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove this check if executor support for hashed distinct for
|
|
|
|
* aggregates is ever added.
|
|
|
|
*/
|
|
|
|
foreach(lc, tdistinct)
|
|
|
|
{
|
|
|
|
SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
|
|
|
|
|
|
|
|
if (!OidIsValid(sortcl->sortop))
|
|
|
|
{
|
2010-02-26 02:01:40 +00:00
|
|
|
Node *expr = get_sortgroupclause_expr(sortcl, tlist);
|
2009-12-15 17:57:48 +00:00
|
|
|
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
2010-02-26 02:01:40 +00:00
|
|
|
errmsg("could not identify an ordering operator for type %s",
|
|
|
|
format_type_be(exprType(expr))),
|
2009-12-15 17:57:48 +00:00
|
|
|
errdetail("Aggregates with DISTINCT must be able to sort their inputs."),
|
|
|
|
parser_errposition(pstate, exprLocation(expr))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the Aggref with the transformation results */
|
|
|
|
agg->args = tlist;
|
|
|
|
agg->aggorder = torder;
|
|
|
|
agg->aggdistinct = tdistinct;
|
|
|
|
|
|
|
|
pstate->p_next_resno = save_next_resno;
|
2003-06-06 15:04:03 +00:00
|
|
|
|
|
|
|
/*
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* Check the arguments to compute the aggregate's level and detect
|
|
|
|
* improper nesting.
|
2003-06-06 15:04:03 +00:00
|
|
|
*/
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
min_varlevel = check_agg_arguments(pstate, agg->args);
|
|
|
|
agg->agglevelsup = min_varlevel;
|
|
|
|
|
|
|
|
/* Mark the correct pstate level as having aggregates */
|
|
|
|
while (min_varlevel-- > 0)
|
|
|
|
pstate = pstate->parentParseState;
|
|
|
|
pstate->p_hasAggs = true;
|
2003-06-06 15:04:03 +00:00
|
|
|
|
|
|
|
/*
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* Check to see if the aggregate function is in an invalid place within
|
|
|
|
* its aggregation query.
|
|
|
|
*
|
|
|
|
* For brevity we support two schemes for reporting an error here: set
|
|
|
|
* "err" to a custom message, or set "errkind" true if the error context
|
|
|
|
* is sufficiently identified by what ParseExprKindName will return, *and*
|
|
|
|
* what it will return is just a SQL keyword. (Otherwise, use a custom
|
|
|
|
* message to avoid creating translation problems.)
|
2003-06-06 15:04:03 +00:00
|
|
|
*/
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
err = NULL;
|
|
|
|
errkind = false;
|
|
|
|
switch (pstate->p_expr_kind)
|
2003-06-06 15:04:03 +00:00
|
|
|
{
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
case EXPR_KIND_NONE:
|
|
|
|
Assert(false); /* can't happen */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_OTHER:
|
|
|
|
/* Accept aggregate here; caller must throw error if wanted */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_JOIN_ON:
|
|
|
|
case EXPR_KIND_JOIN_USING:
|
|
|
|
err = _("aggregate functions are not allowed in JOIN conditions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_FROM_SUBSELECT:
|
|
|
|
/* Should only be possible in a LATERAL subquery */
|
|
|
|
Assert(pstate->p_lateral_active);
|
|
|
|
/* Aggregate scope rules make it worth being explicit here */
|
|
|
|
err = _("aggregate functions are not allowed in FROM clause of their own query level");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_FROM_FUNCTION:
|
|
|
|
err = _("aggregate functions are not allowed in functions in FROM");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WHERE:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_HAVING:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WINDOW_PARTITION:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WINDOW_ORDER:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WINDOW_FRAME_RANGE:
|
|
|
|
err = _("aggregate functions are not allowed in window RANGE");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WINDOW_FRAME_ROWS:
|
|
|
|
err = _("aggregate functions are not allowed in window ROWS");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_SELECT_TARGET:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INSERT_TARGET:
|
|
|
|
case EXPR_KIND_UPDATE_SOURCE:
|
|
|
|
case EXPR_KIND_UPDATE_TARGET:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_GROUP_BY:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_ORDER_BY:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_DISTINCT_ON:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_LIMIT:
|
|
|
|
case EXPR_KIND_OFFSET:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_RETURNING:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_VALUES:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_CHECK_CONSTRAINT:
|
|
|
|
case EXPR_KIND_DOMAIN_CHECK:
|
2013-01-05 08:25:21 -05:00
|
|
|
err = _("aggregate functions are not allowed in check constraints");
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
break;
|
|
|
|
case EXPR_KIND_COLUMN_DEFAULT:
|
|
|
|
case EXPR_KIND_FUNCTION_DEFAULT:
|
|
|
|
err = _("aggregate functions are not allowed in DEFAULT expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INDEX_EXPRESSION:
|
|
|
|
err = _("aggregate functions are not allowed in index expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INDEX_PREDICATE:
|
|
|
|
err = _("aggregate functions are not allowed in index predicates");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_ALTER_COL_TRANSFORM:
|
|
|
|
err = _("aggregate functions are not allowed in transform expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_EXECUTE_PARAMETER:
|
|
|
|
err = _("aggregate functions are not allowed in EXECUTE parameters");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_TRIGGER_WHEN:
|
|
|
|
err = _("aggregate functions are not allowed in trigger WHEN conditions");
|
|
|
|
break;
|
2003-06-06 15:04:03 +00:00
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
/*
|
|
|
|
* There is intentionally no default: case here, so that the
|
|
|
|
* compiler will warn if we add a new ParseExprKind without
|
|
|
|
* extending this switch. If we do see an unrecognized value at
|
|
|
|
* runtime, the behavior will be the same as for EXPR_KIND_OTHER,
|
|
|
|
* which is sane anyway.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
if (err)
|
2008-12-28 18:54:01 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
errmsg_internal("%s", err),
|
|
|
|
parser_errposition(pstate, agg->location)));
|
|
|
|
if (errkind)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
2013-05-29 16:58:43 -04:00
|
|
|
/* translator: %s is name of a SQL construct, eg GROUP BY */
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
errmsg("aggregate functions are not allowed in %s",
|
|
|
|
ParseExprKindName(pstate->p_expr_kind)),
|
|
|
|
parser_errposition(pstate, agg->location)));
|
|
|
|
}
|
2008-12-28 18:54:01 +00:00
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
/*
|
|
|
|
* check_agg_arguments
|
|
|
|
* Scan the arguments of an aggregate function to determine the
|
|
|
|
* aggregate's semantic level (zero is the current select's level,
|
|
|
|
* one is its parent, etc).
|
|
|
|
*
|
|
|
|
* The aggregate's level is the same as the level of the lowest-level variable
|
|
|
|
* or aggregate in its arguments; or if it contains no variables at all, we
|
|
|
|
* presume it to be local.
|
|
|
|
*
|
|
|
|
* We also take this opportunity to detect any aggregates or window functions
|
|
|
|
* nested within the arguments. We can throw error immediately if we find
|
|
|
|
* a window function. Aggregates are a bit trickier because it's only an
|
|
|
|
* error if the inner aggregate is of the same semantic level as the outer,
|
|
|
|
* which we can't know until we finish scanning the arguments.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
check_agg_arguments(ParseState *pstate, List *args)
|
|
|
|
{
|
|
|
|
int agglevel;
|
|
|
|
check_agg_arguments_context context;
|
2003-06-06 15:04:03 +00:00
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
context.pstate = pstate;
|
|
|
|
context.min_varlevel = -1; /* signifies nothing found yet */
|
|
|
|
context.min_agglevel = -1;
|
|
|
|
context.sublevels_up = 0;
|
|
|
|
|
|
|
|
(void) expression_tree_walker((Node *) args,
|
|
|
|
check_agg_arguments_walker,
|
|
|
|
(void *) &context);
|
2012-08-07 19:02:54 -04:00
|
|
|
|
|
|
|
/*
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* If we found no vars nor aggs at all, it's a level-zero aggregate;
|
|
|
|
* otherwise, its level is the minimum of vars or aggs.
|
2012-08-07 19:02:54 -04:00
|
|
|
*/
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
if (context.min_varlevel < 0)
|
|
|
|
{
|
|
|
|
if (context.min_agglevel < 0)
|
|
|
|
return 0;
|
|
|
|
agglevel = context.min_agglevel;
|
|
|
|
}
|
|
|
|
else if (context.min_agglevel < 0)
|
|
|
|
agglevel = context.min_varlevel;
|
|
|
|
else
|
|
|
|
agglevel = Min(context.min_varlevel, context.min_agglevel);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's a nested aggregate of the same semantic level, complain.
|
|
|
|
*/
|
|
|
|
if (agglevel == context.min_agglevel)
|
2012-08-07 19:02:54 -04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
errmsg("aggregate function calls cannot be nested"),
|
|
|
|
parser_errposition(pstate,
|
|
|
|
locate_agg_of_level((Node *) args,
|
|
|
|
agglevel))));
|
|
|
|
|
|
|
|
return agglevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_agg_arguments_walker(Node *node,
|
|
|
|
check_agg_arguments_context *context)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, Var))
|
|
|
|
{
|
|
|
|
int varlevelsup = ((Var *) node)->varlevelsup;
|
|
|
|
|
|
|
|
/* convert levelsup to frame of reference of original query */
|
|
|
|
varlevelsup -= context->sublevels_up;
|
|
|
|
/* ignore local vars of subqueries */
|
|
|
|
if (varlevelsup >= 0)
|
|
|
|
{
|
|
|
|
if (context->min_varlevel < 0 ||
|
|
|
|
context->min_varlevel > varlevelsup)
|
|
|
|
context->min_varlevel = varlevelsup;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsA(node, Aggref))
|
|
|
|
{
|
|
|
|
int agglevelsup = ((Aggref *) node)->agglevelsup;
|
|
|
|
|
|
|
|
/* convert levelsup to frame of reference of original query */
|
|
|
|
agglevelsup -= context->sublevels_up;
|
|
|
|
/* ignore local aggs of subqueries */
|
|
|
|
if (agglevelsup >= 0)
|
|
|
|
{
|
|
|
|
if (context->min_agglevel < 0 ||
|
|
|
|
context->min_agglevel > agglevelsup)
|
|
|
|
context->min_agglevel = agglevelsup;
|
|
|
|
}
|
|
|
|
/* no need to examine args of the inner aggregate */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* We can throw error on sight for a window function */
|
|
|
|
if (IsA(node, WindowFunc))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
|
|
|
errmsg("aggregate function calls cannot contain window function calls"),
|
|
|
|
parser_errposition(context->pstate,
|
|
|
|
((WindowFunc *) node)->location)));
|
|
|
|
if (IsA(node, Query))
|
|
|
|
{
|
|
|
|
/* Recurse into subselects */
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
context->sublevels_up++;
|
|
|
|
result = query_tree_walker((Query *) node,
|
|
|
|
check_agg_arguments_walker,
|
|
|
|
(void *) context,
|
|
|
|
0);
|
|
|
|
context->sublevels_up--;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return expression_tree_walker(node,
|
|
|
|
check_agg_arguments_walker,
|
|
|
|
(void *) context);
|
2003-06-06 15:04:03 +00:00
|
|
|
}
|
|
|
|
|
2008-12-28 18:54:01 +00:00
|
|
|
/*
|
|
|
|
* transformWindowFuncCall -
|
|
|
|
* Finish initial transformation of a window function call
|
|
|
|
*
|
|
|
|
* parse_func.c has recognized the function as a window function, and has set
|
|
|
|
* up all the fields of the WindowFunc except winref. Here we must (1) add
|
|
|
|
* the WindowDef to the pstate (if not a duplicate of one already present) and
|
|
|
|
* set winref to link to it; and (2) mark p_hasWindowFuncs true in the pstate.
|
|
|
|
* Unlike aggregates, only the most closely nested pstate level need be
|
|
|
|
* considered --- there are no "outer window functions" per SQL spec.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
|
|
|
|
WindowDef *windef)
|
|
|
|
{
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
const char *err;
|
|
|
|
bool errkind;
|
|
|
|
|
2008-12-28 18:54:01 +00:00
|
|
|
/*
|
2009-06-11 14:49:15 +00:00
|
|
|
* A window function call can't contain another one (but aggs are OK). XXX
|
|
|
|
* is this required by spec, or just an unimplemented feature?
|
2008-12-28 18:54:01 +00:00
|
|
|
*/
|
|
|
|
if (pstate->p_hasWindowFuncs &&
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
contain_windowfuncs((Node *) wfunc->args))
|
2008-12-28 18:54:01 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WINDOWING_ERROR),
|
|
|
|
errmsg("window function calls cannot be nested"),
|
|
|
|
parser_errposition(pstate,
|
2009-06-11 14:49:15 +00:00
|
|
|
locate_windowfunc((Node *) wfunc->args))));
|
2008-12-28 18:54:01 +00:00
|
|
|
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
/*
|
|
|
|
* Check to see if the window function is in an invalid place within the
|
|
|
|
* query.
|
|
|
|
*
|
|
|
|
* For brevity we support two schemes for reporting an error here: set
|
|
|
|
* "err" to a custom message, or set "errkind" true if the error context
|
|
|
|
* is sufficiently identified by what ParseExprKindName will return, *and*
|
|
|
|
* what it will return is just a SQL keyword. (Otherwise, use a custom
|
|
|
|
* message to avoid creating translation problems.)
|
|
|
|
*/
|
|
|
|
err = NULL;
|
|
|
|
errkind = false;
|
|
|
|
switch (pstate->p_expr_kind)
|
|
|
|
{
|
|
|
|
case EXPR_KIND_NONE:
|
|
|
|
Assert(false); /* can't happen */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_OTHER:
|
|
|
|
/* Accept window func here; caller must throw error if wanted */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_JOIN_ON:
|
|
|
|
case EXPR_KIND_JOIN_USING:
|
|
|
|
err = _("window functions are not allowed in JOIN conditions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_FROM_SUBSELECT:
|
|
|
|
/* can't get here, but just in case, throw an error */
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_FROM_FUNCTION:
|
|
|
|
err = _("window functions are not allowed in functions in FROM");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WHERE:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_HAVING:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_WINDOW_PARTITION:
|
|
|
|
case EXPR_KIND_WINDOW_ORDER:
|
|
|
|
case EXPR_KIND_WINDOW_FRAME_RANGE:
|
|
|
|
case EXPR_KIND_WINDOW_FRAME_ROWS:
|
|
|
|
err = _("window functions are not allowed in window definitions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_SELECT_TARGET:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INSERT_TARGET:
|
|
|
|
case EXPR_KIND_UPDATE_SOURCE:
|
|
|
|
case EXPR_KIND_UPDATE_TARGET:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_GROUP_BY:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_ORDER_BY:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_DISTINCT_ON:
|
|
|
|
/* okay */
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_LIMIT:
|
|
|
|
case EXPR_KIND_OFFSET:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_RETURNING:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_VALUES:
|
|
|
|
errkind = true;
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_CHECK_CONSTRAINT:
|
|
|
|
case EXPR_KIND_DOMAIN_CHECK:
|
2013-01-05 08:25:21 -05:00
|
|
|
err = _("window functions are not allowed in check constraints");
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
break;
|
|
|
|
case EXPR_KIND_COLUMN_DEFAULT:
|
|
|
|
case EXPR_KIND_FUNCTION_DEFAULT:
|
|
|
|
err = _("window functions are not allowed in DEFAULT expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INDEX_EXPRESSION:
|
|
|
|
err = _("window functions are not allowed in index expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_INDEX_PREDICATE:
|
|
|
|
err = _("window functions are not allowed in index predicates");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_ALTER_COL_TRANSFORM:
|
|
|
|
err = _("window functions are not allowed in transform expressions");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_EXECUTE_PARAMETER:
|
|
|
|
err = _("window functions are not allowed in EXECUTE parameters");
|
|
|
|
break;
|
|
|
|
case EXPR_KIND_TRIGGER_WHEN:
|
|
|
|
err = _("window functions are not allowed in trigger WHEN conditions");
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is intentionally no default: case here, so that the
|
|
|
|
* compiler will warn if we add a new ParseExprKind without
|
|
|
|
* extending this switch. If we do see an unrecognized value at
|
|
|
|
* runtime, the behavior will be the same as for EXPR_KIND_OTHER,
|
|
|
|
* which is sane anyway.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WINDOWING_ERROR),
|
|
|
|
errmsg_internal("%s", err),
|
|
|
|
parser_errposition(pstate, wfunc->location)));
|
|
|
|
if (errkind)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WINDOWING_ERROR),
|
2013-05-29 16:58:43 -04:00
|
|
|
/* translator: %s is name of a SQL construct, eg GROUP BY */
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
errmsg("window functions are not allowed in %s",
|
|
|
|
ParseExprKindName(pstate->p_expr_kind)),
|
|
|
|
parser_errposition(pstate, wfunc->location)));
|
|
|
|
|
2008-12-28 18:54:01 +00:00
|
|
|
/*
|
2009-06-11 14:49:15 +00:00
|
|
|
* If the OVER clause just specifies a window name, find that WINDOW
|
|
|
|
* clause (which had better be present). Otherwise, try to match all the
|
|
|
|
* properties of the OVER clause, and make a new entry in the p_windowdefs
|
|
|
|
* list if no luck.
|
2008-12-28 18:54:01 +00:00
|
|
|
*/
|
2008-12-31 00:08:39 +00:00
|
|
|
if (windef->name)
|
2008-12-28 18:54:01 +00:00
|
|
|
{
|
|
|
|
Index winref = 0;
|
|
|
|
ListCell *lc;
|
|
|
|
|
2008-12-31 00:08:39 +00:00
|
|
|
Assert(windef->refname == NULL &&
|
|
|
|
windef->partitionClause == NIL &&
|
|
|
|
windef->orderClause == NIL &&
|
|
|
|
windef->frameOptions == FRAMEOPTION_DEFAULTS);
|
|
|
|
|
2008-12-28 18:54:01 +00:00
|
|
|
foreach(lc, pstate->p_windowdefs)
|
|
|
|
{
|
2009-06-11 14:49:15 +00:00
|
|
|
WindowDef *refwin = (WindowDef *) lfirst(lc);
|
2008-12-28 18:54:01 +00:00
|
|
|
|
|
|
|
winref++;
|
2008-12-31 00:08:39 +00:00
|
|
|
if (refwin->name && strcmp(refwin->name, windef->name) == 0)
|
2008-12-28 18:54:01 +00:00
|
|
|
{
|
|
|
|
wfunc->winref = winref;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lc == NULL) /* didn't find it? */
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
2008-12-31 00:08:39 +00:00
|
|
|
errmsg("window \"%s\" does not exist", windef->name),
|
2008-12-28 18:54:01 +00:00
|
|
|
parser_errposition(pstate, windef->location)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Index winref = 0;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
foreach(lc, pstate->p_windowdefs)
|
|
|
|
{
|
2009-06-11 14:49:15 +00:00
|
|
|
WindowDef *refwin = (WindowDef *) lfirst(lc);
|
2008-12-28 18:54:01 +00:00
|
|
|
|
|
|
|
winref++;
|
|
|
|
if (refwin->refname && windef->refname &&
|
2008-12-31 00:08:39 +00:00
|
|
|
strcmp(refwin->refname, windef->refname) == 0)
|
2009-06-11 14:49:15 +00:00
|
|
|
/* matched on refname */ ;
|
2008-12-28 18:54:01 +00:00
|
|
|
else if (!refwin->refname && !windef->refname)
|
2009-06-11 14:49:15 +00:00
|
|
|
/* matched, no refname */ ;
|
2008-12-28 18:54:01 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
if (equal(refwin->partitionClause, windef->partitionClause) &&
|
2008-12-31 00:08:39 +00:00
|
|
|
equal(refwin->orderClause, windef->orderClause) &&
|
2010-02-12 17:33:21 +00:00
|
|
|
refwin->frameOptions == windef->frameOptions &&
|
|
|
|
equal(refwin->startOffset, windef->startOffset) &&
|
|
|
|
equal(refwin->endOffset, windef->endOffset))
|
2008-12-28 18:54:01 +00:00
|
|
|
{
|
|
|
|
/* found a duplicate window specification */
|
|
|
|
wfunc->winref = winref;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lc == NULL) /* didn't find it? */
|
|
|
|
{
|
|
|
|
pstate->p_windowdefs = lappend(pstate->p_windowdefs, windef);
|
|
|
|
wfunc->winref = list_length(pstate->p_windowdefs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pstate->p_hasWindowFuncs = true;
|
|
|
|
}
|
2003-06-06 15:04:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parseCheckAggregates
|
|
|
|
* Check for aggregates where they shouldn't be and improper grouping.
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* This function should be called after the target list and qualifications
|
|
|
|
* are finalized.
|
2003-06-06 15:04:03 +00:00
|
|
|
*
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* Misplaced aggregates are now mostly detected in transformAggregateCall,
|
|
|
|
* but it seems more robust to check for aggregates in recursive queries
|
|
|
|
* only after everything is finalized. In any case it's hard to detect
|
|
|
|
* improper grouping on-the-fly, so we have to make another pass over the
|
|
|
|
* query for that.
|
2003-06-06 15:04:03 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
parseCheckAggregates(ParseState *pstate, Query *qry)
|
|
|
|
{
|
|
|
|
List *groupClauses = NIL;
|
2004-01-28 07:46:44 +00:00
|
|
|
bool have_non_var_grouping;
|
2010-08-07 02:44:09 +00:00
|
|
|
List *func_grouped_rels = NIL;
|
2004-05-26 04:41:50 +00:00
|
|
|
ListCell *l;
|
2003-06-06 15:04:03 +00:00
|
|
|
bool hasJoinRTEs;
|
2008-10-04 21:56:55 +00:00
|
|
|
bool hasSelfRefRTEs;
|
2005-06-05 22:32:58 +00:00
|
|
|
PlannerInfo *root;
|
2003-06-06 15:04:03 +00:00
|
|
|
Node *clause;
|
|
|
|
|
|
|
|
/* This should only be called if we found aggregates or grouping */
|
2005-03-10 23:21:26 +00:00
|
|
|
Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
|
2003-06-06 15:04:03 +00:00
|
|
|
|
2008-10-04 21:56:55 +00:00
|
|
|
/*
|
|
|
|
* Scan the range table to see if there are JOIN or self-reference CTE
|
|
|
|
* entries. We'll need this info below.
|
|
|
|
*/
|
|
|
|
hasJoinRTEs = hasSelfRefRTEs = false;
|
|
|
|
foreach(l, pstate->p_rtable)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
|
|
|
|
|
|
|
|
if (rte->rtekind == RTE_JOIN)
|
|
|
|
hasJoinRTEs = true;
|
|
|
|
else if (rte->rtekind == RTE_CTE && rte->self_reference)
|
|
|
|
hasSelfRefRTEs = true;
|
|
|
|
}
|
|
|
|
|
2003-06-06 15:04:03 +00:00
|
|
|
/*
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
* Build a list of the acceptable GROUP BY expressions for use by
|
|
|
|
* check_ungrouped_columns().
|
2003-06-06 15:04:03 +00:00
|
|
|
*/
|
2004-05-26 04:41:50 +00:00
|
|
|
foreach(l, qry->groupClause)
|
2003-06-06 15:04:03 +00:00
|
|
|
{
|
2008-08-02 21:32:01 +00:00
|
|
|
SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
|
2003-06-06 15:04:03 +00:00
|
|
|
Node *expr;
|
|
|
|
|
|
|
|
expr = get_sortgroupclause_expr(grpcl, qry->targetList);
|
|
|
|
if (expr == NULL)
|
|
|
|
continue; /* probably cannot happen */
|
|
|
|
groupClauses = lcons(expr, groupClauses);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* If there are join alias vars involved, we have to flatten them to the
|
|
|
|
* underlying vars, so that aliased and unaliased vars will be correctly
|
|
|
|
* taken as equal. We can skip the expense of doing this if no rangetable
|
2009-06-11 14:49:15 +00:00
|
|
|
* entries are RTE_JOIN kind. We use the planner's flatten_join_alias_vars
|
|
|
|
* routine to do the flattening; it wants a PlannerInfo root node, which
|
|
|
|
* fortunately can be mostly dummy.
|
2005-06-05 22:32:58 +00:00
|
|
|
*/
|
2003-06-06 15:04:03 +00:00
|
|
|
if (hasJoinRTEs)
|
2005-06-05 22:32:58 +00:00
|
|
|
{
|
|
|
|
root = makeNode(PlannerInfo);
|
|
|
|
root->parse = qry;
|
2007-01-20 20:45:41 +00:00
|
|
|
root->planner_cxt = CurrentMemoryContext;
|
2005-06-05 22:32:58 +00:00
|
|
|
root->hasJoinRTEs = true;
|
|
|
|
|
|
|
|
groupClauses = (List *) flatten_join_alias_vars(root,
|
2005-10-15 02:49:52 +00:00
|
|
|
(Node *) groupClauses);
|
2005-06-05 22:32:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
root = NULL; /* keep compiler quiet */
|
2003-06-06 15:04:03 +00:00
|
|
|
|
2004-01-28 07:46:44 +00:00
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Detect whether any of the grouping expressions aren't simple Vars; if
|
|
|
|
* they're all Vars then we don't have to work so hard in the recursive
|
|
|
|
* scans. (Note we have to flatten aliases before this.)
|
2004-01-28 07:46:44 +00:00
|
|
|
*/
|
|
|
|
have_non_var_grouping = false;
|
2004-05-26 04:41:50 +00:00
|
|
|
foreach(l, groupClauses)
|
2004-01-28 07:46:44 +00:00
|
|
|
{
|
2004-05-26 04:41:50 +00:00
|
|
|
if (!IsA((Node *) lfirst(l), Var))
|
2004-01-28 07:46:44 +00:00
|
|
|
{
|
|
|
|
have_non_var_grouping = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-06 15:04:03 +00:00
|
|
|
/*
|
|
|
|
* Check the targetlist and HAVING clause for ungrouped variables.
|
2008-12-28 18:54:01 +00:00
|
|
|
*
|
|
|
|
* Note: because we check resjunk tlist elements as well as regular ones,
|
|
|
|
* this will also find ungrouped variables that came from ORDER BY and
|
2009-06-11 14:49:15 +00:00
|
|
|
* WINDOW clauses. For that matter, it's also going to examine the
|
2008-12-28 18:54:01 +00:00
|
|
|
* grouping expressions themselves --- but they'll all pass the test ...
|
2003-06-06 15:04:03 +00:00
|
|
|
*/
|
|
|
|
clause = (Node *) qry->targetList;
|
|
|
|
if (hasJoinRTEs)
|
2005-06-05 22:32:58 +00:00
|
|
|
clause = flatten_join_alias_vars(root, clause);
|
2010-08-07 02:44:09 +00:00
|
|
|
check_ungrouped_columns(clause, pstate, qry,
|
|
|
|
groupClauses, have_non_var_grouping,
|
|
|
|
&func_grouped_rels);
|
2003-06-06 15:04:03 +00:00
|
|
|
|
|
|
|
clause = (Node *) qry->havingQual;
|
|
|
|
if (hasJoinRTEs)
|
2005-06-05 22:32:58 +00:00
|
|
|
clause = flatten_join_alias_vars(root, clause);
|
2010-08-07 02:44:09 +00:00
|
|
|
check_ungrouped_columns(clause, pstate, qry,
|
|
|
|
groupClauses, have_non_var_grouping,
|
|
|
|
&func_grouped_rels);
|
2008-10-04 21:56:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Per spec, aggregates can't appear in a recursive term.
|
|
|
|
*/
|
|
|
|
if (pstate->p_hasAggs && hasSelfRefRTEs)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_RECURSION),
|
Centralize the logic for detecting misplaced aggregates, window funcs, etc.
Formerly we relied on checking after-the-fact to see if an expression
contained aggregates, window functions, or sub-selects when it shouldn't.
This is grotty, easily forgotten (indeed, we had forgotten to teach
DefineIndex about rejecting window functions), and none too efficient
since it requires extra traversals of the parse tree. To improve matters,
define an enum type that classifies all SQL sub-expressions, store it in
ParseState to show what kind of expression we are currently parsing, and
make transformAggregateCall, transformWindowFuncCall, and transformSubLink
check the expression type and throw error if the type indicates the
construct is disallowed. This allows removal of a large number of ad-hoc
checks scattered around the code base. The enum type is sufficiently
fine-grained that we can still produce error messages of at least the
same specificity as before.
Bringing these error checks together revealed that we'd been none too
consistent about phrasing of the error messages, so standardize the wording
a bit.
Also, rewrite checking of aggregate arguments so that it requires only one
traversal of the arguments, rather than up to three as before.
In passing, clean up some more comments left over from add_missing_from
support, and annotate some tests that I think are dead code now that that's
gone. (I didn't risk actually removing said dead code, though.)
2012-08-10 11:35:33 -04:00
|
|
|
errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
|
2008-10-04 21:56:55 +00:00
|
|
|
parser_errposition(pstate,
|
|
|
|
locate_agg_of_level((Node *) qry, 0))));
|
2003-06-06 15:04:03 +00:00
|
|
|
}
|
|
|
|
|
1997-11-25 22:07:18 +00:00
|
|
|
/*
|
1999-12-09 05:58:56 +00: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 03:48:31 +00:00
|
|
|
*
|
|
|
|
* NOTE: we assume that the given clause has been transformed suitably for
|
2003-01-17 03:25:04 +00:00
|
|
|
* parser output. This means we can use expression_tree_walker.
|
1999-06-19 03:48:31 +00:00
|
|
|
*
|
2003-01-17 03:25:04 +00:00
|
|
|
* NOTE: we recognize grouping expressions in the main query, but only
|
|
|
|
* grouping Vars in subqueries. For example, this will be rejected,
|
|
|
|
* although it could be allowed:
|
|
|
|
* SELECT
|
|
|
|
* (SELECT x FROM bar where y = (foo.a + foo.b))
|
|
|
|
* FROM foo
|
|
|
|
* GROUP BY a + b;
|
|
|
|
* The difficulty is the need to account for different sublevels_up.
|
|
|
|
* This appears to require a whole custom version of equal(), which is
|
|
|
|
* way more pain than the feature seems worth.
|
1997-11-25 22:07:18 +00:00
|
|
|
*/
|
1999-12-09 05:58:56 +00:00
|
|
|
static void
|
2010-08-07 02:44:09 +00:00
|
|
|
check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
|
|
|
|
List *groupClauses, bool have_non_var_grouping,
|
|
|
|
List **func_grouped_rels)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
2000-04-12 17:17:23 +00:00
|
|
|
check_ungrouped_columns_context context;
|
1999-12-09 05:58:56 +00:00
|
|
|
|
|
|
|
context.pstate = pstate;
|
2010-08-07 02:44:09 +00:00
|
|
|
context.qry = qry;
|
1999-12-09 05:58:56 +00:00
|
|
|
context.groupClauses = groupClauses;
|
2003-01-17 03:25:04 +00:00
|
|
|
context.have_non_var_grouping = have_non_var_grouping;
|
2010-08-07 02:44:09 +00:00
|
|
|
context.func_grouped_rels = func_grouped_rels;
|
2003-01-17 03:25:04 +00:00
|
|
|
context.sublevels_up = 0;
|
1999-12-09 05:58:56 +00:00
|
|
|
check_ungrouped_columns_walker(node, &context);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
1997-11-26 03:43:18 +00:00
|
|
|
static bool
|
1999-12-09 05:58:56 +00:00
|
|
|
check_ungrouped_columns_walker(Node *node,
|
|
|
|
check_ungrouped_columns_context *context)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
2004-05-26 04:41:50 +00:00
|
|
|
ListCell *gl;
|
1997-11-25 22:07:18 +00:00
|
|
|
|
1999-06-19 03:48:31 +00:00
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
2003-01-17 03:25:04 +00:00
|
|
|
if (IsA(node, Const) ||
|
|
|
|
IsA(node, Param))
|
1999-06-19 03:48:31 +00:00
|
|
|
return false; /* constants are always acceptable */
|
2000-04-12 17:17:23 +00:00
|
|
|
|
1999-12-09 05:58:56 +00:00
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* If we find an aggregate call of the original level, do not recurse into
|
|
|
|
* its arguments; ungrouped vars in the arguments are not an error. We can
|
|
|
|
* also skip looking at the arguments of aggregates of higher levels,
|
|
|
|
* since they could not possibly contain Vars that are of concern to us
|
|
|
|
* (see transformAggregateCall). We do need to look into the arguments of
|
|
|
|
* aggregates of lower levels, however.
|
1999-12-09 05:58:56 +00:00
|
|
|
*/
|
2003-06-06 15:04:03 +00:00
|
|
|
if (IsA(node, Aggref) &&
|
|
|
|
(int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
|
1999-12-09 05:58:56 +00:00
|
|
|
return false;
|
2000-04-12 17:17:23 +00:00
|
|
|
|
1999-12-09 05:58:56 +00:00
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* If we have any GROUP BY items that are not simple Vars, 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
|
|
|
|
* before reaching variables within them. But this only works at the outer
|
|
|
|
* query level, as noted above.
|
1999-06-19 03:48:31 +00:00
|
|
|
*/
|
2003-01-17 03:25:04 +00:00
|
|
|
if (context->have_non_var_grouping && context->sublevels_up == 0)
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
2003-01-17 03:25:04 +00:00
|
|
|
foreach(gl, context->groupClauses)
|
|
|
|
{
|
|
|
|
if (equal(node, lfirst(gl)))
|
|
|
|
return false; /* acceptable, do not descend more */
|
|
|
|
}
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
2000-04-12 17:17:23 +00:00
|
|
|
|
1999-12-09 05:58:56 +00:00
|
|
|
/*
|
2003-01-17 03:25:04 +00:00
|
|
|
* If we have an ungrouped Var of the original query level, we have a
|
2005-10-15 02:49:52 +00:00
|
|
|
* failure. Vars below the original query level are not a problem, and
|
|
|
|
* neither are Vars from above it. (If such Vars are ungrouped as far as
|
|
|
|
* their own query level is concerned, that's someone else's problem...)
|
1999-06-19 03:48:31 +00:00
|
|
|
*/
|
|
|
|
if (IsA(node, Var))
|
1997-11-25 22:07:18 +00:00
|
|
|
{
|
2000-04-12 17:17:23 +00:00
|
|
|
Var *var = (Var *) node;
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
char *attname;
|
1999-12-09 05:58:56 +00:00
|
|
|
|
2003-01-17 03:25:04 +00:00
|
|
|
if (var->varlevelsup != context->sublevels_up)
|
|
|
|
return false; /* it's not local to my query, ignore */
|
2003-08-04 00:43:34 +00:00
|
|
|
|
2003-01-17 03:25:04 +00:00
|
|
|
/*
|
|
|
|
* Check for a match, if we didn't do it above.
|
|
|
|
*/
|
|
|
|
if (!context->have_non_var_grouping || context->sublevels_up != 0)
|
|
|
|
{
|
|
|
|
foreach(gl, context->groupClauses)
|
|
|
|
{
|
2003-08-04 00:43:34 +00:00
|
|
|
Var *gvar = (Var *) lfirst(gl);
|
2003-01-17 03:25:04 +00:00
|
|
|
|
|
|
|
if (IsA(gvar, Var) &&
|
|
|
|
gvar->varno == var->varno &&
|
|
|
|
gvar->varattno == var->varattno &&
|
|
|
|
gvar->varlevelsup == 0)
|
2003-08-04 00:43:34 +00:00
|
|
|
return false; /* acceptable, we're okay */
|
2003-01-17 03:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-07 02:44:09 +00:00
|
|
|
/*
|
|
|
|
* Check whether the Var is known functionally dependent on the GROUP
|
2011-04-10 11:42:00 -04:00
|
|
|
* BY columns. If so, we can allow the Var to be used, because the
|
2010-08-07 02:44:09 +00:00
|
|
|
* grouping is really a no-op for this table. However, this deduction
|
|
|
|
* depends on one or more constraints of the table, so we have to add
|
|
|
|
* those constraints to the query's constraintDeps list, because it's
|
|
|
|
* not semantically valid anymore if the constraint(s) get dropped.
|
|
|
|
* (Therefore, this check must be the last-ditch effort before raising
|
|
|
|
* error: we don't want to add dependencies unnecessarily.)
|
|
|
|
*
|
|
|
|
* Because this is a pretty expensive check, and will have the same
|
|
|
|
* outcome for all columns of a table, we remember which RTEs we've
|
|
|
|
* already proven functional dependency for in the func_grouped_rels
|
2011-04-10 11:42:00 -04:00
|
|
|
* list. This test also prevents us from adding duplicate entries to
|
|
|
|
* the constraintDeps list.
|
2010-08-07 02:44:09 +00:00
|
|
|
*/
|
|
|
|
if (list_member_int(*context->func_grouped_rels, var->varno))
|
2011-04-10 11:42:00 -04:00
|
|
|
return false; /* previously proven acceptable */
|
2010-08-07 02:44:09 +00:00
|
|
|
|
1999-12-09 05:58:56 +00:00
|
|
|
Assert(var->varno > 0 &&
|
2005-10-15 02:49:52 +00:00
|
|
|
(int) var->varno <= list_length(context->pstate->p_rtable));
|
1999-12-09 05:58:56 +00:00
|
|
|
rte = rt_fetch(var->varno, context->pstate->p_rtable);
|
2010-08-07 02:44:09 +00:00
|
|
|
if (rte->rtekind == RTE_RELATION)
|
|
|
|
{
|
|
|
|
if (check_functional_grouping(rte->relid,
|
|
|
|
var->varno,
|
|
|
|
0,
|
|
|
|
context->groupClauses,
|
|
|
|
&context->qry->constraintDeps))
|
|
|
|
{
|
|
|
|
*context->func_grouped_rels =
|
|
|
|
lappend_int(*context->func_grouped_rels, var->varno);
|
2011-04-10 11:42:00 -04:00
|
|
|
return false; /* acceptable */
|
2010-08-07 02:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Found an ungrouped local variable; generate error message */
|
2000-09-25 18:14:55 +00:00
|
|
|
attname = get_rte_attribute_name(rte, var->varattno);
|
2003-01-17 03:25:04 +00:00
|
|
|
if (context->sublevels_up == 0)
|
2003-07-19 20:20:53 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
2003-09-25 15:58:06 +00:00
|
|
|
errmsg("column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
|
2008-09-01 20:42:46 +00:00
|
|
|
rte->eref->aliasname, attname),
|
|
|
|
parser_errposition(context->pstate, var->location)));
|
2003-01-17 03:25:04 +00:00
|
|
|
else
|
2003-07-19 20:20:53 +00:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_GROUPING_ERROR),
|
2003-09-25 06:58:07 +00:00
|
|
|
errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
|
2008-09-01 20:42:46 +00:00
|
|
|
rte->eref->aliasname, attname),
|
|
|
|
parser_errposition(context->pstate, var->location)));
|
2003-01-17 03:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsA(node, Query))
|
|
|
|
{
|
|
|
|
/* Recurse into subselects */
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
context->sublevels_up++;
|
|
|
|
result = query_tree_walker((Query *) node,
|
|
|
|
check_ungrouped_columns_walker,
|
|
|
|
(void *) context,
|
|
|
|
0);
|
|
|
|
context->sublevels_up--;
|
|
|
|
return result;
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
1999-12-09 05:58:56 +00:00
|
|
|
return expression_tree_walker(node, check_ungrouped_columns_walker,
|
|
|
|
(void *) context);
|
1997-11-25 22:07:18 +00:00
|
|
|
}
|
2003-07-01 19:10:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create expression trees for the transition and final functions
|
|
|
|
* of an aggregate. These are needed so that polymorphic functions
|
|
|
|
* can be used within an aggregate --- without the expression trees,
|
|
|
|
* such functions would not know the datatypes they are supposed to use.
|
|
|
|
* (The trees will never actually be executed, however, so we can skimp
|
|
|
|
* a bit on correctness.)
|
|
|
|
*
|
2006-07-27 19:52:07 +00:00
|
|
|
* agg_input_types, agg_state_type, agg_result_type identify the input,
|
2003-07-01 19:10:53 +00:00
|
|
|
* transition, and result types of the aggregate. These should all be
|
2007-06-06 23:00:50 +00:00
|
|
|
* resolved to actual types (ie, none should ever be ANYELEMENT etc).
|
2011-03-19 20:29:08 -04:00
|
|
|
* agg_input_collation is the aggregate function's input collation.
|
2003-07-01 19:10:53 +00:00
|
|
|
*
|
|
|
|
* transfn_oid and finalfn_oid identify the funcs to be called; the latter
|
|
|
|
* may be InvalidOid.
|
|
|
|
*
|
|
|
|
* Pointers to the constructed trees are returned into *transfnexpr and
|
|
|
|
* *finalfnexpr. The latter is set to NULL if there's no finalfn.
|
|
|
|
*/
|
|
|
|
void
|
2006-07-27 19:52:07 +00:00
|
|
|
build_aggregate_fnexprs(Oid *agg_input_types,
|
|
|
|
int agg_num_inputs,
|
2003-07-01 19:10:53 +00:00
|
|
|
Oid agg_state_type,
|
|
|
|
Oid agg_result_type,
|
2011-03-19 20:29:08 -04:00
|
|
|
Oid agg_input_collation,
|
2003-07-01 19:10:53 +00:00
|
|
|
Oid transfn_oid,
|
|
|
|
Oid finalfn_oid,
|
|
|
|
Expr **transfnexpr,
|
|
|
|
Expr **finalfnexpr)
|
|
|
|
{
|
2006-07-27 19:52:07 +00:00
|
|
|
Param *argp;
|
2003-07-01 19:10:53 +00:00
|
|
|
List *args;
|
2006-07-27 19:52:07 +00:00
|
|
|
int i;
|
2003-07-01 19:10:53 +00:00
|
|
|
|
|
|
|
/*
|
2005-10-15 02:49:52 +00:00
|
|
|
* Build arg list to use in the transfn FuncExpr node. We really only care
|
|
|
|
* that transfn can discover the actual argument types at runtime using
|
|
|
|
* get_fn_expr_argtype(), so it's okay to use Param nodes that don't
|
|
|
|
* correspond to any real Param.
|
2003-07-01 19:10:53 +00:00
|
|
|
*/
|
2006-07-27 19:52:07 +00:00
|
|
|
argp = makeNode(Param);
|
|
|
|
argp->paramkind = PARAM_EXEC;
|
|
|
|
argp->paramid = -1;
|
|
|
|
argp->paramtype = agg_state_type;
|
2006-12-10 22:13:27 +00:00
|
|
|
argp->paramtypmod = -1;
|
2011-03-19 20:29:08 -04:00
|
|
|
argp->paramcollid = agg_input_collation;
|
2008-08-28 23:09:48 +00:00
|
|
|
argp->location = -1;
|
2003-07-01 19:10:53 +00:00
|
|
|
|
2006-07-27 19:52:07 +00:00
|
|
|
args = list_make1(argp);
|
2003-07-01 19:10:53 +00:00
|
|
|
|
2006-07-27 19:52:07 +00:00
|
|
|
for (i = 0; i < agg_num_inputs; i++)
|
|
|
|
{
|
|
|
|
argp = makeNode(Param);
|
|
|
|
argp->paramkind = PARAM_EXEC;
|
|
|
|
argp->paramid = -1;
|
|
|
|
argp->paramtype = agg_input_types[i];
|
2006-12-10 22:13:27 +00:00
|
|
|
argp->paramtypmod = -1;
|
2011-03-19 20:29:08 -04:00
|
|
|
argp->paramcollid = agg_input_collation;
|
2008-08-28 23:09:48 +00:00
|
|
|
argp->location = -1;
|
2006-07-27 19:52:07 +00:00
|
|
|
args = lappend(args, argp);
|
2003-07-01 19:10:53 +00:00
|
|
|
}
|
2003-08-04 00:43:34 +00:00
|
|
|
|
|
|
|
*transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
|
|
|
|
agg_state_type,
|
|
|
|
args,
|
2011-03-19 20:29:08 -04:00
|
|
|
InvalidOid,
|
|
|
|
agg_input_collation,
|
2012-10-12 13:35:00 -04:00
|
|
|
COERCE_EXPLICIT_CALL);
|
2003-08-04 00:43:34 +00:00
|
|
|
|
|
|
|
/* see if we have a final function */
|
|
|
|
if (!OidIsValid(finalfn_oid))
|
|
|
|
{
|
|
|
|
*finalfnexpr = NULL;
|
|
|
|
return;
|
2003-07-01 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2003-08-04 00:43:34 +00:00
|
|
|
/*
|
|
|
|
* Build expr tree for final function
|
|
|
|
*/
|
2006-07-27 19:52:07 +00:00
|
|
|
argp = makeNode(Param);
|
|
|
|
argp->paramkind = PARAM_EXEC;
|
|
|
|
argp->paramid = -1;
|
|
|
|
argp->paramtype = agg_state_type;
|
2006-12-10 22:13:27 +00:00
|
|
|
argp->paramtypmod = -1;
|
2011-03-19 20:29:08 -04:00
|
|
|
argp->paramcollid = agg_input_collation;
|
2008-08-28 23:09:48 +00:00
|
|
|
argp->location = -1;
|
2006-07-27 19:52:07 +00:00
|
|
|
args = list_make1(argp);
|
2003-07-01 19:10:53 +00:00
|
|
|
|
2003-08-04 00:43:34 +00:00
|
|
|
*finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
|
|
|
|
agg_result_type,
|
|
|
|
args,
|
2011-03-19 20:29:08 -04:00
|
|
|
InvalidOid,
|
|
|
|
agg_input_collation,
|
2012-10-12 13:35:00 -04:00
|
|
|
COERCE_EXPLICIT_CALL);
|
2003-07-01 19:10:53 +00:00
|
|
|
}
|