Skip to content

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

Merged
merged 5 commits into from
Aug 1, 2025

Conversation

chenkovsky
Copy link
Contributor

for example, spark supports

SELECT * FROM sales_quarterly
    UNPIVOT EXCLUDE NULLS (
        (first_quarter, second_quarter)
        FOR half_of_the_year IN (
            (q1, q2) AS H1,
            (q3, q4) AS H2
        )
    );

https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-unpivot.html

@chenkovsky chenkovsky changed the title feat: support multi value column unpivot feat: support multi value column unpivot & alias in unpivot Jul 23, 2025
src/ast/query.rs Outdated
@@ -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>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 Expr, it would cover both the single_value and multi_value scenarios. Then similarly, we change to columns: Vec<ExprWithAlias>, which would additionally cover the fully qualified column_name variant

Comment on lines 11027 to 11029
"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)"
);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

sql_unpivot_with_alias
);

let sql_unpivot_with_alias = concat!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has the same name as the preceeding sql_unpivot_with_alias scenario, but testing different aspects, do we need to rename this accordingly?

src/ast/query.rs Outdated
@@ -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>,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Expr, it would cover both the single_value and multi_value scenarios. Then similarly, we change to columns: Vec<ExprWithAlias>, which would additionally cover the fully qualified column_name variant

@@ -1351,9 +1392,9 @@ pub enum TableFactor {
/// See <https://docs.snowflake.com/en/sql-reference/constructs/unpivot>.
Copy link
Contributor

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?

Comment on lines 13895 to 13908
let value = match self.peek_token_ref().token {
Token::LParen => {
// multi value column unpivot
Expr::Tuple(
self.parse_parenthesized_column_list(Mandatory, false)?
.into_iter()
.map(Expr::Identifier)
.collect(),
)
}
_ => {
// single value column unpivot
Expr::Identifier(self.parse_identifier()?)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call self.parse_expr() and self.parse_expr_with_alias() transparently instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse_expr may accept some invalid sql.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes I think it would be ok for the parser to be permissive in this case, downstream crates are expected to validate the AST further if needed.
Related can we add a test case that uses a fully qualified column name? e.g. FOR half_of_the_year IN ((foo.bar, bar.baz) AS x) - if I understand the intent correctly I think the parser is supposed to successfully parse that whereas with the current impl it is unable to

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @chenkovsky! Left a minor comment otherwise the changes look good to me

Comment on lines 10818 to 10820
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
p.parse_expr_with_alias()
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we inline this function since its only a couple lines and used once?

@iffyio iffyio changed the title feat: support multi value column unpivot & alias in unpivot feat: support multi value columns and aliases in unpivot Aug 1, 2025
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @chenkovsky!
cc @alamb

@iffyio iffyio merged commit dd650b8 into apache:main Aug 1, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants