Skip to content

Commit 3a7e474

Browse files
committed
Fix GO failing to find following newline
1 parent d62d359 commit 3a7e474

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

src/parser/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19736,11 +19736,27 @@ impl<'a> Parser<'a> {
1973619736
};
1973719737
};
1973819738

19739-
if self.peek_token().token == Token::SemiColon {
19740-
parser_err!(
19741-
"GO may not end with a semicolon",
19742-
self.peek_token().span.start
19743-
)?;
19739+
loop {
19740+
let next_token = self.peek_token_no_skip();
19741+
match next_token.token {
19742+
Token::EOF => break,
19743+
Token::Whitespace(ref w) => match w {
19744+
Whitespace::Newline => break,
19745+
Whitespace::SingleLineComment { comment, prefix: _ } => {
19746+
if comment.ends_with('\n') {
19747+
break;
19748+
}
19749+
_ = self.next_token_no_skip();
19750+
}
19751+
_ => _ = self.next_token_no_skip(),
19752+
},
19753+
_ => {
19754+
parser_err!(
19755+
"GO must be followed by a newline or EOF",
19756+
self.peek_token().span.start
19757+
)?;
19758+
}
19759+
};
1974419760
}
1974519761

1974619762
Ok(Statement::Go(GoStatement { count }))

tests/sqlparser_mssql.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,10 +2928,9 @@ fn parse_mssql_go_keyword() {
29282928
// }
29292929
// );
29302930

2931-
// let actually_column_alias_no_as = "SELECT NULL GO";
2932-
// let stmts = ms().parse_sql_statements(actually_column_alias_no_as).unwrap();
2933-
// assert_eq!(stmts.len(), 1);
2934-
// match &stmts[0] {
2931+
// let actually_column_alias = "SELECT NULL GO";
2932+
// let stmt = ms().one_statement_parses_to(actually_column_alias, "SELECT NULL AS GO");
2933+
// match &stmt {
29352934
// Statement::Query(query) => {
29362935
// let select = query.body.as_select().unwrap();
29372936
// assert_eq!(
@@ -2945,6 +2944,11 @@ fn parse_mssql_go_keyword() {
29452944
// _ => panic!("Expected Query statement"),
29462945
// }
29472946

2947+
let cte_following_go = "USE some_database;\nGO\n;WITH cte AS (\nSELECT 1 x\n)\nSELECT * FROM cte;";
2948+
let stmts = ms().parse_sql_statements(cte_following_go).unwrap();
2949+
assert_eq!(stmts.len(), 3);
2950+
assert_eq!(stmts[1], Statement::Go(GoStatement { count: None }));
2951+
29482952
let multi_line_comment_following = "USE some_database;\nGO/* okay */42";
29492953
let stmts =
29502954
ms().statements_parse_to(multi_line_comment_following, 2, "USE some_database\nGO 42");
@@ -2963,6 +2967,13 @@ fn parse_mssql_go_keyword() {
29632967
err.unwrap_err().to_string(),
29642968
"sql parser error: Expected: literal int or newline, found: x"
29652969
);
2970+
2971+
let invalid_go_delimiter = "SELECT 1\nGO;";
2972+
let err = ms().parse_sql_statements(invalid_go_delimiter);
2973+
assert_eq!(
2974+
err.unwrap_err().to_string(),
2975+
"sql parser error: Expected: literal int or newline, found: ;"
2976+
);
29662977
}
29672978

29682979
#[test]

0 commit comments

Comments
 (0)