Skip to content

BigQuery dialect should escape backslashes in string literals#5094

Open
Samin061 wants to merge 1 commit into
apache:mainfrom
Samin061:bigquery-backslash-escape
Open

BigQuery dialect should escape backslashes in string literals#5094
Samin061 wants to merge 1 commit into
apache:mainfrom
Samin061:bigquery-backslash-escape

Conversation

@Samin061

Copy link
Copy Markdown

Jira Link

No Jira filed yet; happy to open one if preferred. This is a small escaping fix in the SQL generator.

Changes Proposed

BigQuerySqlDialect declares its escaped quote as \', but the base quoteStringLiteral only replaces the quote character, so a literal backslash in a string value reaches BigQuery unescaped. BigQuery treats backslash as an escape character inside string literals, so a pushed-down value such as x\ or \'; ... closes the literal early and the trailing characters are parsed as SQL rather than data. Override quoteStringLiteral to double backslashes before the base method escapes the enclosing quote, so backslash-containing values round-trip as data. Added a rel2sql regression test next to testCharLiteralForBigQuery.

@sonarqubecloud

Copy link
Copy Markdown

.withBigQuery().ok(expectedBigQuery);
}

/** Tests that a backslash in a character literal is escaped for BigQuery,

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.

Please refer to other PRs: first create a relevant Jira ticket describing the background of the issue, and then update the commit message and the PR details accordingly.

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.

@xuzifu666 I think the policy is that very small fixes don't require a JIRA issue

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Per @mihaibudiu's note I'll skip the JIRA since this is a small escaping fix. Happy to file one and update the commit message if you'd rather have it tracked.

+ "FROM \"foodmart\".\"product\"";
final String expectedBigQuery = "SELECT 'a\\\\b'\n"
+ "FROM foodmart.product";
sql(query)

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.

I assume you have validated the resulting query using BigQuery?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah. BigQuery treats backslash as the escape character in string literals, so an unescaped \b reads as a backspace and a trailing \ closes the literal early. Doubling the backslash before the base method escapes the enclosing quote makes the value round-trip as data. I checked the generated output against BigQuery's documented string-literal escaping rules; the rel2sql tests here are string comparisons so it isn't run against a live instance, but I can capture it against a real dataset if you'd like that on record.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants