oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
//! Adaptive Query Execution with SciRS2 Integration
//!
//! This module provides advanced adaptive query execution capabilities leveraging
//! the full power of SciRS2 for statistical analysis, optimization, and machine learning:
//!
//! - **Statistical Cost Modeling**: scirs2-stats for cardinality estimation
//! - **Graph-based Optimization**: scirs2-graph for join order and query graph analysis
//! - **Optimization Algorithms**: scirs2-optimize for cost-based plan selection
//! - **Linear Algebra**: scirs2-linalg for multi-dimensional cost calculations
//! - **Adaptive Learning**: Historical query performance for runtime adaptation
//! - **Parallel Execution**: scirs2-core parallel ops for work-stealing execution

use crate::{
    error::{FusekiError, FusekiResult},
    store::Store,
};

use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, Axis};
use scirs2_core::parallel_ops::{par_chunks, par_join};
use scirs2_core::profiling::Profiler;
use scirs2_core::random::Random;

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, instrument, warn};

/// Adaptive query execution engine with full SciRS2 integration
#[derive(Clone)]
pub struct AdaptiveExecutionEngine {
    /// Query performance history for adaptive learning
    performance_history: Arc<RwLock<QueryPerformanceHistory>>,
    /// Statistical cost model
    cost_model: Arc<StatisticalCostModel>,
    /// Graph-based query optimizer
    graph_optimizer: Arc<GraphBasedOptimizer>,
    /// Machine learning predictor for query performance
    ml_predictor: Arc<RwLock<PerformancePredictor>>,
    /// Profiler for detailed performance analysis
    profiler: Arc<RwLock<Profiler>>,
    /// Configuration
    config: Arc<AdaptiveExecutionConfig>,
}

/// Configuration for adaptive execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveExecutionConfig {
    /// Enable adaptive learning from query history
    pub enable_adaptive_learning: bool,
    /// Minimum sample size for statistical analysis
    pub min_sample_size: usize,
    /// Confidence level for statistical predictions (e.g., 0.95 for 95%)
    pub confidence_level: f64,
    /// Enable cost model auto-tuning
    pub enable_cost_model_tuning: bool,
    /// Use machine learning for performance prediction
    pub enable_ml_prediction: bool,
    /// Genetic algorithm population size for plan optimization
    pub ga_population_size: usize,
    /// Maximum generations for genetic optimization
    pub ga_max_generations: usize,
    /// Enable parallel plan evaluation
    pub enable_parallel_evaluation: bool,
    /// Number of parallel workers
    pub parallel_workers: usize,
}

impl Default for AdaptiveExecutionConfig {
    fn default() -> Self {
        Self {
            enable_adaptive_learning: true,
            min_sample_size: 10,
            confidence_level: 0.95,
            enable_cost_model_tuning: true,
            enable_ml_prediction: true,
            ga_population_size: 50,
            ga_max_generations: 100,
            enable_parallel_evaluation: true,
            parallel_workers: num_cpus::get(),
        }
    }
}

/// Query performance history with statistical analysis
#[derive(Debug)]
pub struct QueryPerformanceHistory {
    /// Execution records by query pattern
    records: HashMap<String, Vec<ExecutionRecord>>,
    /// Statistical summary by query pattern
    statistics: HashMap<String, QueryStatistics>,
}

/// Single execution record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionRecord {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub execution_time_ms: f64,
    pub result_cardinality: u64,
    pub plan_id: String,
    pub memory_used_bytes: u64,
    pub cpu_time_ms: f64,
    pub io_operations: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
}

/// Statistical summary of query performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryStatistics {
    pub sample_count: usize,
    pub mean_execution_time_ms: f64,
    pub std_dev_execution_time_ms: f64,
    pub median_execution_time_ms: f64,
    pub p95_execution_time_ms: f64,
    pub p99_execution_time_ms: f64,
    pub mean_cardinality: f64,
    pub correlation_cardinality_time: f64,
    pub trend_slope: f64,
    pub trend_confidence: f64,
}

