Skip to content

Commit e3d2c66

Browse files
committed
Change to register_table_function_with_options
1 parent 1395396 commit e3d2c66

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

crates/duckdb/src/vtab/function.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ impl BindInfo {
5555
/// Sets the user-provided bind data in the bind object. This object can be retrieved again during execution.
5656
///
5757
/// # Arguments
58-
/// * `extra_data`: The bind data object.
59-
/// * `destroy`: The callback that will be called to destroy the bind data (if any)
58+
/// * `data`: The bind data object.
59+
/// * `free_function`: The callback that will be called to destroy the bind data (if any)
60+
///
61+
/// # Safety
62+
/// `data` must be a valid pointer, and `free_function` must properly free it.
6063
pub unsafe fn set_bind_data(&self, data: *mut c_void, free_function: Option<unsafe extern "C" fn(*mut c_void)>) {
6164
duckdb_bind_set_bind_data(self.ptr, data, free_function);
6265
}

crates/duckdb/src/vtab/mod.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ mod excel;
2323
pub use function::{BindInfo, InitInfo, TableFunction, TableFunctionInfo};
2424
pub use value::Value;
2525

26+
/// Options for registering a table function.
27+
pub struct TableFunctionOptions<E> {
28+
/// Extra info passed to the function at runtime.
29+
/// Accessible via `BindInfo::get_extra_info`, `InitInfo::get_extra_info`,
30+
/// or `TableFunctionInfo::get_extra_info`.
31+
pub extra_info: Option<E>,
32+
}
33+
34+
impl<E> Default for TableFunctionOptions<E> {
35+
fn default() -> Self {
36+
Self { extra_info: None }
37+
}
38+
}
39+
2640
use crate::core::{DataChunkHandle, LogicalTypeHandle};
2741
use ffi::{duckdb_bind_info, duckdb_data_chunk, duckdb_function_info, duckdb_init_info};
2842

@@ -151,23 +165,26 @@ impl Connection {
151165
self.db.borrow_mut().register_table_function(table_function)
152166
}
153167

154-
/// Register the given TableFunction with custom extra info.
155-
///
156-
/// This allows you to pass extra info that can be accessed during bind, init, and execution
157-
/// via `BindInfo::get_extra_info`, `InitInfo::get_extra_info`, or `TableFunctionInfo::get_extra_info`.
168+
/// Register the given TableFunction with options.
158169
#[inline]
159-
pub fn register_table_function_with_extra_info<T: VTab, E>(&self, name: &str, extra_info: &E) -> Result<()>
170+
pub fn register_table_function_with_options<T: VTab, E>(
171+
&self,
172+
name: &str,
173+
options: TableFunctionOptions<E>,
174+
) -> Result<()>
160175
where
161-
E: Clone + Send + Sync + 'static,
176+
E: Send + Sync + 'static,
162177
{
163178
let table_function = TableFunction::default();
164179
table_function
165180
.set_name(name)
166181
.supports_pushdown(T::supports_pushdown())
167182
.set_bind(Some(bind::<T>))
168183
.set_init(Some(init::<T>))
169-
.set_function(Some(func::<T>))
170-
.with_extra_info(extra_info.clone());
184+
.set_function(Some(func::<T>));
185+
if let Some(extra_info) = options.extra_info {
186+
table_function.with_extra_info(extra_info);
187+
}
171188
for ty in T::parameters().unwrap_or_default() {
172189
table_function.add_parameter(&ty);
173190
}
@@ -354,9 +371,14 @@ mod test {
354371
}
355372

356373
#[test]
357-
fn test_table_function_with_extra_info() -> Result<(), Box<dyn Error>> {
374+
fn test_table_function_with_options() -> Result<(), Box<dyn Error>> {
358375
let conn = Connection::open_in_memory()?;
359-
conn.register_table_function_with_extra_info::<PrefixVTab, _>("greet", &"Howdy".to_string())?;
376+
conn.register_table_function_with_options::<PrefixVTab, _>(
377+
"greet",
378+
TableFunctionOptions {
379+
extra_info: Some("Howdy".to_string()),
380+
},
381+
)?;
360382

361383
let val = conn.query_row("select * from greet('partner')", [], |row| <(String,)>::try_from(row))?;
362384
assert_eq!(val, ("Howdy partner".to_string(),));

0 commit comments

Comments
 (0)