Skip to content

fix(bigquery): escape all single quotes in literal_string (r-605)#2791

Merged
Niels-b merged 1 commit into
mainfrom
r-605-fix-issue-with-bigquery-quoting
Jul 14, 2026
Merged

fix(bigquery): escape all single quotes in literal_string (r-605)#2791
Niels-b merged 1 commit into
mainfrom
r-605-fix-issue-with-bigquery-quoting

Conversation

@Niels-b

@Niels-b Niels-b commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Problem

A customer's BigQuery diagnostics-warehouse INSERT into the check-results table failed during the AST-fallback bulk-insert path with:

400 ... Syntax error: concatenated string literals must be separated by whitespace or comments

Root cause

BigQuerySqlDialect.literal_string wraps string values in triple quotes ('''...''') but only escaped exact ''' runs. That leaves a bare ''' inside the content whenever a value:

  • ends with 1–2 single quotes — the trailing quote merges with the closing ''''''', or
  • contains an internal run of ≥4 quotes where len % 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_query embeds 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.py with 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

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>
@sonarqubecloud

Copy link
Copy Markdown

@Niels-b
Niels-b merged commit ebd2cdd into main Jul 14, 2026
21 checks passed
@Niels-b
Niels-b deleted the r-605-fix-issue-with-bigquery-quoting branch July 14, 2026 12:48
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.

2 participants