Description
Steps to reproduce
- Create a table like this:
CREATE TABLE `test` (
`test` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- Try to insert a very large float number:
import MySQLdb
db = MySQLdb.connect(user='test',passwd='test', db='test')
c = db.cursor()
c.execute('show tables')
c.execute('insert into `test` (`test`) VALUES (%s)', [2e2400])
Expected behavior
Since MySQL can't represent infinity and this could result in unexpected data issues (imagine somebody had inf
or nan
columns), I think it should raise a more descriptive error message saying that infinity is not a valid value.
Actual behavior:
Python exception:
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-9-2f445a32ed23> in <module>()
3 c = db.cursor()
4 c.execute('show tables')
----> 5 c.execute('insert into `test` (`test`) VALUES (%s)', [2e2400])
.../.venv/lib/python2.7/site-packages/MySQLdb/cursors.pyc in execute(self, query, args)
248 except Exception:
249 exc, value = sys.exc_info()[:2]
--> 250 self.errorhandler(self, exc, value)
251 self._executed = query
252 if not self._defer_warnings:
.../.venv/lib/python2.7/site-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
48 del connection
49 if isinstance(errorvalue, BaseException):
---> 50 raise errorvalue
51 if errorclass is not None:
52 raise errorclass(errorvalue)
OperationalError: (1054, "Unknown column 'inf' in 'field list'")
Setting a breakpoint reveals that it's trying to run this query:
'insert into
test
(test
) VALUES (inf)'
which is not valid, since MySQL doesn't know about inf
.
What's going on?
Float2Str
receives inf
(huge float) as value and tries to interpolate it using
'%.15g' % o
which returns literally inf
.
Not sure if that's something you'd like to fix here, but since it's producing an invalid MySQL query, I'd say it's a bug in this library.
How do you feel about checking for inf
/nan
and raising some kind of Exception?
Happy to submit a PR if we can agree on a good solution. :)