Skip to content

Support optional semicolon between statements #1937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,17 @@ pub struct ParserOptions {
/// Controls how literal values are unescaped. See
/// [`Tokenizer::with_unescape`] for more details.
pub unescape: bool,
/// Controls if the parser expects a semi-colon token
/// between statements. Default is `true`.
pub require_semicolon_stmt_delimiter: bool,
}

impl Default for ParserOptions {
fn default() -> Self {
Self {
trailing_commas: false,
unescape: true,
require_semicolon_stmt_delimiter: true,
}
}
}
Expand Down Expand Up @@ -467,6 +471,10 @@ impl<'a> Parser<'a> {
expecting_statement_delimiter = false;
}

if !self.options.require_semicolon_stmt_delimiter {
expecting_statement_delimiter = false;
}

match self.peek_token().token {
Token::EOF => break,

Expand Down
5 changes: 5 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ pub fn all_dialects() -> TestedDialects {
])
}

// Returns all available dialects with the specified parser options
pub fn all_dialects_with_options(options: ParserOptions) -> TestedDialects {
TestedDialects::new_with_options(all_dialects().dialects, options)
}

/// Returns all dialects matching the given predicate.
pub fn all_dialects_where<F>(predicate: F) -> TestedDialects
where
Expand Down
22 changes: 20 additions & 2 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ use sqlparser::parser::{Parser, ParserError, ParserOptions};
use sqlparser::tokenizer::Tokenizer;
use sqlparser::tokenizer::{Location, Span};
use test_utils::{
all_dialects, all_dialects_where, alter_table_op, assert_eq_vec, call, expr_from_projection,
join, number, only, table, table_alias, table_from_name, TestedDialects,
all_dialects, all_dialects_where, all_dialects_with_options, alter_table_op, assert_eq_vec,
call, expr_from_projection, join, number, only, table, table_alias, table_from_name,
TestedDialects,
};

#[macro_use]
Expand Down Expand Up @@ -16115,3 +16116,20 @@ fn test_select_exclude() {
ParserError::ParserError("Expected: end of statement, found: EXCLUDE".to_string())
);
}

#[test]
fn test_no_semicolon_required_between_statements() {
let sql = r#"
SELECT * FROM tbl1
SELECT * FROM tbl2
"#;

let dialects = all_dialects_with_options(ParserOptions {
trailing_commas: false,
unescape: true,
require_semicolon_stmt_delimiter: false,
});
let stmts = dialects.parse_sql_statements(sql).unwrap();
assert_eq!(stmts.len(), 2);
assert!(stmts.iter().all(|s| matches!(s, Statement::Query { .. })));
}
26 changes: 25 additions & 1 deletion tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use sqlparser::ast::DeclareAssignment::MsSqlAssignment;
use sqlparser::ast::Value::SingleQuotedString;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, MsSqlDialect};
use sqlparser::parser::{Parser, ParserError};
use sqlparser::parser::{Parser, ParserError, ParserOptions};

#[test]
fn parse_mssql_identifiers() {
Expand Down Expand Up @@ -2327,6 +2327,18 @@ fn ms() -> TestedDialects {
TestedDialects::new(vec![Box::new(MsSqlDialect {})])
}

// MS SQL dialect with support for optional semi-colon statement delimiters
fn tsql() -> TestedDialects {
TestedDialects::new_with_options(
vec![Box::new(MsSqlDialect {})],
ParserOptions {
trailing_commas: false,
unescape: true,
require_semicolon_stmt_delimiter: false,
},
)
}

fn ms_and_generic() -> TestedDialects {
TestedDialects::new(vec![Box::new(MsSqlDialect {}), Box::new(GenericDialect {})])
}
Expand Down Expand Up @@ -2483,3 +2495,15 @@ fn parse_mssql_grant() {
fn parse_mssql_deny() {
ms().verified_stmt("DENY SELECT ON my_table TO public, db_admin");
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we have a similar test or move this over to common.rs? thinking since this flag is being added to the parser itself, it feels a bit like it could be lost if only tested here within mssql

fn test_tsql_no_semicolon_delimiter() {
let sql = r#"
DECLARE @X AS NVARCHAR(MAX)='x'
DECLARE @Y AS NVARCHAR(MAX)='y'
"#;

let stmts = tsql().parse_sql_statements(sql).unwrap();
assert_eq!(stmts.len(), 2);
assert!(stmts.iter().all(|s| matches!(s, Statement::Declare { .. })));
}
1 change: 1 addition & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
ParserOptions {
trailing_commas: false,
unescape: false,
require_semicolon_stmt_delimiter: true,
}
)
.verified_stmt(sql),
Expand Down