mirror of
https://github.com/django/django.git
synced 2025-04-12 03:22:21 +00:00
Refs #28477 -- Fixed handling aliased annotations on aggregation.
Just like when using .annotate(), the .alias() method will generate the necessary JOINs to resolve the alias even if not selected. Since these JOINs could be multi-valued non-selected aggregates must be considered to require subquery wrapping as a GROUP BY is required to combine duplicated tuples from the base table. Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7.
This commit is contained in:
parent
b088cc2fea
commit
10037130c1
@ -443,7 +443,7 @@ class Query(BaseExpression):
|
||||
return {}
|
||||
existing_annotations = {
|
||||
alias: annotation
|
||||
for alias, annotation in self.annotation_select.items()
|
||||
for alias, annotation in self.annotations.items()
|
||||
if alias not in added_aggregate_names
|
||||
}
|
||||
# Existing usage of aggregation can be determined by the presence of
|
||||
|
@ -2088,13 +2088,41 @@ class AggregateTestCase(TestCase):
|
||||
|
||||
|
||||
class AggregateAnnotationPruningTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.a1 = Author.objects.create(age=1)
|
||||
cls.a2 = Author.objects.create(age=2)
|
||||
cls.p1 = Publisher.objects.create(num_awards=1)
|
||||
cls.p2 = Publisher.objects.create(num_awards=0)
|
||||
cls.b1 = Book.objects.create(
|
||||
name="b1",
|
||||
publisher=cls.p1,
|
||||
pages=100,
|
||||
rating=4.5,
|
||||
price=10,
|
||||
contact=cls.a1,
|
||||
pubdate=datetime.date.today(),
|
||||
)
|
||||
cls.b1.authors.add(cls.a1)
|
||||
cls.b2 = Book.objects.create(
|
||||
name="b2",
|
||||
publisher=cls.p2,
|
||||
pages=1000,
|
||||
rating=3.2,
|
||||
price=50,
|
||||
contact=cls.a2,
|
||||
pubdate=datetime.date.today(),
|
||||
)
|
||||
cls.b2.authors.add(cls.a1, cls.a2)
|
||||
|
||||
def test_unused_aliased_aggregate_pruned(self):
|
||||
with CaptureQueriesContext(connection) as ctx:
|
||||
Book.objects.alias(
|
||||
cnt = Book.objects.alias(
|
||||
authors_count=Count("authors"),
|
||||
).count()
|
||||
self.assertEqual(cnt, 2)
|
||||
sql = ctx.captured_queries[0]["sql"].lower()
|
||||
self.assertEqual(sql.count("select"), 1, "No subquery wrapping required")
|
||||
self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
|
||||
self.assertNotIn("authors_count", sql)
|
||||
|
||||
def test_non_aggregate_annotation_pruned(self):
|
||||
@ -2108,9 +2136,10 @@ class AggregateAnnotationPruningTests(TestCase):
|
||||
|
||||
def test_unreferenced_aggregate_annotation_pruned(self):
|
||||
with CaptureQueriesContext(connection) as ctx:
|
||||
Book.objects.annotate(
|
||||
cnt = Book.objects.annotate(
|
||||
authors_count=Count("authors"),
|
||||
).count()
|
||||
self.assertEqual(cnt, 2)
|
||||
sql = ctx.captured_queries[0]["sql"].lower()
|
||||
self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
|
||||
self.assertNotIn("authors_count", sql)
|
||||
|
Loading…
x
Reference in New Issue
Block a user