Opened 22 months ago

Closed 22 months ago

Last modified 22 months ago

#34372 closed Bug (fixed)

Field position reference for aggregate ends up in group-by clause

Reported by: Jannis Vajen Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 4.2
Severity: Release blocker Keywords: orm aggregation
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Changeset 278881e37619278789942513916acafaa88d26f3 introduced a regression. Aggregate queries are rejected by the database due to the aggregated field being added to the GROUP BY clause.

It was difficult for me to pin down, especially because it looks like the error only occurs on the second evaluation of the query. The first query is executed just fine and doesn't contain the position reference to the aggregated field in the GROUP BY, only the second one. Below is a test to reproduce the behaviour:

  • tests/aggregation_regress/tests.py

    diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
    index bfb3919b23..05122db956 100644
    a b class AggregationTests(TestCase):  
    13211321            lambda b: (b.name, b.authorCount),
    13221322        )
    13231323
     1324    def test_quoting_aggregate_order_by_f(self):
     1325        author = Author.objects.get(name="Peter Norvig")
     1326        qs = (
     1327            author.book_set.all()
     1328            .annotate(num=Count("authors"))
     1329            .order_by(F("num").desc())
     1330        )
     1331        list(qs.iterator())
     1332        list(qs.iterator())
     1333
    13241334    def test_stddev(self):
    13251335        self.assertEqual(
    13261336            Book.objects.aggregate(StdDev("pages")),

Change History (6)

comment:1 by Jannis Vajen, 22 months ago

Type: UncategorizedBug

comment:2 by Simon Charette, 22 months ago

Owner: changed from nobody to Simon Charette
Status: newassigned
Triage Stage: UnreviewedAccepted

Thanks a lot for the report and the test Jannis, pretty sure this due to a lack of field.copy() 278881e37619278789942513916acafaa88d26f3 if this only happens on query re-evaluation.

comment:3 by Simon Charette, 22 months ago

Has patch: set

comment:4 by Mariusz Felisiak, 22 months ago

Summary: Regression: field position reference for aggregate ends up in group-by clauseField position reference for aggregate ends up in group-by clause
Triage Stage: AcceptedReady for checkin

comment:5 by GitHub <noreply@…>, 22 months ago

Resolution: fixed
Status: assignedclosed

In b15f162:

Fixed #34372 -- Fixed queryset crash on order by aggregation using OrderBy.

Regression in 278881e37619278789942513916acafaa88d26f3 caused by a lack
of expression copying when an OrderBy expression is explicitly provided.

Thanks Jannis Vajen for the report and regression test.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 22 months ago

In 872dade:

[4.2.x] Fixed #34372 -- Fixed queryset crash on order by aggregation using OrderBy.

Regression in 278881e37619278789942513916acafaa88d26f3 caused by a lack
of expression copying when an OrderBy expression is explicitly provided.

Thanks Jannis Vajen for the report and regression test.
Backport of b15f162f252610e3b510ade465549769ab4356cf from main

Note: See TracTickets for help on using tickets.
Back to Top