Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15597,6 +15597,25 @@ impl<'a> Parser<'a> {
}

pub fn parse_begin_exception_end(&mut self) -> Result<Statement, ParserError> {
// Snowflake allows BEGIN as a standalone transaction statement (no END).
// If the next token is a semicolon or EOF, treat it as a standalone BEGIN.
if dialect_of!(self is SnowflakeDialect) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to add this directly to snowflake parser
https://github.com/Embucket/datafusion-sqlparser-rs/blob/embucket-sync-df50.0.0-parser0.58.0/src/dialect/snowflake.rs#L134-L136
since this logic will be called before default parser

match &self.peek_token_ref().token {
Token::SemiColon | Token::EOF => {
return Ok(Statement::StartTransaction {
begin: true,
statements: vec![],
exception: None,
has_end_keyword: false,
transaction: None,
modifier: None,
modes: Default::default(),
})
}
_ => {}
}
}

let statements = self.parse_statement_list(&[Keyword::EXCEPTION, Keyword::END])?;

let exception = if self.parse_keyword(Keyword::EXCEPTION) {
Expand Down Expand Up @@ -15628,17 +15647,30 @@ impl<'a> Parser<'a> {
None
};

self.expect_keyword(Keyword::END)?;

Ok(Statement::StartTransaction {
begin: true,
statements,
exception,
has_end_keyword: true,
transaction: None,
modifier: None,
modes: Default::default(),
})
if dialect_of!(self is SnowflakeDialect) {
// Make END optional for Snowflake. If present, set flag accordingly.
let has_end = self.parse_keyword(Keyword::END);
Ok(Statement::StartTransaction {
begin: true,
statements,
exception,
has_end_keyword: has_end,
transaction: None,
modifier: None,
modes: Default::default(),
})
} else {
self.expect_keyword(Keyword::END)?;
Ok(Statement::StartTransaction {
begin: true,
statements,
exception,
has_end_keyword: true,
transaction: None,
modifier: None,
modes: Default::default(),
})
}
}

pub fn parse_end(&mut self) -> Result<Statement, ParserError> {
Expand Down
34 changes: 34 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4343,6 +4343,40 @@ fn test_snowflake_fetch_clause_syntax() {
);
}

#[test]
fn test_snowflake_begin_standalone() {
// BEGIN; (no END) should be allowed for Snowflake
let mut stmts = snowflake().parse_sql_statements("BEGIN;").unwrap();
assert_eq!(1, stmts.len());
match stmts.remove(0) {
Statement::StartTransaction {
begin,
has_end_keyword,
statements,
..
} => {
assert!(begin);
assert!(!has_end_keyword);
assert!(statements.is_empty());
}
other => panic!("unexpected stmt: {other:?}"),
}
}

#[test]
fn test_snowflake_begin_commit_sequence() {
let mut stmts = snowflake().parse_sql_statements("BEGIN; COMMIT;").unwrap();
assert_eq!(2, stmts.len());
match stmts.remove(0) {
Statement::StartTransaction { begin, .. } => assert!(begin),
other => panic!("unexpected first stmt: {other:?}"),
}
match stmts.remove(0) {
Statement::Commit { end, .. } => assert!(!end),
other => panic!("unexpected second stmt: {other:?}"),
}
}

#[test]
fn test_snowflake_create_view_with_multiple_column_options() {
let create_view_with_tag =
Expand Down
Loading