Skip to content

Apache DataFusion SQL Query Engine

Config support type conversion (#3522)

comphead1e16e43761f9
diff --git a/datafusion/core/src/config.rs b/datafusion/core/src/config.rs
index 00ccdd523c..2a2139fb25 100644
--- a/datafusion/core/src/config.rs
+++ b/datafusion/core/src/config.rs
@@ -62,6 +62,22 @@ pub struct ConfigDefinition {
default_value: ScalarValue,
}
+macro_rules! get_conf_value {
+ ($SELF: expr, $TPE: ident, $KEY: expr, $TPE_NAME: expr) => {
+ match $SELF.get($KEY) {
+ Some(ScalarValue::$TPE(v)) => v,
+ Some(v) => {
+ warn!(
+ "Config type mismatch for {}. Expected: {}, got: {:?}",
+ $KEY, $TPE_NAME, &v
+ );
+ None
+ }
+ None => None,
+ }
+ };
+}
+
impl ConfigDefinition {
/// Create a configuration option definition
pub fn new(
@@ -288,33 +304,29 @@ impl ConfigOptions {
}
/// get a boolean configuration option
- pub fn get_bool(&self, key: &str) -> bool {
- match self.get(key) {
- Some(ScalarValue::Boolean(Some(b))) => b,
- _ => false,
- }
+ pub fn get_bool(&self, key: &str) -> Option<bool> {
+ get_conf_value!(self, Boolean, key, "bool")
}
/// get a u64 configuration option
- pub fn get_u64(&self, key: &str) -> u64 {
- match self.get(key) {
- Some(ScalarValue::UInt64(Some(n))) => n,
- _ => 0,
- }
+ pub fn get_u64(&self, key: &str) -> Option<u64> {
+ get_conf_value!(self, UInt64, key, "u64")
}
/// get a string configuration option
- pub fn get_string(&self, key: &str) -> String {
- match self.get(key) {
- Some(ScalarValue::Utf8(Some(s))) => s,
- _ => "".into(),
- }
+ pub fn get_string(&self, key: &str) -> Option<String> {
+ get_conf_value!(self, Utf8, key, "string")
}
/// Access the underlying hashmap
pub fn options(&self) -> &HashMap<String, ScalarValue> {
&self.options
}
+
+ /// Tests if the key exists in the configuration
+ pub fn exists(&self, key: &str) -> bool {
+ self.options().contains_key(key)
+ }
}
#[cfg(test)]
@@ -339,16 +351,26 @@ mod test {
fn get_then_set() {
let mut config = ConfigOptions::new();
let config_key = "datafusion.optimizer.filter_null_join_keys";
- assert!(!config.get_bool(config_key));
+ assert!(!config.get_bool(config_key).unwrap_or_default());
config.set_bool(config_key, true);
- assert!(config.get_bool(config_key));
+ assert!(config.get_bool(config_key).unwrap_or_default());
}
#[test]
fn get_invalid_config() {
let config = ConfigOptions::new();
let invalid_key = "not.valid";
- assert!(config.get(invalid_key).is_none());
- assert!(!config.get_bool(invalid_key));
+ assert!(!config.exists(invalid_key));
+ assert!(!config.get_bool(invalid_key).unwrap_or_default());
+ }
+
+ #[test]
+ fn get_config_in_invalid_format() {
+ let config = ConfigOptions::new();
+ let key = "datafusion.execution.batch_size";
+
+ assert!(config.exists(key));
+ assert_eq!(None, config.get_string(key));
+ assert!(!config.get_bool(key).unwrap_or_default());
}
}
diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rs
index ca4b175b1c..ec6374d7ae 100644
--- a/datafusion/core/src/execution/context.rs
+++ b/datafusion/core/src/execution/context.rs
@@ -1256,6 +1256,7 @@ impl SessionConfig {
self.config_options
.read()
.get_u64(OPT_BATCH_SIZE)
+ .unwrap_or_default()
.try_into()
.unwrap()
}
@@ -1448,6 +1449,7 @@ impl SessionState {
.config_options
.read()
.get_bool(OPT_FILTER_NULL_JOIN_KEYS)
+ .unwrap_or_default()
{
rules.push(Arc::new(FilterNullJoinKeys::default()));
}
@@ -1461,12 +1463,18 @@ impl SessionState {
Arc::new(AggregateStatistics::new()),
Arc::new(HashBuildProbeOrder::new()),
];
- if config.config_options.read().get_bool(OPT_COALESCE_BATCHES) {
+ if config
+ .config_options
+ .read()
+ .get_bool(OPT_COALESCE_BATCHES)
+ .unwrap_or_default()
+ {
physical_optimizers.push(Arc::new(CoalesceBatches::new(
config
.config_options
.read()
.get_u64(OPT_COALESCE_TARGET_BATCH_SIZE)
+ .unwrap_or_default()
.try_into()
.unwrap(),
)));
@@ -1571,7 +1579,8 @@ impl SessionState {
self.config
.config_options
.read()
- .get_bool(OPT_OPTIMIZER_SKIP_FAILED_RULES),
+ .get_bool(OPT_OPTIMIZER_SKIP_FAILED_RULES)
+ .unwrap_or_default(),
)
.with_query_execution_start_time(
self.execution_props.query_execution_start_time,
diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rs
index 6dc1932a2b..48b78722f2 100644
--- a/datafusion/core/src/physical_plan/planner.rs
+++ b/datafusion/core/src/physical_plan/planner.rs
@@ -1590,6 +1590,7 @@ impl DefaultPhysicalPlanner {
.config_options
.read()
.get_bool(OPT_EXPLAIN_PHYSICAL_PLAN_ONLY)
+ .unwrap_or_default()
{
stringified_plans = e.stringified_plans.clone();
@@ -1601,6 +1602,7 @@ impl DefaultPhysicalPlanner {
.config_options
.read()
.get_bool(OPT_EXPLAIN_LOGICAL_PLAN_ONLY)
+ .unwrap_or_default()
{
let input = self
.create_initial_plan(e.plan.as_ref(), session_state)
diff --git a/datafusion/core/tests/config_from_env.rs b/datafusion/core/tests/config_from_env.rs
index 8dbbc7b0f4..20d153d8f8 100644
--- a/datafusion/core/tests/config_from_env.rs
+++ b/datafusion/core/tests/config_from_env.rs
@@ -25,7 +25,7 @@ fn get_config_bool_from_env() {
env::set_var(env_key, "true");
let config = ConfigOptions::from_env();
env::remove_var(env_key);
- assert!(config.get_bool(config_key));
+ assert!(config.get_bool(config_key).unwrap_or_default());
}
#[test]
@@ -35,7 +35,7 @@ fn get_config_int_from_env() {
env::set_var(env_key, "4096");
let config = ConfigOptions::from_env();
env::remove_var(env_key);
- assert_eq!(config.get_u64(config_key), 4096);
+ assert_eq!(config.get_u64(config_key).unwrap_or_default(), 4096);
}
#[test]
@@ -45,5 +45,5 @@ fn get_config_int_from_env_invalid() {
env::set_var(env_key, "abc");
let config = ConfigOptions::from_env();
env::remove_var(env_key);
- assert_eq!(config.get_u64(config_key), 4096); // set to its default value
+ assert_eq!(config.get_u64(config_key).unwrap_or_default(), 4096); // set to its default value
}