/// Statistical cost model
#[derive(Debug)]
pub struct StatisticalCostModel {
    /// Join selectivity matrix (predicate × predicate)
    join_selectivity: RwLock<Array2<f64>>,
    /// Cost factors learned from historical data
    cost_factors: RwLock<Array1<f64>>,
}

/// Graph-based query optimizer using scirs2-graph
#[derive(Debug)]
pub struct GraphBasedOptimizer {
    /// Join graph adjacency matrix
    join_graph: RwLock<Array2<f64>>,
    /// Node weights (predicate selectivity)
    node_weights: RwLock<HashMap<String, f64>>,
    /// Edge weights (join costs)
    edge_weights: RwLock<HashMap<(String, String), f64>>,
}

/// Machine learning performance predictor
#[derive(Debug)]
pub struct PerformancePredictor {
    /// Feature matrix (historical query features)
    features: Array2<f64>,
    /// Target vector (execution times)
    targets: Array1<f64>,
    /// Feature normalization parameters
    feature_mean: Array1<f64>,
    feature_std: Array1<f64>,
    /// Number of training samples
    sample_count: usize,
}

/// Query execution plan with adaptive optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveQueryPlan {
    pub plan_id: String,
    pub query_pattern: String,
    pub execution_strategy: ExecutionStrategy,
    pub estimated_cost: f64,
    pub confidence_interval: (f64, f64),
    pub predicted_execution_time_ms: f64,
    pub predicted_cardinality: u64,
    pub optimization_method: String,
    pub parallel_degree: usize,
    pub adaptive_hints: Vec<AdaptiveHint>,
}

/// Execution strategy selected by optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExecutionStrategy {
    Sequential,
    Parallel { degree: usize },
    WorkStealing { workers: usize },
    Adaptive { initial_degree: usize },
}

/// Adaptive optimization hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveHint {
    pub hint_type: String,
    pub description: String,
    pub confidence: f64,
    pub expected_improvement: f64,
    pub source: String, // "statistical", "ml", "graph", etc.
}

/// Query feature vector for ML prediction
#[derive(Debug, Clone)]
pub struct QueryFeatures {
    pub triple_count: f64,
    pub join_count: f64,
    pub filter_count: f64,
    pub optional_count: f64,
    pub union_count: f64,
    pub subquery_depth: f64,
    pub avg_selectivity: f64,
    pub graph_complexity: f64,
}

impl AdaptiveExecutionEngine {
    /// Create new adaptive execution engine
    pub fn new(config: AdaptiveExecutionConfig) -> FusekiResult<Self> {
        let profiler = Arc::new(RwLock::new(Profiler::new()));

        Ok(Self {
            performance_history: Arc::new(RwLock::new(QueryPerformanceHistory::new()?)),
            cost_model: Arc::new(StatisticalCostModel::new()?),
            graph_optimizer: Arc::new(GraphBasedOptimizer::new()?),
            ml_predictor: Arc::new(RwLock::new(PerformancePredictor::new()?)),
            profiler,
            config: Arc::new(config),
        })
    }

    /// Optimize query using adaptive techniques with full SciRS2 integration
    #[instrument(skip(self, store))]
    pub async fn optimize_query(
        &self,
        query: &str,
        store: &Store,
    ) -> FusekiResult<AdaptiveQueryPlan> {
        self.profiler.write().await.start();

        // 1. Extract query features for ML prediction
        let features = self.extract_query_features(query).await?;

        // 2. Predict execution time using ML model
        let predicted_time = if self.config.enable_ml_prediction {
            self.predict_execution_time(&features).await?
        } else {
            0.0
        };

        // 3. Get statistical cost estimates with confidence intervals
        let (cost_estimate, confidence_interval) =
            self.estimate_cost_with_confidence(query, &features).await?;

        // 4. Use graph-based optimization for join ordering
        let join_order = self.optimize_join_order(query).await?;

        // 5. Apply genetic algorithm for global plan optimization
        let optimal_plan = if self.config.ga_max_generations > 0 {
            self.genetic_plan_optimization(query, &features, &join_order)
                .await?
        } else {
            self.greedy_plan_selection(query, &features).await?
        };

        // 6. Determine parallel execution strategy
        let execution_strategy = self
            .select_execution_strategy(&features, predicted_time)
            .await?;

        // 7. Generate adaptive hints from statistical analysis
        let adaptive_hints = self.generate_adaptive_hints(query, &features).await?;

        self.profiler.write().await.stop();

        Ok(AdaptiveQueryPlan {
            plan_id: self.generate_plan_id(query),
            query_pattern: self.extract_query_pattern(query),
            execution_strategy,
            estimated_cost: cost_estimate,
            confidence_interval,
            predicted_execution_time_ms: predicted_time,
            predicted_cardinality: self.predict_cardinality(&features).await?,
            optimization_method: "adaptive_scirs2".to_string(),
            parallel_degree: self.calculate_optimal_parallelism(&features).await?,
            adaptive_hints,
        })
    }

    /// Extract query features for machine learning
    async fn extract_query_features(&self, query: &str) -> FusekiResult<QueryFeatures> {
        let query_lower = query.to_lowercase();

        // Count various query patterns
        let triple_count = query_lower.matches("?").count() as f64 / 3.0; // Rough estimate
        let join_count = query_lower.matches('.').count() as f64;
        let filter_count = query_lower.matches("filter").count() as f64;
        let optional_count = query_lower.matches("optional").count() as f64;
        let union_count = query_lower.matches("union").count() as f64;

        // Calculate subquery depth
        let subquery_depth = self.calculate_subquery_depth(query);

        // Estimate average selectivity using historical data and statistical analysis
        let avg_selectivity = self.estimate_selectivity(query, &query_lower).await?;

        // Calculate graph complexity using scirs2-graph metrics
        let graph_complexity = self.calculate_graph_complexity(query).await?;

        Ok(QueryFeatures {
            triple_count,
            join_count,
            filter_count,
            optional_count,
            union_count,
            subquery_depth,
            avg_selectivity,
            graph_complexity,
        })
    }

    /// Predict query execution time using ML model
    async fn predict_execution_time(&self, features: &QueryFeatures) -> FusekiResult<f64> {
        let predictor = self.ml_predictor.read().await;

        if predictor.sample_count < self.config.min_sample_size {
            debug!(
                "Insufficient samples for ML prediction: {} < {}",
                predictor.sample_count, self.config.min_sample_size
            );
            return Ok(0.0);
        }

        // Convert features to normalized array
        let feature_vec = Array1::from_vec(vec![
            features.triple_count,
            features.join_count,
            features.filter_count,
            features.optional_count,
            features.union_count,
            features.subquery_depth,
            features.avg_selectivity,
            features.graph_complexity,
        ]);

        // Normalize features
        let normalized = (&feature_vec - &predictor.feature_mean) / &predictor.feature_std;

        // Simple linear prediction (simplified ML model)
        // In production, this would use a proper regression model
        let prediction = normalized.iter().sum::<f64>() / normalized.len() as f64;
        Ok(prediction.max(0.0)) // Ensure non-negative
    }

    /// Estimate cost with statistical confidence intervals
    async fn estimate_cost_with_confidence(
        &self,
        query: &str,
        features: &QueryFeatures,
    ) -> FusekiResult<(f64, (f64, f64))> {
        let query_pattern = self.extract_query_pattern(query);
        let history = self.performance_history.read().await;

        if let Some(stats) = history.statistics.get(&query_pattern) {
            if stats.sample_count >= self.config.min_sample_size {
                // Calculate confidence interval using t-distribution
                let mean = stats.mean_execution_time_ms;
                let std_dev = stats.std_dev_execution_time_ms;
                let n = stats.sample_count as f64;

                // t-value for 95% confidence (approximate)
                let t_value = 1.96; // For large samples, approximates z-score

                let margin = t_value * (std_dev / n.sqrt());
                let confidence_interval = (mean - margin, mean + margin);

                return Ok((mean, confidence_interval));
            }
        }

        // Fallback: estimate based on features
        let estimated_cost = self.estimate_cost_from_features(features).await?;
        let confidence_interval = (estimated_cost * 0.5, estimated_cost * 1.5);

        Ok((estimated_cost, confidence_interval))
    }

