oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! CONSTRUCT Query Conformance Tests
//!
//! Tests SPARQL 1.1 CONSTRUCT queries including:
//! - Template with variables
//! - CONSTRUCT WHERE shorthand
//! - Blank node handling in CONSTRUCT
//! - Graph transformation patterns

use super::framework::*;
use super::helpers::*;
use crate::executor::InMemoryDataset;

fn runner() -> ConformanceTestRunner {
    ConformanceTestRunner::new()
}

// ===== HELPER: Build a construct-style algebra =====
// CONSTRUCT queries in our algebra are modeled as SELECT queries that
// return the triple templates as solutions. The ConstructGraph result
// verifies triple count.

/// Build a dataset with assorted triples for CONSTRUCT tests
fn construct_dataset() -> InMemoryDataset {
    dataset_from_triples(vec![
        // People
        (ex("alice"), rdf_type(), iri(&format!("{FOAF}Person"))),
        (ex("alice"), foaf("name"), str_lit("Alice")),
        (ex("alice"), foaf("age"), int_lit(30)),
        (ex("alice"), foaf("mbox"), str_lit("alice@example.org")),
        (ex("bob"), rdf_type(), iri(&format!("{FOAF}Person"))),
        (ex("bob"), foaf("name"), str_lit("Bob")),
        (ex("bob"), foaf("age"), int_lit(25)),
        (ex("charlie"), rdf_type(), iri(&format!("{FOAF}Person"))),
        (ex("charlie"), foaf("name"), str_lit("Charlie")),
        (ex("charlie"), foaf("age"), int_lit(35)),
        // Relationships
        (ex("alice"), foaf("knows"), ex("bob")),
        (ex("alice"), foaf("knows"), ex("charlie")),
        (ex("bob"), foaf("knows"), ex("charlie")),
        // Projects
        (ex("proj1"), ex("name"), str_lit("Project Alpha")),
        (ex("proj1"), ex("lead"), ex("alice")),
        (ex("proj2"), ex("name"), str_lit("Project Beta")),
        (ex("proj2"), ex("lead"), ex("bob")),
    ])
}

/// Dataset for CONSTRUCT WHERE shorthand tests
fn small_dataset() -> InMemoryDataset {
    dataset_from_triples(vec![
        (ex("s1"), ex("p"), str_lit("value1")),
        (ex("s2"), ex("p"), str_lit("value2")),
        (ex("s3"), ex("p"), str_lit("value3")),
    ])
}

// ===== BASIC CONSTRUCT TESTS =====

#[test]
fn test_construct_01_basic_template() {
    // CONSTRUCT { ?s a foaf:Person } WHERE { ?s a foaf:Person }
    // Returns triples matching the pattern
    let ds = construct_dataset();
    let algebra = project(
        bgp(vec![triple(
            var("s"),
            rdf_type(),
            iri(&format!("{FOAF}Person")),
        )]),
        vec![variable("s")],
    );
    let test = ConformanceTest::new(
        "construct-01",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // Alice, Bob, Charlie = 3 triples
        ConformanceResult::ConstructGraph(vec![
            (
                "ex:alice".to_string(),
                "rdf:type".to_string(),
                "foaf:Person".to_string(),
            ),
            (
                "ex:bob".to_string(),
                "rdf:type".to_string(),
                "foaf:Person".to_string(),
            ),
            (
                "ex:charlie".to_string(),
                "rdf:type".to_string(),
                "foaf:Person".to_string(),
            ),
        ]),
    );
    runner().run_test(&test).expect("construct-01 failed");
}

#[test]
fn test_construct_02_template_with_name() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { ?s foaf:name ?n }
    let ds = construct_dataset();
    let algebra = project(
        bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-02",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie have foaf:name; proj1/proj2 use ex:name (different predicate) = 3
        ConformanceResult::ConstructGraph(vec![
            (
                "ex:alice".to_string(),
                "foaf:name".to_string(),
                "Alice".to_string(),
            ),
            (
                "ex:bob".to_string(),
                "foaf:name".to_string(),
                "Bob".to_string(),
            ),
            (
                "ex:charlie".to_string(),
                "foaf:name".to_string(),
                "Charlie".to_string(),
            ),
        ]),
    );
    runner().run_test(&test).expect("construct-02 failed");
}

