Skip to content

Commit 64f38f0

Browse files
Vedinrampage644
authored andcommitted
Support BEGIN as standalon clause (#17)
* Support BEGIN as standalon clause * Only affect snowflake logic
1 parent c498dd9 commit 64f38f0

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
@@ -244,7 +244,24 @@ impl Dialect for SnowflakeDialect {
244244

245245
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
246246
if parser.parse_keyword(Keyword::BEGIN) {
247-
return Some(parser.parse_begin_exception_end());
247+
// Allow standalone BEGIN; for Snowflake
248+
match &parser.peek_token_ref().token {
249+
Token::SemiColon | Token::EOF => {
250+
return Some(Ok(Statement::StartTransaction {
251+
modes: Default::default(),
252+
begin: true,
253+
transaction: None,
254+
modifier: None,
255+
statements: vec![],
256+
exception: None,
257+
has_end_keyword: false,
258+
}))
259+
}
260+
_ => {
261+
// BEGIN ... [EXCEPTION] ... END block
262+
return Some(parser.parse_begin_exception_end());
263+
}
264+
}
248265
}
249266

250267
if parser.parse_keywords(&[Keyword::ALTER, Keyword::DYNAMIC, Keyword::TABLE]) {

tests/sqlparser_snowflake.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,6 +4302,40 @@ fn test_snowflake_fetch_clause_syntax() {
43024302
);
43034303
}
43044304

4305+
#[test]
4306+
fn test_snowflake_begin_standalone() {
4307+
// BEGIN; (no END) should be allowed for Snowflake
4308+
let mut stmts = snowflake().parse_sql_statements("BEGIN;").unwrap();
4309+
assert_eq!(1, stmts.len());
4310+
match stmts.remove(0) {
4311+
Statement::StartTransaction {
4312+
begin,
4313+
has_end_keyword,
4314+
statements,
4315+
..
4316+
} => {
4317+
assert!(begin);
4318+
assert!(!has_end_keyword);
4319+
assert!(statements.is_empty());
4320+
}
4321+
other => panic!("unexpected stmt: {other:?}"),
4322+
}
4323+
}
4324+
4325+
#[test]
4326+
fn test_snowflake_begin_commit_sequence() {
4327+
let mut stmts = snowflake().parse_sql_statements("BEGIN; COMMIT;").unwrap();
4328+
assert_eq!(2, stmts.len());
4329+
match stmts.remove(0) {
4330+
Statement::StartTransaction { begin, .. } => assert!(begin),
4331+
other => panic!("unexpected first stmt: {other:?}"),
4332+
}
4333+
match stmts.remove(0) {
4334+
Statement::Commit { end, .. } => assert!(!end),
4335+
other => panic!("unexpected second stmt: {other:?}"),
4336+
}
4337+
}
4338+
43054339
#[test]
43064340
fn test_snowflake_create_view_with_multiple_column_options() {
43074341
let create_view_with_tag =

0 commit comments

Comments
 (0)