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
//! OPTIONAL and UNION Conformance Tests
//!
//! Tests SPARQL 1.1 OPTIONAL (LEFT JOIN) and UNION patterns.

use super::framework::*;
use super::helpers::*;

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

// ===== OPTIONAL / LEFT JOIN TESTS =====

#[test]
fn test_optional_01_basic() {
    // SELECT ?s ?name ?mbox WHERE { ?s foaf:name ?name . OPTIONAL { ?s foaf:mbox ?mbox } }
    // alice has both name and mbox, bob only name, charlie both
    let ds = optional_dataset();
    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
            bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
            None,
        ),
        vec![variable("s"), variable("name"), variable("mbox")],
    );
    let test = ConformanceTest::new(
        "optional-01",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(3), // 3 people, even without mbox
    );
    runner().run_test(&test).expect("optional-01 failed");
}

#[test]
fn test_optional_02_all_have() {
    // SELECT ?s ?name ?mbox WHERE { ?s foaf:name ?name . OPTIONAL { ?s foaf:mbox ?mbox } }
    // When all entries have the optional property
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), foaf("name"), str_lit("A"));
    ds.add_triple(ex("a"), foaf("mbox"), str_lit("a@ex.org"));
    ds.add_triple(ex("b"), foaf("name"), str_lit("B"));
    ds.add_triple(ex("b"), foaf("mbox"), str_lit("b@ex.org"));

    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
            bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
            None,
        ),
        vec![variable("s"), variable("name"), variable("mbox")],
    );
    let test = ConformanceTest::new(
        "optional-02",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(2),
    );
    runner().run_test(&test).expect("optional-02 failed");
}

#[test]
fn test_optional_03_none_have() {
    // When none have the optional property, we still get left rows
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), foaf("name"), str_lit("A"));
    ds.add_triple(ex("b"), foaf("name"), str_lit("B"));

    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
            bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
            None,
        ),
        vec![variable("s"), variable("name")],
    );
    let test = ConformanceTest::new(
        "optional-03",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(2), // both rows preserved without mbox
    );
    runner().run_test(&test).expect("optional-03 failed");
}

#[test]
fn test_optional_04_empty_left() {
    // OPTIONAL on empty left — no results
    let ds = InMemoryDataset::new();
    let algebra = left_join(
        bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
        bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
        None,
    );
    let test = ConformanceTest::new(
        "optional-04",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(0),
    );
    runner().run_test(&test).expect("optional-04 failed");
}

#[test]
fn test_optional_05_multiple_optional() {
    // SELECT ?s ?name ?mbox ?age WHERE {
    //   ?s foaf:name ?name .
    //   OPTIONAL { ?s foaf:mbox ?mbox }
    //   OPTIONAL { ?s foaf:age ?age }
    // }
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), foaf("name"), str_lit("Alice"));
    ds.add_triple(ex("a"), foaf("mbox"), str_lit("a@ex"));
    ds.add_triple(ex("a"), iri(&format!("{FOAF}age")), int_lit(30));
    ds.add_triple(ex("b"), foaf("name"), str_lit("Bob"));
    // bob has neither mbox nor age

    let algebra = project(
        left_join(
            left_join(
                bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
                bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
                None,
            ),
            bgp(vec![triple(
                var("s"),
                iri(&format!("{FOAF}age")),
                var("age"),
            )]),
            None,
        ),
        vec![
            variable("s"),
            variable("name"),
            variable("mbox"),
            variable("age"),
        ],
    );
    let test = ConformanceTest::new(
        "optional-05",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(2), // alice and bob
    );
    runner().run_test(&test).expect("optional-05 failed");
}

#[test]
fn test_optional_06_specific_people() {
    // Verify alice and bob are both in result of optional query
    let ds = optional_dataset();
    let algebra = project(
        left_join(
            bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
            bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
            None,
        ),
        vec![variable("name")],
    );
    let test = ConformanceTest::new(
        "optional-06",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::SelectResults(vec![
            row(&[("name", "Alice")]),
            row(&[("name", "Bob")]),
            row(&[("name", "Charlie")]),
        ]),
    );
    runner().run_test(&test).expect("optional-06 failed");
}

// ===== UNION TESTS =====

#[test]
fn test_union_01_basic() {
    // SELECT ?s WHERE { { ?s rdf:type foaf:Person } UNION { ?s :orgName ?n } }
    let ds = union_dataset();
    let algebra = project(
        union(
            bgp(vec![triple(
                var("s"),
                rdf_type(),
                iri(&format!("{FOAF}Person")),
            )]),
            bgp(vec![triple(var("s"), ex("orgName"), var("n"))]),
        ),
        vec![variable("s")],
    );
    let test = ConformanceTest::new(
        "union-01",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(3), // alice, bob (persons), acme (org)
    );
    runner().run_test(&test).expect("union-01 failed");
}

#[test]
fn test_union_02_same_variable() {
    // SELECT ?name WHERE { { ?s foaf:name ?name } UNION { ?s :orgName ?name } }
    let ds = union_dataset();
    let algebra = project(
        union(
            bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
            bgp(vec![triple(var("s"), ex("orgName"), var("name"))]),
        ),
        vec![variable("name")],
    );
    let test = ConformanceTest::new(
        "union-02",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(3), // Alice, Bob, ACME Corp
    );
    runner().run_test(&test).expect("union-02 failed");
}

#[test]
fn test_union_03_left_only() {
    // UNION where only left produces results
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), ex("p1"), str_lit("v1"));

    let algebra = union(
        bgp(vec![triple(var("s"), ex("p1"), var("o"))]),
        bgp(vec![triple(var("s"), ex("p2"), var("o"))]),
    );
    let test = ConformanceTest::new(
        "union-03",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(1),
    );
    runner().run_test(&test).expect("union-03 failed");
}

