Merge pull request #155 from eyalleshem/master

Accept non-standard `Select * from (a)` queries, where the table name is inside parentheses.
This commit is contained in:
Nickolay Ponomarev 2020-04-20 04:57:12 +03:00 committed by GitHub
commit 33303b244f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 18 deletions

View file

@ -1771,6 +1771,7 @@ impl Parser {
// ^ ^ ^ ^
// | | | |
// | | | |
// | | | |
// | | | (4) belongs to a SetExpr::Query inside the subquery
// | | (3) starts a derived table (subquery)
// | (2) starts a nested join
@ -1793,18 +1794,12 @@ impl Parser {
// Ignore the error and back up to where we were before.
// Either we'll be able to parse a valid nested join, or
// we won't, and we'll return that error instead.
//
// Even the SQL spec prohibits derived tables and bare
// tables from appearing alone in parentheses, we allowed it
// as some Db's allowed that (snowflake as example)
self.index = index;
let table_and_joins = self.parse_table_and_joins()?;
match table_and_joins.relation {
TableFactor::NestedJoin { .. } => (),
_ => {
if table_and_joins.joins.is_empty() {
// The SQL spec prohibits derived tables and bare
// tables from appearing alone in parentheses.
self.expected("joined table", self.peek_token())?
}
}
}
self.expect_token(&Token::RParen)?;
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
}

View file

@ -1806,11 +1806,19 @@ fn parse_join_nesting() {
vec![join(nest!(nest!(nest!(table("b"), table("c")))))]
);
let res = parse_sql_statements("SELECT * FROM (a NATURAL JOIN (b))");
assert_eq!(
ParserError::ParserError("Expected joined table, found: )".to_string()),
res.unwrap_err()
);
// Parenthesized table names are non-standard, but supported in Snowflake SQL
let sql = "SELECT * FROM (a NATURAL JOIN (b))";
let select = verified_only_select(sql);
let from = only(select.from);
assert_eq!(from.relation, nest!(table("a"), nest!(table("b"))));
// Double parentheses around table names are non-standard, but supported in Snowflake SQL
let sql = "SELECT * FROM (a NATURAL JOIN ((b)))";
let select = verified_only_select(sql);
let from = only(select.from);
assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b")))));
}
#[test]
@ -1953,10 +1961,24 @@ fn parse_derived_tables() {
}))
);
let res = parse_sql_statements("SELECT * FROM ((SELECT 1) AS t)");
// Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL
let sql = "SELECT * FROM ((SELECT 1) AS t)";
let select = verified_only_select(sql);
let from = only(select.from);
assert_eq!(
ParserError::ParserError("Expected joined table, found: )".to_string()),
res.unwrap_err()
from.relation,
TableFactor::NestedJoin(Box::new(TableWithJoins {
relation: TableFactor::Derived {
lateral: false,
subquery: Box::new(verified_query("SELECT 1")),
alias: Some(TableAlias {
name: "t".into(),
columns: vec![],
})
},
joins: Vec::new(),
}))
);
}