Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,19 @@ 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('[') => {
// Redshift nested quoted identifiers (e.g. `["a]b"]`) store the
// value as a complete double-quoted string whose inner `]` is
// literal, so leave those unchanged. Otherwise double each `]`,
// mirroring the tokenizer folding `]]` into `]`, so the
// identifier round-trips (#2409).

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.

No need to refer to issues in the code itself. Code comments should refer to the present code, not previous states of the code, save for areas very prone to regressions and reiterated attempts.

let v = &self.value;
if v.len() >= 2 && v.starts_with('"') && v.ends_with('"') {

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 believe there are several other cases where the current solution fails, other than the one I reported in the test comment. I suggest you fuzz with seeding/use round trip prop tests this code before pushing the next iteration of your PR, since it would have most likely immediately caught the mentioned problems, even if you vibe code this thing using AI. It is a very effective support tool when you lean on code generation, as it gives you test inputs generation and invariant testing.

write!(f, "[{v}]")
} else {
write!(f, "[{}]", v.replace(']', "]]"))

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.

Here you are adding allocation in a hot path with that replace, in a write. I believe it is unnecessary to do so, just print what you need without reallocating the string.

}
}
None => f.write_str(&self.value),
_ => panic!("unexpected quote style"),
}
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,18 @@ fn parse_table_name_in_square_brackets() {
);
}

#[test]
fn parse_bracket_identifier_with_escaped_closing_bracket() {

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.

Testing is insufficient and the proposed changes are currently regressing working cases. For instance, in unescaped mode, SELECT [a]]b] in current main parses correctly, while with this PR it parses to a]]]]b.

// A bracket-quoted identifier whose value contains `]` must serialize
// with the bracket doubled so it round-trips. See #2409.

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.

Same here: comments should be about the code, not about previous states of the code. This is information appropriate for the PR post or commit, not code.

let select = ms().verified_only_select("SELECT [a]]b]");
assert_eq!(
&Expr::Identifier(Ident::with_quote('[', "a]b")),
expr_from_projection(&select.projection[0]),
);
ms().verified_stmt("SELECT [a]]b] FROM [c]]d]");
}

#[test]
fn parse_for_clause() {
ms_and_generic().verified_stmt("SELECT a FROM t FOR JSON PATH");
Expand Down