modin icon indicating copy to clipboard operation
modin copied to clipboard

Incorrect syntax error on read_sql

Open zacqed opened this issue 6 years ago • 25 comments

I am getting the following error in case of msssql.

Connection query details:

params1 = urllib.parse.quote_plus("driver={ODBC Driver 17 for SQL Server};server=server;database=Kepler;Uid=uid;Pwd=password;Encrypt=yes;")
engine2 = 'mssql+pyodbc:///?odbc_connect={}'.format(params1)

Error:

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ')'. (102) (SQLExecDirectW)")
[SQL: SELECT COUNT(*) FROM (select * from cashflow_data as foo)]

Minimal code is as follows:

import modin.experimental.pandas as pd
query = f'''select * from cashflow_data'''
file_data = pd.read_sql(sql = query, con=engine2,max_sessions = 10)
y = datetime.datetime.now() - x

Dataset example:

instu_id cashflow_amount currency_code cf_type data_date import_source entity maturity_date
6.03675E+11 7500 KWD P 30-Sep-19 MANUAL RFOL 1-May-20
6.03468E+11 635.143 KWD P 30-Sep-19 MANUAL RFOL 30-Sep-19
6.03474E+11 20000 KWD P 30-Sep-19 MANUAL RFOL 31-Oct-19
6.03E+11 5149 KWD P 30-Sep-19 MANUAL RFOL 18-Feb-20
6.03511E+11 16635 KWD P 30-Sep-19 MANUAL RFOL 30-Oct-19

@devin-petersohn : Any way to fix it

zacqed avatar Jan 09 '20 07:01 zacqed

Hi @Harshitg, thanks for posting! This looks like something we're going to have to fix internally.

It will take me some time to set up a SQLServer environment to test this locally. I have a suspicion that the way we are aliasing our subqueries for postgres does not translate, so it's going to take some time to find a way to detect the type of database and fix the issue. I will make sure it is out for the next release.

devin-petersohn avatar Jan 10 '20 01:01 devin-petersohn

SQL SERVER requires aliasing the inner query. For instance, the following should work

SELECT COUNT(*) FROM (select * from cashflow_data) as foo

I believe this was the intent from the start, but the alias snuck into the inner query. or am I missing something bigger here?

eavidan avatar Jan 13 '20 17:01 eavidan

@eavidan I believe you are correct. I wanted to set up a testing environment to verify this. Postgres also has this requirement, so we need to make sure that the fix doesn't break the postgres functionality.

devin-petersohn avatar Jan 13 '20 17:01 devin-petersohn

Thinking about it, Microsoft SQL does not support LIMIT and OFFSET as well. So this means that in order to support Microsoft SQL we would probably need to generalize the way we construct our queries better.

eavidan avatar Jan 14 '20 16:01 eavidan

@devin-petersohn After diving into SQL Alchemy and pandas implementation, I believe we would need to created different query templates for different SQL engines. we can add this to sql_reader.py. I am not sure there is a simple way to implement the distribution of the query in a unified way across all SQL engines. does this make sense? any other directions you had in mind?

eavidan avatar Jan 15 '20 10:01 eavidan

Thanks @eavidan that makes sense, I think that's the best approach. As a part of this, we will need to add the different SQL engines to the CI. There's going to be a little setup required to do that.

devin-petersohn avatar Jan 15 '20 21:01 devin-petersohn

Since this is a slightly larger rewrite, I'm pushing to the next release, which should be in 3-4 weeks, possibly sooner.

devin-petersohn avatar Jan 21 '20 19:01 devin-petersohn

@devin-petersohn sounds reasonable. I have tried working with SQL Alchemy to create a query generator that would wrap the given sql query, while being agnostic to the SQL engine. Unfortunately, I was not able to produce a complete solution. Going back to the last approach, do we want to simply check the connection string or engine dialect and choose a query template accordingly. something like this?

row_cnt_template = "SELECT COUNT(*) FROM ({}) as foo"
cols_names_template = "SELECT * FROM ({0}) as foo LIMIT 0"
partition_template = "SELECT * FROM ({0}) as foo LIMIT {1} OFFSET {2}"

engine = sa.create_engine(con)
if engine.driver == "pymssql":
    partition_template = "SELECT * FROM ({0}) as foo ORDER BY(SELECT NULL) OFFSET {2} ROWS FETCH NEXT {1} ROWS ONLY"
    cols_names_template = "SELECT TOP 0 FROM ({0}) as foo"

If so, would a generator for the templates be more suited and should we start only with a Base template and a MSSQL templates?

eavidan avatar Jan 28 '20 16:01 eavidan