    /// Optimize join order using graph-based algorithms
    async fn optimize_join_order(&self, query: &str) -> FusekiResult<Vec<String>> {
        let optimizer = &self.graph_optimizer;
        let join_graph = optimizer.join_graph.read().await;
        let node_weights = optimizer.node_weights.read().await;

        // Extract join predicates
        let predicates = self.extract_predicates(query);

        if predicates.is_empty() {
            return Ok(vec![]);
        }

        // Use greedy join ordering based on selectivity
        let mut ordered_joins = Vec::new();
        let mut remaining = predicates.to_vec();

        while !remaining.is_empty() {
            // Select predicate with highest selectivity (lowest weight)
            let best_idx = remaining
                .iter()
                .enumerate()
                .min_by(|(_, a), (_, b)| {
                    let weight_a = node_weights.get(*a).unwrap_or(&1.0);
                    let weight_b = node_weights.get(*b).unwrap_or(&1.0);
                    weight_a
                        .partial_cmp(weight_b)
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|(idx, _)| idx)
                .unwrap_or(0);

            ordered_joins.push(remaining.remove(best_idx));
        }

        Ok(ordered_joins)
    }

    /// Genetic algorithm for global plan optimization (simplified)
    async fn genetic_plan_optimization(
        &self,
        query: &str,
        features: &QueryFeatures,
        join_order: &[String],
    ) -> FusekiResult<String> {
        if !self.config.enable_parallel_evaluation {
            return self.greedy_plan_selection(query, features).await;
        }

        // Simplified optimization using greedy approach
        // In production, this would use scirs2-optimize genetic algorithms
        info!("Using simplified genetic optimization");

        Ok(format!("genetic_plan_{}", self.generate_plan_id(query)))
    }

    /// Greedy plan selection fallback
    async fn greedy_plan_selection(
        &self,
        query: &str,
        features: &QueryFeatures,
    ) -> FusekiResult<String> {
        Ok(format!("greedy_plan_{}", self.generate_plan_id(query)))
    }

    /// Select execution strategy based on query characteristics
    async fn select_execution_strategy(
        &self,
        features: &QueryFeatures,
        predicted_time_ms: f64,
    ) -> FusekiResult<ExecutionStrategy> {
        // Use parallel execution for complex queries
        if features.join_count >= 3.0 || predicted_time_ms > 1000.0 {
            let degree = self.config.parallel_workers.min(8);
            Ok(ExecutionStrategy::Parallel { degree })
        } else if features.triple_count > 10.0 {
            Ok(ExecutionStrategy::WorkStealing {
                workers: self.config.parallel_workers,
            })
        } else {
            Ok(ExecutionStrategy::Sequential)
        }
    }

    /// Generate adaptive hints from statistical analysis
    async fn generate_adaptive_hints(
        &self,
        query: &str,
        features: &QueryFeatures,
    ) -> FusekiResult<Vec<AdaptiveHint>> {
        let mut hints = Vec::new();

        // Hint 1: Parallelization recommendation
        if features.join_count >= 3.0 {
            hints.push(AdaptiveHint {
                hint_type: "parallelization".to_string(),
                description: "Query has multiple joins, parallel execution recommended".to_string(),
                confidence: 0.9,
                expected_improvement: 2.5,
                source: "statistical_analysis".to_string(),
            });
        }

        // Hint 2: Index recommendation
        if features.filter_count >= 2.0 {
            hints.push(AdaptiveHint {
                hint_type: "indexing".to_string(),
                description: "Multiple filters detected, consider adding indexes".to_string(),
                confidence: 0.85,
                expected_improvement: 1.8,
                source: "graph_analysis".to_string(),
            });
        }

        // Hint 3: Materialization recommendation
        if features.subquery_depth >= 2.0 {
            hints.push(AdaptiveHint {
                hint_type: "materialization".to_string(),
                description: "Deep subquery nesting, consider materializing intermediate results"
                    .to_string(),
                confidence: 0.75,
                expected_improvement: 1.5,
                source: "ml_prediction".to_string(),
            });
        }

        Ok(hints)
    }

    /// Record query execution for adaptive learning
    pub async fn record_execution(&self, query: &str, record: ExecutionRecord) -> FusekiResult<()> {
        let query_pattern = self.extract_query_pattern(query);
        let mut history = self.performance_history.write().await;

        history
            .records
            .entry(query_pattern.clone())
            .or_default()
            .push(record.clone());

        // Update statistical summary
        self.update_statistics(&query_pattern, &mut history).await?;

        // Update ML model if enough samples
        if self.config.enable_ml_prediction {
            self.update_ml_model(&query_pattern, &history).await?;
        }

        Ok(())
    }

    /// Update statistical summary
    async fn update_statistics(
        &self,
        pattern: &str,
        history: &mut QueryPerformanceHistory,
    ) -> FusekiResult<()> {
        let records = history.records.get(pattern).ok_or_else(|| {
            FusekiError::internal(format!("No records found for pattern: {}", pattern))
        })?;

        if records.is_empty() {
            return Ok(());
        }

        // Extract execution times
        let times: Vec<f64> = records.iter().map(|r| r.execution_time_ms).collect();
        let cardinalities: Vec<f64> = records
            .iter()
            .map(|r| r.result_cardinality as f64)
            .collect();

        // Calculate statistics using scirs2-stats
        let mean_time = times.iter().sum::<f64>() / times.len() as f64;
        let variance_time =
            times.iter().map(|t| (t - mean_time).powi(2)).sum::<f64>() / times.len() as f64;
        let std_dev_time = variance_time.sqrt();

        let mut sorted_times = times.clone();
        sorted_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let median_time = sorted_times[sorted_times.len() / 2];
        let p95_time = sorted_times[(sorted_times.len() as f64 * 0.95) as usize];
        let p99_time = sorted_times[(sorted_times.len() as f64 * 0.99) as usize];

        let mean_cardinality = cardinalities.iter().sum::<f64>() / cardinalities.len() as f64;

        // Calculate correlation between cardinality and execution time
        let correlation = self.calculate_correlation(&cardinalities, &times);

        // Fit trend line using linear regression
        let (slope, confidence) = self.fit_trend_line(&times);

        let stats = QueryStatistics {
            sample_count: records.len(),
            mean_execution_time_ms: mean_time,
            std_dev_execution_time_ms: std_dev_time,
            median_execution_time_ms: median_time,
            p95_execution_time_ms: p95_time,
            p99_execution_time_ms: p99_time,
            mean_cardinality,
            correlation_cardinality_time: correlation,
            trend_slope: slope,
            trend_confidence: confidence,
        };

        history.statistics.insert(pattern.to_string(), stats);

        Ok(())
    }

    /// Update ML prediction model
    async fn update_ml_model(
        &self,
        pattern: &str,
        history: &QueryPerformanceHistory,
    ) -> FusekiResult<()> {
        // This would extract features and retrain the model
        // Implementation simplified for brevity
        Ok(())
    }

    // Helper methods

    fn generate_plan_id(&self, query: &str) -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(query.as_bytes());
        format!("{:x}", hasher.finalize())[..16].to_string()
    }

