Skip to content

Fix performance of very nested SQL files, add example to benchmark #217

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

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 13 additions & 0 deletions sqlparser_bench/benches/sqlparser_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ fn basic_queries(c: &mut Criterion) {
group.bench_function("sqlparser::with_select", |b| {
b.iter(|| Parser::parse_sql(&dialect, with_query));
});

let nested_query = "
SELECT
@ FROM((((((((((SELECT
@ FROM((((((SELECT
@ FROM((((((((((SELECT
@ FROM(((((((((((((SELECT
I FROM(((((((SELECT
I FROM
";
group.bench_function("sqlparser::nested_query", |b| {
b.iter(|| Parser::parse_sql(&dialect, nested_query));
});
}

criterion_group!(benches, basic_queries);
Expand Down
26 changes: 21 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use super::dialect::keywords;
use super::dialect::keywords::Keyword;
use super::dialect::Dialect;
use super::tokenizer::*;
use std::collections::HashSet;
use std::error::Error;
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub enum ParserError {
TokenizerError(String),
Expand Down Expand Up @@ -87,12 +87,18 @@ pub struct Parser {
tokens: Vec<Token>,
/// The index of the first unprocessed token in `self.tokens`
index: usize,
/// Memoizes unsuccesful `parse_derived_table_factor` results
memoize_parse_derived_table_factor: HashSet<usize>,
}

impl Parser {
/// Parse the specified tokens
pub fn new(tokens: Vec<Token>) -> Self {
Parser { tokens, index: 0 }
Parser {
tokens,
index: 0,
memoize_parse_derived_table_factor: HashSet::new(),
}
}

/// Parse a SQL statement and produce an Abstract Syntax Tree (AST)
Expand Down Expand Up @@ -2055,9 +2061,19 @@ impl Parser {
// `parse_derived_table_factor` below will return success after parsing the
// subquery, followed by the closing ')', and the alias of the derived table.
// In the example above this is case (3).
return_ok_if_some!(
self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
);

// We will only try to parse `parse_derived_table_factor` if it wasn't tried in a
// previous attempt for the same index
if !self
.memoize_parse_derived_table_factor
.contains(&self.index)
{
return_ok_if_some!(
self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
);
self.memoize_parse_derived_table_factor.insert(self.index);
}

// A parsing error from `parse_derived_table_factor` indicates that the '(' we've
// recently consumed does not start a derived table (cases 1, 2, or 4).
// `maybe_parse` will ignore such an error and rewind to be after the opening '('.
Expand Down