From d7a9f006ed6d6323d3d66e75354202f4d1174ea0 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 17 Nov 2023 19:55:27 -0500 Subject: [PATCH] Refs #34717 -- Avoided computing aggregate refs twice. --- django/db/models/sql/query.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index f50e012f82..2bc16b1df3 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -451,13 +451,14 @@ class Query(BaseExpression): # Temporarily add aggregate to annotations to allow remaining # members of `aggregates` to resolve against each others. self.append_annotation_mask([alias]) + aggregate_refs = aggregate.get_refs() refs_subquery |= any( getattr(self.annotations[ref], "contains_subquery", False) - for ref in aggregate.get_refs() + for ref in aggregate_refs ) refs_window |= any( getattr(self.annotations[ref], "contains_over_clause", True) - for ref in aggregate.get_refs() + for ref in aggregate_refs ) aggregate = aggregate.replace_expressions(replacements) self.annotations[alias] = aggregate