oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
//! SPARQL-star query processing and execution.
//!
//! This module provides support for SPARQL-star queries that can query
//! and manipulate quoted triples using extended SPARQL syntax.

use std::collections::{HashMap, HashSet};

use tracing::{debug, span, Level};

use crate::functions::{Expression, ExpressionEvaluator};
use crate::model::{StarGraph, StarTerm, StarTriple};
use crate::store::StarStore;
use crate::{StarError, StarResult};

/// SPARQL-star query types
#[derive(Debug, Clone, PartialEq)]
pub enum QueryType {
    /// SELECT query
    Select,
    /// CONSTRUCT query
    Construct,
    /// ASK query
    Ask,
    /// DESCRIBE query
    Describe,
}

/// Variable binding for query results
#[derive(Debug, Clone, PartialEq)]
pub struct Binding {
    /// Variable name to term mapping
    bindings: HashMap<String, StarTerm>,
}

impl Binding {
    /// Create a new empty binding
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
        }
    }

    /// Add a variable binding
    pub fn bind(&mut self, variable: &str, term: StarTerm) {
        self.bindings.insert(variable.to_string(), term);
    }

    /// Get the binding for a variable
    pub fn get(&self, variable: &str) -> Option<&StarTerm> {
        self.bindings.get(variable)
    }

    /// Get all variable names
    pub fn variables(&self) -> Vec<&String> {
        self.bindings.keys().collect()
    }

    /// Check if a variable is bound
    pub fn is_bound(&self, variable: &str) -> bool {
        self.bindings.contains_key(variable)
    }

    /// Merge with another binding (fails if conflicting bindings exist)
    pub fn merge(&self, other: &Binding) -> Option<Binding> {
        let mut merged = self.clone();

        for (var, term) in &other.bindings {
            if let Some(existing_term) = merged.bindings.get(var) {
                if existing_term != term {
                    return None; // Conflicting bindings
                }
            } else {
                merged.bindings.insert(var.clone(), term.clone());
            }
        }

        Some(merged)
    }
}

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

/// SPARQL-star basic graph pattern (BGP)
#[derive(Debug, Clone)]
pub struct BasicGraphPattern {
    /// Triple patterns in the BGP
    patterns: Vec<TriplePattern>,
    /// Filter expressions to apply
    filters: Vec<Expression>,
}

/// SPARQL-star triple pattern with support for quoted triple patterns
#[derive(Debug, Clone)]
pub struct TriplePattern {
    /// Subject pattern (can be variable, term, or quoted triple pattern)
    pub subject: TermPattern,
    /// Predicate pattern (can be variable or term)
    pub predicate: TermPattern,
    /// Object pattern (can be variable, term, or quoted triple pattern)
    pub object: TermPattern,
}

/// Term pattern for SPARQL-star queries
#[derive(Debug, Clone)]
pub enum TermPattern {
    /// Concrete term
    Term(StarTerm),
    /// Variable to be bound
    Variable(String),
    /// Quoted triple pattern (SPARQL-star extension)
    QuotedTriplePattern(Box<TriplePattern>),
}

impl TermPattern {
    /// Check if this pattern matches a term with the given binding
    pub fn matches(&self, term: &StarTerm, binding: &Binding) -> bool {
        match self {
            TermPattern::Term(pattern_term) => pattern_term == term,
            TermPattern::Variable(var_name) => {
                if let Some(bound_term) = binding.get(var_name) {
                    bound_term == term
                } else {
                    true // Unbound variable matches anything
                }
            }
            TermPattern::QuotedTriplePattern(pattern) => {
                if let StarTerm::QuotedTriple(quoted_triple) = term {
                    pattern.matches(quoted_triple, binding)
                } else {
                    false
                }
            }
        }
    }

    /// Extract variables from this pattern
    pub fn extract_variables(&self, variables: &mut HashSet<String>) {
        match self {
            TermPattern::Term(_) => {}
            TermPattern::Variable(var) => {
                variables.insert(var.clone());
            }
            TermPattern::QuotedTriplePattern(pattern) => {
                pattern.extract_variables(variables);
            }
        }
    }
}

impl TriplePattern {
    /// Create a new triple pattern
    pub fn new(subject: TermPattern, predicate: TermPattern, object: TermPattern) -> Self {
        Self {
            subject,
            predicate,
            object,
        }
    }

    /// Check if this pattern matches a triple with the given binding
    pub fn matches(&self, triple: &StarTriple, binding: &Binding) -> bool {
        self.subject.matches(&triple.subject, binding)
            && self.predicate.matches(&triple.predicate, binding)
            && self.object.matches(&triple.object, binding)
    }

    /// Try to create a binding from this pattern and a matching triple
    pub fn try_bind(&self, triple: &StarTriple, existing_binding: &Binding) -> Option<Binding> {
        let mut new_binding = existing_binding.clone();

        if !self.bind_term(&self.subject, &triple.subject, &mut new_binding) {
            return None;
        }

        if !self.bind_term(&self.predicate, &triple.predicate, &mut new_binding) {
            return None;
        }

        if !self.bind_term(&self.object, &triple.object, &mut new_binding) {
            return None;
        }

        Some(new_binding)
    }

