Skip to content

Commit 2801207

Browse files
authored
Support BEGIN as standalon clause (#17)
* Support BEGIN as standalon clause * Only affect snowflake logic
1 parent 7dbb724 commit 2801207

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/dialect/snowflake.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,24 @@ impl Dialect for SnowflakeDialect {
132132

133133
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
134134
if parser.parse_keyword(Keyword::BEGIN) {
135-
return Some(parser.parse_begin_exception_end());
135+
// Allow standalone BEGIN; for Snowflake
136+
match &parser.peek_token_ref().token {
137+
Token::SemiColon | Token::EOF => {
138+
return Some(Ok(Statement::StartTransaction {
139+
modes: Default::default(),
140+
begin: true,
141+
transaction: None,
142+
modifier: None,
143+
statements: vec![],
144+
exception: None,
145+
has_end_keyword: false,
146+
}))
147+
}
148+
_ => {
149+
// BEGIN ... [EXCEPTION] ... END block
150+
return Some(parser.parse_begin_exception_end());
151+
}
152+
}
136153
}
137154

138155
if parser.parse_keywords(&[Keyword::ALTER, Keyword::SESSION]) {

tests/sqlparser_snowflake.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,6 +4343,40 @@ fn test_snowflake_fetch_clause_syntax() {
43434343
);
43444344
}
43454345

4346+
#[test]
4347+
fn test_snowflake_begin_standalone() {
4348+
// BEGIN; (no END) should be allowed for Snowflake
4349+
let mut stmts = snowflake().parse_sql_statements("BEGIN;").unwrap();
4350+
assert_eq!(1, stmts.len());
4351+
match stmts.remove(0) {
4352+
Statement::StartTransaction {
4353+
begin,
4354+
has_end_keyword,
4355+
statements,
4356+
..
4357+
} => {
4358+
assert!(begin);
4359+
assert!(!has_end_keyword);
4360+
assert!(statements.is_empty());
4361+
}
4362+
other => panic!("unexpected stmt: {other:?}"),
4363+
}
4364+
}
4365+
4366+
#[test]
4367+
fn test_snowflake_begin_commit_sequence() {
4368+
let mut stmts = snowflake().parse_sql_statements("BEGIN; COMMIT;").unwrap();
4369+
assert_eq!(2, stmts.len());
4370+
match stmts.remove(0) {
4371+
Statement::StartTransaction { begin, .. } => assert!(begin),
4372+
other => panic!("unexpected first stmt: {other:?}"),
4373+
}
4374+
match stmts.remove(0) {
4375+
Statement::Commit { end, .. } => assert!(!end),
4376+
other => panic!("unexpected second stmt: {other:?}"),
4377+
}
4378+
}
4379+
43464380
#[test]
43474381
fn test_snowflake_create_view_with_multiple_column_options() {
43484382
let create_view_with_tag =

0 commit comments

Comments
 (0)