databricks-sql-python icon indicating copy to clipboard operation
databricks-sql-python copied to clipboard

How to use query parameters with latest sqlalchemy dialect?

Open noctuid opened this issue 1 year ago • 3 comments

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.

noctuid avatar Aug 21 '24 20:08 noctuid

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)

kravets-levko avatar Aug 22 '24 10:08 kravets-levko

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.

noctuid avatar Aug 22 '24 15:08 noctuid

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

susodapop avatar Sep 21 '24 00:09 susodapop

Closing this due to inactivity. Please reopen if you still need help. Thanks!

deeksha-db avatar Feb 27 '25 06:02 deeksha-db