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
//! SPARQL query profiling for performance analysis
//!
//! This module provides comprehensive profiling capabilities for SPARQL query execution,
//! tracking parse time, planning time, execution time, and resource usage.

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Query execution phase
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QueryPhase {
    /// Query parsing phase
    Parsing,
    /// Query planning/optimization phase
    Planning,
    /// Query execution phase
    Execution,
    /// Result materialization phase
    Materialization,
}

impl QueryPhase {
    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            QueryPhase::Parsing => "Parsing",
            QueryPhase::Planning => "Planning",
            QueryPhase::Execution => "Execution",
            QueryPhase::Materialization => "Materialization",
        }
    }
}

/// Statistics for a single query execution
#[derive(Debug, Clone)]
pub struct QueryStats {
    /// Query text or ID
    pub query_id: String,
    /// Total execution time
    pub total_duration: Duration,
    /// Time spent in each phase
    pub phase_durations: HashMap<QueryPhase, Duration>,
    /// Number of triples matched
    pub triples_matched: usize,
    /// Number of results returned
    pub results_count: usize,
    /// Peak memory usage (bytes)
    pub peak_memory: usize,
    /// Number of joins performed
    pub joins_performed: usize,
    /// Cache hit rate (0.0 - 1.0)
    pub cache_hit_rate: f64,
    /// Start timestamp
    pub start_time: Option<Instant>,
    /// End timestamp
    pub end_time: Option<Instant>,
}

impl QueryStats {
    /// Create new query statistics
    pub fn new(query_id: String) -> Self {
        Self {
            query_id,
            total_duration: Duration::default(),
            phase_durations: HashMap::new(),
            triples_matched: 0,
            results_count: 0,
            peak_memory: 0,
            joins_performed: 0,
            cache_hit_rate: 0.0,
            start_time: None,
            end_time: None,
        }
    }

    /// Get duration for a specific phase
    pub fn phase_duration(&self, phase: QueryPhase) -> Duration {
        self.phase_durations
            .get(&phase)
            .copied()
            .unwrap_or_default()
    }

    /// Get throughput (results per second)
    pub fn throughput(&self) -> f64 {
        if self.total_duration.as_secs_f64() > 0.0 {
            self.results_count as f64 / self.total_duration.as_secs_f64()
        } else {
            0.0
        }
    }

    /// Get MB processed per second
    pub fn mb_per_second(&self) -> f64 {
        let mb = self.peak_memory as f64 / (1024.0 * 1024.0);
        if self.total_duration.as_secs_f64() > 0.0 {
            mb / self.total_duration.as_secs_f64()
        } else {
            0.0
        }
    }

    /// Generate human-readable report
    pub fn report(&self) -> String {
        let mut lines = vec![
            format!("Query Execution Statistics: {}", self.query_id),
            format!(
                "  Total Duration: {:.3}s",
                self.total_duration.as_secs_f64()
            ),
        ];

        // Phase breakdowns
        for phase in &[
            QueryPhase::Parsing,
            QueryPhase::Planning,
            QueryPhase::Execution,
            QueryPhase::Materialization,
        ] {
            if let Some(duration) = self.phase_durations.get(phase) {
                let percentage = if self.total_duration.as_secs_f64() > 0.0 {
                    (duration.as_secs_f64() / self.total_duration.as_secs_f64()) * 100.0
                } else {
                    0.0
                };
                lines.push(format!(
                    "    {}: {:.3}s ({:.1}%)",
                    phase.name(),
                    duration.as_secs_f64(),
                    percentage
                ));
            }
        }

        lines.extend(vec![
            format!("  Triples Matched: {}", self.triples_matched),
            format!("  Results: {}", self.results_count),
            format!("  Joins: {}", self.joins_performed),
            format!("  Cache Hit Rate: {:.1}%", self.cache_hit_rate * 100.0),
            format!(
                "  Peak Memory: {:.2} MB",
                self.peak_memory as f64 / (1024.0 * 1024.0)
            ),
            format!("  Throughput: {:.0} results/s", self.throughput()),
        ]);

        lines.join("\n")
    }
}

/// SPARQL query profiler
pub struct QueryProfiler {
    /// Current query being profiled
    current_query: Option<QueryStats>,
    /// Phase start times
    phase_start: HashMap<QueryPhase, Instant>,
    /// Whether profiling is enabled
    enabled: bool,
    /// History of profiled queries (limited to last N)
    history: Vec<QueryStats>,
    /// Maximum history size
    max_history: usize,
}

impl QueryProfiler {
    /// Create a new query profiler
    pub fn new() -> Self {
        Self {
            current_query: None,
            phase_start: HashMap::new(),
            enabled: true,
            history: Vec::new(),
            max_history: 100,
        }
    }

    /// Create a disabled profiler (no overhead)
    pub fn disabled() -> Self {
        Self {
            current_query: None,
            phase_start: HashMap::new(),
            enabled: false,
            history: Vec::new(),
            max_history: 0,
        }
    }

    /// Enable profiling
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable profiling
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Check if profiling is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Start profiling a query
    pub fn start_query(&mut self, query_id: String) {
        if !self.enabled {
            return;
        }

        let mut stats = QueryStats::new(query_id);
        stats.start_time = Some(Instant::now());
        self.current_query = Some(stats);
    }

    /// Start a query phase
    pub fn start_phase(&mut self, phase: QueryPhase) {
        if !self.enabled || self.current_query.is_none() {
            return;
        }

        self.phase_start.insert(phase, Instant::now());
    }

    /// End a query phase
    pub fn end_phase(&mut self, phase: QueryPhase) {
        if !self.enabled || self.current_query.is_none() {
            return;
        }

        if let Some(start) = self.phase_start.remove(&phase) {
            let duration = start.elapsed();
            if let Some(ref mut stats) = self.current_query {
                stats.phase_durations.insert(phase, duration);
            }
        }
    }

    /// Record triples matched
    pub fn record_triples(&mut self, count: usize) {
        if let Some(ref mut stats) = self.current_query {
            stats.triples_matched += count;
        }
    }

    /// Record results count
    pub fn record_results(&mut self, count: usize) {
        if let Some(ref mut stats) = self.current_query {
            stats.results_count = count;
        }
    }

    /// Record memory usage
    pub fn record_memory(&mut self, bytes: usize) {
        if let Some(ref mut stats) = self.current_query {
            stats.peak_memory = stats.peak_memory.max(bytes);
        }
    }

    /// Record join operation
    pub fn record_join(&mut self) {
        if let Some(ref mut stats) = self.current_query {
            stats.joins_performed += 1;
        }
    }

    /// Record cache hit rate
    pub fn record_cache_hit_rate(&mut self, rate: f64) {
        if let Some(ref mut stats) = self.current_query {
            stats.cache_hit_rate = rate.clamp(0.0, 1.0);
        }
    }

    /// End profiling current query
    pub fn end_query(&mut self) -> Option<QueryStats> {
        if !self.enabled {
            return None;
        }

        if let Some(mut stats) = self.current_query.take() {
            stats.end_time = Some(Instant::now());
            if let (Some(start), Some(end)) = (stats.start_time, stats.end_time) {
                stats.total_duration = end.duration_since(start);
            }

            // Add to history
            if self.history.len() >= self.max_history {
                self.history.remove(0);
            }
            self.history.push(stats.clone());

            Some(stats)
        } else {
            None
        }
    }

    /// Get current query statistics
    pub fn current_stats(&self) -> Option<&QueryStats> {
        self.current_query.as_ref()
    }

    /// Get query history
    pub fn history(&self) -> &[QueryStats] {
        &self.history
    }

    /// Clear history
    pub fn clear_history(&mut self) {
        self.history.clear();
    }

    /// Get average statistics across all queries in history
    pub fn average_stats(&self) -> Option<AverageStats> {
        if self.history.is_empty() {
            return None;
        }

        let count = self.history.len() as f64;
        let total_duration: Duration = self.history.iter().map(|s| s.total_duration).sum();
        let avg_triples = self
            .history
            .iter()
            .map(|s| s.triples_matched)
            .sum::<usize>() as f64
            / count;
        let avg_results =
            self.history.iter().map(|s| s.results_count).sum::<usize>() as f64 / count;
        let avg_joins = self
            .history
            .iter()
            .map(|s| s.joins_performed)
            .sum::<usize>() as f64
            / count;
        let avg_cache_hit = self.history.iter().map(|s| s.cache_hit_rate).sum::<f64>() / count;
        let avg_memory = self.history.iter().map(|s| s.peak_memory).sum::<usize>() as f64 / count;

        Some(AverageStats {
            query_count: self.history.len(),
            avg_duration: Duration::from_secs_f64(total_duration.as_secs_f64() / count),
            avg_triples_matched: avg_triples,
            avg_results_count: avg_results,
            avg_joins_performed: avg_joins,
            avg_cache_hit_rate: avg_cache_hit,
            avg_peak_memory: avg_memory,
        })
    }

    /// Generate summary report
    pub fn summary_report(&self) -> String {
        if self.history.is_empty() {
            return "No query history available".to_string();
        }

        let mut lines = vec![format!(
            "Query Profiler Summary ({} queries)",
            self.history.len()
        )];

        if let Some(avg) = self.average_stats() {
            lines.push(avg.report());
        }

        lines.join("\n")
    }
}

impl Default for QueryProfiler {
    fn default() -> Self {
        Self::new()
    }
}

/// Average statistics across multiple queries
#[derive(Debug, Clone)]
pub struct AverageStats {
    /// Number of queries
    pub query_count: usize,
    /// Average duration
    pub avg_duration: Duration,
    /// Average triples matched
    pub avg_triples_matched: f64,
    /// Average results count
    pub avg_results_count: f64,
    /// Average joins performed
    pub avg_joins_performed: f64,
    /// Average cache hit rate
    pub avg_cache_hit_rate: f64,
    /// Average peak memory
    pub avg_peak_memory: f64,
}

