[Django] #34863: sqlite math functions don't accept character fields as input

23 views
Skip to first unread message

Django

unread,
Sep 22, 2023, 3:54:03 AM9/22/23
to django-...@googlegroups.com
#34863: sqlite math functions don't accept character fields as input
-------------------------------------+-------------------------------------
Reporter: zacroker | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: sqlite, math
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

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

Django

unread,
Sep 22, 2023, 3:54:41 AM9/22/23
to django-...@googlegroups.com
#34863: sqlite math functions don't accept character fields as input
-------------------------------------+-------------------------------------
Reporter: zacroker | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: sqlite, math | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by zacroker:

Old description:

> 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.

New description:

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.

--

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

Django

unread,
Sep 22, 2023, 3:57:54 AM9/22/23
to django-...@googlegroups.com
#34863: sqlite math functions don't accept character fields as input
-------------------------------------+-------------------------------------
Reporter: zacroker | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: sqlite, math | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

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

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

Django

unread,
Sep 22, 2023, 7:53:51 PM9/22/23
to django-...@googlegroups.com
#34863: sqlite math functions don't accept character fields as input
-------------------------------------+-------------------------------------
Reporter: Zachary Croker | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: sqlite, math | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Zachary Croker):

Replying to [comment:2 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)

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

Django

unread,
Sep 23, 2023, 3:11:47 AM9/23/23
to django-...@googlegroups.com
#34863: sqlite math functions don't accept character fields as input
-------------------------------------+-------------------------------------
Reporter: Zachary Croker | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: sqlite, math | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* resolution: invalid => wontfix


Comment:

Replying to [comment:3 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 [https://docs.djangoproject.com/en/stable/internals/contributing
/triaging-tickets/#closing-tickets follow the triaging guidelines with
regards to wontfix tickets] and take this to DevelopersMailingList or the
Django Forum, if you don't agree.

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

Reply all
Reply to author
Forward
0 new messages