Opened 15 months ago

Closed 15 months ago

Last modified 15 months ago

#34863 closed Bug (wontfix)

sqlite math functions don't accept character fields as input

Reported by: Zachary Croker Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: sqlite, math
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Zachary Croker)

We have a view that performs some trig calculations. It works when testing in MySQL, but fails when using sqlite.

I have overridden the "RADIANS" function implementation from django in our conftest.py file. Details not that important, but essentially is this:

connection.create_function("RADIANS", 1, lambda x: math.radians(float(x)) if x is not None else None)

The issue is, there is a lat/lon field in our database, which is stored as a CharField (we can debate that decision later...), so when it gets passed into the _sqlite_radians function the test fails as it is invalid input. I think that these functions should be able to take in string values.

I have started a PR but thought before I submit it I should see if it is a 'genuine' bug or a decision not to support this field.

Change History (4)

comment:1 by Zachary Croker, 15 months ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 15 months ago

Resolution: invalid
Status: newclosed

Thanks for this ticket, however radians should not accept strings. You can cast values before passing them to this function.

in reply to:  2 ; comment:3 by Zachary Croker, 15 months ago

Replying to Mariusz Felisiak:

Thanks for this ticket, however radians should not accept strings. You can cast values before passing them to this function.

Hi Mariusz, the sqlite docs specify that these functions should accept strings (see https://www.sqlite.org/lang_mathfunc.html)

"The arguments to math functions can be integers, floating-point numbers, or strings or blobs that look like integers or real numbers. If any argument is NULL or is a string or blob that is not readily converted into a number, then the function will return NULL."

Also - in this instance I am unable to cast the values because they are calculated wholly within the view (which I can't change)

in reply to:  3 comment:4 by Mariusz Felisiak, 15 months ago

Resolution: invalidwontfix

Replying to Zachary Croker:

Hi Mariusz, the sqlite docs specify that these functions should accept strings (see https://www.sqlite.org/lang_mathfunc.html)

We're trying to unify behavior for all backends. Accepting strings on SQLite may be confusing.

Please follow the triaging guidelines with regards to wontfix tickets and take this to DevelopersMailingList or the Django Forum, if you don't agree.

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