oxirs-fuseki 0.3.2

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Tests for enhanced BIND and VALUES clause processing

use oxirs_fuseki::bind_values_enhanced::*;
use serde_json::json;
use std::collections::HashMap;

#[tokio::test]
async fn test_bind_string_functions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        PREFIX foaf: <http://xmlns.com/foaf/0.1/>
        SELECT ?name ?upperName ?nameLength
        WHERE {
            ?person foaf:firstName ?first ;
                    foaf:lastName ?last .
            BIND(CONCAT(?first, " ", ?last) AS ?name)
            BIND(UCASE(?name) AS ?upperName)
            BIND(STRLEN(?name) AS ?nameLength)
        }
    "#;

    let mut bindings = vec![
        HashMap::from([
            ("first".to_string(), json!("John")),
            ("last".to_string(), json!("Doe")),
        ]),
        HashMap::from([
            ("first".to_string(), json!("Jane")),
            ("last".to_string(), json!("Smith")),
        ]),
    ];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    // Check first binding
    assert_eq!(bindings[0].get("?name"), Some(&json!("evaluated_result")));
    assert_eq!(
        bindings[0].get("?upperName"),
        Some(&json!("evaluated_result"))
    );
    assert_eq!(
        bindings[0].get("?nameLength"),
        Some(&json!("evaluated_result"))
    );
}

#[tokio::test]
async fn test_bind_numeric_functions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?price ?tax ?total ?rounded
        WHERE {
            ?product :price ?price .
            BIND(?price * 0.1 AS ?tax)
            BIND(?price + ?tax AS ?total)
            BIND(ROUND(?total) AS ?rounded)
        }
    "#;

    let mut bindings = vec![
        HashMap::from([("price".to_string(), json!(99.99))]),
        HashMap::from([("price".to_string(), json!(149.50))]),
    ];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    // Verify bindings were processed
    assert!(bindings[0].contains_key("?tax"));
    assert!(bindings[0].contains_key("?total"));
    assert!(bindings[0].contains_key("?rounded"));
}

#[tokio::test]
async fn test_bind_date_functions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?date ?year ?month ?day
        WHERE {
            ?event :date ?date .
            BIND(YEAR(?date) AS ?year)
            BIND(MONTH(?date) AS ?month)
            BIND(DAY(?date) AS ?day)
            BIND(NOW() AS ?currentTime)
        }
    "#;

    let mut bindings = vec![HashMap::from([("date".to_string(), json!("2024-06-15"))])];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    assert!(bindings[0].contains_key("?year"));
    assert!(bindings[0].contains_key("?month"));
    assert!(bindings[0].contains_key("?day"));
    assert!(bindings[0].contains_key("?currentTime"));
}

#[tokio::test]
async fn test_bind_hash_functions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?email ?emailHash ?secureHash
        WHERE {
            ?person :email ?email .
            BIND(MD5(?email) AS ?emailHash)
            BIND(SHA256(?email) AS ?secureHash)
        }
    "#;

    let mut bindings = vec![HashMap::from([(
        "email".to_string(),
        json!("user@example.com"),
    )])];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    assert!(bindings[0].contains_key("?emailHash"));
    assert!(bindings[0].contains_key("?secureHash"));
}

#[tokio::test]
async fn test_bind_conditional_expressions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?age ?category ?discount
        WHERE {
            ?person :age ?age .
            BIND(IF(?age < 18, "minor", IF(?age >= 65, "senior", "adult")) AS ?category)
            BIND(IF(?category = "senior", 0.2, IF(?category = "minor", 0.1, 0.0)) AS ?discount)
        }
    "#;

    let mut bindings = vec![
        HashMap::from([("age".to_string(), json!(10))]),
        HashMap::from([("age".to_string(), json!(30))]),
        HashMap::from([("age".to_string(), json!(70))]),
    ];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    for binding in &bindings {
        assert!(binding.contains_key("?category"));
        assert!(binding.contains_key("?discount"));
    }
}

