Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ pub enum TableFactor {
/// https://docs.snowflake.com/en/sql-reference/constructs/pivot.html
Pivot {
expr: Expr,
alias: Option<TableAlias>,
val: Ident,
pivot_vals: Vec<Expr>,
},
/// https://docs.snowflake.com/en/sql-reference/constructs/unpivot.html
Unpivot {
expr: Expr,
alias: Option<TableAlias>,
val: Ident,
pivot_vals: Vec<Expr>,
},
Expand Down Expand Up @@ -389,6 +397,25 @@ impl fmt::Display for TableFactor {
}
TableFactor::Pivot {
expr,
alias,
val,
pivot_vals,
} => {
write!(f, "({} FOR {} IN (", expr, val)?;
let mut delim = "";
for pivot_val in pivot_vals {
write!(f, "{}{}", delim, pivot_val)?;
delim = ", ";
}
write!(f, "))")?;
if let Some(alias) = alias {
write!(f, " AS {}", alias)?;
}
Ok(())
}
TableFactor::Unpivot {
expr,
alias,
val,
pivot_vals,
} => {
Expand All @@ -398,7 +425,11 @@ impl fmt::Display for TableFactor {
write!(f, "{}{}", delim, pivot_val)?;
delim = ", ";
}
write!(f, "))")
write!(f, "))")?;
if let Some(alias) = alias {
write!(f, " AS {}", alias)?;
}
Ok(())
}
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
}
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,7 @@ impl<'a> Parser<'a> {
alias.replace(outer_alias);
}
TableFactor::Pivot { .. } => unreachable!(),
TableFactor::Unpivot { .. } => unreachable!(),
TableFactor::NestedJoin(_) => unreachable!(),
};
}
Expand Down Expand Up @@ -2681,8 +2682,10 @@ impl<'a> Parser<'a> {
let pivot_vals = self.parse_comma_separated(Parser::parse_expr)?;
self.expect_token(&Token::RParen)?;
self.expect_token(&Token::RParen)?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(TableFactor::Pivot {
expr,
alias,
val,
pivot_vals,
})
Expand Down