Skip to content

Apache DataFusion SQL Query Engine

Add from_string_hash_map() method for SessionConfig (#6111)

yahoNanJing03713d60d5ef
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();