Fixed #24164 -- Fixed Oracle GIS limited aggregation test failure.

This commit is contained in:
Tim Graham 2015-01-29 07:20:56 -05:00
parent 5b74134f27
commit 29c0073335
4 changed files with 25 additions and 12 deletions

View File

@ -2,7 +2,7 @@ from django.db.models.sql import compiler
class SQLCompiler(compiler.SQLCompiler): 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 Creates the SQL for this query. Returns the SQL string and list
of parameters. This is overridden from the original Query class 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 do_offset = with_limits and (self.query.high_mark is not None
or self.query.low_mark) or self.query.low_mark)
if not do_offset: if not do_offset:
sql, params = super(SQLCompiler, self).as_sql(with_limits=False, sql, params = super(SQLCompiler, self).as_sql(
with_col_aliases=with_col_aliases) with_limits=False,
with_col_aliases=with_col_aliases,
subquery=subquery,
)
else: else:
sql, params = super(SQLCompiler, self).as_sql(with_limits=False, sql, params = super(SQLCompiler, self).as_sql(
with_col_aliases=True) with_limits=False,
with_col_aliases=True,
subquery=subquery,
)
# Wrap the base query in an outer SELECT * with boundaries on # Wrap the base query in an outer SELECT * with boundaries on
# the "_RN" column. This is the canonical way to emulate LIMIT # the "_RN" column. This is the canonical way to emulate LIMIT
# and OFFSET on Oracle. # and OFFSET on Oracle.

View File

@ -30,6 +30,7 @@ class SQLCompiler(object):
self.annotation_col_map = None self.annotation_col_map = None
self.klass_info = None self.klass_info = None
self.ordering_parts = re.compile(r'(.*)\s(ASC|DESC)(.*)') self.ordering_parts = re.compile(r'(.*)\s(ASC|DESC)(.*)')
self.subquery = False
def setup_query(self): def setup_query(self):
if all(self.query.alias_refcount[a] == 0 for a in self.query.tables): 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) sql, params = vendor_impl(self, self.connection)
else: else:
sql, params = node.as_sql(self, self.connection) 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 node.output_field.select_format(self, sql, params)
return 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 Creates the SQL for this query. Returns the SQL string and list of
parameters. parameters.
@ -359,6 +360,7 @@ class SQLCompiler(object):
# However we do not want to get rid of stuff done in pre_sql_setup(), # 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 # as the pre_sql_setup will modify query state in a way that forbids
# another run of it. # another run of it.
self.subquery = subquery
refcounts_before = self.query.alias_refcount.copy() refcounts_before = self.query.alias_refcount.copy()
try: try:
extra_select, order_by, group_by = self.pre_sql_setup() extra_select, order_by, group_by = self.pre_sql_setup()
@ -1115,9 +1117,9 @@ class SQLAggregateCompiler(SQLCompiler):
raise EmptyResultSet raise EmptyResultSet
sql, params = [], [] sql, params = [], []
for annotation in self.query.annotation_select.values(): for annotation in self.query.annotation_select.values():
agg_sql, agg_params = self.compile(annotation) ann_sql, ann_params = self.compile(annotation, select_format=True)
sql.append(agg_sql) sql.append(ann_sql)
params.extend(agg_params) params.extend(ann_params)
self.col_count = len(self.query.annotation_select) self.col_count = len(self.query.annotation_select)
sql = ', '.join(sql) sql = ', '.join(sql)
params = tuple(params) params = tuple(params)

View File

@ -209,4 +209,7 @@ class AggregateQuery(Query):
compiler = 'SQLAggregateCompiler' compiler = 'SQLAggregateCompiler'
def add_subquery(self, query, using): 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,
)

View File

@ -938,6 +938,9 @@ those writing third-party backends in updating their code:
``data_type_check_constraints`` attributes have moved from the ``data_type_check_constraints`` attributes have moved from the
``DatabaseCreation`` class to ``DatabaseWrapper``. ``DatabaseCreation`` class to ``DatabaseWrapper``.
* The ``SQLCompiler.as_sql()`` method now takes a ``subquery`` parameter
(:ticket:`24164`).
:mod:`django.contrib.admin` :mod:`django.contrib.admin`
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~