From 4ccca9eedc2f453602a20f562399a835a24817c1 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sun, 15 Oct 2023 21:59:15 -0400 Subject: [PATCH] [5.0.x] Fixed #34798 -- Fixed QuerySet.aggregate() crash when referencing expressions containing subqueries. Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7, complements e5c844d6f2a4ac6ae674d741b5f1fa2a688cedf4. Refs #28477, #34551. Thanks Haldun Komsuoglu for the report. Backport of 3b4a571275d967512866012955eb0b3ae486d63c from main --- django/db/models/expressions.py | 7 +++++++ django/db/models/sql/query.py | 2 +- docs/releases/4.2.7.txt | 4 +++- tests/aggregation/tests.py | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 181a04493f..9aa78a281f 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -256,6 +256,13 @@ class BaseExpression: for expr in self.get_source_expressions() ) + @cached_property + def contains_subquery(self): + return any( + expr and (getattr(expr, "subquery", False) or expr.contains_subquery) + for expr in self.get_source_expressions() + ) + def resolve_expression( self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False ): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index a7839ccb4d..4690fd304d 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -452,7 +452,7 @@ class Query(BaseExpression): # members of `aggregates` to resolve against each others. self.append_annotation_mask([alias]) refs_subquery |= any( - getattr(self.annotations[ref], "subquery", False) + getattr(self.annotations[ref], "contains_subquery", False) for ref in aggregate.get_refs() ) refs_window |= any( diff --git a/docs/releases/4.2.7.txt b/docs/releases/4.2.7.txt index a883a7dff1..c6d513128c 100644 --- a/docs/releases/4.2.7.txt +++ b/docs/releases/4.2.7.txt @@ -9,4 +9,6 @@ Django 4.2.7 fixes several bugs in 4.2.6. Bugfixes ======== -... +* Fixed a regression in Django 4.2 that caused a crash of + ``QuerySet.aggregate()`` with aggregates referencing expressions containing + subqueries (:ticket:`34798`). diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index ad00afdcc1..8d8e46e312 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -2260,6 +2260,27 @@ class AggregateAnnotationPruningTests(TestCase): self.assertEqual(sql.count("select"), 3, "Subquery wrapping required") self.assertEqual(aggregate, {"sum_total_books": 3}) + def test_referenced_composed_subquery_requires_wrapping(self): + total_books_qs = ( + Author.book_set.through.objects.values("author") + .filter(author=OuterRef("pk")) + .annotate(total=Count("book")) + ) + with self.assertNumQueries(1) as ctx: + aggregate = ( + Author.objects.annotate( + total_books=Subquery(total_books_qs.values("total")), + total_books_ref=F("total_books") / 1, + ) + .values("pk", "total_books_ref") + .aggregate( + sum_total_books=Sum("total_books_ref"), + ) + ) + sql = ctx.captured_queries[0]["sql"].lower() + self.assertEqual(sql.count("select"), 3, "Subquery wrapping required") + self.assertEqual(aggregate, {"sum_total_books": 3}) + @skipUnlessDBFeature("supports_over_clause") def test_referenced_window_requires_wrapping(self): total_books_qs = Book.objects.annotate(