From 7545e9be8fded8652ffcb1abf44937df4ac21adc Mon Sep 17 00:00:00 2001 From: "Heres, Daniel" Date: Mon, 28 Dec 2020 17:40:49 +0100 Subject: [PATCH 1/2] Support analyze table --- src/ast/mod.rs | 24 +++++++++++++++--------- src/parser.rs | 15 +++++++++++++++ tests/sqlparser_common.rs | 12 ++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 2d63bbfab..4232ad022 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -431,15 +431,6 @@ impl fmt::Display for WindowFrameBound { #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Statement { - // EXPLAIN - Explain { - // Carry out the command and show actual run times and other statistics. - analyze: bool, - // Display additional information regarding the plan. - verbose: bool, - /// A SQL query that specifies what to explain - statement: Box, - }, /// SELECT Query(Box), /// INSERT @@ -592,6 +583,20 @@ pub enum Statement { data_types: Vec, statement: Box, }, + /// EXPLAIN + Explain { + /// Carry out the command and show actual run times and other statistics. + analyze: bool, + // Display additional information regarding the plan. + verbose: bool, + /// A SQL query that specifies what to explain + statement: Box, + }, + /// ANALYZE + Analyze { + /// Name of table + table_name: ObjectName, + }, } impl fmt::Display for Statement { @@ -617,6 +622,7 @@ impl fmt::Display for Statement { write!(f, "{}", statement) } + Statement::Analyze { table_name } => write!(f, "ANALYZE TABLE {}", table_name), Statement::Query(s) => write!(f, "{}", s), Statement::Insert { table_name, diff --git a/src/parser.rs b/src/parser.rs index 0db093f93..0b14bde16 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -132,6 +132,7 @@ impl<'a> Parser<'a> { match self.next_token() { Token::Word(w) => match w.keyword { Keyword::EXPLAIN => Ok(self.parse_explain()?), + Keyword::ANALYZE => Ok(self.parse_analyze()?), Keyword::SELECT | Keyword::WITH | Keyword::VALUES => { self.prev_token(); Ok(Statement::Query(Box::new(self.parse_query()?))) @@ -1804,6 +1805,20 @@ impl<'a> Parser<'a> { }) } + pub fn parse_analyze(&mut self) -> Result { + // ANALYZE TABLE table_name + let t = self.expect_keyword(Keyword::TABLE)?; + + let table_name = self.parse_object_name()?; + println!("{:?}", t); + + println!("{:?}", table_name); + + Ok(Statement::Analyze { + table_name, + }) + } + /// Parse a query expression, i.e. a `SELECT` statement optionally /// preceeded with some `WITH` CTE declarations and optionally followed /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index a311dd267..e7d78f950 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -1639,6 +1639,18 @@ fn parse_explain_analyze_with_simple_select() { ); } +#[test] +fn parse_simple_analyze() { + let sql = "ANALYZE TABLE t"; + let stmt = verified_stmt(sql); + assert_eq!( + stmt, + Statement::Analyze { + table_name: ObjectName(vec![Ident::new("t")]) + } + ); +} + #[test] fn parse_named_argument_function() { let sql = "SELECT FUN(a => '1', b => '2') FROM foo"; From 288a8e6d26783ed33e7f26778359b975429a1c36 Mon Sep 17 00:00:00 2001 From: "Heres, Daniel" Date: Mon, 28 Dec 2020 17:46:05 +0100 Subject: [PATCH 2/2] Cleanup --- src/parser.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 0b14bde16..94afeb6e9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1807,16 +1807,11 @@ impl<'a> Parser<'a> { pub fn parse_analyze(&mut self) -> Result { // ANALYZE TABLE table_name - let t = self.expect_keyword(Keyword::TABLE)?; + self.expect_keyword(Keyword::TABLE)?; let table_name = self.parse_object_name()?; - println!("{:?}", t); - - println!("{:?}", table_name); - Ok(Statement::Analyze { - table_name, - }) + Ok(Statement::Analyze { table_name }) } /// Parse a query expression, i.e. a `SELECT` statement optionally