Skip to content

Commit 13b88a3

Browse files
authored
Fixed transaction handling for snowflake (#2263)
1 parent 64f4b1f commit 13b88a3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/dialect/snowflake.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ impl Dialect for SnowflakeDialect {
247247

248248
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
249249
if parser.parse_keyword(Keyword::BEGIN) {
250+
// Snowflake supports both `BEGIN TRANSACTION` and `BEGIN ... END` blocks.
251+
// If the next keyword indicates a transaction statement, let the
252+
// standard parse_begin() handle it.
253+
if parser
254+
.peek_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK, Keyword::NAME])
255+
.is_some()
256+
|| matches!(parser.peek_token_ref().token, Token::SemiColon | Token::EOF)
257+
{
258+
parser.prev_token();
259+
return None;
260+
}
250261
return Some(parser.parse_begin_exception_end());
251262
}
252263

tests/sqlparser_snowflake.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4610,6 +4610,27 @@ END
46104610
assert_eq!(2, exception[1].statements.len());
46114611
}
46124612

4613+
#[test]
4614+
fn test_begin_transaction() {
4615+
snowflake().verified_stmt("BEGIN TRANSACTION");
4616+
snowflake().verified_stmt("BEGIN WORK");
4617+
4618+
// BEGIN TRANSACTION with statements
4619+
let stmts = snowflake()
4620+
.parse_sql_statements("BEGIN TRANSACTION; DROP TABLE IF EXISTS bla; COMMIT")
4621+
.unwrap();
4622+
assert_eq!(3, stmts.len());
4623+
4624+
// Bare BEGIN (no TRANSACTION keyword) with statements
4625+
let stmts = snowflake()
4626+
.parse_sql_statements("BEGIN; DROP TABLE IF EXISTS bla; COMMIT")
4627+
.unwrap();
4628+
assert_eq!(3, stmts.len());
4629+
4630+
// Bare BEGIN at EOF (no semicolon, no TRANSACTION keyword)
4631+
snowflake().verified_stmt("BEGIN");
4632+
}
4633+
46134634
#[test]
46144635
fn test_snowflake_fetch_clause_syntax() {
46154636
let canonical = "SELECT c1 FROM fetch_test FETCH FIRST 2 ROWS ONLY";

0 commit comments

Comments
 (0)