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