-
Notifications
You must be signed in to change notification settings - Fork 151
Implement configuration extension support #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timsaucer
wants to merge
5
commits into
apache:main
Choose a base branch
from
timsaucer:feat/ffi-config-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
examples/datafusion-ffi-example/python/tests/_test_config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from datafusion import SessionConfig, SessionContext | ||
| from datafusion_ffi_example import MyConfig | ||
|
|
||
|
|
||
| def test_catalog_provider(): | ||
| config = MyConfig() | ||
| config = SessionConfig( | ||
| {"datafusion.catalog.information_schema": "true"} | ||
| ).with_extension(config) | ||
| config.set("my_config.baz_count", "42") | ||
| ctx = SessionContext(config) | ||
|
|
||
| result = ctx.sql("SHOW my_config.baz_count;").collect() | ||
| assert result[0][1][0].as_py() == "42" | ||
|
|
||
| ctx.sql("SET my_config.baz_count=1;") | ||
| result = ctx.sql("SHOW my_config.baz_count;").collect() | ||
| assert result[0][1][0].as_py() == "1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::any::Any; | ||
|
|
||
| use datafusion_common::config::{ | ||
| ConfigEntry, ConfigExtension, ConfigField, ExtensionOptions, Visit, | ||
| }; | ||
| use datafusion_common::{DataFusionError, config_err}; | ||
| use datafusion_ffi::config::extension_options::FFI_ExtensionOptions; | ||
| use pyo3::exceptions::PyRuntimeError; | ||
| use pyo3::types::PyCapsule; | ||
| use pyo3::{Bound, PyResult, Python, pyclass, pymethods}; | ||
|
|
||
| /// My own config options. | ||
| #[pyclass( | ||
| from_py_object, | ||
| name = "MyConfig", | ||
| module = "datafusion_ffi_example", | ||
| subclass | ||
| )] | ||
| #[derive(Clone, Debug)] | ||
| pub struct MyConfig { | ||
| /// Should "foo" be replaced by "bar"? | ||
| pub foo_to_bar: bool, | ||
|
|
||
| /// How many "baz" should be created? | ||
| pub baz_count: usize, | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl MyConfig { | ||
| #[new] | ||
| fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| fn __datafusion_extension_options__<'py>( | ||
| &self, | ||
| py: Python<'py>, | ||
| ) -> PyResult<Bound<'py, PyCapsule>> { | ||
| let name = cr"datafusion_extension_options".into(); | ||
|
|
||
| let mut config = FFI_ExtensionOptions::default(); | ||
| config | ||
| .add_config(self) | ||
| .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; | ||
|
|
||
| PyCapsule::new(py, config, Some(name)) | ||
| } | ||
| } | ||
|
|
||
| impl Default for MyConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| foo_to_bar: true, | ||
| baz_count: 1337, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ConfigExtension for MyConfig { | ||
| const PREFIX: &'static str = "my_config"; | ||
| } | ||
|
|
||
| impl ExtensionOptions for MyConfig { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn as_any_mut(&mut self) -> &mut dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn cloned(&self) -> Box<dyn ExtensionOptions> { | ||
| Box::new(self.clone()) | ||
| } | ||
|
|
||
| fn set(&mut self, key: &str, value: &str) -> datafusion_common::Result<()> { | ||
| datafusion_common::config::ConfigField::set(self, key, value) | ||
| } | ||
|
|
||
| fn entries(&self) -> Vec<ConfigEntry> { | ||
| vec![ | ||
| ConfigEntry { | ||
| key: "foo_to_bar".to_owned(), | ||
| value: Some(format!("{}", self.foo_to_bar)), | ||
| description: "foo to bar", | ||
| }, | ||
| ConfigEntry { | ||
| key: "baz_count".to_owned(), | ||
| value: Some(format!("{}", self.baz_count)), | ||
| description: "baz count", | ||
| }, | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| impl ConfigField for MyConfig { | ||
| fn visit<V: Visit>(&self, v: &mut V, _key: &str, _description: &'static str) { | ||
| let key = "foo_to_bar"; | ||
| let desc = "foo to bar"; | ||
| self.foo_to_bar.visit(v, key, desc); | ||
|
|
||
| let key = "baz_count"; | ||
| let desc = "baz count"; | ||
| self.baz_count.visit(v, key, desc); | ||
| } | ||
|
|
||
| fn set(&mut self, key: &str, value: &str) -> Result<(), DataFusionError> { | ||
| let (key, rem) = key.split_once('.').unwrap_or((key, "")); | ||
| match key { | ||
| "foo_to_bar" => self.foo_to_bar.set(rem, value.as_ref()), | ||
| "baz_count" => self.baz_count.set(rem, value.as_ref()), | ||
|
|
||
| _ => config_err!("Config value \"{}\" not found on MyConfig", key), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out specifying submodules in patch is not supported, so this generates a warning