Skip to content

Commit 7c55c48

Browse files
authored
Fix: prevent redundant backslash escapes in rawstring generator (#5040)
1 parent d10fdf5 commit 7c55c48

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

sqlglot/generator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,11 @@ def unicodestring_sql(self, expression: exp.UnicodeString) -> str:
13491349
return f"{left_quote}{this}{right_quote}{escape_sql}"
13501350

13511351
def rawstring_sql(self, expression: exp.RawString) -> str:
1352-
string = self.escape_str(expression.this.replace("\\", "\\\\"), escape_backslash=False)
1352+
string = expression.this
1353+
if "\\" in self.dialect.tokenizer_class.STRING_ESCAPES:
1354+
string = string.replace("\\", "\\\\")
1355+
1356+
string = self.escape_str(string, escape_backslash=False)
13531357
return f"{self.dialect.QUOTE_START}{string}{self.dialect.QUOTE_END}"
13541358

13551359
def datatypeparam_sql(self, expression: exp.DataTypeParam) -> str:

tests/dialects/test_bigquery.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def test_bigquery(self):
236236
self.validate_identity(
237237
"CREATE OR REPLACE VIEW test (tenant_id OPTIONS (description='Test description on table creation')) AS SELECT 1 AS tenant_id, 1 AS customer_id",
238238
)
239+
self.validate_identity(
240+
'SELECT r"\\t"',
241+
"SELECT '\\\\t'",
242+
)
239243
self.validate_identity(
240244
"ARRAY(SELECT AS STRUCT e.x AS y, e.z AS bla FROM UNNEST(bob))::ARRAY<STRUCT<y STRING, bro NUMERIC>>",
241245
"CAST(ARRAY(SELECT AS STRUCT e.x AS y, e.z AS bla FROM UNNEST(bob)) AS ARRAY<STRUCT<y STRING, bro NUMERIC>>)",
@@ -1023,8 +1027,8 @@ def test_bigquery(self):
10231027
r'r"""/\*.*\*/"""',
10241028
write={
10251029
"bigquery": r"'/\\*.*\\*/'",
1026-
"duckdb": r"'/\\*.*\\*/'",
1027-
"presto": r"'/\\*.*\\*/'",
1030+
"duckdb": r"'/\*.*\*/'",
1031+
"presto": r"'/\*.*\*/'",
10281032
"hive": r"'/\\*.*\\*/'",
10291033
"spark": r"'/\\*.*\\*/'",
10301034
},
@@ -1033,8 +1037,8 @@ def test_bigquery(self):
10331037
r'R"""/\*.*\*/"""',
10341038
write={
10351039
"bigquery": r"'/\\*.*\\*/'",
1036-
"duckdb": r"'/\\*.*\\*/'",
1037-
"presto": r"'/\\*.*\\*/'",
1040+
"duckdb": r"'/\*.*\*/'",
1041+
"presto": r"'/\*.*\*/'",
10381042
"hive": r"'/\\*.*\\*/'",
10391043
"spark": r"'/\\*.*\\*/'",
10401044
},

0 commit comments

Comments
 (0)