    /// Try to bind a term pattern to a concrete term
    fn bind_term(&self, pattern: &TermPattern, term: &StarTerm, binding: &mut Binding) -> bool {
        match pattern {
            TermPattern::Term(pattern_term) => pattern_term == term,
            TermPattern::Variable(var_name) => {
                if let Some(existing_term) = binding.get(var_name) {
                    existing_term == term
                } else {
                    binding.bind(var_name, term.clone());
                    true
                }
            }
            TermPattern::QuotedTriplePattern(quoted_pattern) => {
                if let StarTerm::QuotedTriple(quoted_triple) = term {
                    if let Some(new_binding) = quoted_pattern.try_bind(quoted_triple, binding) {
                        // Merge the bindings from the quoted triple pattern
                        for (var, value) in new_binding.bindings.iter() {
                            if !binding.is_bound(var) {
                                binding.bind(var, value.clone());
                            }
                        }
                        true
                    } else {
                        false
                    }
                } else {
                    false
                }
            }
        }
    }

    /// Extract all variables from this pattern
    pub fn extract_variables(&self, variables: &mut HashSet<String>) {
        self.subject.extract_variables(variables);
        self.predicate.extract_variables(variables);
        self.object.extract_variables(variables);
    }
}

impl BasicGraphPattern {
    /// Create a new empty BGP
    pub fn new() -> Self {
        Self {
            patterns: Vec::new(),
            filters: Vec::new(),
        }
    }

    /// Add a triple pattern to the BGP
    pub fn add_pattern(&mut self, pattern: TriplePattern) {
        self.patterns.push(pattern);
    }

    /// Add a filter expression to the BGP
    pub fn add_filter(&mut self, filter: Expression) {
        self.filters.push(filter);
    }

    /// Get all patterns in the BGP
    pub fn patterns(&self) -> &[TriplePattern] {
        &self.patterns
    }

    /// Get all filters in the BGP
    pub fn filters(&self) -> &[Expression] {
        &self.filters
    }

    /// Extract all variables from this BGP
    pub fn extract_variables(&self) -> HashSet<String> {
        let mut variables = HashSet::new();
        for pattern in &self.patterns {
            pattern.extract_variables(&mut variables);
        }
        // Also extract variables from filters
        for filter in &self.filters {
            Self::extract_variables_from_expr(filter, &mut variables);
        }
        variables
    }

    /// Extract variables from an expression
    fn extract_variables_from_expr(expr: &Expression, variables: &mut HashSet<String>) {
        match expr {
            Expression::Variable(var) => {
                variables.insert(var.clone());
            }
            Expression::FunctionCall { args, .. } => {
                for arg in args {
                    Self::extract_variables_from_expr(arg, variables);
                }
            }
            Expression::BinaryOp { left, right, .. } => {
                Self::extract_variables_from_expr(left, variables);
                Self::extract_variables_from_expr(right, variables);
            }
            Expression::UnaryOp { expr, .. } => {
                Self::extract_variables_from_expr(expr, variables);
            }
            Expression::Term(_) => {}
        }
    }
}

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

/// Query optimization strategies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum QueryOptimization {
    /// No optimization (simple left-to-right execution)
    None,
    /// Cost-based optimization using selectivity estimates
    CostBased,
    /// Heuristic-based optimization using pattern analysis
    Heuristic,
}

/// Join strategies for BGP execution
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum JoinStrategy {
    /// Nested loop join (simple but potentially inefficient)
    NestedLoop,
    /// Hash join (good for large result sets)
    Hash,
    /// Index-based join (leverages store indices)
    Index,
}

/// Query execution statistics
#[derive(Debug, Clone, Default)]
pub struct QueryStatistics {
    /// Number of pattern evaluations
    pub pattern_evaluations: usize,
    /// Number of join operations
    pub join_operations: usize,
    /// Total execution time in microseconds
    pub execution_time_us: u64,
    /// Number of intermediate results generated
    pub intermediate_results: usize,
    /// Memory usage estimate in bytes
    pub memory_usage_bytes: usize,
}

/// SPARQL-star query executor with advanced optimization capabilities
pub struct QueryExecutor {
    /// Reference to the RDF-star store
    store: StarStore,
    /// Query optimization strategy
    optimization: QueryOptimization,
    /// Join strategy for BGP execution
    join_strategy: JoinStrategy,
    /// Execution statistics
    statistics: QueryStatistics,
}

impl QueryExecutor {
    /// Create a new query executor with default settings
    pub fn new(store: StarStore) -> Self {
        Self {
            store,
            optimization: QueryOptimization::Heuristic,
            join_strategy: JoinStrategy::Index,
            statistics: QueryStatistics::default(),
        }
    }

    /// Create a new query executor with custom optimization settings
    pub fn with_optimization(
        store: StarStore,
        optimization: QueryOptimization,
        join_strategy: JoinStrategy,
    ) -> Self {
        Self {
            store,
            optimization,
            join_strategy,
            statistics: QueryStatistics::default(),
        }
    }

    /// Get execution statistics
    pub fn statistics(&self) -> &QueryStatistics {
        &self.statistics
    }

    /// Reset execution statistics
    pub fn reset_statistics(&mut self) {
        self.statistics = QueryStatistics::default();
    }

    /// Execute a basic graph pattern against the store with optimization
    pub fn execute_bgp(&mut self, bgp: &BasicGraphPattern) -> StarResult<Vec<Binding>> {
        let span = span!(Level::INFO, "execute_bgp");
        let _enter = span.enter();
        let start_time = std::time::Instant::now();

        if bgp.patterns.is_empty() {
            return Ok(vec![Binding::new()]);
        }

        // Optimize pattern order based on strategy
        let optimized_patterns = self.optimize_pattern_order(bgp)?;

        // Execute patterns using optimized join strategy
        let mut current_bindings =
            self.execute_pattern_optimized(&optimized_patterns[0], &Binding::new())?;
        self.statistics.pattern_evaluations += 1;

        // Join with remaining patterns using selected strategy
        for pattern in &optimized_patterns[1..] {
            current_bindings = self.join_with_pattern(current_bindings, pattern)?;
            self.statistics.pattern_evaluations += 1; // Count each pattern evaluation
            self.statistics.join_operations += 1;
            self.statistics.intermediate_results += current_bindings.len();
        }

        // Apply filters to the bindings
        if !bgp.filters.is_empty() {
            current_bindings = self.apply_filters(current_bindings, &bgp.filters)?;
        }

        self.statistics.execution_time_us += start_time.elapsed().as_micros() as u64;
        debug!(
            "BGP execution produced {} bindings using {} strategy",
            current_bindings.len(),
            format!("{:?}", self.optimization)
        );
        Ok(current_bindings)
    }

    /// Optimize pattern execution order based on selectivity estimates
    fn optimize_pattern_order(&self, bgp: &BasicGraphPattern) -> StarResult<Vec<TriplePattern>> {
        match self.optimization {
            QueryOptimization::None => Ok(bgp.patterns().to_vec()),
            QueryOptimization::Heuristic => self.heuristic_optimization(bgp),
            QueryOptimization::CostBased => self.cost_based_optimization(bgp),
        }
    }

