Apache DataFusion SQL Query Engine
chore: Update `SessionStateBuilder::with_default_features` does not replace existing features (#14935)
irenjjb377725c6087
diff --git a/datafusion/core/src/execution/session_state.rs b/datafusion/core/src/execution/session_state.rsindex 1635731fdd..2ec1ba07b9 100644--- a/datafusion/core/src/execution/session_state.rs+++ b/datafusion/core/src/execution/session_state.rs@@ -1104,16 +1104,44 @@ impl SessionStateBuilder {}}- /// Create default builder with defaults for table_factories, file formats, expr_planners and builtin+ /// Adds defaults for table_factories, file formats, expr_planners and builtin/// scalar, aggregate and windows functions.- pub fn with_default_features(self) -> Self {- self.with_table_factories(SessionStateDefaults::default_table_factories())- .with_file_formats(SessionStateDefaults::default_file_formats())- .with_expr_planners(SessionStateDefaults::default_expr_planners())- .with_scalar_functions(SessionStateDefaults::default_scalar_functions())- .with_aggregate_functions(SessionStateDefaults::default_aggregate_functions())- .with_window_functions(SessionStateDefaults::default_window_functions())- .with_table_function_list(SessionStateDefaults::default_table_functions())+ ///+ /// Note overwrites any previously registered items with the same name.+ pub fn with_default_features(mut self) -> Self {+ self.table_factories+ .get_or_insert_with(HashMap::new)+ .extend(SessionStateDefaults::default_table_factories());++ self.file_formats+ .get_or_insert_with(Vec::new)+ .extend(SessionStateDefaults::default_file_formats());++ self.expr_planners+ .get_or_insert_with(Vec::new)+ .extend(SessionStateDefaults::default_expr_planners());++ self.scalar_functions+ .get_or_insert_with(Vec::new)+ .extend(SessionStateDefaults::default_scalar_functions());++ self.aggregate_functions+ .get_or_insert_with(Vec::new)+ .extend(SessionStateDefaults::default_aggregate_functions());++ self.window_functions+ .get_or_insert_with(Vec::new)+ .extend(SessionStateDefaults::default_window_functions());++ self.table_functions+ .get_or_insert_with(HashMap::new)+ .extend(+ SessionStateDefaults::default_table_functions()+ .into_iter()+ .map(|f| (f.name().to_string(), f)),+ );++ self}/// Set the session id.@@ -2166,4 +2194,19 @@ mod tests {assert!(table_factories.contains_key("employee"));Ok(())}++ #[test]+ fn test_with_default_features_not_override() -> Result<()> {+ use crate::test_util::TestTableFactory;++ // Test whether the table_factory has been overridden.+ let table_factory = Arc::new(TestTableFactory {});+ let session_state = SessionStateBuilder::new()+ .with_table_factory("test".to_string(), table_factory)+ .with_default_features()+ .build();+ assert!(session_state.table_factories().get("test").is_some());++ Ok(())+ }}