Commit d3f00bd5 authored by Anssi Kääriäinen's avatar Anssi Kääriäinen
Browse files

Refactored qs.add_q() and utils/tree.py

The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.

The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.

This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).

Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.

Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
parent d744c550
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -32,13 +32,14 @@ class GeoWhereNode(WhereNode):
    Used to represent the SQL where-clause for spatial databases --
    these are tied to the GeoQuery class that created it.
    """
    def add(self, data, connector):

    def _prepare_data(self, data):
        if isinstance(data, (list, tuple)):
            obj, lookup_type, value = data
            if ( isinstance(obj, Constraint) and
                 isinstance(obj.field, GeometryField) ):
                data = (GeoConstraint(obj), lookup_type, value)
        super(GeoWhereNode, self).add(data, connector)
        return super(GeoWhereNode, self)._prepare_data(data)

    def make_atom(self, child, qn, connection):
        lvalue, lookup_type, value_annot, params_or_value = child
+13 −0
Original line number Diff line number Diff line
"""
Classes to represent the definitions of aggregate functions.
"""
from django.db.models.constants import LOOKUP_SEP

def refs_aggregate(lookup_parts, aggregates):
    """
    A little helper method to check if the lookup_parts contains references
    to the given aggregates set. Because the LOOKUP_SEP is contained in the
    default annotation names we must check each prefix of the lookup_parts
    for match.
    """
    for i in range(len(lookup_parts) + 1):
        if LOOKUP_SEP.join(lookup_parts[0:i]) in aggregates:
            return True
    return False

class Aggregate(object):
    """
+0 −1
Original line number Diff line number Diff line
@@ -4,4 +4,3 @@ Constants used across the ORM in general.

# Separator used to split filter strings apart.
LOOKUP_SEP = '__'
+15 −3
Original line number Diff line number Diff line
import datetime

from django.db.models.aggregates import refs_aggregate
from django.db.models.constants import LOOKUP_SEP
from django.utils import tree

class ExpressionNode(tree.Node):
@@ -37,6 +40,18 @@ class ExpressionNode(tree.Node):
            obj.add(other, connector)
        return obj

    def contains_aggregate(self, existing_aggregates):
        if self.children:
            return any(child.contains_aggregate(existing_aggregates)
                       for child in self.children
                       if hasattr(child, 'contains_aggregate'))
        else:
            return refs_aggregate(self.name.split(LOOKUP_SEP),
                                  existing_aggregates)

    def prepare_database_save(self, unused):
        return self

    ###################
    # VISITOR METHODS #
    ###################
@@ -113,9 +128,6 @@ class ExpressionNode(tree.Node):
            "Use .bitand() and .bitor() for bitwise logical operations."
        )

    def prepare_database_save(self, unused):
        return self

class F(ExpressionNode):
    """
    An expression representing the value of the given field.
+11 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ class Q(tree.Node):
        if not isinstance(other, Q):
            raise TypeError(other)
        obj = type(self)()
        obj.connector = conn
        obj.add(self, conn)
        obj.add(other, conn)
        return obj
@@ -63,6 +64,16 @@ class Q(tree.Node):
        obj.negate()
        return obj

    def clone(self):
        clone = self.__class__._new_instance(
            children=[], connector=self.connector, negated=self.negated)
        for child in self.children:
            if hasattr(child, 'clone'):
                clone.children.append(child.clone())
            else:
                clone.children.append(child)
        return clone

class DeferredAttribute(object):
    """
    A wrapper for a deferred-loading field. When the value is read from this
Loading