summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorDevin Cox <dcox@surefyre.co>2024-08-09 13:56:56 -0700
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-08-12 15:35:19 +0200
commite03083917db03757e48f8edac4c8491b72c8a3c4 (patch)
tree9b96fb24ab403cc56d5510e74d23a70eeab207fa /django/db/models/sql/query.py
parent228128618bd895ecad235d2215f4ad4e3232595d (diff)
Fixed #35586 -- Added support for set-returning database functions.
Aggregation optimization didn't account for not referenced set-returning annotations on Postgres. Co-authored-by: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index c1e2fc1d4f..aef3f48f10 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -491,6 +491,11 @@ class Query(BaseExpression):
)
or having
)
+ set_returning_annotations = {
+ alias
+ for alias, annotation in self.annotation_select.items()
+ if getattr(annotation, "set_returning", False)
+ }
# Decide if we need to use a subquery.
#
# Existing aggregations would cause incorrect results as
@@ -510,6 +515,7 @@ class Query(BaseExpression):
or qualify
or self.distinct
or self.combinator
+ or set_returning_annotations
):
from django.db.models.sql.subqueries import AggregateQuery
@@ -551,6 +557,9 @@ class Query(BaseExpression):
if annotation.get_group_by_cols():
annotation_mask.add(annotation_alias)
inner_query.set_annotation_mask(annotation_mask)
+ # Annotations that possibly return multiple rows cannot
+ # be masked as they might have an incidence on the query.
+ annotation_mask |= set_returning_annotations
# Add aggregates to the outer AggregateQuery. This requires making
# sure all columns referenced by the aggregates are selected in the