Skip to content

fix(sqlalchemy): keep colons in SQL string literals literal - #4279

Merged
anakin87 merged 2 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/sqlalchemy-colon-literals
Nov 24, 2026
Merged

anakin87 merged 2 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/sqlalchemy-colon-literals

Conversation

@astroknot-sheep

@astroknot-sheep astroknot-sheep commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

SQLAlchemyTableRetriever passed queries and init_script statements straight to text(), which reads :name as a bind parameter even inside string literals. The component never passes parameters, so a query like ... WHERE body = 'see :ref' returned A value is required for bind parameter 'ref', and a seed statement with JSON such as '{"k":1}' made warm_up() raise.

Both call sites now use Connection.exec_driver_sql(), so the SQL goes to the driver as written and nothing is parsed for bind parameters. They pass execution_options={"no_parameters": True}, so drivers like psycopg2 also don't treat % (e.g. in LIKE 'a%') as a parameter marker.

How did you test it?

  • Added test_colons_in_string_literals_are_not_bind_parameters (seed data and query both contain colons). It fails on main and passes with this change.
  • pytest tests in integrations/sqlalchemy: 16 passed. ruff check, ruff format --check and mypy -p haystack_integrations.components.retrievers.sqlalchemy are clean.

Notes for the reviewer

I used AI assistance while investigating and drafting this PR. I reviewed every line and ran the tests myself.

Checklist

@astroknot-sheep
astroknot-sheep requested a review from a team as a code owner November 16, 2026 15:31
@astroknot-sheep
astroknot-sheep requested review from anakin87 and removed request for a team November 16, 2026 15:31
@github-actions github-actions Bot added integration:sqlalchemy type:documentation Improvements or additions to documentation labels Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report (sqlalchemy)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/sqlalchemy/src/haystack_integrations/components/retrievers/sqlalchemy
  sqlalchemy_table_retriever.py
Project Total  

This report was generated by python-coverage-comment-action

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello!

I simply recommend using exec_driver_sql() instead of text().

Replacing : might cause different issues.

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Thanks, that's cleaner. Switched both call sites to exec_driver_sql() (500e71a) and dropped the colon escaping. I also pass execution_options={"no_parameters": True}, so psycopg2/MySQL drivers don't read a % in something like LIKE 'a%' as a parameter marker, since text() used to escape that for us. Tests, ruff and mypy pass locally. Updated the description too.

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@anakin87
anakin87 merged commit 7154fcc into deepset-ai:main Nov 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:sqlalchemy type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLAlchemyTableRetriever: colons inside string literals are parsed as bind parameters

2 participants