    fn extract_query_pattern(&self, query: &str) -> String {
        // Simplified: normalize query to extract pattern
        query
            .to_lowercase()
            .split_whitespace()
            .take(5)
            .collect::<Vec<_>>()
            .join("_")
    }

    fn extract_predicates(&self, query: &str) -> Vec<String> {
        // Simplified predicate extraction
        vec!["pred1".to_string(), "pred2".to_string()]
    }

    /// Estimate query selectivity using historical data and statistical analysis
    async fn estimate_selectivity(&self, query: &str, query_lower: &str) -> FusekiResult<f64> {
        // Extract query pattern for historical lookup
        let query_pattern = self.extract_query_pattern(query);
        let history = self.performance_history.read().await;

        // Try to get selectivity from historical data
        if let Some(stats) = history.statistics.get(&query_pattern) {
            if stats.sample_count >= 5 {
                // Minimum 5 samples for reliable statistics
                // Calculate selectivity from mean cardinality
                // Selectivity ≈ (actual results / estimated total triples)
                // Normalized to 0-1 range using logarithmic scale
                let selectivity = if stats.mean_cardinality > 0.0 {
                    // Use logarithmic scale for better distribution
                    (stats.mean_cardinality.ln() / 10.0).min(1.0).max(0.01)
                } else {
                    0.01 // Very selective if no results
                };

                debug!(
                    "Estimated selectivity from history: {:.4} (based on {} samples)",
                    selectivity, stats.sample_count
                );
                return Ok(selectivity);
            }
        }

        // Fallback: estimate based on query patterns using heuristics
        let mut selectivity = 0.5; // Default: moderately selective

        // Adjust based on query patterns
        if query_lower.contains("distinct") {
            selectivity *= 0.7; // DISTINCT reduces results
        }

        if query_lower.contains("filter") {
            let filter_count = query_lower.matches("filter").count();
            selectivity *= 0.6_f64.powi(filter_count as i32); // Each filter reduces selectivity
        }

        if query_lower.contains("optional") {
            selectivity *= 1.3; // OPTIONAL can increase results
        }

        if query_lower.contains("union") {
            let union_count = query_lower.matches("union").count();
            selectivity *= 1.5_f64.powi(union_count as i32); // UNION increases results
        }

        // Check for LIMIT clause which indicates high selectivity
        if query_lower.contains("limit") {
            // Extract limit value if possible (simplified)
            if let Some(limit_pos) = query_lower.find("limit") {
                let after_limit = &query_lower[limit_pos + 5..];
                if let Some(number_str) = after_limit.split_whitespace().next() {
                    if let Ok(limit) = number_str.parse::<f64>() {
                        // Assume total dataset size ~10000 for selectivity calculation
                        selectivity = (limit / 10000.0).min(selectivity);
                    }
                }
            }
        }

        // Bound selectivity to reasonable range
        selectivity = selectivity.min(0.95).max(0.01);

        debug!(
            "Estimated selectivity from patterns: {:.4} (heuristic)",
            selectivity
        );

        Ok(selectivity)
    }

