@@ -359,6 +359,49 @@ impl DFSchema {
359359 self . field_qualifiers . extend ( qualifiers) ;
360360 }
361361
362+ /// Merges `schemas` in order, with the same result as starting from an empty schema and
363+ /// calling [`Self::merge`] with each one.
364+ ///
365+ /// [`Self::merge`] rebuilds its duplicate lookup sets and copies every field it already has
366+ /// on each call, so folding many schemas costs time quadratic in the total field count. This
367+ /// keeps the lookup sets across schemas and builds the result once.
368+ pub fn merge_all < ' a > ( schemas : impl IntoIterator < Item = & ' a DFSchema > ) -> DFSchema {
369+ let mut fields: Vec < FieldRef > = vec ! [ ] ;
370+ let mut qualifiers: Vec < Option < TableReference > > = vec ! [ ] ;
371+ let mut metadata = HashMap :: new ( ) ;
372+ let mut seen_fields: HashSet < ( Option < & ' a TableReference > , & ' a FieldRef ) > =
373+ HashSet :: new ( ) ;
374+ let mut seen_names: HashSet < & ' a str > = HashSet :: new ( ) ;
375+ for schema in schemas {
376+ if schema. inner . fields . is_empty ( ) {
377+ continue ;
378+ }
379+ // Like `merge`, fields of one schema are only checked against earlier schemas.
380+ let mut added = vec ! [ ] ;
381+ for ( qualifier, field) in schema. iter ( ) {
382+ let duplicated_field = match qualifier {
383+ Some ( q) => seen_fields. contains ( & ( Some ( q) , field) ) ,
384+ None => seen_names. contains ( field. name ( ) . as_str ( ) ) ,
385+ } ;
386+ if !duplicated_field {
387+ fields. push ( Arc :: clone ( field) ) ;
388+ qualifiers. push ( qualifier. cloned ( ) ) ;
389+ added. push ( ( qualifier, field) ) ;
390+ }
391+ }
392+ for ( qualifier, field) in added {
393+ seen_fields. insert ( ( qualifier, field) ) ;
394+ seen_names. insert ( field. name ( ) . as_str ( ) ) ;
395+ }
396+ metadata. extend ( schema. inner . metadata . clone ( ) ) ;
397+ }
398+ DFSchema {
399+ inner : Arc :: new ( Schema :: new_with_metadata ( fields, metadata) ) ,
400+ field_qualifiers : qualifiers,
401+ functional_dependencies : FunctionalDependencies :: empty ( ) ,
402+ }
403+ }
404+
362405 /// Get a list of fields for this schema
363406 pub fn fields ( & self ) -> & Fields {
364407 & self . inner . fields
@@ -1382,6 +1425,56 @@ mod tests {
13821425
13831426 use super :: * ;
13841427
1428+ #[ test]
1429+ fn merge_all_matches_repeated_merge ( ) -> Result < ( ) > {
1430+ let field = |name : & str , data_type : DataType | Field :: new ( name, data_type, true ) ;
1431+ let qualified = |q : & str , fields : Vec < Field > | {
1432+ DFSchema :: try_from_qualified_schema ( q, & Schema :: new ( fields) )
1433+ } ;
1434+ let with_metadata = |schema : DFSchema , key : & str | {
1435+ let metadata = HashMap :: from ( [ ( key. to_string ( ) , "v" . to_string ( ) ) ] ) ;
1436+ let inner = schema. inner . as_ref ( ) . clone ( ) . with_metadata ( metadata) ;
1437+ DFSchema :: from_field_specific_qualified_schema (
1438+ schema. field_qualifiers . clone ( ) ,
1439+ & Arc :: new ( inner) ,
1440+ )
1441+ } ;
1442+ let schemas = vec ! [
1443+ qualified(
1444+ "t1" ,
1445+ vec![ field( "a" , DataType :: Int32 ) , field( "b" , DataType :: Utf8 ) ] ,
1446+ ) ?,
1447+ // Same qualified column, and the same name with another type.
1448+ qualified(
1449+ "t1" ,
1450+ vec![ field( "a" , DataType :: Int32 ) , field( "b" , DataType :: Int64 ) ] ,
1451+ ) ?,
1452+ with_metadata(
1453+ qualified(
1454+ "t2" ,
1455+ vec![ field( "a" , DataType :: Int32 ) , field( "d" , DataType :: Int32 ) ] ,
1456+ ) ?,
1457+ "k2" ,
1458+ ) ?,
1459+ // Unqualified columns are duplicates when only the name matches.
1460+ DFSchema :: try_from( Schema :: new( vec![
1461+ field( "a" , DataType :: Int32 ) ,
1462+ field( "c" , DataType :: Int32 ) ,
1463+ ] ) ) ?,
1464+ DFSchema :: empty( ) ,
1465+ DFSchema :: try_from( Schema :: new( vec![ field( "c" , DataType :: Int32 ) ] ) ) ?,
1466+ ] ;
1467+
1468+ let mut expected = DFSchema :: empty ( ) ;
1469+ for schema in & schemas {
1470+ expected. merge ( schema) ;
1471+ }
1472+ let merged = DFSchema :: merge_all ( & schemas) ;
1473+ assert_eq ! ( merged. inner, expected. inner) ;
1474+ assert_eq ! ( merged. field_qualifiers, expected. field_qualifiers) ;
1475+ Ok ( ( ) )
1476+ }
1477+
13851478 /// `qualified_name` doesn't use `TableReference::Display` for performance
13861479 /// reasons, but check that the output is consistent.
13871480 #[ test]
0 commit comments