aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/aggregatecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2013-09-03 17:08:38 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2013-09-03 17:08:46 -0400
commit0d3f4406dfa00d848711fdb4af53be663ffc7d0f (patch)
tree1241750b2425a43f2cdc09cb9f0c8641dc1c4904 /src/backend/commands/aggregatecmds.c
parent8b290f3115db5bbe85176160c7cabe0d927dcc37 (diff)
downloadpostgresql-0d3f4406dfa00d848711fdb4af53be663ffc7d0f.tar.gz
postgresql-0d3f4406dfa00d848711fdb4af53be663ffc7d0f.zip
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/commands/aggregatecmds.c')
-rw-r--r--src/backend/commands/aggregatecmds.c55
1 files changed, 39 insertions, 16 deletions
diff --git a/src/backend/commands/aggregatecmds.c b/src/backend/commands/aggregatecmds.c
index 4a03786210a..78af0924654 100644
--- a/src/backend/commands/aggregatecmds.c
+++ b/src/backend/commands/aggregatecmds.c
@@ -45,10 +45,12 @@
*
* "oldstyle" signals the old (pre-8.2) style where the aggregate input type
* is specified by a BASETYPE element in the parameters. Otherwise,
- * "args" defines the input type(s).
+ * "args" is a list of FunctionParameter structs defining the agg's arguments.
+ * "parameters" is a list of DefElem representing the agg's definition clauses.
*/
Oid
-DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
+DefineAggregate(List *name, List *args, bool oldstyle, List *parameters,
+ const char *queryString)
{
char *aggName;
Oid aggNamespace;
@@ -59,8 +61,12 @@ DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
TypeName *baseType = NULL;
TypeName *transType = NULL;
char *initval = NULL;
- Oid *aggArgTypes;
int numArgs;
+ oidvector *parameterTypes;
+ ArrayType *allParameterTypes;
+ ArrayType *parameterModes;
+ ArrayType *parameterNames;
+ List *parameterDefaults;
Oid transTypeId;
char transTypeType;
ListCell *pl;
@@ -131,6 +137,8 @@ DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
* Historically we allowed the command to look like basetype = 'ANY'
* so we must do a case-insensitive comparison for the name ANY. Ugh.
*/
+ Oid aggArgTypes[1];
+
if (baseType == NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
@@ -139,22 +147,26 @@ DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
if (pg_strcasecmp(TypeNameToString(baseType), "ANY") == 0)
{
numArgs = 0;
- aggArgTypes = NULL;
+ aggArgTypes[0] = InvalidOid;
}
else
{
numArgs = 1;
- aggArgTypes = (Oid *) palloc(sizeof(Oid));
aggArgTypes[0] = typenameTypeId(NULL, baseType);
}
+ parameterTypes = buildoidvector(aggArgTypes, numArgs);
+ allParameterTypes = NULL;
+ parameterModes = NULL;
+ parameterNames = NULL;
+ parameterDefaults = NIL;
}
else
{
/*
- * New style: args is a list of TypeNames (possibly zero of 'em).
+ * New style: args is a list of FunctionParameters (possibly zero of
+ * 'em). We share functioncmds.c's code for processing them.
*/
- ListCell *lc;
- int i = 0;
+ Oid requiredResultType;
if (baseType != NULL)
ereport(ERROR,
@@ -162,13 +174,20 @@ DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
errmsg("basetype is redundant with aggregate input type specification")));
numArgs = list_length(args);
- aggArgTypes = (Oid *) palloc(sizeof(Oid) * numArgs);
- foreach(lc, args)
- {
- TypeName *curTypeName = (TypeName *) lfirst(lc);
-
- aggArgTypes[i++] = typenameTypeId(NULL, curTypeName);
- }
+ interpret_function_parameter_list(args,
+ InvalidOid,
+ true, /* is an aggregate */
+ queryString,
+ &parameterTypes,
+ &allParameterTypes,
+ &parameterModes,
+ &parameterNames,
+ &parameterDefaults,
+ &requiredResultType);
+ /* Parameter defaults are not currently allowed by the grammar */
+ Assert(parameterDefaults == NIL);
+ /* There shouldn't have been any OUT parameters, either */
+ Assert(requiredResultType == InvalidOid);
}
/*
@@ -219,8 +238,12 @@ DefineAggregate(List *name, List *args, bool oldstyle, List *parameters)
*/
return AggregateCreate(aggName, /* aggregate name */
aggNamespace, /* namespace */
- aggArgTypes, /* input data type(s) */
numArgs,
+ parameterTypes,
+ PointerGetDatum(allParameterTypes),
+ PointerGetDatum(parameterModes),
+ PointerGetDatum(parameterNames),
+ parameterDefaults,
transfuncName, /* step function name */
finalfuncName, /* final function name */
sortoperatorName, /* sort operator name */