From 3255fd3ea8b5f8e9c63c1825fdcd2fc1fb30cc22 Mon Sep 17 00:00:00 2001 From: Eyal Leshem Date: Sat, 4 Apr 2020 23:21:36 +0300 Subject: [PATCH] Add support to to table_name inside parenthesis --- src/parser.rs | 15 +++++---------- tests/sqlparser_common.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index c9e32ed3b..e988f9c09 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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))) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 71b11f3b7..939673dcd 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -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(), + })) ); }