oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//! # Stream Fusion Optimizer
//!
//! Automatically detects and fuses consecutive stream operations into single passes,
//! eliminating intermediate allocations, reducing function call overhead, and improving
//! cache locality for significant performance gains.
//!
//! ## Features
//!
//! - **Automatic Fusion**: Detects fusable operation sequences
//! - **Multiple Fusion Rules**: Map-Map, Filter-Filter, Map-Filter combinations
//! - **Cost-Based Optimization**: Only fuses when beneficial
//! - **Performance Metrics**: Tracks fusion benefits and overhead reduction
//! - **Safe Transformations**: Validates fusion correctness
//! - **Configurable**: Enable/disable specific fusion types
//!
//! ## Fusion Rules
//!
//! 1. **Map Fusion**: `map(f) → map(g)` becomes `map(g ∘ f)`
//! 2. **Filter Fusion**: `filter(p) → filter(q)` becomes `filter(p && q)`
//! 3. **Map-Filter Fusion**: `map(f) → filter(p)` becomes `filter_map(|x| p(f(x)))`
//! 4. **Filter-Map Reordering**: Sometimes safe to reorder for better fusion
//!
//! ## Example
//!
//! ```ignore
//! use oxirs_stream::stream_fusion::{FusionOptimizer, FusionConfig};
//! use oxirs_stream::stream_fusion::Operation;
//!
//! # async fn example() -> anyhow::Result<()> {
//! let config = FusionConfig {
//!     enable_fusion: true,
//!     max_fusion_depth: 5,
//!     enable_map_fusion: true,
//!     enable_filter_fusion: true,
//!     enable_cross_fusion: true,
//!     ..Default::default()
//! };
//!
//! let mut optimizer = FusionOptimizer::new(config);
//!
//! // Define a pipeline with multiple operations
//! let pipeline = vec![
//!     Operation::Map { name: "normalize".to_string() },
//!     Operation::Map { name: "transform".to_string() },
//!     Operation::Filter { name: "validate".to_string() },
//!     Operation::Filter { name: "check_bounds".to_string() },
//! ];
//!
//! // Optimize the pipeline
//! let optimized = optimizer.optimize_pipeline(&pipeline)?;
//!
//! // Get fusion statistics
//! let stats = optimizer.get_stats();
//! println!("Fused {} operations, saved {}% overhead",
//!          stats.operations_fused, stats.overhead_reduction_percent);
//! # Ok(())
//! # }
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, info};

/// Configuration for stream fusion optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FusionConfig {
    /// Enable stream fusion optimization
    pub enable_fusion: bool,

    /// Maximum number of operations to fuse in a single chain
    pub max_fusion_depth: usize,

    /// Enable map-map fusion
    pub enable_map_fusion: bool,

    /// Enable filter-filter fusion
    pub enable_filter_fusion: bool,

    /// Enable map-filter cross fusion
    pub enable_cross_fusion: bool,

    /// Enable filter-map reordering (requires analysis)
    pub enable_reordering: bool,

    /// Minimum operations required to consider fusion (avoid overhead for small chains)
    pub min_fusion_size: usize,

    /// Cost threshold for fusion (only fuse if benefit > cost)
    pub cost_threshold: f32,

    /// Enable aggressive fusion (may increase compilation time)
    pub aggressive_mode: bool,

    /// Enable fusion metrics collection
    pub collect_metrics: bool,
}

impl Default for FusionConfig {
    fn default() -> Self {
        Self {
            enable_fusion: true,
            max_fusion_depth: 10,
            enable_map_fusion: true,
            enable_filter_fusion: true,
            enable_cross_fusion: true,
            enable_reordering: false, // Conservative default
            min_fusion_size: 2,
            cost_threshold: 0.1,
            aggressive_mode: false,
            collect_metrics: true,
        }
    }
}

/// Stream operation types that can be fused
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Operation {
    /// Map operation: transforms each element
    Map { name: String },

    /// Filter operation: selects elements based on predicate
    Filter { name: String },

    /// FlatMap operation: maps and flattens
    FlatMap { name: String },

    /// Distinct operation: removes duplicates
    Distinct { name: String },

    /// Take operation: limits number of elements
    Take { count: usize },

    /// Skip operation: skips first n elements
    Skip { count: usize },

    /// Custom operation: user-defined
    Custom { name: String, fusable: bool },
}

