diff --git a/django/db/backends/oracle/compiler.py b/django/db/backends/oracle/compiler.py index cfc8a08c1bf..1044a5feb76 100644 --- a/django/db/backends/oracle/compiler.py +++ b/django/db/backends/oracle/compiler.py @@ -2,7 +2,7 @@ from django.db.models.sql import compiler class SQLCompiler(compiler.SQLCompiler): - def as_sql(self, with_limits=True, with_col_aliases=False): + def as_sql(self, with_limits=True, with_col_aliases=False, subquery=False): """ Creates the SQL for this query. Returns the SQL string and list of parameters. This is overridden from the original Query class @@ -20,12 +20,17 @@ class SQLCompiler(compiler.SQLCompiler): do_offset = with_limits and (self.query.high_mark is not None or self.query.low_mark) if not do_offset: - sql, params = super(SQLCompiler, self).as_sql(with_limits=False, - with_col_aliases=with_col_aliases) + sql, params = super(SQLCompiler, self).as_sql( + with_limits=False, + with_col_aliases=with_col_aliases, + subquery=subquery, + ) else: - sql, params = super(SQLCompiler, self).as_sql(with_limits=False, - with_col_aliases=True) - + sql, params = super(SQLCompiler, self).as_sql( + with_limits=False, + with_col_aliases=True, + subquery=subquery, + ) # Wrap the base query in an outer SELECT * with boundaries on # the "_RN" column. This is the canonical way to emulate LIMIT # and OFFSET on Oracle. diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 7944e35d8ab..5b647888d13 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -30,6 +30,7 @@ class SQLCompiler(object): self.annotation_col_map = None self.klass_info = None self.ordering_parts = re.compile(r'(.*)\s(ASC|DESC)(.*)') + self.subquery = False def setup_query(self): if all(self.query.alias_refcount[a] == 0 for a in self.query.tables): @@ -342,11 +343,11 @@ class SQLCompiler(object): sql, params = vendor_impl(self, self.connection) else: sql, params = node.as_sql(self, self.connection) - if select_format: + if select_format and not self.subquery: return node.output_field.select_format(self, sql, params) return sql, params - def as_sql(self, with_limits=True, with_col_aliases=False): + def as_sql(self, with_limits=True, with_col_aliases=False, subquery=False): """ Creates the SQL for this query. Returns the SQL string and list of parameters. @@ -359,6 +360,7 @@ class SQLCompiler(object): # However we do not want to get rid of stuff done in pre_sql_setup(), # as the pre_sql_setup will modify query state in a way that forbids # another run of it. + self.subquery = subquery refcounts_before = self.query.alias_refcount.copy() try: extra_select, order_by, group_by = self.pre_sql_setup() @@ -1115,9 +1117,9 @@ class SQLAggregateCompiler(SQLCompiler): raise EmptyResultSet sql, params = [], [] for annotation in self.query.annotation_select.values(): - agg_sql, agg_params = self.compile(annotation) - sql.append(agg_sql) - params.extend(agg_params) + ann_sql, ann_params = self.compile(annotation, select_format=True) + sql.append(ann_sql) + params.extend(ann_params) self.col_count = len(self.query.annotation_select) sql = ', '.join(sql) params = tuple(params) diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 35ce71311dd..be7438b2a73 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -209,4 +209,7 @@ class AggregateQuery(Query): compiler = 'SQLAggregateCompiler' def add_subquery(self, query, using): - self.subquery, self.sub_params = query.get_compiler(using).as_sql(with_col_aliases=True) + self.subquery, self.sub_params = query.get_compiler(using).as_sql( + with_col_aliases=True, + subquery=True, + ) diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 4ee959a6980..a283e3c1571 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -938,6 +938,9 @@ those writing third-party backends in updating their code: ``data_type_check_constraints`` attributes have moved from the ``DatabaseCreation`` class to ``DatabaseWrapper``. +* The ``SQLCompiler.as_sql()`` method now takes a ``subquery`` parameter + (:ticket:`24164`). + :mod:`django.contrib.admin` ~~~~~~~~~~~~~~~~~~~~~~~~~~~