From 7695ca5e5d5d35328082cbf8e599712d2b294a04 Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:00:49 -0800 Subject: [PATCH 1/3] MsSql: Read one statement as the body of WHILE without BEGIN --- .../mssql_while_single_statement.sql | 3 ++ src/parser/mod.rs | 29 +++++++++++----- tests/sqlparser_mssql.rs | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 fuzz/fuzz_seeds/mssql_while_single_statement.sql diff --git a/fuzz/fuzz_seeds/mssql_while_single_statement.sql b/fuzz/fuzz_seeds/mssql_while_single_statement.sql new file mode 100644 index 0000000000..73a0831197 --- /dev/null +++ b/fuzz/fuzz_seeds/mssql_while_single_statement.sql @@ -0,0 +1,3 @@ +WHILE @n < 10 + SET @n = @n + 1 +SELECT @n \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index aba553b839..a41f342aae 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -871,18 +871,33 @@ impl<'a> Parser<'a> { }) } - /// Parse a `WHILE` statement. + /// Parse a `WHILE` statement, whose body is one statement or a + /// `BEGIN ... END` block. /// /// See [Statement::While] fn parse_while(&mut self) -> Result { - self.expect_keyword_is(Keyword::WHILE)?; - let while_block = self.parse_conditional_statement_block(&[Keyword::END])?; + let start_token = self.expect_keyword(Keyword::WHILE)?; + let condition = self.parse_expr()?; + let conditional_statements = if self.peek_keyword(Keyword::BEGIN) { + self.parse_conditional_statements(&[Keyword::END])? + } else { + ConditionalStatements::Sequence { + statements: vec![self.parse_statement()?], + } + }; - Ok(WhileStatement { while_block }) + Ok(WhileStatement { + while_block: ConditionalStatementBlock { + start_token: AttachedToken(start_token), + condition: Some(condition), + then_token: None, + conditional_statements, + }, + }) } /// Parses an expression and associated list of statements - /// belonging to a conditional statement like `IF` or `WHEN` or `WHILE`. + /// belonging to a conditional statement like `IF` or `WHEN`. /// /// Example: /// ```sql @@ -897,10 +912,6 @@ impl<'a> Parser<'a> { let condition = match &start_token.token { Token::Word(w) if w.keyword == Keyword::ELSE => None, - Token::Word(w) if w.keyword == Keyword::WHILE => { - let expr = self.parse_expr()?; - Some(expr) - } _ => { let expr = self.parse_expr()?; then_token = Some(AttachedToken(self.expect_keyword(Keyword::THEN)?)); diff --git a/tests/sqlparser_mssql.rs b/tests/sqlparser_mssql.rs index cadcabf035..fbd5b75ed0 100644 --- a/tests/sqlparser_mssql.rs +++ b/tests/sqlparser_mssql.rs @@ -2983,3 +2983,36 @@ fn parse_quoted_statement_keyword_aliases() { ms().verified_stmt("SELECT * FROM t [PRINT]"); ms().verified_stmt("SELECT * FROM t AS [PRINT]"); } + +#[test] +fn parse_mssql_while_single_statement_body() { + let stmts = ms() + .parse_sql_statements("WHILE @n < 10 SET @n = @n + 1; SELECT @n") + .unwrap(); + assert_eq!(stmts.len(), 2); + match &stmts[0] { + Statement::While(w) => assert_eq!(w.while_block.statements().len(), 1), + other => panic!("Expected While, got: {other:?}"), + } + assert!(matches!(&stmts[1], Statement::Query(_))); + + let stmts = tsql() + .parse_sql_statements("WHILE @n < 10\n SET @n = @n + 1\nSELECT @n") + .unwrap(); + assert_eq!(stmts.len(), 2); + assert!(matches!(&stmts[1], Statement::Query(_))); + + let stmts = ms() + .parse_sql_statements("WHILE @i < 3 WHILE @j < 3 SET @j = @j + 1; SELECT 1") + .unwrap(); + assert_eq!(stmts.len(), 2); + match &stmts[0] { + Statement::While(w) => { + assert!(matches!( + w.while_block.statements().as_slice(), + [Statement::While(_)] + )) + } + other => panic!("Expected While, got: {other:?}"), + } +} From cb8c3b559db60cb12ddc50274c6a9c8420270bec Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:27:41 -0800 Subject: [PATCH 2/3] Print a WHILE body without BEGIN without its semicolon, and test the round trip --- .../mssql_while_single_statement.sql | 4 +- src/ast/mod.rs | 16 +++++- tests/sqlparser_mssql.rs | 52 ++++++++++--------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/fuzz/fuzz_seeds/mssql_while_single_statement.sql b/fuzz/fuzz_seeds/mssql_while_single_statement.sql index 73a0831197..66b8b06ad2 100644 --- a/fuzz/fuzz_seeds/mssql_while_single_statement.sql +++ b/fuzz/fuzz_seeds/mssql_while_single_statement.sql @@ -1,3 +1 @@ -WHILE @n < 10 - SET @n = @n + 1 -SELECT @n \ No newline at end of file +WHILE @n < 10 SET @n = @n + 1; SELECT @n \ No newline at end of file diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 1f4ddd4de8..b12481838b 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -2805,8 +2805,20 @@ pub struct WhileStatement { impl fmt::Display for WhileStatement { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let WhileStatement { while_block } = self; - write!(f, "{while_block}")?; - Ok(()) + match &while_block.conditional_statements { + // A body without `BEGIN` is one statement, ended by the enclosing list's `;` + ConditionalStatements::Sequence { statements } => { + write!(f, "{}", while_block.start_token.0)?; + if let Some(condition) = &while_block.condition { + write!(f, " {condition}")?; + } + if !statements.is_empty() { + write!(f, " {}", display_separated(statements, "; "))?; + } + Ok(()) + } + ConditionalStatements::BeginEnd(_) => write!(f, "{while_block}"), + } } } diff --git a/tests/sqlparser_mssql.rs b/tests/sqlparser_mssql.rs index fbd5b75ed0..6507652909 100644 --- a/tests/sqlparser_mssql.rs +++ b/tests/sqlparser_mssql.rs @@ -1542,7 +1542,7 @@ fn test_mssql_cursor() { #[test] fn test_mssql_while_statement() { - let while_single_statement = "WHILE 1 = 0 PRINT 'Hello World';"; + let while_single_statement = "WHILE 1 = 0 PRINT 'Hello World'"; let stmt = ms().verified_stmt(while_single_statement); assert_eq!( stmt, @@ -2986,33 +2986,37 @@ fn parse_quoted_statement_keyword_aliases() { #[test] fn parse_mssql_while_single_statement_body() { - let stmts = ms() + let stmts = ms_and_generic() .parse_sql_statements("WHILE @n < 10 SET @n = @n + 1; SELECT @n") .unwrap(); - assert_eq!(stmts.len(), 2); - match &stmts[0] { - Statement::While(w) => assert_eq!(w.while_block.statements().len(), 1), - other => panic!("Expected While, got: {other:?}"), - } - assert!(matches!(&stmts[1], Statement::Query(_))); + let [Statement::While(w), Statement::Query(_)] = &stmts[..] else { + panic!("{stmts:?}") + }; + assert!(matches!( + w.while_block.statements()[..], + [Statement::Set(_)] + )); let stmts = tsql() - .parse_sql_statements("WHILE @n < 10\n SET @n = @n + 1\nSELECT @n") + .parse_sql_statements( + r#"WHILE @n < 10 + SET @n = @n + 1 +SELECT @n"#, + ) .unwrap(); - assert_eq!(stmts.len(), 2); - assert!(matches!(&stmts[1], Statement::Query(_))); + assert!(matches!( + stmts[..], + [Statement::While(_), Statement::Query(_)] + )); - let stmts = ms() - .parse_sql_statements("WHILE @i < 3 WHILE @j < 3 SET @j = @j + 1; SELECT 1") - .unwrap(); - assert_eq!(stmts.len(), 2); - match &stmts[0] { - Statement::While(w) => { - assert!(matches!( - w.while_block.statements().as_slice(), - [Statement::While(_)] - )) - } - other => panic!("Expected While, got: {other:?}"), - } + ms_and_generic().verified_stmt("WHILE @i < 3 WHILE @j < 3 SET @j = @j + 1"); + ms_and_generic().verified_stmt("WHILE 1 = 1 BEGIN WHILE 2 = 2 SELECT 1; SELECT 2; END"); + ms().verified_stmt("IF 1 = 1 WHILE @i < 3 SET @i = @i + 1; ELSE PRINT 'x';"); + + assert_eq!( + ms_and_generic().parse_sql_statements("WHILE @n < 10"), + Err(ParserError::ParserError( + "Expected: an SQL statement, found: EOF".to_string() + )) + ); } From 6bd13babe35d14a226e1a16e7a3da855d92764cd Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:31:37 -0800 Subject: [PATCH 3/3] Name the fuzz seed after the PR --- ...single_statement.sql => mssql_2992_while_single_statement.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fuzz/fuzz_seeds/{mssql_while_single_statement.sql => mssql_2992_while_single_statement.sql} (100%) diff --git a/fuzz/fuzz_seeds/mssql_while_single_statement.sql b/fuzz/fuzz_seeds/mssql_2992_while_single_statement.sql similarity index 100% rename from fuzz/fuzz_seeds/mssql_while_single_statement.sql rename to fuzz/fuzz_seeds/mssql_2992_while_single_statement.sql