Skip to content
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

How to use query parameters with latest sqlalchemy dialect? #426

Open
noctuid opened this issue Aug 21, 2024 · 3 comments
Open

How to use query parameters with latest sqlalchemy dialect? #426

noctuid opened this issue Aug 21, 2024 · 3 comments

Comments

@noctuid
Copy link

noctuid commented Aug 21, 2024

This may be a dumb question, but it's not exactly clear how to do query parameters specifically with sqlalchemy 2 from docs/parameters.md. For the old sqlalchemy dialect on databricks-sql-connector 2.7.0, something like conn.execute(sqlalchemy.text("CREATE TABLE foo (col1 STRING COMMENT :comment1, ...) ..."), {"comment1": "foo", ...}) works fine. With sqlalchemy 2 and databricks-sql-connector 3.3.0, this gives an error like [PARSE_SYNTAX_ERROR] Syntax error at or near ':'. I tried alternate syntaxes, text(...).bindparams, etc. but wasn't able to get it working.

@kravets-levko
Copy link
Collaborator

Hi @noctuid! Since v3.0.0 databricks-sql-connector switched from using inline parameters (when library substitutes values and then sends SQL for execution) to native parameters (when SQL and parameters are sent separately, and server then does its magic). Native parameters are safer and allow server to do more optimizations, therefore they are enabled by default in databricks-sql-connector and SQLAlchemy dialect. It is still possible to use old approach in databricks-sql-connector itself, but not in SQLAlchemy dialect.

I need to check why native parameters are not recognized in your case. If this is blocker for you - the only workaround I can suggest for now is to rollback to databricks-sql-connector v2 (which uses old parameters approach)

@noctuid
Copy link
Author

noctuid commented Aug 22, 2024

For now we will wait to upgrade. Do you have an example of a code using parameters with sqlalchemy? Did mine look fine? I also tried without sqlalchemy.text and with exec_driver_sql but got the same error.

@susodapop
Copy link
Contributor

Hey @noctuid quite strange that this isn't working for you. The :named parameter approach is the default in databricks-sql-connector==3.0.0 and above. Can you share a minimal reproduction? It almost seems like you're getting that error because you're using :named style with the older dialect in connector version 2.x

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

3 participants