Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,22 @@ impl Dialect for MsSqlDialect {
}

fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
if parser.peek_keyword(Keyword::IF) {
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())
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

@guan404ming guan404ming Jan 31, 2026

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

} else if self.parse_keyword(Keyword::TRY) {
Some(TransactionModifier::Try)

Copy link
Copy Markdown
Member Author

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!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh I see, that makes sense! Can we do something like this to clarify the code?

we pull out the linked fallback logic into a function parse_transaction_modifier(). So that it can be reused.

Then here we can do e.g.

if without_modifier = self.maybe_parse(|parser| {
    if parser.parse_transaction_modifier()?.is_some() {
        parser_error!()
    } else {
        Some(())
    }
})?.is_some();

such that with that bool we know whether to defer to the main parser logic or continue with the begin/exception logic

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the really nice suggestion. I just updated it does more clean and work as well.

} else if parser.peek_keyword(Keyword::IF) {
Some(self.parse_if_stmt(parser))
} else if parser.parse_keywords(&[Keyword::CREATE, Keyword::TRIGGER]) {
Some(self.parse_create_trigger(parser, false))
Expand Down
71 changes: 71 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2554,3 +2554,74 @@ fn test_sql_keywords_as_column_aliases() {
}
}
}

#[test]
fn parse_mssql_begin_end_block() {
// Single statement
let sql = "BEGIN SELECT 1; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
begin,
has_end_keyword,
statements,
transaction,
modifier,
..
} => {
assert!(begin);
assert!(has_end_keyword);
assert!(transaction.is_none());
assert!(modifier.is_none());
assert_eq!(statements.len(), 1);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// Multiple statements
let sql = "BEGIN SELECT 1; SELECT 2; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
statements,
has_end_keyword,
..
} => {
assert!(has_end_keyword);
assert_eq!(statements.len(), 2);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// DML inside BEGIN/END
let sql = "BEGIN INSERT INTO t VALUES (1); UPDATE t SET x = 2; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
statements,
has_end_keyword,
..
} => {
assert!(has_end_keyword);
assert_eq!(statements.len(), 2);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// BEGIN TRANSACTION still works
let sql = "BEGIN TRANSACTION";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
begin,
has_end_keyword,
transaction,
..
} => {
assert!(begin);
assert!(!has_end_keyword);
assert!(transaction.is_some());
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}
}