2009-01-15 19:06:34 +08:00
|
|
|
"""
|
|
|
|
Classes to represent the definitions of aggregate functions.
|
|
|
|
"""
|
2013-12-25 21:13:18 +08:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.db.models.expressions import Func, Value
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.fields import FloatField, IntegerField
|
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.
2012-05-25 05:27:24 +08:00
|
|
|
|
2013-10-18 19:25:30 +08:00
|
|
|
__all__ = [
|
|
|
|
'Aggregate', 'Avg', 'Count', 'Max', 'Min', 'StdDev', 'Sum', 'Variance',
|
|
|
|
]
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class Aggregate(Func):
|
|
|
|
contains_aggregate = True
|
|
|
|
name = None
|
|
|
|
|
2015-01-02 09:39:31 +08:00
|
|
|
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
|
2013-12-25 21:13:18 +08:00
|
|
|
assert len(self.source_expressions) == 1
|
2015-01-02 09:39:31 +08:00
|
|
|
# Aggregates are not allowed in UPDATE queries, so ignore for_save
|
2013-12-25 21:13:18 +08:00
|
|
|
c = super(Aggregate, self).resolve_expression(query, allow_joins, reuse, summarize)
|
|
|
|
if c.source_expressions[0].contains_aggregate and not summarize:
|
|
|
|
name = self.source_expressions[0].name
|
|
|
|
raise FieldError("Cannot compute %s('%s'): '%s' is an aggregate" % (
|
|
|
|
c.name, name, name))
|
|
|
|
c._patch_aggregate(query) # backward-compatibility support
|
|
|
|
return c
|
|
|
|
|
|
|
|
@property
|
|
|
|
def input_field(self):
|
|
|
|
return self.source_expressions[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_alias(self):
|
|
|
|
if hasattr(self.source_expressions[0], 'name'):
|
|
|
|
return '%s__%s' % (self.source_expressions[0].name, self.name.lower())
|
|
|
|
raise TypeError("Complex expressions require an alias")
|
|
|
|
|
|
|
|
def get_group_by_cols(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _patch_aggregate(self, query):
|
2009-01-15 19:06:34 +08:00
|
|
|
"""
|
2013-12-25 21:13:18 +08:00
|
|
|
Helper method for patching 3rd party aggregates that do not yet support
|
|
|
|
the new way of subclassing. This method should be removed in 2.0
|
|
|
|
|
|
|
|
add_to_query(query, alias, col, source, is_summary) will be defined on
|
|
|
|
legacy aggregates which, in turn, instantiates the SQL implementation of
|
|
|
|
the aggregate. In all the cases found, the general implementation of
|
|
|
|
add_to_query looks like:
|
|
|
|
|
|
|
|
def add_to_query(self, query, alias, col, source, is_summary):
|
|
|
|
klass = SQLImplementationAggregate
|
|
|
|
aggregate = klass(col, source=source, is_summary=is_summary, **self.extra)
|
|
|
|
query.aggregates[alias] = aggregate
|
|
|
|
|
|
|
|
By supplying a known alias, we can get the SQLAggregate out of the
|
|
|
|
aggregates dict, and use the sql_function and sql_template attributes
|
|
|
|
to patch *this* aggregate.
|
2009-01-15 19:06:34 +08:00
|
|
|
"""
|
2013-12-25 21:13:18 +08:00
|
|
|
if not hasattr(self, 'add_to_query') or self.function is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
placeholder_alias = "_XXXXXXXX_"
|
|
|
|
self.add_to_query(query, placeholder_alias, None, None, None)
|
|
|
|
sql_aggregate = query.aggregates.pop(placeholder_alias)
|
|
|
|
if 'sql_function' not in self.extra and hasattr(sql_aggregate, 'sql_function'):
|
|
|
|
self.extra['function'] = sql_aggregate.sql_function
|
|
|
|
|
|
|
|
if hasattr(sql_aggregate, 'sql_template'):
|
|
|
|
self.extra['template'] = sql_aggregate.sql_template
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Avg(Aggregate):
|
2013-12-25 21:13:18 +08:00
|
|
|
function = 'AVG'
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Avg'
|
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
def __init__(self, expression, **extra):
|
2015-04-24 08:13:24 +08:00
|
|
|
output_field = extra.pop('output_field', FloatField())
|
|
|
|
super(Avg, self).__init__(expression, output_field=output_field, **extra)
|
2013-12-25 21:13:18 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Count(Aggregate):
|
2013-12-25 21:13:18 +08:00
|
|
|
function = 'COUNT'
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Count'
|
2013-12-25 21:13:18 +08:00
|
|
|
template = '%(function)s(%(distinct)s%(expressions)s)'
|
|
|
|
|
|
|
|
def __init__(self, expression, distinct=False, **extra):
|
|
|
|
if expression == '*':
|
|
|
|
expression = Value(expression)
|
|
|
|
super(Count, self).__init__(
|
|
|
|
expression, distinct='DISTINCT ' if distinct else '', output_field=IntegerField(), **extra)
|
|
|
|
|
2015-01-27 10:40:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "{}({}, distinct={})".format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.arg_joiner.join(str(arg) for arg in self.source_expressions),
|
|
|
|
'False' if self.extra['distinct'] == '' else 'True',
|
|
|
|
)
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def convert_value(self, value, expression, connection, context):
|
2013-12-25 21:13:18 +08:00
|
|
|
if value is None:
|
|
|
|
return 0
|
|
|
|
return int(value)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Max(Aggregate):
|
2013-12-25 21:13:18 +08:00
|
|
|
function = 'MAX'
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Max'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Min(Aggregate):
|
2013-12-25 21:13:18 +08:00
|
|
|
function = 'MIN'
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Min'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class StdDev(Aggregate):
|
|
|
|
name = 'StdDev'
|
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
def __init__(self, expression, sample=False, **extra):
|
|
|
|
self.function = 'STDDEV_SAMP' if sample else 'STDDEV_POP'
|
|
|
|
super(StdDev, self).__init__(expression, output_field=FloatField(), **extra)
|
|
|
|
|
2015-01-27 10:40:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "{}({}, sample={})".format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.arg_joiner.join(str(arg) for arg in self.source_expressions),
|
|
|
|
'False' if self.function == 'STDDEV_POP' else 'True',
|
|
|
|
)
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def convert_value(self, value, expression, connection, context):
|
2013-12-25 21:13:18 +08:00
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return float(value)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Sum(Aggregate):
|
2013-12-25 21:13:18 +08:00
|
|
|
function = 'SUM'
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Sum'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Variance(Aggregate):
|
|
|
|
name = 'Variance'
|
2013-12-25 21:13:18 +08:00
|
|
|
|
|
|
|
def __init__(self, expression, sample=False, **extra):
|
|
|
|
self.function = 'VAR_SAMP' if sample else 'VAR_POP'
|
|
|
|
super(Variance, self).__init__(expression, output_field=FloatField(), **extra)
|
|
|
|
|
2015-01-27 10:40:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "{}({}, sample={})".format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.arg_joiner.join(str(arg) for arg in self.source_expressions),
|
|
|
|
'False' if self.function == 'VAR_POP' else 'True',
|
|
|
|
)
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def convert_value(self, value, expression, connection, context):
|
2013-12-25 21:13:18 +08:00
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return float(value)
|