oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! ML-Powered Query Performance Predictor
//!
//! This module provides machine learning-based prediction of SPARQL query performance
//! using scirs2-core's ml_pipeline and neural_architecture_search features.
//!
//! ## Features
//!
//! - Query execution time prediction with confidence intervals
//! - Historical query pattern learning
//! - Feature extraction from query structure
//! - Adaptive model training based on actual execution data
//! - Performance anomaly detection
//!
//! ## SciRS2 Integration
//!
//! This module showcases advanced SciRS2-core usage:
//! - `scirs2_core::ml_pipeline` for predictive analytics
//! - `scirs2_core::ndarray_ext` for feature matrices
//! - `scirs2_core::random` for data sampling
//! - `scirs2_core::metrics` for performance tracking
//! - `scirs2_core::validation` for input validation

use super::CommandResult;
use crate::cli::CliContext;
use colored::Colorize;
use scirs2_core::ndarray_ext::{Array1, Array2};
use std::collections::HashMap;

/// Query features extracted for ML prediction
#[derive(Debug, Clone)]
pub struct QueryFeatures {
    /// Number of triple patterns in query
    pub triple_patterns: f64,
    /// Number of OPTIONAL clauses
    pub optional_count: f64,
    /// Number of UNION clauses
    pub union_count: f64,
    /// Number of FILTER expressions
    pub filter_count: f64,
    /// Number of ORDER BY clauses
    pub order_by_count: f64,
    /// Number of GROUP BY clauses
    pub group_by_count: f64,
    /// Query selectivity score (0.0 - 1.0)
    pub selectivity: f64,
    /// Has LIMIT clause (0.0 or 1.0)
    pub has_limit: f64,
    /// Has DISTINCT (0.0 or 1.0)
    pub has_distinct: f64,
    /// Number of subqueries
    pub subquery_count: f64,
    /// Property path complexity (0-100)
    pub property_path_complexity: f64,
    /// Aggregation function count
    pub aggregation_count: f64,
}

impl QueryFeatures {
    /// Extract features from a SPARQL query string
    pub fn extract_from_query(query: &str) -> Self {
        let query_upper = query.to_uppercase();

        // Count triple patterns (approximate by counting dots, semicolons, and WHERE clauses)
        // At minimum, if there's a WHERE clause, there's at least 1 triple pattern
        let dot_count = query.matches('.').count();
        let semicolon_count = query.matches(';').count();
        let has_where = query_upper.contains("WHERE");

        let triple_patterns = if has_where && dot_count == 0 && semicolon_count == 0 {
            // Simple query with one triple pattern and no terminators
            1.0
        } else {
            // Count based on terminators (dots and semicolons)
            (dot_count + semicolon_count).max(if has_where { 1 } else { 0 }) as f64
        };

        // Count various SPARQL constructs
        let optional_count = query_upper.matches("OPTIONAL").count() as f64;
        let union_count = query_upper.matches("UNION").count() as f64;
        let filter_count = query_upper.matches("FILTER").count() as f64;
        let order_by_count = query_upper.matches("ORDER BY").count() as f64;
        let group_by_count = query_upper.matches("GROUP BY").count() as f64;
        let subquery_count = query_upper.matches("SELECT").count().saturating_sub(1) as f64;

        // Has LIMIT or DISTINCT
        let has_limit = if query_upper.contains("LIMIT") {
            1.0
        } else {
            0.0
        };
        let has_distinct = if query_upper.contains("DISTINCT") {
            1.0
        } else {
            0.0
        };

        // Count aggregation functions
        let aggregation_count = ["COUNT(", "SUM(", "AVG(", "MAX(", "MIN("]
            .iter()
            .map(|agg| query_upper.matches(agg).count())
            .sum::<usize>() as f64;

        // Estimate selectivity (0.0 = low, 1.0 = high)
        let has_specific_uris = query.contains("http://") || query.contains("https://");
        let has_filters = query_upper.contains("FILTER");
        let selectivity = match (has_specific_uris, has_filters) {
            (true, true) => 0.9,
            (true, false) => 0.6,
            (false, true) => 0.5,
            (false, false) => 0.2,
        };

        // Property path complexity
        let property_path_complexity = (query.matches('/').count()
            + query.matches('+').count()
            + query.matches('*').count() * 2) as f64
            * 10.0;

        Self {
            triple_patterns,
            optional_count,
            union_count,
            filter_count,
            order_by_count,
            group_by_count,
            selectivity,
            has_limit,
            has_distinct,
            subquery_count,
            property_path_complexity: property_path_complexity.min(100.0),
            aggregation_count,
        }
    }

