Skip to content

OperationalError: (1054, "Unknown column 'inf' in 'field list'") #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
teeberg opened this issue Jun 1, 2018 · 1 comment
Closed

OperationalError: (1054, "Unknown column 'inf' in 'field list'") #246

teeberg opened this issue Jun 1, 2018 · 1 comment

Comments

@teeberg
Copy link

teeberg commented Jun 1, 2018

Steps to reproduce

  1. Create a table like this:
CREATE TABLE `test` (
  `test` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. 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. :)

@methane
Copy link
Member

methane commented Jun 4, 2018

I'm OK to raise DataError for inf and nan.
Please submit same PR to PyMySQL to keep both project shares same code as possible.

methane added a commit that referenced this issue Dec 14, 2018
* Raise ProgrammingError when inf or nan is passed
  Fixes #246
* Rename _mysql_exceptions -> _exceptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants