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
//! SPARQL EXISTS and NOT EXISTS evaluation.
//!
//! This module implements the EXISTS and NOT EXISTS graph pattern evaluation
//! as defined in SPARQL 1.1 specification (section 18.2.1).
//!
//! # Overview
//!
//! EXISTS checks whether a graph pattern matches at least one solution given
//! the current input bindings. NOT EXISTS is its negation.
//!
//! The evaluator supports:
//! - Triple patterns with variable bindings
//! - AND (join) patterns
//! - OPTIONAL (left outer join) patterns
//! - FILTER patterns
//! - UNION patterns

use std::collections::HashMap;

/// A single RDF triple fact in the dataset.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TripleFact {
    pub subject: String,
    pub predicate: String,
    pub object: String,
}

impl TripleFact {
    pub fn new(
        subject: impl Into<String>,
        predicate: impl Into<String>,
        object: impl Into<String>,
    ) -> Self {
        Self {
            subject: subject.into(),
            predicate: predicate.into(),
            object: object.into(),
        }
    }
}

/// A SPARQL graph pattern.
#[derive(Debug, Clone)]
pub enum GraphPattern {
    /// A single triple pattern (may contain variables starting with `?`)
    Triple(TripleFact),
    /// Conjunction of multiple patterns
    And(Vec<GraphPattern>),
    /// Optional (left outer join) pattern
    Optional(Box<GraphPattern>),
    /// Filter with a condition expression (simplified: just variable=value)
    Filter {
        pattern: Box<GraphPattern>,
        condition: String,
    },
    /// Union of two patterns
    Union(Box<GraphPattern>, Box<GraphPattern>),
}

/// A solution mapping: variable name (without `?`) → bound value.
pub type SolutionMapping = HashMap<String, String>;

/// Evaluates SPARQL EXISTS and NOT EXISTS graph patterns.
#[derive(Debug, Default)]
pub struct ExistsEvaluator;

impl ExistsEvaluator {
    /// Create a new `ExistsEvaluator`.
    pub fn new() -> Self {
        Self
    }

    /// Returns `true` if the pattern has at least one solution given `input` bindings.
    pub fn evaluate_exists(
        &self,
        facts: &[TripleFact],
        pattern: &GraphPattern,
        input: &SolutionMapping,
    ) -> bool {
        let solutions = self.inner_match(facts, pattern, input.clone());
        !solutions.is_empty()
    }

    /// Returns `true` if the pattern has NO solutions given `input` bindings.
    pub fn evaluate_not_exists(
        &self,
        facts: &[TripleFact],
        pattern: &GraphPattern,
        input: &SolutionMapping,
    ) -> bool {
        !self.evaluate_exists(facts, pattern, input)
    }

    /// Recursively match a pattern against facts, extending `bindings`.
    ///
    /// Returns all possible solution mappings that satisfy the pattern.
    pub fn inner_match(
        &self,
        facts: &[TripleFact],
        pattern: &GraphPattern,
        bindings: SolutionMapping,
    ) -> Vec<SolutionMapping> {
        match pattern {
            GraphPattern::Triple(triple) => self.match_triple(facts, triple, bindings),

            GraphPattern::And(patterns) => {
                // Start with the initial bindings as a single solution, then
                // progressively join each pattern.
                let mut current_solutions = vec![bindings];
                for sub_pattern in patterns {
                    let mut next_solutions = Vec::new();
                    for sol in current_solutions {
                        let results = self.inner_match(facts, sub_pattern, sol);
                        next_solutions.extend(results);
                    }
                    current_solutions = next_solutions;
                }
                current_solutions
            }

            GraphPattern::Optional(sub_pattern) => {
                // Left outer join: if sub_pattern has solutions, return them;
                // otherwise return the original bindings unchanged.
                let results = self.inner_match(facts, sub_pattern, bindings.clone());
                if results.is_empty() {
                    vec![bindings]
                } else {
                    results
                }
            }

            GraphPattern::Filter {
                pattern: sub_pattern,
                condition,
            } => {
                let solutions = self.inner_match(facts, sub_pattern, bindings);
                solutions
                    .into_iter()
                    .filter(|sol| self.evaluate_filter_condition(sol, condition))
                    .collect()
            }

            GraphPattern::Union(left, right) => {
                let mut left_solutions = self.inner_match(facts, left, bindings.clone());
                let right_solutions = self.inner_match(facts, right, bindings);
                left_solutions.extend(right_solutions);
                left_solutions
            }
        }
    }

