diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-09-03 17:08:38 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-09-03 17:08:46 -0400 |
commit | 0d3f4406dfa00d848711fdb4af53be663ffc7d0f (patch) | |
tree | 1241750b2425a43f2cdc09cb9f0c8641dc1c4904 /src/backend/utils/adt/ruleutils.c | |
parent | 8b290f3115db5bbe85176160c7cabe0d927dcc37 (diff) |
Allow aggregate functions to be VARIADIC.
There's no inherent reason why an aggregate function can't be variadic
(even VARIADIC ANY) if its transition function can handle the case.
Indeed, this patch to add the feature touches none of the planner or
executor, and little of the parser; the main missing stuff was DDL and
pg_dump support.
It is true that variadic aggregates can create the same sort of ambiguity
about parameters versus ORDER BY keys that was complained of when we
(briefly) had both one- and two-argument forms of string_agg(). However,
the policy formed in response to that discussion only said that we'd not
create any built-in aggregates with varying numbers of arguments, not that
we shouldn't allow users to do it. So the logical extension of that is
we can allow users to make variadic aggregates as long as we're wary about
shipping any such in core.
In passing, this patch allows aggregate function arguments to be named, to
the extent of remembering the names in pg_proc and dumping them in pg_dump.
You can't yet call an aggregate using named-parameter notation. That seems
like a likely future extension, but it'll take some work, and it's not what
this patch is really about. Likewise, there's still some work needed to
make window functions handle VARIADIC fully, but I left that for another
day.
initdb forced because of new aggvariadic field in Aggref parse nodes.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 2b005d6e973..37d66e18e8f 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -7405,6 +7405,7 @@ get_agg_expr(Aggref *aggref, deparse_context *context) Oid argtypes[FUNC_MAX_ARGS]; List *arglist; int nargs; + bool use_variadic; ListCell *l; /* Extract the regular arguments, ignoring resjunk stuff for the moment */ @@ -7430,13 +7431,26 @@ get_agg_expr(Aggref *aggref, deparse_context *context) appendStringInfo(buf, "%s(%s", generate_function_name(aggref->aggfnoid, nargs, NIL, argtypes, - false, NULL), + aggref->aggvariadic, + &use_variadic), (aggref->aggdistinct != NIL) ? "DISTINCT " : ""); + /* aggstar can be set only in zero-argument aggregates */ if (aggref->aggstar) appendStringInfoChar(buf, '*'); else - get_rule_expr((Node *) arglist, context, true); + { + nargs = 0; + foreach(l, arglist) + { + if (nargs++ > 0) + appendStringInfoString(buf, ", "); + if (use_variadic && lnext(l) == NULL) + appendStringInfoString(buf, "VARIADIC "); + get_rule_expr((Node *) lfirst(l), context, true); + } + } + if (aggref->aggorder != NIL) { appendStringInfoString(buf, " ORDER BY "); @@ -8581,7 +8595,7 @@ generate_relation_name(Oid relid, List *namespaces) * types. (Those matter because of ambiguous-function resolution rules.) * * If we're dealing with a potentially variadic function (in practice, this - * means a FuncExpr and not some other way of calling the function), then + * means a FuncExpr or Aggref, not some other way of calling a function), then * was_variadic must specify whether VARIADIC appeared in the original call, * and *use_variadic_p will be set to indicate whether to print VARIADIC in * the output. For non-FuncExpr cases, was_variadic should be FALSE and |