duckdb_extension_framework/table_functions/
table_function.rs

1use crate::duckly::{
2    duckdb_create_table_function, duckdb_delete_callback_t, duckdb_destroy_table_function,
3    duckdb_table_function, duckdb_table_function_add_parameter, duckdb_table_function_init_t,
4    duckdb_table_function_set_bind, duckdb_table_function_set_extra_info,
5    duckdb_table_function_set_function, duckdb_table_function_set_init,
6    duckdb_table_function_set_local_init, duckdb_table_function_set_name,
7    duckdb_table_function_supports_projection_pushdown,
8};
9use crate::logical_type::LogicalType;
10#[allow(unused)]
11use crate::table_functions::InitInfo;
12use std::ffi::{c_void, CString};
13
14/// A function that returns a queryable table
15#[derive(Debug)]
16pub struct TableFunction {
17    pub(crate) ptr: duckdb_table_function,
18}
19
20impl Drop for TableFunction {
21    fn drop(&mut self) {
22        unsafe {
23            duckdb_destroy_table_function(&mut self.ptr);
24        }
25    }
26}
27
28impl TableFunction {
29    /// Sets whether or not the given table function supports projection pushdown.
30    ///
31    /// If this is set to true, the system will provide a list of all required columns in the `init` stage through
32    /// the [`InitInfo::get_column_indices`] method.
33    /// If this is set to false (the default), the system will expect all columns to be projected.
34    ///
35    /// # Arguments
36    ///  * `pushdown`: True if the table function supports projection pushdown, false otherwise.
37    pub fn supports_pushdown(&self, supports: bool) -> &Self {
38        unsafe {
39            duckdb_table_function_supports_projection_pushdown(self.ptr, supports);
40        }
41        self
42    }
43
44    /// Adds a parameter to the table function.
45    ///
46    /// # Arguments
47    ///  * `logical_type`: The type of the parameter to add.
48    pub fn add_parameter(&self, logical_type: &LogicalType) -> &Self {
49        unsafe {
50            duckdb_table_function_add_parameter(self.ptr, logical_type.typ);
51        }
52        self
53    }
54
55    /// Sets the main function of the table function
56    ///
57    /// # Arguments
58    ///  * `function`: The function
59    pub fn set_function(
60        &self,
61        func: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
62    ) -> &Self {
63        unsafe {
64            duckdb_table_function_set_function(self.ptr, func);
65        }
66        self
67    }
68
69    /// Sets the init function of the table function
70    ///
71    /// # Arguments
72    ///  * `function`: The init function
73    pub fn set_init(&self, init_func: Option<unsafe extern "C" fn(*mut c_void)>) -> &Self {
74        unsafe {
75            duckdb_table_function_set_init(self.ptr, init_func);
76        }
77        self
78    }
79
80    /// Sets the bind function of the table function
81    ///
82    /// # Arguments
83    ///  * `function`: The bind function
84    pub fn set_bind(&self, bind_func: Option<unsafe extern "C" fn(*mut c_void)>) -> &Self {
85        unsafe {
86            duckdb_table_function_set_bind(self.ptr, bind_func);
87        }
88        self
89    }
90
91    /// Creates a new empty table function.
92    pub fn new() -> Self {
93        Self {
94            ptr: unsafe { duckdb_create_table_function() },
95        }
96    }
97
98    /// Sets the name of the given table function.
99    ///
100    /// # Arguments
101    ///  * `name`: The name of the table function
102    pub fn set_name(&self, name: &str) -> &TableFunction {
103        unsafe {
104            let string = CString::from_vec_unchecked(name.as_bytes().into());
105            duckdb_table_function_set_name(self.ptr, string.as_ptr());
106        }
107        self
108    }
109
110    /// Assigns extra information to the table function that can be fetched during binding, etc.
111    ///
112    /// # Arguments
113    /// * `extra_info`: The extra information
114    /// * `destroy`: The callback that will be called to destroy the bind data (if any)
115    ///
116    /// # Safety
117    pub unsafe fn set_extra_info(
118        &self,
119        extra_info: *mut c_void,
120        destroy: duckdb_delete_callback_t,
121    ) {
122        duckdb_table_function_set_extra_info(self.ptr, extra_info, destroy);
123    }
124
125    /// Sets the thread-local init function of the table function
126    ///
127    /// # Arguments
128    /// * `init`: The init function
129    pub fn set_local_init(&self, init: duckdb_table_function_init_t) {
130        unsafe { duckdb_table_function_set_local_init(self.ptr, init) };
131    }
132}
133impl Default for TableFunction {
134    fn default() -> Self {
135        Self::new()
136    }
137}