    /// Match a single triple pattern against all facts.
    fn match_triple(
        &self,
        facts: &[TripleFact],
        triple: &TripleFact,
        bindings: SolutionMapping,
    ) -> Vec<SolutionMapping> {
        let mut results = Vec::new();
        for fact in facts {
            if let Some(new_bindings) = self.try_bind_triple(triple, fact, bindings.clone()) {
                results.push(new_bindings);
            }
        }
        results
    }

    /// Try to bind a triple pattern to a concrete fact, extending `bindings`.
    ///
    /// Returns `None` if the triple pattern conflicts with the fact or bindings.
    fn try_bind_triple(
        &self,
        pattern: &TripleFact,
        fact: &TripleFact,
        mut bindings: SolutionMapping,
    ) -> Option<SolutionMapping> {
        // Attempt to match each component
        bindings = self.try_bind_term(&pattern.subject, &fact.subject, bindings)?;
        bindings = self.try_bind_term(&pattern.predicate, &fact.predicate, bindings)?;
        bindings = self.try_bind_term(&pattern.object, &fact.object, bindings)?;
        Some(bindings)
    }

    /// Try to bind a pattern term (variable or constant) to a concrete value.
    fn try_bind_term(
        &self,
        term: &str,
        value: &str,
        mut bindings: SolutionMapping,
    ) -> Option<SolutionMapping> {
        if Self::is_variable(term) {
            let var_name = term.trim_start_matches('?').to_string();
            if let Some(existing) = bindings.get(&var_name) {
                if existing != value {
                    return None; // Conflicting binding
                }
                // Already bound to same value — OK
            } else {
                bindings.insert(var_name, value.to_string());
            }
            Some(bindings)
        } else {
            // Constant: must match exactly
            if term == value {
                Some(bindings)
            } else {
                None
            }
        }
    }

    /// Evaluate a simplified filter condition string.
    ///
    /// Supports formats:
    /// - `?var=value` — variable equals literal
    /// - `?var!=value` — variable not equals literal
    fn evaluate_filter_condition(&self, bindings: &SolutionMapping, condition: &str) -> bool {
        if let Some(pos) = condition.find("!=") {
            let lhs = condition[..pos].trim();
            let rhs = condition[pos + 2..].trim();
            if Self::is_variable(lhs) {
                let var_name = lhs.trim_start_matches('?');
                if let Some(val) = bindings.get(var_name) {
                    return val != rhs;
                }
                // Unbound variable: condition is false (conservative)
                return false;
            }
        } else if let Some(pos) = condition.find('=') {
            let lhs = condition[..pos].trim();
            let rhs = condition[pos + 1..].trim();
            if Self::is_variable(lhs) {
                let var_name = lhs.trim_start_matches('?');
                if let Some(val) = bindings.get(var_name) {
                    return val == rhs;
                }
                return false;
            }
        }
        // Unrecognised condition: pass through
        true
    }

    /// Returns `true` if the term is a SPARQL variable (starts with `?`).
    pub fn is_variable(term: &str) -> bool {
        term.starts_with('?')
    }

    /// Returns `true` if two solution mappings have no conflicting bindings.
    ///
    /// Two mappings are *compatible* if for every variable bound in both, they
    /// agree on the same value.
    pub fn compatible(m1: &SolutionMapping, m2: &SolutionMapping) -> bool {
        for (var, val1) in m1 {
            if let Some(val2) = m2.get(var) {
                if val1 != val2 {
                    return false;
                }
            }
        }
        true
    }

