@@ -11009,20 +11009,14 @@ fn parse_unpivot_table() {
11009
11009
index_hints: vec![],
11010
11010
}),
11011
11011
null_inclusion: None,
11012
- value: Ident {
11013
- value: "quantity".to_string(),
11014
- quote_style: None,
11015
- span: Span::empty(),
11016
- },
11017
-
11018
- name: Ident {
11019
- value: "quarter".to_string(),
11020
- quote_style: None,
11021
- span: Span::empty(),
11022
- },
11012
+ value: Expr::Identifier(Ident::new("quantity")),
11013
+ name: Ident::new("quarter"),
11023
11014
columns: ["Q1", "Q2", "Q3", "Q4"]
11024
11015
.into_iter()
11025
- .map(Ident::new)
11016
+ .map(|col| ExprWithAlias {
11017
+ expr: Expr::Identifier(Ident::new(col)),
11018
+ alias: None,
11019
+ })
11026
11020
.collect(),
11027
11021
alias: Some(TableAlias {
11028
11022
name: Ident::new("u"),
@@ -11084,6 +11078,129 @@ fn parse_unpivot_table() {
11084
11078
verified_stmt(sql_unpivot_include_nulls).to_string(),
11085
11079
sql_unpivot_include_nulls
11086
11080
);
11081
+
11082
+ let sql_unpivot_with_alias = concat!(
11083
+ "SELECT * FROM sales AS s ",
11084
+ "UNPIVOT INCLUDE NULLS ",
11085
+ "(quantity FOR quarter IN ",
11086
+ "(Q1 AS Quater1, Q2 AS Quater2, Q3 AS Quater3, Q4 AS Quater4)) ",
11087
+ "AS u (product, quarter, quantity)"
11088
+ );
11089
+
11090
+ if let Unpivot { value, columns, .. } =
11091
+ &verified_only_select(sql_unpivot_with_alias).from[0].relation
11092
+ {
11093
+ assert_eq!(
11094
+ *columns,
11095
+ vec![
11096
+ ExprWithAlias {
11097
+ expr: Expr::Identifier(Ident::new("Q1")),
11098
+ alias: Some(Ident::new("Quater1")),
11099
+ },
11100
+ ExprWithAlias {
11101
+ expr: Expr::Identifier(Ident::new("Q2")),
11102
+ alias: Some(Ident::new("Quater2")),
11103
+ },
11104
+ ExprWithAlias {
11105
+ expr: Expr::Identifier(Ident::new("Q3")),
11106
+ alias: Some(Ident::new("Quater3")),
11107
+ },
11108
+ ExprWithAlias {
11109
+ expr: Expr::Identifier(Ident::new("Q4")),
11110
+ alias: Some(Ident::new("Quater4")),
11111
+ },
11112
+ ]
11113
+ );
11114
+ assert_eq!(*value, Expr::Identifier(Ident::new("quantity")));
11115
+ }
11116
+
11117
+ assert_eq!(
11118
+ verified_stmt(sql_unpivot_with_alias).to_string(),
11119
+ sql_unpivot_with_alias
11120
+ );
11121
+
11122
+ let sql_unpivot_with_alias_and_multi_value = concat!(
11123
+ "SELECT * FROM sales AS s ",
11124
+ "UNPIVOT INCLUDE NULLS ((first_quarter, second_quarter) ",
11125
+ "FOR half_of_the_year IN (",
11126
+ "(Q1, Q2) AS H1, ",
11127
+ "(Q3, Q4) AS H2",
11128
+ "))"
11129
+ );
11130
+
11131
+ if let Unpivot { value, columns, .. } =
11132
+ &verified_only_select(sql_unpivot_with_alias_and_multi_value).from[0].relation
11133
+ {
11134
+ assert_eq!(
11135
+ *columns,
11136
+ vec![
11137
+ ExprWithAlias {
11138
+ expr: Expr::Tuple(vec![
11139
+ Expr::Identifier(Ident::new("Q1")),
11140
+ Expr::Identifier(Ident::new("Q2")),
11141
+ ]),
11142
+ alias: Some(Ident::new("H1")),
11143
+ },
11144
+ ExprWithAlias {
11145
+ expr: Expr::Tuple(vec![
11146
+ Expr::Identifier(Ident::new("Q3")),
11147
+ Expr::Identifier(Ident::new("Q4")),
11148
+ ]),
11149
+ alias: Some(Ident::new("H2")),
11150
+ },
11151
+ ]
11152
+ );
11153
+ assert_eq!(
11154
+ *value,
11155
+ Expr::Tuple(vec![
11156
+ Expr::Identifier(Ident::new("first_quarter")),
11157
+ Expr::Identifier(Ident::new("second_quarter")),
11158
+ ])
11159
+ );
11160
+ }
11161
+
11162
+ assert_eq!(
11163
+ verified_stmt(sql_unpivot_with_alias_and_multi_value).to_string(),
11164
+ sql_unpivot_with_alias_and_multi_value
11165
+ );
11166
+
11167
+ let sql_unpivot_with_alias_and_multi_value_and_qualifier = concat!(
11168
+ "SELECT * FROM sales AS s ",
11169
+ "UNPIVOT INCLUDE NULLS ((first_quarter, second_quarter) ",
11170
+ "FOR half_of_the_year IN (",
11171
+ "(sales.Q1, sales.Q2) AS H1, ",
11172
+ "(sales.Q3, sales.Q4) AS H2",
11173
+ "))"
11174
+ );
11175
+
11176
+ if let Unpivot { columns, .. } =
11177
+ &verified_only_select(sql_unpivot_with_alias_and_multi_value_and_qualifier).from[0].relation
11178
+ {
11179
+ assert_eq!(
11180
+ *columns,
11181
+ vec![
11182
+ ExprWithAlias {
11183
+ expr: Expr::Tuple(vec![
11184
+ Expr::CompoundIdentifier(vec![Ident::new("sales"), Ident::new("Q1"),]),
11185
+ Expr::CompoundIdentifier(vec![Ident::new("sales"), Ident::new("Q2"),]),
11186
+ ]),
11187
+ alias: Some(Ident::new("H1")),
11188
+ },
11189
+ ExprWithAlias {
11190
+ expr: Expr::Tuple(vec![
11191
+ Expr::CompoundIdentifier(vec![Ident::new("sales"), Ident::new("Q3"),]),
11192
+ Expr::CompoundIdentifier(vec![Ident::new("sales"), Ident::new("Q4"),]),
11193
+ ]),
11194
+ alias: Some(Ident::new("H2")),
11195
+ },
11196
+ ]
11197
+ );
11198
+ }
11199
+
11200
+ assert_eq!(
11201
+ verified_stmt(sql_unpivot_with_alias_and_multi_value_and_qualifier).to_string(),
11202
+ sql_unpivot_with_alias_and_multi_value_and_qualifier
11203
+ );
11087
11204
}
11088
11205
11089
11206
#[test]
@@ -11181,20 +11298,14 @@ fn parse_pivot_unpivot_table() {
11181
11298
index_hints: vec![],
11182
11299
}),
11183
11300
null_inclusion: None,
11184
- value: Ident {
11185
- value: "population".to_string(),
11186
- quote_style: None,
11187
- span: Span::empty()
11188
- },
11189
-
11190
- name: Ident {
11191
- value: "year".to_string(),
11192
- quote_style: None,
11193
- span: Span::empty()
11194
- },
11301
+ value: Expr::Identifier(Ident::new("population")),
11302
+ name: Ident::new("year"),
11195
11303
columns: ["population_2000", "population_2010"]
11196
11304
.into_iter()
11197
- .map(Ident::new)
11305
+ .map(|col| ExprWithAlias {
11306
+ expr: Expr::Identifier(Ident::new(col)),
11307
+ alias: None,
11308
+ })
11198
11309
.collect(),
11199
11310
alias: Some(TableAlias {
11200
11311
name: Ident::new("u"),
0 commit comments