-
Notifications
You must be signed in to change notification settings - Fork 753
Escape closing brackets in bracket-quoted identifiers #2418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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). | ||
| let v = &self.value; | ||
| if v.len() >= 2 && v.starts_with('"') && v.ends_with('"') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(']', "]]")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"), | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -937,6 +937,18 @@ fn parse_table_name_in_square_brackets() { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_bracket_identifier_with_escaped_closing_bracket() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
| // A bracket-quoted identifier whose value contains `]` must serialize | ||
| // with the bracket doubled so it round-trips. See #2409. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
|
||
There was a problem hiding this comment.
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.