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
//! SPARQL MINUS pattern evaluation (set-difference algebra).
//!
//! Implements the SPARQL 1.1 MINUS operator as specified in:
//! <https://www.w3.org/TR/sparql11-query/#neg-minus>
//!
//! The MINUS operator removes solution mappings from the left-hand side
//! that are *compatible* with at least one solution mapping in the right-hand side.
//! Two solution mappings are compatible when they agree on every variable
//! they share in common.  If the two mappings share no variables, they are
//! *not* compatible, so the left-hand row is **kept**.

use std::collections::HashMap;

/// A single solution mapping: variable name → RDF term string.
pub type Binding = HashMap<String, String>;

/// An ordered collection of solution mappings.
#[derive(Debug, Clone, Default)]
pub struct BindingSet {
    rows: Vec<Binding>,
}

impl BindingSet {
    /// Create an empty `BindingSet`.
    pub fn new() -> Self {
        Self { rows: Vec::new() }
    }

    /// Append a binding row.
    pub fn push(&mut self, b: Binding) {
        self.rows.push(b);
    }

    /// Number of rows.
    pub fn len(&self) -> usize {
        self.rows.len()
    }

    /// True when there are no rows.
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Iterate over rows.
    pub fn iter(&self) -> impl Iterator<Item = &Binding> {
        self.rows.iter()
    }
}

/// Statistics produced alongside a MINUS evaluation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MinusStats {
    /// Number of rows in the left-hand operand.
    pub lhs_count: usize,
    /// Number of rows in the right-hand operand.
    pub rhs_count: usize,
    /// Number of rows that passed through (were *not* eliminated).
    pub result_count: usize,
    /// Number of rows that were eliminated.
    pub eliminated_count: usize,
}

/// Evaluates the SPARQL MINUS algebra operator.
///
/// ```
/// use oxirs_arq::minus_evaluator::{Binding, BindingSet, MinusEvaluator};
///
/// let mut lhs = BindingSet::new();
/// let mut row = Binding::new();
/// row.insert("x".to_string(), "<:a>".to_string());
/// lhs.push(row);
///
/// let rhs = BindingSet::new(); // empty RHS → nothing eliminated
/// let result = MinusEvaluator::new().evaluate(&lhs, &rhs);
/// assert_eq!(result.len(), 1);
/// ```
#[derive(Debug, Clone, Default)]
pub struct MinusEvaluator;

impl MinusEvaluator {
    /// Create a new evaluator (stateless).
    pub fn new() -> Self {
        Self
    }

    /// Return the variable names present in both `a` and `b`.
    pub fn shared_vars(a: &Binding, b: &Binding) -> Vec<String> {
        a.keys().filter(|k| b.contains_key(*k)).cloned().collect()
    }

    /// Return `true` when `a` and `b` share at least one variable **and**
    /// all shared variables have identical values.
    ///
    /// Per the SPARQL 1.1 spec (§18.5):
    /// - If the two mappings have *no* shared domain, they are **not** compatible
    ///   for the purpose of MINUS elimination, so `compatible` returns `false`.
    pub fn compatible(a: &Binding, b: &Binding) -> bool {
        let shared = Self::shared_vars(a, b);
        if shared.is_empty() {
            // No shared variables → row is NOT eliminated by this RHS row.
            return false;
        }
        // All shared variables must agree.
        shared.iter().all(|var| a.get(var) == b.get(var))
    }

    /// Evaluate MINUS: keep every row in `lhs` that is *not* compatible with
    /// any row in `rhs`.
    pub fn evaluate(&self, lhs: &BindingSet, rhs: &BindingSet) -> BindingSet {
        let mut result = BindingSet::new();
        for left_row in lhs.iter() {
            let eliminated = rhs
                .iter()
                .any(|right_row| Self::compatible(left_row, right_row));
            if !eliminated {
                result.push(left_row.clone());
            }
        }
        result
    }

    /// Like `evaluate`, but also returns statistics about the operation.
    pub fn evaluate_with_stats(
        &self,
        lhs: &BindingSet,
        rhs: &BindingSet,
    ) -> (BindingSet, MinusStats) {
        let lhs_count = lhs.len();
        let rhs_count = rhs.len();

        let result = self.evaluate(lhs, rhs);
        let result_count = result.len();
        let eliminated_count = lhs_count.saturating_sub(result_count);

        let stats = MinusStats {
            lhs_count,
            rhs_count,
            result_count,
            eliminated_count,
        };
        (result, stats)
    }
}

// ─── tests ────────────────────────────────────────────────────────────────────

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

    fn make_binding(pairs: &[(&str, &str)]) -> Binding {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn binding_set(rows: Vec<Binding>) -> BindingSet {
        let mut bs = BindingSet::new();
        for r in rows {
            bs.push(r);
        }
        bs
    }

    // ── BindingSet basic API ──────────────────────────────────────────────────

    #[test]
    fn test_binding_set_new_is_empty() {
        let bs = BindingSet::new();
        assert!(bs.is_empty());
        assert_eq!(bs.len(), 0);
    }

    #[test]
    fn test_binding_set_push_and_len() {
        let mut bs = BindingSet::new();
        bs.push(make_binding(&[("x", ":a")]));
        assert_eq!(bs.len(), 1);
        assert!(!bs.is_empty());
    }

    #[test]
    fn test_binding_set_iter() {
        let mut bs = BindingSet::new();
        bs.push(make_binding(&[("x", ":a")]));
        bs.push(make_binding(&[("x", ":b")]));
        let vals: Vec<&str> = bs.iter().map(|r| r["x"].as_str()).collect();
        assert_eq!(vals, vec![":a", ":b"]);
    }

    #[test]
    fn test_binding_set_default() {
        let bs = BindingSet::default();
        assert!(bs.is_empty());
    }

    // ── shared_vars ──────────────────────────────────────────────────────────

    #[test]
    fn test_shared_vars_none() {
        let a = make_binding(&[("x", ":a")]);
        let b = make_binding(&[("y", ":b")]);
        let mut sv = MinusEvaluator::shared_vars(&a, &b);
        sv.sort();
        assert!(sv.is_empty());
    }

    #[test]
    fn test_shared_vars_one() {
        let a = make_binding(&[("x", ":a"), ("y", ":c")]);
        let b = make_binding(&[("x", ":a"), ("z", ":d")]);
        let mut sv = MinusEvaluator::shared_vars(&a, &b);
        sv.sort();
        assert_eq!(sv, vec!["x"]);
    }

    #[test]
    fn test_shared_vars_all() {
        let a = make_binding(&[("x", ":a"), ("y", ":b")]);
        let b = make_binding(&[("x", ":a"), ("y", ":b")]);
        let mut sv = MinusEvaluator::shared_vars(&a, &b);
        sv.sort();
        assert_eq!(sv, vec!["x", "y"]);
    }

    #[test]
    fn test_shared_vars_empty_bindings() {
        let a = Binding::new();
        let b = make_binding(&[("x", ":a")]);
        assert!(MinusEvaluator::shared_vars(&a, &b).is_empty());
    }

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

    #[test]
    fn test_compatible_no_shared_vars_not_compatible() {
        let a = make_binding(&[("x", ":a")]);
        let b = make_binding(&[("y", ":b")]);
        assert!(!MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_shared_agree() {
        let a = make_binding(&[("x", ":a"), ("y", ":c")]);
        let b = make_binding(&[("x", ":a"), ("z", ":d")]);
        assert!(MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_shared_disagree() {
        let a = make_binding(&[("x", ":a")]);
        let b = make_binding(&[("x", ":b")]);
        assert!(!MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_multiple_shared_all_agree() {
        let a = make_binding(&[("x", ":a"), ("y", ":b")]);
        let b = make_binding(&[("x", ":a"), ("y", ":b")]);
        assert!(MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_multiple_shared_one_disagrees() {
        let a = make_binding(&[("x", ":a"), ("y", ":b")]);
        let b = make_binding(&[("x", ":a"), ("y", ":DIFFERENT")]);
        assert!(!MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_empty_both() {
        let a = Binding::new();
        let b = Binding::new();
        assert!(!MinusEvaluator::compatible(&a, &b));
    }

    // ── evaluate: basic cases ─────────────────────────────────────────────────

    #[test]
    fn test_evaluate_empty_lhs_empty_rhs() {
        let eval = MinusEvaluator::new();
        let lhs = BindingSet::new();
        let rhs = BindingSet::new();
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_nonempty_lhs_empty_rhs() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let rhs = BindingSet::new();
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_empty_lhs_nonempty_rhs() {
        let eval = MinusEvaluator::new();
        let lhs = BindingSet::new();
        let rhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_no_shared_vars_keeps_all() {
        // LHS and RHS have no variables in common → nothing eliminated.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let rhs = binding_set(vec![make_binding(&[("y", ":a")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_evaluate_all_eliminated() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let rhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_partial_overlap() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":c")]),
        ]);
        let rhs = binding_set(vec![make_binding(&[("x", ":b")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 2);
        let values: Vec<&str> = result.iter().map(|r| r["x"].as_str()).collect();
        assert!(!values.contains(&":b"));
    }

    #[test]
    fn test_evaluate_shared_var_disagrees_kept() {
        // Shared var x has different value → not compatible → kept.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":b")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_multiple_vars_partial_match() {
        // Shares x, but values differ → keep.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":1")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":2")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_extra_vars_in_lhs_do_not_affect() {
        // LHS has extra variable; shared var matches → eliminate.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a"), ("extra", ":e")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_extra_vars_in_rhs_do_not_affect() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a"), ("extra", ":e")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_large_rhs_no_match_keeps_lhs() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":target")])]);
        let rhs_rows: Vec<Binding> = (0..50)
            .map(|i| make_binding(&[("x", &format!(":item{}", i))]))
            .collect();
        let rhs = binding_set(rhs_rows);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_large_rhs_one_match_eliminates() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":item25")])]);
        let rhs_rows: Vec<Binding> = (0..50)
            .map(|i| make_binding(&[("x", &format!(":item{}", i))]))
            .collect();
        let rhs = binding_set(rhs_rows);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_order_preserved() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":c")]),
            make_binding(&[("x", ":d")]),
        ]);
        let rhs = binding_set(vec![make_binding(&[("x", ":b")])]);
        let result = eval.evaluate(&lhs, &rhs);
        let values: Vec<&str> = result.iter().map(|r| r["x"].as_str()).collect();
        assert_eq!(values, vec![":a", ":c", ":d"]);
    }

    #[test]
    fn test_evaluate_multiple_rhs_rows_eliminate_multiple() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":c")]),
        ]);
        let rhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":c")]),
        ]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
        assert_eq!(result.iter().next().map(|r| r["x"].as_str()), Some(":b"));
    }

    // ── evaluate_with_stats ──────────────────────────────────────────────────

    #[test]
    fn test_stats_empty_operands() {
        let eval = MinusEvaluator::new();
        let (result, stats) = eval.evaluate_with_stats(&BindingSet::new(), &BindingSet::new());
        assert_eq!(stats.lhs_count, 0);
        assert_eq!(stats.rhs_count, 0);
        assert_eq!(stats.result_count, 0);
        assert_eq!(stats.eliminated_count, 0);
        assert!(result.is_empty());
    }

    #[test]
    fn test_stats_nothing_eliminated() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let rhs = binding_set(vec![make_binding(&[("y", ":z")])]);
        let (result, stats) = eval.evaluate_with_stats(&lhs, &rhs);
        assert_eq!(stats.lhs_count, 2);
        assert_eq!(stats.rhs_count, 1);
        assert_eq!(stats.result_count, 2);
        assert_eq!(stats.eliminated_count, 0);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_stats_all_eliminated() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let rhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
        ]);
        let (result, stats) = eval.evaluate_with_stats(&lhs, &rhs);
        assert_eq!(stats.lhs_count, 2);
        assert_eq!(stats.rhs_count, 2);
        assert_eq!(stats.result_count, 0);
        assert_eq!(stats.eliminated_count, 2);
        assert!(result.is_empty());
    }

    #[test]
    fn test_stats_partial() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":c")]),
        ]);
        let rhs = binding_set(vec![make_binding(&[("x", ":b")])]);
        let (_result, stats) = eval.evaluate_with_stats(&lhs, &rhs);
        assert_eq!(stats.lhs_count, 3);
        assert_eq!(stats.rhs_count, 1);
        assert_eq!(stats.result_count, 2);
        assert_eq!(stats.eliminated_count, 1);
    }

    #[test]
    fn test_stats_count_consistency() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("x", ":a")]),
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":c")]),
            make_binding(&[("x", ":d")]),
        ]);
        let rhs = binding_set(vec![
            make_binding(&[("x", ":b")]),
            make_binding(&[("x", ":d")]),
        ]);
        let (result, stats) = eval.evaluate_with_stats(&lhs, &rhs);
        assert_eq!(stats.result_count + stats.eliminated_count, stats.lhs_count);
        assert_eq!(result.len(), stats.result_count);
    }

    // ── edge cases ──────────────────────────────────────────────────────────

    #[test]
    fn test_evaluate_lhs_with_empty_binding() {
        // An empty binding has no variables → not compatible with anything.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![Binding::new()]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_rhs_with_empty_binding() {
        // Empty RHS binding has no variables → not compatible with anything in LHS.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let rhs = binding_set(vec![Binding::new()]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_both_empty_bindings() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![Binding::new()]);
        let rhs = binding_set(vec![Binding::new()]);
        // Empty ∩ Empty = empty shared vars → not compatible → keep.
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_evaluate_three_vars_two_shared_all_agree() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":b"), ("z", ":c")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":b")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_three_vars_two_shared_one_disagrees() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":b"), ("z", ":c")])]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a"), ("y", ":DIFF")])]);
        let result = eval.evaluate(&lhs, &rhs);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_minus_evaluator_default() {
        let eval = MinusEvaluator;
        let lhs = BindingSet::new();
        let rhs = BindingSet::new();
        assert!(eval.evaluate(&lhs, &rhs).is_empty());
    }

    #[test]
    fn test_evaluate_duplicate_lhs_rows() {
        let eval = MinusEvaluator::new();
        let row = make_binding(&[("x", ":a")]);
        let lhs = binding_set(vec![row.clone(), row.clone()]);
        let rhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        // Both duplicate rows should be eliminated.
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_evaluate_rhs_eliminates_via_any_row() {
        // RHS has two rows; only the second matches.
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":match")])]);
        let rhs = binding_set(vec![
            make_binding(&[("x", ":no_match")]),
            make_binding(&[("x", ":match")]),
        ]);
        let result = eval.evaluate(&lhs, &rhs);
        assert!(result.is_empty());
    }

    #[test]
    fn test_shared_vars_large_overlap() {
        let a: Binding = (0..20)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        let b: Binding = (10..30)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        let mut sv = MinusEvaluator::shared_vars(&a, &b);
        sv.sort();
        assert_eq!(sv.len(), 10);
        for i in 10..20usize {
            assert!(sv.contains(&format!("v{}", i)));
        }
    }

    #[test]
    fn test_compatible_large_shared_all_agree() {
        let a: Binding = (0..20)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        let b: Binding = (0..20)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        assert!(MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_compatible_large_shared_one_disagrees() {
        let mut a: Binding = (0..20)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        let b: Binding = (0..20)
            .map(|i| (format!("v{}", i), format!(":val{}", i)))
            .collect();
        a.insert("v5".to_string(), ":different".to_string());
        assert!(!MinusEvaluator::compatible(&a, &b));
    }

    #[test]
    fn test_stats_rhs_count_zero() {
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![make_binding(&[("x", ":a")])]);
        let (_, stats) = eval.evaluate_with_stats(&lhs, &BindingSet::new());
        assert_eq!(stats.rhs_count, 0);
        assert_eq!(stats.eliminated_count, 0);
    }

    #[test]
    fn test_binding_set_push_multiple() {
        let mut bs = BindingSet::new();
        for i in 0..10 {
            bs.push(make_binding(&[("x", &format!(":v{}", i))]));
        }
        assert_eq!(bs.len(), 10);
    }

    #[test]
    fn test_evaluate_complex_multi_var_scenario() {
        // Simulates a typical SPARQL MINUS scenario:
        //   LHS: SELECT ?person ?age → 4 people
        //   RHS: SELECT ?person → 2 people to exclude
        let eval = MinusEvaluator::new();
        let lhs = binding_set(vec![
            make_binding(&[("person", ":alice"), ("age", "30")]),
            make_binding(&[("person", ":bob"), ("age", "25")]),
            make_binding(&[("person", ":carol"), ("age", "35")]),
            make_binding(&[("person", ":dave"), ("age", "28")]),
        ]);
        let rhs = binding_set(vec![
            make_binding(&[("person", ":bob")]),
            make_binding(&[("person", ":dave")]),
        ]);
        let (result, stats) = eval.evaluate_with_stats(&lhs, &rhs);
        assert_eq!(stats.result_count, 2);
        assert_eq!(stats.eliminated_count, 2);
        let persons: Vec<&str> = result.iter().map(|r| r["person"].as_str()).collect();
        assert!(persons.contains(&":alice"));
        assert!(persons.contains(&":carol"));
    }
}