oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
//! JIT Compilation for SPARQL Queries
//!
//! This module provides Just-In-Time compilation for SPARQL queries, transforming
//! high-level SPARQL algebra into optimized execution plans that can be compiled
//! to native code or bytecode for improved performance.
//!
//! # Features
//!
//! - **Query Plan Compilation**: Transform SPARQL algebra into optimized execution plans
//! - **Code Generation**: Generate specialized code for query patterns
//! - **Plan Caching**: Reuse compiled plans with intelligent invalidation
//! - **Adaptive Optimization**: Runtime profiling and re-compilation
//! - **Performance Tracking**: Measure compilation and execution metrics
//!
//! # Architecture
//!
//! ```text
//! SPARQL Query → Algebra → Plan Generation → Code Gen → Specialized Executor
//!                   ↓           ↓              ↓              ↓
//!              Optimization  Lowering    Specialization  Execution
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use oxirs_arq::jit_compiler::{QueryJitCompiler, JitCompilerConfig};
//!
//! let config = JitCompilerConfig::default();
//! let mut compiler = QueryJitCompiler::new(config)?;
//!
//! // Compile a SPARQL algebra
//! let compiled = compiler.compile(&algebra)?;
//!
//! // Execute compiled plan
//! let results = compiled.execute(&dataset)?;
//! ```

use crate::algebra::Algebra;
use crate::cardinality_estimator::{CardinalityEstimator, EstimatorConfig};
use anyhow::Result;
use dashmap::DashMap;
use parking_lot::RwLock;
use scirs2_core::metrics::MetricsRegistry;
use scirs2_core::profiling::Profiler;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

/// Configuration for SPARQL query JIT compilation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JitCompilerConfig {
    /// Enable JIT compilation
    pub enabled: bool,

    /// Optimization level (0-3)
    pub optimization_level: usize,

    /// Enable query plan caching
    pub enable_caching: bool,

    /// Maximum cache size (in bytes)
    pub max_cache_size: usize,

    /// Cache TTL (time-to-live)
    pub cache_ttl: Duration,

    /// Enable adaptive optimization
    pub adaptive_optimization: bool,

    /// Minimum execution count before re-optimization
    pub min_executions_for_reopt: usize,

    /// Compilation timeout
    pub compilation_timeout: Duration,

    /// Enable performance profiling
    pub enable_profiling: bool,

    /// Enable specialized code generation
    pub enable_specialization: bool,

    /// Maximum plan complexity for compilation
    pub max_plan_complexity: usize,
}

impl Default for JitCompilerConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            optimization_level: 2,
            enable_caching: true,
            max_cache_size: 512 * 1024 * 1024,    // 512MB
            cache_ttl: Duration::from_secs(3600), // 1 hour
            adaptive_optimization: true,
            min_executions_for_reopt: 10,
            compilation_timeout: Duration::from_secs(30),
            enable_profiling: true,
            enable_specialization: true,
            max_plan_complexity: 1000,
        }
    }
}

/// Compiled SPARQL query plan ready for execution
#[derive(Clone)]
pub struct CompiledQuery {
    /// Unique query identifier
    pub id: String,

    /// Original SPARQL algebra
    pub algebra: Arc<Algebra>,

    /// Compiled execution plan
    pub plan: Arc<ExecutionPlan>,

    /// Compilation timestamp
    pub compiled_at: Instant,

    /// Execution statistics
    pub stats: Arc<RwLock<ExecutionStats>>,

    /// Optimization metadata
    pub metadata: QueryMetadata,
}

impl fmt::Debug for CompiledQuery {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CompiledQuery")
            .field("id", &self.id)
            .field("compiled_at", &self.compiled_at.elapsed())
            .field("stats", &self.stats)
            .field("metadata", &self.metadata)
            .finish()
    }
}

/// Execution plan generated from SPARQL algebra
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionPlan {
    /// Plan operations
    pub operations: Vec<PlanOperation>,

    /// Estimated cost
    pub estimated_cost: f64,

    /// Estimated memory usage (bytes)
    pub estimated_memory: usize,

    /// Optimization hints applied
    pub optimization_hints: Vec<String>,

    /// Specialization metadata
    pub specializations: Vec<Specialization>,
}

/// Individual operation in execution plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PlanOperation {
    /// Scan triple patterns (specialized for pattern type)
    ScanTriples {
        pattern_id: usize,
        pattern_type: PatternType,
        estimated_cardinality: usize,
    },

    /// Hash join operation (with strategy)
    HashJoin {
        left_id: usize,
        right_id: usize,
        join_variables: Vec<String>,
        strategy: JitJoinStrategy,
    },

    /// Nested loop join (for small cardinalities)
    NestedLoopJoin {
        left_id: usize,
        right_id: usize,
        join_variables: Vec<String>,
    },

    /// Filter operation (with specialization)
    Filter {
        expr_id: usize,
        filter_type: FilterType,
    },

    /// Project variables
    Project { variables: Vec<String> },

    /// Sort operation
    Sort {
        variables: Vec<String>,
        ascending: Vec<bool>,
    },

    /// Limit operation
    Limit { limit: usize },

    /// Offset operation
    Offset { offset: usize },

    /// Distinct operation
    Distinct,

    /// Union operation
    Union { branches: Vec<usize> },

    /// Optional (left join) operation
    Optional { left_id: usize, right_id: usize },
}

/// Pattern types for specialization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PatternType {
    /// All variables (?s ?p ?o)
    AllVariables,

    /// Subject bound (s ?p ?o)
    SubjectBound,

    /// Predicate bound (?s p ?o)
    PredicateBound,

    /// Object bound (?s ?p o)
    ObjectBound,

    /// Subject-Predicate bound (s p ?o)
    SubjectPredicateBound,

    /// Subject-Object bound (s ?p o)
    SubjectObjectBound,

    /// Predicate-Object bound (?s p o)
    PredicateObjectBound,

    /// Fully bound (s p o)
    FullyBound,
}

/// Join strategies for JIT compilation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum JitJoinStrategy {
    /// Hash join (default for large inputs)
    Hash,

    /// Sort-merge join
    SortMerge,

    /// Index nested loop join
    IndexNestedLoop,

    /// Bind join (for federated queries)
    Bind,
}

/// Filter types for optimization
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FilterType {
    /// Simple equality filter
    Equality,

    /// Numeric comparison
    NumericComparison,

    /// String operation
    StringOperation,

    /// Regex filter
    Regex,

    /// Boolean logic
    BooleanLogic,

    /// Complex expression
    Complex,
}

/// Specialization applied to the plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Specialization {
    /// Specialization type
    pub spec_type: SpecializationType,

    /// Description
    pub description: String,

    /// Expected speedup factor
    pub speedup_factor: f64,
}

/// Types of specializations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SpecializationType {
    /// Pattern-specific scanning
    PatternScanning,

    /// Join strategy selection
    JoinStrategy,

    /// Filter pushdown
    FilterPushdown,

    /// Index usage
    IndexUsage,

    /// SIMD vectorization
    SimdVectorization,

    /// Parallel execution
    ParallelExecution,
}

/// Query execution statistics
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ExecutionStats {
    /// Number of executions
    pub execution_count: usize,

    /// Total execution time
    pub total_execution_time: Duration,

    /// Average execution time
    pub avg_execution_time: Duration,

    /// Minimum execution time
    pub min_execution_time: Option<Duration>,

    /// Maximum execution time
    pub max_execution_time: Option<Duration>,

    /// Total results produced
    pub total_results: usize,

    /// Average results per execution
    pub avg_results: f64,

    /// Last execution timestamp (excluded from serialization)
    #[serde(skip)]
    pub last_executed: Option<Instant>,

    /// Compilation time
    pub compilation_time: Duration,

    /// Re-optimization count
    pub reoptimization_count: usize,
}

impl ExecutionStats {
    /// Update statistics with a new execution
    pub fn record_execution(&mut self, duration: Duration, result_count: usize) {
        self.execution_count += 1;
        self.total_execution_time += duration;
        self.avg_execution_time = self.total_execution_time / self.execution_count as u32;
        self.total_results += result_count;
        self.avg_results = self.total_results as f64 / self.execution_count as f64;
        self.last_executed = Some(Instant::now());

        self.min_execution_time = Some(
            self.min_execution_time
                .map_or(duration, |min| min.min(duration)),
        );
        self.max_execution_time = Some(
            self.max_execution_time
                .map_or(duration, |max| max.max(duration)),
        );
    }