    /// Heuristic-based pattern optimization
    fn heuristic_optimization(&self, bgp: &BasicGraphPattern) -> StarResult<Vec<TriplePattern>> {
        let mut patterns = bgp.patterns().to_vec();

        // Sort patterns by selectivity (quoted triple patterns first, then most specific)
        patterns.sort_by(|a, b| {
            let a_selectivity = self.estimate_pattern_selectivity(a);
            let b_selectivity = self.estimate_pattern_selectivity(b);
            a_selectivity
                .partial_cmp(&b_selectivity)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(patterns)
    }

    /// Cost-based pattern optimization using statistics
    fn cost_based_optimization(&self, bgp: &BasicGraphPattern) -> StarResult<Vec<TriplePattern>> {
        let mut patterns = bgp.patterns().to_vec();

        // Sort by estimated cost (lower cost first)
        patterns.sort_by(|a, b| {
            let a_cost = self.estimate_pattern_cost(a);
            let b_cost = self.estimate_pattern_cost(b);
            a_cost
                .partial_cmp(&b_cost)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(patterns)
    }

    /// Estimate pattern selectivity (lower is more selective)
    fn estimate_pattern_selectivity(&self, pattern: &TriplePattern) -> f64 {
        let mut selectivity = 1.0;

        // Quoted triple patterns are typically more selective
        if matches!(pattern.subject, TermPattern::QuotedTriplePattern(_)) {
            selectivity *= 0.1;
        }
        if matches!(pattern.object, TermPattern::QuotedTriplePattern(_)) {
            selectivity *= 0.1;
        }

        // Concrete terms are more selective than variables
        if matches!(pattern.subject, TermPattern::Term(_)) {
            selectivity *= 0.3;
        }
        if matches!(pattern.predicate, TermPattern::Term(_)) {
            selectivity *= 0.2;
        }
        if matches!(pattern.object, TermPattern::Term(_)) {
            selectivity *= 0.3;
        }

        selectivity
    }

    /// Estimate pattern execution cost
    fn estimate_pattern_cost(&self, pattern: &TriplePattern) -> f64 {
        // Base cost for pattern evaluation
        let mut cost = 100.0;

        // Add cost for quoted triple pattern complexity
        if matches!(pattern.subject, TermPattern::QuotedTriplePattern(_)) {
            cost += 500.0;
        }
        if matches!(pattern.object, TermPattern::QuotedTriplePattern(_)) {
            cost += 500.0;
        }

        // Reduce cost for concrete terms (can use indices)
        if matches!(pattern.subject, TermPattern::Term(_)) {
            cost *= 0.5;
        }
        if matches!(pattern.predicate, TermPattern::Term(_)) {
            cost *= 0.3;
        }
        if matches!(pattern.object, TermPattern::Term(_)) {
            cost *= 0.5;
        }

        cost
    }

    /// Execute a single triple pattern with optimization
    fn execute_pattern_optimized(
        &self,
        pattern: &TriplePattern,
        initial_binding: &Binding,
    ) -> StarResult<Vec<Binding>> {
        // Try to use store's advanced indexing for quoted triple patterns
        if self.can_use_index_optimization(pattern) {
            self.execute_pattern_with_index(pattern, initial_binding)
        } else {
            self.execute_pattern(pattern, initial_binding)
        }
    }

    /// Check if pattern can benefit from index optimization
    fn can_use_index_optimization(&self, pattern: &TriplePattern) -> bool {
        // Use index optimization for patterns with quoted triples or concrete terms
        matches!(
            pattern.subject,
            TermPattern::QuotedTriplePattern(_) | TermPattern::Term(_)
        ) || matches!(
            pattern.predicate,
            TermPattern::QuotedTriplePattern(_) | TermPattern::Term(_)
        ) || matches!(
            pattern.object,
            TermPattern::QuotedTriplePattern(_) | TermPattern::Term(_)
        )
    }

    /// Execute pattern using store indices for better performance
    fn execute_pattern_with_index(
        &self,
        pattern: &TriplePattern,
        initial_binding: &Binding,
    ) -> StarResult<Vec<Binding>> {
        let mut bindings = Vec::new();

        // Extract concrete terms for index lookup
        let subject_term = match &pattern.subject {
            TermPattern::Term(term) => Some(term),
            _ => None,
        };
        let predicate_term = match &pattern.predicate {
            TermPattern::Term(term) => Some(term),
            _ => None,
        };
        let object_term = match &pattern.object {
            TermPattern::Term(term) => Some(term),
            _ => None,
        };

        // Use store's query method for better performance
        let matching_triples =
            self.store
                .query_triples(subject_term, predicate_term, object_term)?;

        for triple in matching_triples.iter() {
            if pattern.matches(triple, initial_binding) {
                if let Some(new_binding) = pattern.try_bind(triple, initial_binding) {
                    bindings.push(new_binding);
                }
            }
        }

        Ok(bindings)
    }

    /// Join current bindings with a new pattern using the selected join strategy
    fn join_with_pattern(
        &self,
        current_bindings: Vec<Binding>,
        pattern: &TriplePattern,
    ) -> StarResult<Vec<Binding>> {
        match self.join_strategy {
            JoinStrategy::NestedLoop => self.nested_loop_join(current_bindings, pattern),
            JoinStrategy::Hash => self.hash_join(current_bindings, pattern),
            JoinStrategy::Index => self.index_join(current_bindings, pattern),
        }
    }

    /// Nested loop join implementation
    fn nested_loop_join(
        &self,
        current_bindings: Vec<Binding>,
        pattern: &TriplePattern,
    ) -> StarResult<Vec<Binding>> {
        let mut new_bindings = Vec::new();

        for binding in &current_bindings {
            let pattern_bindings = self.execute_pattern_optimized(pattern, binding)?;
            new_bindings.extend(pattern_bindings);
        }

        Ok(new_bindings)
    }

    /// Hash join implementation (simplified version)
    fn hash_join(
        &self,
        current_bindings: Vec<Binding>,
        pattern: &TriplePattern,
    ) -> StarResult<Vec<Binding>> {
        // For simplicity, fall back to nested loop join
        // A full implementation would build hash tables on join variables
        self.nested_loop_join(current_bindings, pattern)
    }

    /// Index-aware join implementation
    fn index_join(
        &self,
        current_bindings: Vec<Binding>,
        pattern: &TriplePattern,
    ) -> StarResult<Vec<Binding>> {
        let mut new_bindings = Vec::new();

        // Group bindings by shared variables to optimize index lookups
        let shared_vars = self.find_shared_variables(&current_bindings, pattern);

        if shared_vars.is_empty() {
            // No shared variables, fall back to nested loop
            return self.nested_loop_join(current_bindings, pattern);
        }

        // Use index lookups when possible
        for binding in &current_bindings {
            let pattern_bindings = self.execute_pattern_with_binding_context(pattern, binding)?;
            new_bindings.extend(pattern_bindings);
        }

        Ok(new_bindings)
    }

    /// Find variables shared between current bindings and pattern
    fn find_shared_variables(&self, bindings: &[Binding], pattern: &TriplePattern) -> Vec<String> {
        if bindings.is_empty() {
            return Vec::new();
        }

        let mut pattern_vars = std::collections::HashSet::new();
        pattern.extract_variables(&mut pattern_vars);

        let binding_vars: std::collections::HashSet<String> =
            bindings[0].variables().into_iter().cloned().collect();

        pattern_vars.intersection(&binding_vars).cloned().collect()
    }

    /// Execute pattern with binding context for better optimization
    fn execute_pattern_with_binding_context(
        &self,
        pattern: &TriplePattern,
        binding: &Binding,
    ) -> StarResult<Vec<Binding>> {
        // Try to extract concrete terms from the binding to improve index usage
        let resolved_pattern = self.resolve_pattern_variables(pattern, binding);
        self.execute_pattern_optimized(&resolved_pattern, binding)
    }

    /// Resolve pattern variables that are bound to create more specific patterns
    fn resolve_pattern_variables(
        &self,
        pattern: &TriplePattern,
        binding: &Binding,
    ) -> TriplePattern {
        let subject = match &pattern.subject {
            TermPattern::Variable(var) => {
                if let Some(term) = binding.get(var) {
                    TermPattern::Term(term.clone())
                } else {
                    pattern.subject.clone()
                }
            }
            _ => pattern.subject.clone(),
        };

        let predicate = match &pattern.predicate {
            TermPattern::Variable(var) => {
                if let Some(term) = binding.get(var) {
                    TermPattern::Term(term.clone())
                } else {
                    pattern.predicate.clone()
                }
            }
            _ => pattern.predicate.clone(),
        };

        let object = match &pattern.object {
            TermPattern::Variable(var) => {
                if let Some(term) = binding.get(var) {
                    TermPattern::Term(term.clone())
                } else {
                    pattern.object.clone()
                }
            }
            _ => pattern.object.clone(),
        };

        TriplePattern::new(subject, predicate, object)
    }

    /// Apply filter expressions to bindings
    fn apply_filters(
        &self,
        bindings: Vec<Binding>,
        filters: &[Expression],
    ) -> StarResult<Vec<Binding>> {
        let mut filtered_bindings = Vec::new();

        for binding in bindings {
            let mut passes_all_filters = true;

            for filter in filters {
                // Convert binding to HashMap for expression evaluation
                let binding_map: HashMap<String, StarTerm> = binding.bindings.clone();

                match ExpressionEvaluator::evaluate(filter, &binding_map) {
                    Ok(result) => {
                        // Check if the result is a boolean true
                        if let Some(literal) = result.as_literal() {
                            if let Some(datatype) = &literal.datatype {
                                if datatype.iri == "http://www.w3.org/2001/XMLSchema#boolean" {
                                    if literal.value != "true" {
                                        passes_all_filters = false;
                                        break;
                                    }
                                } else {
                                    // Non-boolean result is considered false
                                    passes_all_filters = false;
                                    break;
                                }
                            } else {
                                // No datatype, check if it's a truthy value
                                if literal.value.is_empty()
                                    || literal.value == "false"
                                    || literal.value == "0"
                                {
                                    passes_all_filters = false;
                                    break;
                                }
                            }
                        } else {
                            // Non-literal results are considered false in filter context
                            passes_all_filters = false;
                            break;
                        }
                    }
                    Err(_) => {
                        // Filter evaluation error means the binding doesn't pass
                        passes_all_filters = false;
                        break;
                    }
                }
            }

            if passes_all_filters {
                filtered_bindings.push(binding);
            }
        }

        Ok(filtered_bindings)
    }

    /// Execute a single triple pattern (legacy method for compatibility)
    fn execute_pattern(
        &self,
        pattern: &TriplePattern,
        initial_binding: &Binding,
    ) -> StarResult<Vec<Binding>> {
        let mut bindings = Vec::new();

        // Get all triples from the store
        let triples = self.store.triples();

        for triple in triples {
            if pattern.matches(&triple, initial_binding) {
                if let Some(new_binding) = pattern.try_bind(&triple, initial_binding) {
                    bindings.push(new_binding);
                }
            }
        }

        Ok(bindings)
    }

    /// Execute a SELECT query with optimization
    pub fn execute_select(
        &mut self,
        bgp: &BasicGraphPattern,
        select_vars: &[String],
    ) -> StarResult<Vec<HashMap<String, StarTerm>>> {
        let span = span!(Level::INFO, "execute_select");
        let _enter = span.enter();

        let bindings = self.execute_bgp(bgp)?;
        let mut results = Vec::new();

        for binding in bindings {
            let mut result = HashMap::new();

            for var in select_vars {
                if let Some(term) = binding.get(var) {
                    result.insert(var.clone(), term.clone());
                }
            }

            if !result.is_empty() {
                results.push(result);
            }
        }

        debug!(
            "SELECT query produced {} results with {} pattern evaluations",
            results.len(),
            self.statistics.pattern_evaluations
        );
        Ok(results)
    }

    /// Execute a CONSTRUCT query with optimization
    pub fn execute_construct(
        &mut self,
        bgp: &BasicGraphPattern,
        construct_patterns: &[TriplePattern],
    ) -> StarResult<StarGraph> {
        let span = span!(Level::INFO, "execute_construct");
        let _enter = span.enter();

        let bindings = self.execute_bgp(bgp)?;
        let mut constructed_graph = StarGraph::new();

        for binding in bindings {
            for pattern in construct_patterns {
                if let Some(triple) = self.instantiate_pattern(pattern, &binding)? {
                    constructed_graph.insert(triple)?;
                }
            }
        }

        debug!(
            "CONSTRUCT query produced {} triples with {} join operations",
            constructed_graph.len(),
            self.statistics.join_operations
        );
        Ok(constructed_graph)
    }

    /// Execute an ASK query with optimization
    pub fn execute_ask(&mut self, bgp: &BasicGraphPattern) -> StarResult<bool> {
        let span = span!(Level::INFO, "execute_ask");
        let _enter = span.enter();

        let bindings = self.execute_bgp(bgp)?;
        let result = !bindings.is_empty();

        debug!(
            "ASK query result: {} (execution time: {}µs)",
            result, self.statistics.execution_time_us
        );
        Ok(result)
    }

    /// Instantiate a triple pattern with a binding
    fn instantiate_pattern(
        &self,
        pattern: &TriplePattern,
        binding: &Binding,
    ) -> StarResult<Option<StarTriple>> {
        let subject = self.instantiate_term_pattern(&pattern.subject, binding)?;
        let predicate = self.instantiate_term_pattern(&pattern.predicate, binding)?;
        let object = self.instantiate_term_pattern(&pattern.object, binding)?;

        if let (Some(s), Some(p), Some(o)) = (subject, predicate, object) {
            Ok(Some(StarTriple::new(s, p, o)))
        } else {
            Ok(None)
        }
    }

    /// Instantiate a term pattern with a binding
    fn instantiate_term_pattern(
        &self,
        pattern: &TermPattern,
        binding: &Binding,
    ) -> StarResult<Option<StarTerm>> {
        match pattern {
            TermPattern::Term(term) => Ok(Some(term.clone())),
            TermPattern::Variable(var) => Ok(binding.get(var).cloned()),
            TermPattern::QuotedTriplePattern(quoted_pattern) => {
                if let Some(triple) = self.instantiate_pattern(quoted_pattern, binding)? {
                    Ok(Some(StarTerm::quoted_triple(triple)))
                } else {
                    Ok(None)
                }
            }
        }
    }
}

/// Simple SPARQL-star query parser (very basic implementation)
pub struct QueryParser;

impl QueryParser {
    /// Parse a simple SELECT query with quoted triple patterns
    pub fn parse_simple_select(query: &str) -> StarResult<(Vec<String>, BasicGraphPattern)> {
        // This is a very simplified parser for demonstration
        // A real implementation would use a proper SPARQL grammar parser

        let lines: Vec<&str> = query.lines().map(|l| l.trim()).collect();
        let mut select_vars = Vec::new();
        let mut bgp = BasicGraphPattern::new();

        let mut in_where = false;

        for line in lines {
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if line.to_uppercase().starts_with("SELECT") {
                // Extract variables
                let parts: Vec<&str> = line.split_whitespace().collect();
                for part in &parts[1..] {
                    if let Some(stripped) = part.strip_prefix('?') {
                        select_vars.push(stripped.to_string());
                    }
                }
            } else if line.to_uppercase().contains("WHERE") {
                in_where = true;
            } else if in_where && line.contains('.') {
                // Parse triple pattern
                if let Ok(pattern) = Self::parse_triple_pattern(line) {
                    bgp.add_pattern(pattern);
                }
            }
        }

        Ok((select_vars, bgp))
    }

    /// Parse a simple triple pattern
    fn parse_triple_pattern(line: &str) -> StarResult<TriplePattern> {
        // Remove trailing dot and split
        let line = line.trim_end_matches('.').trim();
        let parts = Self::tokenize_pattern(line)?;

        if parts.len() != 3 {
            return Err(StarError::query_error(format!(
                "Invalid triple pattern: {line}"
            )));
        }

        let subject = Self::parse_term_pattern(&parts[0])?;
        let predicate = Self::parse_term_pattern(&parts[1])?;
        let object = Self::parse_term_pattern(&parts[2])?;

        Ok(TriplePattern::new(subject, predicate, object))
    }

    /// Tokenize a pattern handling quoted triples
    fn tokenize_pattern(pattern: &str) -> StarResult<Vec<String>> {
        let mut tokens = Vec::new();
        let mut current_token = String::new();
        let mut chars = pattern.chars().peekable();
        let mut depth = 0;
        let mut in_string = false;

        while let Some(ch) = chars.next() {
            match ch {
                '"' => {
                    in_string = !in_string;
                    current_token.push(ch);
                }
                '<' if !in_string && chars.peek() == Some(&'<') => {
                    chars.next(); // consume second '<'
                    depth += 1;
                    current_token.push_str("<<");
                }
                '>' if !in_string && chars.peek() == Some(&'>') => {
                    chars.next(); // consume second '>'
                    depth -= 1;
                    current_token.push_str(">>");
                }
                ' ' | '\t' if !in_string && depth == 0 => {
                    if !current_token.trim().is_empty() {
                        tokens.push(current_token.trim().to_string());
                        current_token.clear();
                    }
                }
                _ => {
                    current_token.push(ch);
                }
            }
        }

        if !current_token.trim().is_empty() {
            tokens.push(current_token.trim().to_string());
        }

        Ok(tokens)
    }

    /// Parse a term pattern
    fn parse_term_pattern(term_str: &str) -> StarResult<TermPattern> {
        let term_str = term_str.trim();

        // Variable
        if let Some(stripped) = term_str.strip_prefix('?') {
            return Ok(TermPattern::Variable(stripped.to_string()));
        }

        // Quoted triple pattern
        if term_str.starts_with("<<") && term_str.ends_with(">>") {
            let inner = &term_str[2..term_str.len() - 2];
            let inner_pattern = Self::parse_triple_pattern(inner)?;
            return Ok(TermPattern::QuotedTriplePattern(Box::new(inner_pattern)));
        }

        // Regular term
        let term = Self::parse_concrete_term(term_str)?;
        Ok(TermPattern::Term(term))
    }

    /// Parse a concrete term (not a variable or pattern)
    fn parse_concrete_term(term_str: &str) -> StarResult<StarTerm> {
        // IRI
        if term_str.starts_with('<') && term_str.ends_with('>') {
            let iri = &term_str[1..term_str.len() - 1];
            return StarTerm::iri(iri);
        }

        // Blank node
        if let Some(id) = term_str.strip_prefix("_:") {
            return StarTerm::blank_node(id);
        }

        // Literal
        if term_str.starts_with('"') {
            // Simple literal parsing (not complete)
            let end_quote = term_str.rfind('"').unwrap_or(term_str.len());
            let value = &term_str[1..end_quote];
            return StarTerm::literal(value);
        }

        Err(StarError::query_error(format!(
            "Cannot parse term: {term_str}"
        )))
    }
}

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

    #[test]
    fn test_binding_operations() {
        let mut binding = Binding::new();
        assert!(!binding.is_bound("x"));

        binding.bind("x", StarTerm::iri("http://example.org/alice").unwrap());
        assert!(binding.is_bound("x"));
        assert_eq!(
            binding.get("x"),
            Some(&StarTerm::iri("http://example.org/alice").unwrap())
        );

        let mut other = Binding::new();
        other.bind("y", StarTerm::literal("test").unwrap());

        let merged = binding.merge(&other).unwrap();
        assert!(merged.is_bound("x"));
        assert!(merged.is_bound("y"));
    }

    #[test]
    fn test_triple_pattern_matching() {
        let pattern = TriplePattern::new(
            TermPattern::Variable("x".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/knows").unwrap()),
            TermPattern::Variable("y".to_string()),
        );

        let triple = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/knows").unwrap(),
            StarTerm::iri("http://example.org/bob").unwrap(),
        );

        let binding = Binding::new();
        assert!(pattern.matches(&triple, &binding));

        let new_binding = pattern.try_bind(&triple, &binding).unwrap();
        assert_eq!(
            new_binding.get("x"),
            Some(&StarTerm::iri("http://example.org/alice").unwrap())
        );
        assert_eq!(
            new_binding.get("y"),
            Some(&StarTerm::iri("http://example.org/bob").unwrap())
        );
    }

    #[test]
    fn test_quoted_triple_pattern() {
        let inner_pattern = TriplePattern::new(
            TermPattern::Variable("x".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
            TermPattern::Variable("age".to_string()),
        );

        let outer_pattern = TriplePattern::new(
            TermPattern::QuotedTriplePattern(Box::new(inner_pattern)),
            TermPattern::Term(StarTerm::iri("http://example.org/certainty").unwrap()),
            TermPattern::Variable("cert".to_string()),
        );

        let inner_triple = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let outer_triple = StarTriple::new(
            StarTerm::quoted_triple(inner_triple),
            StarTerm::iri("http://example.org/certainty").unwrap(),
            StarTerm::literal("0.9").unwrap(),
        );

        let binding = Binding::new();
        assert!(outer_pattern.matches(&outer_triple, &binding));

        let new_binding = outer_pattern.try_bind(&outer_triple, &binding).unwrap();
        assert!(new_binding.is_bound("x"));
        assert!(new_binding.is_bound("age"));
        assert!(new_binding.is_bound("cert"));
    }

    #[test]
    fn test_bgp_execution() {
        let store = StarStore::new();

        // Add some test data
        let triple1 = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/knows").unwrap(),
            StarTerm::iri("http://example.org/bob").unwrap(),
        );

        let triple2 = StarTriple::new(
            StarTerm::iri("http://example.org/bob").unwrap(),
            StarTerm::iri("http://example.org/knows").unwrap(),
            StarTerm::iri("http://example.org/charlie").unwrap(),
        );

        store.insert(&triple1).unwrap();
        store.insert(&triple2).unwrap();

        let mut executor = QueryExecutor::new(store);

        // Create BGP: ?x knows ?y
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("x".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/knows").unwrap()),
            TermPattern::Variable("y".to_string()),
        ));

        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 2);
    }

    #[test]
    fn test_query_optimization_strategies() {
        let store = StarStore::new();

        // Add test data with quoted triples
        let inner_triple = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let quoted_triple = StarTriple::new(
            StarTerm::quoted_triple(inner_triple),
            StarTerm::iri("http://example.org/certainty").unwrap(),
            StarTerm::literal("0.9").unwrap(),
        );

        store.insert(&quoted_triple).unwrap();

        // Test different optimization strategies
        for optimization in [
            QueryOptimization::None,
            QueryOptimization::Heuristic,
            QueryOptimization::CostBased,
        ] {
            for join_strategy in [
                JoinStrategy::NestedLoop,
                JoinStrategy::Index,
                JoinStrategy::Hash,
            ] {
                let mut executor =
                    QueryExecutor::with_optimization(store.clone(), optimization, join_strategy);

                let mut bgp = BasicGraphPattern::new();
                bgp.add_pattern(TriplePattern::new(
                    TermPattern::QuotedTriplePattern(Box::new(TriplePattern::new(
                        TermPattern::Variable("x".to_string()),
                        TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
                        TermPattern::Variable("age".to_string()),
                    ))),
                    TermPattern::Term(StarTerm::iri("http://example.org/certainty").unwrap()),
                    TermPattern::Variable("cert".to_string()),
                ));

                let bindings = executor.execute_bgp(&bgp).unwrap();
                assert_eq!(bindings.len(), 1);

                // Check that statistics are collected
                let stats = executor.statistics();
                assert!(stats.pattern_evaluations > 0);
                // Note: execution_time_us may be 0 on fast systems where execution
                // completes in sub-microsecond time, so we don't assert > 0
            }
        }
    }

    #[test]
    fn test_advanced_sparql_star_queries() {
        let store = StarStore::new();

        // Create a complex RDF-star dataset
        let fact1 = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let fact2 = StarTriple::new(
            StarTerm::iri("http://example.org/bob").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("30").unwrap(),
        );

        let meta1 = StarTriple::new(
            StarTerm::quoted_triple(fact1.clone()),
            StarTerm::iri("http://example.org/source").unwrap(),
            StarTerm::literal("census").unwrap(),
        );

        let meta2 = StarTriple::new(
            StarTerm::quoted_triple(fact2.clone()),
            StarTerm::iri("http://example.org/source").unwrap(),
            StarTerm::literal("survey").unwrap(),
        );

        store.insert(&fact1).unwrap();
        store.insert(&fact2).unwrap();
        store.insert(&meta1).unwrap();
        store.insert(&meta2).unwrap();

        let mut executor = QueryExecutor::with_optimization(
            store,
            QueryOptimization::CostBased,
            JoinStrategy::Index,
        );

        // Query: Find all facts from census source
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::QuotedTriplePattern(Box::new(TriplePattern::new(
                TermPattern::Variable("person".to_string()),
                TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
                TermPattern::Variable("age".to_string()),
            ))),
            TermPattern::Term(StarTerm::iri("http://example.org/source").unwrap()),
            TermPattern::Term(StarTerm::literal("census").unwrap()),
        ));

        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 1);

        // Verify the binding contains Alice's data
        let binding = &bindings[0];
        assert!(binding.is_bound("person"));
        assert!(binding.is_bound("age"));

        if let Some(person_term) = binding.get("person") {
            if let Some(person_node) = person_term.as_named_node() {
                assert!(person_node.iri.contains("alice"));
            }
        }
    }

    #[test]
    fn test_query_statistics_tracking() {
        let store = StarStore::new();

        // Add multiple triples
        for i in 0..10 {
            let triple = StarTriple::new(
                StarTerm::iri(&format!("http://example.org/person{i}")).unwrap(),
                StarTerm::iri("http://example.org/age").unwrap(),
                StarTerm::literal(&format!("{}", 20 + i)).unwrap(),
            );
            store.insert(&triple).unwrap();
        }

        let mut executor = QueryExecutor::new(store);

        // Complex BGP with multiple patterns
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("x".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
            TermPattern::Variable("age".to_string()),
        ));
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("x".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
            TermPattern::Variable("age2".to_string()),
        ));

        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 10);

        // Check statistics
        let stats = executor.statistics();
        assert!(stats.pattern_evaluations >= 2); // At least 2 patterns evaluated
        assert!(stats.join_operations >= 1); // At least 1 join performed
                                             // Note: execution_time_us may be 0 on fast systems (sub-microsecond execution)
        assert!(stats.intermediate_results > 0);
    }

    #[test]
    fn test_query_parser() {
        let query = r#"
            SELECT ?x ?y
            WHERE {
                ?x <http://example.org/knows> ?y .
            }
        "#;

        let (vars, bgp) = QueryParser::parse_simple_select(query).unwrap();
        assert_eq!(vars, vec!["x", "y"]);
        assert_eq!(bgp.patterns().len(), 1);
    }

    #[test]
    fn test_quoted_triple_query_parsing() {
        let query = r#"
            SELECT ?cert
            WHERE {
                << ?x <http://example.org/age> ?age >> <http://example.org/certainty> ?cert .
            }
        "#;

        let (vars, bgp) = QueryParser::parse_simple_select(query).unwrap();
        assert_eq!(vars, vec!["cert"]);
        assert_eq!(bgp.patterns().len(), 1);

        let pattern = &bgp.patterns()[0];
        assert!(matches!(
            pattern.subject,
            TermPattern::QuotedTriplePattern(_)
        ));
    }

    #[test]
    fn test_sparql_star_functions_in_filters() {
        let store = StarStore::new();

        // Add test data with quoted triples
        let fact = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let meta = StarTriple::new(
            StarTerm::quoted_triple(fact.clone()),
            StarTerm::iri("http://example.org/source").unwrap(),
            StarTerm::literal("census").unwrap(),
        );

        store.insert(&fact).unwrap();
        store.insert(&meta).unwrap();

        let mut executor = QueryExecutor::new(store);

        // Query with SPARQL-star function in filter
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("quoted".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/source").unwrap()),
            TermPattern::Variable("source".to_string()),
        ));

        // Add filter: isTRIPLE(?quoted)
        bgp.add_filter(Expression::is_triple(Expression::var("quoted")));

        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 1);

        // Verify the binding contains a quoted triple
        let binding = &bindings[0];
        assert!(binding.is_bound("quoted"));
        if let Some(quoted_term) = binding.get("quoted") {
            assert!(quoted_term.is_quoted_triple());
        }
    }

    #[test]
    fn test_sparql_star_function_composition() {
        let store = StarStore::new();

        // Create nested quoted triple
        let inner = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/knows").unwrap(),
            StarTerm::iri("http://example.org/bob").unwrap(),
        );

        let outer = StarTriple::new(
            StarTerm::quoted_triple(inner.clone()),
            StarTerm::iri("http://example.org/confidence").unwrap(),
            StarTerm::literal("0.8").unwrap(),
        );

        store.insert(&inner).unwrap();
        store.insert(&outer).unwrap();

        let mut executor = QueryExecutor::new(store);

        // Query that uses SUBJECT function
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("statement".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/confidence").unwrap()),
            TermPattern::Variable("conf".to_string()),
        ));

        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 1);

        // Verify we can extract the subject of the quoted triple
        let binding = &bindings[0];
        if let Some(statement) = binding.get("statement") {
            assert!(statement.is_quoted_triple());

            // Test SUBJECT function evaluation
            let subject_result = crate::functions::FunctionEvaluator::evaluate(
                crate::functions::StarFunction::Subject,
                std::slice::from_ref(statement),
            )
            .unwrap();

            // The subject should be the alice IRI
            assert_eq!(
                subject_result,
                StarTerm::iri("http://example.org/alice").unwrap()
            );
        }
    }

    #[test]
    fn test_filter_with_triple_construction() {
        let store = StarStore::new();

        // Add some triples
        let triple1 = StarTriple::new(
            StarTerm::iri("http://example.org/alice").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("25").unwrap(),
        );

        let triple2 = StarTriple::new(
            StarTerm::iri("http://example.org/bob").unwrap(),
            StarTerm::iri("http://example.org/age").unwrap(),
            StarTerm::literal("30").unwrap(),
        );

        store.insert(&triple1).unwrap();
        store.insert(&triple2).unwrap();

        let mut executor = QueryExecutor::new(store);

        // Query that constructs a triple in a filter
        let mut bgp = BasicGraphPattern::new();
        bgp.add_pattern(TriplePattern::new(
            TermPattern::Variable("person".to_string()),
            TermPattern::Term(StarTerm::iri("http://example.org/age").unwrap()),
            TermPattern::Variable("age".to_string()),
        ));

        // This test demonstrates using TRIPLE function in expressions
        // In a real implementation, we might check if a constructed triple exists
        let bindings = executor.execute_bgp(&bgp).unwrap();
        assert_eq!(bindings.len(), 2);
    }
}