Skip to content

Support optional table for ANALYZE statement #1599

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 3 commits into from
Dec 19, 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
8 changes: 7 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,7 @@ pub enum Statement {
cache_metadata: bool,
noscan: bool,
compute_statistics: bool,
has_table_keyword: bool,
},
/// ```sql
/// TRUNCATE
Expand Down Expand Up @@ -3641,8 +3642,13 @@ impl fmt::Display for Statement {
cache_metadata,
noscan,
compute_statistics,
has_table_keyword,
} => {
write!(f, "ANALYZE TABLE {table_name}")?;
write!(
f,
"ANALYZE{}{table_name}",
if *has_table_keyword { " TABLE " } else { " " }
)?;
if let Some(ref parts) = partitions {
if !parts.is_empty() {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl Spanned for Statement {
cache_metadata: _,
noscan: _,
compute_statistics: _,
has_table_keyword: _,
} => union_spans(
core::iter::once(table_name.span())
.chain(partitions.iter().flat_map(|i| i.iter().map(|k| k.span())))
Expand Down
3 changes: 2 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'a> Parser<'a> {
}

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

Ok(Statement::Analyze {
has_table_keyword,
table_name,
for_columns,
columns,
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ fn parse_select_with_table_alias() {
);
}

#[test]
fn parse_analyze() {
verified_stmt("ANALYZE TABLE test_table");
verified_stmt("ANALYZE test_table");
}

#[test]
fn parse_invalid_table_name() {
let ast = all_dialects().run_parser_method("db.public..customer", |parser| {
Expand Down
Loading