@@ -1104,16 +1104,44 @@ impl SessionStateBuilder {
11041104 }
11051105 }
11061106
1107- /// Create default builder with defaults for table_factories, file formats, expr_planners and builtin
1107+ /// Adds defaults for table_factories, file formats, expr_planners and builtin
11081108 /// scalar, aggregate and windows functions.
1109- pub fn with_default_features ( self ) -> Self {
1110- self . with_table_factories ( SessionStateDefaults :: default_table_factories ( ) )
1111- . with_file_formats ( SessionStateDefaults :: default_file_formats ( ) )
1112- . with_expr_planners ( SessionStateDefaults :: default_expr_planners ( ) )
1113- . with_scalar_functions ( SessionStateDefaults :: default_scalar_functions ( ) )
1114- . with_aggregate_functions ( SessionStateDefaults :: default_aggregate_functions ( ) )
1115- . with_window_functions ( SessionStateDefaults :: default_window_functions ( ) )
1116- . with_table_function_list ( SessionStateDefaults :: default_table_functions ( ) )
1109+ ///
1110+ /// Note overwrites any previously registered items with the same name.
1111+ pub fn with_default_features ( mut self ) -> Self {
1112+ self . table_factories
1113+ . get_or_insert_with ( HashMap :: new)
1114+ . extend ( SessionStateDefaults :: default_table_factories ( ) ) ;
1115+
1116+ self . file_formats
1117+ . get_or_insert_with ( Vec :: new)
1118+ . extend ( SessionStateDefaults :: default_file_formats ( ) ) ;
1119+
1120+ self . expr_planners
1121+ . get_or_insert_with ( Vec :: new)
1122+ . extend ( SessionStateDefaults :: default_expr_planners ( ) ) ;
1123+
1124+ self . scalar_functions
1125+ . get_or_insert_with ( Vec :: new)
1126+ . extend ( SessionStateDefaults :: default_scalar_functions ( ) ) ;
1127+
1128+ self . aggregate_functions
1129+ . get_or_insert_with ( Vec :: new)
1130+ . extend ( SessionStateDefaults :: default_aggregate_functions ( ) ) ;
1131+
1132+ self . window_functions
1133+ . get_or_insert_with ( Vec :: new)
1134+ . extend ( SessionStateDefaults :: default_window_functions ( ) ) ;
1135+
1136+ self . table_functions
1137+ . get_or_insert_with ( HashMap :: new)
1138+ . extend (
1139+ SessionStateDefaults :: default_table_functions ( )
1140+ . into_iter ( )
1141+ . map ( |f| ( f. name ( ) . to_string ( ) , f) ) ,
1142+ ) ;
1143+
1144+ self
11171145 }
11181146
11191147 /// Set the session id.
@@ -2166,4 +2194,19 @@ mod tests {
21662194 assert ! ( table_factories. contains_key( "employee" ) ) ;
21672195 Ok ( ( ) )
21682196 }
2197+
2198+ #[ test]
2199+ fn test_with_default_features_not_override ( ) -> Result < ( ) > {
2200+ use crate :: test_util:: TestTableFactory ;
2201+
2202+ // Test whether the table_factory has been overridden.
2203+ let table_factory = Arc :: new ( TestTableFactory { } ) ;
2204+ let session_state = SessionStateBuilder :: new ( )
2205+ . with_table_factory ( "test" . to_string ( ) , table_factory)
2206+ . with_default_features ( )
2207+ . build ( ) ;
2208+ assert ! ( session_state. table_factories( ) . get( "test" ) . is_some( ) ) ;
2209+
2210+ Ok ( ( ) )
2211+ }
21692212}
0 commit comments