impl AverageStats {
    /// Generate report
    pub fn report(&self) -> String {
        format!(
            "Average Statistics:\n\
             - Queries: {}\n\
             - Duration: {:.3}s\n\
             - Triples Matched: {:.0}\n\
             - Results: {:.0}\n\
             - Joins: {:.1}\n\
             - Cache Hit Rate: {:.1}%\n\
             - Peak Memory: {:.2} MB",
            self.query_count,
            self.avg_duration.as_secs_f64(),
            self.avg_triples_matched,
            self.avg_results_count,
            self.avg_joins_performed,
            self.avg_cache_hit_rate * 100.0,
            self.avg_peak_memory / (1024.0 * 1024.0)
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_query_stats_creation() {
        let stats = QueryStats::new("test_query".to_string());
        assert_eq!(stats.query_id, "test_query");
        assert_eq!(stats.triples_matched, 0);
        assert_eq!(stats.results_count, 0);
    }

    #[test]
    fn test_query_phase_duration() {
        let mut stats = QueryStats::new("test".to_string());
        stats
            .phase_durations
            .insert(QueryPhase::Parsing, Duration::from_millis(100));
        stats
            .phase_durations
            .insert(QueryPhase::Execution, Duration::from_millis(500));

        assert_eq!(stats.phase_duration(QueryPhase::Parsing).as_millis(), 100);
        assert_eq!(stats.phase_duration(QueryPhase::Execution).as_millis(), 500);
        assert_eq!(stats.phase_duration(QueryPhase::Planning).as_millis(), 0);
    }

    #[test]
    fn test_profiler_basic() {
        let mut profiler = QueryProfiler::new();
        assert!(profiler.is_enabled());

        profiler.start_query("SELECT * WHERE { ?s ?p ?o }".to_string());
        thread::sleep(Duration::from_millis(10));

        profiler.record_triples(100);
        profiler.record_results(50);
        profiler.record_join();

        let stats = profiler.end_query().unwrap();
        assert_eq!(stats.triples_matched, 100);
        assert_eq!(stats.results_count, 50);
        assert_eq!(stats.joins_performed, 1);
        assert!(stats.total_duration.as_millis() >= 10);
    }

    #[test]
    fn test_profiler_phases() {
        let mut profiler = QueryProfiler::new();

        profiler.start_query("test".to_string());

        profiler.start_phase(QueryPhase::Parsing);
        thread::sleep(Duration::from_millis(10));
        profiler.end_phase(QueryPhase::Parsing);

        profiler.start_phase(QueryPhase::Execution);
        thread::sleep(Duration::from_millis(20));
        profiler.end_phase(QueryPhase::Execution);

        let stats = profiler.end_query().unwrap();
        assert!(stats.phase_duration(QueryPhase::Parsing).as_millis() >= 10);
        assert!(stats.phase_duration(QueryPhase::Execution).as_millis() >= 20);
    }

    #[test]
    fn test_disabled_profiler() {
        let mut profiler = QueryProfiler::disabled();
        assert!(!profiler.is_enabled());

        profiler.start_query("test".to_string());
        profiler.record_triples(100);

        let stats = profiler.end_query();
        assert!(stats.is_none());
    }

    #[test]
    fn test_profiler_history() {
        let mut profiler = QueryProfiler::new();

        for i in 0..5 {
            profiler.start_query(format!("query_{}", i));
            profiler.record_results(i * 10);
            profiler.end_query();
        }

        assert_eq!(profiler.history().len(), 5);
        assert_eq!(profiler.history()[0].results_count, 0);
        assert_eq!(profiler.history()[4].results_count, 40);
    }

    #[test]
    fn test_average_stats() {
        let mut profiler = QueryProfiler::new();

        for i in 1..=3 {
            profiler.start_query(format!("query_{}", i));
            profiler.record_triples(i * 100);
            profiler.record_results(i * 10);
            profiler.end_query();
        }

        let avg = profiler.average_stats().unwrap();
        assert_eq!(avg.query_count, 3);
        assert_eq!(avg.avg_triples_matched, 200.0); // (100 + 200 + 300) / 3
        assert_eq!(avg.avg_results_count, 20.0); // (10 + 20 + 30) / 3
    }

    #[test]
    fn test_throughput() {
        let mut stats = QueryStats::new("test".to_string());
        stats.results_count = 1000;
        stats.total_duration = Duration::from_secs(2);

        assert_eq!(stats.throughput(), 500.0);
    }

    #[test]
    fn test_cache_hit_rate_clamping() {
        let mut profiler = QueryProfiler::new();
        profiler.start_query("test".to_string());

        profiler.record_cache_hit_rate(1.5); // Should clamp to 1.0
        let stats = profiler.end_query().unwrap();
        assert_eq!(stats.cache_hit_rate, 1.0);
    }
}