    /// Merge two compatible solution mappings into one.
    ///
    /// Bindings from `m2` are added to `m1`; `m1` values win on conflicts.
    pub fn merge(mut m1: SolutionMapping, m2: SolutionMapping) -> SolutionMapping {
        for (k, v) in m2 {
            m1.entry(k).or_insert(v);
        }
        m1
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    fn evaluator() -> ExistsEvaluator {
        ExistsEvaluator::new()
    }

    fn empty_input() -> SolutionMapping {
        SolutionMapping::new()
    }

    fn facts_abc() -> Vec<TripleFact> {
        vec![
            TripleFact::new(":Alice", "knows", ":Bob"),
            TripleFact::new(":Bob", "knows", ":Carol"),
            TripleFact::new(":Alice", "age", "30"),
            TripleFact::new(":Bob", "age", "25"),
        ]
    }

    // ── is_variable ──────────────────────────────────────────────────────────

    #[test]
    fn test_is_variable_true() {
        assert!(ExistsEvaluator::is_variable("?x"));
    }

    #[test]
    fn test_is_variable_false_constant() {
        assert!(!ExistsEvaluator::is_variable(":Alice"));
    }

    #[test]
    fn test_is_variable_empty_string() {
        assert!(!ExistsEvaluator::is_variable(""));
    }

    #[test]
    fn test_is_variable_question_mark_only() {
        assert!(ExistsEvaluator::is_variable("?"));
    }

    // ── compatible ───────────────────────────────────────────────────────────

    #[test]
    fn test_compatible_empty_maps() {
        let m1 = SolutionMapping::new();
        let m2 = SolutionMapping::new();
        assert!(ExistsEvaluator::compatible(&m1, &m2));
    }

    #[test]
    fn test_compatible_same_bindings() {
        let m1: SolutionMapping = [("x".into(), "1".into())].into();
        let m2: SolutionMapping = [("x".into(), "1".into())].into();
        assert!(ExistsEvaluator::compatible(&m1, &m2));
    }

    #[test]
    fn test_compatible_different_vars() {
        let m1: SolutionMapping = [("x".into(), "1".into())].into();
        let m2: SolutionMapping = [("y".into(), "2".into())].into();
        assert!(ExistsEvaluator::compatible(&m1, &m2));
    }

    #[test]
    fn test_compatible_conflict() {
        let m1: SolutionMapping = [("x".into(), "1".into())].into();
        let m2: SolutionMapping = [("x".into(), "2".into())].into();
        assert!(!ExistsEvaluator::compatible(&m1, &m2));
    }

    // ── merge ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_merge_disjoint() {
        let m1: SolutionMapping = [("x".into(), "1".into())].into();
        let m2: SolutionMapping = [("y".into(), "2".into())].into();
        let merged = ExistsEvaluator::merge(m1, m2);
        assert_eq!(merged.get("x").map(|s| s.as_str()), Some("1"));
        assert_eq!(merged.get("y").map(|s| s.as_str()), Some("2"));
    }

    #[test]
    fn test_merge_m1_wins_on_overlap() {
        let m1: SolutionMapping = [("x".into(), "1".into())].into();
        let m2: SolutionMapping = [("x".into(), "99".into())].into();
        let merged = ExistsEvaluator::merge(m1, m2);
        assert_eq!(merged.get("x").map(|s| s.as_str()), Some("1"));
    }

    // ── EXISTS basic ──────────────────────────────────────────────────────────

