@@ -255,14 +255,54 @@ fn try_merge_collector_schemas(
255255 schema1 : & CollectorSchema ,
256256 schema2 : & CollectorSchema ,
257257) -> Result < CollectorSchema > {
258- let fields = try_merge_fields_schemas ( & schema1. fields , & schema2. fields ) ?;
258+ // Union all fields from both schemas
259+ let mut field_map: HashMap < FieldName , EnrichedValueType > = HashMap :: new ( ) ;
260+
261+ // Add fields from schema1
262+ for field in & schema1. fields {
263+ field_map. insert ( field. name . clone ( ) , field. value_type . clone ( ) ) ;
264+ }
265+
266+ // Merge fields from schema2
267+ for field in & schema2. fields {
268+ if let Some ( existing_type) = field_map. get ( & field. name ) {
269+ // Try to merge types if they differ
270+ let merged_type = try_make_common_value_type ( existing_type, & field. value_type ) ?;
271+ field_map. insert ( field. name . clone ( ) , merged_type) ;
272+ } else {
273+ field_map. insert ( field. name . clone ( ) , field. value_type . clone ( ) ) ;
274+ }
275+ }
276+
277+ // Sort fields by name for consistent ordering
278+ let mut fields: Vec < FieldSchema > = field_map
279+ . into_iter ( )
280+ . map ( |( name, value_type) | FieldSchema {
281+ name,
282+ value_type,
283+ description : None ,
284+ } )
285+ . collect ( ) ;
286+ fields. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
287+
288+ // Handle auto_uuid_field_idx
289+ let auto_uuid_field_idx = match ( schema1. auto_uuid_field_idx , schema2. auto_uuid_field_idx ) {
290+ ( Some ( idx1) , Some ( idx2) ) => {
291+ let name1 = & schema1. fields [ idx1] . name ;
292+ let name2 = & schema2. fields [ idx2] . name ;
293+ if name1 == name2 {
294+ // Find the new index of the auto_uuid field
295+ fields. iter ( ) . position ( |f| & f. name == name1)
296+ } else {
297+ None // Different auto_uuid fields, disable
298+ }
299+ }
300+ _ => None , // If either doesn't have it, or both don't, disable
301+ } ;
302+
259303 Ok ( CollectorSchema {
260304 fields,
261- auto_uuid_field_idx : if schema1. auto_uuid_field_idx == schema2. auto_uuid_field_idx {
262- schema1. auto_uuid_field_idx
263- } else {
264- None
265- } ,
305+ auto_uuid_field_idx,
266306 } )
267307}
268308
@@ -803,16 +843,29 @@ impl AnalyzerContext {
803843 let ( struct_mapping, fields_schema) = analyze_struct_mapping ( & op. input , op_scope) ?;
804844 let has_auto_uuid_field = op. auto_uuid_field . is_some ( ) ;
805845 let fingerprinter = Fingerprinter :: default ( ) . with ( & fields_schema) ?;
846+ let input_field_names = fields_schema. iter ( ) . map ( |f| f. name . clone ( ) ) . collect ( ) ;
847+ let collector_ref = add_collector (
848+ & op. scope_name ,
849+ op. collector_name . clone ( ) ,
850+ CollectorSchema :: from_fields ( fields_schema, op. auto_uuid_field . clone ( ) ) ,
851+ op_scope,
852+ ) ?;
853+ // Get the merged collector schema after adding
854+ let collector_schema: Arc < CollectorSchema > = {
855+ let scope = find_scope ( & op. scope_name , op_scope) ?. 1 ;
856+ let states = scope. states . lock ( ) . unwrap ( ) ;
857+ let collector = states. collectors
858+ . get ( & op. collector_name )
859+ . unwrap ( ) ;
860+ collector. schema . clone ( )
861+ } ;
806862 let collect_op = AnalyzedReactiveOp :: Collect ( AnalyzedCollectOp {
807863 name : reactive_op. name . clone ( ) ,
808864 has_auto_uuid_field,
809865 input : struct_mapping,
810- collector_ref : add_collector (
811- & op. scope_name ,
812- op. collector_name . clone ( ) ,
813- CollectorSchema :: from_fields ( fields_schema, op. auto_uuid_field . clone ( ) ) ,
814- op_scope,
815- ) ?,
866+ input_field_names,
867+ collector_schema,
868+ collector_ref,
816869 fingerprinter,
817870 } ) ;
818871 async move { Ok ( collect_op) } . boxed ( )
0 commit comments