Opened 8 years ago

Closed 8 years ago

#27980 closed Bug (invalid)

ExpressionWrapper is maybe masking a NotImplementedError on SQLite

Reported by: Denis Roldán Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: db, orm, sqlite, datetime, NotImplementedError
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello folks!

The new ExpressionWrapper is maybe masking some NotImplementedErrors. Here's an example:

Under Django 1.10 & Python 3.6:

If I try

tasks = Task.objects.all()
tasks.annotate(time_elapsed=Sum(F('resolution_date')-F('request_date'), output_field=DateTimeField())).aggregate(Avg('time_elapsed'))

Falls correctly into:

NotImplementedError: You cannot use Sum, Avg, StdDev, and Variance aggregations on date/time fields in sqlite3 since date/time is saved as text.

Of course, if I change into Postgre all runs smoothly. Unfortunately if I use the brand new ExpressionWrapper I don't get any exception raised:

from django.db.models import ExpressionWrapper, F, DurationField, Avg

tasks = Task.objects.all()
duration = ExpressionWrapper(F('resolution_date') - F('request_date'), output_field=DurationField())
tasks.annotate(time_elapsed=duration).aggregate(Avg('time_elapsed'))
{'time_elapsed__avg': None}

Under postgre and MySQL I can get that value and, of course, is not None. And, by the way, you can do this:

tasks.annotate(time_elapsed=duration)[0].time_elapsed
datetime.timedelta(16, 74471, 878000)

So maybe it's a bug of the Avg Implementation when using a ExpressionWrapper or maybe the exception is masked as None.

Change History (3)

comment:1 by Tim Graham, 8 years ago

I'm not sure this has anything to do with ExpressionWrapper. The first example uses output_field=DateTimeField() but the second uses output_field=DurationField() which doesn't have the limitation as far as I see. I think the issue is invalid, but let me know if I missed something as I didn't do any testing.

in reply to:  1 comment:2 by Denis Roldán, 8 years ago

Replying to Tim Graham:

I'm not sure this has anything to do with ExpressionWrapper. The first example uses output_field=DateTimeField() but the second uses output_field=DurationField() which doesn't have the limitation as far as I see. I think the issue is invalid, but let me know if I missed something as I didn't do any testing.

Damn... You're right. Sorry and thanks for your effort!!

comment:3 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top