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

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

Merged
merged 16 commits into from Aug 19, 2022

Conversation

andygrove
Copy link
Collaborator

@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 2889163360

  • 94 of 97 (96.91%) changed or added relevant lines in 5 files are covered.
  • 605 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.03%) to 85.467%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/dialect/postgresql.rs 21 22 95.45%
tests/sqlparser_custom_dialect.rs 54 56 96.43%
Files with Coverage Reduction New Missed Lines %
tests/sqlparser_postgres.rs 18 98.06%
tests/sqlparser_common.rs 60 97.05%
src/ast/mod.rs 205 77.61%
src/parser.rs 322 83.05%
Totals Coverage Status
Change from base Build 2877505870: 0.03%
Covered Lines: 9609
Relevant Lines: 11243

💛 - 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
Collaborator 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
Collaborator

@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
Collaborator

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
Collaborator

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
Collaborator 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 sqlparser-rs: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