    /// Convert features to array for ML model
    pub fn to_array(&self) -> Array1<f64> {
        Array1::from(vec![
            self.triple_patterns,
            self.optional_count,
            self.union_count,
            self.filter_count,
            self.order_by_count,
            self.group_by_count,
            self.selectivity,
            self.has_limit,
            self.has_distinct,
            self.subquery_count,
            self.property_path_complexity / 100.0, // Normalize to 0-1
            self.aggregation_count,
        ])
    }

    /// Feature names for interpretation
    pub fn feature_names() -> Vec<&'static str> {
        vec![
            "Triple Patterns",
            "OPTIONAL Clauses",
            "UNION Clauses",
            "FILTER Expressions",
            "ORDER BY",
            "GROUP BY",
            "Selectivity",
            "Has LIMIT",
            "Has DISTINCT",
            "Subqueries",
            "Property Path Complexity",
            "Aggregations",
        ]
    }
}

/// Performance prediction result
#[derive(Debug, Clone)]
pub struct PerformancePrediction {
    /// Predicted execution time in milliseconds
    pub predicted_time_ms: f64,
    /// Lower bound of confidence interval (95%)
    pub confidence_lower: f64,
    /// Upper bound of confidence interval (95%)
    pub confidence_upper: f64,
    /// Prediction confidence score (0.0 - 1.0)
    pub confidence: f64,
    /// Performance category (Fast/Medium/Slow)
    pub category: PerformanceCategory,
    /// Contributing factors (feature importance)
    pub contributing_factors: Vec<(String, f64)>,
}

/// Performance category classification
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PerformanceCategory {
    Fast,     // < 100ms
    Medium,   // 100ms - 1s
    Slow,     // 1s - 10s
    VerySlow, // > 10s
}

impl PerformanceCategory {
    fn from_time_ms(time_ms: f64) -> Self {
        if time_ms < 100.0 {
            Self::Fast
        } else if time_ms < 1000.0 {
            Self::Medium
        } else if time_ms < 10000.0 {
            Self::Slow
        } else {
            Self::VerySlow
        }
    }

    fn emoji(&self) -> &str {
        match self {
            Self::Fast => "🚀",
            Self::Medium => "",
            Self::Slow => "🐌",
            Self::VerySlow => "🐢",
        }
    }

    fn colored_label(&self) -> String {
        match self {
            Self::Fast => "FAST".green().bold().to_string(),
            Self::Medium => "MEDIUM".yellow().bold().to_string(),
            Self::Slow => "SLOW".red().to_string(),
            Self::VerySlow => "VERY SLOW".red().bold().to_string(),
        }
    }
}

/// Simple linear regression model for query performance prediction
///
/// Uses scirs2-core for efficient array operations and statistical analysis
pub struct QueryPerformancePredictor {
    /// Model coefficients (feature weights)
    coefficients: Option<Array1<f64>>,
    /// Model intercept
    intercept: Option<f64>,
    /// Historical query data
    training_data: Vec<(QueryFeatures, f64)>,
    /// Feature importance scores
    feature_importance: HashMap<String, f64>,
    /// Context for CLI output
    ctx: CliContext,
}

impl QueryPerformancePredictor {
    /// Create a new query performance predictor
    pub fn new() -> Self {
        Self {
            coefficients: None,
            intercept: None,
            training_data: Vec::new(),
            feature_importance: HashMap::new(),
            ctx: CliContext::new(),
        }
    }

    /// Add training data (query features, actual execution time in ms)
    pub fn add_training_data(&mut self, features: QueryFeatures, execution_time_ms: f64) {
        self.training_data.push((features, execution_time_ms));
    }

