This is an automated email from the ASF dual-hosted git repository.

mneumann pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 7535d93fa6 Implement Unparse TryCast Expr --> String Support (#10542)
7535d93fa6 is described below

commit 7535d93fa631f0fc42ca1521bc257ca20480b246
Author: Xin Li <[email protected]>
AuthorDate: Thu May 16 18:16:20 2024 +0800

    Implement Unparse TryCast Expr --> String Support (#10542)
    
    * TryCast Expr --> String Support
    
    * Fix format
---
 datafusion/sql/src/unparser/expr.rs | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/datafusion/sql/src/unparser/expr.rs 
b/datafusion/sql/src/unparser/expr.rs
index cd45cf9908..a9bfed5757 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -26,7 +26,7 @@ use datafusion_common::{
 };
 use datafusion_expr::{
     expr::{Alias, Exists, InList, ScalarFunction, Sort, WindowFunction},
-    Between, BinaryExpr, Case, Cast, Expr, Like, Operator,
+    Between, BinaryExpr, Case, Cast, Expr, Like, Operator, TryCast,
 };
 use sqlparser::ast::{
     self, Expr as AstExpr, Function, FunctionArg, Ident, UnaryOperator,
@@ -400,7 +400,14 @@ impl Unparser<'_> {
             Expr::GetIndexedField(_) => {
                 not_impl_err!("Unsupported Expr conversion: {expr:?}")
             }
-            Expr::TryCast(_) => not_impl_err!("Unsupported Expr conversion: 
{expr:?}"),
+            Expr::TryCast(TryCast { expr, data_type }) => {
+                let inner_expr = self.expr_to_sql(expr)?;
+                Ok(ast::Expr::TryCast {
+                    expr: Box::new(inner_expr),
+                    data_type: self.arrow_dtype_to_ast_dtype(data_type)?,
+                    format: None,
+                })
+            }
             Expr::Wildcard { qualifier: _ } => {
                 not_impl_err!("Unsupported Expr conversion: {expr:?}")
             }
@@ -867,8 +874,9 @@ mod tests {
     use datafusion_expr::{
         case, col, exists,
         expr::{AggregateFunction, AggregateFunctionDefinition},
-        lit, not, not_exists, table_scan, when, wildcard, ColumnarValue, 
ScalarUDF,
-        ScalarUDFImpl, Signature, Volatility, WindowFrame, 
WindowFunctionDefinition,
+        lit, not, not_exists, table_scan, try_cast, when, wildcard, 
ColumnarValue,
+        ScalarUDF, ScalarUDFImpl, Signature, Volatility, WindowFrame,
+        WindowFunctionDefinition,
     };
 
     use crate::unparser::dialect::CustomDialect;
@@ -1144,6 +1152,14 @@ mod tests {
                 not_exists(Arc::new(dummy_logical_plan.clone())),
                 r#"NOT EXISTS (SELECT "t"."a" FROM "t" WHERE ("t"."a" = 1))"#,
             ),
+            (
+                try_cast(col("a"), DataType::Date64),
+                r#"TRY_CAST("a" AS DATETIME)"#,
+            ),
+            (
+                try_cast(col("a"), DataType::UInt32),
+                r#"TRY_CAST("a" AS INTEGER UNSIGNED)"#,
+            ),
         ];
 
         for (expr, expected) in tests {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to