Skip to content

Commit

Permalink
Fix some places where trying to parse an empty string could panic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdt committed Dec 10, 2024
1 parent 3169861 commit 1010701
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
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

0 comments on commit 1010701

Please sign in to comment.