From e03083917db03757e48f8edac4c8491b72c8a3c4 Mon Sep 17 00:00:00 2001 From: Devin Cox Date: Fri, 9 Aug 2024 13:56:56 -0700 Subject: 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 --- django/db/models/sql/query.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'django/db/models/sql/query.py') 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 -- cgit v1.3