#[tokio::test]
async fn test_values_simple_inline() {
    let processor = EnhancedValuesProcessor::new();

    let query = r#"
        SELECT ?person ?email
        WHERE {
            VALUES (?person ?email) {
                (:alice "alice@example.com")
                (:bob "bob@example.com")
                (:charlie "charlie@example.com")
            }
            ?person :hasEmail ?email .
        }
    "#;

    let mut bindings = vec![
        HashMap::new(), // Empty initial binding
    ];

    processor
        .process_values_clauses(query, &mut bindings)
        .await
        .unwrap();

    // Should create 3 bindings from VALUES
    assert_eq!(bindings.len(), 3);
}

#[tokio::test]
async fn test_values_multiple_variables() {
    let processor = EnhancedValuesProcessor::new();

    let query = r#"
        SELECT ?x ?y ?z
        WHERE {
            VALUES (?x ?y ?z) {
                (1 2 3)
                (4 5 6)
                (7 8 9)
                (UNDEF 10 11)
            }
        }
    "#;

    let mut bindings = vec![HashMap::new()];

    processor
        .process_values_clauses(query, &mut bindings)
        .await
        .unwrap();

    // Should handle UNDEF values properly
    assert_eq!(bindings.len(), 4);
}

#[tokio::test]
async fn test_values_with_existing_bindings() {
    let processor = EnhancedValuesProcessor::new();

    let query = r#"
        SELECT ?person ?status ?priority
        WHERE {
            ?person :name ?name .
            VALUES (?status ?priority) {
                ("active" 1)
                ("pending" 2)
                ("inactive" 3)
            }
        }
    "#;

    let mut bindings = vec![
        HashMap::from([
            ("person".to_string(), json!(":john")),
            ("name".to_string(), json!("John Doe")),
        ]),
        HashMap::from([
            ("person".to_string(), json!(":jane")),
            ("name".to_string(), json!("Jane Smith")),
        ]),
    ];

    processor
        .process_values_clauses(query, &mut bindings)
        .await
        .unwrap();

    // Should create cross product: 2 persons × 3 status values = 6 bindings
    assert_eq!(bindings.len(), 6);
}

#[tokio::test]
async fn test_bind_expression_caching() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?x ?computed
        WHERE {
            ?item :value ?x .
            BIND(CONCAT("prefix_", STR(?x), "_suffix") AS ?computed)
        }
    "#;

    // Large number of bindings with same expression
    let mut bindings: Vec<_> = (0..100)
        .map(|i| HashMap::from([("x".to_string(), json!(i))]))
        .collect();

    let start = std::time::Instant::now();
    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();
    let duration = start.elapsed();

    // All bindings should have computed value
    for binding in &bindings {
        assert!(binding.contains_key("?computed"));
    }

    // Cache should make this fast
    assert!(duration.as_millis() < 1000);
}

#[tokio::test]
async fn test_values_optimization_strategies() {
    let processor = EnhancedValuesProcessor::new();

    // Large VALUES clause that should trigger optimization
    let mut query = String::from(
        r#"
        SELECT ?id ?value
        WHERE {
            VALUES (?id ?value) {
    "#,
    );

    // Add 1000 value pairs
    for i in 0..1000 {
        query.push_str(&format!("                ({} {})\n", i, i * 10));
    }

    query.push_str(
        r#"
            }
            ?entity :id ?id ;
                    :value ?value .
        }
    "#,
    );

    let mut bindings = vec![HashMap::new()];

    let start = std::time::Instant::now();
    processor
        .process_values_clauses(&query, &mut bindings)
        .await
        .unwrap();
    let duration = start.elapsed();

    // Should handle large VALUES efficiently
    assert_eq!(bindings.len(), 1000);
    assert!(duration.as_millis() < 5000);
}

#[tokio::test]
async fn test_bind_complex_expressions() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?uri ?localName ?namespace
        WHERE {
            ?s ?p ?uri .
            FILTER(ISURI(?uri))
            BIND(REPLACE(STR(?uri), "^.*/([^/]*)$", "$1") AS ?localName)
            BIND(REPLACE(STR(?uri), "/[^/]*$", "/") AS ?namespace)
        }
    "#;

    let mut bindings = vec![
        HashMap::from([("uri".to_string(), json!("http://example.org/onto/Person"))]),
        HashMap::from([("uri".to_string(), json!("http://schema.org/Organization"))]),
    ];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    for binding in &bindings {
        assert!(binding.contains_key("?localName"));
        assert!(binding.contains_key("?namespace"));
    }
}

#[tokio::test]
async fn test_bind_coalesce_function() {
    let processor = EnhancedBindProcessor::new();

    let query = r#"
        SELECT ?name ?displayName
        WHERE {
            ?person :firstName ?first .
            OPTIONAL { ?person :nickName ?nick }
            OPTIONAL { ?person :lastName ?last }
            BIND(COALESCE(?nick, ?first, "Anonymous") AS ?displayName)
            BIND(CONCAT(COALESCE(?first, ""), " ", COALESCE(?last, "")) AS ?name)
        }
    "#;

    let mut bindings = vec![
        HashMap::from([
            ("first".to_string(), json!("John")),
            ("nick".to_string(), json!("Johnny")),
            ("last".to_string(), json!("Doe")),
        ]),
        HashMap::from([
            ("first".to_string(), json!("Jane")),
            // No nickname
            ("last".to_string(), json!("Smith")),
        ]),
        HashMap::from([
            // No first name or nickname
            ("last".to_string(), json!("Anonymous")),
        ]),
    ];

    processor
        .process_bind_clauses(query, &mut bindings)
        .await
        .unwrap();

    // All bindings should have displayName
    for binding in &bindings {
        assert!(binding.contains_key("?displayName"));
        assert!(binding.contains_key("?name"));
    }
}

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

    #[tokio::test]
    async fn test_bind_performance_with_many_expressions() {
        let processor = EnhancedBindProcessor::new();

        let query = r#"
            SELECT ?x ?a ?b ?c ?d ?e
            WHERE {
                ?item :value ?x .
                BIND(?x * 2 AS ?a)
                BIND(?a + 10 AS ?b)
                BIND(?b / 3 AS ?c)
                BIND(ROUND(?c) AS ?d)
                BIND(STR(?d) AS ?e)
            }
        "#;

        // Create many bindings
        let mut bindings: Vec<_> = (0..10000)
            .map(|i| HashMap::from([("x".to_string(), json!(i as f64))]))
            .collect();

        let start = std::time::Instant::now();
        processor
            .process_bind_clauses(query, &mut bindings)
            .await
            .unwrap();
        let duration = start.elapsed();

        // Should process efficiently even with many bindings
        assert!(duration.as_secs() < 5);

        // Verify all bindings have all computed values
        for binding in &bindings {
            assert!(binding.contains_key("?a"));
            assert!(binding.contains_key("?b"));
            assert!(binding.contains_key("?c"));
            assert!(binding.contains_key("?d"));
            assert!(binding.contains_key("?e"));
        }
    }

    #[tokio::test]
    async fn test_values_memory_efficiency() {
        let processor = EnhancedValuesProcessor::new();

        // Create a VALUES clause with many columns
        let mut query = String::from(
            r#"
            SELECT ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j
            WHERE {
                VALUES (?a ?b ?c ?d ?e ?f ?g ?h ?i ?j) {
        "#,
        );

        // Add rows
        for n in 0..5000 {
            query.push_str(&format!(
                "                    ({} {} {} {} {} {} {} {} {} {})\n",
                n,
                n + 1,
                n + 2,
                n + 3,
                n + 4,
                n + 5,
                n + 6,
                n + 7,
                n + 8,
                n + 9
            ));
        }

        query.push_str(
            "                }
            }
        ",
        );

        let mut bindings = vec![HashMap::new()];

        let start = std::time::Instant::now();
        processor
            .process_values_clauses(&query, &mut bindings)
            .await
            .unwrap();
        let duration = start.elapsed();

        // Should handle efficiently
        assert_eq!(bindings.len(), 5000);
        assert!(duration.as_secs() < 10);
    }
}