@@ -1528,19 +1528,23 @@ impl<'a> Parser<'a> {
1528
1528
pub fn parse_optional_cast_format ( & mut self ) -> Result < Option < CastFormat > , ParserError > {
1529
1529
if self . parse_keyword ( Keyword :: FORMAT ) {
1530
1530
let value = self . parse_value ( ) ?;
1531
- if self . parse_keywords ( & [ Keyword :: AT , Keyword :: TIME , Keyword :: ZONE ] ) {
1532
- Ok ( Some ( CastFormat :: ValueAtTimeZone (
1533
- value,
1534
- self . parse_value ( ) ?,
1535
- ) ) )
1536
- } else {
1537
- Ok ( Some ( CastFormat :: Value ( value) ) )
1531
+ match self . parse_optional_time_zone ( ) ? {
1532
+ Some ( tz) => Ok ( Some ( CastFormat :: ValueAtTimeZone ( value, tz) ) ) ,
1533
+ None => Ok ( Some ( CastFormat :: Value ( value) ) ) ,
1538
1534
}
1539
1535
} else {
1540
1536
Ok ( None )
1541
1537
}
1542
1538
}
1543
1539
1540
+ pub fn parse_optional_time_zone ( & mut self ) -> Result < Option < Value > , ParserError > {
1541
+ if self . parse_keywords ( & [ Keyword :: AT , Keyword :: TIME , Keyword :: ZONE ] ) {
1542
+ self . parse_value ( ) . map ( Some )
1543
+ } else {
1544
+ Ok ( None )
1545
+ }
1546
+ }
1547
+
1544
1548
/// mssql-like convert function
1545
1549
fn parse_mssql_convert ( & mut self ) -> Result < Expr , ParserError > {
1546
1550
self . expect_token ( & Token :: LParen ) ?;
@@ -2534,12 +2538,35 @@ impl<'a> Parser<'a> {
2534
2538
) ,
2535
2539
}
2536
2540
} else if Token :: DoubleColon == tok {
2537
- Ok ( Expr :: Cast {
2541
+ let data_type = self . parse_data_type ( ) ?;
2542
+
2543
+ let cast_expr = Expr :: Cast {
2538
2544
kind : CastKind :: DoubleColon ,
2539
2545
expr : Box :: new ( expr) ,
2540
- data_type : self . parse_data_type ( ) ? ,
2546
+ data_type : data_type . clone ( ) ,
2541
2547
format : None ,
2542
- } )
2548
+ } ;
2549
+
2550
+ match data_type {
2551
+ DataType :: Date
2552
+ | DataType :: Datetime ( _)
2553
+ | DataType :: Timestamp ( _, _)
2554
+ | DataType :: Time ( _, _) => {
2555
+ let value = self . parse_optional_time_zone ( ) ?;
2556
+ match value {
2557
+ Some ( Value :: SingleQuotedString ( tz) ) => Ok ( Expr :: AtTimeZone {
2558
+ timestamp : Box :: new ( cast_expr) ,
2559
+ time_zone : tz,
2560
+ } ) ,
2561
+ None => Ok ( cast_expr) ,
2562
+ _ => Err ( ParserError :: ParserError ( format ! (
2563
+ "Expected Token::SingleQuotedString after AT TIME ZONE, but found: {}" ,
2564
+ value. unwrap( )
2565
+ ) ) ) ,
2566
+ }
2567
+ }
2568
+ _ => Ok ( cast_expr) ,
2569
+ }
2543
2570
} else if Token :: ExclamationMark == tok {
2544
2571
// PostgreSQL factorial operation
2545
2572
Ok ( Expr :: UnaryOp {
0 commit comments