@@ -10,7 +10,12 @@ use pyo3::types::PyCapsule;
1010use pyo3:: { Bound , PyResult , Python , pyclass, pymethods} ;
1111
1212/// My own config options.
13- #[ pyclass( name = "MyConfig" , module = "datafusion_ffi_example" , subclass) ]
13+ #[ pyclass(
14+ from_py_object,
15+ name = "MyConfig" ,
16+ module = "datafusion_ffi_example" ,
17+ subclass
18+ ) ]
1419#[ derive( Clone , Debug ) ]
1520pub struct MyConfig {
1621 /// Should "foo" be replaced by "bar"?
@@ -109,3 +114,90 @@ impl ConfigField for MyConfig {
109114 }
110115 }
111116}
117+
118+ /// My own table options.
119+ #[ pyclass(
120+ from_py_object,
121+ name = "MyTableOptions" ,
122+ module = "datafusion_ffi_example" ,
123+ subclass
124+ ) ]
125+ #[ derive( Clone , Debug ) ]
126+ pub struct MyTableOptions {
127+ /// A random option name for testing
128+ pub some_option : usize ,
129+ }
130+
131+ #[ pymethods]
132+ impl MyTableOptions {
133+ #[ new]
134+ fn new ( ) -> Self {
135+ Self :: default ( )
136+ }
137+
138+ fn __datafusion_extension_options__ < ' py > (
139+ & self ,
140+ py : Python < ' py > ,
141+ ) -> PyResult < Bound < ' py , PyCapsule > > {
142+ let name = cr"datafusion_extension_options" . into ( ) ;
143+
144+ let mut config = FFI_ExtensionOptions :: default ( ) ;
145+ config
146+ . add_config ( self )
147+ . map_err ( |e| PyRuntimeError :: new_err ( e. to_string ( ) ) ) ?;
148+
149+ PyCapsule :: new ( py, config, Some ( name) )
150+ }
151+ }
152+
153+ impl Default for MyTableOptions {
154+ fn default ( ) -> Self {
155+ Self { some_option : 1337 }
156+ }
157+ }
158+
159+ impl ConfigExtension for MyTableOptions {
160+ const PREFIX : & ' static str = "my_format" ;
161+ }
162+
163+ impl ExtensionOptions for MyTableOptions {
164+ fn as_any ( & self ) -> & dyn Any {
165+ self
166+ }
167+
168+ fn as_any_mut ( & mut self ) -> & mut dyn Any {
169+ self
170+ }
171+
172+ fn cloned ( & self ) -> Box < dyn ExtensionOptions > {
173+ Box :: new ( self . clone ( ) )
174+ }
175+
176+ fn set ( & mut self , key : & str , value : & str ) -> datafusion_common:: Result < ( ) > {
177+ datafusion_common:: config:: ConfigField :: set ( self , key, value)
178+ }
179+
180+ fn entries ( & self ) -> Vec < ConfigEntry > {
181+ vec ! [ ConfigEntry {
182+ key: "some_option" . to_owned( ) ,
183+ value: Some ( format!( "{}" , self . some_option) ) ,
184+ description: "some description" ,
185+ } ]
186+ }
187+ }
188+
189+ impl ConfigField for MyTableOptions {
190+ fn visit < V : Visit > ( & self , v : & mut V , _key : & str , _description : & ' static str ) {
191+ let key = "some_option" ;
192+ let desc = "some description" ;
193+ self . some_option . visit ( v, key, desc) ;
194+ }
195+
196+ fn set ( & mut self , key : & str , value : & str ) -> Result < ( ) , DataFusionError > {
197+ let ( key, rem) = key. split_once ( '.' ) . unwrap_or ( ( key, "" ) ) ;
198+ match key {
199+ "some_option" => self . some_option . set ( rem, value. as_ref ( ) ) ,
200+ _ => config_err ! ( "Config value \" {}\" not found on MyTableOptions" , key) ,
201+ }
202+ }
203+ }
0 commit comments