Skip to content
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

Correctly tokenize nested comments #1629

Merged
merged 6 commits into from
Jan 5, 2025
Merged

Conversation

hansott
Copy link
Contributor

@hansott hansott commented Dec 30, 2024

The tokenizer currently throws EOF error for select 'foo' /*/**/*/

last_ch causes problems when tokenizing nested comments, we have to consume the combination of /* or */

The existing tokenize_nested_multiline_comment test fails after fixing this logic:

/*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/
^^ Start                                      ^^ End nested comment

proof:

psql (14.15 (Homebrew), server 14.11)
Type "help" for help.

main_db=# SELECT 'foo' /*/**/*/;
 ?column?
----------
 foo
(1 row)

main_db=# SELECT 'foo' /*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/;
ERROR:  syntax error at or near ";"
LINE 1: .../*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/;
                                                                      ^
main_db=# SELECT 'foo' /*multi-line\n* \n/* comment \n /*comment*/*/ */;
 ?column?
----------
 foo
(1 row)

Relevant: #726

The tokenizer currently throws EOF error for

`select 'foo' /*/**/*/`

`last_ch` causes problems when tokenizing nested comments, we have to
consume the combination of /* or */

The existing `tokenize_nested_multiline_comment` test fails after fixing
this logic:

/*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/
^^ Start                                      ^^ End nested comment

Relevant: apache#726
@hansott
Copy link
Contributor Author

hansott commented Dec 30, 2024

@iffyio I'm thinking about adding an option in the dialect, cause not every database supports these nested comments.

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hansott! Left a couple comments

src/tokenizer.rs Outdated
if last_ch == '/' && ch == '*' {
if ch == '/' && matches!(chars.peek(), Some('*')) && supports_nested_comments {
s.push(ch);
s.push(chars.next().unwrap()); // consume the '*'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rewrite this to return an error in order to avoid the unwrap?

src/tokenizer.rs Outdated
break Ok(Some(Token::Whitespace(Whitespace::MultiLineComment(s))));
}
s.push(slash.unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, it would be nice if we can avoid unwraping in code

src/tokenizer.rs Outdated

loop {
match chars.next() {
Some(ch) => {
if last_ch == '/' && ch == '*' {
if ch == '/' && matches!(chars.peek(), Some('*')) && supports_nested_comments {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move some of the condition out to the match statement? thinking that way we avoid extra nesting + continue. And the different cases are clearer. e.g.

Some('/') if matches!(chars.peek(), Some('*')) && supports_nested_comments => { ... }
Some('*') if matches!(chars.peek(), Some('/')) => { ... }
Some(ch) => { ... }
None => { ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will do!

@hansott
Copy link
Contributor Author

hansott commented Jan 5, 2025

@iffyio Addressed your feedback, thanks a lot! Tests passing ✅

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @hansott!
cc @alamb

@iffyio iffyio merged commit 8bc63f0 into apache:main Jan 5, 2025
9 checks passed
@hansott hansott deleted the fix-nested-comments branch January 6, 2025 08:37
hansott added a commit to hansott/datafusion-sqlparser-rs that referenced this pull request Jan 7, 2025
…o patch-carriage-return

* 'main' of github.com:hansott/datafusion-sqlparser-rs:
  Add support for MySQL's INSERT INTO ... SET syntax (apache#1641)
  Add support for Snowflake LIST and REMOVE (apache#1639)
  Add support for the SQL OVERLAPS predicate (apache#1638)
  Add support for various Snowflake grantees (apache#1640)
  Add support for USE SECONDARY ROLE (vs. ROLES) (apache#1637)
  Correctly tokenize nested comments (apache#1629)
  Add support for MYSQL's `RENAME TABLE` (apache#1616)
  Test benchmarks and Improve benchmark README.md (apache#1627)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants