Refs #20939 -- Moved subquery ordering clearing optimization to the __in lookup.

Queries could potentially be resolved in cases where ordering matter.
This commit is contained in:
Simon Charette 2017-04-26 23:20:04 -04:00
parent 8095496a1c
commit e62ea0bb9c
2 changed files with 12 additions and 9 deletions

View File

@ -367,9 +367,18 @@ class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
placeholder = '(' + ', '.join(sqls) + ')' placeholder = '(' + ', '.join(sqls) + ')'
return (placeholder, sqls_params) return (placeholder, sqls_params)
else: else:
if not getattr(self.rhs, 'has_select_fields', True): from django.db.models.sql.query import Query # avoid circular import
self.rhs.clear_select_clause() if isinstance(self.rhs, Query):
self.rhs.add_fields(['pk']) query = self.rhs
# It's safe to drop ordering if the queryset isn't using
# slicing, distinct(*fields), or select_for_update().
if (query.low_mark == 0 and query.high_mark is None and
not query.distinct_fields and
not query.select_for_update):
query.clear_ordering(True)
if not query.has_select_fields:
query.clear_select_clause()
query.add_fields(['pk'])
return super().process_rhs(compiler, connection) return super().process_rhs(compiler, connection)
def get_rhs_op(self, connection, rhs): def get_rhs_op(self, connection, rhs):

View File

@ -981,12 +981,6 @@ class Query:
# Subqueries need to use a different set of aliases than the outer query. # Subqueries need to use a different set of aliases than the outer query.
clone.bump_prefix(query) clone.bump_prefix(query)
clone.subquery = True clone.subquery = True
# It's safe to drop ordering if the queryset isn't using slicing,
# distinct(*fields) or select_for_update().
if (self.low_mark == 0 and self.high_mark is None and
not self.distinct_fields and
not self.select_for_update):
clone.clear_ordering(True)
return clone return clone
def prepare_lookup_value(self, value, lookups, can_reuse, allow_joins=True): def prepare_lookup_value(self, value, lookups, can_reuse, allow_joins=True):