Skip to content
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

Fix some places where trying to parse an empty string could panic. #2051

Merged
merged 1 commit into from
Dec 11, 2024
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
2 changes: 2 additions & 0 deletions crates/sql-parser/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub enum SqlUnsupported {
MultiStatement,
#[error("Multi-table DELETE is not supported")]
MultiTableDelete,
#[error("Empty SQL query")]
Empty,
}

impl SqlUnsupported {
Expand Down
6 changes: 6 additions & 0 deletions crates/sql-parser/src/parser/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub fn parse_sql(sql: &str) -> SqlParseResult<SqlAst> {
if stmts.len() > 1 {
return Err(SqlUnsupported::MultiStatement.into());
}
if stmts.is_empty() {
return Err(SqlUnsupported::Empty.into());
}
parse_statement(stmts.swap_remove(0))
}

Expand Down Expand Up @@ -519,6 +522,9 @@ mod tests {
"select a from t where",
// Empty GROUP BY
"select a, count(*) from t group by",
// Empty statement
"",
" ",
] {
assert!(parse_sql(sql).is_err());
}
Expand Down
9 changes: 6 additions & 3 deletions crates/sql-parser/src/parser/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ use super::{
/// Parse a SQL string
pub fn parse_subscription(sql: &str) -> SqlParseResult<SqlAst> {
let mut stmts = Parser::parse_sql(&PostgreSqlDialect {}, sql)?;
if stmts.len() > 1 {
return Err(SqlUnsupported::MultiStatement.into());
match stmts.len() {
0 => Err(SqlUnsupported::Empty.into()),
1 => parse_statement(stmts.swap_remove(0)),
_ => Err(SqlUnsupported::MultiStatement.into()),
}
parse_statement(stmts.swap_remove(0))
}

/// Parse a SQL query
Expand Down Expand Up @@ -174,6 +175,8 @@ mod tests {
fn unsupported() {
for sql in [
"delete from t",
" ",
"",
"select distinct a from t",
"select * from (select * from t) join (select * from s) on a = b",
] {
Expand Down
Loading