Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#34972 closed Bug (worksforme)

Aggregate with CombinedExpression resulting in NotImplemented instead of None after update from 3.2 -> 4.2

Reported by: ElRoberto538 Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: CombinedExpression, NotImplemented, aggregate
Cc: David Sanders Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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']

Change History (4)

comment:1 by Natalia Bidart, 13 months ago

Resolution: worksforme
Status: newclosed

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.

comment:2 by Simon Charette, 13 months ago

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

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 expression that doesn't implement empty_result_set_value from an empty query should be evaluated by the database.

Last edited 13 months ago by Simon Charette (previous) (diff)

comment:3 by David Sanders, 13 months ago

Cc: David Sanders added

comment:4 by ElRoberto538, 13 months ago

We tried it in a different project and confirmed it worked, I expect this is an issue with the mssql-django DB connector. Confirmed non-issue, we had a version mismatch between mssql-django and pyodbc due to running Apple Silicon. Installing the latest version of both using a work-around gives me the expected output of None when using the original query we had an issue with (for reference the test query above still returns NotImplemented, but I guess it was just a bad example in this case).

Last edited 13 months ago by ElRoberto538 (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top