mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #31679 -- Delayed annotating aggregations.
By avoiding to annotate aggregations meant to be possibly pushed to an outer query until their references are resolved it is possible to aggregate over a query with the same alias. Even if #34176 is a convoluted case to support, this refactor seems worth it given the reduction in complexity it brings with regards to annotation removal when performing a subquery pushdown.
This commit is contained in:
parent
d526d1569c
commit
1297c0d0d7
4 changed files with 51 additions and 65 deletions
|
@ -1258,11 +1258,11 @@ class AggregateTestCase(TestCase):
|
|||
self.assertEqual(author.sum_age, other_author.sum_age)
|
||||
|
||||
def test_aggregate_over_aggregate(self):
|
||||
msg = "Cannot compute Avg('age'): 'age' is an aggregate"
|
||||
msg = "Cannot resolve keyword 'age_agg' into field."
|
||||
with self.assertRaisesMessage(FieldError, msg):
|
||||
Author.objects.annotate(age_alias=F("age"),).aggregate(
|
||||
age=Sum(F("age")),
|
||||
avg_age=Avg(F("age")),
|
||||
Author.objects.aggregate(
|
||||
age_agg=Sum(F("age")),
|
||||
avg_age=Avg(F("age_agg")),
|
||||
)
|
||||
|
||||
def test_annotated_aggregate_over_annotated_aggregate(self):
|
||||
|
@ -2086,6 +2086,14 @@ class AggregateTestCase(TestCase):
|
|||
)
|
||||
self.assertEqual(len(qs), 6)
|
||||
|
||||
def test_aggregation_over_annotation_shared_alias(self):
|
||||
self.assertEqual(
|
||||
Publisher.objects.annotate(agg=Count("book__authors"),).aggregate(
|
||||
agg=Count("agg"),
|
||||
),
|
||||
{"agg": 5},
|
||||
)
|
||||
|
||||
|
||||
class AggregateAnnotationPruningTests(TestCase):
|
||||
@classmethod
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue