Opened 2 years ago
Closed 2 years ago
#34606 closed Bug (fixed)
Right() function on Oracle and SQLite returns improper value when the length is zero.
| Reported by: | Kacper Wolkiewicz | Owned by: | Kacper Wolkiewicz |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 4.0 |
| Severity: | Normal | Keywords: | oracle right substr |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Hi
I have found that the RIGHT database function on Oracle returns the whole string instead of empty string when the given length is 0. You can't explicitly give 0 to the RIGHT function, but it may be computed by the database. Basic example (you can use any model with a CharField):
from django.db.models.functions import Right, Length MyModel.objects.annotate(suffix=Right("foo", Length("foo") - Length("foo")))
On PostgreSQL this will return an empty string under the field suffix, but on Oracle this will return the whole contents of the field foo. This is because Django uses the SUBSTR function on Oracle by multiplying the given length value by -1 and giving it as a position argument. I think it is not intended behavior and it should return the empty string as PostgreSQL does. Or at least be documented as a Note in the Right function documentation.
Change History (5)
comment:1 by , 2 years ago
| Summary: | RIGHT function on Oracle returns improper value when the length is zero → Right() function on Oracle and SQLite returns improper value when the length is zero. |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 2 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Sure, I can take it. To which version should I make a patch?
By the way greeting from MGA :)
comment:4 by , 2 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Thanks for the report. SQLite is also affected. As far as I'm aware, it's enough to pass a substring length, e.g.:
django/db/models/functions/text.py
Would you like to prepare a patch? (a regression test is required.)