Skip to content

Support "Select * from (a)" queries (when the table name is inside Parenthesis) #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2020
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
15 changes: 5 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
}
Expand Down
38 changes: 30 additions & 8 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(),
}))
);
}

Expand Down