Skip to content

Commit

Permalink
snowflake: PIVOT on derived table factors (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
lustefaniak authored Nov 20, 2023
1 parent 5a3f193 commit dc2ceed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6737,9 +6737,20 @@ impl<'a> Parser<'a> {
// `parse_derived_table_factor` below will return success after parsing the
// subquery, followed by the closing ')', and the alias of the derived table.
// In the example above this is case (3).
return_ok_if_some!(
if let Some(mut table) =
self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
);
{
while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT])
{
table = match kw {
Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
_ => unreachable!(),
}
}
return Ok(table);
}

// A parsing error from `parse_derived_table_factor` indicates that the '(' we've
// recently consumed does not start a derived table (cases 1, 2, or 4).
// `maybe_parse` will ignore such an error and rewind to be after the opening '('.
Expand Down
7 changes: 7 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,10 @@ fn parse_division_correctly() {
"SELECT tbl1.field / tbl2.field FROM tbl1 JOIN tbl2 ON tbl1.id = tbl2.entity_id",
);
}

#[test]
fn parse_pivot_of_table_factor_derived() {
snowflake().verified_stmt(
"SELECT * FROM (SELECT place_id, weekday, open FROM times AS p) PIVOT(max(open) FOR weekday IN (0, 1, 2, 3, 4, 5, 6)) AS p (place_id, open_sun, open_mon, open_tue, open_wed, open_thu, open_fri, open_sat)"
);
}

0 comments on commit dc2ceed

Please sign in to comment.