#[test]
fn test_union_04_right_only() {
    // UNION where only right produces results
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), ex("p2"), str_lit("v2"));

    let algebra = union(
        bgp(vec![triple(var("s"), ex("p1"), var("o"))]),
        bgp(vec![triple(var("s"), ex("p2"), var("o"))]),
    );
    let test = ConformanceTest::new(
        "union-04",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(1),
    );
    runner().run_test(&test).expect("union-04 failed");
}

#[test]
fn test_union_05_empty_both() {
    // UNION of two empty patterns
    let ds = InMemoryDataset::new();
    let algebra = union(
        bgp(vec![triple(var("s"), ex("p1"), var("o"))]),
        bgp(vec![triple(var("s"), ex("p2"), var("o"))]),
    );
    let test = ConformanceTest::new(
        "union-05",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(0),
    );
    runner().run_test(&test).expect("union-05 failed");
}

#[test]
fn test_union_06_nested() {
    // Nested UNION: { p1 } UNION { { p2 } UNION { p3 } }
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), ex("p1"), str_lit("v1"));
    ds.add_triple(ex("b"), ex("p2"), str_lit("v2"));
    ds.add_triple(ex("c"), ex("p3"), str_lit("v3"));

    let algebra = union(
        bgp(vec![triple(var("s"), ex("p1"), var("o"))]),
        union(
            bgp(vec![triple(var("s"), ex("p2"), var("o"))]),
            bgp(vec![triple(var("s"), ex("p3"), var("o"))]),
        ),
    );
    let test = ConformanceTest::new(
        "union-06",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(3),
    );
    runner().run_test(&test).expect("union-06 failed");
}

#[test]
fn test_union_07_distinct_after() {
    // SELECT DISTINCT ?s WHERE { { ?s foaf:name ?n } UNION { ?s rdf:type ?t } }
    // alice appears in both unions
    let ds = union_dataset();
    let algebra = distinct(project(
        union(
            bgp(vec![triple(var("s"), foaf("name"), var("n"))]),
            bgp(vec![triple(var("s"), rdf_type(), var("t"))]),
        ),
        vec![variable("s")],
    ));
    let test = ConformanceTest::new(
        "union-07",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(3), // alice, bob, acme (distinct)
    );
    runner().run_test(&test).expect("union-07 failed");
}

#[test]
fn test_union_08_with_filter() {
    // SELECT ?name WHERE { { ?s foaf:name ?name } UNION { ?s :orgName ?name } . FILTER(STRLEN(?name) > 3) }
    let ds = union_dataset();
    // Use regex-based filter approximation: names longer than 3 chars
    let algebra = project(
        filter(
            union(
                bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
                bgp(vec![triple(var("s"), ex("orgName"), var("name"))]),
            ),
            expr_gt(
                expr_fn("str", vec![expr_var("name")]),
                expr_lit(lit_str("")),
            ),
        ),
        vec![variable("name")],
    );
    let test = ConformanceTest::new(
        "union-08",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(3),
    );
    runner().run_test(&test).expect("union-08 failed");
}

// ===== OPTIONAL + UNION COMBINATION =====

#[test]
fn test_optional_union_01() {
    // Combine OPTIONAL inside a UNION branch
    let mut ds = InMemoryDataset::new();
    ds.add_triple(ex("a"), foaf("name"), str_lit("Alice"));
    ds.add_triple(ex("b"), ex("code"), str_lit("B001"));

    let algebra = project(
        union(
            left_join(
                bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
                bgp(vec![triple(var("s"), ex("code"), var("code"))]),
                None,
            ),
            bgp(vec![triple(var("s"), ex("code"), var("code"))]),
        ),
        vec![variable("s")],
    );
    let test = ConformanceTest::new(
        "optional-union-01",
        ConformanceGroup::Union,
        algebra,
        ds,
        ConformanceResult::ResultCount(2), // a (from optional), b (from union right)
    );
    runner().run_test(&test).expect("optional-union-01 failed");
}

// ===== OPTIONAL WITH FILTER =====

#[test]
fn test_optional_filter_01() {
    // SELECT ?name WHERE { ?s foaf:name ?name . OPTIONAL { ?s foaf:mbox ?mbox } . FILTER(!BOUND(?mbox) || ?mbox = "alice@example.org") }
    // Only alice has mbox from optional_dataset, bob has no mbox
    let ds = optional_dataset();

    // Simpler test: filter on the left part after optional
    let algebra = project(
        filter(
            left_join(
                bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
                bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
                None,
            ),
            expr_eq(expr_var("name"), expr_lit(lit_str("Alice"))),
        ),
        vec![variable("name")],
    );
    let test = ConformanceTest::new(
        "optional-filter-01",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::SelectResults(vec![row(&[("name", "Alice")])]),
    );
    runner().run_test(&test).expect("optional-filter-01 failed");
}

#[test]
fn test_optional_filter_02_no_result() {
    // OPTIONAL + FILTER that rejects all left rows
    let ds = optional_dataset();
    let algebra = project(
        filter(
            left_join(
                bgp(vec![triple(var("s"), foaf("name"), var("name"))]),
                bgp(vec![triple(var("s"), foaf("mbox"), var("mbox"))]),
                None,
            ),
            expr_eq(expr_var("name"), expr_lit(lit_str("NonExistent"))),
        ),
        vec![variable("name")],
    );
    let test = ConformanceTest::new(
        "optional-filter-02",
        ConformanceGroup::Optional,
        algebra,
        ds,
        ConformanceResult::ResultCount(0),
    );
    runner().run_test(&test).expect("optional-filter-02 failed");
}