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 authored Dec 11, 2024
1 parent 3169861 commit 3782f88
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

3 comments on commit 3782f88

@github-actions
Copy link

@github-actions github-actions bot commented on 3782f88 Dec 11, 2024

Choose a reason for hiding this comment

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

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 3782f88 Dec 11, 2024

Choose a reason for hiding this comment

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

Callgrind benchmark results

Callgrind Benchmark Report

These benchmarks were run using callgrind,
an instruction-level profiler. They allow comparisons between sqlite (sqlite), SpacetimeDB running through a module (stdb_module), and the underlying SpacetimeDB data storage engine (stdb_raw). Callgrind emulates a CPU to collect the below estimates.

Measurement changes larger than five percent are in bold.

In-memory benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 6455 6455 0.00% 6555 6559 -0.06%
sqlite 5609 5609 0.00% 5951 6061 -1.81%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 1 u64 75267 75267 0.00% 75711 75763 -0.07%
stdb_raw u32_u64_str no_index 64 128 2 string 117551 117551 0.00% 118321 118327 -0.01%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 25072 25072 0.00% 25634 25648 -0.05%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 24039 24039 0.00% 24471 24475 -0.02%
sqlite u32_u64_str no_index 64 128 2 string 144351 144351 0.00% 145901 145821 0.05%
sqlite u32_u64_str no_index 64 128 1 u64 123742 123742 0.00% 125194 125052 0.11%
sqlite u32_u64_str btree_each_column 64 128 1 u64 131059 131059 0.00% 132687 132473 0.16%
sqlite u32_u64_str btree_each_column 64 128 2 string 134158 134158 0.00% 135824 135752 0.05%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 815455 814337 0.14% 832871 862377 -3.42%
stdb_raw u32_u64_str btree_each_column 64 128 1040515 1040919 -0.04% 1100193 1072649 2.57%
sqlite u32_u64_str unique_0 64 128 399360 399360 0.00% 415582 414108 0.36%
sqlite u32_u64_str btree_each_column 64 128 984611 984611 0.00% 1024099 1018945 0.51%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 138448 138448 0.00% 138564 138534 0.02%
stdb_raw u32_u64_str unique_0 64 15873 15873 0.00% 15981 15951 0.19%
sqlite u32_u64_str unique_0 1024 1042724 1042718 0.00% 1046110 1046082 0.00%
sqlite u32_u64_str unique_0 64 74704 74722 -0.02% 75892 75912 -0.03%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47528 47528 0.00% 50316 50180 0.27%
64 bsatn 25509 25509 0.00% 27787 27753 0.12%
16 bsatn 8200 8200 0.00% 9628 9594 0.35%
16 json 12188 12188 0.00% 14194 14092 0.72%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 19061720 19052516 0.05% 19597048 19567092 0.15%
stdb_raw u32_u64_str unique_0 64 128 1251271 1237102 1.15% 1318901 1274198 3.51%
sqlite u32_u64_str unique_0 1024 1024 1802155 1802137 0.00% 1811143 1811105 0.00%
sqlite u32_u64_str unique_0 64 128 128540 128540 0.00% 131306 131336 -0.02%
On-disk benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 6460 6460 0.00% 6564 6564 0.00%
sqlite 5651 5651 0.00% 6019 6141 -1.99%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 1 u64 75272 75272 0.00% 75716 75732 -0.02%
stdb_raw u32_u64_str no_index 64 128 2 string 117548 117556 -0.01% 118246 118276 -0.03%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 25111 25076 0.14% 25657 25688 -0.12%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 24044 24044 0.00% 24464 24456 0.03%
sqlite u32_u64_str no_index 64 128 1 u64 125663 125663 0.00% 127311 127241 0.06%
sqlite u32_u64_str no_index 64 128 2 string 146272 146272 0.00% 148038 148002 0.02%
sqlite u32_u64_str btree_each_column 64 128 2 string 136344 136344 0.00% 138354 138354 0.00%
sqlite u32_u64_str btree_each_column 64 128 1 u64 133155 133155 0.00% 135099 134985 0.08%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 763257 762654 0.08% 810647 809938 0.09%
stdb_raw u32_u64_str btree_each_column 64 128 989743 987474 0.23% 1048797 1049952 -0.11%
sqlite u32_u64_str unique_0 64 128 416908 416908 0.00% 432638 431052 0.37%
sqlite u32_u64_str btree_each_column 64 128 1023158 1023158 0.00% 1062388 1057352 0.48%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 138453 138453 0.00% 138549 138515 0.02%
stdb_raw u32_u64_str unique_0 64 15878 15878 0.00% 15978 15940 0.24%
sqlite u32_u64_str unique_0 1024 1045786 1045786 0.00% 1049538 1049620 -0.01%
sqlite u32_u64_str unique_0 64 76476 76476 0.00% 77788 77818 -0.04%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47528 47528 0.00% 50316 50180 0.27%
64 bsatn 25509 25509 0.00% 27787 27753 0.12%
16 bsatn 8200 8200 0.00% 9628 9594 0.35%
16 json 12188 12188 0.00% 14194 14092 0.72%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 17968185 17967555 0.00% 18550015 18562363 -0.07%
stdb_raw u32_u64_str unique_0 64 128 1191002 1190928 0.01% 1257534 1259674 -0.17%
sqlite u32_u64_str unique_0 1024 1024 1809785 1809785 0.00% 1818517 1818569 -0.00%
sqlite u32_u64_str unique_0 64 128 132687 132687 0.00% 135517 135591 -0.05%

@github-actions
Copy link

@github-actions github-actions bot commented on 3782f88 Dec 11, 2024

Choose a reason for hiding this comment

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

Criterion benchmark results

Error when comparing benchmarks:

Caused by:

Please sign in to comment.