duckdb-rs
duckdb-rs copied to clipboard
is there any way to add extra_info to scalar funtion or table funtion.
I need to register glabal state to scalar function or table function. but I found that extra_info for register operation is not easy to understand.
Can anyone provide some examples to use extra_info in extension to share state or struct.
thanks.
/// Register the given ScalarFunction with the current db
#[inline]
pub fn register_scalar_function<S: VScalar>(&self, name: &str) -> crate::Result<()> {
let set = ScalarFunctionSet::new(name);
for signature in S::signatures() {
let scalar_function = ScalarFunction::new(name)?;
signature.register_with_scalar(&scalar_function);
scalar_function.set_function(Some(scalar_func::<S>));
scalar_function.set_extra_info::<S::State>();
set.add_function(scalar_function)?;
}
self.db.borrow_mut().register_scalar_function_set(set)
}
but set_extra_info use State::default() to register state. is there anyway to pass context into the extra_info.
Thanks for bringing this up.
As you noted, the current API only allows setting extra_info with T::default(), which isn't very helpful when it comes to passing custom context to scalar functions.
I'll spend some time improving the API.
@Colory Do you mind explaining your actual use case? You can certainly access global state in scalars using Rust primitives, but I understand that it might be hacky.
#558 introduces register_scalar_function_with_state, which does exactly what you need.
@Colory Do you mind explaining your actual use case? You can certainly access global state in scalars using Rust primitives, but I understand that it might be hacky.
Thanks. Because I want to maintain a global state and share states into it. I aim to implement a function similar to that of duckdb_ui, maintaining a server state.