#29500: SQLite functions crashes on NULL values
-------------------------------------+-------------------------------------
     Reporter:  Sergey Fedoseev      |                    Owner:  Srinivas
                                     |  Reddy Thatiparthy
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  2.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

 We need to be careful how we handle this to ensure that the behaviour
 mirrors other backends.

 I've checked PostgreSQL and when any one of the arguments to `POWER()`.
 `LPAD()` or `RPAD()` is `NULL` they return `NULL`.
 We should ensure that we check whether any one of the arguments is `None`
 and, if so, return `None`.

 We must not catch exceptions such as `TypeError` or `ValueError` to do
 this as has been done in the initial version of the
 [https://github.com/django/django/pull/10121 PR].
 If we were to pass a string to `_sqlite_power()` we would expect a
 `TypeError` which should blow up, not return `None`. Compare to
 PostgreSQL:
 {{{
 postgres=# select power(2, 'abc');
 ERROR:  invalid input syntax for type double precision: "abc"
 LINE 1: select power(2, 'abc');
                         ^
 }}}
 The second part of the problem here is that the sqlite backend suppresses
 the error message and returns a different exception:
 {{{
 OperationalError: user-defined function raised exception
 }}}
 Obviously this is not particularly helpful, but a quick search and I found
 the following on Stack Overflow: https://stackoverflow.com/a/45834923
 It points to the documentation for
 
[https://docs.python.org/3/library/sqlite3.html#sqlite3.enable_callback_tracebacks
 sqlite3.enable_callback_tracebacks()].

 I would recommend the following:

 1. Creation of a decorator to check for `None` passed into any of the
 arguments which returns `None` or calls the function as appropriate.
 2. Enabling callbacks on tracebacks for sqlite3 (always / when debug
 enabled / documentation change to give instruction).

 Note that the outcome of this pull request will affect
 [https://github.com/django/django/pull/9622 PR/9622] which I am reviewing,
 particular with respect to my
 [https://github.com/django/django/pull/9622#discussion_r165360894
 comment].

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.c9bbbdb14e946e7b83c543e2211fecab%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to