Opened 11 years ago
Last modified 11 years ago
#22128 closed Cleanup/optimization
The Aggregation guide should warn that this feature does not work when using the sqlite backend and certain types — at Version 4
Reported by: | Rigel Di Scala | Owned by: | |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | sqlite |
Cc: | Baptiste Mispelon, Rigel Di Scala, Shai Berger | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
When using aggregation clauses like "HAVING SUM" in sqlite3, SUM is a function and therefore does not have affinity. Therefore, the correct type must be used in the statement, or it might yield incorrect results:
>>> len(list(cursor.execute("SELECT * from thing_thing WHERE thing_thing.cost > '0' GROUP BY thing_thing.cost HAVING SUM(thing_thing.cost) > 0"))) 3 >>> len(list(cursor.execute("SELECT * from thing_thing WHERE thing_thing.cost > '0' GROUP BY thing_thing.cost HAVING SUM(thing_thing.cost) > '0'"))) 0
This can be problematic when using the decimal.Decimal
type, as the it will be coerced to a string by the django.db.backends.utils.rev_typecast_decimal
adapter. For example:
>>> query = "SELECT * from thing_thing WHERE thing_thing.cost > '0' GROUP BY thing_thing.cost HAVING SUM(thing_thing.cost) > ?" >>> len(list(cursor.execute(query, (0.0,)))) 3 >>> len(list(cursor.execute(query, ('0.0',)))) 0 >>> from decimal import Decimal >>> len(list(cursor.execute(query, (Decimal('0.0'),)))) 0
Therefore, any queryset instantiated by a sqlite3 backend that uses the aggregation features described in the Django documentation Aggregation guide will most probably return incorrect results. The problem has been time consuming to debug, as the origin is not immediately clear.
The master ticket for this issue appears to be #18247. The duplicated #22117 was recently open, but it is expected that more will come.
Therefore, I recommend that a warning message be included after the first few paragraphs of the Aggregation guide, that provides an explanation of the problem.
An example message could be:
.. warning:: Aggregation is not currently supported by the Django sqlite backend when using types that coerce to strings. One example is :class:`decimal.Decimal`, as instances of this type will be converted to `str` instead of `float`, in order to preserve their arithmetic precision.
Change History (4)
comment:1 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 11 years ago
comment:3 by , 11 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:4 by , 11 years ago
Description: | modified (diff) |
---|
Sorry, I made a mistake in the description. The master ticket is #18247, and not the unrelated #21179.