[Django] #28797: Sum aggregation might return "-0.00" instead of None

6 views
Skip to first unread message

Django

unread,
Nov 14, 2017, 9:49:06 AM11/14/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of None
-------------------------------------+-------------------------------------
Reporter: simhnna | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have an application with the following Model:

{{{
class Split(models.Model):
amount = models.DecimalField(max_digits=10, decimal_places=2)
}}}

And perform the following query:
{{{
Split.objects.filter(...).aggregate(models.Sum('amount'))['amount__sum']
or 0
}}}

which returns the mentioned "-0.00" for specific filters. I'm not sure why
that happens.
A values_list for an example where it happens is here:
{{{
[29.63, -30.0, 155.5, -156.45, -0.08, 400.0, -400.0, 440.0, -463.98,
-10.0, -700.0, 735.0, -14.83, -150.0, 0.08, -9.5, -2.34, -62.82, -500.0,
735.0, -300.0, -500.0, 490.0, -170.6, -10.0, -300.0, -0.11, 735.0, 300.0,
-239.5]
}}}

The whole code is part of https://github.com/agstrike/silverstrike with
the query in
https://github.com/agstrike/silverstrike/blob/master/silverstrike/models.py#L54

If you point me at the right direction I might be able to debug a little

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

Django

unread,
Nov 14, 2017, 9:51:29 AM11/14/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of "0.00"
-------------------------------------+-------------------------------------
Reporter: Simon Hanna | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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

Django

unread,
Nov 14, 2017, 9:53:45 AM11/14/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of "0.00"
-------------------------------------+-------------------------------------
Reporter: Simon Hanna | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
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 Simon Hanna):

I'm using a sqlite database.

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

Django

unread,
Nov 14, 2017, 11:02:07 AM11/14/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of "0.00"
-------------------------------------+-------------------------------------
Reporter: Simon Hanna | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
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 Tim Graham):

Did you try execute a raw SQL query? It might SQLite at fault rather than
Django.

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

Django

unread,
Nov 15, 2017, 11:39:09 AM11/15/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of "0.00"
-------------------------------------+-------------------------------------
Reporter: Simon Hanna | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
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 camillo):

I cannot reproduce this. I wrote following test in tests/aggregation and
it passed. Can you check, if it also pass on your machine?

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def test_sum_to_zero(self):
for price in [29.63, -30.0, 155.5, -156.45, -0.08, 400.0, -400.0,


440.0, -463.98, -10.0, -700.0, 735.0,
-14.83, -150.0, 0.08, -9.5, -2.34, -62.82, -500.0,
735.0, -300.0, -500.0, 490.0, -170.6,

-10.0, -300.0, -0.11, 735.0, 300.0, -239.5]:
Book.objects.create(
isbn='1', name='sum_zero',
pages=1, rating=4.0, price=price, contact=self.a8,
publisher=self.p3,
pubdate=datetime.date(1995, 1, 15))
price =
Book.objects.filter(name='sum_zero').aggregate(Sum('price'))['price__sum']
self.assertEqual(0.0, price)
}}}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28797#comment:4>

Django

unread,
Nov 20, 2017, 2:47:08 PM11/20/17
to django-...@googlegroups.com
#28797: Sum aggregation might return "-0.00" instead of "0.00"
-------------------------------------+-------------------------------------
Reporter: Simon Hanna | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: needsinfo
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 Tim Graham):

* status: new => closed
* resolution: => needsinfo


--
Ticket URL: <https://code.djangoproject.com/ticket/28797#comment:5>

Reply all
Reply to author
Forward
0 new messages