Parse an optional column list after a CTE

This commit is contained in:
Nickolay Ponomarev 2019-02-11 23:58:13 +03:00
parent f859c9b80e
commit cccf7f0d8e
3 changed files with 40 additions and 11 deletions

View file

@ -1027,9 +1027,14 @@ fn parse_ctes() {
fn assert_ctes_in_select(expected: &[&str], sel: &SQLQuery) {
let mut i = 0;
for exp in expected {
let Cte { query, alias } = &sel.ctes[i];
let Cte {
query,
alias,
renamed_columns,
} = &sel.ctes[i];
assert_eq!(*exp, query.to_string());
assert_eq!(if i == 0 { "a" } else { "b" }, alias);
assert!(renamed_columns.is_empty());
i += 1;
}
}
@ -1066,6 +1071,16 @@ fn parse_ctes() {
assert_ctes_in_select(&cte_sqls, &only(&select.ctes).query);
}
#[test]
fn parse_cte_renamed_columns() {
let sql = "WITH cte (col1, col2) AS (SELECT foo, bar FROM baz) SELECT * FROM cte";
let query = all_dialects().verified_query(sql);
assert_eq!(
vec!["col1", "col2"],
query.ctes.first().unwrap().renamed_columns
);
}
#[test]
fn parse_derived_tables() {
let sql = "SELECT a.x, b.y FROM (SELECT x FROM foo) AS a CROSS JOIN (SELECT y FROM bar) AS b";