@@ -83,23 +83,28 @@ impl fmt::Display for ParserError {
8383impl Error for ParserError { }
8484
8585/// SQL Parser
86- pub struct Parser {
86+ pub struct Parser < ' a > {
8787 tokens : Vec < Token > ,
8888 /// The index of the first unprocessed token in `self.tokens`
8989 index : usize ,
90+ dialect : & ' a dyn Dialect ,
9091}
9192
92- impl Parser {
93+ impl < ' a > Parser < ' a > {
9394 /// Parse the specified tokens
94- pub fn new ( tokens : Vec < Token > ) -> Self {
95- Parser { tokens, index : 0 }
95+ pub fn new ( tokens : Vec < Token > , dialect : & ' a dyn Dialect ) -> Self {
96+ Parser {
97+ tokens,
98+ index : 0 ,
99+ dialect,
100+ }
96101 }
97102
98103 /// Parse a SQL statement and produce an Abstract Syntax Tree (AST)
99104 pub fn parse_sql ( dialect : & dyn Dialect , sql : & str ) -> Result < Vec < Statement > , ParserError > {
100105 let mut tokenizer = Tokenizer :: new ( dialect, & sql) ;
101106 let tokens = tokenizer. tokenize ( ) ?;
102- let mut parser = Parser :: new ( tokens) ;
107+ let mut parser = Parser :: new ( tokens, dialect ) ;
103108 let mut stmts = Vec :: new ( ) ;
104109 let mut expecting_statement_delimiter = false ;
105110 debug ! ( "Parsing sql '{}'..." , sql) ;
@@ -950,7 +955,7 @@ impl Parser {
950955 /// Parse a comma-separated list of 1+ items accepted by `F`
951956 pub fn parse_comma_separated < T , F > ( & mut self , mut f : F ) -> Result < Vec < T > , ParserError >
952957 where
953- F : FnMut ( & mut Parser ) -> Result < T , ParserError > ,
958+ F : FnMut ( & mut Parser < ' a > ) -> Result < T , ParserError > ,
954959 {
955960 let mut values = vec ! [ ] ;
956961 loop {
@@ -2056,9 +2061,91 @@ impl Parser {
20562061 } ;
20572062 joins. push ( join) ;
20582063 }
2064+
20592065 Ok ( TableWithJoins { relation, joins } )
20602066 }
20612067
2068+ fn add_alias_to_single_table_in_parenthesis (
2069+ & self ,
2070+ table_facor : TableFactor ,
2071+ consumed_alias : TableAlias ,
2072+ ) -> Result < TableFactor , ParserError > {
2073+ match table_facor {
2074+ // Add the alias to dervied table
2075+ TableFactor :: Derived {
2076+ lateral,
2077+ subquery,
2078+ alias,
2079+ } => match alias {
2080+ None => Ok ( TableFactor :: Derived {
2081+ lateral,
2082+ subquery,
2083+ alias : Some ( consumed_alias) ,
2084+ } ) ,
2085+ // "Select * from (table1 as alias1) as alias1" - it prohabited
2086+ Some ( alias) => Err ( ParserError :: ParserError ( format ! (
2087+ "duplicate alias {}" ,
2088+ alias
2089+ ) ) ) ,
2090+ } ,
2091+ // Add The alias to the table
2092+ TableFactor :: Table {
2093+ name,
2094+ alias,
2095+ args,
2096+ with_hints,
2097+ } => match alias {
2098+ None => Ok ( TableFactor :: Table {
2099+ name,
2100+ alias : Some ( consumed_alias) ,
2101+ args,
2102+ with_hints,
2103+ } ) ,
2104+ // "Select * from (table1 as alias1) as alias1" - it prohabited
2105+ Some ( alias) => Err ( ParserError :: ParserError ( format ! (
2106+ "duplicate alias {}" ,
2107+ alias
2108+ ) ) ) ,
2109+ } ,
2110+ TableFactor :: NestedJoin ( _) => Err ( ParserError :: ParserError (
2111+ "aliasing joins is not allowed" . to_owned ( ) ,
2112+ ) ) ,
2113+ }
2114+ }
2115+
2116+ fn remove_redundent_parenthesis (
2117+ & mut self ,
2118+ table_and_joins : TableWithJoins ,
2119+ ) -> Result < TableFactor , ParserError > {
2120+ let table_factor = table_and_joins. relation ;
2121+
2122+ // check if we have alias after the parenthesis
2123+ let alias = match self . parse_optional_table_alias ( keywords:: RESERVED_FOR_TABLE_ALIAS ) ? {
2124+ None => {
2125+ return Ok ( table_factor) ;
2126+ }
2127+ Some ( alias) => alias,
2128+ } ;
2129+
2130+ // if we have alias, we attached it to the single table that inside parenthesis
2131+ self . add_alias_to_single_table_in_parenthesis ( table_factor, alias)
2132+ }
2133+
2134+ fn validate_nested_join ( & self , table_and_joins : & TableWithJoins ) -> Result < ( ) , ParserError > {
2135+ match table_and_joins. relation {
2136+ TableFactor :: NestedJoin { .. } => ( ) ,
2137+ _ => {
2138+ if table_and_joins. joins . is_empty ( ) {
2139+ // validate thats indeed join and not dervied
2140+ // or nested table
2141+ self . expected ( "joined table" , self . peek_token ( ) ) ?
2142+ }
2143+ }
2144+ }
2145+
2146+ Ok ( ( ) )
2147+ }
2148+
20622149 /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
20632150 pub fn parse_table_factor ( & mut self ) -> Result < TableFactor , ParserError > {
20642151 if self . parse_keyword ( Keyword :: LATERAL ) {
@@ -2102,10 +2189,28 @@ impl Parser {
21022189 // followed by some joins or another level of nesting.
21032190 let table_and_joins = self . parse_table_and_joins ( ) ?;
21042191 self . expect_token ( & Token :: RParen ) ?;
2192+
21052193 // The SQL spec prohibits derived and bare tables from appearing
2106- // alone in parentheses. We don't enforce this as some databases
2107- // (e.g. Snowflake) allow such syntax.
2108- Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
2194+ // alone in parentheses. But as some databases
2195+ // (e.g. Snowflake) allow such syntax - it's can be allowed
2196+ // for specfic dialect.
2197+ if self . dialect . alllow_single_table_in_parenthesis ( ) {
2198+ if table_and_joins. joins . is_empty ( ) {
2199+ // In case the DB's like snowflake that allowed single dervied or bare
2200+ // table in parenthesis (for example : `Select * from (a) as b` )
2201+ // the parser will parse it as Nested join, but if it's actually a single table
2202+ // we don't want to treat such case as join , because we don't actually join
2203+ // any tables.
2204+ let table_factor = self . remove_redundent_parenthesis ( table_and_joins) ?;
2205+ Ok ( table_factor)
2206+ } else {
2207+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
2208+ }
2209+ } else {
2210+ // Defualt behaviuor
2211+ self . validate_nested_join ( & table_and_joins) ?;
2212+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
2213+ }
21092214 } else {
21102215 let name = self . parse_object_name ( ) ?;
21112216 // Postgres, MSSQL: table-valued functions:
0 commit comments