#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 , 2 years ago
| Resolution: | → worksforme | 
|---|---|
| Status: | new → closed | 
comment:2 by , 2 years 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.
comment:3 by , 2 years ago
| Cc: | added | 
|---|
comment:4 by , 2 years ago
We tried it in a different project and confirmed it worked, I expect this is an issue with the mssql-django DB connector.
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:
Feel free to re-open when you can provide further details.