jayzhan211 commented on issue #9892:
URL: 
https://github.com/apache/arrow-datafusion/issues/9892#issuecomment-2041079289

   I draft the idea here
   ```rust
   use std::sync::OnceLock;
   
   pub type ScalarFactory = Box<dyn Fn() -> Arc<ScalarUDF> + Send + Sync>;
   
   /// HashMap Singleton for UDFs
   /// Replace register_all with our built-in functions
   pub fn scalar_functions() -> &'static Mutex<HashMap<String, ScalarFactory>> {
       static FUNCTIONS: OnceLock<Mutex<HashMap<String, ScalarFactory>>> = 
OnceLock::new();
       FUNCTIONS.get_or_init(|| {
           let mut functions = HashMap::new();
           functions.insert(
               String::from("array_to_string"),
               Box::new(string::array_to_string_udf) as _,
           );
           // TODO: Add more builtin functions here
           Mutex::new(functions)
       })
   }
   
   // Get an UDF by name
   //
   // Replace with `get_udf`
   // fn get_function_meta(&self, name: &str) -> Option<Arc<ScalarUDF>> {
   //     self.state.scalar_functions().get(name).cloned()
   // }
   pub fn get_udf(name: &str) -> Option<Arc<ScalarUDF>> {
       scalar_functions().lock().unwrap().get(name).map(|f| f())
   }
   
   /// Register a single new UDF
   /// 
   /// Repalce old regsiter_udf
   pub fn register_udf(name: &str, udf: ScalarFactory) -> Option<ScalarFactory> 
{
       scalar_functions().lock().unwrap().insert(name.to_string(), udf)
   }
   ```


-- 
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