Skip to content

Conversation

@guan404ming
Copy link
Member

@guan404ming guan404ming commented Jan 30, 2026

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; END would fail because parse_begin() only handles BEGIN TRANSACTION.

How

wires MSSQL into the same path like BigQuery and Snowflake

Signed-off-by: Guan-Ming (Wesley) Chiu <[email protected]>
Signed-off-by: Guan-Ming (Wesley) Chiu <[email protected]>
Comment on lines 148 to 162
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
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
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
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
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
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.

@guan404ming guan404ming requested a review from iffyio February 2, 2026 10:54
Signed-off-by: Guan-Ming (Wesley) Chiu <[email protected]>
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.

Left some minor comments, otherwise the changes look good to me overall

@guan404ming guan404ming requested a review from iffyio February 3, 2026 15:51
@guan404ming
Copy link
Member Author

Just updated, thanks for the suggestion!

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 @guan404ming!

@iffyio iffyio added this pull request to the merge queue Feb 3, 2026
Merged via the queue into apache:main with commit 3ac5670 Feb 3, 2026
10 checks passed
@guan404ming
Copy link
Member Author

Thanks @iffyio for your detailed reviews!

@guan404ming guan404ming deleted the mssql-begin-end-block branch February 3, 2026 16:14
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