Wednesday, March 30, 2016

Re: POSSIBLE BUG: using Coalesce() with __in=[]

Thank you Simon!

I'll file a bug and include a link here, including your points. Your source documentation was incredibly helpful.

On Wednesday, March 30, 2016 at 12:03:10 PM UTC-5, Simon Charette wrote:
Hi Ryan,

I think this should be considered a bug.

From what I understand the ORM simply doesn't perform any query in this case
as the `pk__in` lookup cannot match any `OrderItem` and result in an
`EmptyResultSet` exception[1].

This exception is caught in the `Query.get_aggregation()` method where all
aggregates are converted to `None`[2].

I suppose we should alter the `except EmptyResultSet` clause to account for
`outer_query.annotation_select` items that are `Coalesce()` instances used with
`Value()` but I'm unsure about how it should be done.

I think this will be hard to fix correctly as `Coalesce` can be nested.

e.g.

# This should return `0`
Coalesce(Coalesce(Sum('quantity'), Value(0)), F('other_field'))

Cheers,
Simon

[1] https://github.com/django/django/blob/2e0cd26ffb29189add1e0435913fd1490f52b20d/django/db/models/lookups.py#L221-L223
[2] https://github.com/django/django/blob/2e0cd26ffb29189add1e0435913fd1490f52b20d/django/db/models/sql/query.py#L439-L445

Le mercredi 30 mars 2016 12:27:49 UTC-4, Ryan Prater a écrit :
It seems that using an empty list when using the `__in=` filter prevents an Aggregate Coalesce from working properly. See below:

# Test with matched Queryset. Sum will return 50
OrderItem.objects.filter(pk__in=[1]).aggregate(test=Coalesce(Sum('quantity'), Value(0)))
>>> {'test': 50}
 
# Test with unmatched Queryset. Sum will return 0
OrderItem.objects.filter(pk__in=[-1]).aggregate(test=Coalesce(Sum('quantity'), Value(0)))
>>> {'test':0}
 
# Test with unmatched Queryset (using empty list). Sum will return NONE
OrderItem.objects.filter(pk__in=[]).aggregate(test=Coalesce(Sum('quantity'), Value(0)))
>>> {'test': None}

Can someone confirm? I will post as a bug if so.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc6081b8-71b1-4643-98da-9abb10b51d82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment