Skip to content

Commit eae5629

Browse files
authored
Support optional table for ANALYZE statement (#1599)
1 parent 6523dab commit eae5629

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

src/ast/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,7 @@ pub enum Statement {
23542354
cache_metadata: bool,
23552355
noscan: bool,
23562356
compute_statistics: bool,
2357+
has_table_keyword: bool,
23572358
},
23582359
/// ```sql
23592360
/// TRUNCATE
@@ -3651,8 +3652,13 @@ impl fmt::Display for Statement {
36513652
cache_metadata,
36523653
noscan,
36533654
compute_statistics,
3655+
has_table_keyword,
36543656
} => {
3655-
write!(f, "ANALYZE TABLE {table_name}")?;
3657+
write!(
3658+
f,
3659+
"ANALYZE{}{table_name}",
3660+
if *has_table_keyword { " TABLE " } else { " " }
3661+
)?;
36563662
if let Some(ref parts) = partitions {
36573663
if !parts.is_empty() {
36583664
write!(f, " PARTITION ({})", display_comma_separated(parts))?;

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ impl Spanned for Statement {
284284
cache_metadata: _,
285285
noscan: _,
286286
compute_statistics: _,
287+
has_table_keyword: _,
287288
} => union_spans(
288289
core::iter::once(table_name.span())
289290
.chain(partitions.iter().flat_map(|i| i.iter().map(|k| k.span())))

src/parser/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl<'a> Parser<'a> {
851851
}
852852

853853
pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
854-
self.expect_keyword(Keyword::TABLE)?;
854+
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
855855
let table_name = self.parse_object_name(false)?;
856856
let mut for_columns = false;
857857
let mut cache_metadata = false;
@@ -896,6 +896,7 @@ impl<'a> Parser<'a> {
896896
}
897897

898898
Ok(Statement::Analyze {
899+
has_table_keyword,
899900
table_name,
900901
for_columns,
901902
columns,

tests/sqlparser_common.rs

+6
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ fn parse_select_with_table_alias() {
562562
);
563563
}
564564

565+
#[test]
566+
fn parse_analyze() {
567+
verified_stmt("ANALYZE TABLE test_table");
568+
verified_stmt("ANALYZE test_table");
569+
}
570+
565571
#[test]
566572
fn parse_invalid_table_name() {
567573
let ast = all_dialects().run_parser_method("db.public..customer", |parser| {

0 commit comments

Comments
 (0)