#33018: Incorrect annotation value when doing a subquery with empty queryset
-------------------------------------+-------------------------------------
     Reporter:  decomorreno          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  orm, annotate,       |             Triage Stage:  Accepted
  EmptyQuerySet, empty, count        |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted


Comment:

 The `0` assignment on empty result set comes from
 
[https://github.com/django/django/blob/6c3525a09db5177bf4e3856de85bf8b1300402d5/django/db/models/sql/compiler.py#L268-L270
 this line]. I assume we could adjust the logic to rely on `getattr(col,
 'empty_aggregate_value', NotImplemented)` and fallback to `'0'` if it's
 missing.

 Makes me wonder if we want to rename `empty_aggregate_value` to
 `empty_result_set_value` instead since it's not entirely bound to
 aggregation anymore.

 e.g. the following should exhibit the same behavior

 {{{#!python
 Author.objects.annotate(annotation=Coalesce(Author.objects.empty(), 42))
 }}}

 It also seems weird that we default to `0` as opposed to `NULL` which
 would be a more correct value.

 Alternatively we could adjust `Coalesce.as_sql` to catch `EmptyResultSet`
 when it's compiling its source expressions but that's more involved as
 most of the logic for that currently lives in `Func.as_sql`. We could also
 use both of these approaches.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33018#comment:2>
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/069.3c3697a9acf163402273031a90166e61%40djangoproject.com.

Reply via email to