Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[1.2.X] Fixed #11293 -- fixed using Q objects to generate ORs with ag…
…gregates. Backport of [15173].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15174 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Jan 11, 2011
1 parent 52999af commit b28756b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 16 deletions.
52 changes: 37 additions & 15 deletions django/db/models/sql/query.py
Expand Up @@ -922,6 +922,19 @@ def remove_inherited_models(self):
self.unref_alias(alias)
self.included_inherited_models = {}

def need_force_having(self, q_object):
"""
Returns whether or not all elements of this q_object need to be put
together in the HAVING clause.
"""
for child in q_object.children:
if isinstance(child, Node):
if self.need_force_having(child):
return True
else:
if child[0].split(LOOKUP_SEP)[0] in self.aggregates:
return True
return False

def add_aggregate(self, aggregate, model, alias, is_summary):
"""
Expand Down Expand Up @@ -972,7 +985,7 @@ def add_aggregate(self, aggregate, model, alias, is_summary):
aggregate.add_to_query(self, alias, col=col, source=source, is_summary=is_summary)

def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
can_reuse=None, process_extras=True):
can_reuse=None, process_extras=True, force_having=False):
"""
Add a single filter to the query. The 'filter_expr' is a pair:
(filter_string, value). E.g. ('name__contains', 'fred')
Expand Down Expand Up @@ -1026,14 +1039,14 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
value = SQLEvaluator(value, self)
having_clause = value.contains_aggregate

for alias, aggregate in self.aggregates.items():
if alias == parts[0]:
entry = self.where_class()
entry.add((aggregate, lookup_type, value), AND)
if negate:
entry.negate()
self.having.add(entry, AND)
return
if parts[0] in self.aggregates:
aggregate = self.aggregates[parts[0]]
entry = self.where_class()
entry.add((aggregate, lookup_type, value), AND)
if negate:
entry.negate()
self.having.add(entry, connector)
return

opts = self.get_meta()
alias = self.get_initial_alias()
Expand Down Expand Up @@ -1082,7 +1095,7 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
self.promote_alias_chain(table_it, table_promote)


if having_clause:
if having_clause or force_having:
if (alias, col) not in self.group_by:
self.group_by.append((alias, col))
self.having.add((Constraint(alias, col, field), lookup_type, value),
Expand Down Expand Up @@ -1123,7 +1136,7 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
self.add_filter(filter, negate=negate, can_reuse=can_reuse,
process_extras=False)

def add_q(self, q_object, used_aliases=None):
def add_q(self, q_object, used_aliases=None, force_having=False):
"""
Adds a Q-object to the current filter.
Expand All @@ -1141,16 +1154,25 @@ def add_q(self, q_object, used_aliases=None):
else:
subtree = False
connector = AND
if q_object.connector == OR and not force_having:
force_having = self.need_force_having(q_object)
for child in q_object.children:
if connector == OR:
refcounts_before = self.alias_refcount.copy()
self.where.start_subtree(connector)
if force_having:
self.having.start_subtree(connector)
else:
self.where.start_subtree(connector)
if isinstance(child, Node):
self.add_q(child, used_aliases)
self.add_q(child, used_aliases, force_having=force_having)
else:
self.add_filter(child, connector, q_object.negated,
can_reuse=used_aliases)
self.where.end_subtree()
can_reuse=used_aliases, force_having=force_having)
if force_having:
self.having.end_subtree()
else:
self.where.end_subtree()

if connector == OR:
# Aliases that were newly added or not used at all need to
# be promoted to outer joins if they are nullable relations.
Expand Down
55 changes: 54 additions & 1 deletion tests/regressiontests/aggregation_regress/tests.py
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.core.exceptions import FieldError
from django.db import DEFAULT_DB_ALIAS
from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F
from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F, Q
from django.test import TestCase, Approximate

from models import Author, Book, Publisher, Clues, Entries, HardbackBook
Expand Down Expand Up @@ -694,6 +694,59 @@ def test_having_group_by(self):
list(qs), list(Book.objects.values_list("name", flat=True))
)

def test_annotation_disjunction(self):
qs = Book.objects.annotate(n_authors=Count("authors")).filter(
Q(n_authors=2) | Q(name="Python Web Development with Django")
)
self.assertQuerysetEqual(
qs, [
"Artificial Intelligence: A Modern Approach",
"Python Web Development with Django",
"The Definitive Guide to Django: Web Development Done Right",
],
attrgetter("name")
)

qs = Book.objects.annotate(n_authors=Count("authors")).filter(
Q(name="The Definitive Guide to Django: Web Development Done Right") | (Q(name="Artificial Intelligence: A Modern Approach") & Q(n_authors=3))
)
self.assertQuerysetEqual(
qs, [
"The Definitive Guide to Django: Web Development Done Right",
],
attrgetter("name")
)

qs = Publisher.objects.annotate(
rating_sum=Sum("book__rating"),
book_count=Count("book")
).filter(
Q(rating_sum__gt=5.5) | Q(rating_sum__isnull=True)
).order_by('pk')
self.assertQuerysetEqual(
qs, [
"Apress",
"Prentice Hall",
"Jonno's House of Books",
],
attrgetter("name")
)

qs = Publisher.objects.annotate(
rating_sum=Sum("book__rating"),
book_count=Count("book")
).filter(
Q(pk__lt=F("book_count")) | Q(rating_sum=None)
).order_by("pk")
self.assertQuerysetEqual(
qs, [
"Apress",
"Jonno's House of Books",
],
attrgetter("name")
)


if run_stddev_tests():
def test_stddev(self):
self.assertEqual(
Expand Down

0 comments on commit b28756b

Please sign in to comment.