Ticket #12886: 12886.patch

File 12886.patch, 3.2 KB (added by Filip Gruszczyński, 14 years ago)

partial patch with tests showing the problem

  • django/db/models/sql/query.py

     
    333333        if not self.aggregate_select:
    334334            return {}
    335335
    336         # If there is a group by clause, aggregating does not add useful
    337         # information but retrieves only the first row. Aggregate
     336        # If there is a group by or limit clause, aggregating does not add
     337        # useful information but retrieves only the first row. Aggregate
    338338        # over the subquery instead.
    339         if self.group_by is not None:
     339        if self.group_by is not None or self.low_mark or self.high_mark:
    340340            from subqueries import AggregateQuery
    341341            query = AggregateQuery(self.model)
    342342
     
    390390            obj = AggregateQuery(obj.model)
    391391            obj.add_subquery(subquery, using=using)
    392392
     393        low_mark, high_mark = self.low_mark, self.high_mark
     394        obj.clear_limits()
    393395        obj.add_count_column()
    394396        number = obj.get_aggregation(using=using)[None]
    395397
    396398        # Apply offset and limit constraints manually, since using LIMIT/OFFSET
    397399        # in SQL (in variants that provide them) doesn't change the COUNT
    398400        # output.
    399         number = max(0, number - self.low_mark)
    400         if self.high_mark is not None:
    401             number = min(number, self.high_mark - self.low_mark)
     401        number = max(0, number - low_mark)
     402        if high_mark is not None:
     403            number = min(number, high_mark - low_mark)
    402404
    403405        return number
    404406
     
    920922                    aggregate.name, field_name, field_name))
    921923        elif ((len(field_list) > 1) or
    922924            (field_list[0] not in [i.name for i in opts.fields]) or
    923             self.group_by is None or
     925            (self.group_by is None and
     926             (not self.low_mark and not self.high_mark)) or
    924927            not is_summary):
    925928            # If:
    926929            #   - the field descriptor has more than one part (foo__bar), or
  • tests/modeltests/aggregation/tests.py

     
    1717        vals = Author.objects.aggregate(Avg("age"))
    1818        self.assertEqual(vals, {"age__avg": Approximate(37.4, places=1)})
    1919
     20    def test_slice_aggregate(self):
     21        vals = Author.objects.all()[0:2].aggregate(Avg("age"))
     22        self.assertEqual(vals, {"age__avg": Approximate(34.5, places=1)})
     23       
     24    def test_slice_annotate_aggregate(self):
     25        vals = Author.objects.all()[0:2].annotate().aggregate(Avg("age"))
     26        self.assertEqual(vals, {"age__avg": Approximate(34.5, places=1)})
     27
     28    def test_join_annotate_aggregate(self):
     29        vals = Book.objects.all().annotate().aggregate(Max("authors__age"))
     30        self.assertEqual(len(vals), 1)
     31        self.assertEqual(vals["authors__age__max"], 57)
     32       
    2033    def test_multiple_aggregates(self):
    2134        vals = Author.objects.aggregate(Sum("age"), Avg("age"))
    2235        self.assertEqual(vals, {"age__sum": 337, "age__avg": Approximate(37.4, places=1)})
Back to Top