#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 Mariusz Felisiak, 11 months ago

Summary: RIGHT function on Oracle returns improper value when the length is zeroRight() function on Oracle and SQLite returns improper value when the length is zero.
Triage Stage: UnreviewedAccepted

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

    diff --git a/django/db/models/functions/text.py b/django/db/models/functions/text.py
    index fba2840c4b..f717213636 100644
    a b class Right(Left):  
    276276
    277277    def get_substr(self):
    278278        return Substr(
    279             self.source_expressions[0], self.source_expressions[1] * Value(-1)
     279            self.source_expressions[0], self.source_expressions[1] * Value(-1), self.source_expressions[1],
    280280        )
    281281
    282282

Would you like to prepare a patch? (a regression test is required.)

comment:2 by Kacper Wolkiewicz, 11 months ago

Owner: changed from nobody to Kacper Wolkiewicz
Status: newassigned

Sure, I can take it. To which version should I make a patch?

By the way greeting from MGA :)

comment:3 by Kacper Wolkiewicz, 11 months ago

Has patch: set
Last edited 11 months ago by Bhuvnesh (previous) (diff)

comment:4 by Mariusz Felisiak, 11 months ago

Triage Stage: AcceptedReady for checkin

comment:5 by GitHub <noreply@…>, 11 months ago

Resolution: fixed
Status: assignedclosed

In 91be6e1:

Fixed #34606 -- Fixed Right() function with zero length on Oracle and SQLite.

Note: See TracTickets for help on using tickets.
Back to Top