Skip to content

Commit 68d5cbd

Browse files
committed
update
1 parent fad46e3 commit 68d5cbd

File tree

5 files changed

+79
-128
lines changed

5 files changed

+79
-128
lines changed

src/ast/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ pub use self::query::{
7676
AfterMatchSkip, ConnectBy, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
7777
ExceptSelectItem, ExcludeSelectItem, ExprWithAlias, ExprWithAliasAndOrderBy, Fetch, ForClause,
7878
ForJson, ForXml, FormatClause, GroupByExpr, GroupByWithModifier, IdentWithAlias,
79-
IdentsWithAlias, IlikeSelectItem, InputFormatClause, Interpolate, InterpolateExpr, Join,
80-
JoinConstraint, JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling,
81-
JsonTableNamedColumn, JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType,
82-
MatchRecognizePattern, MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr,
83-
NonBlock, Offset, OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind,
84-
OrderByOptions, PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
79+
IlikeSelectItem, InputFormatClause, Interpolate, InterpolateExpr, Join, JoinConstraint,
80+
JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, JsonTableNamedColumn,
81+
JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType, MatchRecognizePattern,
82+
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
83+
OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind, OrderByOptions,
84+
PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
8585
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
8686
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SetExpr, SetOperator,
8787
SetQuantifier, Setting, SymbolDefinition, Table, TableAlias, TableAliasColumnDef, TableFactor,

src/ast/query.rs

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -745,47 +745,6 @@ impl fmt::Display for IdentWithAlias {
745745
}
746746
}
747747

748-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
749-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
750-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
751-
pub struct IdentsWithAlias {
752-
pub idents: Vec<Ident>,
753-
pub alias: Option<Ident>,
754-
}
755-
756-
impl IdentsWithAlias {
757-
pub fn new(idents: Vec<Ident>, alias: Option<Ident>) -> Self {
758-
Self { idents, alias }
759-
}
760-
}
761-
762-
impl fmt::Display for IdentsWithAlias {
763-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
764-
match self.idents.len() {
765-
0 => Ok(()),
766-
1 => {
767-
if let Some(alias) = &self.alias {
768-
write!(f, "{} AS {}", self.idents[0], alias)
769-
} else {
770-
write!(f, "{}", self.idents[0])
771-
}
772-
}
773-
_ => {
774-
if let Some(alias) = &self.alias {
775-
write!(
776-
f,
777-
"({}) AS {}",
778-
display_comma_separated(&self.idents),
779-
alias
780-
)
781-
} else {
782-
write!(f, "({})", display_comma_separated(&self.idents))
783-
}
784-
}
785-
}
786-
}
787-
}
788-
789748
/// Additional options for wildcards, e.g. Snowflake `EXCLUDE`/`RENAME` and Bigquery `EXCEPT`.
790749
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
791750
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -1390,11 +1349,12 @@ pub enum TableFactor {
13901349
/// ```
13911350
///
13921351
/// See <https://docs.snowflake.com/en/sql-reference/constructs/unpivot>.
1352+
/// See <https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot>.
13931353
Unpivot {
13941354
table: Box<TableFactor>,
1395-
value: Vec<Ident>,
1355+
value: Expr,
13961356
name: Ident,
1397-
columns: Vec<IdentsWithAlias>,
1357+
columns: Vec<ExprWithAlias>,
13981358
null_inclusion: Option<NullInclusion>,
13991359
alias: Option<TableAlias>,
14001360
},
@@ -2076,17 +2036,10 @@ impl fmt::Display for TableFactor {
20762036
if let Some(null_inclusion) = null_inclusion {
20772037
write!(f, " {null_inclusion} ")?;
20782038
}
2079-
write!(f, "(")?;
2080-
if value.len() == 1 {
2081-
// single value column unpivot
2082-
write!(f, "{}", value[0])?;
2083-
} else {
2084-
// multi value column unpivot
2085-
write!(f, "({})", display_comma_separated(value))?;
2086-
}
20872039
write!(
20882040
f,
2089-
" FOR {} IN ({}))",
2041+
"({} FOR {} IN ({}))",
2042+
value,
20902043
name,
20912044
display_comma_separated(columns)
20922045
)?;

src/ast/spans.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,15 +1985,9 @@ impl Spanned for TableFactor {
19851985
alias,
19861986
} => union_spans(
19871987
core::iter::once(table.span())
1988-
.chain(value.iter().map(|i| i.span))
1988+
.chain(core::iter::once(value.span()))
19891989
.chain(core::iter::once(name.span))
1990-
.chain(columns.iter().flat_map(|ilist| {
1991-
ilist
1992-
.idents
1993-
.iter()
1994-
.map(|i| i.span)
1995-
.chain(ilist.alias.as_ref().map(|a| a.span))
1996-
}))
1990+
.chain(columns.iter().map(|ilist| ilist.span()))
19971991
.chain(alias.as_ref().map(|alias| alias.span())),
19981992
),
19991993
TableFactor::MatchRecognize {

src/parser/mod.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10810,26 +10810,13 @@ impl<'a> Parser<'a> {
1081010810
}
1081110811
}
1081210812

10813-
pub fn parse_identifiers_with_alias(&mut self) -> Result<IdentsWithAlias, ParserError> {
10814-
let idents = match self.peek_token_ref().token {
10815-
Token::LParen => self.parse_parenthesized_column_list(Mandatory, false)?,
10816-
_ => vec![self.parse_identifier()?],
10817-
};
10818-
let alias = if self.parse_keyword(Keyword::AS) {
10819-
Some(self.parse_identifier()?)
10820-
} else {
10821-
None
10822-
};
10823-
Ok(IdentsWithAlias { idents, alias })
10824-
}
10825-
1082610813
pub fn parse_parenthesized_columns_with_alias_list(
1082710814
&mut self,
1082810815
optional: IsOptional,
1082910816
allow_empty: bool,
10830-
) -> Result<Vec<IdentsWithAlias>, ParserError> {
10817+
) -> Result<Vec<ExprWithAlias>, ParserError> {
1083110818
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
10832-
p.parse_identifiers_with_alias()
10819+
p.parse_expr_with_alias()
1083310820
})
1083410821
}
1083510822