#[test]
fn test_construct_03_where_shorthand() {
    // CONSTRUCT WHERE { ?s ex:p ?o }
    // WHERE shorthand: template == WHERE pattern
    let ds = small_dataset();
    let algebra = project(
        bgp(vec![triple(var("s"), ex("p"), var("o"))]),
        vec![variable("s"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-03",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            (
                "ex:s1".to_string(),
                "ex:p".to_string(),
                "value1".to_string(),
            ),
            (
                "ex:s2".to_string(),
                "ex:p".to_string(),
                "value2".to_string(),
            ),
            (
                "ex:s3".to_string(),
                "ex:p".to_string(),
                "value3".to_string(),
            ),
        ]),
    );
    runner().run_test(&test).expect("construct-03 failed");
}

#[test]
fn test_construct_04_with_filter() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { ?s foaf:name ?n ; foaf:age ?age FILTER(?age > 28) }
    let ds = construct_dataset();
    let algebra = project(
        filter(
            bgp(vec![
                triple(var("s"), foaf("name"), var("n")),
                triple(var("s"), iri(&format!("{FOAF}age")), var("age")),
            ]),
            expr_gt(
                expr_var("age"),
                Expression::Literal(Literal {
                    value: "28".to_string(),
                    language: None,
                    datatype: Some(oxirs_core::model::NamedNode::new_unchecked(
                        "http://www.w3.org/2001/XMLSchema#integer",
                    )),
                }),
            ),
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-04",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice (30), charlie (35) — not bob (25)
        ConformanceResult::ConstructGraph(vec![
            (
                "ex:alice".to_string(),
                "foaf:name".to_string(),
                "Alice".to_string(),
            ),
            (
                "ex:charlie".to_string(),
                "foaf:name".to_string(),
                "Charlie".to_string(),
            ),
        ]),
    );
    runner().run_test(&test).expect("construct-04 failed");
}

#[test]
fn test_construct_05_empty_result() {
    // CONSTRUCT with WHERE clause matching nothing
    let ds = small_dataset();
    let algebra = project(
        filter(
            bgp(vec![triple(var("s"), ex("p"), var("o"))]),
            expr_eq(expr_var("o"), expr_lit(lit_str("nonexistent"))),
        ),
        vec![variable("s"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-05",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![]),
    );
    runner().run_test(&test).expect("construct-05 failed");
}

#[test]
fn test_construct_06_with_union() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { { ?s foaf:name ?n } UNION { ?s ex:name ?n } }
    let ds = construct_dataset();
    let algebra = project(
        union(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            bgp(vec![triple(var("s"), ex("name"), var("n"))]),
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-06",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie (foaf:name) + proj1, proj2 (ex:name) = 5
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()), // placeholder 1
            ("".to_string(), "".to_string(), "".to_string()), // placeholder 2
            ("".to_string(), "".to_string(), "".to_string()), // placeholder 3
            ("".to_string(), "".to_string(), "".to_string()), // placeholder 4
            ("".to_string(), "".to_string(), "".to_string()), // placeholder 5
        ]),
    );
    runner().run_test(&test).expect("construct-06 failed");
}

#[test]
fn test_construct_07_with_optional() {
    // CONSTRUCT { ?s foaf:name ?n ; foaf:mbox ?mb } WHERE { ?s foaf:name ?n OPTIONAL { ?s foaf:mbox ?mb } }
    let ds = construct_dataset();
    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            bgp(vec![triple(var("s"), foaf("mbox"), var("mb"))]),
            None,
        ),
        vec![variable("s"), variable("n"), variable("mb")],
    );
    let test = ConformanceTest::new(
        "construct-07",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice (foaf:name + mbox), bob (foaf:name only), charlie (foaf:name only) = 3
        // proj1/proj2 use ex:name, not foaf:name, so they don't match the left BGP
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-07 failed");
}

