-
Notifications
You must be signed in to change notification settings - Fork 710
feat: support multi value columns and aliases in unpivot #1969
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -745,6 +745,47 @@ impl fmt::Display for IdentWithAlias { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
| #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
| #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
| pub struct IdentsWithAlias { | ||
| pub idents: Vec<Ident>, | ||
| pub alias: Option<Ident>, | ||
| } | ||
|
|
||
| impl IdentsWithAlias { | ||
| pub fn new(idents: Vec<Ident>, alias: Option<Ident>) -> Self { | ||
| Self { idents, alias } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for IdentsWithAlias { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self.idents.len() { | ||
| 0 => Ok(()), | ||
| 1 => { | ||
| if let Some(alias) = &self.alias { | ||
| write!(f, "{} AS {}", self.idents[0], alias) | ||
| } else { | ||
| write!(f, "{}", self.idents[0]) | ||
| } | ||
| } | ||
| _ => { | ||
| if let Some(alias) = &self.alias { | ||
| write!( | ||
| f, | ||
| "({}) AS {}", | ||
| display_comma_separated(&self.idents), | ||
| alias | ||
| ) | ||
| } else { | ||
| write!(f, "({})", display_comma_separated(&self.idents)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Additional options for wildcards, e.g. Snowflake `EXCLUDE`/`RENAME` and Bigquery `EXCEPT`. | ||
| #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
| #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
|
|
@@ -1351,9 +1392,9 @@ pub enum TableFactor { | |
| /// See <https://docs.snowflake.com/en/sql-reference/constructs/unpivot>. | ||
| Unpivot { | ||
| table: Box<TableFactor>, | ||
| value: Ident, | ||
| value: Vec<Ident>, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use expr::Identifier ? otherwise we cannot visit this ident by vistor.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm yeah I think repr wise we could represent this as an arbitrary |
||
| name: Ident, | ||
| columns: Vec<Ident>, | ||
| columns: Vec<IdentsWithAlias>, | ||
| null_inclusion: Option<NullInclusion>, | ||
| alias: Option<TableAlias>, | ||
| }, | ||
|
|
@@ -2035,10 +2076,17 @@ impl fmt::Display for TableFactor { | |
| if let Some(null_inclusion) = null_inclusion { | ||
| write!(f, " {null_inclusion} ")?; | ||
| } | ||
| write!(f, "(")?; | ||
| if value.len() == 1 { | ||
| // single value column unpivot | ||
| write!(f, "{}", value[0])?; | ||
| } else { | ||
| // multi value column unpivot | ||
| write!(f, "({})", display_comma_separated(value))?; | ||
| } | ||
| write!( | ||
| f, | ||
| "({} FOR {} IN ({}))", | ||
| value, | ||
| " FOR {} IN ({}))", | ||
| name, | ||
| display_comma_separated(columns) | ||
| )?; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10947,11 +10947,11 @@ fn parse_unpivot_table() { | |
| index_hints: vec![], | ||
| }), | ||
| null_inclusion: None, | ||
| value: Ident { | ||
| value: vec![Ident { | ||
| value: "quantity".to_string(), | ||
| quote_style: None, | ||
| span: Span::empty(), | ||
| }, | ||
| }], | ||
|
|
||
| name: Ident { | ||
| value: "quarter".to_string(), | ||
|
|
@@ -10960,7 +10960,7 @@ fn parse_unpivot_table() { | |
| }, | ||
| columns: ["Q1", "Q2", "Q3", "Q4"] | ||
| .into_iter() | ||
| .map(Ident::new) | ||
| .map(|col| IdentsWithAlias::new(vec![Ident::new(col)], None)) | ||
| .collect(), | ||
| alias: Some(TableAlias { | ||
| name: Ident::new("u"), | ||
|
|
@@ -11022,6 +11022,67 @@ fn parse_unpivot_table() { | |
| verified_stmt(sql_unpivot_include_nulls).to_string(), | ||
| sql_unpivot_include_nulls | ||
| ); | ||
|
|
||
| let sql_unpivot_with_alias = concat!( | ||
|
iffyio marked this conversation as resolved.
|
||
| "SELECT * FROM sales AS s ", | ||
| "UNPIVOT INCLUDE NULLS (quantity FOR quarter IN (Q1 AS Quater1, Q2 AS Quater2, Q3 AS Quater3, Q4 AS Quater4)) AS u (product, quarter, quantity)" | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it look like the formatting is a bit off here with cargo fmt, could you manually unindent these lines? |
||
|
|
||
| if let Unpivot { value, columns, .. } = | ||
| &verified_only_select(sql_unpivot_with_alias).from[0].relation | ||
| { | ||
| assert_eq!( | ||
| *columns, | ||
| vec![ | ||
| IdentsWithAlias::new(vec![Ident::new("Q1")], Some(Ident::new("Quater1"))), | ||
| IdentsWithAlias::new(vec![Ident::new("Q2")], Some(Ident::new("Quater2"))), | ||
| IdentsWithAlias::new(vec![Ident::new("Q3")], Some(Ident::new("Quater3"))), | ||
| IdentsWithAlias::new(vec![Ident::new("Q4")], Some(Ident::new("Quater4"))), | ||
| ] | ||
| ); | ||
| assert_eq!(*value, vec![Ident::new("quantity")]); | ||
| } | ||
|
|
||
| assert_eq!( | ||
| verified_stmt(sql_unpivot_with_alias).to_string(), | ||
| sql_unpivot_with_alias | ||
| ); | ||
|
|
||
| let sql_unpivot_with_alias = concat!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has the same name as the preceeding |
||
| "SELECT * FROM sales AS s ", | ||
| "UNPIVOT INCLUDE NULLS ((first_quarter, second_quarter) ", | ||
| "FOR half_of_the_year IN (", | ||
| "(Q1, Q2) AS H1, ", | ||
| "(Q3, Q4) AS H2", | ||
| "))" | ||
| ); | ||
|
|
||
| if let Unpivot { value, columns, .. } = | ||
| &verified_only_select(sql_unpivot_with_alias).from[0].relation | ||
| { | ||
| assert_eq!( | ||
| *columns, | ||
| vec![ | ||
| IdentsWithAlias::new( | ||
| vec![Ident::new("Q1"), Ident::new("Q2")], | ||
| Some(Ident::new("H1")) | ||
| ), | ||
| IdentsWithAlias::new( | ||
| vec![Ident::new("Q3"), Ident::new("Q4")], | ||
| Some(Ident::new("H2")) | ||
| ), | ||
| ] | ||
| ); | ||
| assert_eq!( | ||
| *value, | ||
| vec![Ident::new("first_quarter"), Ident::new("second_quarter")] | ||
| ); | ||
| } | ||
|
|
||
| assert_eq!( | ||
| verified_stmt(sql_unpivot_with_alias).to_string(), | ||
| sql_unpivot_with_alias | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -11119,11 +11180,11 @@ fn parse_pivot_unpivot_table() { | |
| index_hints: vec![], | ||
| }), | ||
| null_inclusion: None, | ||
| value: Ident { | ||
| value: vec![Ident { | ||
| value: "population".to_string(), | ||
| quote_style: None, | ||
| span: Span::empty() | ||
| }, | ||
| }], | ||
|
|
||
| name: Ident { | ||
| value: "year".to_string(), | ||
|
|
@@ -11132,7 +11193,7 @@ fn parse_pivot_unpivot_table() { | |
| }, | ||
| columns: ["population_2000", "population_2010"] | ||
| .into_iter() | ||
| .map(Ident::new) | ||
| .map(|col| IdentsWithAlias::new(vec![Ident::new(col)], None)) | ||
| .collect(), | ||
| alias: Some(TableAlias { | ||
| name: Ident::new("u"), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we include the link to the databricks documentation here as well?