Skip to content

Commit

Permalink
[1.1.X] Fixed #11916 -- Corrected handling of aggregation when there …
Browse files Browse the repository at this point in the history
…is a subquery provided in an extra(select=) clause. Thanks to jaklaassen@gmail.com for the report, and to tobias, paluh, Karen Tracey and Ian Kelly for their work on the fix.

Backport of r12896 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12897 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Mar 31, 2010
1 parent f0fa080 commit 7a7b9a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Expand Up @@ -875,7 +875,7 @@ def get_grouping(self):
elif hasattr(col, 'as_sql'):
result.append(col.as_sql(qn))
else:
result.append(str(col))
result.append('(%s)' % str(col))
return result, params

def get_ordering(self):
Expand Down
29 changes: 26 additions & 3 deletions tests/regressiontests/aggregation_regress/tests.py
@@ -1,5 +1,6 @@
from django.conf import settings
from django.test import TestCase
from django.db.models import Max
from django.db.models import Count, Max

from regressiontests.aggregation_regress.models import *

Expand All @@ -13,7 +14,7 @@ def test_aggregates_in_where_clause(self):
Tests that the subselect works and returns results equivalent to a
query with the IDs listed.
Before the corresponding fix for this bug, this test passed in 1.1 and
failed in 1.2-beta (trunk).
"""
Expand All @@ -33,7 +34,7 @@ def test_aggregates_in_where_clause_pre_eval(self):
Same as the above test, but evaluates the queryset for the subquery
before it's used as a subquery.
Before the corresponding fix for this bug, this test failed in both
1.1 and 1.2-beta (trunk).
"""
Expand All @@ -46,3 +47,25 @@ def test_aggregates_in_where_clause_pre_eval(self):
qs1 = books.filter(id__in=qs)
qs2 = books.filter(id__in=list(qs))
self.assertEqual(list(qs1), list(qs2))

if settings.DATABASE_ENGINE != 'oracle':
def test_annotate_with_extra(self):
"""
Regression test for #11916: Extra params + aggregation creates
incorrect SQL.
"""
#oracle doesn't support subqueries in group by clause
shortest_book_sql = """
SELECT name
FROM aggregation_regress_book b
WHERE b.publisher_id = aggregation_regress_publisher.id
ORDER BY b.pages
LIMIT 1
"""
# tests that this query does not raise a DatabaseError due to the full
# subselect being (erroneously) added to the GROUP BY parameters
qs = Publisher.objects.extra(select={
'name_of_shortest_book': shortest_book_sql,
}).annotate(total_books=Count('book'))
# force execution of the query
list(qs)

0 comments on commit 7a7b9a0

Please sign in to comment.