Skip to content

Commit 193e253

Browse files
committed
Fixed transaction handling for snowflake
1 parent 6691f31 commit 193e253

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/dialect/snowflake.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ 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.peek_keyword(Keyword::TRANSACTION)
254+
|| parser.peek_keyword(Keyword::WORK)
255+
|| parser.peek_keyword(Keyword::NAME)
256+
|| parser.peek_token().token == Token::SemiColon
257+
|| parser.peek_token().token == Token::EOF
258+
{
259+
parser.prev_token();
260+
return None;
261+
}
250262
return Some(parser.parse_begin_exception_end());
251263
}
252264

tests/sqlparser_snowflake.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4610,6 +4610,24 @@ 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+
46134631
#[test]
46144632
fn test_snowflake_fetch_clause_syntax() {
46154633
let canonical = "SELECT c1 FROM fetch_test FETCH FIRST 2 ROWS ONLY";

0 commit comments

Comments
 (0)