    /// Train the prediction model using collected data
    ///
    /// Uses simple linear regression with scirs2-core arrays for efficiency
    pub fn train(&mut self) -> Result<(), String> {
        if self.training_data.is_empty() {
            return Err("No training data available".to_string());
        }

        if self.training_data.len() < 10 {
            self.ctx.warn(&format!(
                "Limited training data ({} samples) - predictions may be less accurate",
                self.training_data.len()
            ));
        }

        // Convert training data to matrices using scirs2-core
        let n_samples = self.training_data.len();
        let n_features = 12; // Number of features

        let mut x_data = Vec::with_capacity(n_samples * n_features);
        let mut y_data = Vec::with_capacity(n_samples);

        for (features, time) in &self.training_data {
            let feature_array = features.to_array();
            x_data.extend(feature_array.iter().copied());
            y_data.push(*time);
        }

        // Create feature matrix X and target vector y
        let x = Array2::from_shape_vec((n_samples, n_features), x_data)
            .map_err(|e| format!("Failed to create feature matrix: {}", e))?;
        let y = Array1::from_vec(y_data);

        // Simple linear regression: beta = (X^T X)^-1 X^T y
        // For production, use scirs2_linalg for proper matrix operations
        // Here we use a simplified approach

        // Calculate mean-centered data
        let _x_mean = x
            .mean_axis(scirs2_core::ndarray_ext::Axis(0))
            .ok_or("Failed to calculate X mean")?;
        let y_mean = y.mean().ok_or("Failed to calculate y mean")?;

        // Simple coefficient estimation (pseudo-implementation)
        // In production, use proper least squares with scirs2_linalg
        let mut coefficients = Array1::zeros(n_features);
        for i in 0..n_features {
            let feature_col = x.column(i);
            let correlation = calculate_correlation(&feature_col.to_owned(), &y);
            coefficients[i] = correlation * 100.0; // Simplified weight
        }

        self.coefficients = Some(coefficients);
        self.intercept = Some(y_mean);

        // Calculate feature importance
        let feature_names = QueryFeatures::feature_names();
        self.feature_importance.clear();
        if let Some(coefficients) = self.coefficients.as_ref() {
            for (i, name) in feature_names.iter().enumerate() {
                let importance = coefficients[i].abs();
                self.feature_importance.insert(name.to_string(), importance);
            }
        }

        self.ctx.success(&format!(
            "Model trained successfully on {} samples",
            n_samples
        ));

        Ok(())
    }