impl Operation {
    /// Check if this operation can be fused with another
    pub fn can_fuse_with(&self, other: &Operation) -> bool {
        match (self, other) {
            // Map can fuse with map
            (Operation::Map { .. }, Operation::Map { .. }) => true,
            // Filter can fuse with filter
            (Operation::Filter { .. }, Operation::Filter { .. }) => true,
            // Map can fuse with filter
            (Operation::Map { .. }, Operation::Filter { .. }) => true,
            // Custom operations check fusable flag
            (Operation::Custom { fusable: true, .. }, Operation::Custom { fusable: true, .. }) => {
                true
            }
            _ => false,
        }
    }

    /// Get operation name for debugging
    pub fn name(&self) -> String {
        match self {
            Operation::Map { name } => format!("map({})", name),
            Operation::Filter { name } => format!("filter({})", name),
            Operation::FlatMap { name } => format!("flat_map({})", name),
            Operation::Distinct { name } => format!("distinct({})", name),
            Operation::Take { count } => format!("take({})", count),
            Operation::Skip { count } => format!("skip({})", count),
            Operation::Custom { name, .. } => format!("custom({})", name),
        }
    }
}

/// Fused operation combining multiple operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FusedOperation {
    /// Original operations that were fused
    pub original_ops: Vec<Operation>,

    /// Fused operation type
    pub fused_type: FusedType,

    /// Estimated cost savings (0.0-1.0)
    pub cost_savings: f32,

    /// Fusion timestamp
    pub fused_at: chrono::DateTime<chrono::Utc>,
}

/// Type of fused operation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FusedType {
    /// Multiple maps fused into one
    MapChain,

    /// Multiple filters fused into one
    FilterChain,

    /// Map and filter fused
    MapFilter,

    /// Complex fusion
    Complex,
}

/// Fusion statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FusionStats {
    /// Total pipelines optimized
    pub pipelines_optimized: u64,

    /// Total operations analyzed
    pub operations_analyzed: u64,

    /// Total operations fused
    pub operations_fused: u64,

    /// Number of fusion chains created
    pub fusion_chains_created: u64,

    /// Estimated overhead reduction (percentage)
    pub overhead_reduction_percent: f32,

    /// Average fusion chain length
    pub avg_fusion_chain_length: f32,

    /// Map fusions performed
    pub map_fusions: u64,

    /// Filter fusions performed
    pub filter_fusions: u64,

    /// Cross fusions performed (map+filter)
    pub cross_fusions: u64,

    /// Reorderings performed
    pub reorderings: u64,

    /// Total optimization time
    pub total_optimization_time: Duration,

    /// Last optimization timestamp
    pub last_optimization: Option<chrono::DateTime<chrono::Utc>>,
}

/// Stream fusion optimizer
pub struct FusionOptimizer {
    config: FusionConfig,
    stats: Arc<RwLock<FusionStats>>,
    fusion_cache: Arc<RwLock<HashMap<String, Vec<FusedOperation>>>>,
}

