This is an automated email from the ASF dual-hosted git repository.
nju_yaho pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 03713d60d5 Add from_string_hash_map() method for SessionConfig (#6111)
03713d60d5 is described below
commit 03713d60d5ef01752b3114390005c5797a75021f
Author: yahoNanJing <[email protected]>
AuthorDate: Tue Apr 25 13:01:31 2023 +0800
Add from_string_hash_map() method for SessionConfig (#6111)
Co-authored-by: yangzhong <[email protected]>
---
datafusion/common/src/config.rs | 32 +++++++++++++++++++++++++++++++-
datafusion/execution/src/config.rs | 5 +++++
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/datafusion/common/src/config.rs b/datafusion/common/src/config.rs
index b808f340d7..35afabf307 100644
--- a/datafusion/common/src/config.rs
+++ b/datafusion/common/src/config.rs
@@ -19,7 +19,7 @@
use crate::{DataFusionError, Result};
use std::any::Any;
-use std::collections::BTreeMap;
+use std::collections::{BTreeMap, HashMap};
use std::fmt::Display;
/// A macro that wraps a configuration struct and automatically derives
@@ -480,6 +480,36 @@ impl ConfigOptions {
Ok(ret)
}
+ /// Create new ConfigOptions struct, taking values from a string hash map.
+ ///
+ /// Only the built-in configurations will be extracted from the hash map
+ /// and other key value pairs will be ignored.
+ pub fn from_string_hash_map(settings: HashMap<String, String>) ->
Result<Self> {
+ struct Visitor(Vec<String>);
+
+ impl Visit for Visitor {
+ fn some<V: Display>(&mut self, key: &str, _: V, _: &'static str) {
+ self.0.push(key.to_string())
+ }
+
+ fn none(&mut self, key: &str, _: &'static str) {
+ self.0.push(key.to_string())
+ }
+ }
+
+ let mut keys = Visitor(vec![]);
+ let mut ret = Self::default();
+ ret.visit(&mut keys, "datafusion", "");
+
+ for key in keys.0 {
+ if let Some(var) = settings.get(&key) {
+ ret.set(&key, var)?;
+ }
+ }
+
+ Ok(ret)
+ }
+
/// Returns the [`ConfigEntry`] stored within this [`ConfigOptions`]
pub fn entries(&self) -> Vec<ConfigEntry> {
struct Visitor(Vec<ConfigEntry>);
diff --git a/datafusion/execution/src/config.rs
b/datafusion/execution/src/config.rs
index 81b7e8f0a6..730a767826 100644
--- a/datafusion/execution/src/config.rs
+++ b/datafusion/execution/src/config.rs
@@ -57,6 +57,11 @@ impl SessionConfig {
Ok(ConfigOptions::from_env()?.into())
}
+ /// Create new ConfigOptions struct, taking values from a string hash map.
+ pub fn from_string_hash_map(settings: HashMap<String, String>) ->
Result<Self> {
+ Ok(ConfigOptions::from_string_hash_map(settings)?.into())
+ }
+
/// Set a configuration option
pub fn set(mut self, key: &str, value: ScalarValue) -> Self {
self.options.set(key, &value.to_string()).unwrap();