[Django] #34972: Aggregate with CombinedExpression resulting in NotImplemented instead of None after update from 3.2 -> 4.2

17 views
Skip to first unread message

Django

unread,
Nov 16, 2023, 8:37:45 PM11/16/23
to django-...@googlegroups.com
#34972: Aggregate with CombinedExpression resulting in NotImplemented instead of
None after update from 3.2 -> 4.2
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
ElRoberto538 |
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) | Keywords: CombinedExpression,
Severity: Normal | NotImplemented, aggregate
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi we're currently updating to Django 4.2 before 3.2 extended support
ends, and have discovered a weird issue with some of our queries. Using
3.2 our simplified example query below would return None, now it returns
NotImplemented, is this expected behavior? Shouldn't CombinedExpression
have empty_result_set_value set to None to maintain existing behavior?


{{{
OurModel.objects.none().aggregate(random_aggregate=Sum('field_1')/Count('field_2'))['random_aggregate']
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34972>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2023, 9:01:46 PM11/16/23
to django-...@googlegroups.com
#34972: Aggregate with CombinedExpression resulting in NotImplemented instead of
None after update from 3.2 -> 4.2
-------------------------------------+-------------------------------------
Reporter: ElRoberto538 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: CombinedExpression, | Triage Stage:
NotImplemented, aggregate | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* status: new => closed
* resolution: => worksforme


Comment:

Hello, thank you for your report.

Could you please provide more details to help us triage this ticket? With
the given information I can't reproduce the exception (I used a simple
model I created for this report).
Specifically, we'd need:
1. Your model definitions to exercise the provided query
2. The exact exception you are getting

Feel free to re-open when you can provide further details.

--
Ticket URL: <https://code.djangoproject.com/ticket/34972#comment:1>

Django

unread,
Nov 17, 2023, 1:22:19 AM11/17/23
to django-...@googlegroups.com
#34972: Aggregate with CombinedExpression resulting in NotImplemented instead of
None after update from 3.2 -> 4.2
-------------------------------------+-------------------------------------
Reporter: ElRoberto538 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: CombinedExpression, | Triage Stage:
NotImplemented, aggregate | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I think the issue is slightly more complicated than setting
`empty_result_set_value = None` as I would expect

{{{#!python
OurModel.objects.none().aggregate(random_aggregate=Count('field_2')/Count('field_2'))['random_aggregate']
}}}

To return `{"random_aggregate": 0}`.

The fact `NotImplemented` is returned here is problematic though and I
assume you have some third party apps that interacts with query
compilation in a way that does not account for the fact that an aggregate
that doesn't implement `empty_result_set_value` from an empty query should
be evaluated by the database.

--
Ticket URL: <https://code.djangoproject.com/ticket/34972#comment:2>

Django

unread,
Nov 17, 2023, 8:58:14 AM11/17/23
to django-...@googlegroups.com
#34972: Aggregate with CombinedExpression resulting in NotImplemented instead of
None after update from 3.2 -> 4.2
-------------------------------------+-------------------------------------
Reporter: ElRoberto538 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: CombinedExpression, | Triage Stage:
NotImplemented, aggregate | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* cc: David Sanders (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34972#comment:3>

Django

unread,
Nov 19, 2023, 5:19:49 PM11/19/23
to django-...@googlegroups.com
#34972: Aggregate with CombinedExpression resulting in NotImplemented instead of
None after update from 3.2 -> 4.2
-------------------------------------+-------------------------------------
Reporter: ElRoberto538 | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: CombinedExpression, | Triage Stage:
NotImplemented, aggregate | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ElRoberto538):

We tried it in a different project and confirmed it worked, I expect this
is an issue with the mssql-django DB connector.

--
Ticket URL: <https://code.djangoproject.com/ticket/34972#comment:4>

Reply all
Reply to author
Forward
0 new messages