#26067 closed New feature (fixed)
Orderable ArrayAgg and StringAgg
Reported by: | Floris den Hengst | Owned by: | Floris den Hengst |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | ArrayAgg StringAgg ordering |
Cc: | Matthew Pava | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
The Postgres-specific ArrayAgg and StringAgg aggregations were added in Django 1.9.
The documentation of Postgres 9.0 first mentions the possibility of ordering the results within aggregations such as ARRAY_AGG and STRING_AGG.
This could be useful in some cases.
For example: it could make sense to perform a StringAgg in lexicographical order in some cases.
The basic format of ordering within aggregations in SQL is quite simple:
SELECT ARRAY_AGG(some_field ORDER BY some_field ASC) FROM table; SELECT ARRAY_AGG(some_field ORDER BY some_field DESC) FROM table; SELECT ARRAY_AGG(some_field ORDER BY other_field ASC) FROM table;
It would be nice if the above would be supported as follows:
Model.objects.aggregate(ArrayAgg(some_field, order_by='some_field')) Model.objects.aggregate(ArrayAgg(some_field, order_by='-some_field')) Model.objects.aggregate(ArrayAgg(some_field, order_by='other_field'))
where order_by is an optional parameter. If it not specified, behavior can remain unchanged from the current implementation.
As noted by Josh Smeaton in the mailinglist discussion for this feature, any ordering added within may need to be contributed to GROUP BY. This might require some investigation.
Change History (14)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | → master |
comment:3 by , 8 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:4 by , 8 years ago
follow-up: 7 comment:6 by , 8 years ago
Patch needs improvement: | set |
---|
Comments for improvement are on the PR.
comment:7 by , 8 years ago
Replying to Tim Graham:
Comments for improvement are on the PR.
The original PR was closed due to inactivity.
A new PR (in which the proposed improvements have been included) can be found here: https://github.com/django/django/pull/7604
comment:8 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:9 by , 8 years ago
Patch needs improvement: | set |
---|
Patch needs rebasing and targeting for Django 2.0.
comment:10 by , 8 years ago
Patch needs improvement: | unset |
---|
Unset patch new improvement after rebasing and targeting for Django 2.0.
comment:11 by , 7 years ago
Patch needs improvement: | set |
---|
comment:12 by , 7 years ago
Cc: | added |
---|
As pointed out by Josh Smeaton in the mailing list discussion:
In the Postgres' documentation these aggregations are only mentioned as being special by meaningful difference in ordering input values:
I don't think anything in the ORDER BY clause in the aggregation would have to be contributed to a GROUP BY clause in the surrounding query as the aggregation operation is independent of the grouping of the surrounding query.