Ticket #3566: queryset_modular_aggregates.diff
| File queryset_modular_aggregates.diff, 5.4 kB (added by jbronn, 1 year ago) |
|---|
-
a/django/db/models/sql/query.py
old new 16 16 from django.db.models import signals 17 17 from django.db.models.fields import FieldDoesNotExist 18 18 from django.db.models.query_utils import select_related_descend 19 from django.db.models.sql import aggregates 19 from django.db.models.sql import aggregates as aggregates_module 20 20 from django.db.models.sql.where import WhereNode, EverythingNode, AND, OR 21 21 from django.core.exceptions import FieldError 22 22 from datastructures import EmptyResultSet, Empty, MultiJoin … … 40 40 41 41 alias_prefix = 'T' 42 42 query_terms = QUERY_TERMS 43 43 aggregates = aggregates_module 44 44 45 def __init__(self, model, connection, where=WhereNode): 45 46 self.model = model 46 47 self.connection = connection … … 198 199 obj._setup_query() 199 200 return obj 200 201 202 def normalize(self, aggregate, value): 203 """ 204 Returns a normalized Python object from the given aggregate object 205 and raw database value. 206 """ 207 return self.connection.ops.db_aggregate_to_value(aggregate, value) 208 201 209 def results_iter(self): 202 210 """ 203 211 Returns an iterator over the results from executing this query. 204 212 """ 205 213 resolve_columns = hasattr(self, 'resolve_columns') 206 214 fields = None 207 normalize = self.connection.ops.db_aggregate_to_value208 215 for rows in self.execute_sql(MULTI): 209 216 for row in rows: 210 217 if resolve_columns: … … 221 228 if self.aggregate_select: 222 229 aggregate_start = len(self.extra_select.keys()) + len(self.select) 223 230 row = row[:aggregate_start] + tuple( 224 normalize(aggregate, value)231 self.normalize(aggregate, value) 225 232 for (alias, aggregate), value 226 233 in zip(self.aggregate_select.items(), row[aggregate_start:]) 227 234 ) … … 263 270 query.select_related = False 264 271 query.related_select_cols = [] 265 272 query.related_select_fields = [] 266 267 normalize = self.connection.ops.db_aggregate_to_value 273 268 274 return dict( 269 (alias, normalize(aggregate, val))275 (alias, self.normalize(aggregate, val)) 270 276 for (alias, aggregate), val 271 277 in zip(query.aggregate_select.items(), query.execute_sql(SINGLE)) 272 278 ) … … 1178 1184 aggregate_expr.lookup in self.aggregate_select.keys()): 1179 1185 # Aggregate is over an annotation 1180 1186 field_name = field_list[0] 1181 aggregate = aggregate_expr.add_to_query(self, aggregates,1187 aggregate = aggregate_expr.add_to_query(self, self.aggregates, 1182 1188 col=field_name, 1183 1189 source=self.aggregate_select[field_name], 1184 1190 is_summary=is_summary) … … 1197 1203 for column_alias in join_list: 1198 1204 self.promote_alias(column_alias, unconditional=True) 1199 1205 1200 aggregate = aggregate_expr.add_to_query(self, aggregates,1206 aggregate = aggregate_expr.add_to_query(self, self.aggregates, 1201 1207 col=(join_list[-1], col), 1202 1208 source=target, 1203 1209 is_summary=is_summary) … … 1211 1217 col = (opts.db_table, field.column) 1212 1218 else: 1213 1219 col = field_name 1214 aggregate = aggregate_expr.add_to_query(self, aggregates,1220 aggregate = aggregate_expr.add_to_query(self, self.aggregates, 1215 1221 col=col, 1216 1222 source=field, 1217 1223 is_summary=is_summary) … … 1790 1796 """ 1791 1797 if not self.distinct: 1792 1798 if not self.select: 1793 count = aggregates.Count('*', is_summary=True)1799 count = self.aggregates.Count('*', is_summary=True) 1794 1800 else: 1795 1801 assert len(self.select) == 1, \ 1796 1802 "Cannot add count col with multiple cols in 'select': %r" % self.select 1797 count = aggregates.Count(self.select[0])1803 count = self.aggregates.Count(self.select[0]) 1798 1804 else: 1799 1805 opts = self.model._meta 1800 1806 if not self.select: 1801 count = aggregates.Count((self.join((None, opts.db_table, None, None)), opts.pk.column),1807 count = self.aggregates.Count((self.join((None, opts.db_table, None, None)), opts.pk.column), 1802 1808 is_summary=True, distinct=True) 1803 1809 else: 1804 1810 # Because of SQL portability issues, multi-column, distinct … … 1806 1812 assert len(self.select) == 1, \ 1807 1813 "Cannot add count col with multiple cols in 'select'." 1808 1814 1809 count = aggregates.Count(self.select[0], distinct=True)1815 count = self.aggregates.Count(self.select[0], distinct=True) 1810 1816 # Distinct handling is done in Count(), so don't do it at this 1811 1817 # level. 1812 1818 self.distinct = False
