Skip to content

Commit c82e41e

Browse files
VedinJanKaul
authored andcommitted
Support BEGIN as standalon clause (#17)
* Support BEGIN as standalon clause * Only affect snowflake logic
1 parent 3dc691d commit c82e41e

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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
@@ -4310,6 +4310,40 @@ fn test_snowflake_fetch_clause_syntax() {
43104310
);
43114311
}
43124312

4313+
#[test]
4314+
fn test_snowflake_begin_standalone() {
4315+
// BEGIN; (no END) should be allowed for Snowflake
4316+
let mut stmts = snowflake().parse_sql_statements("BEGIN;").unwrap();
4317+
assert_eq!(1, stmts.len());
4318+
match stmts.remove(0) {
4319+
Statement::StartTransaction {
4320+
begin,
4321+
has_end_keyword,
4322+
statements,
4323+
..
4324+
} => {
4325+
assert!(begin);
4326+
assert!(!has_end_keyword);
4327+
assert!(statements.is_empty());
4328+
}
4329+
other => panic!("unexpected stmt: {other:?}"),
4330+
}
4331+
}
4332+
4333+
#[test]
4334+
fn test_snowflake_begin_commit_sequence() {
4335+
let mut stmts = snowflake().parse_sql_statements("BEGIN; COMMIT;").unwrap();
4336+
assert_eq!(2, stmts.len());
4337+
match stmts.remove(0) {
4338+
Statement::StartTransaction { begin, .. } => assert!(begin),
4339+
other => panic!("unexpected first stmt: {other:?}"),
4340+
}
4341+
match stmts.remove(0) {
4342+
Statement::Commit { end, .. } => assert!(!end),
4343+
other => panic!("unexpected second stmt: {other:?}"),
4344+
}
4345+
}
4346+
43134347
#[test]
43144348
fn test_snowflake_create_view_with_multiple_column_options() {
43154349
let create_view_with_tag =

0 commit comments

Comments
 (0)