Skip to content

Support parsing EXPLAIN ESTIMATE of Clickhouse #1605

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 1 commit into from
Dec 17, 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
7 changes: 7 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,9 @@ pub enum Statement {
///
/// [SQLite](https://sqlite.org/lang_explain.html)
query_plan: bool,
/// `EXPLAIN ESTIMATE`
/// [Clickhouse](https://clickhouse.com/docs/en/sql-reference/statements/explain#explain-estimate)
estimate: bool,
/// A SQL query that specifies what to explain
statement: Box<Statement>,
/// Optional output format of explain
Expand Down Expand Up @@ -3471,6 +3474,7 @@ impl fmt::Display for Statement {
verbose,
analyze,
query_plan,
estimate,
statement,
format,
options,
Expand All @@ -3483,6 +3487,9 @@ impl fmt::Display for Statement {
if *analyze {
write!(f, "ANALYZE ")?;
}
if *estimate {
write!(f, "ESTIMATE ")?;
}

if *verbose {
write!(f, "VERBOSE ")?;
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ define_keywords!(
ERROR,
ESCAPE,
ESCAPED,
ESTIMATE,
EVENT,
EVERY,
EXCEPT,
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9087,6 +9087,7 @@ impl<'a> Parser<'a> {
let mut analyze = false;
let mut verbose = false;
let mut query_plan = false;
let mut estimate = false;
let mut format = None;
let mut options = None;

Expand All @@ -9099,6 +9100,8 @@ impl<'a> Parser<'a> {
options = Some(self.parse_utility_options()?)
} else if self.parse_keywords(&[Keyword::QUERY, Keyword::PLAN]) {
query_plan = true;
} else if self.parse_keyword(Keyword::ESTIMATE) {
estimate = true;
} else {
analyze = self.parse_keyword(Keyword::ANALYZE);
verbose = self.parse_keyword(Keyword::VERBOSE);
Expand All @@ -9116,6 +9119,7 @@ impl<'a> Parser<'a> {
analyze,
verbose,
query_plan,
estimate,
statement: Box::new(statement),
format,
options,
Expand Down
30 changes: 30 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4375,6 +4375,7 @@ fn run_explain_analyze(
analyze,
verbose,
query_plan,
estimate,
statement,
format,
options,
Expand All @@ -4384,6 +4385,7 @@ fn run_explain_analyze(
assert_eq!(format, expected_format);
assert_eq!(options, exepcted_options);
assert!(!query_plan);
assert!(!estimate);
assert_eq!("SELECT sqrt(id) FROM foo", statement.to_string());
}
_ => panic!("Unexpected Statement, must be Explain"),
Expand Down Expand Up @@ -4528,6 +4530,34 @@ fn parse_explain_query_plan() {
);
}

#[test]
fn parse_explain_estimate() {
let statement = all_dialects().verified_stmt("EXPLAIN ESTIMATE SELECT sqrt(id) FROM foo");

match &statement {
Statement::Explain {
query_plan,
estimate,
analyze,
verbose,
statement,
..
} => {
assert!(estimate);
assert!(!query_plan);
assert!(!analyze);
assert!(!verbose);
assert_eq!("SELECT sqrt(id) FROM foo", statement.to_string());
}
_ => unreachable!(),
}

assert_eq!(
"EXPLAIN ESTIMATE SELECT sqrt(id) FROM foo",
statement.to_string()
);
}

#[test]
fn parse_named_argument_function() {
let dialects = all_dialects_where(|d| {
Expand Down
Loading