Skip to content

Commit aaa38a8

Browse files
author
Roman Borschel
committed
Fix: Snowflake ALTER SESSION cannot be followed by other statements.
Fixes apache#1775. Currently parse_session_options does not check for semicolons, which makes it impossible to parse multiple statements, where a statement comes after ALTER SESSION. Furthermore, it seems we should not advance the parser onto the semicolon as it is otherwise skipped in the next loop iteration of parse_statements() where the parser advances to the next token, which would then result in "Expected end of statement".
1 parent 60df756 commit aaa38a8

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/dialect/snowflake.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1013,10 +1013,15 @@ fn parse_session_options(
10131013
let mut options: Vec<KeyValueOption> = Vec::new();
10141014
let empty = String::new;
10151015
loop {
1016-
match parser.next_token().token {
1017-
Token::SemiColon => break,
1018-
Token::Comma => continue,
1016+
let next_token = parser.peek_token();
1017+
match next_token.token {
1018+
Token::SemiColon | Token::EOF => break,
1019+
Token::Comma => {
1020+
parser.advance_token();
1021+
continue;
1022+
}
10191023
Token::Word(key) => {
1024+
parser.advance_token();
10201025
if set {
10211026
let option = parse_option(parser, key)?;
10221027
options.push(option);
@@ -1028,13 +1033,8 @@ fn parse_session_options(
10281033
});
10291034
}
10301035
}
1031-
t => {
1032-
println!("> NEXT TOKEN {:?}", t);
1033-
println!("> PEEK TOKEN {:?}", parser.peek_token().token);
1034-
if parser.peek_token().token == Token::EOF {
1035-
break;
1036-
}
1037-
return parser.expected("another option", parser.peek_token());
1036+
_ => {
1037+
return parser.expected("another option or end of statement", next_token);
10381038
}
10391039
}
10401040
}

tests/sqlparser_snowflake.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,12 @@ fn test_alter_session() {
35073507
}
35083508

35093509
#[test]
3510-
fn voodoo() {
3511-
let res = snowflake().parse_sql_statements("ALTER SESSION SET QUERY_TAG='hello'; SELECT 42");
3512-
println!("{:?}", res);
3510+
fn test_alter_session_followed_by_statement() {
3511+
let stmts = snowflake()
3512+
.parse_sql_statements("ALTER SESSION SET QUERY_TAG='hello'; SELECT 42")
3513+
.unwrap();
3514+
match stmts[..] {
3515+
[Statement::AlterSession { .. }, Statement::Query { .. }] => {}
3516+
_ => panic!("Unexpected statements: {:?}", stmts),
3517+
}
35133518
}

0 commit comments

Comments
 (0)