impl FusionOptimizer {
    /// Create a new fusion optimizer
    pub fn new(config: FusionConfig) -> Self {
        Self {
            config,
            stats: Arc::new(RwLock::new(FusionStats::default())),
            fusion_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Optimize a pipeline by fusing operations
    pub async fn optimize_pipeline(&mut self, pipeline: &[Operation]) -> Result<Vec<Operation>> {
        if !self.config.enable_fusion {
            return Ok(pipeline.to_vec());
        }

        if pipeline.len() < self.config.min_fusion_size {
            debug!(
                "Pipeline too small for fusion: {} operations",
                pipeline.len()
            );
            return Ok(pipeline.to_vec());
        }

        let start_time = Instant::now();

        // Update stats
        let mut stats = self.stats.write().await;
        stats.pipelines_optimized += 1;
        stats.operations_analyzed += pipeline.len() as u64;
        drop(stats);

        // Perform fusion optimization
        let optimized = self.fuse_operations(pipeline).await?;

        // Update optimization time
        let optimization_time = start_time.elapsed();
        let mut stats = self.stats.write().await;
        stats.total_optimization_time += optimization_time;
        stats.last_optimization = Some(chrono::Utc::now());

        info!(
            "Optimized pipeline: {} ops -> {} ops in {:?}",
            pipeline.len(),
            optimized.len(),
            optimization_time
        );

        Ok(optimized)
    }

    /// Fuse consecutive operations in the pipeline
    async fn fuse_operations(&self, operations: &[Operation]) -> Result<Vec<Operation>> {
        let mut result = Vec::new();
        let mut i = 0;

        while i < operations.len() {
            // Try to fuse starting from this position
            let fusion_chain = self.find_fusion_chain(operations, i).await?;

            if fusion_chain.len() > 1 {
                // Multiple operations can be fused
                let fused_op = self.create_fused_operation(&fusion_chain).await?;
                result.push(fused_op);

                // Update stats
                let mut stats = self.stats.write().await;
                stats.operations_fused += fusion_chain.len() as u64;
                stats.fusion_chains_created += 1;

                // Update fusion type counts
                let fusion_type = self.classify_fusion(&fusion_chain);
                match fusion_type {
                    FusedType::MapChain => stats.map_fusions += 1,
                    FusedType::FilterChain => stats.filter_fusions += 1,
                    FusedType::MapFilter => stats.cross_fusions += 1,
                    FusedType::Complex => {}
                }

                // Calculate overhead reduction
                let reduction = self.estimate_overhead_reduction(&fusion_chain);
                stats.overhead_reduction_percent =
                    (stats.overhead_reduction_percent + reduction) / 2.0;

                // Update average chain length
                let chain_len = fusion_chain.len() as f32;
                stats.avg_fusion_chain_length = (stats.avg_fusion_chain_length
                    * (stats.fusion_chains_created - 1) as f32
                    + chain_len)
                    / stats.fusion_chains_created as f32;

                i += fusion_chain.len();
            } else {
                // Cannot fuse, keep original operation
                result.push(operations[i].clone());
                i += 1;
            }
        }

        Ok(result)
    }

    /// Find the longest chain of fusable operations starting from position
    async fn find_fusion_chain(
        &self,
        operations: &[Operation],
        start: usize,
    ) -> Result<Vec<Operation>> {
        let mut chain = vec![operations[start].clone()];
        let mut current = start;

        while current + 1 < operations.len() && chain.len() < self.config.max_fusion_depth {
            let current_op = &operations[current];
            let next_op = &operations[current + 1];

            // Check if operations can be fused based on config
            let can_fuse = match (current_op, next_op) {
                (Operation::Map { .. }, Operation::Map { .. }) => self.config.enable_map_fusion,
                (Operation::Filter { .. }, Operation::Filter { .. }) => {
                    self.config.enable_filter_fusion
                }
                (Operation::Map { .. }, Operation::Filter { .. }) => {
                    self.config.enable_cross_fusion
                }
                (Operation::Filter { .. }, Operation::Map { .. }) => {
                    self.config.enable_cross_fusion && self.config.enable_reordering
                }
                _ => current_op.can_fuse_with(next_op),
            };

            if can_fuse {
                // Check cost-benefit
                let benefit = self.estimate_fusion_benefit(current_op, next_op);
                if benefit >= self.config.cost_threshold {
                    chain.push(next_op.clone());
                    current += 1;
                } else {
                    debug!("Fusion benefit too low: {}", benefit);
                    break;
                }
            } else {
                break;
            }
        }

        Ok(chain)
    }

    /// Create a fused operation from a chain
    async fn create_fused_operation(&self, chain: &[Operation]) -> Result<Operation> {
        if chain.is_empty() {
            return Err(anyhow!("Cannot create fused operation from empty chain"));
        }

        if chain.len() == 1 {
            return Ok(chain[0].clone());
        }

        // Classify the fusion type
        let fusion_type = self.classify_fusion(chain);

        // Create appropriate fused operation
        match fusion_type {
            FusedType::MapChain => {
                // Combine map operations
                let names: Vec<String> = chain
                    .iter()
                    .filter_map(|op| {
                        if let Operation::Map { name } = op {
                            Some(name.clone())
                        } else {
                            None
                        }
                    })
                    .collect();

                Ok(Operation::Map {
                    name: format!("fused[{}]", names.join(" → ")),
                })
            }
            FusedType::FilterChain => {
                // Combine filter operations
                let names: Vec<String> = chain
                    .iter()
                    .filter_map(|op| {
                        if let Operation::Filter { name } = op {
                            Some(name.clone())
                        } else {
                            None
                        }
                    })
                    .collect();

                Ok(Operation::Filter {
                    name: format!("fused[{} && ...]", names.join(" && ")),
                })
            }
            FusedType::MapFilter => {
                // Combine map and filter
                let op_names: Vec<String> = chain.iter().map(|op| op.name()).collect();

                Ok(Operation::Custom {
                    name: format!("fused_map_filter[{}]", op_names.join(" → ")),
                    fusable: true,
                })
            }
            FusedType::Complex => {
                // Complex fusion
                let op_names: Vec<String> = chain.iter().map(|op| op.name()).collect();

                Ok(Operation::Custom {
                    name: format!("fused_complex[{}]", op_names.join(" → ")),
                    fusable: true,
                })
            }
        }
    }

    /// Classify the type of fusion for a chain
    fn classify_fusion(&self, chain: &[Operation]) -> FusedType {
        let all_maps = chain.iter().all(|op| matches!(op, Operation::Map { .. }));
        let all_filters = chain
            .iter()
            .all(|op| matches!(op, Operation::Filter { .. }));

        if all_maps {
            FusedType::MapChain
        } else if all_filters {
            FusedType::FilterChain
        } else if chain.iter().any(|op| matches!(op, Operation::Map { .. }))
            && chain
                .iter()
                .any(|op| matches!(op, Operation::Filter { .. }))
        {
            FusedType::MapFilter
        } else {
            FusedType::Complex
        }
    }

    /// Estimate the benefit of fusing two operations
    fn estimate_fusion_benefit(&self, op1: &Operation, op2: &Operation) -> f32 {
        // Base benefit from eliminating intermediate overhead
        let base_benefit = 0.3;

        // Additional benefit based on operation types
        let type_benefit = match (op1, op2) {
            // Map-map fusion has high benefit (eliminates intermediate allocation)
            (Operation::Map { .. }, Operation::Map { .. }) => 0.4,
            // Filter-filter fusion has good benefit (combines predicates)
            (Operation::Filter { .. }, Operation::Filter { .. }) => 0.35,
            // Map-filter has moderate benefit
            (Operation::Map { .. }, Operation::Filter { .. }) => 0.25,
            // Other combinations
            _ => 0.2,
        };

        // Aggressive mode increases benefit estimates
        let aggressive_multiplier = if self.config.aggressive_mode {
            1.2
        } else {
            1.0
        };

        (base_benefit + type_benefit) * aggressive_multiplier
    }

    /// Estimate overhead reduction from fusing a chain
    fn estimate_overhead_reduction(&self, chain: &[Operation]) -> f32 {
        if chain.len() <= 1 {
            return 0.0;
        }

        // Each fused operation eliminates one intermediate step
        // Estimate 15-25% overhead reduction per eliminated step
        let eliminated_steps = (chain.len() - 1) as f32;
        let reduction_per_step = 0.20; // 20% average

        (eliminated_steps * reduction_per_step * 100.0).min(95.0)
    }

    /// Get fusion statistics
    pub async fn get_stats(&self) -> FusionStats {
        self.stats.read().await.clone()
    }

    /// Reset statistics
    pub async fn reset_stats(&self) {
        let mut stats = self.stats.write().await;
        *stats = FusionStats::default();
    }

    /// Analyze a pipeline without applying fusion (dry run)
    pub async fn analyze_pipeline(&self, pipeline: &[Operation]) -> Result<FusionAnalysis> {
        let mut fusable_chains = Vec::new();
        let mut i = 0;

        while i < pipeline.len() {
            let chain = self.find_fusion_chain(pipeline, i).await?;

            if chain.len() > 1 {
                let fusion_type = self.classify_fusion(&chain);
                let benefit = self.estimate_overhead_reduction(&chain);

                fusable_chains.push(FusableChain {
                    start_index: i,
                    operations: chain.clone(),
                    fusion_type,
                    estimated_benefit: benefit,
                });

                i += chain.len();
            } else {
                i += 1;
            }
        }

        let ops_saved: usize = fusable_chains.iter().map(|c| c.operations.len() - 1).sum();
        let estimated_final_count = pipeline.len() - ops_saved;

        Ok(FusionAnalysis {
            original_operation_count: pipeline.len(),
            fusable_chains,
            estimated_final_count,
        })
    }

    /// Clear the fusion cache
    pub async fn clear_cache(&self) {
        self.fusion_cache.write().await.clear();
    }
}

/// Result of pipeline analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FusionAnalysis {
    /// Original number of operations
    pub original_operation_count: usize,

    /// Chains that can be fused
    pub fusable_chains: Vec<FusableChain>,

    /// Estimated operation count after fusion
    pub estimated_final_count: usize,
}

/// A chain of operations that can be fused
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FusableChain {
    /// Starting index in the original pipeline
    pub start_index: usize,

    /// Operations in the chain
    pub operations: Vec<Operation>,

    /// Type of fusion
    pub fusion_type: FusedType,

    /// Estimated benefit (percentage)
    pub estimated_benefit: f32,
}

impl FusionAnalysis {
    /// Get a summary of the analysis
    pub fn summary(&self) -> String {
        format!(
            "Pipeline Analysis: {} ops -> {} ops ({} fusable chains, {:.1}% reduction)",
            self.original_operation_count,
            self.estimated_final_count,
            self.fusable_chains.len(),
            ((self.original_operation_count - self.estimated_final_count) as f32
                / self.original_operation_count as f32
                * 100.0)
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_fusion_optimizer_creation() {
        let config = FusionConfig::default();
        let optimizer = FusionOptimizer::new(config);
        assert!(optimizer.config.enable_fusion);
    }

    #[tokio::test]
    async fn test_map_fusion() {
        let config = FusionConfig {
            enable_map_fusion: true,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
            Operation::Map {
                name: "step3".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should fuse all three maps into one
        assert_eq!(optimized.len(), 1);
        assert!(matches!(optimized[0], Operation::Map { .. }));

        let stats = optimizer.get_stats().await;
        assert_eq!(stats.operations_fused, 3);
        assert_eq!(stats.map_fusions, 1);
    }

    #[tokio::test]
    async fn test_filter_fusion() {
        let config = FusionConfig {
            enable_filter_fusion: true,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Filter {
                name: "check1".to_string(),
            },
            Operation::Filter {
                name: "check2".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should fuse filters
        assert_eq!(optimized.len(), 1);
        assert!(matches!(optimized[0], Operation::Filter { .. }));

        let stats = optimizer.get_stats().await;
        assert_eq!(stats.filter_fusions, 1);
    }

    #[tokio::test]
    async fn test_mixed_fusion() {
        let config = FusionConfig {
            enable_cross_fusion: true,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "transform".to_string(),
            },
            Operation::Filter {
                name: "validate".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should fuse map and filter
        assert_eq!(optimized.len(), 1);

        let stats = optimizer.get_stats().await;
        assert_eq!(stats.cross_fusions, 1);
    }

    #[tokio::test]
    async fn test_no_fusion_when_disabled() {
        let config = FusionConfig {
            enable_fusion: false,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should not fuse
        assert_eq!(optimized.len(), 2);
    }

    #[tokio::test]
    async fn test_min_fusion_size() {
        let config = FusionConfig {
            min_fusion_size: 3,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should not fuse (too small)
        assert_eq!(optimized.len(), 2);
    }

    #[tokio::test]
    async fn test_max_fusion_depth() {
        let config = FusionConfig {
            max_fusion_depth: 2,
            ..Default::default()
        };

        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
            Operation::Map {
                name: "step3".to_string(),
            },
        ];

        let optimized = optimizer.optimize_pipeline(&pipeline).await.unwrap();

        // Should fuse only first 2, then the third separately (or as another fusion)
        assert!(optimized.len() <= 2);
    }

    #[tokio::test]
    async fn test_fusion_analysis() {
        let config = FusionConfig::default();
        let optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
            Operation::Filter {
                name: "check".to_string(),
            },
        ];

        let analysis = optimizer.analyze_pipeline(&pipeline).await.unwrap();

        assert_eq!(analysis.original_operation_count, 3);
        assert!(!analysis.fusable_chains.is_empty());
        assert!(analysis.estimated_final_count < analysis.original_operation_count);
    }

    #[tokio::test]
    async fn test_operation_can_fuse() {
        let map1 = Operation::Map {
            name: "map1".to_string(),
        };
        let map2 = Operation::Map {
            name: "map2".to_string(),
        };
        let filter1 = Operation::Filter {
            name: "filter1".to_string(),
        };

        assert!(map1.can_fuse_with(&map2));
        assert!(map1.can_fuse_with(&filter1));
    }

    #[tokio::test]
    async fn test_stats_tracking() {
        let config = FusionConfig::default();
        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
        ];

        optimizer.optimize_pipeline(&pipeline).await.unwrap();

        let stats = optimizer.get_stats().await;
        assert_eq!(stats.pipelines_optimized, 1);
        assert!(stats.operations_fused > 0);
        assert!(stats.fusion_chains_created > 0);
    }

    #[tokio::test]
    async fn test_reset_stats() {
        let config = FusionConfig::default();
        let mut optimizer = FusionOptimizer::new(config);

        let pipeline = vec![
            Operation::Map {
                name: "step1".to_string(),
            },
            Operation::Map {
                name: "step2".to_string(),
            },
        ];

        optimizer.optimize_pipeline(&pipeline).await.unwrap();
        optimizer.reset_stats().await;

        let stats = optimizer.get_stats().await;
        assert_eq!(stats.pipelines_optimized, 0);
        assert_eq!(stats.operations_fused, 0);
    }
}