@@ -34,6 +34,14 @@ macro_rules! parser_err {
34
34
} ;
35
35
}
36
36
37
+ macro_rules! maybe {
38
+ ( $e: expr) => { {
39
+ if let Some ( v) = $e {
40
+ return Ok ( v) ;
41
+ }
42
+ } } ;
43
+ }
44
+
37
45
#[ derive( PartialEq ) ]
38
46
pub enum IsOptional {
39
47
Optional ,
@@ -176,6 +184,34 @@ impl Parser {
176
184
177
185
/// Parse an expression prefix
178
186
pub fn parse_prefix ( & mut self ) -> Result < Expr , ParserError > {
187
+ // PostgreSQL allows any string literal to be prceded by a type name, indicating that the
188
+ // string literal represents a literal of that type. Some examples:
189
+ //
190
+ // DATE '2020-05-20'
191
+ // TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
192
+ // BOOL 'true'
193
+ //
194
+ // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
195
+ // matters is the fact that INTERVAL string literals may optionally be followed by special
196
+ // keywords, e.g.:
197
+ //
198
+ // INTERVAL '7' DAY
199
+ //
200
+ // Note also that naively `SELECT date` looks like a syntax error because the `date` type
201
+ // name is not followed by a string literal, but in fact is a valid expression that should
202
+ // parse as the column name "date".
203
+ maybe ! ( self . maybe_parse( |parser| {
204
+ match parser. parse_data_type( ) ? {
205
+ DataType :: Interval => parser. parse_literal_interval( ) ,
206
+ data_type => Ok ( Expr :: Cast {
207
+ expr: Box :: new( Expr :: Value ( Value :: SingleQuotedString (
208
+ parser. parse_literal_string( ) ?,
209
+ ) ) ) ,
210
+ data_type,
211
+ } ) ,
212
+ }
213
+ } ) ) ;
214
+
179
215
let tok = self
180
216
. next_token ( )
181
217
. ok_or_else ( || ParserError :: ParserError ( "Unexpected EOF" . to_string ( ) ) ) ?;
@@ -187,7 +223,6 @@ impl Parser {
187
223
}
188
224
"CASE" => self . parse_case_expr ( ) ,
189
225
"CAST" => self . parse_cast_expr ( ) ,
190
- "DATE" => Ok ( Expr :: Value ( Value :: Date ( self . parse_literal_string ( ) ?) ) ) ,
191
226
"EXISTS" => self . parse_exists_expr ( ) ,
192
227
"EXTRACT" => self . parse_extract_expr ( ) ,
193
228
"INTERVAL" => self . parse_literal_interval ( ) ,
@@ -196,8 +231,6 @@ impl Parser {
196
231
op : UnaryOperator :: Not ,
197
232
expr : Box :: new ( self . parse_subexpr ( Self :: UNARY_NOT_PREC ) ?) ,
198
233
} ) ,
199
- "TIME" => Ok ( Expr :: Value ( Value :: Time ( self . parse_literal_string ( ) ?) ) ) ,
200
- "TIMESTAMP" => Ok ( Expr :: Value ( Value :: Timestamp ( self . parse_literal_string ( ) ?) ) ) ,
201
234
// Here `w` is a word, check if it's a part of a multi-part
202
235
// identifier, a function call, or a simple identifier:
203
236
_ => match self . peek_token ( ) {
@@ -912,6 +945,20 @@ impl Parser {
912
945
Ok ( values)
913
946
}
914
947
948
+ #[ must_use]
949
+ fn maybe_parse < T , F > ( & mut self , mut f : F ) -> Option < T >
950
+ where
951
+ F : FnMut ( & mut Parser ) -> Result < T , ParserError > ,
952
+ {
953
+ let index = self . index ;
954
+ if let Ok ( t) = f ( self ) {
955
+ Some ( t)
956
+ } else {
957
+ self . index = index;
958
+ None
959
+ }
960
+ }
961
+
915
962
/// Parse either `ALL` or `DISTINCT`. Returns `true` if `DISTINCT` is parsed and results in a
916
963
/// `ParserError` if both `ALL` and `DISTINCT` are fround.
917
964
pub fn parse_all_or_distinct ( & mut self ) -> Result < bool , ParserError > {
@@ -1908,7 +1955,6 @@ impl Parser {
1908
1955
}
1909
1956
1910
1957
if self . consume_token ( & Token :: LParen ) {
1911
- let index = self . index ;
1912
1958
// A left paren introduces either a derived table (i.e., a subquery)
1913
1959
// or a nested join. It's nearly impossible to determine ahead of
1914
1960
// time which it is... so we just try to parse both.
@@ -1925,30 +1971,20 @@ impl Parser {
1925
1971
// | (2) starts a nested join
1926
1972
// (1) an additional set of parens around a nested join
1927
1973
//
1928
- match self . parse_derived_table_factor ( NotLateral ) {
1929
- // The recently consumed '(' started a derived table, and we've
1930
- // parsed the subquery, followed by the closing ')', and the
1931
- // alias of the derived table. In the example above this is
1932
- // case (3), and the next token would be `NATURAL`.
1933
- Ok ( table_factor) => Ok ( table_factor) ,
1934
- Err ( _) => {
1935
- // A parsing error from `parse_derived_table_factor` indicates that
1936
- // the '(' we've recently consumed does not start a derived table
1937
- // (cases 1, 2, or 4). Ignore the error and back up to where we
1938
- // were before - right after the opening '('.
1939
- self . index = index;
1940
-
1941
- // Inside the parentheses we expect to find a table factor
1942
- // followed by some joins or another level of nesting.
1943
- let table_and_joins = self . parse_table_and_joins ( ) ?;
1944
- self . expect_token ( & Token :: RParen ) ?;
1945
- // The SQL spec prohibits derived and bare tables from appearing
1946
- // alone in parentheses. We don't enforce this as some databases
1947
- // (e.g. Snowflake) allow such syntax.
1948
1974
1949
- Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
1950
- }
1951
- }
1975
+ // Check if the recently consumed '(' started a derived table, in which case we've
1976
+ // parsed the subquery, followed by the closing ')', and the alias of the derived
1977
+ // table. In the example above this is case (3), or another nested join (2).
1978
+ maybe ! ( self . maybe_parse( |parser| parser. parse_derived_table_factor( NotLateral ) ) ) ;
1979
+
1980
+ // Inside the parentheses we expect to find a table factor
1981
+ // followed by some joins or another level of nesting.
1982
+ let table_and_joins = self . parse_table_and_joins ( ) ?;
1983
+ self . expect_token ( & Token :: RParen ) ?;
1984
+ // The SQL spec prohibits derived and bare tables from appearing
1985
+ // alone in parentheses. We don't enforce this as some databases
1986
+ // (e.g. Snowflake) allow such syntax.
1987
+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
1952
1988
} else {
1953
1989
let name = self . parse_object_name ( ) ?;
1954
1990
// Postgres, MSSQL: table-valued functions:
0 commit comments