#26430: Coalesce in Aggregations ignored when EmptyResultSet returned
-------------------------------------+-------------------------------------
     Reporter:  Ryan Prater          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.9
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  aggregation          |             Triage Stage:  Accepted
  coalesce in queryset               |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

 Replying to [comment:7 Simon Charette]:
 > This happens to be addressed by
 https://github.com/django/django/pull/12602

 I was looking into reviving https://github.com/django/django/pull/12602.

 It seems that some parts of that were already addressed in
 c2d4926702045e342a668057f0a758eec9db9436.
 Notably this
 
[https://github.com/django/django/commit/c2d4926702045e342a668057f0a758eec9db9436
 #diff-68490047146b30a432d0d7b7793005e89c112289dcb273fa082b4e9df9b81af7R977
 solved] the `None` instead of `0` issue for `Count()` in
 `aggregation_regress.tests.AggregationTests.test_empty_filter_aggregate`.

 It doesn't seem to be true that this ticket will be addressed by that PR
 as implied by
 [https://github.com/django/django/pull/11715#issuecomment-602050401 this
 comment].
 Changing the test to use `Coalesce()` as below still fails with
 `total_age` being `None`. There is nothing to determine that the second
 argument of `Coalesce()` should be used.

 {{{#!diff
              self.assertEqual(
 -
 
Author.objects.filter(id__in=[]).annotate(Count('friends')).aggregate(Count('pk')),
 -                {'pk__count': 0},
 +
 
Author.objects.filter(pk__in=[]).annotate(Count('friends')).aggregate(total_age=Coalesce(Sum('age'),
 0)),
 +                {'total_age': 0},
              )
 }}}

 In the case of `Count()` we get 0 because of `Count.convert_value()`
 explicitly returning `0` if the value is `None`.

 It seems that https://github.com/django/django/pull/6361 would be the way
 forward.
 Was there a reason you abandoned that approach, Simon? Would you be happy
 for me to pick that up again?

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.84ce98e6dca56f0ac91b6658d3ae12d3%40djangoproject.com.

Reply via email to