@@ -34,6 +34,15 @@ macro_rules! parser_err {
34
34
} ;
35
35
}
36
36
37
+ // Returns a successful result if the optional expression is some
38
+ macro_rules! return_ok_if_some {
39
+ ( $e: expr) => { {
40
+ if let Some ( v) = $e {
41
+ return Ok ( v) ;
42
+ }
43
+ } } ;
44
+ }
45
+
37
46
#[ derive( PartialEq ) ]
38
47
pub enum IsOptional {
39
48
Optional ,
@@ -171,6 +180,38 @@ impl Parser {
171
180
172
181
/// Parse an expression prefix
173
182
pub fn parse_prefix ( & mut self ) -> Result < Expr , ParserError > {
183
+ // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
184
+ // string literal represents a literal of that type. Some examples:
185
+ //
186
+ // DATE '2020-05-20'
187
+ // TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
188
+ // BOOL 'true'
189
+ //
190
+ // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
191
+ // matters is the fact that INTERVAL string literals may optionally be followed by special
192
+ // keywords, e.g.:
193
+ //
194
+ // INTERVAL '7' DAY
195
+ //
196
+ // Note also that naively `SELECT date` looks like a syntax error because the `date` type
197
+ // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
198
+ // expression that should parse as the column name "date".
199
+ return_ok_if_some ! ( self . maybe_parse( |parser| {
200
+ match parser. parse_data_type( ) ? {
201
+ DataType :: Interval => parser. parse_literal_interval( ) ,
202
+ // Single-quoted strings are parsed as custom data types, however this not desirable
203
+ // when we are handling input like `"NOT 'a' NOT LIKE 'b'"` because this will produce a
204
+ // TypedString instead of a SingleQuotedString. Further, this leads to issues where the
205
+ // same input will yield a BinaryOperator instead of the correct UnaryOperator. Here we
206
+ // handle that specific case by returning an error.
207
+ DataType :: Custom ( ..) => parser_err!( "dummy" ) ,
208
+ data_type => Ok ( Expr :: TypedString {
209
+ data_type,
210
+ value: parser. parse_literal_string( ) ?,
211
+ } ) ,
212
+ }
213
+ } ) ) ;
214
+
174
215
let expr = match self . next_token ( ) {
175
216
Token :: Word ( w) => match w. keyword . as_ref ( ) {
176
217
"TRUE" | "FALSE" | "NULL" => {
@@ -179,7 +220,6 @@ impl Parser {
179
220
}
180
221
"CASE" => self . parse_case_expr ( ) ,
181
222
"CAST" => self . parse_cast_expr ( ) ,
182
- "DATE" => Ok ( Expr :: Value ( Value :: Date ( self . parse_literal_string ( ) ?) ) ) ,
183
223
"EXISTS" => self . parse_exists_expr ( ) ,
184
224
"EXTRACT" => self . parse_extract_expr ( ) ,
185
225
"INTERVAL" => self . parse_literal_interval ( ) ,
@@ -188,8 +228,6 @@ impl Parser {
188
228
op : UnaryOperator :: Not ,
189
229
expr : Box :: new ( self . parse_subexpr ( Self :: UNARY_NOT_PREC ) ?) ,
190
230
} ) ,
191
- "TIME" => Ok ( Expr :: Value ( Value :: Time ( self . parse_literal_string ( ) ?) ) ) ,
192
- "TIMESTAMP" => Ok ( Expr :: Value ( Value :: Timestamp ( self . parse_literal_string ( ) ?) ) ) ,
193
231
// Here `w` is a word, check if it's a part of a multi-part
194
232
// identifier, a function call, or a simple identifier:
195
233
_ => match self . peek_token ( ) {
@@ -895,6 +933,20 @@ impl Parser {
895
933
Ok ( values)
896
934
}
897
935
936
+ #[ must_use]
937
+ fn maybe_parse < T , F > ( & mut self , mut f : F ) -> Option < T >
938
+ where
939
+ F : FnMut ( & mut Parser ) -> Result < T , ParserError > ,
940
+ {
941
+ let index = self . index ;
942
+ if let Ok ( t) = f ( self ) {
943
+ Some ( t)
944
+ } else {
945
+ self . index = index;
946
+ None
947
+ }
948
+ }
949
+
898
950
/// Parse either `ALL` or `DISTINCT`. Returns `true` if `DISTINCT` is parsed and results in a
899
951
/// `ParserError` if both `ALL` and `DISTINCT` are fround.
900
952
pub fn parse_all_or_distinct ( & mut self ) -> Result < bool , ParserError > {
@@ -1877,7 +1929,6 @@ impl Parser {
1877
1929
}
1878
1930
1879
1931
if self . consume_token ( & Token :: LParen ) {
1880
- let index = self . index ;
1881
1932
// A left paren introduces either a derived table (i.e., a subquery)
1882
1933
// or a nested join. It's nearly impossible to determine ahead of
1883
1934
// time which it is... so we just try to parse both.
@@ -1894,30 +1945,26 @@ impl Parser {
1894
1945
// | (2) starts a nested join
1895
1946
// (1) an additional set of parens around a nested join
1896
1947
//
1897
- match self . parse_derived_table_factor ( NotLateral ) {
1898
- // The recently consumed '(' started a derived table, and we've
1899
- // parsed the subquery, followed by the closing ')', and the
1900
- // alias of the derived table. In the example above this is
1901
- // case (3), and the next token would be `NATURAL`.
1902
- Ok ( table_factor) => Ok ( table_factor) ,
1903
- Err ( _) => {
1904
- // A parsing error from `parse_derived_table_factor` indicates that
1905
- // the '(' we've recently consumed does not start a derived table
1906
- // (cases 1, 2, or 4). Ignore the error and back up to where we
1907
- // were before - right after the opening '('.
1908
- self . index = index;
1909
-
1910
- // Inside the parentheses we expect to find a table factor
1911
- // followed by some joins or another level of nesting.
1912
- let table_and_joins = self . parse_table_and_joins ( ) ?;
1913
- self . expect_token ( & Token :: RParen ) ?;
1914
- // The SQL spec prohibits derived and bare tables from appearing
1915
- // alone in parentheses. We don't enforce this as some databases
1916
- // (e.g. Snowflake) allow such syntax.
1917
1948
1918
- Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
1919
- }
1920
- }
1949
+ // Check if the recently consumed '(' started a derived table, in which case we've
1950
+ // parsed the subquery, followed by the closing ')', and the alias of the derived
1951
+ // table. In the example above this is case (3).
1952
+ //
1953
+ // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
1954
+ // recently consumed does not start a derived table (cases 1, 2, or 4). Ignore the
1955
+ // error and back up to where we after the opening '('.
1956
+ return_ok_if_some ! (
1957
+ self . maybe_parse( |parser| parser. parse_derived_table_factor( NotLateral ) )
1958
+ ) ;
1959
+
1960
+ // Inside the parentheses we expect to find a table factor
1961
+ // followed by some joins or another level of nesting.
1962
+ let table_and_joins = self . parse_table_and_joins ( ) ?;
1963
+ self . expect_token ( & Token :: RParen ) ?;
1964
+ // The SQL spec prohibits derived and bare tables from appearing
1965
+ // alone in parentheses. We don't enforce this as some databases
1966
+ // (e.g. Snowflake) allow such syntax.
1967
+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
1921
1968
} else {
1922
1969
let name = self . parse_object_name ( ) ?;
1923
1970
// Postgres, MSSQL: table-valued functions:
0 commit comments