Skip to content

fix(sqlalchemy-bigquery): update literal binds test for SQLAlchemy 2.0#17029

Merged
shuoweil merged 2 commits into
mainfrom
fix/sqlalchemy-literal-binds-16042
May 11, 2026
Merged

fix(sqlalchemy-bigquery): update literal binds test for SQLAlchemy 2.0#17029
shuoweil merged 2 commits into
mainfrom
fix/sqlalchemy-literal-binds-16042

Conversation

@chalmerlowe
Copy link
Copy Markdown
Contributor

This PR fixes the sqlalchemy-bigquery portion of Issue #16042.

Problem:
test_compiled_query_literal_binds was skipped for SQLAlchemy >= 2.0 because it was failing. The failure occurs because in SQLAlchemy 2.0, Connection.execute() expects an executable object or a string (wrapped in text()), and passing a Compiled object directly is no longer supported.

Fix:

  • Removed the skip condition for SQLAlchemy >= 2.0.
  • Modified the test to wrap the compiled query string in sqlalchemy.text() before passing it to conn.execute().

This allows the test to run and pass on SQLAlchemy 2.0.

Removes the skip for SQLAlchemy >= 2.0 on test_compiled_query_literal_binds.

Modifies the test to wrap the compiled query in sqlalchemy.text(str(compiled)) before execution, as passing a Compiled object directly to Connection.execute() is not supported in SQLAlchemy 2.0.

Fixes sqlalchemy-bigquery portion of Issue #16042.
@chalmerlowe chalmerlowe requested review from a team as code owners May 11, 2026 17:41
@chalmerlowe chalmerlowe requested review from shuoweil and removed request for a team May 11, 2026 17:41
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enables the test_compiled_query_literal_binds test for SQLAlchemy 2.0 and updates the execution logic to use sqlalchemy.text(str(compiled)). Feedback suggests that using sqlalchemy.text() may cause issues if the SQL contains colons, as SQLAlchemy might misinterpret them as bind parameters. It is recommended to use exec_driver_sql where available to ensure safer execution and better compatibility across different SQLAlchemy versions.

compiled = q.compile(engine, compile_kwargs={"literal_binds": True})
with engine.connect() as conn:
result = conn.execute(compiled).fetchall()
result = conn.execute(sqlalchemy.text(str(compiled))).fetchall()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using sqlalchemy.text() on a string generated with literal_binds=True can be problematic if the SQL contains colons (e.g., in timestamps or string literals), as SQLAlchemy will attempt to interpret them as bind parameters. A more robust approach that maintains compatibility across SQLAlchemy 1.3, 1.4, and 2.0 is to use exec_driver_sql when available, as it bypasses parameter parsing entirely and is the recommended way to execute raw SQL strings in modern SQLAlchemy.

Suggested change
result = conn.execute(sqlalchemy.text(str(compiled))).fetchall()
if hasattr(conn, "exec_driver_sql"):
result = conn.exec_driver_sql(str(compiled)).fetchall()
else:
result = conn.execute(compiled).fetchall()

)
with engine_using_test_dataset.connect() as conn:
result = conn.execute(compiled).fetchall()
result = conn.execute(sqlalchemy.text(str(compiled))).fetchall()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Same as above: using exec_driver_sql is safer for literal binds to avoid potential issues with colon parsing in SQLAlchemy 1.4 and 2.0.

Suggested change
result = conn.execute(sqlalchemy.text(str(compiled))).fetchall()
if hasattr(conn, "exec_driver_sql"):
result = conn.exec_driver_sql(str(compiled)).fetchall()
else:
result = conn.execute(compiled).fetchall()

@chalmerlowe chalmerlowe added the automerge Merge the pull request once unit tests and other checks pass. label May 11, 2026
@shuoweil shuoweil merged commit 74f7a41 into main May 11, 2026
31 checks passed
@shuoweil shuoweil deleted the fix/sqlalchemy-literal-binds-16042 branch May 11, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Merge the pull request once unit tests and other checks pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants