This is an automated email from the ASF dual-hosted git repository.
mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new ca9f9cff37 BigQuery dialect should escape backslashes in string
literals
ca9f9cff37 is described below
commit ca9f9cff37f773ab7bce9c261702ed054bb499b9
Author: bibi samina <[email protected]>
AuthorDate: Sat Jul 11 11:12:17 2026 +0530
BigQuery dialect should escape backslashes in string literals
---
.../org/apache/calcite/sql/dialect/BigQuerySqlDialect.java | 10 ++++++++++
.../apache/calcite/rel/rel2sql/RelToSqlConverterTest.java | 14 ++++++++++++++
2 files changed, 24 insertions(+)
diff --git
a/core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java
b/core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java
index 6e5ebeb4c0..6683054819 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java
@@ -117,6 +117,16 @@ public BigQuerySqlDialect(SqlDialect.Context context) {
|| RESERVED_KEYWORDS.contains(val.toUpperCase(Locale.ROOT));
}
+ @Override public void quoteStringLiteral(StringBuilder buf,
+ @Nullable String charsetName, String val) {
+ // BigQuery treats backslash as an escape character inside string literals,
+ // so a literal backslash must be doubled before the base method escapes
the
+ // enclosing quote as \'. Otherwise a value containing a backslash (e.g.
+ // "x\" or "\'; ...") terminates the literal early and the trailing text is
+ // parsed as SQL rather than data.
+ super.quoteStringLiteral(buf, charsetName, val.replace("\\", "\\\\"));
+ }
+
@Override public boolean supportsImplicitTypeCoercion(RexCall call) {
return super.supportsImplicitTypeCoercion(call)
&& RexUtil.isLiteral(call.getOperands().get(0), false)
diff --git
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 95bbe98f6a..3327d0f368 100644
---
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -3905,6 +3905,20 @@ private SqlDialect nonOrdinalDialect() {
.withBigQuery().ok(expectedBigQuery);
}
+ /** Tests that a backslash in a character literal is escaped for BigQuery,
+ * which uses backslash as the escape character. Without doubling the
+ * backslash the generated literal is terminated early. */
+ @Test void testCharLiteralWithBackslashForBigQuery() {
+ final String query = "select 'a\\b' from \"product\"";
+ final String expectedPostgresql = "SELECT 'a\\b'\n"
+ + "FROM \"foodmart\".\"product\"";
+ final String expectedBigQuery = "SELECT 'a\\\\b'\n"
+ + "FROM foodmart.product";
+ sql(query)
+ .withPostgresql().ok(expectedPostgresql)
+ .withBigQuery().ok(expectedBigQuery);
+ }
+
@Test void testIdentifier() {
// Note that IGNORE is reserved in BigQuery but not in standard SQL
final String query = "select *\n"