    /// Check if re-optimization is beneficial
    pub fn should_reoptimize(&self, min_executions: usize) -> bool {
        self.execution_count >= min_executions
            && self.avg_execution_time > Duration::from_millis(100)
    }
}

/// Query compilation metadata
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QueryMetadata {
    /// Query complexity score (0-1000)
    pub complexity: usize,

    /// Estimated memory usage (bytes)
    pub estimated_memory: usize,

    /// Number of triple patterns
    pub triple_pattern_count: usize,

    /// Number of joins
    pub join_count: usize,

    /// Number of filters
    pub filter_count: usize,

    /// Has aggregation
    pub has_aggregation: bool,

    /// Has optional patterns
    pub has_optional: bool,

    /// Has union patterns
    pub has_union: bool,

    /// Optimization opportunities
    pub optimization_opportunities: Vec<String>,
}

/// SPARQL Query JIT Compiler
pub struct QueryJitCompiler {
    /// Compiler configuration
    config: JitCompilerConfig,

    /// Compiled query cache
    query_cache: Arc<DashMap<String, Arc<CompiledQuery>>>,

    /// Metric registry (reserved for future use)
    #[allow(dead_code)]
    metrics: Arc<MetricsRegistry>,

    /// Performance profiler (reserved for future use)
    #[allow(dead_code)]
    profiler: Arc<Profiler>,

    /// Cardinality estimator for accurate query planning
    cardinality_estimator: Arc<CardinalityEstimator>,

    /// Random seed for cache eviction
    _rng_seed: u64,

    /// Compilation statistics
    stats: Arc<RwLock<CompilerStats>>,
}

/// Compiler-wide statistics
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct CompilerStats {
    /// Total compilations
    pub total_compilations: usize,

    /// Total compilation time
    pub total_compilation_time: Duration,

    /// Cache hits
    pub cache_hits: usize,

    /// Cache misses
    pub cache_misses: usize,

    /// Cache evictions
    pub cache_evictions: usize,

    /// Failed compilations
    pub failed_compilations: usize,

    /// Average compilation time
    pub avg_compilation_time: Duration,
}

impl QueryJitCompiler {
    /// Create a new JIT compiler with the given configuration
    pub fn new(config: JitCompilerConfig) -> Result<Self> {
        // Initialize metrics
        let metrics = Arc::new(MetricsRegistry::new());

        // Initialize profiler
        let profiler = Arc::new(Profiler::new());

        // Initialize cardinality estimator with default configuration
        let cardinality_estimator = Arc::new(CardinalityEstimator::new(EstimatorConfig::default()));

        Ok(Self {
            config,
            query_cache: Arc::new(DashMap::new()),
            metrics,
            profiler,
            cardinality_estimator,
            _rng_seed: 42,
            stats: Arc::new(RwLock::new(CompilerStats::default())),
        })
    }

    /// Compile a SPARQL algebra expression
    pub fn compile(&mut self, algebra: &Algebra) -> Result<Arc<CompiledQuery>> {
        let start_time = Instant::now();

        // Generate query ID from algebra
        let query_id = self.generate_query_id(algebra);

        // Check cache first
        if self.config.enable_caching {
            if let Some(cached) = self.query_cache.get(&query_id) {
                self.record_cache_hit();
                debug!("JIT cache hit for query: {}", query_id);
                return Ok(cached.clone());
            }
        }

        self.record_cache_miss();
        info!("Compiling query: {}", query_id);

        // Analyze query to extract metadata
        let metadata = self.analyze_query(algebra)?;

        // Check complexity threshold
        if metadata.complexity > self.config.max_plan_complexity {
            warn!(
                "Query complexity ({}) exceeds threshold ({}), using basic execution",
                metadata.complexity, self.config.max_plan_complexity
            );
        }

        // Generate execution plan
        let plan = self.generate_execution_plan(algebra, &metadata)?;

        // Create compiled query
        let compiled = Arc::new(CompiledQuery {
            id: query_id.clone(),
            algebra: Arc::new(algebra.clone()),
            plan: Arc::new(plan),
            compiled_at: Instant::now(),
            stats: Arc::new(RwLock::new(ExecutionStats {
                compilation_time: start_time.elapsed(),
                ..Default::default()
            })),
            metadata,
        });

        // Update cache
        if self.config.enable_caching {
            self.insert_into_cache(query_id.clone(), compiled.clone())?;
        }

        // Record metrics
        let compilation_time = start_time.elapsed();
        self.record_compilation(compilation_time);

        info!(
            "Query compiled successfully in {:?}: {}",
            compilation_time, query_id
        );

        Ok(compiled)
    }

