This is an automated email from the ASF dual-hosted git repository.

LucaCappelletti94 pushed a commit to branch word-display-quote-escaping
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit d5bd027f2956e900238ce553d7d6434388f3dc31
Author: LucaCappelletti94 <[email protected]>
AuthorDate: Tue Sep 22 11:51:09 2026 +0200

    Escape quotes when displaying Word tokens
---
 src/ast/mod.rs               | 26 +++++++++++++++++---------
 src/tokenizer.rs             | 39 ++++++++++++++++++++++++++++++++-------
 tests/sqlparser_snowflake.rs |  7 +++++++
 3 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index dcfd1a96..7ad9f7c0 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -378,17 +378,25 @@ impl From<&str> for Ident {
     }
 }
 
+pub(crate) fn fmt_ident(
+    f: &mut fmt::Formatter,
+    value: &str,
+    quote_style: Option<char>,
+) -> fmt::Result {
+    match quote_style {
+        Some(q) if q == '"' || q == '\'' || q == '`' => {
+            let escaped = value::escape_quoted_string(value, q);
+            write!(f, "{q}{escaped}{q}")
+        }
+        Some('[') => write!(f, "[{value}]"),
+        None => f.write_str(value),
+        _ => panic!("unexpected quote style"),
+    }
+}
+
 impl fmt::Display for Ident {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match self.quote_style {
-            Some(q) if q == '"' || q == '\'' || q == '`' => {
-                let escaped = value::escape_quoted_string(&self.value, q);
-                write!(f, "{q}{escaped}{q}")
-            }
-            Some('[') => write!(f, "[{}]", self.value),
-            None => f.write_str(&self.value),
-            _ => panic!("unexpected quote style"),
-        }
+        fmt_ident(f, &self.value, self.quote_style)
     }
 }
 
diff --git a/src/tokenizer.rs b/src/tokenizer.rs
index 36a3abf0..e17433e5 100644
--- a/src/tokenizer.rs
+++ b/src/tokenizer.rs
@@ -470,13 +470,7 @@ pub struct Word {
 
 impl fmt::Display for Word {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match self.quote_style {
-            Some(s) if s == '"' || s == '[' || s == '`' => {
-                write!(f, "{}{}{}", s, self.value, Word::matching_end_quote(s))
-            }
-            None => f.write_str(&self.value),
-            _ => panic!("Unexpected quote_style!"),
-        }
+        crate::ast::fmt_ident(f, &self.value, self.quote_style)
     }
 }
 
@@ -4552,4 +4546,35 @@ mod tests {
             ],
         );
     }
+
+    #[test]
+    fn test_word_display_quote_escaping() {
+        assert_eq!(
+            Word {
+                value: "a\"b".to_string(),
+                quote_style: Some('"'),
+                keyword: Keyword::NoKeyword,
+            }
+            .to_string(),
+            "\"a\"\"b\""
+        );
+        assert_eq!(
+            Word {
+                value: "a`b".to_string(),
+                quote_style: Some('`'),
+                keyword: Keyword::NoKeyword,
+            }
+            .to_string(),
+            "`a``b`"
+        );
+        assert_eq!(
+            Word {
+                value: "a b".to_string(),
+                quote_style: Some('['),
+                keyword: Keyword::NoKeyword,
+            }
+            .to_string(),
+            "[a b]"
+        );
+    }
 }
diff --git a/tests/sqlparser_snowflake.rs b/tests/sqlparser_snowflake.rs
index 059560dc..2b16ffe5 100644
--- a/tests/sqlparser_snowflake.rs
+++ b/tests/sqlparser_snowflake.rs
@@ -4912,3 +4912,10 @@ fn test_select_dollar_column_from_stage() {
     // With table function args, without alias
     snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format => 
'myformat')");
 }
+
+#[test]
+fn test_snowflake_stage_name_with_escaped_quotes() {
+    snowflake().verified_stmt("REMOVE @````");
+    snowflake().one_statement_parses_to("RM @````", "REMOVE @````");
+    snowflake().verified_stmt(r#"REMOVE @"stage""name""#);
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to