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

alamb 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 b80080ece5 Improve Unparser (scalar_to_sql) to respect dialect 
timestamp type overrides (#14407)
b80080ece5 is described below

commit b80080ece5d8f4ca3ff39f41369b0f8b002f2a4d
Author: Sergei Grebnov <[email protected]>
AuthorDate: Tue Feb 4 12:44:06 2025 -0800

    Improve Unparser (scalar_to_sql) to respect dialect timestamp type 
overrides (#14407)
---
 datafusion/sql/src/unparser/expr.rs | 42 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/datafusion/sql/src/unparser/expr.rs 
b/datafusion/sql/src/unparser/expr.rs
index 0aa3d4d3bc..56fea0023f 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -965,10 +965,21 @@ impl Unparser<'_> {
                 ))?
                 .to_string()
         };
+
+        let time_unit = match T::DATA_TYPE {
+            DataType::Timestamp(unit, _) => unit,
+            _ => {
+                return Err(internal_datafusion_err!(
+                    "Expected Timestamp, got {:?}",
+                    T::DATA_TYPE
+                ))
+            }
+        };
+
         Ok(ast::Expr::Cast {
             kind: ast::CastKind::Cast,
             expr: Box::new(ast::Expr::Value(SingleQuotedString(ts))),
-            data_type: ast::DataType::Timestamp(None, TimezoneInfo::None),
+            data_type: self.dialect.timestamp_cast_dtype(&time_unit, &None),
             format: None,
         })
     }
@@ -2581,6 +2592,35 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn custom_dialect_with_timestamp_cast_dtype_scalar_expr() -> Result<()> {
+        let default_dialect = CustomDialectBuilder::new().build();
+        let mysql_dialect = CustomDialectBuilder::new()
+            .with_timestamp_cast_dtype(
+                ast::DataType::Datetime(None),
+                ast::DataType::Datetime(None),
+            )
+            .build();
+
+        for (dialect, identifier) in [
+            (&default_dialect, "TIMESTAMP"),
+            (&mysql_dialect, "DATETIME"),
+        ] {
+            let unparser = Unparser::new(dialect);
+            let expr = Expr::Literal(ScalarValue::TimestampMillisecond(
+                Some(1738285549123),
+                None,
+            ));
+            let ast = unparser.expr_to_sql(&expr)?;
+
+            let actual = format!("{}", ast);
+            let expected = format!(r#"CAST('2025-01-31 01:05:49.123' AS 
{identifier})"#);
+
+            assert_eq!(actual, expected);
+        }
+        Ok(())
+    }
+
     #[test]
     fn custom_dialect_date32_ast_dtype() -> Result<()> {
         let default_dialect = CustomDialectBuilder::default().build();


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

Reply via email to