    /// Generate a unique identifier for the query
    fn generate_query_id(&self, algebra: &Algebra) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        format!("{:?}", algebra).hash(&mut hasher);
        format!("query_{:x}", hasher.finish())
    }

    /// Analyze query to extract compilation metadata
    fn analyze_query(&self, algebra: &Algebra) -> Result<QueryMetadata> {
        let mut metadata = QueryMetadata::default();

        // Recursively analyze algebra structure
        self.analyze_algebra_recursive(algebra, &mut metadata);

        // Calculate complexity score
        metadata.complexity = self.calculate_complexity(&metadata);

        // Estimate memory usage
        metadata.estimated_memory = self.estimate_memory(&metadata);

        // Identify optimization opportunities
        metadata.optimization_opportunities = self.identify_optimizations(&metadata);

        Ok(metadata)
    }

    /// Recursively analyze algebra structure
    #[allow(clippy::only_used_in_recursion)]
    fn analyze_algebra_recursive(&self, algebra: &Algebra, metadata: &mut QueryMetadata) {
        match algebra {
            Algebra::Bgp(patterns) => {
                metadata.triple_pattern_count += patterns.len();
            }
            Algebra::Join { left, right } => {
                metadata.join_count += 1;
                self.analyze_algebra_recursive(left, metadata);
                self.analyze_algebra_recursive(right, metadata);
            }
            Algebra::Filter { pattern, .. } => {
                metadata.filter_count += 1;
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::LeftJoin { left, right, .. } => {
                metadata.has_optional = true;
                metadata.join_count += 1;
                self.analyze_algebra_recursive(left, metadata);
                self.analyze_algebra_recursive(right, metadata);
            }
            Algebra::Union { left, right } => {
                metadata.has_union = true;
                self.analyze_algebra_recursive(left, metadata);
                self.analyze_algebra_recursive(right, metadata);
            }
            Algebra::Group { pattern, .. } => {
                metadata.has_aggregation = true;
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::Project { pattern, .. }
            | Algebra::Distinct { pattern }
            | Algebra::Reduced { pattern }
            | Algebra::OrderBy { pattern, .. } => {
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::Slice {
                pattern,
                offset: _,
                limit: _,
            } => {
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::Graph { graph: _, pattern } => {
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::Extend { pattern, .. } => {
                self.analyze_algebra_recursive(pattern, metadata);
            }
            Algebra::Minus { left, right } => {
                self.analyze_algebra_recursive(left, metadata);
                self.analyze_algebra_recursive(right, metadata);
            }
            _ => {}
        }
    }

    /// Calculate query complexity score
    fn calculate_complexity(&self, metadata: &QueryMetadata) -> usize {
        let mut score = 0;

        // Base complexity from triple patterns
        score += metadata.triple_pattern_count * 10;

        // Join complexity (exponential growth)
        score += metadata.join_count.pow(2) * 20;

        // Filter complexity
        score += metadata.filter_count * 15;

        // Additional complexity for special features
        if metadata.has_aggregation {
            score += 50;
        }
        if metadata.has_optional {
            score += 30;
        }
        if metadata.has_union {
            score += 25;
        }

        score.min(1000) // Cap at 1000
    }

    /// Estimate memory usage for query execution
    fn estimate_memory(&self, metadata: &QueryMetadata) -> usize {
        let base_memory = 1024 * 1024; // 1MB base

        // Memory per triple pattern
        let pattern_memory = metadata.triple_pattern_count * 100 * 1024; // 100KB per pattern

        // Memory per join (increases exponentially)
        let join_memory = if metadata.join_count > 0 {
            2_usize.pow(metadata.join_count as u32) * 50 * 1024 // 50KB * 2^joins
        } else {
            0
        };

        base_memory + pattern_memory + join_memory
    }

    /// Identify optimization opportunities
    fn identify_optimizations(&self, metadata: &QueryMetadata) -> Vec<String> {
        let mut opportunities = Vec::new();

        if metadata.join_count > 2 {
            opportunities.push("Consider join reordering".to_string());
        }

        if metadata.filter_count > 0 {
            opportunities.push("Filter pushdown optimization".to_string());
        }

        if metadata.triple_pattern_count > 5 {
            opportunities.push("Pattern specialization".to_string());
        }

        if metadata.has_aggregation {
            opportunities.push("Streaming aggregation".to_string());
        }

        opportunities
    }

    /// Generate execution plan from algebra
    fn generate_execution_plan(
        &self,
        algebra: &Algebra,
        metadata: &QueryMetadata,
    ) -> Result<ExecutionPlan> {
        let mut operations = Vec::new();
        let mut specializations = Vec::new();

        // Lower algebra to operations
        self.lower_to_operations(algebra, &mut operations)?;

        // Apply optimizations based on configuration
        if self.config.optimization_level >= 1 {
            self.optimize_plan(&mut operations, &mut specializations)?;
        }

        // Calculate estimated cost
        let estimated_cost = self.calculate_plan_cost(&operations);

        Ok(ExecutionPlan {
            operations,
            estimated_cost,
            estimated_memory: metadata.estimated_memory,
            optimization_hints: metadata.optimization_opportunities.clone(),
            specializations,
        })
    }

    /// Lower algebra to executable operations
    #[allow(clippy::ptr_arg)]
    fn lower_to_operations(&self, algebra: &Algebra, ops: &mut Vec<PlanOperation>) -> Result<()> {
        match algebra {
            Algebra::Bgp(patterns) => {
                // Basic graph pattern - scan operation
                let pattern_type = self.determine_pattern_type(algebra);

                // Estimate cardinality using CardinalityEstimator
                let estimated_cardinality = if !patterns.is_empty() {
                    // Use the first pattern for estimation
                    // For multiple patterns, we could sum or average the estimates
                    match self
                        .cardinality_estimator
                        .estimate_triple_pattern(&patterns[0])
                    {
                        Ok(cardinality) => cardinality,
                        Err(e) => {
                            warn!("Cardinality estimation failed: {}, using default", e);
                            10_000 // Fallback to conservative default
                        }
                    }
                } else {
                    10_000 // Default for empty BGP
                };

                debug!(
                    "BGP cardinality estimate: {} for {} patterns",
                    estimated_cardinality,
                    patterns.len()
                );

                ops.push(PlanOperation::ScanTriples {
                    pattern_id: ops.len(),
                    pattern_type,
                    estimated_cardinality: estimated_cardinality.try_into().unwrap_or(10_000),
                });
            }
            Algebra::Join { left, right } => {
                let left_start = ops.len();
                self.lower_to_operations(left, ops)?;

                let right_start = ops.len();
                self.lower_to_operations(right, ops)?;

                // Extract join variables (intersection of left and right variables)
                let left_vars = left.variables();
                let right_vars = right.variables();
                let join_variables: Vec<String> = left_vars
                    .iter()
                    .filter(|v| right_vars.contains(v))
                    .map(|v| v.name().to_string())
                    .collect();

                ops.push(PlanOperation::HashJoin {
                    left_id: left_start,
                    right_id: right_start,
                    join_variables,
                    strategy: JitJoinStrategy::Hash,
                });
            }
            Algebra::Filter { pattern, .. } => {
                self.lower_to_operations(pattern, ops)?;

                ops.push(PlanOperation::Filter {
                    expr_id: ops.len(),
                    filter_type: FilterType::Complex,
                });
            }
            Algebra::Project { pattern, variables } => {
                self.lower_to_operations(pattern, ops)?;

                ops.push(PlanOperation::Project {
                    variables: variables.iter().map(|v| v.name().to_string()).collect(),
                });
            }
            Algebra::Distinct { pattern } => {
                self.lower_to_operations(pattern, ops)?;
                ops.push(PlanOperation::Distinct);
            }
            Algebra::Slice {
                pattern,
                offset,
                limit,
            } => {
                self.lower_to_operations(pattern, ops)?;

                if let Some(off) = offset {
                    ops.push(PlanOperation::Offset { offset: *off });
                }
                if let Some(lim) = limit {
                    ops.push(PlanOperation::Limit { limit: *lim });
                }
            }
            _ => {
                // For other types, use a basic scan operation
                ops.push(PlanOperation::ScanTriples {
                    pattern_id: ops.len(),
                    pattern_type: PatternType::AllVariables,
                    estimated_cardinality: 1000,
                });
            }
        }

        Ok(())
    }

    /// Determine the type of triple pattern for specialization
    fn determine_pattern_type(&self, _algebra: &Algebra) -> PatternType {
        // For now, return AllVariables - would need actual pattern analysis
        PatternType::AllVariables
    }

    /// Optimize the execution plan
    #[allow(clippy::ptr_arg)]
    fn optimize_plan(
        &self,
        operations: &mut Vec<PlanOperation>,
        specializations: &mut Vec<Specialization>,
    ) -> Result<()> {
        // Apply pattern-specific optimizations
        for op in operations.iter_mut() {
            if let PlanOperation::ScanTriples { pattern_type, .. } = op {
                // Record specialization
                specializations.push(Specialization {
                    spec_type: SpecializationType::PatternScanning,
                    description: format!("Specialized scan for pattern type: {:?}", pattern_type),
                    speedup_factor: 1.5,
                });
            }
        }

        Ok(())
    }

    /// Calculate estimated cost of execution plan
    fn calculate_plan_cost(&self, operations: &[PlanOperation]) -> f64 {
        let mut total_cost = 0.0;

        for op in operations {
            total_cost += match op {
                PlanOperation::ScanTriples {
                    estimated_cardinality,
                    ..
                } => *estimated_cardinality as f64 * 0.1,
                PlanOperation::HashJoin { .. } => 100.0,
                PlanOperation::NestedLoopJoin { .. } => 500.0,
                PlanOperation::Filter { .. } => 10.0,
                PlanOperation::Project { .. } => 5.0,
                PlanOperation::Sort { .. } => 200.0,
                PlanOperation::Limit { .. } => 1.0,
                PlanOperation::Offset { .. } => 1.0,
                PlanOperation::Distinct => 150.0,
                PlanOperation::Union { .. } => 50.0,
                PlanOperation::Optional { .. } => 120.0,
            };
        }

        total_cost
    }

    /// Insert compiled query into cache
    fn insert_into_cache(&self, query_id: String, compiled: Arc<CompiledQuery>) -> Result<()> {
        // Check cache size and evict if necessary
        if self.query_cache.len() * 1024 * 1024 > self.config.max_cache_size {
            self.evict_cache_entry()?;
        }

        self.query_cache.insert(query_id, compiled);

        Ok(())
    }

    /// Evict a cache entry (random eviction for now)
    fn evict_cache_entry(&self) -> Result<()> {
        if let Some(key) = self.query_cache.iter().next().map(|e| e.key().clone()) {
            self.query_cache.remove(&key);
            self.stats.write().cache_evictions += 1;
            debug!("Evicted cached query: {}", key);
        }

        Ok(())
    }

    /// Record a cache hit
    fn record_cache_hit(&self) {
        self.stats.write().cache_hits += 1;
    }

    /// Record a cache miss
    fn record_cache_miss(&self) {
        self.stats.write().cache_misses += 1;
    }

    /// Record a compilation
    fn record_compilation(&self, duration: Duration) {
        let mut stats = self.stats.write();
        stats.total_compilations += 1;
        stats.total_compilation_time += duration;
        stats.avg_compilation_time = if stats.total_compilations > 0 {
            stats.total_compilation_time / stats.total_compilations as u32
        } else {
            Duration::ZERO
        };
    }

    /// Get compiler statistics
    pub fn stats(&self) -> CompilerStats {
        self.stats.read().clone()
    }

    /// Clear the query cache
    pub fn clear_cache(&self) {
        self.query_cache.clear();
        info!("JIT query cache cleared");
    }
}

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

    #[test]
    fn test_jit_compiler_creation() {
        let config = JitCompilerConfig::default();
        let compiler = QueryJitCompiler::new(config);
        assert!(compiler.is_ok());
    }

    #[test]
    fn test_complexity_calculation() {
        let compiler = QueryJitCompiler::new(JitCompilerConfig::default()).unwrap();

        let metadata = QueryMetadata {
            triple_pattern_count: 5,
            join_count: 2,
            filter_count: 3,
            has_aggregation: true,
            has_optional: true,
            has_union: false,
            ..Default::default()
        };

        let complexity = compiler.calculate_complexity(&metadata);
        assert!(complexity > 0);
        assert!(complexity <= 1000);
    }

    #[test]
    fn test_memory_estimation() {
        let compiler = QueryJitCompiler::new(JitCompilerConfig::default()).unwrap();

        let metadata = QueryMetadata {
            triple_pattern_count: 3,
            join_count: 2,
            ..Default::default()
        };

        let memory = compiler.estimate_memory(&metadata);
        assert!(memory > 0);
    }

    #[test]
    fn test_cache_operations() {
        let config = JitCompilerConfig {
            enable_caching: true,
            ..Default::default()
        };
        let compiler = QueryJitCompiler::new(config).unwrap();

        // Initially empty
        assert_eq!(compiler.query_cache.len(), 0);

        // Clear should work on empty cache
        compiler.clear_cache();
        assert_eq!(compiler.query_cache.len(), 0);
    }

    #[test]
    fn test_execution_stats() {
        let mut stats = ExecutionStats::default();

        stats.record_execution(Duration::from_millis(100), 50);
        assert_eq!(stats.execution_count, 1);
        assert_eq!(stats.total_results, 50);
        assert_eq!(stats.avg_results, 50.0);

        stats.record_execution(Duration::from_millis(200), 30);
        assert_eq!(stats.execution_count, 2);
        assert_eq!(stats.total_results, 80);
        assert_eq!(stats.avg_results, 40.0);
    }

    #[test]
    fn test_should_reoptimize() {
        let mut stats = ExecutionStats::default();

        // Not enough executions
        assert!(!stats.should_reoptimize(10));

        // Execute many times with slow queries
        for _ in 0..15 {
            stats.record_execution(Duration::from_millis(150), 10);
        }

        assert!(stats.should_reoptimize(10));
    }

    #[test]
    fn test_pattern_type_variants() {
        // Test that all pattern types are defined
        let patterns = [
            PatternType::AllVariables,
            PatternType::SubjectBound,
            PatternType::PredicateBound,
            PatternType::ObjectBound,
            PatternType::SubjectPredicateBound,
            PatternType::SubjectObjectBound,
            PatternType::PredicateObjectBound,
            PatternType::FullyBound,
        ];

        assert_eq!(patterns.len(), 8);
    }

    #[test]
    fn test_join_strategy_variants() {
        let strategies = [
            JitJoinStrategy::Hash,
            JitJoinStrategy::SortMerge,
            JitJoinStrategy::IndexNestedLoop,
            JitJoinStrategy::Bind,
        ];

        assert_eq!(strategies.len(), 4);
    }
}

#[cfg(test)]
mod extended_tests {
    use super::*;
    use std::time::Duration;

    fn default_compiler() -> QueryJitCompiler {
        QueryJitCompiler::new(JitCompilerConfig::default()).unwrap()
    }

    // --- JitCompilerConfig tests ---

    #[test]
    fn test_default_config_has_reasonable_values() {
        let config = JitCompilerConfig::default();
        assert!(config.enabled, "Compiler should be enabled by default");
        assert!(
            config.enable_caching,
            "Caching should be enabled by default"
        );
        assert!(config.max_cache_size > 0, "Cache size should be positive");
        assert!(
            config.optimization_level <= 3,
            "Optimization level should be 0-3"
        );
        assert!(
            config.max_plan_complexity > 0,
            "Max plan complexity should be positive"
        );
    }

    #[test]
    fn test_config_with_disabled_caching() {
        let config = JitCompilerConfig {
            enable_caching: false,
            ..Default::default()
        };
        let compiler = QueryJitCompiler::new(config);
        assert!(
            compiler.is_ok(),
            "Compiler should initialize with caching disabled"
        );
    }

    #[test]
    fn test_config_with_disabled_compiler() {
        let config = JitCompilerConfig {
            enabled: false,
            ..Default::default()
        };
        let compiler = QueryJitCompiler::new(config);
        assert!(
            compiler.is_ok(),
            "Compiler should initialize even when disabled"
        );
    }

    // --- ExecutionStats tests ---

    #[test]
    fn test_execution_stats_initial_state() {
        let stats = ExecutionStats::default();
        assert_eq!(stats.execution_count, 0);
        assert_eq!(stats.total_results, 0);
        assert!(stats.min_execution_time.is_none());
        assert!(stats.max_execution_time.is_none());
    }

    #[test]
    fn test_execution_stats_min_max_tracking() {
        let mut stats = ExecutionStats::default();
        stats.record_execution(Duration::from_millis(50), 10);
        stats.record_execution(Duration::from_millis(200), 20);
        stats.record_execution(Duration::from_millis(100), 15);

        assert_eq!(stats.min_execution_time.unwrap(), Duration::from_millis(50));
        assert_eq!(
            stats.max_execution_time.unwrap(),
            Duration::from_millis(200)
        );
    }

    #[test]
    fn test_execution_stats_average_results() {
        let mut stats = ExecutionStats::default();
        stats.record_execution(Duration::from_millis(10), 10);
        stats.record_execution(Duration::from_millis(10), 20);
        stats.record_execution(Duration::from_millis(10), 30);

        assert!(
            (stats.avg_results - 20.0).abs() < 0.001,
            "Average results should be 20.0"
        );
    }

    #[test]
    fn test_execution_stats_total_time_accumulates() {
        let mut stats = ExecutionStats::default();
        stats.record_execution(Duration::from_millis(100), 5);
        stats.record_execution(Duration::from_millis(200), 5);

        assert_eq!(stats.total_execution_time, Duration::from_millis(300));
    }

    #[test]
    fn test_should_reoptimize_below_min_executions() {
        let mut stats = ExecutionStats::default();
        // Just 5 executions with slow queries - below threshold
        for _ in 0..5 {
            stats.record_execution(Duration::from_millis(200), 10);
        }
        assert!(
            !stats.should_reoptimize(10),
            "Should not reoptimize below min_executions threshold"
        );
    }

    #[test]
    fn test_should_not_reoptimize_fast_queries() {
        let mut stats = ExecutionStats::default();
        // Many executions but all very fast
        for _ in 0..20 {
            stats.record_execution(Duration::from_millis(1), 10);
        }
        assert!(
            !stats.should_reoptimize(10),
            "Fast queries should not trigger reoptimization"
        );
    }

    // --- QueryMetadata tests ---

    #[test]
    fn test_query_metadata_default() {
        let meta = QueryMetadata::default();
        assert_eq!(meta.triple_pattern_count, 0);
        assert!(!meta.has_aggregation);
        assert!(!meta.has_optional);
        assert!(!meta.has_union);
    }

    #[test]
    fn test_query_metadata_with_aggregation() {
        let meta = QueryMetadata {
            has_aggregation: true,
            join_count: 3,
            triple_pattern_count: 4,
            ..Default::default()
        };
        assert!(meta.has_aggregation);
        assert_eq!(meta.join_count, 3);
    }

    // --- CompilerStats tests ---

    #[test]
    fn test_compiler_stats_initial_values() {
        let compiler = default_compiler();
        let stats = compiler.stats();
        assert_eq!(stats.total_compilations, 0);
        assert_eq!(stats.cache_hits, 0);
        assert_eq!(stats.cache_misses, 0);
        assert_eq!(stats.cache_evictions, 0);
    }

    // --- Cache management tests ---

    #[test]
    fn test_clear_cache_makes_it_empty() {
        let compiler = default_compiler();
        // Cache starts empty; clear should still work
        compiler.clear_cache();
        assert_eq!(compiler.query_cache.len(), 0);
    }

    // --- JoinStrategy and FilterType tests ---

    #[test]
    fn test_jit_join_strategy_all_variants() {
        let _: Vec<JitJoinStrategy> = vec![
            JitJoinStrategy::Hash,
            JitJoinStrategy::SortMerge,
            JitJoinStrategy::IndexNestedLoop,
            JitJoinStrategy::Bind,
        ];
    }

    #[test]
    fn test_filter_type_all_variants() {
        let _: Vec<FilterType> = vec![
            FilterType::Equality,
            FilterType::NumericComparison,
            FilterType::StringOperation,
            FilterType::Regex,
            FilterType::BooleanLogic,
            FilterType::Complex,
        ];
    }

    // --- PatternType tests ---

    #[test]
    fn test_pattern_type_all_8_variants_coverage() {
        let variants = [
            PatternType::AllVariables,
            PatternType::SubjectBound,
            PatternType::PredicateBound,
            PatternType::ObjectBound,
            PatternType::SubjectPredicateBound,
            PatternType::SubjectObjectBound,
            PatternType::PredicateObjectBound,
            PatternType::FullyBound,
        ];
        // All variants should be uniquely representable
        assert_eq!(variants.len(), 8);
    }
}