From 04518e310d4552ff7595a34f5a7f93487d78a406 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 22 Sep 2022 23:57:06 -0400 Subject: Refs #33308 -- Enabled explicit GROUP BY and ORDER BY aliases. This ensures explicit grouping from using values() before annotating an aggregate function groups by selected aliases if supported. The GROUP BY feature is disabled on Oracle because it doesn't support it. --- django/db/models/sql/query.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (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 61e39b5153..e454a6e868 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -2220,8 +2220,8 @@ class Query(BaseExpression): primary key, and the query would be equivalent, the optimization will be made automatically. """ - # Column names from JOINs to check collisions with aliases. if allow_aliases: + # Column names from JOINs to check collisions with aliases. column_names = set() seen_models = set() for join in list(self.alias_map.values())[1:]: # Skip base table. @@ -2231,7 +2231,20 @@ class Query(BaseExpression): {field.column for field in model._meta.local_concrete_fields} ) seen_models.add(model) - + if self.values_select: + # If grouping by aliases is allowed assign selected values + # aliases by moving them to annotations. + group_by_annotations = {} + values_select = {} + for alias, expr in zip(self.values_select, self.select): + if isinstance(expr, Col): + values_select[alias] = expr + else: + group_by_annotations[alias] = expr + self.annotations = {**group_by_annotations, **self.annotations} + self.append_annotation_mask(group_by_annotations) + self.select = tuple(values_select.values()) + self.values_select = tuple(values_select) group_by = list(self.select) if self.annotation_select: for alias, annotation in self.annotation_select.items(): -- cgit v1.3