Skip to content

Commit ab951d2

Browse files
kluchrjfelixxm
authored andcommitted
[3.1.x] Fixed #32152 -- Fixed grouping by subquery aliases.
Regression in 42c08ee. Thanks Simon Charette for the review. Backport of 4ac2d4f from master
1 parent 62f6ab2 commit ab951d2

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

django/db/models/sql/query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,8 +2148,10 @@ def set_values(self, fields):
21482148
field_names.append(f)
21492149
self.set_extra_mask(extra_names)
21502150
self.set_annotation_mask(annotation_names)
2151+
selected = frozenset(field_names + extra_names + annotation_names)
21512152
else:
21522153
field_names = [f.attname for f in self.model._meta.concrete_fields]
2154+
selected = frozenset(field_names)
21532155
# Selected annotations must be known before setting the GROUP BY
21542156
# clause.
21552157
if self.group_by is True:
@@ -2163,7 +2165,7 @@ def set_values(self, fields):
21632165
# the selected fields anymore.
21642166
group_by = []
21652167
for expr in self.group_by:
2166-
if isinstance(expr, Ref) and expr.refs not in field_names:
2168+
if isinstance(expr, Ref) and expr.refs not in selected:
21672169
expr = self.annotations[expr.refs]
21682170
group_by.append(expr)
21692171
self.group_by = tuple(group_by)

docs/releases/3.0.11.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ Django 3.0.11 release notes
44

55
*Expected November 2, 2020*
66

7-
Django 3.0.11 adds compatibility with Python 3.9.
7+
Django 3.0.11 fixes a regression in 3.0.7 and adds compatibility with Python
8+
3.9.
9+
10+
Bugfixes
11+
========
12+
13+
* Fixed a regression in Django 3.0.7 that didn't use ``Subquery()`` aliases in
14+
the ``GROUP BY`` clause (:ticket:`32152`).

docs/releases/3.1.3.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ Bugfixes
5757
* Fixed a regression in Django 3.1 that caused incorrect textarea layout on
5858
medium-sized screens in the admin change form view with the sidebar open
5959
(:ticket:`32127`).
60+
61+
* Fixed a regression in Django 3.0.7 that didn't use ``Subquery()`` aliases in
62+
the ``GROUP BY`` clause (:ticket:`32152`).

tests/annotations/tests.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
NullBooleanField, OuterRef, Q, Subquery, Sum, Value, When,
1111
)
1212
from django.db.models.expressions import RawSQL
13-
from django.db.models.functions import Length, Lower
13+
from django.db.models.functions import ExtractYear, Length, Lower
1414
from django.test import TestCase, skipUnlessDBFeature
1515

1616
from .models import (
@@ -664,6 +664,25 @@ def test_annotation_exists_aggregate_values_chaining(self):
664664
datetime.date(2008, 11, 3),
665665
])
666666

667+
@skipUnlessDBFeature('supports_subqueries_in_group_by')
668+
def test_annotation_subquery_and_aggregate_values_chaining(self):
669+
qs = Book.objects.annotate(
670+
pub_year=ExtractYear('pubdate')
671+
).values('pub_year').annotate(
672+
top_rating=Subquery(
673+
Book.objects.filter(
674+
pubdate__year=OuterRef('pub_year')
675+
).order_by('-rating').values('rating')[:1]
676+
),
677+
total_pages=Sum('pages'),
678+
).values('pub_year', 'total_pages', 'top_rating')
679+
self.assertCountEqual(qs, [
680+
{'pub_year': 1991, 'top_rating': 5.0, 'total_pages': 946},
681+
{'pub_year': 1995, 'top_rating': 4.0, 'total_pages': 1132},
682+
{'pub_year': 2007, 'top_rating': 4.5, 'total_pages': 447},
683+
{'pub_year': 2008, 'top_rating': 4.0, 'total_pages': 1178},
684+
])
685+
667686
@skipIf(
668687
connection.vendor == 'mysql' and 'ONLY_FULL_GROUP_BY' in connection.sql_mode,
669688
'GROUP BY optimization does not work properly when ONLY_FULL_GROUP_BY '

0 commit comments

Comments
 (0)