|
@@ -402,6 +402,7 @@ class Query(BaseExpression):
|
|
|
return {}
|
|
|
|
|
|
|
|
|
+ refs_subquery = False
|
|
|
replacements = {}
|
|
|
annotation_select_mask = self.annotation_select_mask
|
|
|
for alias, aggregate_expr in aggregate_exprs.items():
|
|
@@ -414,6 +415,10 @@ class Query(BaseExpression):
|
|
|
|
|
|
|
|
|
self.append_annotation_mask([alias])
|
|
|
+ refs_subquery |= any(
|
|
|
+ getattr(self.annotations[ref], "subquery", False)
|
|
|
+ for ref in aggregate.get_refs()
|
|
|
+ )
|
|
|
aggregate = aggregate.replace_expressions(replacements)
|
|
|
self.annotations[alias] = aggregate
|
|
|
replacements[Ref(alias, aggregate)] = aggregate
|
|
@@ -445,6 +450,7 @@ class Query(BaseExpression):
|
|
|
isinstance(self.group_by, tuple)
|
|
|
or self.is_sliced
|
|
|
or has_existing_aggregation
|
|
|
+ or refs_subquery
|
|
|
or qualify
|
|
|
or self.distinct
|
|
|
or self.combinator
|