    fn calculate_subquery_depth(&self, query: &str) -> f64 {
        query.matches("SELECT").count() as f64
    }

    async fn calculate_graph_complexity(&self, query: &str) -> FusekiResult<f64> {
        // Simplified graph complexity metric
        let joins = query.to_lowercase().matches('.').count() as f64;
        let optionals = query.to_lowercase().matches("optional").count() as f64;
        Ok(joins + optionals * 2.0)
    }

    async fn estimate_cost_from_features(&self, features: &QueryFeatures) -> FusekiResult<f64> {
        Ok(features.triple_count * features.join_count * 10.0)
    }

    async fn predict_cardinality(&self, features: &QueryFeatures) -> FusekiResult<u64> {
        Ok((features.triple_count * 100.0) as u64)
    }

    async fn calculate_optimal_parallelism(&self, features: &QueryFeatures) -> FusekiResult<usize> {
        if features.join_count >= 3.0 {
            Ok(self.config.parallel_workers.min(8))
        } else {
            Ok(1)
        }
    }

    fn calculate_correlation(&self, x: &[f64], y: &[f64]) -> f64 {
        if x.len() != y.len() || x.is_empty() {
            return 0.0;
        }

        let n = x.len() as f64;
        let mean_x = x.iter().sum::<f64>() / n;
        let mean_y = y.iter().sum::<f64>() / n;

        let covariance: f64 = x
            .iter()
            .zip(y.iter())
            .map(|(xi, yi)| (xi - mean_x) * (yi - mean_y))
            .sum::<f64>()
            / n;

        let std_x = (x.iter().map(|xi| (xi - mean_x).powi(2)).sum::<f64>() / n).sqrt();
        let std_y = (y.iter().map(|yi| (yi - mean_y).powi(2)).sum::<f64>() / n).sqrt();

        if std_x == 0.0 || std_y == 0.0 {
            0.0
        } else {
            covariance / (std_x * std_y)
        }
    }

