summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-12-15 21:00:59 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-12-16 20:19:24 +0100
commit77278929c86168f075600d9d8c8e76a4792e672b (patch)
tree62c5808bac7712ec85d4d99958dd3788e9e54c56
parenteea4f92f9aa57d1b25f1c28d11c3b5a6a5841e82 (diff)
Fixed #35042 -- Fixed a count() crash on combined queries.
Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7. Thanks Marcin for the report.
-rw-r--r--django/db/models/sql/query.py6
-rw-r--r--tests/aggregation/tests.py15
2 files changed, 18 insertions, 3 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 2bc16b1df3..7166b8cb5d 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -520,11 +520,11 @@ class Query(BaseExpression):
self.model._meta.pk.get_col(inner_query.get_initial_alias()),
)
inner_query.default_cols = False
- if not qualify:
+ if not qualify and not self.combinator:
# Mask existing annotations that are not referenced by
# aggregates to be pushed to the outer query unless
- # filtering against window functions is involved as it
- # requires complex realising.
+ # filtering against window functions or if the query is
+ # combined as both would require complex realiasing logic.
annotation_mask = set()
if isinstance(self.group_by, tuple):
for expr in self.group_by:
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 62e9c6a27a..4408535228 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -2344,3 +2344,18 @@ class AggregateAnnotationPruningTests(TestCase):
max_book_author=Max("book__authors"),
).aggregate(count=Count("id", filter=Q(id__in=[F("max_book_author"), 0])))
self.assertEqual(aggregates, {"count": 1})
+
+ def test_aggregate_combined_queries(self):
+ # Combined queries could have members in their values select mask while
+ # others have them in their annotation mask which makes annotation
+ # pruning complex to implement hence why it's not implemented.
+ qs = Author.objects.values(
+ "age",
+ other=Value(0),
+ ).union(
+ Book.objects.values(
+ age=Value(0),
+ other=Value(0),
+ )
+ )
+ self.assertEqual(qs.count(), 3)