Skip to content

Commit 67f0bca

Browse files
Moved Analyze out of Statement enum
1 parent ade4082 commit 67f0bca

File tree

3 files changed

+95
-82
lines changed

3 files changed

+95
-82
lines changed

src/ast/mod.rs

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,6 +3060,59 @@ impl Display for ExceptionWhen {
30603060
}
30613061
}
30623062

3063+
/// ANALYZE TABLE statement (Hive-specific)
3064+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3065+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3066+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3067+
pub struct Analyze {
3068+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
3069+
pub table_name: ObjectName,
3070+
pub partitions: Option<Vec<Expr>>,
3071+
pub for_columns: bool,
3072+
pub columns: Vec<Ident>,
3073+
pub cache_metadata: bool,
3074+
pub noscan: bool,
3075+
pub compute_statistics: bool,
3076+
pub has_table_keyword: bool,
3077+
}
3078+
3079+
impl fmt::Display for Analyze {
3080+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3081+
write!(
3082+
f,
3083+
"ANALYZE{}{table_name}",
3084+
if self.has_table_keyword {
3085+
" TABLE "
3086+
} else {
3087+
" "
3088+
},
3089+
table_name = self.table_name
3090+
)?;
3091+
if let Some(ref parts) = self.partitions {
3092+
if !parts.is_empty() {
3093+
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
3094+
}
3095+
}
3096+
3097+
if self.compute_statistics {
3098+
write!(f, " COMPUTE STATISTICS")?;
3099+
}
3100+
if self.noscan {
3101+
write!(f, " NOSCAN")?;
3102+
}
3103+
if self.cache_metadata {
3104+
write!(f, " CACHE METADATA")?;
3105+
}
3106+
if self.for_columns {
3107+
write!(f, " FOR COLUMNS")?;
3108+
if !self.columns.is_empty() {
3109+
write!(f, " {}", display_comma_separated(&self.columns))?;
3110+
}
3111+
}
3112+
Ok(())
3113+
}
3114+
}
3115+
30633116
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
30643117
#[allow(clippy::large_enum_variant)]
30653118
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -3074,17 +3127,7 @@ pub enum Statement {
30743127
/// ANALYZE
30753128
/// ```
30763129
/// Analyze (Hive)
3077-
Analyze {
3078-
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
3079-
table_name: ObjectName,
3080-
partitions: Option<Vec<Expr>>,
3081-
for_columns: bool,
3082-
columns: Vec<Ident>,
3083-
cache_metadata: bool,
3084-
noscan: bool,
3085-
compute_statistics: bool,
3086-
has_table_keyword: bool,
3087-
},
3130+
Analyze(Analyze),
30883131
Set(Set),
30893132
/// ```sql
30903133
/// TRUNCATE
@@ -4324,6 +4367,12 @@ pub enum Statement {
43244367
Vacuum(VacuumStatement),
43254368
}
43264369

4370+
impl From<Analyze> for Statement {
4371+
fn from(analyze: Analyze) -> Self {
4372+
Statement::Analyze(analyze)
4373+
}
4374+
}
4375+
43274376
/// ```sql
43284377
/// {COPY | REVOKE} CURRENT GRANTS
43294378
/// ```
@@ -4633,44 +4682,7 @@ impl fmt::Display for Statement {
46334682
)?;
46344683
Ok(())
46354684
}
4636-
Statement::Analyze {
4637-
table_name,
4638-
partitions,
4639-
for_columns,
4640-
columns,
4641-
cache_metadata,
4642-
noscan,
4643-
compute_statistics,
4644-
has_table_keyword,
4645-
} => {
4646-
write!(
4647-
f,
4648-
"ANALYZE{}{table_name}",
4649-
if *has_table_keyword { " TABLE " } else { " " }
4650-
)?;
4651-
if let Some(ref parts) = partitions {
4652-
if !parts.is_empty() {
4653-
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
4654-
}
4655-
}
4656-
4657-
if *compute_statistics {
4658-
write!(f, " COMPUTE STATISTICS")?;
4659-
}
4660-
if *noscan {
4661-
write!(f, " NOSCAN")?;
4662-
}
4663-
if *cache_metadata {
4664-
write!(f, " CACHE METADATA")?;
4665-
}
4666-
if *for_columns {
4667-
write!(f, " FOR COLUMNS")?;
4668-
if !columns.is_empty() {
4669-
write!(f, " {}", display_comma_separated(columns))?;
4670-
}
4671-
}
4672-
Ok(())
4673-
}
4685+
Statement::Analyze(analyze) => analyze.fmt(f),
46744686
Statement::Insert(insert) => insert.fmt(f),
46754687
Statement::Install {
46764688
extension_name: name,

src/ast/spans.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ use crate::tokenizer::Span;
2525

2626
use super::{
2727
dcl::SecondaryRoles, value::ValueWithSpan, AccessExpr, AlterColumnOperation,
28-
AlterIndexOperation, AlterTableOperation, Array, Assignment, AssignmentTarget, AttachedToken,
29-
BeginEndStatements, CaseStatement, CloseCursor, ClusteredIndex, ColumnDef, ColumnOption,
30-
ColumnOptionDef, ConditionalStatementBlock, ConditionalStatements, ConflictTarget, ConnectBy,
31-
ConstraintCharacteristics, CopySource, CreateIndex, CreateTable, CreateTableOptions, Cte,
32-
Delete, DoUpdate, ExceptSelectItem, ExcludeSelectItem, Expr, ExprWithAlias, Fetch, FromTable,
33-
Function, FunctionArg, FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList,
34-
FunctionArguments, GroupByExpr, HavingBound, IfStatement, IlikeSelectItem, IndexColumn, Insert,
35-
Interpolate, InterpolateExpr, Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem,
36-
LateralView, LimitClause, MatchRecognizePattern, Measure, NamedParenthesizedList,
37-
NamedWindowDefinition, ObjectName, ObjectNamePart, Offset, OnConflict, OnConflictAction,
38-
OnInsert, OpenStatement, OrderBy, OrderByExpr, OrderByKind, Partition, PivotValueSource,
39-
ProjectionSelect, Query, RaiseStatement, RaiseStatementValue, ReferentialAction,
40-
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
41-
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
42-
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins,
43-
UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WhileStatement,
44-
WildcardAdditionalOptions, With, WithFill,
28+
AlterIndexOperation, AlterTableOperation, Analyze, Array, Assignment, AssignmentTarget,
29+
AttachedToken, BeginEndStatements, CaseStatement, CloseCursor, ClusteredIndex, ColumnDef,
30+
ColumnOption, ColumnOptionDef, ConditionalStatementBlock, ConditionalStatements,
31+
ConflictTarget, ConnectBy, ConstraintCharacteristics, CopySource, CreateIndex, CreateTable,
32+
CreateTableOptions, Cte, Delete, DoUpdate, ExceptSelectItem, ExcludeSelectItem, Expr,
33+
ExprWithAlias, Fetch, FromTable, Function, FunctionArg, FunctionArgExpr,
34+
FunctionArgumentClause, FunctionArgumentList, FunctionArguments, GroupByExpr, HavingBound,
35+
IfStatement, IlikeSelectItem, IndexColumn, Insert, Interpolate, InterpolateExpr, Join,
36+
JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView, LimitClause,
37+
MatchRecognizePattern, Measure, NamedParenthesizedList, NamedWindowDefinition, ObjectName,
38+
ObjectNamePart, Offset, OnConflict, OnConflictAction, OnInsert, OpenStatement, OrderBy,
39+
OrderByExpr, OrderByKind, Partition, PivotValueSource, ProjectionSelect, Query, RaiseStatement,
40+
RaiseStatementValue, ReferentialAction, RenameSelectItem, ReplaceSelectElement,
41+
ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SqlOption, Statement, Subscript,
42+
SymbolDefinition, TableAlias, TableAliasColumnDef, TableConstraint, TableFactor, TableObject,
43+
TableOptionsClustered, TableWithJoins, UpdateTableFromKind, Use, Value, Values, ViewColumnDef,
44+
WhileStatement, WildcardAdditionalOptions, With, WithFill,
4545
};
4646

4747
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -298,20 +298,7 @@ impl Spanned for Values {
298298
impl Spanned for Statement {
299299
fn span(&self) -> Span {
300300
match self {
301-
Statement::Analyze {
302-
table_name,
303-
partitions,
304-
for_columns: _,
305-
columns,
306-
cache_metadata: _,
307-
noscan: _,
308-
compute_statistics: _,
309-
has_table_keyword: _,
310-
} => union_spans(
311-
core::iter::once(table_name.span())
312-
.chain(partitions.iter().flat_map(|i| i.iter().map(|k| k.span())))
313-
.chain(columns.iter().map(|i| i.span)),
314-
),
301+
Statement::Analyze(analyze) => analyze.span(),
315302
Statement::Truncate {
316303
table_names,
317304
partitions,
@@ -944,6 +931,20 @@ impl Spanned for ConstraintCharacteristics {
944931
}
945932
}
946933

934+
impl Spanned for Analyze {
935+
fn span(&self) -> Span {
936+
union_spans(
937+
core::iter::once(self.table_name.span())
938+
.chain(
939+
self.partitions
940+
.iter()
941+
.flat_map(|i| i.iter().map(|k| k.span())),
942+
)
943+
.chain(self.columns.iter().map(|i| i.span)),
944+
)
945+
}
946+
}
947+
947948
/// # partial span
948949
///
949950
/// Missing spans:

src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ impl<'a> Parser<'a> {
11671167
}
11681168
}
11691169

1170-
Ok(Statement::Analyze {
1170+
Ok(Analyze {
11711171
has_table_keyword,
11721172
table_name,
11731173
for_columns,
@@ -1176,7 +1176,7 @@ impl<'a> Parser<'a> {
11761176
cache_metadata,
11771177
noscan,
11781178
compute_statistics,
1179-
})
1179+
}.into())
11801180
}
11811181

11821182
/// Parse a new expression including wildcard & qualified wildcard.

0 commit comments

Comments
 (0)