Skip to content

Commit 94ff468

Browse files
authored
Support ANALYZE TABLE syntax (andialbrecht#285)
* Support analyze table * Cleanup
1 parent 17f2930 commit 94ff468

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/ast/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,15 +431,6 @@ impl fmt::Display for WindowFrameBound {
431431
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
432432
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
433433
pub enum Statement {
434-
// EXPLAIN
435-
Explain {
436-
// Carry out the command and show actual run times and other statistics.
437-
analyze: bool,
438-
// Display additional information regarding the plan.
439-
verbose: bool,
440-
/// A SQL query that specifies what to explain
441-
statement: Box<Statement>,
442-
},
443434
/// SELECT
444435
Query(Box<Query>),
445436
/// INSERT
@@ -592,6 +583,20 @@ pub enum Statement {
592583
data_types: Vec<DataType>,
593584
statement: Box<Statement>,
594585
},
586+
/// EXPLAIN
587+
Explain {
588+
/// Carry out the command and show actual run times and other statistics.
589+
analyze: bool,
590+
// Display additional information regarding the plan.
591+
verbose: bool,
592+
/// A SQL query that specifies what to explain
593+
statement: Box<Statement>,
594+
},
595+
/// ANALYZE
596+
Analyze {
597+
/// Name of table
598+
table_name: ObjectName,
599+
},
595600
}
596601

597602
impl fmt::Display for Statement {
@@ -617,6 +622,7 @@ impl fmt::Display for Statement {
617622

618623
write!(f, "{}", statement)
619624
}
625+
Statement::Analyze { table_name } => write!(f, "ANALYZE TABLE {}", table_name),
620626
Statement::Query(s) => write!(f, "{}", s),
621627
Statement::Insert {
622628
table_name,

src/parser.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl<'a> Parser<'a> {
132132
match self.next_token() {
133133
Token::Word(w) => match w.keyword {
134134
Keyword::EXPLAIN => Ok(self.parse_explain()?),
135+
Keyword::ANALYZE => Ok(self.parse_analyze()?),
135136
Keyword::SELECT | Keyword::WITH | Keyword::VALUES => {
136137
self.prev_token();
137138
Ok(Statement::Query(Box::new(self.parse_query()?)))
@@ -1804,6 +1805,15 @@ impl<'a> Parser<'a> {
18041805
})
18051806
}
18061807

1808+
pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
1809+
// ANALYZE TABLE table_name
1810+
self.expect_keyword(Keyword::TABLE)?;
1811+
1812+
let table_name = self.parse_object_name()?;
1813+
1814+
Ok(Statement::Analyze { table_name })
1815+
}
1816+
18071817
/// Parse a query expression, i.e. a `SELECT` statement optionally
18081818
/// preceeded with some `WITH` CTE declarations and optionally followed
18091819
/// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't

tests/sqlparser_common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,18 @@ fn parse_explain_analyze_with_simple_select() {
16391639
);
16401640
}
16411641

1642+
#[test]
1643+
fn parse_simple_analyze() {
1644+
let sql = "ANALYZE TABLE t";
1645+
let stmt = verified_stmt(sql);
1646+
assert_eq!(
1647+
stmt,
1648+
Statement::Analyze {
1649+
table_name: ObjectName(vec![Ident::new("t")])
1650+
}
1651+
);
1652+
}
1653+
16421654
#[test]
16431655
fn parse_named_argument_function() {
16441656
let sql = "SELECT FUN(a => '1', b => '2') FROM foo";

0 commit comments

Comments
 (0)