-
Notifications
You must be signed in to change notification settings - Fork 691
MSSQL: Support standalone BEGIN...END blocks #2186
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?
Conversation
Signed-off-by: Guan-Ming (Wesley) Chiu <105915352+guan404ming@users.noreply.github.com>
Signed-off-by: Guan-Ming (Wesley) Chiu <105915352+guan404ming@users.noreply.github.com>
| if parser.parse_keyword(Keyword::BEGIN) { | ||
| if parser.peek_keyword(Keyword::TRANSACTION) | ||
| || parser.peek_keyword(Keyword::WORK) | ||
| || parser.peek_keyword(Keyword::TRY) | ||
| || parser.peek_keyword(Keyword::CATCH) | ||
| || parser.peek_keyword(Keyword::DEFERRED) | ||
| || parser.peek_keyword(Keyword::IMMEDIATE) | ||
| || parser.peek_keyword(Keyword::EXCLUSIVE) | ||
| || parser.peek_token_ref().token == Token::SemiColon | ||
| || parser.peek_token_ref().token == Token::EOF | ||
| { | ||
| parser.prev_token(); | ||
| return None; | ||
| } | ||
| Some(parser.parse_begin_exception_end()) |
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.
would a condition like this do what we want?
if parse.peek_keywords(BEGIN, TRANSACTION) {
None
} else if parse_keyword(BEGIN) {
Some(parser.parse_begin_exception_end())
}its not super clear to me why the current logic looks at WORK, TRY etc keywords?
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.
Thanks for the review. These keywords are checked because parse_begin() handles more than just BEGIN TRANSACTION. It also handles WORK, TRY, CATCH, DEFERRED, IMMEDIATE, EXCLUSIVE (since MsSql's supports_start_transaction_modifier() returns true), as well as bare BEGIN;. If any of these are missed, they'd be incorrectly routed to parse_begin_exception_end() and fail to parse.
For example, BEGIN TRY ... END TRY is valid MSSQL syntax. If we only check for BEGIN TRANSACTION, then BEGIN TRY would fall through to parse_begin_exception_end(), which would try to parse TRY as a SQL statement and fail. because TRY is supposed to be handled as a
datafusion-sqlparser-rs/src/parser/mod.rs
Lines 17938 to 17939 in c8b7f7c
| } else if self.parse_keyword(Keyword::TRY) { | |
| Some(TransactionModifier::Try) |
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.
Feel free to let me know if there are any not clear, thanks!
Why
I'm currently building sql-parser npm pkg for js/ts named sqlparser-ts, which powered by this amazing repo via WebAssembly! When reconstruct all dialects test in typescript, I find some issue and decide to contribute back to upstream~
The MSSQL dialect previously had no way to parse standalone BEGIN...END blocks.
BEGIN SELECT 1; ENDwould fail because parse_begin() only handles BEGIN TRANSACTION.How
wires MSSQL into the same path like BigQuery and Snowflake