#[test]
fn test_construct_08_distinct_construct() {
    // CONSTRUCT DISTINCT — deduplicated results
    let ds = construct_dataset();
    let algebra = project(
        distinct(bgp(vec![triple(var("s"), rdf_type(), var("t"))])),
        vec![variable("s"), variable("t")],
    );
    let test = ConformanceTest::new(
        "construct-08",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // Alice, Bob, Charlie rdf:type foaf:Person = 3 distinct
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-08 failed");
}

#[test]
fn test_construct_09_with_limit() {
    // CONSTRUCT { ?s ex:p ?o } WHERE { ?s ex:p ?o } LIMIT 2
    let ds = small_dataset();
    let algebra = project(
        slice(
            bgp(vec![triple(var("s"), ex("p"), var("o"))]),
            None,
            Some(2),
        ),
        vec![variable("s"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-09",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-09 failed");
}

#[test]
fn test_construct_10_with_offset() {
    // CONSTRUCT OFFSET 1 — skip first
    let ds = small_dataset();
    let algebra = project(
        slice(
            bgp(vec![triple(var("s"), ex("p"), var("o"))]),
            Some(1),
            None,
        ),
        vec![variable("s"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-10",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-10 failed");
}

// ===== CONSTRUCT WITH JOINS =====

#[test]
fn test_construct_11_join_pattern() {
    // CONSTRUCT { ?person foaf:knows ?friend } WHERE { ?person foaf:knows ?friend }
    let ds = construct_dataset();
    let algebra = project(
        bgp(vec![triple(var("person"), foaf("knows"), var("friend"))]),
        vec![variable("person"), variable("friend")],
    );
    let test = ConformanceTest::new(
        "construct-11",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice-bob, alice-charlie, bob-charlie = 3
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-11 failed");
}

#[test]
fn test_construct_12_multi_pattern() {
    // CONSTRUCT { ?s foaf:name ?n ; foaf:age ?a } WHERE { ?s foaf:name ?n ; foaf:age ?a }
    let ds = construct_dataset();
    let algebra = project(
        bgp(vec![
            triple(var("s"), foaf("name"), var("n")),
            triple(var("s"), iri(&format!("{FOAF}age")), var("a")),
        ]),
        vec![variable("s"), variable("n"), variable("a")],
    );
    let test = ConformanceTest::new(
        "construct-12",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie each have name and age = 3
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-12 failed");
}

#[test]
fn test_construct_13_project_lead() {
    // CONSTRUCT { ?proj ex:lead ?lead } WHERE { ?proj ex:lead ?lead }
    let ds = construct_dataset();
    let algebra = project(
        bgp(vec![triple(var("proj"), ex("lead"), var("lead"))]),
        vec![variable("proj"), variable("lead")],
    );
    let test = ConformanceTest::new(
        "construct-13",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // proj1-alice, proj2-bob = 2
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-13 failed");
}

// ===== CONSTRUCT WITH VALUES =====

#[test]
fn test_construct_14_with_values() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { VALUES ?s { ex:alice ex:bob } ?s foaf:name ?n }
    let ds = construct_dataset();
    let algebra = project(
        join(
            values(
                vec![variable("s")],
                vec![
                    {
                        let mut b = crate::algebra::Binding::new();
                        b.insert(variable("s"), ex("alice"));
                        b
                    },
                    {
                        let mut b = crate::algebra::Binding::new();
                        b.insert(variable("s"), ex("bob"));
                        b
                    },
                ],
            ),
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-14",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice-Alice, bob-Bob = 2
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-14 failed");
}

// ===== CONSTRUCT TEMPLATE TRANSFORMATIONS =====

#[test]
fn test_construct_15_bind_transform() {
    // CONSTRUCT { ?s ex:nameLen ?len } WHERE { ?s foaf:name ?n BIND(strlen(?n) AS ?len) }
    let ds = construct_dataset();
    let algebra = project(
        extend(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            variable("len"),
            expr_fn("strlen", vec![expr_var("n")]),
        ),
        vec![variable("s"), variable("n"), variable("len")],
    );
    let test = ConformanceTest::new(
        "construct-15",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie have foaf:name; proj1/proj2 use ex:name = 3
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-15 failed");
}

#[test]
fn test_construct_16_order_by_then_construct() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { ?s foaf:name ?n } ORDER BY ?n
    let ds = construct_dataset();
    let algebra = project(
        order_by(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            vec![asc_cond(expr_var("n"))],
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-16",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie have foaf:name; proj1/proj2 use ex:name = 3 ordered alphabetically
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-16 failed");
}

// ===== CONSTRUCT BLANK NODES =====

#[test]
fn test_construct_17_blank_node_subjects() {
    // Dataset with blank node subjects
    let mut ds = InMemoryDataset::new();
    let bnode1 = crate::algebra::Term::BlankNode("b1".to_string());
    let bnode2 = crate::algebra::Term::BlankNode("b2".to_string());
    ds.add_triple(bnode1.clone(), foaf("name"), str_lit("Anonymous1"));
    ds.add_triple(bnode2.clone(), foaf("name"), str_lit("Anonymous2"));
    ds.add_triple(bnode1, rdf_type(), iri(&format!("{FOAF}Person")));

    let algebra = project(
        bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-17",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // b1 and b2 each have name = 2
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-17 failed");
}

#[test]
fn test_construct_18_blank_node_objects() {
    // Dataset with blank node objects
    let mut ds = InMemoryDataset::new();
    let bnode = crate::algebra::Term::BlankNode("addr1".to_string());
    ds.add_triple(ex("alice"), ex("address"), bnode.clone());
    ds.add_triple(bnode.clone(), ex("street"), str_lit("123 Main St"));
    ds.add_triple(bnode, ex("city"), str_lit("Springfield"));

    let algebra = project(
        bgp(vec![triple(var("s"), ex("address"), var("addr"))]),
        vec![variable("s"), variable("addr")],
    );
    let test = ConformanceTest::new(
        "construct-18",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice has one address
        ConformanceResult::ConstructGraph(vec![("".to_string(), "".to_string(), "".to_string())]),
    );
    runner().run_test(&test).expect("construct-18 failed");
}

// ===== NESTED CONSTRUCT PATTERNS =====

#[test]
fn test_construct_19_nested_join() {
    // Join two BGPs for CONSTRUCT
    let ds = construct_dataset();
    let algebra = project(
        join(
            bgp(vec![triple(var("proj"), ex("lead"), var("lead"))]),
            bgp(vec![triple(var("lead"), foaf("name"), var("lead_name"))]),
        ),
        vec![variable("proj"), variable("lead"), variable("lead_name")],
    );
    let test = ConformanceTest::new(
        "construct-19",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // proj1→alice→Alice, proj2→bob→Bob = 2
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-19 failed");
}

#[test]
fn test_construct_20_full_pipeline() {
    // Full pipeline: join + filter + order + limit for CONSTRUCT
    let ds = construct_dataset();
    let algebra = project(
        slice(
            order_by(
                filter(
                    bgp(vec![
                        triple(var("s"), rdf_type(), iri(&format!("{FOAF}Person"))),
                        triple(var("s"), foaf("name"), var("n")),
                        triple(var("s"), iri(&format!("{FOAF}age")), var("age")),
                    ]),
                    expr_gt(
                        expr_var("age"),
                        Expression::Literal(Literal {
                            value: "24".to_string(),
                            language: None,
                            datatype: Some(oxirs_core::model::NamedNode::new_unchecked(
                                "http://www.w3.org/2001/XMLSchema#integer",
                            )),
                        }),
                    ),
                ),
                vec![asc_cond(expr_var("n"))],
            ),
            None,
            Some(2),
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-20",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice (30) and bob (25) — ordered by name, limit 2
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-20 failed");
}

// ===== ADDITIONAL CONSTRUCT EDGE CASES =====

#[test]
fn test_construct_21_single_triple() {
    // Single triple result
    let ds = small_dataset();
    let algebra = project(
        filter(
            bgp(vec![triple(var("s"), ex("p"), var("o"))]),
            expr_eq(expr_var("s"), expr_iri("http://example.org/s1")),
        ),
        vec![variable("s"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-21",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![(
            "ex:s1".to_string(),
            "ex:p".to_string(),
            "value1".to_string(),
        )]),
    );
    runner().run_test(&test).expect("construct-21 failed");
}

#[test]
fn test_construct_22_all_subject_types() {
    // Dataset with IRI, blank node, and literal subjects (unusual but valid)
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("r1"), ex("pred"), int_lit(1));
    ds.add_triple(ex("r2"), ex("pred"), int_lit(2));
    ds.add_triple(
        crate::algebra::Term::BlankNode("bn1".to_string()),
        ex("pred"),
        int_lit(3),
    );

    let algebra = project(
        bgp(vec![triple(var("s"), ex("pred"), var("v"))]),
        vec![variable("s"), variable("v")],
    );
    let test = ConformanceTest::new(
        "construct-22",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-22 failed");
}

#[test]
fn test_construct_23_type_check_in_where() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { ?s foaf:name ?n FILTER(isIRI(?s)) }
    let ds = construct_dataset();
    let algebra = project(
        filter(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            Expression::Unary {
                op: crate::algebra::UnaryOperator::IsIri,
                operand: Box::new(expr_var("s")),
            },
        ),
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-23",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice, bob, charlie have foaf:name with IRI subjects = 3
        // (proj1/proj2 use ex:name, not foaf:name)
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-23 failed");
}

#[test]
fn test_construct_24_nested_optional_blank() {
    // CONSTRUCT from nested optional with blank nodes
    let mut ds = InMemoryDataset::new();
    let addr = crate::algebra::Term::BlankNode("addr".to_string());
    ds.add_triple(ex("alice"), foaf("name"), str_lit("Alice"));
    ds.add_triple(ex("alice"), ex("address"), addr.clone());
    ds.add_triple(addr, ex("city"), str_lit("Springfield"));
    ds.add_triple(ex("bob"), foaf("name"), str_lit("Bob"));
    // bob has no address

    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            bgp(vec![triple(var("s"), ex("address"), var("addr"))]),
            None,
        ),
        vec![variable("s"), variable("n"), variable("addr")],
    );
    let test = ConformanceTest::new(
        "construct-24",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // alice (with addr), bob (without addr) = 2 solutions
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-24 failed");
}

#[test]
fn test_construct_25_subquery_in_construct() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { SELECT ?s ?n WHERE { ?s foaf:name ?n } LIMIT 3 }
    let ds = construct_dataset();
    // Model the subquery as a slice inside a project
    let inner = project(
        slice(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            None,
            Some(3),
        ),
        vec![variable("s"), variable("n")],
    );
    let algebra = project(inner, vec![variable("s"), variable("n")]);
    let test = ConformanceTest::new(
        "construct-25",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-25 failed");
}

#[test]
fn test_construct_26_full_graph_copy() {
    // Copy all triples from small_dataset
    let ds = small_dataset();
    let algebra = project(
        bgp(vec![triple(var("s"), var("p"), var("o"))]),
        vec![variable("s"), variable("p"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-26",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // 3 triples in small_dataset
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-26 failed");
}

#[test]
fn test_construct_27_with_str_function() {
    // CONSTRUCT uses STR() result
    let ds = small_dataset();
    let algebra = project(
        extend(
            bgp(vec![triple(var("s"), ex("p"), var("o"))]),
            variable("os"),
            expr_fn("str", vec![expr_var("o")]),
        ),
        vec![variable("s"), variable("os")],
    );
    let test = ConformanceTest::new(
        "construct-27",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-27 failed");
}

#[test]
fn test_construct_28_minus_pattern() {
    // CONSTRUCT { ?s foaf:name ?n } WHERE { ?s foaf:name ?n MINUS { ?s foaf:mbox ?mb } }
    let ds = construct_dataset();
    let algebra = project(
        crate::algebra::Algebra::Minus {
            left: Box::new(bgp(vec![triple(var("s"), foaf("name"), var("n"))])),
            right: Box::new(bgp(vec![triple(var("s"), foaf("mbox"), var("mb"))])),
        },
        vec![variable("s"), variable("n")],
    );
    let test = ConformanceTest::new(
        "construct-28",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        // foaf:name matches alice/bob/charlie (3); alice has mbox so is excluded by MINUS.
        // proj1/proj2 use ex:name, not foaf:name, so they are not in the left side. Result = 2.
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-28 failed");
}

#[test]
fn test_construct_29_reduced_results() {
    // CONSTRUCT REDUCED — may remove duplicates
    let ds = construct_dataset();
    let algebra = project(
        crate::algebra::Algebra::Reduced {
            pattern: Box::new(bgp(vec![triple(var("s"), rdf_type(), var("t"))])),
        },
        vec![variable("s"), variable("t")],
    );
    let test = ConformanceTest::new(
        "construct-29",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
            ("".to_string(), "".to_string(), "".to_string()),
        ]),
    );
    runner().run_test(&test).expect("construct-29 failed");
}

#[test]
fn test_construct_30_empty_dataset() {
    // CONSTRUCT from empty dataset yields empty graph
    let ds = InMemoryDataset::new();
    let algebra = project(
        bgp(vec![triple(var("s"), var("p"), var("o"))]),
        vec![variable("s"), variable("p"), variable("o")],
    );
    let test = ConformanceTest::new(
        "construct-30",
        ConformanceGroup::BasicPatterns,
        algebra,
        ds,
        ConformanceResult::ConstructGraph(vec![]),
    );
    runner().run_test(&test).expect("construct-30 failed");
}