Skip to content

Add ability for dialects to override prefix, infix, and statement parsing #581

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

Merged
merged 16 commits into from
Aug 19, 2022

Conversation

andygrove
Copy link
Member

@andygrove andygrove commented Aug 17, 2022

Closes #580

This change adds some new functions to Dialect:

  • parse_prefix
  • parse_infix
  • parse_statement

The parser will invoke these methods to see if the dialect wants to override the parsing of these items; otherwise, it will parse them itself. This is a much cleaner way to add dialect-specific parsing, IMO and will allow us to start removing all the if dialect_of hacks from the main parser.

I moved the Postgres parsing of comments and the SqlLite parsing of REPLACE into their respective dialects to demonstrate how this works.

@coveralls
Copy link

coveralls commented Aug 17, 2022

Pull Request Test Coverage Report for Build 2877619323

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 14 of 15 (93.33%) changed or added relevant lines in 2 files are covered.
  • 400 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.04%) to 85.396%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser.rs 8 9 88.89%
Files with Coverage Reduction New Missed Lines %
src/dialect/mod.rs 1 94.44%
src/parser.rs 399 83.1%
Totals Coverage Status
Change from base Build 2877505870: -0.04%
Covered Lines: 9514
Relevant Lines: 11141

💛 - Coveralls

@andygrove andygrove closed this Aug 17, 2022
@andygrove andygrove reopened this Aug 17, 2022
@andygrove andygrove marked this pull request as ready for review August 17, 2022 19:51
@andygrove andygrove requested a review from alamb August 17, 2022 19:52
@andygrove andygrove changed the title prototype ability for dialects to override prefix and infix parsing Add ability for dialects to override prefix, infix, and statement parsing Aug 17, 2022
@andygrove andygrove marked this pull request as draft August 17, 2022 20:18
Comment on lines -198 to -204
Keyword::REPLACE if dialect_of!(self is SQLiteDialect ) => {
self.prev_token();
Ok(self.parse_insert()?)
}
Keyword::COMMENT if dialect_of!(self is PostgreSqlDialect) => {
Ok(self.parse_comment()?)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This dialect-specific code has now moved to the appropriate dialects.

@andygrove andygrove marked this pull request as ready for review August 17, 2022 21:35
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I think it looks like a great idea -- thank you @andygrove

I had a small comment on tests but otherwise 👍 🚀

src/parser.rs Outdated
let sql = "SELECT 1 + 2";
let ast = Parser::parse_sql(&dialect, sql)?;
let query = &ast[0];
assert_eq!("SELECT NULL + 2", &format!("{}", query));
Copy link
Contributor

Choose a reason for hiding this comment

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

😆

src/parser.rs Outdated
@@ -4926,4 +4910,124 @@ mod tests {
assert_eq!(ast.to_string(), sql.to_string());
});
}

#[test]
fn custom_prefix_parser() -> Result<(), ParserError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend putting these tests into tests somewhere so that we can be sure everything needed is exposed publically.

Perhaps tests/sqlparser_custom_dialects.rs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I have moved them.

@andygrove andygrove merged commit 72559e9 into apache:main Aug 19, 2022
@andygrove andygrove deleted the dialect-override-prefix-infix branch August 19, 2022 11:44
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.

Allow dialects to override prefix and infix parsing
3 participants