-
Notifications
You must be signed in to change notification settings - Fork 118
[Config] - Support for key-value pair configuration settings #52
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 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 Config | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def config(): | ||
return Config() | ||
|
||
|
||
def test_get_then_set(config): | ||
config_key = "datafusion.optimizer.filter_null_join_keys" | ||
|
||
assert config.get(config_key).as_py() is False | ||
|
||
config.set(config_key, True) | ||
assert config.get(config_key).as_py() is True | ||
|
||
|
||
def test_get_all(config): | ||
config.get_all() | ||
|
||
|
||
def test_get_invalid_config(config): | ||
assert config.get("not.valid.key") is None |
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,82 @@ | ||
// 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 pyo3::prelude::*; | ||
use pyo3::types::*; | ||
|
||
use datafusion::config::ConfigOptions; | ||
use datafusion_common::ScalarValue; | ||
|
||
#[pyclass(name = "Config", module = "datafusion", subclass)] | ||
#[derive(Clone)] | ||
pub(crate) struct PyConfig { | ||
config: ConfigOptions, | ||
} | ||
|
||
#[pymethods] | ||
impl PyConfig { | ||
#[new] | ||
fn py_new() -> Self { | ||
Self { | ||
config: ConfigOptions::new(), | ||
} | ||
} | ||
|
||
/// Get configurations from environment variables | ||
#[staticmethod] | ||
pub fn from_env() -> Self { | ||
Self { | ||
config: ConfigOptions::from_env(), | ||
} | ||
} | ||
|
||
/// Get a configuration option | ||
pub fn get(&mut self, key: &str, py: Python) -> PyResult<PyObject> { | ||
Ok(self.config.get(key).into_py(py)) | ||
} | ||
|
||
/// Set a configuration option | ||
pub fn set(&mut self, key: &str, value: PyObject, py: Python) { | ||
self.config.set(key, py_obj_to_scalar_value(py, value)) | ||
} | ||
|
||
/// Get all configuration options | ||
pub fn get_all(&mut self, py: Python) -> PyResult<PyObject> { | ||
let dict = PyDict::new(py); | ||
for (key, value) in self.config.options() { | ||
dict.set_item(key, value.clone().into_py(py))?; | ||
} | ||
Ok(dict.into()) | ||
} | ||
} | ||
|
||
/// Convert a python object to a ScalarValue | ||
fn py_obj_to_scalar_value(py: Python, obj: PyObject) -> ScalarValue { | ||
if let Ok(value) = obj.extract::<bool>(py) { | ||
ScalarValue::Boolean(Some(value)) | ||
} else if let Ok(value) = obj.extract::<i64>(py) { | ||
ScalarValue::Int64(Some(value)) | ||
} else if let Ok(value) = obj.extract::<u64>(py) { | ||
ScalarValue::UInt64(Some(value)) | ||
} else if let Ok(value) = obj.extract::<f64>(py) { | ||
ScalarValue::Float64(Some(value)) | ||
} else if let Ok(value) = obj.extract::<String>(py) { | ||
ScalarValue::Utf8(Some(value)) | ||
} else { | ||
panic!("Unsupported value type") | ||
} | ||
} |
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
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.
I think we should use UInt64 here. We don't have any Int64 configs.