|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::any::Any; |
| 19 | + |
| 20 | +use datafusion_common::config::{ |
| 21 | + ConfigEntry, ConfigExtension, ConfigField, ExtensionOptions, Visit, |
| 22 | +}; |
| 23 | +use datafusion_common::{DataFusionError, config_err}; |
| 24 | +use datafusion_ffi::config::extension_options::FFI_ExtensionOptions; |
| 25 | +use pyo3::exceptions::PyRuntimeError; |
| 26 | +use pyo3::types::PyCapsule; |
| 27 | +use pyo3::{Bound, PyResult, Python, pyclass, pymethods}; |
| 28 | + |
| 29 | +/// My own config options. |
| 30 | +#[pyclass( |
| 31 | + from_py_object, |
| 32 | + name = "MyConfig", |
| 33 | + module = "datafusion_ffi_example", |
| 34 | + subclass |
| 35 | +)] |
| 36 | +#[derive(Clone, Debug)] |
| 37 | +pub struct MyConfig { |
| 38 | + /// Should "foo" be replaced by "bar"? |
| 39 | + pub foo_to_bar: bool, |
| 40 | + |
| 41 | + /// How many "baz" should be created? |
| 42 | + pub baz_count: usize, |
| 43 | +} |
| 44 | + |
| 45 | +#[pymethods] |
| 46 | +impl MyConfig { |
| 47 | + #[new] |
| 48 | + fn new() -> Self { |
| 49 | + Self::default() |
| 50 | + } |
| 51 | + |
| 52 | + fn __datafusion_extension_options__<'py>( |
| 53 | + &self, |
| 54 | + py: Python<'py>, |
| 55 | + ) -> PyResult<Bound<'py, PyCapsule>> { |
| 56 | + let name = cr"datafusion_extension_options".into(); |
| 57 | + |
| 58 | + let mut config = FFI_ExtensionOptions::default(); |
| 59 | + config |
| 60 | + .add_config(self) |
| 61 | + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; |
| 62 | + |
| 63 | + PyCapsule::new(py, config, Some(name)) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Default for MyConfig { |
| 68 | + fn default() -> Self { |
| 69 | + Self { |
| 70 | + foo_to_bar: true, |
| 71 | + baz_count: 1337, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl ConfigExtension for MyConfig { |
| 77 | + const PREFIX: &'static str = "my_config"; |
| 78 | +} |
| 79 | + |
| 80 | +impl ExtensionOptions for MyConfig { |
| 81 | + fn as_any(&self) -> &dyn Any { |
| 82 | + self |
| 83 | + } |
| 84 | + |
| 85 | + fn as_any_mut(&mut self) -> &mut dyn Any { |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + fn cloned(&self) -> Box<dyn ExtensionOptions> { |
| 90 | + Box::new(self.clone()) |
| 91 | + } |
| 92 | + |
| 93 | + fn set(&mut self, key: &str, value: &str) -> datafusion_common::Result<()> { |
| 94 | + datafusion_common::config::ConfigField::set(self, key, value) |
| 95 | + } |
| 96 | + |
| 97 | + fn entries(&self) -> Vec<ConfigEntry> { |
| 98 | + vec![ |
| 99 | + ConfigEntry { |
| 100 | + key: "foo_to_bar".to_owned(), |
| 101 | + value: Some(format!("{}", self.foo_to_bar)), |
| 102 | + description: "foo to bar", |
| 103 | + }, |
| 104 | + ConfigEntry { |
| 105 | + key: "baz_count".to_owned(), |
| 106 | + value: Some(format!("{}", self.baz_count)), |
| 107 | + description: "baz count", |
| 108 | + }, |
| 109 | + ] |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl ConfigField for MyConfig { |
| 114 | + fn visit<V: Visit>(&self, v: &mut V, _key: &str, _description: &'static str) { |
| 115 | + let key = "foo_to_bar"; |
| 116 | + let desc = "foo to bar"; |
| 117 | + self.foo_to_bar.visit(v, key, desc); |
| 118 | + |
| 119 | + let key = "baz_count"; |
| 120 | + let desc = "baz count"; |
| 121 | + self.baz_count.visit(v, key, desc); |
| 122 | + } |
| 123 | + |
| 124 | + fn set(&mut self, key: &str, value: &str) -> Result<(), DataFusionError> { |
| 125 | + let (key, rem) = key.split_once('.').unwrap_or((key, "")); |
| 126 | + match key { |
| 127 | + "foo_to_bar" => self.foo_to_bar.set(rem, value.as_ref()), |
| 128 | + "baz_count" => self.baz_count.set(rem, value.as_ref()), |
| 129 | + |
| 130 | + _ => config_err!("Config value \"{}\" not found on MyConfig", key), |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments