Skip to content

Commit 3255fd3

Browse files
committed
Add support to to table_name inside parenthesis
1 parent 172ba42 commit 3255fd3

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/parser.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,7 @@ impl Parser {
17711771
// ^ ^ ^ ^
17721772
// | | | |
17731773
// | | | |
1774+
// | | | |
17741775
// | | | (4) belongs to a SetExpr::Query inside the subquery
17751776
// | | (3) starts a derived table (subquery)
17761777
// | (2) starts a nested join
@@ -1793,18 +1794,12 @@ impl Parser {
17931794
// Ignore the error and back up to where we were before.
17941795
// Either we'll be able to parse a valid nested join, or
17951796
// we won't, and we'll return that error instead.
1797+
//
1798+
// Even the SQL spec prohibits derived tables and bare
1799+
// tables from appearing alone in parentheses, we allowed it
1800+
// as some Db's allowed that (snowflake as example)
17961801
self.index = index;
17971802
let table_and_joins = self.parse_table_and_joins()?;
1798-
match table_and_joins.relation {
1799-
TableFactor::NestedJoin { .. } => (),
1800-
_ => {
1801-
if table_and_joins.joins.is_empty() {
1802-
// The SQL spec prohibits derived tables and bare
1803-
// tables from appearing alone in parentheses.
1804-
self.expected("joined table", self.peek_token())?
1805-
}
1806-
}
1807-
}
18081803
self.expect_token(&Token::RParen)?;
18091804
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
18101805
}

tests/sqlparser_common.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -1806,11 +1806,19 @@ fn parse_join_nesting() {
18061806
vec![join(nest!(nest!(nest!(table("b"), table("c")))))]
18071807
);
18081808

1809-
let res = parse_sql_statements("SELECT * FROM (a NATURAL JOIN (b))");
1810-
assert_eq!(
1811-
ParserError::ParserError("Expected joined table, found: )".to_string()),
1812-
res.unwrap_err()
1813-
);
1809+
// Parenthesized table names are non-standard, but supported in Snowflake SQL
1810+
let sql = "SELECT * FROM (a NATURAL JOIN (b))";
1811+
let select = verified_only_select(sql);
1812+
let from = only(select.from);
1813+
1814+
assert_eq!(from.relation, nest!(table("a"), nest!(table("b"))));
1815+
1816+
// Double parentheses around table names are non-standard, but supported in Snowflake SQL
1817+
let sql = "SELECT * FROM (a NATURAL JOIN ((b)))";
1818+
let select = verified_only_select(sql);
1819+
let from = only(select.from);
1820+
1821+
assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b")))));
18141822
}
18151823

18161824
#[test]
@@ -1953,10 +1961,24 @@ fn parse_derived_tables() {
19531961
}))
19541962
);
19551963

1956-
let res = parse_sql_statements("SELECT * FROM ((SELECT 1) AS t)");
1964+
// Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL
1965+
let sql = "SELECT * FROM ((SELECT 1) AS t)";
1966+
let select = verified_only_select(sql);
1967+
let from = only(select.from);
1968+
19571969
assert_eq!(
1958-
ParserError::ParserError("Expected joined table, found: )".to_string()),
1959-
res.unwrap_err()
1970+
from.relation,
1971+
TableFactor::NestedJoin(Box::new(TableWithJoins {
1972+
relation: TableFactor::Derived {
1973+
lateral: false,
1974+
subquery: Box::new(verified_query("SELECT 1")),
1975+
alias: Some(TableAlias {
1976+
name: "t".into(),
1977+
columns: vec![],
1978+
})
1979+
},
1980+
joins: Vec::new(),
1981+
}))
19601982
);
19611983
}
19621984

0 commit comments

Comments
 (0)