    #[test]
    fn test_exists_simple_triple_found() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new(":Alice", "knows", ":Bob"));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_exists_simple_triple_not_found() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new(":Alice", "knows", ":Dave"));
        assert!(!e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_exists_variable_subject() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", ":Bob"));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_exists_all_variables() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "?p", "?o"));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_exists_empty_graph() {
        let e = evaluator();
        let facts: Vec<TripleFact> = vec![];
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "?p", "?o"));
        assert!(!e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_exists_with_pre_bound_input_matching() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", "?y"));
        let mut input = SolutionMapping::new();
        input.insert("x".into(), ":Alice".into());
        assert!(e.evaluate_exists(&facts, &pattern, &input));
    }

    #[test]
    fn test_exists_with_pre_bound_input_not_matching() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", "?y"));
        // Dave doesn't know anyone
        let mut input = SolutionMapping::new();
        input.insert("x".into(), ":Dave".into());
        assert!(!e.evaluate_exists(&facts, &pattern, &input));
    }

    #[test]
    fn test_exists_scoped_binding_not_leaked() {
        // EXISTS evaluation must not affect the outer solution mapping.
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", ":Bob"));
        let input = empty_input();
        e.evaluate_exists(&facts, &pattern, &input);
        // Original input unchanged
        assert!(input.is_empty());
    }

    // ── NOT EXISTS basic ──────────────────────────────────────────────────────

    #[test]
    fn test_not_exists_simple_triple_absent() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new(":Nobody", "knows", ":Bob"));
        assert!(e.evaluate_not_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_not_exists_simple_triple_present() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new(":Alice", "knows", ":Bob"));
        assert!(!e.evaluate_not_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_not_exists_all_variables_nonempty_graph() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "?p", "?o"));
        assert!(!e.evaluate_not_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_not_exists_empty_graph() {
        let e = evaluator();
        let facts: Vec<TripleFact> = vec![];
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "?p", "?o"));
        assert!(e.evaluate_not_exists(&facts, &pattern, &empty_input()));
    }

    // ── AND patterns ──────────────────────────────────────────────────────────

    #[test]
    fn test_and_both_match() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", ":Bob")),
            GraphPattern::Triple(TripleFact::new("?x", "age", "?a")),
        ]);
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_and_second_fails() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", ":Bob")),
            GraphPattern::Triple(TripleFact::new("?x", "flies", "?a")),
        ]);
        assert!(!e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_and_variable_join() {
        let e = evaluator();
        let facts = facts_abc();
        // Find ?x that knows ?y, and ?y also knows someone
        let pattern = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", "?y")),
            GraphPattern::Triple(TripleFact::new("?y", "knows", "?z")),
        ]);
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_and_empty_pattern_list() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::And(vec![]);
        // Empty AND → trivially satisfied (vacuous join)
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    // ── OPTIONAL patterns ─────────────────────────────────────────────────────

    #[test]
    fn test_optional_pattern_present() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Optional(Box::new(GraphPattern::Triple(TripleFact::new(
            ":Alice", "knows", ":Bob",
        ))));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_optional_pattern_absent_still_exists() {
        let e = evaluator();
        let facts = facts_abc();
        // Optional triple that doesn't exist: still returns the empty input binding
        let pattern = GraphPattern::Optional(Box::new(GraphPattern::Triple(TripleFact::new(
            ":Nobody", "knows", ":Bob",
        ))));
        // Should return 1 solution (the empty input)
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 1);
        assert!(solutions[0].is_empty());
    }

    #[test]
    fn test_optional_extends_bindings_when_found() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Optional(Box::new(GraphPattern::Triple(TripleFact::new(
            "?s", "age", "?a",
        ))));
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        // Each fact with predicate "age" should produce a binding
        assert!(solutions.iter().any(|sol| sol.contains_key("a")));
    }

    // ── UNION patterns ────────────────────────────────────────────────────────

    #[test]
    fn test_union_left_matches() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Union(
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Alice", "knows", ":Bob",
            ))),
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Nobody", "knows", ":Bob",
            ))),
        );
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_union_right_matches() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Union(
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Nobody", "knows", ":Bob",
            ))),
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Alice", "knows", ":Bob",
            ))),
        );
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_union_neither_matches() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Union(
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Nobody", "knows", ":Bob",
            ))),
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Noone", "knows", ":Bob",
            ))),
        );
        assert!(!e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_union_both_match_returns_all_solutions() {
        let e = evaluator();
        let facts = facts_abc();
        // Both branches match: should have 2 solutions
        let pattern = GraphPattern::Union(
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Alice", "knows", ":Bob",
            ))),
            Box::new(GraphPattern::Triple(TripleFact::new(
                ":Bob", "knows", ":Carol",
            ))),
        );
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 2);
    }

    // ── FILTER patterns ───────────────────────────────────────────────────────

    #[test]
    fn test_filter_passing() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Filter {
            pattern: Box::new(GraphPattern::Triple(TripleFact::new("?x", "age", "?a"))),
            condition: "?a=30".to_string(),
        };
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 1);
        assert_eq!(solutions[0].get("x").map(|s| s.as_str()), Some(":Alice"));
    }

    #[test]
    fn test_filter_not_equals() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Filter {
            pattern: Box::new(GraphPattern::Triple(TripleFact::new("?x", "age", "?a"))),
            condition: "?a!=30".to_string(),
        };
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 1);
        assert_eq!(solutions[0].get("x").map(|s| s.as_str()), Some(":Bob"));
    }

    #[test]
    fn test_filter_removes_all() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Filter {
            pattern: Box::new(GraphPattern::Triple(TripleFact::new("?x", "age", "?a"))),
            condition: "?a=999".to_string(),
        };
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert!(solutions.is_empty());
    }

    // ── inner_match detailed ──────────────────────────────────────────────────

    #[test]
    fn test_inner_match_returns_multiple_solutions() {
        let e = evaluator();
        let facts = facts_abc();
        // ?x knows ?y: should find 2 solutions
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", "?y"));
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 2);
    }

    #[test]
    fn test_inner_match_pre_bound_narrows_results() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "knows", "?y"));
        let mut input = SolutionMapping::new();
        input.insert("x".into(), ":Alice".into());
        let solutions = e.inner_match(&facts, &pattern, input);
        assert_eq!(solutions.len(), 1);
        assert_eq!(solutions[0].get("y").map(|s| s.as_str()), Some(":Bob"));
    }

    #[test]
    fn test_inner_match_constant_predicate() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "age", "?o"));
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 2);
    }

    #[test]
    fn test_inner_match_no_matching_facts() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "flies", "?o"));
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert!(solutions.is_empty());
    }

    // ── scoped bindings (EXISTS must not leak) ─────────────────────────────────

    #[test]
    fn test_exists_bindings_scoped_does_not_modify_input() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?newvar", "knows", ":Bob"));
        let input = SolutionMapping::new();
        let result = e.evaluate_exists(&facts, &pattern, &input);
        assert!(result);
        // The original `input` is unchanged (Rust ownership ensures this)
        assert!(input.is_empty());
    }

    // ── complex nested patterns ───────────────────────────────────────────────

    #[test]
    fn test_and_within_union() {
        let e = evaluator();
        let facts = facts_abc();
        let knows_bob = GraphPattern::Triple(TripleFact::new("?x", "knows", ":Bob"));
        let bob_old = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", ":Carol")),
            GraphPattern::Triple(TripleFact::new("?x", "age", "?a")),
        ]);
        let pattern = GraphPattern::Union(Box::new(knows_bob), Box::new(bob_old));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_optional_within_and() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", "?y")),
            GraphPattern::Optional(Box::new(GraphPattern::Triple(TripleFact::new(
                "?x", "email", "?e",
            )))),
        ]);
        // Should return solutions (email is optional, won't block)
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert!(!solutions.is_empty());
    }

    #[test]
    fn test_filter_within_and() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::And(vec![
            GraphPattern::Triple(TripleFact::new("?x", "knows", "?y")),
            GraphPattern::Filter {
                pattern: Box::new(GraphPattern::Triple(TripleFact::new("?x", "age", "?a"))),
                condition: "?a=30".to_string(),
            },
        ]);
        let solutions = e.inner_match(&facts, &pattern, empty_input());
        assert_eq!(solutions.len(), 1);
        assert_eq!(solutions[0].get("x").map(|s| s.as_str()), Some(":Alice"));
    }

    #[test]
    fn test_not_exists_with_bound_variable() {
        let e = evaluator();
        let facts = facts_abc();
        let pattern = GraphPattern::Triple(TripleFact::new("?x", "flies", "?z"));
        let mut input = SolutionMapping::new();
        input.insert("x".into(), ":Alice".into());
        assert!(e.evaluate_not_exists(&facts, &pattern, &input));
    }

    #[test]
    fn test_triple_fact_new_constructor() {
        let t = TripleFact::new(":s", ":p", ":o");
        assert_eq!(t.subject, ":s");
        assert_eq!(t.predicate, ":p");
        assert_eq!(t.object, ":o");
    }

    #[test]
    fn test_merge_empty_maps() {
        let m1 = SolutionMapping::new();
        let m2 = SolutionMapping::new();
        let merged = ExistsEvaluator::merge(m1, m2);
        assert!(merged.is_empty());
    }

    #[test]
    fn test_exists_single_fact_dataset_match() {
        let e = evaluator();
        let facts = vec![TripleFact::new("ex:Alice", "foaf:knows", "ex:Bob")];
        let pattern = GraphPattern::Triple(TripleFact::new("?s", "foaf:knows", "?o"));
        assert!(e.evaluate_exists(&facts, &pattern, &empty_input()));
    }

    #[test]
    fn test_not_exists_on_empty_dataset() {
        let e = evaluator();
        let pattern =
            GraphPattern::And(vec![GraphPattern::Triple(TripleFact::new("?x", "p", "?y"))]);
        assert!(e.evaluate_not_exists(&[], &pattern, &empty_input()));
    }
}