mirror of
https://github.com/postgres/postgres.git
synced 2025-07-20 05:03:10 +03:00
Support ORDER BY within aggregate function calls, at long last providing a
non-kluge method for controlling the order in which values are fed to an aggregate function. At the same time eliminate the old implementation restriction that DISTINCT was only supported for single-argument aggregates. Possibly release-notable behavioral change: formerly, agg(DISTINCT x) dropped null values of x unconditionally. Now, it does so only if the agg transition function is strict; otherwise nulls are treated as DISTINCT normally would, ie, you get one copy. Andrew Gierth, reviewed by Hitoshi Harada
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.46 2009/06/11 14:48:59 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.47 2009/12/15 17:57:46 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -239,12 +239,12 @@ find_minmax_aggs_walker(Node *node, List **context)
|
||||
{
|
||||
Aggref *aggref = (Aggref *) node;
|
||||
Oid aggsortop;
|
||||
Expr *curTarget;
|
||||
TargetEntry *curTarget;
|
||||
MinMaxAggInfo *info;
|
||||
ListCell *l;
|
||||
|
||||
Assert(aggref->agglevelsup == 0);
|
||||
if (list_length(aggref->args) != 1)
|
||||
if (list_length(aggref->args) != 1 || aggref->aggorder != NIL)
|
||||
return true; /* it couldn't be MIN/MAX */
|
||||
/* note: we do not care if DISTINCT is mentioned ... */
|
||||
|
||||
@ -255,19 +255,19 @@ find_minmax_aggs_walker(Node *node, List **context)
|
||||
/*
|
||||
* Check whether it's already in the list, and add it if not.
|
||||
*/
|
||||
curTarget = linitial(aggref->args);
|
||||
curTarget = (TargetEntry *) linitial(aggref->args);
|
||||
foreach(l, *context)
|
||||
{
|
||||
info = (MinMaxAggInfo *) lfirst(l);
|
||||
if (info->aggfnoid == aggref->aggfnoid &&
|
||||
equal(info->target, curTarget))
|
||||
equal(info->target, curTarget->expr))
|
||||
return false;
|
||||
}
|
||||
|
||||
info = (MinMaxAggInfo *) palloc0(sizeof(MinMaxAggInfo));
|
||||
info->aggfnoid = aggref->aggfnoid;
|
||||
info->aggsortop = aggsortop;
|
||||
info->target = curTarget;
|
||||
info->target = curTarget->expr;
|
||||
|
||||
*context = lappend(*context, info);
|
||||
|
||||
@ -584,15 +584,15 @@ replace_aggs_with_params_mutator(Node *node, List **context)
|
||||
if (IsA(node, Aggref))
|
||||
{
|
||||
Aggref *aggref = (Aggref *) node;
|
||||
TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
|
||||
ListCell *l;
|
||||
Expr *curTarget = linitial(aggref->args);
|
||||
|
||||
foreach(l, *context)
|
||||
{
|
||||
MinMaxAggInfo *info = (MinMaxAggInfo *) lfirst(l);
|
||||
|
||||
if (info->aggfnoid == aggref->aggfnoid &&
|
||||
equal(info->target, curTarget))
|
||||
equal(info->target, curTarget->expr))
|
||||
return (Node *) info->param;
|
||||
}
|
||||
elog(ERROR, "failed to re-find aggregate info record");
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.261 2009/10/28 14:55:38 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.262 2009/12/15 17:57:46 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -1096,11 +1096,12 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
bool can_sort;
|
||||
|
||||
/*
|
||||
* Executor doesn't support hashed aggregation with DISTINCT
|
||||
* aggregates. (Doing so would imply storing *all* the input
|
||||
* values in the hash table, which seems like a certain loser.)
|
||||
* Executor doesn't support hashed aggregation with DISTINCT or
|
||||
* ORDER BY aggregates. (Doing so would imply storing *all* the
|
||||
* input values in the hash table, and/or running many sorts in
|
||||
* parallel, either of which seems like a certain loser.)
|
||||
*/
|
||||
can_hash = (agg_counts.numDistinctAggs == 0 &&
|
||||
can_hash = (agg_counts.numOrderedAggs == 0 &&
|
||||
grouping_is_hashable(parse->groupClause));
|
||||
can_sort = grouping_is_sortable(parse->groupClause);
|
||||
if (can_hash && can_sort)
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.280 2009/12/14 02:15:52 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.281 2009/12/15 17:57:47 tgl Exp $
|
||||
*
|
||||
* HISTORY
|
||||
* AUTHOR DATE MAJOR EVENT
|
||||
@ -471,21 +471,22 @@ count_agg_clauses_walker(Node *node, AggClauseCounts *counts)
|
||||
HeapTuple aggTuple;
|
||||
Form_pg_aggregate aggform;
|
||||
Oid aggtranstype;
|
||||
int i;
|
||||
ListCell *l;
|
||||
|
||||
Assert(aggref->agglevelsup == 0);
|
||||
counts->numAggs++;
|
||||
if (aggref->aggdistinct)
|
||||
counts->numDistinctAggs++;
|
||||
if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
|
||||
counts->numOrderedAggs++;
|
||||
|
||||
/* extract argument types */
|
||||
numArguments = list_length(aggref->args);
|
||||
inputTypes = (Oid *) palloc(sizeof(Oid) * numArguments);
|
||||
i = 0;
|
||||
/* extract argument types (ignoring any ORDER BY expressions) */
|
||||
inputTypes = (Oid *) palloc(sizeof(Oid) * list_length(aggref->args));
|
||||
numArguments = 0;
|
||||
foreach(l, aggref->args)
|
||||
{
|
||||
inputTypes[i++] = exprType((Node *) lfirst(l));
|
||||
TargetEntry *tle = (TargetEntry *) lfirst(l);
|
||||
|
||||
if (!tle->resjunk)
|
||||
inputTypes[numArguments++] = exprType((Node *) tle->expr);
|
||||
}
|
||||
|
||||
/* fetch aggregate transition datatype from pg_aggregate */
|
||||
|
Reference in New Issue
Block a user