Skip to content

Commit dc2ceed

Browse files
authored
snowflake: PIVOT on derived table factors (#1027)
1 parent 5a3f193 commit dc2ceed

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/parser/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6737,9 +6737,20 @@ impl<'a> Parser<'a> {
67376737
// `parse_derived_table_factor` below will return success after parsing the
67386738
// subquery, followed by the closing ')', and the alias of the derived table.
67396739
// In the example above this is case (3).
6740-
return_ok_if_some!(
6740+
if let Some(mut table) =
67416741
self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
6742-
);
6742+
{
6743+
while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT])
6744+
{
6745+
table = match kw {
6746+
Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
6747+
Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
6748+
_ => unreachable!(),
6749+
}
6750+
}
6751+
return Ok(table);
6752+
}
6753+
67436754
// A parsing error from `parse_derived_table_factor` indicates that the '(' we've
67446755
// recently consumed does not start a derived table (cases 1, 2, or 4).
67456756
// `maybe_parse` will ignore such an error and rewind to be after the opening '('.

tests/sqlparser_snowflake.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,3 +1140,10 @@ fn parse_division_correctly() {
11401140
"SELECT tbl1.field / tbl2.field FROM tbl1 JOIN tbl2 ON tbl1.id = tbl2.entity_id",
11411141
);
11421142
}
1143+
1144+
#[test]
1145+
fn parse_pivot_of_table_factor_derived() {
1146+
snowflake().verified_stmt(
1147+
"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)"
1148+
);
1149+
}

0 commit comments

Comments
 (0)