summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-05-21 23:49:05 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-05-23 07:39:18 +0200
commit57f499e412c7c28b4a1f1b740468bf6eabbdb695 (patch)
treeb2e9034613da969d3b76972b054fbb9b410890d9 /django/db/models/sql/query.py
parentb4563cdd23ef0361588430c1f51bea819b3e0692 (diff)
[4.2.x] Refs #34551 -- Fixed QuerySet.aggregate() crash on precending aggregation reference.
Regression in 1297c0d0d76a708017fe196b61a0ab324df76954. Refs #31679. Backport of 2ee01747c32a7275a7a1a5f7862acba7db764921 from main
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index b615d06f28..8739a7b09c 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -387,7 +387,10 @@ class Query(BaseExpression):
"""
if not aggregate_exprs:
return {}
- aggregates = {}
+ # Store annotation mask prior to temporarily adding aggregations for
+ # resolving purpose to facilitate their subsequent removal.
+ replacements = {}
+ annotation_select_mask = self.annotation_select_mask
for alias, aggregate_expr in aggregate_exprs.items():
self.check_alias(alias)
aggregate = aggregate_expr.resolve_expression(
@@ -395,7 +398,16 @@ class Query(BaseExpression):
)
if not aggregate.contains_aggregate:
raise TypeError("%s is not an aggregate expression" % alias)
- aggregates[alias] = aggregate
+ # Temporarily add aggregate to annotations to allow remaining
+ # members of `aggregates` to resolve against each others.
+ self.append_annotation_mask([alias])
+ aggregate = aggregate.replace_expressions(replacements)
+ self.annotations[alias] = aggregate
+ replacements[Ref(alias, aggregate)] = aggregate
+ # Stash resolved aggregates now that they have been allowed to resolve
+ # against each other.
+ aggregates = {alias: self.annotations.pop(alias) for alias in aggregate_exprs}
+ self.set_annotation_mask(annotation_select_mask)
# Existing usage of aggregation can be determined by the presence of
# selected aggregates but also by filters against aliased aggregates.
_, having, qualify = self.where.split_having_qualify()