2009-01-15 19:06:34 +08:00
|
|
|
"""
|
|
|
|
Classes to represent the default SQL aggregate functions
|
|
|
|
"""
|
2012-06-02 09:13:36 +08:00
|
|
|
import copy
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2012-02-23 03:40:27 +08:00
|
|
|
from django.db.models.fields import IntegerField, FloatField
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-11-03 19:45:15 +08:00
|
|
|
|
|
|
|
__all__ = ['Aggregate', 'Avg', 'Count', 'Max', 'Min', 'StdDev', 'Sum', 'Variance']
|
|
|
|
|
|
|
|
|
2012-02-23 03:40:27 +08:00
|
|
|
# Fake fields used to identify aggregate types in data-conversion operations.
|
|
|
|
ordinal_aggregate_field = IntegerField()
|
|
|
|
computed_aggregate_field = FloatField()
|
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 Aggregate(object):
|
|
|
|
"""
|
|
|
|
Default SQL Aggregate.
|
|
|
|
"""
|
|
|
|
is_ordinal = False
|
|
|
|
is_computed = False
|
|
|
|
sql_template = '%(function)s(%(field)s)'
|
|
|
|
|
|
|
|
def __init__(self, col, source=None, is_summary=False, **extra):
|
|
|
|
"""Instantiate an SQL aggregate
|
|
|
|
|
|
|
|
* col is a column reference describing the subject field
|
|
|
|
of the aggregate. It can be an alias, or a tuple describing
|
|
|
|
a table and column name.
|
|
|
|
* source is the underlying field or aggregate definition for
|
|
|
|
the column reference. If the aggregate is not an ordinal or
|
|
|
|
computed type, this reference is used to determine the coerced
|
|
|
|
output type of the aggregate.
|
|
|
|
* extra is a dictionary of additional data to provide for the
|
|
|
|
aggregate definition
|
|
|
|
|
|
|
|
Also utilizes the class variables:
|
|
|
|
* sql_function, the name of the SQL function that implements the
|
|
|
|
aggregate.
|
|
|
|
* sql_template, a template string that is used to render the
|
|
|
|
aggregate into SQL.
|
|
|
|
* is_ordinal, a boolean indicating if the output of this aggregate
|
|
|
|
is an integer (e.g., a count)
|
|
|
|
* is_computed, a boolean indicating if this output of this aggregate
|
|
|
|
is a computed float (e.g., an average), regardless of the input
|
|
|
|
type.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.col = col
|
|
|
|
self.source = source
|
|
|
|
self.is_summary = is_summary
|
|
|
|
self.extra = extra
|
|
|
|
|
|
|
|
# Follow the chain of aggregate sources back until you find an
|
|
|
|
# actual field, or an aggregate that forces a particular output
|
|
|
|
# type. This type of this field will be used to coerce values
|
|
|
|
# retrieved from the database.
|
|
|
|
tmp = self
|
|
|
|
|
|
|
|
while tmp and isinstance(tmp, Aggregate):
|
|
|
|
if getattr(tmp, 'is_ordinal', False):
|
|
|
|
tmp = ordinal_aggregate_field
|
|
|
|
elif getattr(tmp, 'is_computed', False):
|
|
|
|
tmp = computed_aggregate_field
|
|
|
|
else:
|
|
|
|
tmp = tmp.source
|
|
|
|
|
|
|
|
self.field = tmp
|
|
|
|
|
2013-03-02 07:06:56 +08:00
|
|
|
def relabeled_clone(self, change_map):
|
|
|
|
clone = copy.copy(self)
|
2009-01-15 19:06:34 +08:00
|
|
|
if isinstance(self.col, (list, tuple)):
|
2013-03-02 07:06:56 +08:00
|
|
|
clone.col = (change_map.get(self.col[0], self.col[0]), self.col[1])
|
|
|
|
return clone
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def as_sql(self, qn, connection):
|
2013-02-10 23:15:49 +08:00
|
|
|
"Return the aggregate, rendered as SQL with parameters."
|
|
|
|
params = []
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
if hasattr(self.col, 'as_sql'):
|
2013-02-10 23:15:49 +08:00
|
|
|
field_name, params = self.col.as_sql(qn, connection)
|
2009-01-15 19:06:34 +08:00
|
|
|
elif isinstance(self.col, (list, tuple)):
|
2013-08-30 07:20:00 +08:00
|
|
|
field_name = '.'.join(qn(c) for c in self.col)
|
2009-01-15 19:06:34 +08:00
|
|
|
else:
|
2013-09-24 08:52:58 +08:00
|
|
|
field_name = qn(self.col)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
substitutions = {
|
2009-01-15 19:06:34 +08:00
|
|
|
'function': self.sql_function,
|
|
|
|
'field': field_name
|
|
|
|
}
|
2013-02-10 23:15:49 +08:00
|
|
|
substitutions.update(self.extra)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
return self.sql_template % substitutions, params
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Avg(Aggregate):
|
|
|
|
is_computed = True
|
|
|
|
sql_function = 'AVG'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Count(Aggregate):
|
|
|
|
is_ordinal = True
|
|
|
|
sql_function = 'COUNT'
|
|
|
|
sql_template = '%(function)s(%(distinct)s%(field)s)'
|
|
|
|
|
|
|
|
def __init__(self, col, distinct=False, **extra):
|
2013-05-27 10:47:50 +08:00
|
|
|
super(Count, self).__init__(col, distinct='DISTINCT ' if distinct else '', **extra)
|
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):
|
|
|
|
sql_function = 'MAX'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Min(Aggregate):
|
|
|
|
sql_function = 'MIN'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class StdDev(Aggregate):
|
|
|
|
is_computed = True
|
|
|
|
|
|
|
|
def __init__(self, col, sample=False, **extra):
|
|
|
|
super(StdDev, self).__init__(col, **extra)
|
2013-05-17 22:33:36 +08:00
|
|
|
self.sql_function = 'STDDEV_SAMP' if sample else 'STDDEV_POP'
|
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 Sum(Aggregate):
|
|
|
|
sql_function = 'SUM'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Variance(Aggregate):
|
|
|
|
is_computed = True
|
|
|
|
|
|
|
|
def __init__(self, col, sample=False, **extra):
|
|
|
|
super(Variance, self).__init__(col, **extra)
|
2013-05-17 22:33:36 +08:00
|
|
|
self.sql_function = 'VAR_SAMP' if sample else 'VAR_POP'
|