@@ -26,7 +26,7 @@ use datafusion_common::{
2626} ;
2727use datafusion_expr:: {
2828 expr:: { Alias , Exists , InList , ScalarFunction , Sort , WindowFunction } ,
29- Between , BinaryExpr , Case , Cast , Expr , Like , Operator ,
29+ Between , BinaryExpr , Case , Cast , Expr , Like , Operator , TryCast ,
3030} ;
3131use sqlparser:: ast:: {
3232 self , Expr as AstExpr , Function , FunctionArg , Ident , UnaryOperator ,
@@ -400,7 +400,14 @@ impl Unparser<'_> {
400400 Expr :: GetIndexedField ( _) => {
401401 not_impl_err ! ( "Unsupported Expr conversion: {expr:?}" )
402402 }
403- Expr :: TryCast ( _) => not_impl_err ! ( "Unsupported Expr conversion: {expr:?}" ) ,
403+ Expr :: TryCast ( TryCast { expr, data_type } ) => {
404+ let inner_expr = self . expr_to_sql ( expr) ?;
405+ Ok ( ast:: Expr :: TryCast {
406+ expr : Box :: new ( inner_expr) ,
407+ data_type : self . arrow_dtype_to_ast_dtype ( data_type) ?,
408+ format : None ,
409+ } )
410+ }
404411 Expr :: Wildcard { qualifier : _ } => {
405412 not_impl_err ! ( "Unsupported Expr conversion: {expr:?}" )
406413 }
@@ -867,8 +874,9 @@ mod tests {
867874 use datafusion_expr:: {
868875 case, col, exists,
869876 expr:: { AggregateFunction , AggregateFunctionDefinition } ,
870- lit, not, not_exists, table_scan, when, wildcard, ColumnarValue , ScalarUDF ,
871- ScalarUDFImpl , Signature , Volatility , WindowFrame , WindowFunctionDefinition ,
877+ lit, not, not_exists, table_scan, try_cast, when, wildcard, ColumnarValue ,
878+ ScalarUDF , ScalarUDFImpl , Signature , Volatility , WindowFrame ,
879+ WindowFunctionDefinition ,
872880 } ;
873881
874882 use crate :: unparser:: dialect:: CustomDialect ;
@@ -1144,6 +1152,14 @@ mod tests {
11441152 not_exists( Arc :: new( dummy_logical_plan. clone( ) ) ) ,
11451153 r#"NOT EXISTS (SELECT "t"."a" FROM "t" WHERE ("t"."a" = 1))"# ,
11461154 ) ,
1155+ (
1156+ try_cast( col( "a" ) , DataType :: Date64 ) ,
1157+ r#"TRY_CAST("a" AS DATETIME)"# ,
1158+ ) ,
1159+ (
1160+ try_cast( col( "a" ) , DataType :: UInt32 ) ,
1161+ r#"TRY_CAST("a" AS INTEGER UNSIGNED)"# ,
1162+ ) ,
11471163 ] ;
11481164
11491165 for ( expr, expected) in tests {
0 commit comments