Commits

Mike Bayer committed a4f94c0

SelectResults will use a subselect, when calling an aggregate (i.e.
max, min, etc.) on a SelectResults that has an ORDER BY clause
[ticket:252]

  • Participants
  • Parent commits e7ebf2f

Comments (0)

Files changed (2)

 - inheritance check uses issubclass() instead of direct __mro__ check
 to make sure class A inherits from B, allowing mapper inheritance to more 
 flexibly correspond to class inheritance [ticket:271]
+- SelectResults will use a subselect, when calling an aggregate (i.e.
+max, min, etc.) on a SelectResults that has an ORDER BY clause
+[ticket:252]
 
 0.2.6
 - big overhaul to schema to allow truly composite primary and foreign

lib/sqlalchemy/ext/selectresults.py

     def count(self):
         """executes the SQL count() function against the SelectResults criterion."""
         return self._query.count(self._clause)
-    
+
+    def _col_aggregate(self, col, func):
+        """executes func() function against the given column
+
+        For performance, only use subselect if order_by attribute is set.
+        
+        """
+        if self._ops.get('order_by'):
+            s1 = sql.select([col], self._clause, **self._ops).alias('u')
+            return sql.select([func(s1.corresponding_column(col))]).scalar()
+        else:
+            return sql.select([func(col)], self._clause, **self._ops).scalar()
+
     def min(self, col):
         """executes the SQL min() function against the given column"""
-        return sql.select([sql.func.min(col)], self._clause, **self._ops).scalar()
+        return self._col_aggregate(col, sql.func.min)
 
     def max(self, col):
         """executes the SQL max() function against the given column"""
-        return sql.select([sql.func.max(col)], self._clause, **self._ops).scalar()
+        return self._col_aggregate(col, sql.func.max)
 
     def sum(self, col):
         """executes the SQL sum() function against the given column"""
-        return sql.select([sql.func.sum(col)], self._clause, **self._ops).scalar()
+        return self._col_aggregate(col, sql.func.sum)
 
     def avg(self, col):
         """executes the SQL avg() function against the given column"""
-        return sql.select([sql.func.avg(col)], self._clause, **self._ops).scalar()
+        return self._col_aggregate(col, sql.func.avg)
 
     def clone(self):
         """creates a copy of this SelectResults."""