@@ -43,6 +43,14 @@ macro_rules! parser_err {
43
43
} ;
44
44
}
45
45
46
+ macro_rules! maybe {
47
+ ( $e: expr) => { {
48
+ if let Some ( v) = $e {
49
+ return Ok ( v) ;
50
+ }
51
+ } } ;
52
+ }
53
+
46
54
#[ derive( Debug , Clone , PartialEq ) ]
47
55
pub struct ParserError {
48
56
/// Original query (so we can easily print an error)
@@ -245,6 +253,34 @@ impl Parser {
245
253
246
254
/// Parse an expression prefix
247
255
pub fn parse_prefix ( & mut self ) -> Result < Expr , ParserError > {
256
+ // PostgreSQL allows any string literal to be preceded by a type name,
257
+ // indicating that the string literal represents a literal of that type.
258
+ // Some examples:
259
+ //
260
+ // DATE '2020-05-20'
261
+ // TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
262
+ // BOOL 'true'
263
+ //
264
+ // The first two are standard SQL, while the latter is a PostgreSQL
265
+ // extension. Complicating matters is the fact that INTERVAL string
266
+ // literals may optionally be followed by some special keywords, e.g.:
267
+ //
268
+ // INTERVAL '7' DAY
269
+ //
270
+ // Note also that naively `SELECT date` looks like a syntax error
271
+ // because the `date` type name is not followed by a string literal, but
272
+ // in fact is a valid expression that should parse as the column name
273
+ // "date".
274
+ maybe ! ( self . maybe_parse( |parser| {
275
+ match parser. parse_data_type( ) ? {
276
+ DataType :: Interval => parser. parse_literal_interval( ) ,
277
+ data_type => Ok ( Expr :: TypedString {
278
+ data_type,
279
+ value: parser. parse_literal_string( ) ?,
280
+ } ) ,
281
+ }
282
+ } ) ) ;
283
+
248
284
let tok = self
249
285
. next_token ( )
250
286
. ok_or_else ( || self . error ( self . peek_prev_range ( ) , "Unexpected EOF" . to_string ( ) ) ) ?;
@@ -257,17 +293,13 @@ impl Parser {
257
293
"LIST" => self . parse_list ( ) ,
258
294
"CASE" => self . parse_case_expr ( ) ,
259
295
"CAST" => self . parse_cast_expr ( ) ,
260
- "DATE" => Ok ( Expr :: Value ( self . parse_date ( ) ?) ) ,
261
296
"EXISTS" => self . parse_exists_expr ( ) ,
262
297
"EXTRACT" => self . parse_extract_expr ( ) ,
263
298
"INTERVAL" => self . parse_literal_interval ( ) ,
264
299
"NOT" => Ok ( Expr :: UnaryOp {
265
300
op : UnaryOperator :: Not ,
266
301
expr : Box :: new ( self . parse_subexpr ( Precedence :: UnaryNot ) ?) ,
267
302
} ) ,
268
- "TIME" => Ok ( Expr :: Value ( self . parse_time ( ) ?) ) ,
269
- "TIMESTAMP" => self . parse_timestamp ( ) ,
270
- "TIMESTAMPTZ" => self . parse_timestamptz ( ) ,
271
303
// Here `w` is a word, check if it's a part of a multi-part
272
304
// identifier, a function call, or a simple identifier:
273
305
w if keywords:: RESERVED_FOR_EXPRESSIONS . contains ( & w) => {
@@ -546,33 +578,6 @@ impl Parser {
546
578
}
547
579
}
548
580
549
- fn parse_date ( & mut self ) -> Result < Value , ParserError > {
550
- let value = self . parse_literal_string ( ) ?;
551
- Ok ( Value :: Date ( value) )
552
- }
553
-
554
- fn parse_time ( & mut self ) -> Result < Value , ParserError > {
555
- let value = self . parse_literal_string ( ) ?;
556
- Ok ( Value :: Time ( value) )
557
- }
558
-
559
- fn parse_timestamp ( & mut self ) -> Result < Expr , ParserError > {
560
- if self . parse_keyword ( "WITH" ) {
561
- self . expect_keywords ( & [ "TIME" , "ZONE" ] ) ?;
562
- let value = self . parse_literal_string ( ) ?;
563
- return Ok ( Expr :: Value ( Value :: TimestampTz ( value) ) ) ;
564
- } else if self . parse_keyword ( "WITHOUT" ) {
565
- self . expect_keywords ( & [ "TIME" , "ZONE" ] ) ?;
566
- }
567
- let value = self . parse_literal_string ( ) ?;
568
- Ok ( Expr :: Value ( Value :: Timestamp ( value) ) )
569
- }
570
-
571
- fn parse_timestamptz ( & mut self ) -> Result < Expr , ParserError > {
572
- let value = self . parse_literal_string ( ) ?;
573
- Ok ( Expr :: Value ( Value :: TimestampTz ( value) ) )
574
- }
575
-
576
581
/// Parse an INTERVAL literal.
577
582
///
578
583
/// Some syntactically valid intervals:
@@ -1103,6 +1108,20 @@ impl Parser {
1103
1108
Ok ( values)
1104
1109
}
1105
1110
1111
+ #[ must_use]
1112
+ fn maybe_parse < T , F > ( & mut self , mut f : F ) -> Option < T >
1113
+ where
1114
+ F : FnMut ( & mut Parser ) -> Result < T , ParserError > ,
1115
+ {
1116
+ let index = self . index ;
1117
+ if let Ok ( t) = f ( self ) {
1118
+ Some ( t)
1119
+ } else {
1120
+ self . index = index;
1121
+ None
1122
+ }
1123
+ }
1124
+
1106
1125
/// Parse a SQL CREATE statement
1107
1126
pub fn parse_create ( & mut self ) -> Result < Statement , ParserError > {
1108
1127
if self . parse_keyword ( "DATABASE" ) {
@@ -1895,7 +1914,7 @@ impl Parser {
1895
1914
}
1896
1915
// Interval types can be followed by a complicated interval
1897
1916
// qualifier that we don't currently support. See
1898
- // parse_interval_literal for a taste.
1917
+ // parse_literal_interval for a taste.
1899
1918
"INTERVAL" => DataType :: Interval ,
1900
1919
"REGCLASS" => DataType :: Regclass ,
1901
1920
"TEXT" | "STRING" => DataType :: Text ,
@@ -2498,7 +2517,6 @@ impl Parser {
2498
2517
}
2499
2518
2500
2519
if self . consume_token ( & Token :: LParen ) {
2501
- let index = self . index ;
2502
2520
// A left paren introduces either a derived table (i.e., a subquery)
2503
2521
// or a nested join. It's nearly impossible to determine ahead of
2504
2522
// time which it is... so we just try to parse both.
@@ -2515,39 +2533,36 @@ impl Parser {
2515
2533
// | (2) starts a nested join
2516
2534
// (1) an additional set of parens around a nested join
2517
2535
//
2518
- match self . parse_derived_table_factor ( NotLateral ) {
2519
- // The recently consumed '(' started a derived table, and we've
2520
- // parsed the subquery, followed by the closing ')', and the
2521
- // alias of the derived table. In the example above this is
2522
- // case (3), and the next token would be `NATURAL`.
2523
- Ok ( table_factor) => Ok ( table_factor) ,
2524
- Err ( _) => {
2525
- // The '(' we've recently consumed does not start a derived
2526
- // table. For valid input this can happen either when the
2527
- // token following the paren can't start a query (e.g. `foo`
2528
- // in `FROM (foo NATURAL JOIN bar)`, or when the '(' we've
2529
- // consumed is followed by another '(' that starts a
2530
- // derived table, like (3), or another nested join (2).
2531
- //
2532
- // Ignore the error and back up to where we were before.
2533
- // Either we'll be able to parse a valid nested join, or
2534
- // we won't, and we'll return that error instead.
2535
- self . index = index;
2536
- let table_and_joins = self . parse_table_and_joins ( ) ?;
2537
- match table_and_joins. relation {
2538
- TableFactor :: NestedJoin { .. } => ( ) ,
2539
- _ => {
2540
- if table_and_joins. joins . is_empty ( ) {
2541
- // The SQL spec prohibits derived tables and bare
2542
- // tables from appearing alone in parentheses.
2543
- self . expected ( self . peek_range ( ) , "joined table" , self . peek_token ( ) ) ?
2544
- }
2545
- }
2536
+
2537
+ // Check if the recently consumed '(' started a derived table, in
2538
+ // which case we've parsed the subquery, followed by the closing
2539
+ // ')', and the alias of the derived table. In the example above
2540
+ // this is case (3), and the next token would be `NATURAL`.
2541
+ maybe ! ( self . maybe_parse( |parser| parser. parse_derived_table_factor( NotLateral ) ) ) ;
2542
+
2543
+ // The '(' we've recently consumed does not start a derived table.
2544
+ // For valid input this can happen either when the token following
2545
+ // the paren can't start a query (e.g. `foo` in `FROM (foo NATURAL
2546
+ // JOIN bar)`, or when the '(' we've consumed is followed by another
2547
+ // '(' that starts a derived table, like (3), or another nested join
2548
+ // (2).
2549
+ //
2550
+ // Ignore the error and back up to where we were before. Either
2551
+ // we'll be able to parse a valid nested join, or we won't, and
2552
+ // we'll return that error instead.
2553
+ let table_and_joins = self . parse_table_and_joins ( ) ?;
2554
+ match table_and_joins. relation {
2555
+ TableFactor :: NestedJoin { .. } => ( ) ,
2556
+ _ => {
2557
+ if table_and_joins. joins . is_empty ( ) {
2558
+ // The SQL spec prohibits derived tables and bare
2559
+ // tables from appearing alone in parentheses.
2560
+ self . expected ( self . peek_range ( ) , "joined table" , self . peek_token ( ) ) ?
2546
2561
}
2547
- self . expect_token ( & Token :: RParen ) ?;
2548
- Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
2549
2562
}
2550
2563
}
2564
+ self . expect_token ( & Token :: RParen ) ?;
2565
+ Ok ( TableFactor :: NestedJoin ( Box :: new ( table_and_joins) ) )
2551
2566
} else {
2552
2567
let name = self . parse_object_name ( ) ?;
2553
2568
// Postgres, MSSQL: table-valued functions:
0 commit comments