Skip to content

fix(spanner): eliminate silent SQL truncation in DB-API - #19046

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-eliminate-silent-truncation
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-eliminate-silent-truncation

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

When SQL queries executed through the DB-API driver contained non-ASCII characters (such as accented characters in table names, string values, or comments), an internal routine attempted to automatically wrap those words in backticks. Due to a bug in that routine, any SQL text following the non-ASCII character was silently discarded.

For targeted UPDATE and DELETE statements (such as deleting a single row by ID), stripping the trailing WHERE clause caused the driver to detect an unqualified statement and automatically append "WHERE 1=1", turning a scoped single-row modification into an accidental full-table wipe. Additionally, string literals containing Unicode characters were erroneously converted into column identifiers, and adjacent punctuation was corrupted.

This change removes the legacy auto-backticking routine entirely. The database driver now treats incoming SQL statements as transparent text rather than attempting to guess and quote identifiers using regular expressions. Frameworks like SQLAlchemy and Django already quote identifiers safely at the query compiler level. Query preprocessing in the driver is now limited strictly to translating parameter placeholders and unescaping literal percent signs. Comprehensive regression tests have been added to ensure queries containing Unicode table names, string literals, and comments preserve their complete WHERE predicates.

When SQL queries executed through the DB-API driver contained non-ASCII characters (such as accented characters in table names, string values, or comments), an internal routine attempted to automatically wrap those words in backticks. Due to a bug in that routine, any SQL text following the non-ASCII character was silently discarded.

For targeted UPDATE and DELETE statements (such as deleting a single row by ID), stripping the trailing WHERE clause caused the driver to detect an unqualified statement and automatically append "WHERE 1=1", turning a scoped single-row modification into an accidental full-table wipe. Additionally, string literals containing Unicode characters were erroneously converted into column identifiers, and adjacent punctuation was corrupted.

This change removes the legacy auto-backticking routine entirely. The database driver now treats incoming SQL statements as transparent text rather than attempting to guess and quote identifiers using regular expressions. Frameworks like SQLAlchemy and Django already quote identifiers safely at the query compiler level. Query preprocessing in the driver is now limited strictly to translating parameter placeholders and unescaping literal percent signs. Comprehensive regression tests have been added to ensure queries containing Unicode table names, string literals, and comments preserve their complete WHERE predicates.
@olavloite
olavloite requested a review from a team as a code owner November 11, 2026 17:22

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

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.

Code Review

This pull request removes the sanitize_literals_for_upload and backtick_unicode helper functions, replacing their usage with a simple sql.replace("%%", "%") to prevent the unintended backticking or modification of Unicode characters in SQL identifiers, string literals, and predicates. It also adds comprehensive unit tests to ensure Unicode is correctly preserved across various SQL statements. I have no feedback to provide as there are no review comments.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant