From e76cc93b0168fa3abbafb9af1ab4535814b751f0 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 23 Nov 2023 00:09:08 -0500 Subject: Fixed #34987 -- Fixed queryset crash when mixing aggregate and window annotations. Regression in f387d024fc75569d2a4a338bfda76cc2f328f627. Just like `OrderByList` the `ExpressionList` expression used to wrap `Window.partition_by` must implement `get_group_by_cols` to ensure the necessary grouping when mixing window expressions with aggregate annotations is performed against the partition members and not the partition expression itself. This is necessary because while `partition_by` is implemented as a source expression of `Window` it's actually a fragment of the WINDOW expression at the SQL level and thus it should result in a group by its members and not the sum of them. Thanks ElRoberto538 for the report. --- django/db/models/expressions.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'django') diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 74ae9cab8e..36c0bbd50a 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -1265,6 +1265,12 @@ class ExpressionList(Func): # Casting to numeric is unnecessary. return self.as_sql(compiler, connection, **extra_context) + def get_group_by_cols(self): + group_by_cols = [] + for partition in self.get_source_expressions(): + group_by_cols.extend(partition.get_group_by_cols()) + return group_by_cols + class OrderByList(Func): allowed_default = False -- cgit v1.3