summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2022-09-28 00:44:46 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-06 11:38:03 +0200
commit3d734c09ff0138441dfe0a59010435871d17950f (patch)
tree26db20983d0b6931bfc7537a81cdc310e7a2fade /django/db/models/sql/query.py
parent04518e310d4552ff7595a34f5a7f93487d78a406 (diff)
Refs #33992 -- Refactored subquery grouping logic.
This required moving the combined queries slicing logic to the compiler in order to allow Query.exists() to be called at expression resolving time. It allowed for Query.exists() to be called at Exists() initialization time and thus ensured that get_group_by_cols() was operating on the terminal representation of the query that only has a single column selected.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py17
1 files changed, 9 insertions, 8 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index e454a6e868..9d4ca0ffb6 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -556,7 +556,7 @@ class Query(BaseExpression):
def has_filters(self):
return self.where
- def exists(self, using, limit=True):
+ def exists(self, limit=True):
q = self.clone()
if not (q.distinct and q.is_sliced):
if q.group_by is True:
@@ -568,11 +568,8 @@ class Query(BaseExpression):
q.set_group_by(allow_aliases=False)
q.clear_select_clause()
if q.combined_queries and q.combinator == "union":
- limit_combined = connections[
- using
- ].features.supports_slicing_ordering_in_compound
q.combined_queries = tuple(
- combined_query.exists(using, limit=limit_combined)
+ combined_query.exists(limit=False)
for combined_query in q.combined_queries
)
q.clear_ordering(force=True)
@@ -1150,12 +1147,16 @@ class Query(BaseExpression):
if col.alias in self.external_aliases
]
- def get_group_by_cols(self, alias=None):
+ def get_group_by_cols(self, alias=None, wrapper=None):
+ # If wrapper is referenced by an alias for an explicit GROUP BY through
+ # values() a reference to this expression and not the self must be
+ # returned to ensure external column references are not grouped against
+ # as well.
if alias:
- return [Ref(alias, self)]
+ return [Ref(alias, wrapper or self)]
external_cols = self.get_external_cols()
if any(col.possibly_multivalued for col in external_cols):
- return [self]
+ return [wrapper or self]
return external_cols
def as_sql(self, compiler, connection):