fix(bigquery): escape all single quotes in literal_string (r-605)#2791
Merged
Conversation
BigQuerySqlDialect.literal_string wraps string values in triple quotes
('''...''') but only escaped exact ''' runs. That left a bare ''' inside
the content whenever a value ends in a single quote (its trailing quote
merges with the closing ''' -> '''') or contains a run of 4/5/7/8 single
quotes. The stray ''' prematurely closes the string, so BigQuery sees two
adjacent literals and rejects the statement with "Syntax error:
concatenated string literals must be separated by whitespace or comments".
This surfaced on the AST-fallback check-results INSERT: the failed_rows_query
value embeds the scan-id as its own literal ('''<scan_id>'''), and the
shipped escaping doubled those quotes into a six-quote run.
Escape the backslash first, then EVERY single quote as \', keeping the
triple-quote wrapper so multi-line check SQL still needs no newline
escaping. Escaping every quote guarantees no bare ''' can appear in the
content, for all inputs (verified with a round-trip decode across boundary
quotes, runs 1-8, multiline and backslashes).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
m1n0
approved these changes
Jul 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
A customer's BigQuery diagnostics-warehouse INSERT into the check-results table failed during the AST-fallback bulk-insert path with:
Root cause
BigQuerySqlDialect.literal_stringwraps string values in triple quotes ('''...''') but only escaped exact'''runs. That leaves a bare'''inside the content whenever a value:'''→'''', orlen % 3 ∈ {1,2}(4, 5, 7, 8 …).The stray
'''prematurely closes the string, so BigQuery sees two adjacent string literals and rejects the statement.The customer hit it on the check-results INSERT:
failed_rows_queryembeds the scan-id as its own literal ('''<scan_id>'''), and the shipped escaping ('''+ quote-doubling) turned that into a six-quote run (''''''<scan_id>'''''').Fix
Escape the backslash first, then every single quote as
\', keeping the triple-quote wrapper so multi-line check SQL still needs no newline escaping. Because every'is escaped, no bare'''can appear inside the content — safe for all inputs.Tests
Added parametrised unit tests in
soda-bigquery/tests/unit/test_bigquery_dialect.pywith a BigQuery triple-quote tokeniser that asserts the rendered literal closes exactly at its end and round-trips: boundary quotes, quote-runs 1–8, multiline, backslashes, and the scan-id-embedded query.Validated end-to-end on live BigQuery (see the companion soda-extensions PR): the AST-fallback check-results INSERT now executes cleanly; with the fix reverted the same test fails with the reported error.
🤖 Generated with Claude Code