summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2022-09-22 23:57:06 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-06 09:34:31 +0200
commit04518e310d4552ff7595a34f5a7f93487d78a406 (patch)
tree0cb94a0dfc2b8c63d3da0ceb90d41b048b067695 /django/db/models/sql/query.py
parentc58a8acd413ccc992dd30afd98ed900897e1f719 (diff)
downloaddjango-04518e310d4552ff7595a34f5a7f93487d78a406.tar.gz
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.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py17
1 files changed, 15 insertions, 2 deletions
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():