[Django] #32463: 'Sum' aggregate function not working with Window function for sqlite3 version 3.27.2

81 views
Skip to first unread message

Django

unread,
Feb 19, 2021, 11:34:12 AM2/19/21
to django-...@googlegroups.com
#32463: 'Sum' aggregate function not working with Window function for sqlite3
version 3.27.2
--------------------------------------------+------------------------
Reporter: blackhorsesacher | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
'Sum' aggregate function not working with Window function for sqlite3
version 3.27.2
Produces these errors:
>>>sqlite3.OperationalError: near "OVER": syntax error
>>>django.db.utils.OperationalError: near "OVER": syntax error
This is similar to the fixed issue #30027 "SQLite (pre 3.25.0) does not
support window functions, raises OperationalError", but I am using SQLite
3.27.2 and the native SQL query is working from sqlite3 command line and
also when using Model.objects.raw(), which I'm having to use as a
workaround.
It should be reproducible as follows, assuming model "myapp.mymodel" has a
field "amount":
>>>from django.db.models import Sum, Window
>>>from myapp.models import mymodel
>>>mymodel.objects.all().annotate(cumsum=Window(expression=Sum('amount')))
The raw query works OK:
>>>rawqs=mymodel.objects.raw("SELECT *, SUM (amount) OVER (ORDER BY id) AS
cumsum FROM myapp_mymodel")

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

Django

unread,
Feb 19, 2021, 2:34:52 PM2/19/21
to django-...@googlegroups.com
#32463: 'Sum' aggregate function not working with Window function on SQLite.
-------------------------------------+-------------------------------------
Reporter: blackhorsesacher | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate
* component: Uncategorized => Database layer (models, ORM)


Old description:

> 'Sum' aggregate function not working with Window function for sqlite3
> version 3.27.2
> Produces these errors:
> >>>sqlite3.OperationalError: near "OVER": syntax error
> >>>django.db.utils.OperationalError: near "OVER": syntax error
> This is similar to the fixed issue #30027 "SQLite (pre 3.25.0) does not
> support window functions, raises OperationalError", but I am using SQLite
> 3.27.2 and the native SQL query is working from sqlite3 command line and
> also when using Model.objects.raw(), which I'm having to use as a
> workaround.
> It should be reproducible as follows, assuming model "myapp.mymodel" has
> a field "amount":
> >>>from django.db.models import Sum, Window
> >>>from myapp.models import mymodel
> >>>mymodel.objects.all().annotate(cumsum=Window(expression=Sum('amount')))
> The raw query works OK:
> >>>rawqs=mymodel.objects.raw("SELECT *, SUM (amount) OVER (ORDER BY id)
> AS cumsum FROM myapp_mymodel")

New description:

'Sum' aggregate function not working with Window function for sqlite3
version 3.27.2
Produces these errors:
{{{
>>>sqlite3.OperationalError: near "OVER": syntax error
>>>django.db.utils.OperationalError: near "OVER": syntax error
}}}
This is similar to the fixed issue #30027 "SQLite (pre 3.25.0) does not
support window functions, raises OperationalError", but I am using SQLite
3.27.2 and the native SQL query is working from sqlite3 command line and
also when using Model.objects.raw(), which I'm having to use as a
workaround.
It should be reproducible as follows, assuming model "myapp.mymodel" has a
field "amount":
{{{
>>> from django.db.models import Sum, Window
>>> from myapp.models import mymodel
>>>
mymodel.objects.all().annotate(cumsum=Window(expression=Sum('amount')))
}}}
The raw query works OK:
{{{
>>> rawqs=mymodel.objects.raw("SELECT *, SUM (amount) OVER (ORDER BY id)
AS cumsum FROM myapp_mymodel")
}}}

--

Comment:

Thanks for this report, It looks like a duplicate of #31723 (fixed in
71d10ca8c90ccc1fd0ccd6683716dd3c3116ae6a, Django 3.2+) . Can you confirm
that `amount` is a `DecimalField`?

--
Ticket URL: <https://code.djangoproject.com/ticket/32463#comment:1>

Django

unread,
Feb 19, 2021, 5:56:52 PM2/19/21
to django-...@googlegroups.com
#32463: 'Sum' aggregate function not working with Window function on SQLite.
-------------------------------------+-------------------------------------
Reporter: blackhorsesacher | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by blackhorsesacher):

Thank you for the very rapid response. Yes, I confirm that 'amount' is a
DecimalField

--
Ticket URL: <https://code.djangoproject.com/ticket/32463#comment:2>

Django

unread,
Feb 20, 2021, 6:36:55 AM2/20/21
to django-...@googlegroups.com
#32463: 'Sum' aggregate function not working with Window function on SQLite.
-------------------------------------+-------------------------------------
Reporter: blackhorsesacher | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by blackhorsesacher):

Replying to [comment:2 blackhorsesacher]:


> Thank you for the very rapid response. Yes, I confirm that 'amount' is
a DecimalField

I've now tested with Django 3.2 beta 1 and confirm that it's now working
well. Thanks again.

--
Ticket URL: <https://code.djangoproject.com/ticket/32463#comment:3>

Reply all
Reply to author
Forward
0 new messages