Skip to content

Add support for converting pyformat SQL to positional markers with bulk_parameters#812

Open
bgunebakan wants to merge 1 commit into
mainfrom
fix/bulk-parameters-pyformat-conversion
Open

Add support for converting pyformat SQL to positional markers with bulk_parameters#812
bgunebakan wants to merge 1 commit into
mainfrom
fix/bulk-parameters-pyformat-conversion

Conversation

@bgunebakan

@bgunebakan bgunebakan commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary of the changes / Why this is an improvement

Fixes a silent runtime error when cursor.execute() is called with bulk_parameters and a SQL statement that uses %(name)s (pyformat) placeholders.

When bulk_parameters is passed alongside pyformat SQL, the driver was sending the raw %(name)s markers verbatim to CrateDB, which rejected them with:

SQLParseException[no viable alternative at input 'VALUES (%']

The fix adds an elif branch in execute() that rewrites the SQL template to $N positional markers before the request is sent.

The bug was introduced indirectly by PR #810 which declared paramstyle = "pyformat" for the driver. Before that change, SQLAlchemy generated ? style SQL, so the bulk_parameters path was never fed pyformat placeholders so the broken code path simply didn't exist.

It fixes implementation in sqlalchemy-crate package crate/sqlalchemy-cratedb#266

Checklist

  • Link to issue this PR refers to (if applicable): Fixes #???
  • Unit tests added

@bgunebakan bgunebakan self-assigned this Jun 10, 2026
Comment on lines +146 to +147
elif bulk_parameters is not None and _NAMED_PARAM_RE.search(sql):
sql = _rewrite_pyformat_sql(sql)

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.

Wouldn't the bulk_parameters also need some sort of conversion and be in dict format for this to work reliably?

Or does pyformat imply that the bulk parameters order matches the order of named placeholders? If so - is that documented somewhere?

Do other clients allow this?

@bgunebakan bgunebakan added the bug label Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants