mirror of
https://github.com/postgres/postgres.git
synced 2025-11-15 03:41:20 +03:00
Implement the FILTER clause for aggregate function calls.
This is SQL-standard with a few extensions, namely support for subqueries and outer references in clause expressions. catversion bump due to change in Aggref and WindowFunc. David Fetter, reviewed by Dean Rasheed.
This commit is contained in:
@@ -492,6 +492,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
|
||||
opt_frame_clause frame_extent frame_bound
|
||||
%type <str> opt_existing_window_name
|
||||
%type <boolean> opt_if_not_exists
|
||||
%type <node> filter_clause
|
||||
|
||||
/*
|
||||
* Non-keyword token types. These are hard-wired into the "flex" lexer.
|
||||
@@ -538,8 +539,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
|
||||
EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN
|
||||
EXTENSION EXTERNAL EXTRACT
|
||||
|
||||
FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
|
||||
FREEZE FROM FULL FUNCTION FUNCTIONS
|
||||
FALSE_P FAMILY FETCH FILTER FIRST_P FLOAT_P FOLLOWING FOR
|
||||
FORCE FOREIGN FORWARD FREEZE FROM FULL FUNCTION FUNCTIONS
|
||||
|
||||
GLOBAL GRANT GRANTED GREATEST GROUP_P
|
||||
|
||||
@@ -11112,10 +11113,11 @@ func_application: func_name '(' ')'
|
||||
* (Note that many of the special SQL functions wouldn't actually make any
|
||||
* sense as functional index entries, but we ignore that consideration here.)
|
||||
*/
|
||||
func_expr: func_application over_clause
|
||||
func_expr: func_application filter_clause over_clause
|
||||
{
|
||||
FuncCall *n = (FuncCall*)$1;
|
||||
n->over = $2;
|
||||
n->agg_filter = $2;
|
||||
n->over = $3;
|
||||
$$ = (Node*)n;
|
||||
}
|
||||
| func_expr_common_subexpr
|
||||
@@ -11526,6 +11528,11 @@ window_definition:
|
||||
}
|
||||
;
|
||||
|
||||
filter_clause:
|
||||
FILTER '(' WHERE a_expr ')' { $$ = $4; }
|
||||
| /*EMPTY*/ { $$ = NULL; }
|
||||
;
|
||||
|
||||
over_clause: OVER window_specification
|
||||
{ $$ = $2; }
|
||||
| OVER ColId
|
||||
@@ -12500,6 +12507,7 @@ unreserved_keyword:
|
||||
| EXTENSION
|
||||
| EXTERNAL
|
||||
| FAMILY
|
||||
| FILTER
|
||||
| FIRST_P
|
||||
| FOLLOWING
|
||||
| FORCE
|
||||
|
||||
@@ -44,7 +44,7 @@ typedef struct
|
||||
int sublevels_up;
|
||||
} check_ungrouped_columns_context;
|
||||
|
||||
static int check_agg_arguments(ParseState *pstate, List *args);
|
||||
static int check_agg_arguments(ParseState *pstate, List *args, Expr *filter);
|
||||
static bool check_agg_arguments_walker(Node *node,
|
||||
check_agg_arguments_context *context);
|
||||
static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
|
||||
@@ -160,7 +160,7 @@ transformAggregateCall(ParseState *pstate, Aggref *agg,
|
||||
* Check the arguments to compute the aggregate's level and detect
|
||||
* improper nesting.
|
||||
*/
|
||||
min_varlevel = check_agg_arguments(pstate, agg->args);
|
||||
min_varlevel = check_agg_arguments(pstate, agg->args, agg->aggfilter);
|
||||
agg->agglevelsup = min_varlevel;
|
||||
|
||||
/* Mark the correct pstate level as having aggregates */
|
||||
@@ -207,6 +207,9 @@ transformAggregateCall(ParseState *pstate, Aggref *agg,
|
||||
case EXPR_KIND_HAVING:
|
||||
/* okay */
|
||||
break;
|
||||
case EXPR_KIND_FILTER:
|
||||
errkind = true;
|
||||
break;
|
||||
case EXPR_KIND_WINDOW_PARTITION:
|
||||
/* okay */
|
||||
break;
|
||||
@@ -299,8 +302,8 @@ transformAggregateCall(ParseState *pstate, Aggref *agg,
|
||||
* 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.
|
||||
* or aggregate in its arguments or filter expression; 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
|
||||
@@ -309,7 +312,7 @@ transformAggregateCall(ParseState *pstate, Aggref *agg,
|
||||
* which we can't know until we finish scanning the arguments.
|
||||
*/
|
||||
static int
|
||||
check_agg_arguments(ParseState *pstate, List *args)
|
||||
check_agg_arguments(ParseState *pstate, List *args, Expr *filter)
|
||||
{
|
||||
int agglevel;
|
||||
check_agg_arguments_context context;
|
||||
@@ -323,6 +326,10 @@ check_agg_arguments(ParseState *pstate, List *args)
|
||||
check_agg_arguments_walker,
|
||||
(void *) &context);
|
||||
|
||||
(void) expression_tree_walker((Node *) filter,
|
||||
check_agg_arguments_walker,
|
||||
(void *) &context);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
@@ -481,6 +488,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
|
||||
case EXPR_KIND_HAVING:
|
||||
errkind = true;
|
||||
break;
|
||||
case EXPR_KIND_FILTER:
|
||||
errkind = true;
|
||||
break;
|
||||
case EXPR_KIND_WINDOW_PARTITION:
|
||||
case EXPR_KIND_WINDOW_ORDER:
|
||||
case EXPR_KIND_WINDOW_FRAME_RANGE:
|
||||
@@ -807,11 +817,10 @@ check_ungrouped_columns_walker(Node *node,
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* its arguments or filter; ungrouped vars there are not an error. We can
|
||||
* also skip looking at aggregates of higher levels, since they could not
|
||||
* possibly contain Vars of concern to us (see transformAggregateCall).
|
||||
* We do need to look at aggregates of lower levels, however.
|
||||
*/
|
||||
if (IsA(node, Aggref) &&
|
||||
(int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
|
||||
|
||||
@@ -575,6 +575,10 @@ assign_collations_walker(Node *node, assign_collations_context *context)
|
||||
* the case above for T_TargetEntry will apply
|
||||
* appropriate checks to agg ORDER BY items.
|
||||
*
|
||||
* Likewise, we assign collations for the (bool)
|
||||
* expression in aggfilter, independently of any
|
||||
* other args.
|
||||
*
|
||||
* We need not recurse into the aggorder or
|
||||
* aggdistinct lists, because those contain only
|
||||
* SortGroupClause nodes which we need not
|
||||
@@ -595,6 +599,24 @@ assign_collations_walker(Node *node, assign_collations_context *context)
|
||||
(void) assign_collations_walker((Node *) tle,
|
||||
&loccontext);
|
||||
}
|
||||
|
||||
assign_expr_collations(context->pstate,
|
||||
(Node *) aggref->aggfilter);
|
||||
}
|
||||
break;
|
||||
case T_WindowFunc:
|
||||
{
|
||||
/*
|
||||
* WindowFunc requires special processing only for
|
||||
* its aggfilter clause, as for aggregates.
|
||||
*/
|
||||
WindowFunc *wfunc = (WindowFunc *) node;
|
||||
|
||||
(void) assign_collations_walker((Node *) wfunc->args,
|
||||
&loccontext);
|
||||
|
||||
assign_expr_collations(context->pstate,
|
||||
(Node *) wfunc->aggfilter);
|
||||
}
|
||||
break;
|
||||
case T_CaseExpr:
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "nodes/nodeFuncs.h"
|
||||
#include "optimizer/var.h"
|
||||
#include "parser/analyze.h"
|
||||
#include "parser/parse_clause.h"
|
||||
#include "parser/parse_coerce.h"
|
||||
#include "parser/parse_collate.h"
|
||||
#include "parser/parse_expr.h"
|
||||
@@ -462,7 +463,7 @@ transformIndirection(ParseState *pstate, Node *basenode, List *indirection)
|
||||
newresult = ParseFuncOrColumn(pstate,
|
||||
list_make1(n),
|
||||
list_make1(result),
|
||||
NIL, false, false, false,
|
||||
NIL, NULL, false, false, false,
|
||||
NULL, true, location);
|
||||
if (newresult == NULL)
|
||||
unknown_attribute(pstate, result, strVal(n), location);
|
||||
@@ -630,7 +631,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
|
||||
node = ParseFuncOrColumn(pstate,
|
||||
list_make1(makeString(colname)),
|
||||
list_make1(node),
|
||||
NIL, false, false, false,
|
||||
NIL, NULL, false, false, false,
|
||||
NULL, true, cref->location);
|
||||
}
|
||||
break;
|
||||
@@ -675,7 +676,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
|
||||
node = ParseFuncOrColumn(pstate,
|
||||
list_make1(makeString(colname)),
|
||||
list_make1(node),
|
||||
NIL, false, false, false,
|
||||
NIL, NULL, false, false, false,
|
||||
NULL, true, cref->location);
|
||||
}
|
||||
break;
|
||||
@@ -733,7 +734,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
|
||||
node = ParseFuncOrColumn(pstate,
|
||||
list_make1(makeString(colname)),
|
||||
list_make1(node),
|
||||
NIL, false, false, false,
|
||||
NIL, NULL, false, false, false,
|
||||
NULL, true, cref->location);
|
||||
}
|
||||
break;
|
||||
@@ -1241,6 +1242,7 @@ transformFuncCall(ParseState *pstate, FuncCall *fn)
|
||||
{
|
||||
List *targs;
|
||||
ListCell *args;
|
||||
Expr *tagg_filter;
|
||||
|
||||
/* Transform the list of arguments ... */
|
||||
targs = NIL;
|
||||
@@ -1250,11 +1252,22 @@ transformFuncCall(ParseState *pstate, FuncCall *fn)
|
||||
(Node *) lfirst(args)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Transform the aggregate filter using transformWhereClause(), to which
|
||||
* FILTER is virtually identical...
|
||||
*/
|
||||
tagg_filter = NULL;
|
||||
if (fn->agg_filter != NULL)
|
||||
tagg_filter = (Expr *)
|
||||
transformWhereClause(pstate, (Node *) fn->agg_filter,
|
||||
EXPR_KIND_FILTER, "FILTER");
|
||||
|
||||
/* ... and hand off to ParseFuncOrColumn */
|
||||
return ParseFuncOrColumn(pstate,
|
||||
fn->funcname,
|
||||
targs,
|
||||
fn->agg_order,
|
||||
tagg_filter,
|
||||
fn->agg_star,
|
||||
fn->agg_distinct,
|
||||
fn->func_variadic,
|
||||
@@ -1430,6 +1443,7 @@ transformSubLink(ParseState *pstate, SubLink *sublink)
|
||||
case EXPR_KIND_FROM_FUNCTION:
|
||||
case EXPR_KIND_WHERE:
|
||||
case EXPR_KIND_HAVING:
|
||||
case EXPR_KIND_FILTER:
|
||||
case EXPR_KIND_WINDOW_PARTITION:
|
||||
case EXPR_KIND_WINDOW_ORDER:
|
||||
case EXPR_KIND_WINDOW_FRAME_RANGE:
|
||||
@@ -2579,6 +2593,8 @@ ParseExprKindName(ParseExprKind exprKind)
|
||||
return "WHERE";
|
||||
case EXPR_KIND_HAVING:
|
||||
return "HAVING";
|
||||
case EXPR_KIND_FILTER:
|
||||
return "FILTER";
|
||||
case EXPR_KIND_WINDOW_PARTITION:
|
||||
return "window PARTITION BY";
|
||||
case EXPR_KIND_WINDOW_ORDER:
|
||||
|
||||
@@ -56,13 +56,13 @@ static Node *ParseComplexProjection(ParseState *pstate, char *funcname,
|
||||
* Also, when is_column is true, we return NULL on failure rather than
|
||||
* reporting a no-such-function error.
|
||||
*
|
||||
* The argument expressions (in fargs) must have been transformed already.
|
||||
* But the agg_order expressions, if any, have not been.
|
||||
* The argument expressions (in fargs) and filter must have been transformed
|
||||
* already. But the agg_order expressions, if any, have not been.
|
||||
*/
|
||||
Node *
|
||||
ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
List *agg_order, bool agg_star, bool agg_distinct,
|
||||
bool func_variadic,
|
||||
List *agg_order, Expr *agg_filter,
|
||||
bool agg_star, bool agg_distinct, bool func_variadic,
|
||||
WindowDef *over, bool is_column, int location)
|
||||
{
|
||||
Oid rettype;
|
||||
@@ -174,8 +174,8 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
* the "function call" could be a projection. We also check that there
|
||||
* wasn't any aggregate or variadic decoration, nor an argument name.
|
||||
*/
|
||||
if (nargs == 1 && agg_order == NIL && !agg_star && !agg_distinct &&
|
||||
over == NULL && !func_variadic && argnames == NIL &&
|
||||
if (nargs == 1 && agg_order == NIL && agg_filter == NULL && !agg_star &&
|
||||
!agg_distinct && over == NULL && !func_variadic && argnames == NIL &&
|
||||
list_length(funcname) == 1)
|
||||
{
|
||||
Oid argtype = actual_arg_types[0];
|
||||
@@ -251,6 +251,12 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
errmsg("ORDER BY specified, but %s is not an aggregate function",
|
||||
NameListToString(funcname)),
|
||||
parser_errposition(pstate, location)));
|
||||
if (agg_filter)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("FILTER specified, but %s is not an aggregate function",
|
||||
NameListToString(funcname)),
|
||||
parser_errposition(pstate, location)));
|
||||
if (over)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
@@ -402,6 +408,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
/* aggcollid and inputcollid will be set by parse_collate.c */
|
||||
/* args, aggorder, aggdistinct will be set by transformAggregateCall */
|
||||
aggref->aggstar = agg_star;
|
||||
aggref->aggfilter = agg_filter;
|
||||
/* agglevelsup will be set by transformAggregateCall */
|
||||
aggref->location = location;
|
||||
|
||||
@@ -460,6 +467,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
/* winref will be set by transformWindowFuncCall */
|
||||
wfunc->winstar = agg_star;
|
||||
wfunc->winagg = (fdresult == FUNCDETAIL_AGGREGATE);
|
||||
wfunc->aggfilter = agg_filter;
|
||||
wfunc->location = location;
|
||||
|
||||
/*
|
||||
@@ -482,6 +490,16 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
||||
NameListToString(funcname)),
|
||||
parser_errposition(pstate, location)));
|
||||
|
||||
/*
|
||||
* Reject window functions which are not aggregates in the case of
|
||||
* FILTER.
|
||||
*/
|
||||
if (!wfunc->winagg && agg_filter)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("FILTER is not implemented in non-aggregate window functions"),
|
||||
parser_errposition(pstate, location)));
|
||||
|
||||
/*
|
||||
* ordered aggs not allowed in windows yet
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user