duckdb-rs icon indicating copy to clipboard operation
duckdb-rs copied to clipboard

is there any way to add extra_info to scalar funtion or table funtion.

Open Colory opened this issue 5 months ago • 1 comments

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.

Colory avatar Jun 04 '25 02:06 Colory

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

Colory avatar Jun 04 '25 02:06 Colory

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.

mlafeldt avatar Jul 31 '25 09:07 mlafeldt

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

mlafeldt avatar Jul 31 '25 09:07 mlafeldt

#558 introduces register_scalar_function_with_state, which does exactly what you need.

mlafeldt avatar Jul 31 '25 14:07 mlafeldt

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

Colory avatar Aug 08 '25 08:08 Colory