@@ -13908,11 +13895,16 @@ impl<'a> Parser<'a> {
1390813895
let value = match self.peek_token_ref().token {
1390913896
Token::LParen => {
1391013897
// multi value column unpivot
13911-
self.parse_parenthesized_column_list(Mandatory, false)?
13898+
Expr::Tuple(
13899+
self.parse_parenthesized_column_list(Mandatory, false)?
13900+
.into_iter()
13901+
.map(|col| Expr::Identifier(col))
13902+
.collect(),
13903+
)
1391213904
}
1391313905
_ => {
1391413906
// single value column unpivot
13915-
vec![self.parse_identifier()?]
13907+
Expr::Identifier(self.parse_identifier()?)
1391613908
}
1391713909
};
1391813910
self.expect_keyword_is(Keyword::FOR)?;

tests/sqlparser_common.rs

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10947,20 +10947,14 @@ fn parse_unpivot_table() {
1094710947
index_hints: vec![],
1094810948
}),
1094910949
null_inclusion: None,
10950-
value: vec![Ident {
10951-
value: "quantity".to_string(),
10952-
quote_style: None,
10953-
span: Span::empty(),
10954-
}],
10955-
10956-
name: Ident {
10957-
value: "quarter".to_string(),
10958-
quote_style: None,
10959-
span: Span::empty(),
10960-
},
10950+
value: Expr::Identifier(Ident::new("quantity")),
10951+
name: Ident::new("quarter"),
1096110952
columns: ["Q1", "Q2", "Q3", "Q4"]
1096210953
.into_iter()
10963-
.map(|col| IdentsWithAlias::new(vec![Ident::new(col)], None))
10954+
.map(|col| ExprWithAlias {
10955+
expr: Expr::Identifier(Ident::new(col)),
10956+
alias: None,
10957+
})
1096410958
.collect(),
1096510959
alias: Some(TableAlias {
1096610960
name: Ident::new("u"),
@@ -11024,31 +11018,46 @@ fn parse_unpivot_table() {
1102411018
);
1102511019

1102611020
let sql_unpivot_with_alias = concat!(
11027-
"SELECT * FROM sales AS s ",
11028-
"UNPIVOT INCLUDE NULLS (quantity FOR quarter IN (Q1 AS Quater1, Q2 AS Quater2, Q3 AS Quater3, Q4 AS Quater4)) AS u (product, quarter, quantity)"
11029-
);
11021+
"SELECT * FROM sales AS s ",
11022+
"UNPIVOT INCLUDE NULLS ",
11023+
"(quantity FOR quarter IN ",
11024+
"(Q1 AS Quater1, Q2 AS Quater2, Q3 AS Quater3, Q4 AS Quater4)) ",
11025+
"AS u (product, quarter, quantity)"
11026+
);
1103011027

1103111028
if let Unpivot { value, columns, .. } =
1103211029
&verified_only_select(sql_unpivot_with_alias).from[0].relation
1103311030
{
1103411031
assert_eq!(
1103511032
*columns,
1103611033
vec![
11037-
IdentsWithAlias::new(vec![Ident::new("Q1")], Some(Ident::new("Quater1"))),
11038-
IdentsWithAlias::new(vec![Ident::new("Q2")], Some(Ident::new("Quater2"))),
11039-
IdentsWithAlias::new(vec![Ident::new("Q3")], Some(Ident::new("Quater3"))),
11040-
IdentsWithAlias::new(vec![Ident::new("Q4")], Some(Ident::new("Quater4"))),
11034+
ExprWithAlias {
11035+
expr: Expr::Identifier(Ident::new("Q1")),
11036+
alias: Some(Ident::new("Quater1")),
11037+
},
11038+
ExprWithAlias {
11039+
expr: Expr::Identifier(Ident::new("Q2")),
11040+
alias: Some(Ident::new("Quater2")),
11041+
},
11042+
ExprWithAlias {
11043+
expr: Expr::Identifier(Ident::new("Q3")),
11044+
alias: Some(Ident::new("Quater3")),
11045+
},
11046+
ExprWithAlias {
11047+
expr: Expr::Identifier(Ident::new("Q4")),
11048+
alias: Some(Ident::new("Quater4")),
11049+
},
1104111050
]
1104211051
);
11043-
assert_eq!(*value, vec![Ident::new("quantity")]);
11052+
assert_eq!(*value, Expr::Identifier(Ident::new("quantity")));
1104411053
}
1104511054

1104611055
assert_eq!(
1104711056
verified_stmt(sql_unpivot_with_alias).to_string(),
1104811057
sql_unpivot_with_alias
1104911058
);
1105011059

11051-
let sql_unpivot_with_alias = concat!(
11060+
let sql_unpivot_with_alias_and_multi_value = concat!(
1105211061
"SELECT * FROM sales AS s ",
1105311062
"UNPIVOT INCLUDE NULLS ((first_quarter, second_quarter) ",
1105411063
"FOR half_of_the_year IN (",
@@ -11058,30 +11067,39 @@ fn parse_unpivot_table() {
1105811067
);
1105911068

1106011069
if let Unpivot { value, columns, .. } =
11061-
&verified_only_select(sql_unpivot_with_alias).from[0].relation
11070+
&verified_only_select(sql_unpivot_with_alias_and_multi_value).from[0].relation
1106211071
{
1106311072
assert_eq!(
1106411073
*columns,
1106511074
vec![
11066-
IdentsWithAlias::new(
11067-
vec![Ident::new("Q1"), Ident::new("Q2")],
11068-
Some(Ident::new("H1"))
11069-
),
11070-
IdentsWithAlias::new(
11071-
vec![Ident::new("Q3"), Ident::new("Q4")],
11072-
Some(Ident::new("H2"))
11073-
),
11075+
ExprWithAlias {
11076+
expr: Expr::Tuple(vec![
11077+
Expr::Identifier(Ident::new("Q1")),
11078+
Expr::Identifier(Ident::new("Q2")),
11079+
]),
11080+
alias: Some(Ident::new("H1")),
11081+
},
11082+
ExprWithAlias {
11083+
expr: Expr::Tuple(vec![
11084+
Expr::Identifier(Ident::new("Q3")),
11085+
Expr::Identifier(Ident::new("Q4")),
11086+
]),
11087+
alias: Some(Ident::new("H2")),
11088+
},
1107411089
]
1107511090
);
1107611091
assert_eq!(
1107711092
*value,
11078-
vec![Ident::new("first_quarter"), Ident::new("second_quarter")]
11093+
Expr::Tuple(vec![
11094+
Expr::Identifier(Ident::new("first_quarter")),
11095+
Expr::Identifier(Ident::new("second_quarter")),
11096+
])
1107911097
);
1108011098
}
1108111099

1108211100
assert_eq!(
11083-
verified_stmt(sql_unpivot_with_alias).to_string(),
11084-
sql_unpivot_with_alias
11101+
verified_stmt(sql_unpivot_with_alias_and_multi_value).to_string(),
11102+
sql_unpivot_with_alias_and_multi_value
1108511103
);
1108611104
}
1108711105

@@ -11180,20 +11198,14 @@ fn parse_pivot_unpivot_table() {
1118011198
index_hints: vec![],
1118111199
}),
1118211200
null_inclusion: None,
11183-
value: vec![Ident {
11184-
value: "population".to_string(),
11185-
quote_style: None,
11186-
span: Span::empty()
11187-
}],
11188-
11189-
name: Ident {
11190-
value: "year".to_string(),
11191-
quote_style: None,
11192-
span: Span::empty()
11193-
},
11201+
value: Expr::Identifier(Ident::new("population")),
11202+
name: Ident::new("year"),
1119411203
columns: ["population_2000", "population_2010"]
1119511204
.into_iter()
11196-
.map(|col| IdentsWithAlias::new(vec![Ident::new(col)], None))
11205+
.map(|col| ExprWithAlias {
11206+
expr: Expr::Identifier(Ident::new(col)),
11207+
alias: None,
11208+
})
1119711209
.collect(),
1119811210
alias: Some(TableAlias {
1119911211
name: Ident::new("u"),

0 commit comments

Comments
 (0)