datafusion_python/
config.rs

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
18use pyo3::prelude::*;
19use pyo3::types::*;
20
21use datafusion::config::ConfigOptions;
22
23use crate::errors::PyDataFusionResult;
24use crate::utils::py_obj_to_scalar_value;
25
26#[pyclass(name = "Config", module = "datafusion", subclass)]
27#[derive(Clone)]
28pub(crate) struct PyConfig {
29    config: ConfigOptions,
30}
31
32#[pymethods]
33impl PyConfig {
34    #[new]
35    fn py_new() -> Self {
36        Self {
37            config: ConfigOptions::new(),
38        }
39    }
40
41    /// Get configurations from environment variables
42    #[staticmethod]
43    pub fn from_env() -> PyDataFusionResult<Self> {
44        Ok(Self {
45            config: ConfigOptions::from_env()?,
46        })
47    }
48
49    /// Get a configuration option
50    pub fn get<'py>(&mut self, key: &str, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
51        let options = self.config.to_owned();
52        for entry in options.entries() {
53            if entry.key == key {
54                return Ok(entry.value.into_pyobject(py)?);
55            }
56        }
57        Ok(None::<String>.into_pyobject(py)?)
58    }
59
60    /// Set a configuration option
61    pub fn set(&mut self, key: &str, value: PyObject, py: Python) -> PyDataFusionResult<()> {
62        let scalar_value = py_obj_to_scalar_value(py, value)?;
63        self.config.set(key, scalar_value.to_string().as_str())?;
64        Ok(())
65    }
66
67    /// Get all configuration options
68    pub fn get_all(&mut self, py: Python) -> PyResult<PyObject> {
69        let dict = PyDict::new(py);
70        let options = self.config.to_owned();
71        for entry in options.entries() {
72            dict.set_item(entry.key, entry.value.clone().into_pyobject(py)?)?;
73        }
74        Ok(dict.into())
75    }
76
77    fn __repr__(&mut self, py: Python) -> PyResult<String> {
78        let dict = self.get_all(py);
79        match dict {
80            Ok(result) => Ok(format!("Config({result})")),
81            Err(err) => Ok(format!("Error: {:?}", err.to_string())),
82        }
83    }
84}