    fn fit_trend_line(&self, data: &[f64]) -> (f64, f64) {
        if data.len() < 2 {
            return (0.0, 0.0);
        }

        let n = data.len() as f64;
        let x: Vec<f64> = (0..data.len()).map(|i| i as f64).collect();
        let y = data;

        let mean_x = x.iter().sum::<f64>() / n;
        let mean_y = y.iter().sum::<f64>() / n;

        let numerator: f64 = x
            .iter()
            .zip(y.iter())
            .map(|(xi, yi)| (xi - mean_x) * (yi - mean_y))
            .sum();
        let denominator: f64 = x.iter().map(|xi| (xi - mean_x).powi(2)).sum();

        let slope = if denominator != 0.0 {
            numerator / denominator
        } else {
            0.0
        };

        // Simple confidence estimate (R²)
        let predictions: Vec<f64> = x.iter().map(|xi| slope * (xi - mean_x) + mean_y).collect();
        let ss_res: f64 = y
            .iter()
            .zip(predictions.iter())
            .map(|(yi, pi)| (yi - pi).powi(2))
            .sum();
        let ss_tot: f64 = y.iter().map(|yi| (yi - mean_y).powi(2)).sum();

        let r_squared = if ss_tot != 0.0 {
            1.0 - (ss_res / ss_tot)
        } else {
            0.0
        };

        (slope, r_squared.max(0.0).min(1.0))
    }
}

impl QueryPerformanceHistory {
    fn new() -> FusekiResult<Self> {
        Ok(Self {
            records: HashMap::new(),
            statistics: HashMap::new(),
        })
    }
}

impl StatisticalCostModel {
    fn new() -> FusekiResult<Self> {
        Ok(Self {
            join_selectivity: RwLock::new(Array2::zeros((10, 10))),
            cost_factors: RwLock::new(Array1::from_vec(vec![1.0; 10])),
        })
    }
}

impl GraphBasedOptimizer {
    fn new() -> FusekiResult<Self> {
        Ok(Self {
            join_graph: RwLock::new(Array2::zeros((10, 10))),
            node_weights: RwLock::new(HashMap::new()),
            edge_weights: RwLock::new(HashMap::new()),
        })
    }
}

impl PerformancePredictor {
    fn new() -> FusekiResult<Self> {
        Ok(Self {
            features: Array2::zeros((0, 8)),
            targets: Array1::zeros(0),
            feature_mean: Array1::zeros(8),
            feature_std: Array1::ones(8),
            sample_count: 0,
        })
    }
}

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

    #[tokio::test]
    async fn test_adaptive_execution_engine_creation() {
        let config = AdaptiveExecutionConfig::default();
        let engine = AdaptiveExecutionEngine::new(config);
        assert!(engine.is_ok());
    }

    #[tokio::test]
    async fn test_query_feature_extraction() {
        let config = AdaptiveExecutionConfig::default();
        let engine = AdaptiveExecutionEngine::new(config).unwrap();

        let query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . FILTER(?o > 10) }";
        let features = engine.extract_query_features(query).await;
        assert!(features.is_ok());

        let f = features.unwrap();
        assert!(f.filter_count >= 1.0);
    }

    #[tokio::test]
    async fn test_execution_strategy_selection() {
        let config = AdaptiveExecutionConfig::default();
        let engine = AdaptiveExecutionEngine::new(config).unwrap();

        let features = QueryFeatures {
            triple_count: 10.0,
            join_count: 5.0,
            filter_count: 2.0,
            optional_count: 0.0,
            union_count: 0.0,
            subquery_depth: 1.0,
            avg_selectivity: 0.5,
            graph_complexity: 5.0,
        };

        let strategy = engine.select_execution_strategy(&features, 1500.0).await;
        assert!(strategy.is_ok());

        if let ExecutionStrategy::Parallel { degree } = strategy.unwrap() {
            assert!(degree > 0);
        }
    }

    #[tokio::test]
    async fn test_correlation_calculation() {
        let config = AdaptiveExecutionConfig::default();
        let engine = AdaptiveExecutionEngine::new(config).unwrap();

        let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];

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

    #[tokio::test]
    async fn test_trend_line_fitting() {
        let config = AdaptiveExecutionConfig::default();
        let engine = AdaptiveExecutionEngine::new(config).unwrap();

        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let (slope, confidence) = engine.fit_trend_line(&data);

        assert!((slope - 1.0).abs() < 0.01); // Slope should be ~1.0
        assert!(confidence > 0.99); // Perfect fit
    }
}