Re: [Django] #36751: Aggregation with an empty filter over a queryset with annotations crashes since Django 5.2.2

8 views
Skip to first unread message

Django

unread,
Nov 23, 2025, 11:51:13 AM11/23/25
to django-...@googlegroups.com
#36751: Aggregation with an empty filter over a queryset with annotations crashes
since Django 5.2.2
-------------------------------------+-------------------------------------
Reporter: Rafael Urben | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1

Comment:

This [https://github.com/django/django/pull/20303/ PR] should address the
issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/36751#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 24, 2025, 6:14:50 AM11/24/25
to django-...@googlegroups.com
#36751: Aggregation with an empty filter over a queryset with annotations crashes
since Django 5.2.2
-------------------------------------+-------------------------------------
Reporter: Rafael Urben | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"2a6e0bd72d4a69725b957d6748a4b834f21b12b5" 2a6e0bd7]:
{{{#!CommitTicketReference repository=""
revision="2a6e0bd72d4a69725b957d6748a4b834f21b12b5"
Fixed #36751 -- Fixed empty filtered aggregation crash over annotated
queryset.

Regression in b8e5a8a9a2a767f584cbe89a878a42363706f939.

Refs #36404.

The replace_expressions method was innapropriately dealing with falsey
but not None source expressions causing them to also be potentially
evaluated when __bool__ was invoked (e.g. QuerySet.__bool__ evaluates
the queryset).

The changes introduced in b8e5a8a9a2, which were to deal with a similar
issue, surfaced the problem as aggregation over an annotated queryset
requires an inlining (or pushdown) of aggregate references which is
achieved through replace_expressions.

In cases where an empty Q object was provided as an aggregate filter,
such as when the admin facetting feature was used as reported, it would
wrongly be turned into None, instead of an empty WhereNode, causing a
crash at aggregate filter compilation.

Note that the crash signature differed depending on whether or not the
backend natively supports aggregate filtering
(supports_aggregate_filter_clause) as the fallback, which makes use
Case / When expressions, would result in a TypeError instead of a
NoneType AttributeError.

Thanks Rafael Urben for the report, Antoliny and Youngkwang Yang for
the triage.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36751#comment:8>

Django

unread,
Nov 24, 2025, 6:15:38 AM11/24/25
to django-...@googlegroups.com
#36751: Aggregation with an empty filter over a queryset with annotations crashes
since Django 5.2.2
-------------------------------------+-------------------------------------
Reporter: Rafael Urben | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"abce62994620956c3c9cce6edcebf8fcfb2670d4" abce629]:
{{{#!CommitTicketReference repository=""
revision="abce62994620956c3c9cce6edcebf8fcfb2670d4"
[6.0.x] Fixed #36751 -- Fixed empty filtered aggregation crash over
annotated queryset.

Regression in b8e5a8a9a2a767f584cbe89a878a42363706f939.

Refs #36404.

The replace_expressions method was innapropriately dealing with falsey
but not None source expressions causing them to also be potentially
evaluated when __bool__ was invoked (e.g. QuerySet.__bool__ evaluates
the queryset).

The changes introduced in b8e5a8a9a2, which were to deal with a similar
issue, surfaced the problem as aggregation over an annotated queryset
requires an inlining (or pushdown) of aggregate references which is
achieved through replace_expressions.

In cases where an empty Q object was provided as an aggregate filter,
such as when the admin facetting feature was used as reported, it would
wrongly be turned into None, instead of an empty WhereNode, causing a
crash at aggregate filter compilation.

Note that the crash signature differed depending on whether or not the
backend natively supports aggregate filtering
(supports_aggregate_filter_clause) as the fallback, which makes use
Case / When expressions, would result in a TypeError instead of a
NoneType AttributeError.

Thanks Rafael Urben for the report, Antoliny and Youngkwang Yang for
the triage.
Backport of 2a6e0bd72d4a69725b957d6748a4b834f21b12b5 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36751#comment:9>

Django

unread,
Nov 24, 2025, 6:16:14 AM11/24/25
to django-...@googlegroups.com
#36751: Aggregation with an empty filter over a queryset with annotations crashes
since Django 5.2.2
-------------------------------------+-------------------------------------
Reporter: Rafael Urben | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"1e7327770c908cb51f888c9269322247fa3525a5" 1e73277]:
{{{#!CommitTicketReference repository=""
revision="1e7327770c908cb51f888c9269322247fa3525a5"
[5.2.x] Fixed #36751 -- Fixed empty filtered aggregation crash over
annotated queryset.

Regression in b8e5a8a9a2a767f584cbe89a878a42363706f939.

Refs #36404.

The replace_expressions method was innapropriately dealing with falsey
but not None source expressions causing them to also be potentially
evaluated when __bool__ was invoked (e.g. QuerySet.__bool__ evaluates
the queryset).

The changes introduced in b8e5a8a9a2, which were to deal with a similar
issue, surfaced the problem as aggregation over an annotated queryset
requires an inlining (or pushdown) of aggregate references which is
achieved through replace_expressions.

In cases where an empty Q object was provided as an aggregate filter,
such as when the admin facetting feature was used as reported, it would
wrongly be turned into None, instead of an empty WhereNode, causing a
crash at aggregate filter compilation.

Note that the crash signature differed depending on whether or not the
backend natively supports aggregate filtering
(supports_aggregate_filter_clause) as the fallback, which makes use
Case / When expressions, would result in a TypeError instead of a
NoneType AttributeError.

Thanks Rafael Urben for the report, Antoliny and Youngkwang Yang for
the triage.
Backport of 2a6e0bd72d4a69725b957d6748a4b834f21b12b5 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36751#comment:10>
Reply all
Reply to author
Forward
0 new messages