Skip to content

Commit

Permalink
Fixed #10290: do not use aliases when adding extra_selects to the GRO…
Browse files Browse the repository at this point in the history
…UP BY clause, to generate compliant sql that will be accepted by Oracle.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9905 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
nightflyerkilo committed Feb 25, 2009
1 parent 86a048b commit 8ffe898
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 18 additions & 10 deletions django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,21 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
result.append('AND')
result.append(' AND '.join(self.extra_where))

grouping = self.get_grouping()
grouping, gb_params = self.get_grouping()
if grouping:
if ordering:
# If the backend can't group by PK (i.e., any database
# other than MySQL), then any fields mentioned in the
# ordering clause needs to be in the group by clause.
if not self.connection.features.allows_group_by_pk:
grouping.extend([str(col) for col in ordering_group_by
if col not in grouping])
for col, col_params in ordering_group_by:
if col not in grouping:
grouping.append(str(col))
gb_params.extend(col_params)
else:
ordering = self.connection.ops.force_no_ordering()
result.append('GROUP BY %s' % ', '.join(grouping))
params.extend(gb_params)

if having:
result.append('HAVING %s' % having)
Expand Down Expand Up @@ -710,17 +713,22 @@ def get_grouping(self):
Returns a tuple representing the SQL elements in the "group by" clause.
"""
qn = self.quote_name_unless_alias
result = []
result, params = [], []
if self.group_by is not None:
group_by = self.group_by or []
for col in group_by + self.related_select_cols + self.extra_select.keys():

extra_selects = []
for extra_select, extra_params in self.extra_select.itervalues():
extra_selects.append(extra_select)
params.extend(extra_params)
for col in group_by + self.related_select_cols + extra_selects:
if isinstance(col, (list, tuple)):
result.append('%s.%s' % (qn(col[0]), qn(col[1])))
elif hasattr(col, 'as_sql'):
result.append(col.as_sql(qn))
else:
result.append(str(col))
return result
return result, params

def get_ordering(self):
"""
Expand Down Expand Up @@ -768,7 +776,7 @@ def get_ordering(self):
else:
order = asc
result.append('%s %s' % (field, order))
group_by.append(field)
group_by.append((field, []))
continue
col, order = get_order_dir(field, asc)
if col in self.aggregate_select:
Expand All @@ -783,7 +791,7 @@ def get_ordering(self):
processed_pairs.add((table, col))
if not distinct or elt in select_aliases:
result.append('%s %s' % (elt, order))
group_by.append(elt)
group_by.append((elt, []))
elif get_order_dir(field)[0] not in self.extra_select:
# 'col' is of the form 'field' or 'field1__field2' or
# '-field1__field2__field', etc.
Expand All @@ -795,13 +803,13 @@ def get_ordering(self):
if distinct and elt not in select_aliases:
ordering_aliases.append(elt)
result.append('%s %s' % (elt, order))
group_by.append(elt)
group_by.append((elt, []))
else:
elt = qn2(col)
if distinct and col not in select_aliases:
ordering_aliases.append(elt)
result.append('%s %s' % (elt, order))
group_by.append(elt)
group_by.append(self.extra_select[col])
self.ordering_aliases = ordering_aliases
return result, group_by

Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/aggregation_regress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ def __unicode__(self):
>>> Book.objects.annotate(num_authors=Count('authors')).filter(num_authors=2).dates('pubdate', 'day')
[datetime.datetime(1995, 1, 15, 0, 0), datetime.datetime(2007, 12, 6, 0, 0)]
# Regression for #10290 - extra selects with parameters can be used for
# grouping.
>>> qs = Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'sheets' : '(pages + %s) / %s'}, select_params=[1, 2]).order_by('sheets').values('sheets')
>>> [int(x['sheets']) for x in qs]
[150, 175, 224, 264, 473, 566]
"""
}

Expand Down

0 comments on commit 8ffe898

Please sign in to comment.