alamb commented on PR #7978:
URL: 
https://github.com/apache/arrow-datafusion/pull/7978#issuecomment-1787877801

   As somewhat of an aside, I implemented a user defined function today (a 
backwards compatible implementation of `to_timestamp`) and it is quite annoying 
to have to provide the different callbacks as `Arc` around dynamic functions. 
Thank you for pushing this forward
   
   <details><summary>Example User Defined Function</summary>
   <p>
   
   ```rust
   //! Implementation of `to_timestamp` function that
   //! overrides the built in version in DataFusion because the semantics 
changed
   //! upstream: <https://github.com/apache/arrow-datafusion/pull/7844>
   use std::sync::Arc;
   
   use arrow::datatypes::DataType;
   use arrow::datatypes::TimeUnit;
   use datafusion::common::internal_err;
   use datafusion::logical_expr::{ReturnTypeFunction, Signature};
   use datafusion::physical_expr::datetime_expressions;
   use datafusion::physical_expr::expressions::cast_column;
   use datafusion::{
       error::DataFusionError,
       logical_expr::{ScalarFunctionImplementation, ScalarUDF, Volatility},
       physical_plan::ColumnarValue,
   };
   use once_cell::sync::Lazy;
   
   /// The name of the function
   pub const TO_TIMESTAMP_FUNCTION_NAME: &str = "to_timestamp";
   
   /// Implementation of to_timestamp
   pub(crate) static TO_TIMESTAMP_UDF: Lazy<Arc<ScalarUDF>> = Lazy::new(|| {
       Arc::new(ScalarUDF::new(
           TO_TIMESTAMP_FUNCTION_NAME,
           &Signature::uniform(
               1,
               vec![
                   DataType::Int64,
                   DataType::Timestamp(TimeUnit::Nanosecond, None),
                   DataType::Timestamp(TimeUnit::Microsecond, None),
                   DataType::Timestamp(TimeUnit::Millisecond, None),
                   DataType::Timestamp(TimeUnit::Second, None),
                   DataType::Utf8,
               ],
               Volatility::Immutable,
           ),
           &TO_TIMESTAMP_RETURN_TYPE,
           &TO_TIMESTAMP_IMPL,
       ))
   });
   
   static TO_TIMESTAMP_RETURN_TYPE: Lazy<ReturnTypeFunction> = Lazy::new(|| {
       let func =
           |_arg_types: &[DataType]| 
Ok(Arc::new(DataType::Timestamp(TimeUnit::Nanosecond, None)));
   
       Arc::new(func)
   });
   
   static TO_TIMESTAMP_IMPL: Lazy<ScalarFunctionImplementation> = Lazy::new(|| {
       let func = |args: &[ColumnarValue]| {
           if args.len() != 1 {
               return internal_err!("to_timestamp expected 1 argument, got {}", 
args.len());
           }
   
           match args[0].data_type() {
               // call through to arrow cast kernel
               DataType::Int64 | DataType::Timestamp(_, _) => cast_column(
                   &args[0],
                   &DataType::Timestamp(TimeUnit::Nanosecond, None),
                   None,
               ),
               DataType::Utf8 => datetime_expressions::to_timestamp_nanos(args),
               dt => internal_err!("to_timestamp does not support argument type 
'{dt}'"),
           }
       };
   
       Arc::new(func)
   });
   
   // https://github.com/apache/arrow-datafusion/pull/7844
   ```
   
   </p>
   </details> 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to