Skip to content

Commit 55f0e57

Browse files
committed
Add support to to table_name inside parenthesis
1 parent 172ba42 commit 55f0e57

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/parser.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,8 +1768,9 @@ impl Parser {
17681768
// /-------------------------------------------------------\
17691769
// | /-----------------------------------\ |
17701770
// SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
1771-
// ^ ^ ^ ^
1772-
// | | | |
1771+
// ^ ^ ^ ^
1772+
// | | | |
1773+
// | | | |
17731774
// | | | |
17741775
// | | | (4) belongs to a SetExpr::Query inside the subquery
17751776
// | | (3) starts a derived table (subquery)
@@ -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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,11 +1806,26 @@ 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))");
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+
18101814
assert_eq!(
1811-
ParserError::ParserError("Expected joined table, found: )".to_string()),
1812-
res.unwrap_err()
1815+
from.relation,
1816+
nest!(table("a"), nest!(table("b")))
18131817
);
1818+
1819+
// Double parentheses around table names are non-standard, but supported in Snowflake SQL
1820+
let sql = "SELECT * FROM (a NATURAL JOIN ((b)))";
1821+
let select = verified_only_select(sql);
1822+
let from = only(select.from);
1823+
1824+
assert_eq!(
1825+
from.relation,
1826+
nest!(table("a"), nest!(nest!(table("b"))))
1827+
);
1828+
18141829
}
18151830

18161831
#[test]
@@ -1953,11 +1968,27 @@ fn parse_derived_tables() {
19531968
}))
19541969
);
19551970

1956-
let res = parse_sql_statements("SELECT * FROM ((SELECT 1) AS t)");
1971+
// Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL
1972+
let sql = "SELECT * FROM ((SELECT 1) AS t)";
1973+
let select = verified_only_select(sql);
1974+
let from = only(select.from);
1975+
19571976
assert_eq!(
1958-
ParserError::ParserError("Expected joined table, found: )".to_string()),
1959-
res.unwrap_err()
1977+
from.relation,
1978+
TableFactor::NestedJoin(Box::new(TableWithJoins {
1979+
relation: TableFactor::Derived {
1980+
lateral: false,
1981+
subquery: Box::new(verified_query("SELECT 1")),
1982+
alias: Some(TableAlias {
1983+
name: "t".into(),
1984+
columns: vec![],
1985+
})
1986+
},
1987+
joins: Vec::new(),
1988+
}))
19601989
);
1990+
1991+
19611992
}
19621993

19631994
#[test]

0 commit comments

Comments
 (0)