2002-04-15 07:22:04 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* aggregatecmds.c
|
|
|
|
*
|
|
|
|
* Routines for aggregate-manipulation commands
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2002-04-15 07:22:04 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/aggregatecmds.c
|
2002-04-15 07:22:04 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* The "DefineFoo" routines take the parse tree and pick out the
|
|
|
|
* appropriate arguments/flags, passing the results to the
|
|
|
|
* corresponding "FooDefine" routines (in src/catalog) that do
|
|
|
|
* the actual catalog-munging. These routines also verify permission
|
|
|
|
* of the user to execute the command.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/dependency.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "catalog/indexing.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "catalog/pg_aggregate.h"
|
2002-04-27 05:45:03 +02:00
|
|
|
#include "catalog/pg_proc.h"
|
2002-08-22 02:01:51 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2013-03-07 02:52:06 +01:00
|
|
|
#include "commands/alter.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "parser/parse_func.h"
|
|
|
|
#include "parser/parse_type.h"
|
|
|
|
#include "utils/acl.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DefineAggregate
|
2006-04-15 19:45:46 +02:00
|
|
|
*
|
|
|
|
* "oldstyle" signals the old (pre-8.2) style where the aggregate input type
|
|
|
|
* is specified by a BASETYPE element in the parameters. Otherwise,
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
* "args" is a pair, whose first element is a list of FunctionParameter structs
|
|
|
|
* defining the agg's arguments (both direct and aggregated), and whose second
|
|
|
|
* element is an Integer node with the number of direct args, or -1 if this
|
|
|
|
* isn't an ordered-set aggregate.
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
* "parameters" is a list of DefElem representing the agg's definition clauses.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
Change many routines to return ObjectAddress rather than OID
The changed routines are mostly those that can be directly called by
ProcessUtilitySlow; the intention is to make the affected object
information more precise, in support for future event trigger changes.
Originally it was envisioned that the OID of the affected object would
be enough, and in most cases that is correct, but upon actually
implementing the event trigger changes it turned out that ObjectAddress
is more widely useful.
Additionally, some command execution routines grew an output argument
that's an object address which provides further info about the executed
command. To wit:
* for ALTER DOMAIN / ADD CONSTRAINT, it corresponds to the address of
the new constraint
* for ALTER OBJECT / SET SCHEMA, it corresponds to the address of the
schema that originally contained the object.
* for ALTER EXTENSION {ADD, DROP} OBJECT, it corresponds to the address
of the object added to or dropped from the extension.
There's no user-visible change in this commit, and no functional change
either.
Discussion: 20150218213255.GC6717@tamriel.snowman.net
Reviewed-By: Stephen Frost, Andres Freund
2015-03-03 18:10:50 +01:00
|
|
|
ObjectAddress
|
2016-09-06 18:00:00 +02:00
|
|
|
DefineAggregate(ParseState *pstate, List *name, List *args, bool oldstyle, List *parameters)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
char *aggName;
|
|
|
|
Oid aggNamespace;
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult aclresult;
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
char aggKind = AGGKIND_NORMAL;
|
2002-04-15 07:22:04 +02:00
|
|
|
List *transfuncName = NIL;
|
|
|
|
List *finalfuncName = NIL;
|
2016-01-20 19:46:50 +01:00
|
|
|
List *combinefuncName = NIL;
|
2016-03-29 21:04:05 +02:00
|
|
|
List *serialfuncName = NIL;
|
|
|
|
List *deserialfuncName = NIL;
|
2014-04-12 17:58:53 +02:00
|
|
|
List *mtransfuncName = NIL;
|
|
|
|
List *minvtransfuncName = NIL;
|
|
|
|
List *mfinalfuncName = NIL;
|
Allow polymorphic aggregates to have non-polymorphic state data types.
Before 9.4, such an aggregate couldn't be declared, because its final
function would have to have polymorphic result type but no polymorphic
argument, which CREATE FUNCTION would quite properly reject. The
ordered-set-aggregate patch found a workaround: allow the final function
to be declared as accepting additional dummy arguments that have types
matching the aggregate's regular input arguments. However, we failed
to notice that this problem applies just as much to regular aggregates,
despite the fact that we had a built-in regular aggregate array_agg()
that was known to be undeclarable in SQL because its final function
had an illegal signature. So what we should have done, and what this
patch does, is to decouple the extra-dummy-arguments behavior from
ordered-set aggregates and make it generally available for all aggregate
declarations. We have to put this into 9.4 rather than waiting till
later because it slightly alters the rules for declaring ordered-set
aggregates.
The patch turned out a bit bigger than I'd hoped because it proved
necessary to record the extra-arguments option in a new pg_aggregate
column. I'd thought we could just look at the final function's pronargs
at runtime, but that didn't work well for variadic final functions.
It's probably just as well though, because it simplifies life for pg_dump
to record the option explicitly.
While at it, fix array_agg() to have a valid final-function signature,
and add an opr_sanity test to notice future deviations from polymorphic
consistency. I also marked the percentile_cont() aggregates as not
needing extra arguments, since they don't.
2014-04-24 01:17:31 +02:00
|
|
|
bool finalfuncExtraArgs = false;
|
|
|
|
bool mfinalfuncExtraArgs = false;
|
2005-04-12 06:26:34 +02:00
|
|
|
List *sortoperatorName = NIL;
|
2002-04-15 07:22:04 +02:00
|
|
|
TypeName *baseType = NULL;
|
|
|
|
TypeName *transType = NULL;
|
2014-04-12 17:58:53 +02:00
|
|
|
TypeName *mtransType = NULL;
|
2013-11-16 22:03:40 +01:00
|
|
|
int32 transSpace = 0;
|
2014-04-12 17:58:53 +02:00
|
|
|
int32 mtransSpace = 0;
|
2002-04-15 07:22:04 +02:00
|
|
|
char *initval = NULL;
|
2014-04-12 17:58:53 +02:00
|
|
|
char *minitval = NULL;
|
2016-04-05 22:06:15 +02:00
|
|
|
char *parallel = NULL;
|
2006-10-04 02:30:14 +02:00
|
|
|
int numArgs;
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
int numDirectArgs = 0;
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
oidvector *parameterTypes;
|
|
|
|
ArrayType *allParameterTypes;
|
|
|
|
ArrayType *parameterModes;
|
|
|
|
ArrayType *parameterNames;
|
|
|
|
List *parameterDefaults;
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
Oid variadicArgType;
|
2002-04-15 07:22:04 +02:00
|
|
|
Oid transTypeId;
|
2014-04-12 17:58:53 +02:00
|
|
|
Oid mtransTypeId = InvalidOid;
|
2012-10-04 23:54:53 +02:00
|
|
|
char transTypeType;
|
2014-04-12 17:58:53 +02:00
|
|
|
char mtransTypeType = 0;
|
2016-04-05 22:06:15 +02:00
|
|
|
char proparallel = PROPARALLEL_UNSAFE;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *pl;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Convert list of names to a name and namespace */
|
2006-04-15 19:45:46 +02:00
|
|
|
aggNamespace = QualifiedNameGetCreationNamespace(name, &aggName);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Check we have creation rights in target namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(aggNamespace, GetUserId(), ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(aggNamespace));
|
2002-04-27 05:45:03 +02:00
|
|
|
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
/* Deconstruct the output of the aggr_args grammar production */
|
|
|
|
if (!oldstyle)
|
|
|
|
{
|
|
|
|
Assert(list_length(args) == 2);
|
|
|
|
numDirectArgs = intVal(lsecond(args));
|
|
|
|
if (numDirectArgs >= 0)
|
|
|
|
aggKind = AGGKIND_ORDERED_SET;
|
|
|
|
else
|
|
|
|
numDirectArgs = 0;
|
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type. For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))". Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.
As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.
Patch by me, after an idea of Andrew Gierth's.
Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
2017-04-10 19:51:29 +02:00
|
|
|
args = linitial_node(List, args);
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Examine aggregate's definition clauses */
|
2002-04-15 07:22:04 +02:00
|
|
|
foreach(pl, parameters)
|
|
|
|
{
|
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type. For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))". Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.
As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.
Patch by me, after an idea of Andrew Gierth's.
Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
2017-04-10 19:51:29 +02:00
|
|
|
DefElem *defel = lfirst_node(DefElem, pl);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* sfunc1, stype1, and initcond1 are accepted as obsolete spellings
|
|
|
|
* for sfunc, stype, initcond.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(defel->defname, "sfunc") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
transfuncName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "sfunc1") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
transfuncName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "finalfunc") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
finalfuncName = defGetQualifiedName(defel);
|
2016-01-20 19:46:50 +01:00
|
|
|
else if (pg_strcasecmp(defel->defname, "combinefunc") == 0)
|
|
|
|
combinefuncName = defGetQualifiedName(defel);
|
2016-03-29 21:04:05 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "serialfunc") == 0)
|
|
|
|
serialfuncName = defGetQualifiedName(defel);
|
|
|
|
else if (pg_strcasecmp(defel->defname, "deserialfunc") == 0)
|
|
|
|
deserialfuncName = defGetQualifiedName(defel);
|
2014-04-12 17:58:53 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "msfunc") == 0)
|
|
|
|
mtransfuncName = defGetQualifiedName(defel);
|
|
|
|
else if (pg_strcasecmp(defel->defname, "minvfunc") == 0)
|
|
|
|
minvtransfuncName = defGetQualifiedName(defel);
|
|
|
|
else if (pg_strcasecmp(defel->defname, "mfinalfunc") == 0)
|
|
|
|
mfinalfuncName = defGetQualifiedName(defel);
|
Allow polymorphic aggregates to have non-polymorphic state data types.
Before 9.4, such an aggregate couldn't be declared, because its final
function would have to have polymorphic result type but no polymorphic
argument, which CREATE FUNCTION would quite properly reject. The
ordered-set-aggregate patch found a workaround: allow the final function
to be declared as accepting additional dummy arguments that have types
matching the aggregate's regular input arguments. However, we failed
to notice that this problem applies just as much to regular aggregates,
despite the fact that we had a built-in regular aggregate array_agg()
that was known to be undeclarable in SQL because its final function
had an illegal signature. So what we should have done, and what this
patch does, is to decouple the extra-dummy-arguments behavior from
ordered-set aggregates and make it generally available for all aggregate
declarations. We have to put this into 9.4 rather than waiting till
later because it slightly alters the rules for declaring ordered-set
aggregates.
The patch turned out a bit bigger than I'd hoped because it proved
necessary to record the extra-arguments option in a new pg_aggregate
column. I'd thought we could just look at the final function's pronargs
at runtime, but that didn't work well for variadic final functions.
It's probably just as well though, because it simplifies life for pg_dump
to record the option explicitly.
While at it, fix array_agg() to have a valid final-function signature,
and add an opr_sanity test to notice future deviations from polymorphic
consistency. I also marked the percentile_cont() aggregates as not
needing extra arguments, since they don't.
2014-04-24 01:17:31 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "finalfunc_extra") == 0)
|
|
|
|
finalfuncExtraArgs = defGetBoolean(defel);
|
|
|
|
else if (pg_strcasecmp(defel->defname, "mfinalfunc_extra") == 0)
|
|
|
|
mfinalfuncExtraArgs = defGetBoolean(defel);
|
2005-04-12 06:26:34 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "sortop") == 0)
|
|
|
|
sortoperatorName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "basetype") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
baseType = defGetTypeName(defel);
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
else if (pg_strcasecmp(defel->defname, "hypothetical") == 0)
|
|
|
|
{
|
|
|
|
if (defGetBoolean(defel))
|
|
|
|
{
|
|
|
|
if (aggKind == AGGKIND_NORMAL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("only ordered-set aggregates can be hypothetical")));
|
|
|
|
aggKind = AGGKIND_HYPOTHETICAL;
|
|
|
|
}
|
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "stype") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
transType = defGetTypeName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "stype1") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
transType = defGetTypeName(defel);
|
2013-11-16 22:03:40 +01:00
|
|
|
else if (pg_strcasecmp(defel->defname, "sspace") == 0)
|
|
|
|
transSpace = defGetInt32(defel);
|
2014-04-12 17:58:53 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "mstype") == 0)
|
|
|
|
mtransType = defGetTypeName(defel);
|
|
|
|
else if (pg_strcasecmp(defel->defname, "msspace") == 0)
|
|
|
|
mtransSpace = defGetInt32(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "initcond") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
initval = defGetString(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "initcond1") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
initval = defGetString(defel);
|
2014-04-12 17:58:53 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "minitcond") == 0)
|
|
|
|
minitval = defGetString(defel);
|
2016-04-05 22:06:15 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "parallel") == 0)
|
|
|
|
parallel = defGetString(defel);
|
2002-04-15 07:22:04 +02:00
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(WARNING,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("aggregate attribute \"%s\" not recognized",
|
|
|
|
defel->defname)));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure we have our required definitions
|
|
|
|
*/
|
|
|
|
if (transType == NULL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate stype must be specified")));
|
2002-04-15 07:22:04 +02:00
|
|
|
if (transfuncName == NIL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate sfunc must be specified")));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2014-04-12 17:58:53 +02:00
|
|
|
/*
|
|
|
|
* if mtransType is given, mtransfuncName and minvtransfuncName must be as
|
|
|
|
* well; if not, then none of the moving-aggregate options should have
|
|
|
|
* been given.
|
|
|
|
*/
|
|
|
|
if (mtransType != NULL)
|
|
|
|
{
|
|
|
|
if (mtransfuncName == NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate msfunc must be specified when mstype is specified")));
|
|
|
|
if (minvtransfuncName == NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate minvfunc must be specified when mstype is specified")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mtransfuncName != NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate msfunc must not be specified without mstype")));
|
|
|
|
if (minvtransfuncName != NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate minvfunc must not be specified without mstype")));
|
|
|
|
if (mfinalfuncName != NIL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate mfinalfunc must not be specified without mstype")));
|
|
|
|
if (mtransSpace != 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate msspace must not be specified without mstype")));
|
|
|
|
if (minitval != NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate minitcond must not be specified without mstype")));
|
|
|
|
}
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
2006-07-27 21:52:07 +02:00
|
|
|
* look up the aggregate's input datatype(s).
|
2006-04-15 19:45:46 +02:00
|
|
|
*/
|
|
|
|
if (oldstyle)
|
|
|
|
{
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Old style: use basetype parameter. This supports aggregates of
|
|
|
|
* zero or one input, with input type ANY meaning zero inputs.
|
2006-04-15 19:45:46 +02:00
|
|
|
*
|
|
|
|
* Historically we allowed the command to look like basetype = 'ANY'
|
|
|
|
* so we must do a case-insensitive comparison for the name ANY. Ugh.
|
|
|
|
*/
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
Oid aggArgTypes[1];
|
|
|
|
|
2006-04-15 19:45:46 +02:00
|
|
|
if (baseType == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate input type must be specified")));
|
|
|
|
|
|
|
|
if (pg_strcasecmp(TypeNameToString(baseType), "ANY") == 0)
|
2006-07-27 21:52:07 +02:00
|
|
|
{
|
|
|
|
numArgs = 0;
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
aggArgTypes[0] = InvalidOid;
|
2006-07-27 21:52:07 +02:00
|
|
|
}
|
2006-04-15 19:45:46 +02:00
|
|
|
else
|
2006-07-27 21:52:07 +02:00
|
|
|
{
|
|
|
|
numArgs = 1;
|
2010-10-25 20:40:46 +02:00
|
|
|
aggArgTypes[0] = typenameTypeId(NULL, baseType);
|
2006-07-27 21:52:07 +02:00
|
|
|
}
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
parameterTypes = buildoidvector(aggArgTypes, numArgs);
|
|
|
|
allParameterTypes = NULL;
|
|
|
|
parameterModes = NULL;
|
|
|
|
parameterNames = NULL;
|
|
|
|
parameterDefaults = NIL;
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
variadicArgType = InvalidOid;
|
2006-04-15 19:45:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
* New style: args is a list of FunctionParameters (possibly zero of
|
|
|
|
* 'em). We share functioncmds.c's code for processing them.
|
2006-04-15 19:45:46 +02:00
|
|
|
*/
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
Oid requiredResultType;
|
2006-07-27 21:52:07 +02:00
|
|
|
|
2006-04-15 19:45:46 +02:00
|
|
|
if (baseType != NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("basetype is redundant with aggregate input type specification")));
|
|
|
|
|
2006-07-27 21:52:07 +02:00
|
|
|
numArgs = list_length(args);
|
2016-09-06 18:00:00 +02:00
|
|
|
interpret_function_parameter_list(pstate,
|
|
|
|
args,
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
InvalidOid,
|
|
|
|
true, /* is an aggregate */
|
|
|
|
¶meterTypes,
|
|
|
|
&allParameterTypes,
|
|
|
|
¶meterModes,
|
|
|
|
¶meterNames,
|
|
|
|
¶meterDefaults,
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
&variadicArgType,
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
&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);
|
2006-04-15 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look up the aggregate's transtype.
|
2002-08-22 02:01:51 +02:00
|
|
|
*
|
2006-10-04 02:30:14 +02:00
|
|
|
* transtype can't be a pseudo-type, since we need to be able to store
|
|
|
|
* values of the transtype. However, we can allow polymorphic transtype
|
2014-05-06 18:12:18 +02:00
|
|
|
* in some cases (AggregateCreate will check). Also, we allow "internal"
|
2008-11-14 20:47:50 +01:00
|
|
|
* for functions that want to pass pointers to private data structures;
|
2009-06-11 16:49:15 +02:00
|
|
|
* but allow that only to superusers, since you could crash the system (or
|
|
|
|
* worse) by connecting up incompatible internal-using functions in an
|
|
|
|
* aggregate.
|
2002-04-15 07:22:04 +02:00
|
|
|
*/
|
2010-10-25 20:40:46 +02:00
|
|
|
transTypeId = typenameTypeId(NULL, transType);
|
2012-10-04 23:54:53 +02:00
|
|
|
transTypeType = get_typtype(transTypeId);
|
|
|
|
if (transTypeType == TYPTYPE_PSEUDO &&
|
2007-04-02 05:49:42 +02:00
|
|
|
!IsPolymorphicType(transTypeId))
|
2008-11-14 20:47:50 +01:00
|
|
|
{
|
|
|
|
if (transTypeId == INTERNALOID && superuser())
|
2009-06-11 16:49:15 +02:00
|
|
|
/* okay */ ;
|
2008-11-14 20:47:50 +01:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate transition data type cannot be %s",
|
|
|
|
format_type_be(transTypeId))));
|
|
|
|
}
|
2002-04-15 07:22:04 +02:00
|
|
|
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
if (serialfuncName && deserialfuncName)
|
2016-03-29 21:04:05 +02:00
|
|
|
{
|
|
|
|
/*
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
* Serialization is only needed/allowed for transtype INTERNAL.
|
2016-03-29 21:04:05 +02:00
|
|
|
*/
|
|
|
|
if (transTypeId != INTERNALOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
errmsg("serialization functions may be specified only when the aggregate transition data type is %s",
|
2016-06-10 00:02:36 +02:00
|
|
|
format_type_be(INTERNALOID))));
|
2016-03-29 21:04:05 +02:00
|
|
|
}
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
else if (serialfuncName || deserialfuncName)
|
2016-03-29 21:04:05 +02:00
|
|
|
{
|
|
|
|
/*
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
* Cannot specify one function without the other.
|
2016-03-29 21:04:05 +02:00
|
|
|
*/
|
Fix type-safety problem with parallel aggregate serial/deserialization.
The original specification for this called for the deserialization function
to have signature "deserialize(serialtype) returns transtype", which is a
security violation if transtype is INTERNAL (which it always would be in
practice) and serialtype is not (which ditto). The patch blithely overrode
the opr_sanity check for that, which was sloppy-enough work in itself,
but the indisputable reason this cannot be allowed to stand is that CREATE
FUNCTION will reject such a signature and thus it'd be impossible for
extensions to create parallelizable aggregates.
The minimum fix to make the signature type-safe is to add a second, dummy
argument of type INTERNAL. But to lock it down a bit more and make misuse
of INTERNAL-accepting functions less likely, let's get rid of the ability
to specify a "serialtype" for an aggregate and just say that the only
useful serialtype is BYTEA --- which, in practice, is the only interesting
value anyway, due to the usefulness of the send/recv infrastructure for
this purpose. That means we only have to allow "serialize(internal)
returns bytea" and "deserialize(bytea, internal) returns internal" as
the signatures for these support functions.
In passing fix bogus signature of int4_avg_combine, which I found thanks
to adding an opr_sanity check on combinefunc signatures.
catversion bump due to removing pg_aggregate.aggserialtype and adjusting
signatures of assorted built-in functions.
David Rowley and Tom Lane
Discussion: <27247.1466185504@sss.pgh.pa.us>
2016-06-22 22:52:41 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("must specify both or neither of serialization and deserialization functions")));
|
2016-03-29 21:04:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-12 17:58:53 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* If a moving-aggregate transtype is specified, look that up. Same
|
2014-04-12 17:58:53 +02:00
|
|
|
* restrictions as for transtype.
|
|
|
|
*/
|
|
|
|
if (mtransType)
|
|
|
|
{
|
|
|
|
mtransTypeId = typenameTypeId(NULL, mtransType);
|
|
|
|
mtransTypeType = get_typtype(mtransTypeId);
|
|
|
|
if (mtransTypeType == TYPTYPE_PSEUDO &&
|
|
|
|
!IsPolymorphicType(mtransTypeId))
|
|
|
|
{
|
|
|
|
if (mtransTypeId == INTERNALOID && superuser())
|
|
|
|
/* okay */ ;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("aggregate transition data type cannot be %s",
|
|
|
|
format_type_be(mtransTypeId))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:54:53 +02:00
|
|
|
/*
|
|
|
|
* If we have an initval, and it's not for a pseudotype (particularly a
|
|
|
|
* polymorphic type), make sure it's acceptable to the type's input
|
|
|
|
* function. We will store the initval as text, because the input
|
|
|
|
* function isn't necessarily immutable (consider "now" for timestamp),
|
|
|
|
* and we want to use the runtime not creation-time interpretation of the
|
|
|
|
* value. However, if it's an incorrect value it seems much more
|
|
|
|
* user-friendly to complain at CREATE AGGREGATE time.
|
|
|
|
*/
|
|
|
|
if (initval && transTypeType != TYPTYPE_PSEUDO)
|
|
|
|
{
|
|
|
|
Oid typinput,
|
|
|
|
typioparam;
|
|
|
|
|
|
|
|
getTypeInputInfo(transTypeId, &typinput, &typioparam);
|
|
|
|
(void) OidInputFunctionCall(typinput, initval, typioparam, -1);
|
|
|
|
}
|
|
|
|
|
2014-04-12 17:58:53 +02:00
|
|
|
/*
|
|
|
|
* Likewise for moving-aggregate initval.
|
|
|
|
*/
|
|
|
|
if (minitval && mtransTypeType != TYPTYPE_PSEUDO)
|
|
|
|
{
|
|
|
|
Oid typinput,
|
|
|
|
typioparam;
|
|
|
|
|
|
|
|
getTypeInputInfo(mtransTypeId, &typinput, &typioparam);
|
|
|
|
(void) OidInputFunctionCall(typinput, minitval, typioparam, -1);
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:06:15 +02:00
|
|
|
if (parallel)
|
|
|
|
{
|
|
|
|
if (pg_strcasecmp(parallel, "safe") == 0)
|
|
|
|
proparallel = PROPARALLEL_SAFE;
|
|
|
|
else if (pg_strcasecmp(parallel, "restricted") == 0)
|
|
|
|
proparallel = PROPARALLEL_RESTRICTED;
|
|
|
|
else if (pg_strcasecmp(parallel, "unsafe") == 0)
|
|
|
|
proparallel = PROPARALLEL_UNSAFE;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("parameter \"parallel\" must be SAFE, RESTRICTED, or UNSAFE")));
|
|
|
|
}
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* Most of the argument-checking is done inside of AggregateCreate
|
|
|
|
*/
|
2013-05-29 22:58:43 +02:00
|
|
|
return AggregateCreate(aggName, /* aggregate name */
|
2012-12-24 00:25:03 +01:00
|
|
|
aggNamespace, /* namespace */
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
aggKind,
|
2012-12-24 00:25:03 +01:00
|
|
|
numArgs,
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
numDirectArgs,
|
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.
2013-09-03 23:08:38 +02:00
|
|
|
parameterTypes,
|
|
|
|
PointerGetDatum(allParameterTypes),
|
|
|
|
PointerGetDatum(parameterModes),
|
|
|
|
PointerGetDatum(parameterNames),
|
|
|
|
parameterDefaults,
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
variadicArgType,
|
2012-12-24 00:25:03 +01:00
|
|
|
transfuncName, /* step function name */
|
|
|
|
finalfuncName, /* final function name */
|
2016-01-20 19:46:50 +01:00
|
|
|
combinefuncName, /* combine function name */
|
2016-03-29 21:04:05 +02:00
|
|
|
serialfuncName, /* serial function name */
|
|
|
|
deserialfuncName, /* deserial function name */
|
2014-04-12 17:58:53 +02:00
|
|
|
mtransfuncName, /* fwd trans function name */
|
|
|
|
minvtransfuncName, /* inv trans function name */
|
|
|
|
mfinalfuncName, /* final function name */
|
Allow polymorphic aggregates to have non-polymorphic state data types.
Before 9.4, such an aggregate couldn't be declared, because its final
function would have to have polymorphic result type but no polymorphic
argument, which CREATE FUNCTION would quite properly reject. The
ordered-set-aggregate patch found a workaround: allow the final function
to be declared as accepting additional dummy arguments that have types
matching the aggregate's regular input arguments. However, we failed
to notice that this problem applies just as much to regular aggregates,
despite the fact that we had a built-in regular aggregate array_agg()
that was known to be undeclarable in SQL because its final function
had an illegal signature. So what we should have done, and what this
patch does, is to decouple the extra-dummy-arguments behavior from
ordered-set aggregates and make it generally available for all aggregate
declarations. We have to put this into 9.4 rather than waiting till
later because it slightly alters the rules for declaring ordered-set
aggregates.
The patch turned out a bit bigger than I'd hoped because it proved
necessary to record the extra-arguments option in a new pg_aggregate
column. I'd thought we could just look at the final function's pronargs
at runtime, but that didn't work well for variadic final functions.
It's probably just as well though, because it simplifies life for pg_dump
to record the option explicitly.
While at it, fix array_agg() to have a valid final-function signature,
and add an opr_sanity test to notice future deviations from polymorphic
consistency. I also marked the percentile_cont() aggregates as not
needing extra arguments, since they don't.
2014-04-24 01:17:31 +02:00
|
|
|
finalfuncExtraArgs,
|
|
|
|
mfinalfuncExtraArgs,
|
2012-12-24 00:25:03 +01:00
|
|
|
sortoperatorName, /* sort operator name */
|
2013-05-29 22:58:43 +02:00
|
|
|
transTypeId, /* transition data type */
|
2013-11-16 22:03:40 +01:00
|
|
|
transSpace, /* transition space */
|
2014-04-12 17:58:53 +02:00
|
|
|
mtransTypeId, /* transition data type */
|
|
|
|
mtransSpace, /* transition space */
|
|
|
|
initval, /* initial condition */
|
2016-04-05 22:06:15 +02:00
|
|
|
minitval, /* initial condition */
|
|
|
|
proparallel); /* parallel safe? */
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|