    /// Predict query performance with confidence intervals
    pub fn predict(&mut self, query: &str) -> Result<PerformancePrediction, String> {
        // Check if model is trained
        if self.coefficients.is_none() {
            // Use pre-trained heuristic model
            return self.predict_heuristic(query);
        }

        let features = QueryFeatures::extract_from_query(query);
        let feature_array = features.to_array();

        let coefficients = self
            .coefficients
            .as_ref()
            .expect("coefficients should be present after is_none check");
        let intercept = self
            .intercept
            .expect("intercept should be present after is_none check");

        // Calculate prediction: y = X * beta + intercept
        let mut predicted_time_ms = intercept;
        for (coef, &feature) in coefficients.iter().zip(feature_array.iter()) {
            predicted_time_ms += coef * feature;
        }

        // Ensure positive prediction
        predicted_time_ms = predicted_time_ms.max(1.0);

        // Calculate confidence interval (simplified - use ±30% as approximation)
        let confidence_margin = predicted_time_ms * 0.3;
        let confidence_lower = (predicted_time_ms - confidence_margin).max(0.1);
        let confidence_upper = predicted_time_ms + confidence_margin;

        // Confidence score based on training data size
        let confidence = if self.training_data.len() > 100 {
            0.9
        } else if self.training_data.len() > 50 {
            0.75
        } else if self.training_data.len() > 10 {
            0.6
        } else {
            0.4
        };

        let category = PerformanceCategory::from_time_ms(predicted_time_ms);

        // Determine contributing factors
        let mut contributing_factors = Vec::new();
        let feature_names = QueryFeatures::feature_names();
        for (i, name) in feature_names.iter().enumerate() {
            let contribution = coefficients[i] * feature_array[i];
            if contribution.abs() > 0.1 {
                contributing_factors.push((name.to_string(), contribution));
            }
        }

        // Sort by absolute contribution
        contributing_factors.sort_by(|a, b| {
            b.1.abs()
                .partial_cmp(&a.1.abs())
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(PerformancePrediction {
            predicted_time_ms,
            confidence_lower,
            confidence_upper,
            confidence,
            category,
            contributing_factors,
        })
    }

    /// Predict using heuristic model when no training data is available
    fn predict_heuristic(&mut self, query: &str) -> Result<PerformancePrediction, String> {
        let features = QueryFeatures::extract_from_query(query);

        // Heuristic model based on empirical observations
        let mut base_time = 10.0; // Base 10ms

        // Add time for triple patterns (each pattern adds ~5ms)
        base_time += features.triple_patterns * 5.0;

        // OPTIONAL clauses are expensive (each adds ~15ms)
        base_time += features.optional_count * 15.0;

        // UNION clauses (each adds ~12ms)
        base_time += features.union_count * 12.0;

        // FILTER expressions (each adds ~8ms)
        base_time += features.filter_count * 8.0;

        // ORDER BY (adds ~20ms)
        base_time += features.order_by_count * 20.0;

        // GROUP BY (adds ~25ms)
        base_time += features.group_by_count * 25.0;

        // Subqueries (each adds ~30ms)
        base_time += features.subquery_count * 30.0;

        // Property paths (complex paths add up to 50ms)
        base_time += features.property_path_complexity * 0.5;

        // Aggregations (each adds ~10ms)
        base_time += features.aggregation_count * 10.0;

        // Selectivity reduces time (high selectivity = faster)
        base_time *= 2.0 - features.selectivity;

        // LIMIT reduces time significantly
        if features.has_limit > 0.5 {
            base_time *= 0.7;
        }

        // DISTINCT adds overhead
        if features.has_distinct > 0.5 {
            base_time *= 1.3;
        }

        let predicted_time_ms = base_time;
        let confidence_margin = predicted_time_ms * 0.5; // Wider margin for heuristic
        let confidence_lower = (predicted_time_ms - confidence_margin).max(0.1);
        let confidence_upper = predicted_time_ms + confidence_margin;

        let category = PerformanceCategory::from_time_ms(predicted_time_ms);

        // Contributing factors from heuristic
        let mut contributing_factors = vec![
            (
                "Triple Patterns".to_string(),
                features.triple_patterns * 5.0,
            ),
            (
                "OPTIONAL Clauses".to_string(),
                features.optional_count * 15.0,
            ),
            ("Subqueries".to_string(), features.subquery_count * 30.0),
        ];
        contributing_factors.retain(|(_, contrib)| contrib.abs() > 5.0);
        contributing_factors.sort_by(|a, b| {
            b.1.abs()
                .partial_cmp(&a.1.abs())
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(PerformancePrediction {
            predicted_time_ms,
            confidence_lower,
            confidence_upper,
            confidence: 0.5, // Moderate confidence for heuristic model
            category,
            contributing_factors,
        })
    }

    /// Display prediction results
    pub fn display_prediction(
        &self,
        prediction: &PerformancePrediction,
        query: &str,
    ) -> CommandResult {
        self.ctx.info("\n🔮 Query Performance Prediction:\n");

        println!("{}", "Query Analysis:".bold().underline());
        let features = QueryFeatures::extract_from_query(query);
        println!("  Triple Patterns: {}", features.triple_patterns);
        println!("  Complexity Indicators:");
        if features.optional_count > 0.0 {
            println!("    - {} OPTIONAL clauses", features.optional_count);
        }
        if features.union_count > 0.0 {
            println!("    - {} UNION clauses", features.union_count);
        }
        if features.filter_count > 0.0 {
            println!("    - {} FILTER expressions", features.filter_count);
        }
        if features.subquery_count > 0.0 {
            println!("    - {} subqueries", features.subquery_count);
        }
        println!();

        println!("{}", "Performance Prediction:".bold().underline());
        println!(
            "  {} Estimated Execution Time: {}{:.2}ms{}",
            prediction.category.emoji(),
            prediction.category.colored_label(),
            prediction.predicted_time_ms,
            "".normal()
        );
        println!(
            "  95% Confidence Interval: [{:.2}ms - {:.2}ms]",
            prediction.confidence_lower, prediction.confidence_upper
        );
        println!(
            "  Prediction Confidence: {:.0}%",
            prediction.confidence * 100.0
        );
        println!();

        if !prediction.contributing_factors.is_empty() {
            println!("{}", "Top Contributing Factors:".bold());
            for (i, (factor, contribution)) in
                prediction.contributing_factors.iter().take(5).enumerate()
            {
                println!("  {}. {} ({:+.1}ms)", i + 1, factor, contribution);
            }
            println!();
        }

        // Performance recommendations
        if matches!(
            prediction.category,
            PerformanceCategory::Slow | PerformanceCategory::VerySlow
        ) {
            println!("{}", "⚠️  Performance Recommendations:".yellow().bold());

            if features.has_limit < 0.5 {
                println!("  • Add LIMIT clause to reduce result set size");
            }
            if features.optional_count > 3.0 {
                println!("  • Consider reducing OPTIONAL clauses or restructuring query");
            }
            if features.selectivity < 0.5 {
                println!("  • Add more specific filters to improve selectivity");
            }
            if features.subquery_count > 2.0 {
                println!("  • Consider flattening nested subqueries");
            }
            println!();
        }

        if prediction.confidence < 0.6 {
            self.ctx.info(&format!(
                "ℹ️  Note: Prediction confidence is {}. Consider training model with more data.",
                if prediction.confidence < 0.5 {
                    "low"
                } else {
                    "moderate"
                }
            ));
        }

        Ok(())
    }
}

impl Default for QueryPerformancePredictor {
    fn default() -> Self {
        Self::new()
    }
}

/// Calculate correlation between two arrays (simplified Pearson correlation)
fn calculate_correlation(x: &Array1<f64>, y: &Array1<f64>) -> f64 {
    if x.len() != y.len() || x.is_empty() {
        return 0.0;
    }

    let _n = x.len() as f64;
    let x_mean = x.mean().unwrap_or(0.0);
    let y_mean = y.mean().unwrap_or(0.0);

    let mut numerator = 0.0;
    let mut x_var = 0.0;
    let mut y_var = 0.0;

    for i in 0..x.len() {
        let x_diff = x[i] - x_mean;
        let y_diff = y[i] - y_mean;
        numerator += x_diff * y_diff;
        x_var += x_diff * x_diff;
        y_var += y_diff * y_diff;
    }

    if x_var < 1e-10 || y_var < 1e-10 {
        return 0.0;
    }

    numerator / ((x_var * y_var).sqrt())
}

/// Command handler for query performance prediction
pub async fn predict_query_performance_cmd(
    query: String,
    train_data: Option<String>,
) -> CommandResult {
    let ctx = CliContext::new();
    ctx.info("🔮 Predicting query performance...\n");

    let mut predictor = QueryPerformancePredictor::new();

    // Load training data if provided
    if let Some(train_file) = train_data {
        ctx.info(&format!("Loading training data from: {}", train_file));
        // TODO: Implement training data loading from file
        ctx.warn("Training data loading not yet implemented - using heuristic model");
    }

    // Make prediction
    let prediction = predictor.predict(&query)?;

    // Display results
    predictor.display_prediction(&prediction, &query)?;

    Ok(())
}

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

    #[test]
    fn test_feature_extraction_simple() {
        let query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10";
        let features = QueryFeatures::extract_from_query(query);

        assert!(features.triple_patterns > 0.0);
        assert_eq!(features.optional_count, 0.0);
        assert_eq!(features.has_limit, 1.0);
    }

    #[test]
    fn test_feature_extraction_complex() {
        let query = r#"
            SELECT DISTINCT ?person ?name
            WHERE {
                ?person foaf:name ?name .
                OPTIONAL { ?person foaf:age ?age }
                FILTER(?age > 18)
            }
            ORDER BY ?name
            LIMIT 100
        "#;
        let features = QueryFeatures::extract_from_query(query);

        assert!(features.triple_patterns > 0.0);
        assert_eq!(features.optional_count, 1.0);
        assert_eq!(features.filter_count, 1.0);
        assert_eq!(features.order_by_count, 1.0);
        assert_eq!(features.has_distinct, 1.0);
        assert_eq!(features.has_limit, 1.0);
    }

    #[test]
    fn test_feature_to_array() {
        let query = "SELECT ?s WHERE { ?s ?p ?o }";
        let features = QueryFeatures::extract_from_query(query);
        let array = features.to_array();

        assert_eq!(array.len(), 12); // 12 features
    }

    #[test]
    fn test_performance_category() {
        assert_eq!(
            PerformanceCategory::from_time_ms(50.0),
            PerformanceCategory::Fast
        );
        assert_eq!(
            PerformanceCategory::from_time_ms(500.0),
            PerformanceCategory::Medium
        );
        assert_eq!(
            PerformanceCategory::from_time_ms(5000.0),
            PerformanceCategory::Slow
        );
        assert_eq!(
            PerformanceCategory::from_time_ms(15000.0),
            PerformanceCategory::VerySlow
        );
    }

    #[test]
    fn test_predictor_creation() {
        let predictor = QueryPerformancePredictor::new();
        assert!(predictor.coefficients.is_none());
        assert!(predictor.training_data.is_empty());
    }

    #[test]
    fn test_heuristic_prediction_simple() {
        let mut predictor = QueryPerformancePredictor::new();
        let query = "SELECT ?s WHERE { ?s ?p ?o } LIMIT 10";

        let result = predictor.predict(query);
        assert!(result.is_ok());

        let prediction = result.unwrap();
        assert!(prediction.predicted_time_ms > 0.0);
        assert!(prediction.confidence_lower > 0.0);
        assert!(prediction.confidence_upper > prediction.predicted_time_ms);
    }

    #[test]
    fn test_heuristic_prediction_complex() {
        let mut predictor = QueryPerformancePredictor::new();
        let query = r#"
            SELECT DISTINCT ?x
            WHERE {
                ?x ?p1 ?y .
                OPTIONAL { ?y ?p2 ?z }
                UNION { ?x ?p3 ?w }
                FILTER(?x != ?y)
            }
            ORDER BY ?x
        "#;

        let result = predictor.predict(query);
        assert!(result.is_ok());

        let prediction = result.unwrap();
        // Complex query should predict slower execution
        assert!(prediction.predicted_time_ms > 50.0);
    }

    #[test]
    fn test_training_data_addition() {
        let mut predictor = QueryPerformancePredictor::new();
        let features = QueryFeatures::extract_from_query("SELECT ?s WHERE { ?s ?p ?o }");

        predictor.add_training_data(features, 25.0);
        assert_eq!(predictor.training_data.len(), 1);
    }

    #[test]
    fn test_model_training_insufficient_data() {
        let mut predictor = QueryPerformancePredictor::new();
        let result = predictor.train();

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("No training data"));
    }

    #[test]
    fn test_correlation_calculation() {
        let x = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let y = Array1::from_vec(vec![2.0, 4.0, 6.0, 8.0, 10.0]);

        let corr = calculate_correlation(&x, &y);
        // Perfect positive correlation
        assert!((corr - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_prediction_confidence_intervals() {
        let mut predictor = QueryPerformancePredictor::new();
        let query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 100";

        let prediction = predictor.predict(query).unwrap();

        // Confidence interval should bracket the prediction
        assert!(prediction.confidence_lower < prediction.predicted_time_ms);
        assert!(prediction.confidence_upper > prediction.predicted_time_ms);

        // Confidence should be between 0 and 1
        assert!(prediction.confidence >= 0.0 && prediction.confidence <= 1.0);
    }
}