Yes, that should be good. Ideally this code block would go in a util of some sort outside of the implementation so it's easy to find/add new templates if necessary.

Ideally, as a part of this effort we can add better testing for the various SQL systems. I have set up a Windows test in the CI so we can add mssql to the testing environment there.

devin-petersohn avatar Jan 28 '20 17:01 devin-petersohn

I agree. started working on simple query generator as I believe it will make things more robust and clear instead of supplying templates. Still not sure about the. naming part or if utils is the right file for it. https://github.com/eavidan/modin/tree/issue_979/modin/engines/base/io/sql any thoughts? would you rather we open a WIP PR and conduct this discussion over there?

eavidan avatar Jan 29 '20 16:01 eavidan

@eavidan Let's do a WIP PR and have the discussion there.

devin-petersohn avatar Jan 30 '20 20:01 devin-petersohn

We have an open PR for this in progress, but the design is not finalized, pushing to next release.

devin-petersohn avatar Feb 20 '20 17:02 devin-petersohn

I'm getting a similar error with Oracle.

pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT COUNT(*) FROM (select * from FROM fusion.fnd_flex_value_sets ffvs) as foo': ORA-00933: SQL command not properly ended

Here's my Modin setup:

from distributed import Client
client = Client(n_workers=6)
# Modin will connect to the Dask Client
import modin.pandas as pd

Apparently, oracle does not support table aliases with 'as' in front, hence the SQL command error.

SudharsanRama avatar Mar 13 '20 10:03 SudharsanRama

Thanks @SudharsanRama, we are planning on getting this fix out for all databases in the next release.

As a temporary workaround, you can pass a SQLAlchemy connection to the pd.read_sql command. It will not run in parallel, but it should work.

devin-petersohn avatar Mar 16 '20 16:03 devin-petersohn

@SudharsanRama if I am not mistaken you have an error in your SQL syntax. It looks like you have a couple of FROM clauses.

select * from FROM fusion.fnd_flex_value_sets ffvs

eavidan avatar Mar 17 '20 08:03 eavidan

@SudharsanRama if I am not mistaken you have an error in your SQL syntax. It looks like you have a couple of FROM clauses.

select * from FROM fusion.fnd_flex_value_sets ffvs

Yup. Sorry for the typo. I didn't want to put the whole query in there. But you get the idea.

SudharsanRama avatar Mar 19 '20 18:03 SudharsanRama

@eavidan I tried the approach you described, but I'm getting an exception. Below is my code snippet

` import modin.experimental.pandas as pd

SQL = "SELECT * FROM my_table WHERE my_date = '20200304'"

CONN = f'mssql+pyodbc://{getenv("UID")}:{getenv("PWD")}@{getenv("SERVER")}/DB_TEST?driver=ODBC Driver 17 for SQL Server'

pd.read_sql(sql=SQL, con=CONN) sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server] [SQL Server]Incorrect syntax near 'LIMIT'. (102) (SQLExecDirectW)") [SQL: SELECT * FROM (SELECT * FROM my_table WHERE my_date = '20200304') as foo LIMIT 0] (Background on this error at: http://sqlalche.me/e/13/f405) `

thoughtnewbie avatar Mar 19 '21 02:03 thoughtnewbie

@eavidan I tried the approach you described, but I'm getting an exception. Below is my code snippet

` import modin.experimental.pandas as pd

SQL = "SELECT * FROM my_table WHERE my_date = '20200304'"

CONN = f'mssql+pyodbc://{getenv("UID")}:{getenv("PWD")}@{getenv("SERVER")}/DB_TEST?driver=ODBC Driver 17 for SQL Server'

pd.read_sql(sql=SQL, con=CONN) sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server] [SQL Server]Incorrect syntax near 'LIMIT'. (102) (SQLExecDirectW)") [SQL: SELECT * FROM (SELECT * FROM my_table WHERE my_date = '20200304') as foo LIMIT 0] (Background on this error at: http://sqlalche.me/e/13/f405) `

I am getting the same error message.

anil-kk avatar Mar 29 '21 07:03 anil-kk

I think it is possible to fix this in a database-agnostic way. Marking for the next release.

devin-petersohn avatar Mar 30 '21 14:03 devin-petersohn

Hi, I am getting a similar error when trying to read from an oracle database.

mock code: import modin.pandas as pd query = """select distinct col_a, col_b FROM schema.table""" pd.read_sql(query, conn)

error: sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-00933: SQL command not properly ended [SQL: SELECT COUNT(*) FROM (select distinct col_a, col_b FROM schema.table) AS _

abjess avatar Jul 06 '22 22:07 abjess