-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
read_sql chokes on mysql when using labels with queries due to unnecessary quoting #7826
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
Comments
you need to use I am not sure this is a bug in the heurestic or user error (in that you need to explicity call a different function here). |
I call it a bug (but feel free to disagree), and here's the one liner to fix it: was if pandas_sql.has_table(sql):
pandas_sql.meta.reflect(only=[sql])
return pandas_sql.read_table(
sql, index_col=index_col, coerce_float=coerce_float,
parse_dates=parse_dates, columns=columns) Works with: if pandas_sql.get_table(sql) is not None:
pandas_sql.meta.reflect(only=[sql])
return pandas_sql.read_table(
sql, index_col=index_col, coerce_float=coerce_float,
parse_dates=parse_dates, columns=columns) Note that the docs just say that the first argument is a query (maybe I'm missing something, but what else would it be other than a |
ok, seems reasonable. want to do a pull-request (with a tests) for this? |
Will try to. I suck at git, and I have to wait 'til I have access to my linux box so I can build pandas, but I'll do my best :) In terms of tests, what do we need? I don't have access to all the different database types that |
I think u can make a reasonable generic one yes? this should be tested on all engines |
OK - we'll see what I come up with :) |
@maxgrenderjones Thanks for the report. However, I can't reproduce this.
I am running this with sqlalchemy 9.6, MySQL 5.6 and pymysql 0.6.1. Would it be possible it has to do something wih MariaDB or your python driver? And for the docs, if you look at the current one: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html, the text should already be updated to reflect the current situation. |
And BTW, it seems to me like a bug in |
Thanks @jorisvandenbossche, looks like there's more to this one than would appear at first glance Before I begin, my versions:
My test code reload(pandas)
reload(pandas.io)
reload(pandas.io.sql)
from IPython.display import display
for engine in [model.getOurSQLEngine(), model.getMySQLEngine(), model.getPyMySQLEngine()]:
print(engine.dialect.driver)
metadata=MetaData(bind=engine)
metadata.reflect()
df = pandas.DataFrame({'a': range(3), 'b':list('abc')})
testtable='test'
df.to_sql(testtable, engine, index=False, if_exists='replace')
display(pandas.io.sql.read_sql(testtable, engine))
try:
display(pandas.read_sql(testtable, engine))
except Exception as e:
print(e)
display(pandas.io.sql.read_sql('SELECT %s.a FROM %s LIMIT 1' % (testtable, testtable), engine))
display(pandas.io.sql.read_sql('SELECT %s.a AS aid FROM %s LIMIT 1' % (testtable, testtable), engine)) Output:
So note that Now run the same code, changing the for loop to: for engine in [model.getOurSQLEngine(), model.getMySQLEngine(), model.getPyMySQLEngine()]:
print(engine.dialect.driver)
metadata=MetaData(bind=engine)
metadata.reflect()
df = pandas.DataFrame({'a': range(3), 'b':list('abc')})
testtable='tablewithaverylongname'
df.to_sql(testtable, engine, index=False, if_exists='replace')
display(pandas.io.sql.read_sql(testtable, engine))
try:
display(pandas.read_sql(testtable, engine))
except Exception as e:
print(e)
try:
display(pandas.io.sql.read_sql('SELECT %s.a FROM %s LIMIT 1' % (testtable, testtable), engine))
display(pandas.io.sql.read_sql('SELECT %s.a AS aid FROM %s LIMIT 1' % (testtable, testtable), engine))
except Exception as e:
print(e) and our issue comes back again
P.S. fwiw, |
Hmm, very strange:
Are you sure you are picking the correct pandas version? ( |
@maxgrenderjones See #7961. Can you check if this solves the issue? (difficult to check for me, as I couldn't reproduce it, but this fix should least prevent an error when |
Will do |
@maxgrenderjones Have you been able to test it? |
Fixes the issue. (Note that my previous |
@maxgrenderjones Thanks for testing! |
Not sure if this is a pandas bug or an upstream one, but here's an example of the bug (pandas-0.14.1, mariadb 10, sqlalchemy-0.9.4)
The error you get back is:
So it never gets as far as running the actual query, because it's tried to run a
DESCRIBE
query with ``` quotes which fails. i.e.But
Ok. So looking at the stacktrace, I reckon this is a pandas bug as it seems to be calling
has_table
on my sql query, which doesn't seem to make any sense?The text was updated successfully, but these errors were encountered: