LucaCappelletti94 commented on code in PR #2418:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2418#discussion_r3925971877


##########
src/ast/mod.rs:
##########
@@ -385,7 +385,28 @@ impl fmt::Display for Ident {
                 let escaped = value::escape_quoted_string(&self.value, q);
                 write!(f, "{q}{escaped}{q}")
             }
-            Some('[') => write!(f, "[{}]", self.value),
+            Some('[') => {
+                let v = &self.value;
+                if v.len() >= 2 && v.starts_with('"') && v.ends_with('"') {
+                    // A nested double-quoted identifier (e.g. Redshift 
`["a]b"]`)
+                    // keeps its inner quotes in the value and its `]` is 
already
+                    // literal, so emit it unchanged.
+                    write!(f, "[{v}]")
+                } else {
+                    // Double a lone `]` so the identifier round-trips, but 
leave
+                    // an already-doubled `]]` intact: in the tokenizer's 
no-escape
+                    // mode the value still holds the raw `]]`, and 
re-doubling it
+                    // would corrupt the identifier.
+                    write!(f, "[")?;
+                    let mut rest = v.as_str();
+                    while let Some(pos) = rest.find(']') {
+                        write!(f, "{}]]", &rest[..pos])?;
+                        let after = &rest[pos + 1..];
+                        rest = after.strip_prefix(']').unwrap_or(after);

Review Comment:
   `ms().verified_stmt("SELECT [a]]]]b]")` fails because value `a]]b` formats 
as `[a]]b]` and reparses as `a]b`.



##########
src/ast/mod.rs:
##########
@@ -385,7 +385,28 @@ impl fmt::Display for Ident {
                 let escaped = value::escape_quoted_string(&self.value, q);
                 write!(f, "{q}{escaped}{q}")
             }
-            Some('[') => write!(f, "[{}]", self.value),
+            Some('[') => {
+                let v = &self.value;
+                if v.len() >= 2 && v.starts_with('"') && v.ends_with('"') {

Review Comment:
   `ms().verified_stmt(r#"SELECT ["a]]b"]"#)` fails because valid MSSQL and 
SQLite value `"a]b"` formats as malformed `SELECT ["a]b"]`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to