@@ -45,6 +45,18 @@ pub struct ColumnStatistics {
4545 pub has_null : bool ,
4646}
4747
48+ // String map lifetime is managed by C++ code
49+ crate :: lifetime_wrapper!( DuckdbStringMap , cpp:: duckdb_vx_string_map, |_| { } ) ;
50+ impl DuckdbStringMapRef {
51+ pub fn push ( & mut self , key : & str , value : & str ) {
52+ let key = CString :: new ( key) . unwrap_or_else ( |_| CString :: default ( ) ) ;
53+ let value = CString :: new ( value) . unwrap_or_else ( |_| CString :: default ( ) ) ;
54+ unsafe {
55+ cpp:: duckdb_vx_string_map_insert ( self . as_ptr ( ) , key. as_ptr ( ) , value. as_ptr ( ) ) ;
56+ }
57+ }
58+ }
59+
4860/// A trait that defines the supported operations for a table function in DuckDB.
4961///
5062/// This trait does not yet cover the full C++ API, see table_function.hpp.
@@ -154,9 +166,7 @@ pub trait TableFunction: Sized + Debug {
154166 ) -> VortexResult < u64 > ;
155167
156168 /// Returns a vector of key-value pairs for EXPLAIN output
157- fn to_string ( _bind_data : & Self :: BindData ) -> Option < Vec < ( String , String ) > > {
158- None
159- }
169+ fn to_string ( bind_data : & Self :: BindData , map : & mut DuckdbStringMapRef ) ;
160170
161171 // TODO(ngates): there are many more callbacks that can be configured.
162172}
@@ -223,35 +233,13 @@ impl DatabaseRef {
223233 }
224234}
225235
226- /// The to_string callback for a table function.
227236unsafe extern "C-unwind" fn to_string_callback < T : TableFunction > (
228237 bind_data : * mut c_void ,
229- ) -> cpp:: duckdb_vx_string_map {
238+ map : cpp:: duckdb_vx_string_map ,
239+ ) {
230240 let bind_data = unsafe { & * ( bind_data as * const T :: BindData ) } ;
231-
232- match T :: to_string ( bind_data) {
233- Some ( map) => {
234- // Create a new C++ map
235- let cpp_map = unsafe { cpp:: duckdb_vx_string_map_create ( ) } ;
236-
237- // Fill the map with key-value pairs
238- for ( key, value) in map {
239- let key_cstr = CString :: new ( key) . unwrap_or_else ( |_| CString :: default ( ) ) ;
240- let value_cstr = CString :: new ( value) . unwrap_or_else ( |_| CString :: default ( ) ) ;
241-
242- unsafe {
243- cpp:: duckdb_vx_string_map_insert (
244- cpp_map,
245- key_cstr. as_ptr ( ) ,
246- value_cstr. as_ptr ( ) ,
247- ) ;
248- }
249- }
250-
251- cpp_map
252- }
253- None => ptr:: null_mut ( ) ,
254- }
241+ let map = unsafe { DuckdbStringMap :: borrow_mut ( map) } ;
242+ T :: to_string ( bind_data, map) ;
255243}
256244
257245/// The native function callback for a table function.
0 commit comments