oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Query EXPLAIN/ANALYZE command
//!
//! Provides query optimization insights, execution plans, and performance analysis

use anyhow::{Context, Result};
use oxirs_arq::query::parse_query;
use std::fmt::Write as FmtWrite;
use std::path::{Path, PathBuf};
use std::time::Instant;

/// Query analysis mode
#[derive(Debug, Clone, Copy)]
pub enum AnalysisMode {
    /// Show query plan only (no execution)
    Explain,
    /// Execute query and show performance metrics
    Analyze,
    /// Show both plan and execution metrics
    Full,
}

/// Query execution statistics
#[derive(Debug)]
pub struct QueryStats {
    pub parse_time_ms: f64,
    pub optimization_time_ms: f64,
    pub execution_time_ms: f64,
    pub total_time_ms: f64,
    pub result_count: usize,
    pub intermediate_results: usize,
    pub memory_used_mb: f64,
}

/// Explain query execution plan
pub async fn explain_query(
    dataset: String,
    query: String,
    is_file: bool,
    mode: AnalysisMode,
) -> Result<()> {
    explain_query_with_options(dataset, query, is_file, mode, None).await
}

/// Explain query execution plan with optional graphical output
pub async fn explain_query_with_options(
    dataset: String,
    query: String,
    is_file: bool,
    mode: AnalysisMode,
    graphviz_output: Option<PathBuf>,
) -> Result<()> {
    let total_start = Instant::now();

    // Read query from file if needed
    let query_str = if is_file {
        std::fs::read_to_string(&query)
            .with_context(|| format!("Failed to read query file: {}", query))?
    } else {
        query
    };

    println!("🔍 Query Analysis Mode: {:?}\n", mode);
    println!("📊 Query:\n{}\n", query_str);
    println!("{}\n", "=".repeat(80));

    // Parse query
    let parse_start = Instant::now();
    let parsed_query = parse_query(&query_str).with_context(|| "Failed to parse SPARQL query")?;
    let parse_time = parse_start.elapsed();

    println!("✅ Query Type: {:?}", parsed_query.query_type);
    println!(
        "⏱️  Parse Time: {:.3}ms\n",
        parse_time.as_secs_f64() * 1000.0
    );

    // Show query structure
    println!("📋 Query Structure:");
    println!("  Variables: {:?}", parsed_query.select_variables);
    println!("  Distinct: {}", parsed_query.distinct);
    println!("  Order By: {} clauses", parsed_query.order_by.len());
    println!("  Group By: {} clauses", parsed_query.group_by.len());
    if let Some(limit) = parsed_query.limit {
        println!("  Limit: {}", limit);
    }
    if let Some(offset) = parsed_query.offset {
        println!("  Offset: {}", offset);
    }
    println!();

    // Analyze algebra
    println!("🧮 Query Algebra:");
    println!("{:#?}\n", parsed_query.where_clause);

    // Estimate query complexity
    let complexity = estimate_complexity(&parsed_query.where_clause);
    println!("📈 Estimated Complexity:");
    println!("  Complexity Score: {}", complexity.score);
    println!("  Join Operations: {}", complexity.joins);
    println!("  Filters: {}", complexity.filters);
    println!("  Optional Patterns: {}", complexity.optionals);
    println!("  Unions: {}", complexity.unions);
    println!();

    // Show optimization opportunities
    show_optimization_hints(&parsed_query, &complexity);

    // Generate graphical query plan if requested
    if let Some(ref output_path) = graphviz_output {
        println!("{}\n", "=".repeat(80));
        println!("📊 Generating Graphical Query Plan\n");

        let dot_content = generate_query_plan_dot(&parsed_query, &complexity)?;
        std::fs::write(output_path, &dot_content)?;

        println!(
            "✅ Query plan visualization saved to: {}",
            output_path.display()
        );
        println!(
            "   Render with: dot -Tpng {} -o {}.png",
            output_path.display(),
            output_path.with_extension("").display()
        );
        println!("   Or view interactively: xdot {}\n", output_path.display());
    }

    // Execute query if requested
    if matches!(mode, AnalysisMode::Analyze | AnalysisMode::Full) {
        println!("{}\n", "=".repeat(80));
        println!("⚡ Query Execution Analysis\n");

        let _dataset_path = Path::new(&dataset);

        // Note: Full query execution with timing will be implemented in Beta.1
        // For now, we provide the query plan and optimization analysis
        println!("💡 Query execution with detailed timing will be available in Beta.1");
        println!("   For now, use 'oxirs query' to execute queries");
        println!("   This command focuses on query optimization analysis");
        println!();

        let parse_and_analysis_time = total_start.elapsed();
        println!(
            "⏱️  Analysis Time: {:.3}ms",
            parse_and_analysis_time.as_secs_f64() * 1000.0
        );
        println!();
    }

    Ok(())
}

/// Complexity metrics
#[derive(Debug, Default)]
struct ComplexityMetrics {
    score: usize,
    joins: usize,
    filters: usize,
    optionals: usize,
    unions: usize,
}

/// Estimate query complexity
fn estimate_complexity(algebra: &oxirs_arq::Algebra) -> ComplexityMetrics {
    use oxirs_arq::Algebra;

    let mut metrics = ComplexityMetrics::default();

    match algebra {
        Algebra::Bgp(patterns) => {
            metrics.score = patterns.len();
        }
        Algebra::Join { left, right } => {
            metrics.joins += 1;
            metrics.score += 2;
            let left_metrics = estimate_complexity(left);
            let right_metrics = estimate_complexity(right);
            metrics.score += left_metrics.score + right_metrics.score;
            metrics.joins += left_metrics.joins + right_metrics.joins;
            metrics.filters += left_metrics.filters + right_metrics.filters;
            metrics.optionals += left_metrics.optionals + right_metrics.optionals;
            metrics.unions += left_metrics.unions + right_metrics.unions;
        }
        Algebra::LeftJoin { left, right, .. } => {
            metrics.optionals += 1;
            metrics.score += 3;
            let left_metrics = estimate_complexity(left);
            let right_metrics = estimate_complexity(right);
            metrics.score += left_metrics.score + right_metrics.score;
            metrics.joins += left_metrics.joins + right_metrics.joins;
            metrics.filters += left_metrics.filters + right_metrics.filters;
            metrics.optionals += left_metrics.optionals + right_metrics.optionals;
            metrics.unions += left_metrics.unions + right_metrics.unions;
        }
        Algebra::Union { left, right } => {
            metrics.unions += 1;
            metrics.score += 2;
            let left_metrics = estimate_complexity(left);
            let right_metrics = estimate_complexity(right);
            metrics.score += left_metrics.score + right_metrics.score;
            metrics.joins += left_metrics.joins + right_metrics.joins;
            metrics.filters += left_metrics.filters + right_metrics.filters;
            metrics.optionals += left_metrics.optionals + right_metrics.optionals;
            metrics.unions += left_metrics.unions + right_metrics.unions;
        }
        Algebra::Filter { pattern, .. } => {
            metrics.filters += 1;
            metrics.score += 1;
            let inner = estimate_complexity(pattern);
            metrics.score += inner.score;
            metrics.joins += inner.joins;
            metrics.filters += inner.filters;
            metrics.optionals += inner.optionals;
            metrics.unions += inner.unions;
        }
        _ => {
            metrics.score = 1;
        }
    }

    metrics
}

/// Show optimization hints
fn show_optimization_hints(query: &oxirs_arq::query::Query, complexity: &ComplexityMetrics) {
    println!("💡 Optimization Hints:");

    let mut hints = Vec::new();

    // Check for missing LIMIT
    if query.limit.is_none() && complexity.score > 5 {
        hints.push("Consider adding LIMIT clause to reduce result size");
    }

    // Check for multiple filters
    if complexity.filters > 3 {
        hints.push("Multiple FILTER clauses detected - consider combining with && operator");
    }

    // Check for OPTIONAL without filter
    if complexity.optionals > 0 {
        hints.push("OPTIONAL patterns can be expensive - ensure they are necessary");
    }

    // Check for UNION
    if complexity.unions > 2 {
        hints.push("Multiple UNION operations - consider rewriting as a single BGP if possible");
    }

    // Check for complex joins
    if complexity.joins > 4 {
        hints.push("Many JOIN operations - ensure join order is optimal");
        hints.push("Consider adding more specific triple patterns to reduce intermediate results");
    }

    // Check for DISTINCT
    if query.distinct && complexity.score > 10 {
        hints.push("DISTINCT on complex query - this may be expensive");
    }

    // Check for GROUP BY without aggregate
    if !query.group_by.is_empty() {
        hints.push("GROUP BY detected - ensure aggregates are used efficiently");
    }

    if hints.is_empty() {
        println!("  ✅ Query appears well-optimized");
    } else {
        for (i, hint) in hints.iter().enumerate() {
            println!("  {}. {}", i + 1, hint);
        }
    }
    println!();
}

/// Generate Graphviz DOT format for query plan visualization
fn generate_query_plan_dot(
    query: &oxirs_arq::query::Query,
    complexity: &ComplexityMetrics,
) -> Result<String> {
    let mut dot = String::new();
    writeln!(dot, "digraph QueryPlan {{")?;
    writeln!(dot, "  rankdir=BT;")?;
    writeln!(
        dot,
        "  node [shape=box, style=\"rounded,filled\", fontname=\"Arial\"];"
    )?;
    writeln!(dot, "  edge [fontname=\"Arial\", fontsize=10];")?;
    writeln!(dot)?;

    // Add title with complexity metrics
    writeln!(dot, "  labelloc=\"t\";")?;
    writeln!(
        dot,
        "  label=<Query Plan<BR/><FONT POINT-SIZE=\"10\">Complexity: {} | Joins: {} | Filters: {} | Optionals: {}</FONT>>;",
        complexity.score, complexity.joins, complexity.filters, complexity.optionals
    )?;
    writeln!(dot)?;

    // Node counter for unique IDs
    let mut node_id = 0;

    // Generate root node
    let root_id = node_id;
    node_id += 1;

    let query_type_str = format!("{:?}", query.query_type);
    writeln!(
        dot,
        "  n{} [label=\"{}\", fillcolor=\"#90EE90\"];",
        root_id, query_type_str
    )?;

    // Add modifiers
    if query.distinct {
        let distinct_id = node_id;
        node_id += 1;
        writeln!(
            dot,
            "  n{} [label=\"DISTINCT\", fillcolor=\"#FFD700\"];",
            distinct_id
        )?;
        writeln!(dot, "  n{} -> n{};", distinct_id, root_id)?;
    }

    if query.limit.is_some() || query.offset.is_some() {
        let limit_id = node_id;
        node_id += 1;
        let label = match (query.limit, query.offset) {
            (Some(l), Some(o)) => format!("LIMIT {} OFFSET {}", l, o),
            (Some(l), None) => format!("LIMIT {}", l),
            (None, Some(o)) => format!("OFFSET {}", o),
            _ => String::new(),
        };
        writeln!(
            dot,
            "  n{} [label=\"{}\", fillcolor=\"#FFD700\"];",
            limit_id, label
        )?;
        writeln!(dot, "  n{} -> n{};", limit_id, root_id)?;
    }

    // Add ORDER BY if present
    if !query.order_by.is_empty() {
        let order_id = node_id;
        node_id += 1;
        writeln!(
            dot,
            "  n{} [label=\"ORDER BY ({})\", fillcolor=\"#FFD700\"];",
            order_id,
            query.order_by.len()
        )?;
        writeln!(dot, "  n{} -> n{};", order_id, root_id)?;
    }

    // Add GROUP BY if present
    if !query.group_by.is_empty() {
        let group_id = node_id;
        node_id += 1;
        writeln!(
            dot,
            "  n{} [label=\"GROUP BY ({})\", fillcolor=\"#FFD700\"];",
            group_id,
            query.group_by.len()
        )?;
        writeln!(dot, "  n{} -> n{};", group_id, root_id)?;
    }

    // Generate algebra tree
    let algebra_root = generate_algebra_nodes(&query.where_clause, &mut node_id, &mut dot)?;

    // Connect algebra to query root
    writeln!(dot, "  n{} -> n{};", algebra_root, root_id)?;

    writeln!(dot)?;
    writeln!(dot, "}}")?;

    Ok(dot)
}

/// Generate nodes for algebra tree (recursive)
fn generate_algebra_nodes(
    algebra: &oxirs_arq::Algebra,
    node_id: &mut usize,
    dot: &mut String,
) -> Result<usize> {
    use oxirs_arq::Algebra;

    let current_id = *node_id;
    *node_id += 1;

    match algebra {
        Algebra::Bgp(patterns) => {
            writeln!(
                dot,
                "  n{} [label=\"BGP\\n({} patterns)\", fillcolor=\"#87CEEB\"];",
                current_id,
                patterns.len()
            )?;

            // Add pattern details
            for (i, pattern) in patterns.iter().enumerate().take(3) {
                let pattern_id = *node_id;
                *node_id += 1;
                let label = format!("Pattern {}: {:?}", i + 1, pattern);
                let truncated = if label.len() > 50 {
                    format!("{}...", &label[..50])
                } else {
                    label
                };
                writeln!(
                    dot,
                    "  n{} [label=\"{}\", fillcolor=\"#E0FFFF\", shape=ellipse];",
                    pattern_id,
                    escape_dot_label(&truncated)
                )?;
                writeln!(dot, "  n{} -> n{};", current_id, pattern_id)?;
            }

            if patterns.len() > 3 {
                let more_id = *node_id;
                *node_id += 1;
                writeln!(
                    dot,
                    "  n{} [label=\"... {} more\", fillcolor=\"#E0FFFF\", shape=ellipse];",
                    more_id,
                    patterns.len() - 3
                )?;
                writeln!(dot, "  n{} -> n{};", current_id, more_id)?;
            }
        }
        Algebra::Join { left, right } => {
            writeln!(
                dot,
                "  n{} [label=\"JOIN\", fillcolor=\"#FFA07A\"];",
                current_id
            )?;

            let left_id = generate_algebra_nodes(left, node_id, dot)?;
            let right_id = generate_algebra_nodes(right, node_id, dot)?;

            writeln!(dot, "  n{} -> n{} [label=\"left\"];", current_id, left_id)?;
            writeln!(dot, "  n{} -> n{} [label=\"right\"];", current_id, right_id)?;
        }
        Algebra::LeftJoin { left, right, .. } => {
            writeln!(
                dot,
                "  n{} [label=\"LEFT JOIN\\n(OPTIONAL)\", fillcolor=\"#FFB6C1\"];",
                current_id
            )?;

            let left_id = generate_algebra_nodes(left, node_id, dot)?;
            let right_id = generate_algebra_nodes(right, node_id, dot)?;

            writeln!(dot, "  n{} -> n{} [label=\"left\"];", current_id, left_id)?;
            writeln!(
                dot,
                "  n{} -> n{} [label=\"optional\"];",
                current_id, right_id
            )?;
        }
        Algebra::Union { left, right } => {
            writeln!(
                dot,
                "  n{} [label=\"UNION\", fillcolor=\"#DDA0DD\"];",
                current_id
            )?;

            let left_id = generate_algebra_nodes(left, node_id, dot)?;
            let right_id = generate_algebra_nodes(right, node_id, dot)?;

            writeln!(dot, "  n{} -> n{} [label=\"left\"];", current_id, left_id)?;
            writeln!(dot, "  n{} -> n{} [label=\"right\"];", current_id, right_id)?;
        }
        Algebra::Filter { pattern, .. } => {
            writeln!(
                dot,
                "  n{} [label=\"FILTER\", fillcolor=\"#F0E68C\"];",
                current_id
            )?;

            let inner_id = generate_algebra_nodes(pattern, node_id, dot)?;
            writeln!(dot, "  n{} -> n{};", current_id, inner_id)?;
        }
        Algebra::Graph { graph, pattern } => {
            let graph_str = format!("{:?}", graph);
            let truncated = if graph_str.len() > 30 {
                format!("{}...", &graph_str[..30])
            } else {
                graph_str
            };

            writeln!(
                dot,
                "  n{} [label=\"GRAPH\\n{}\", fillcolor=\"#98FB98\"];",
                current_id,
                escape_dot_label(&truncated)
            )?;

            let inner_id = generate_algebra_nodes(pattern, node_id, dot)?;
            writeln!(dot, "  n{} -> n{};", current_id, inner_id)?;
        }
        Algebra::Extend { pattern, .. } => {
            writeln!(
                dot,
                "  n{} [label=\"EXTEND\", fillcolor=\"#FFDAB9\"];",
                current_id
            )?;

            let inner_id = generate_algebra_nodes(pattern, node_id, dot)?;
            writeln!(dot, "  n{} -> n{};", current_id, inner_id)?;
        }
        Algebra::Minus { left, right } => {
            writeln!(
                dot,
                "  n{} [label=\"MINUS\", fillcolor=\"#F08080\"];",
                current_id
            )?;

            let left_id = generate_algebra_nodes(left, node_id, dot)?;
            let right_id = generate_algebra_nodes(right, node_id, dot)?;

            writeln!(dot, "  n{} -> n{} [label=\"left\"];", current_id, left_id)?;
            writeln!(dot, "  n{} -> n{} [label=\"minus\"];", current_id, right_id)?;
        }
        Algebra::Service {
            endpoint, pattern, ..
        } => {
            let endpoint_str = format!("{:?}", endpoint);
            let truncated = if endpoint_str.len() > 30 {
                format!("{}...", &endpoint_str[..30])
            } else {
                endpoint_str
            };

            writeln!(
                dot,
                "  n{} [label=\"SERVICE\\n{}\", fillcolor=\"#AFEEEE\"];",
                current_id,
                escape_dot_label(&truncated)
            )?;

            let inner_id = generate_algebra_nodes(pattern, node_id, dot)?;
            writeln!(dot, "  n{} -> n{};", current_id, inner_id)?;
        }
        _ => {
            writeln!(
                dot,
                "  n{} [label=\"{:?}\", fillcolor=\"#D3D3D3\"];",
                current_id,
                format!("{:?}", algebra)
                    .split('(')
                    .next()
                    .unwrap_or("Unknown")
            )?;
        }
    }

    Ok(current_id)
}

/// Escape special characters for DOT label
fn escape_dot_label(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\n', "\\n")
}

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

    #[test]
    fn test_generate_query_plan_dot_basic() {
        let query_str = "SELECT ?s ?p ?o WHERE { ?s ?p ?o }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        let dot = generate_query_plan_dot(&query, &complexity).expect("Failed to generate DOT");

        // Print for debugging
        println!("Generated DOT:\n{}", dot);

        // Verify basic structure
        assert!(
            dot.contains("digraph QueryPlan"),
            "Should contain digraph QueryPlan"
        );
        assert!(dot.contains("rankdir=BT"), "Should contain rankdir=BT");
        assert!(dot.contains("BGP"), "Should contain BGP");
        // Query type is Debug format, could be "Select" not "SELECT"
        assert!(
            dot.contains("Select") || dot.contains("SELECT"),
            "Should contain Select/SELECT"
        );
    }

    #[test]
    fn test_generate_query_plan_dot_with_join() {
        let query_str =
            "SELECT ?s WHERE { ?s <http://example.org/p1> ?o1 . ?s <http://example.org/p2> ?o2 }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        let dot = generate_query_plan_dot(&query, &complexity).expect("Failed to generate DOT");

        // Should contain complexity metrics
        assert!(
            dot.contains("Complexity:"),
            "Should contain complexity metrics"
        );
        assert!(
            dot.contains("Select") || dot.contains("SELECT"),
            "Should contain Select"
        );
        assert!(dot.contains("BGP"), "Should contain BGP");
    }

    #[test]
    fn test_generate_query_plan_dot_with_filter() {
        let query_str = "SELECT ?s WHERE { ?s ?p ?o FILTER(?o > 10) }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        let dot = generate_query_plan_dot(&query, &complexity).expect("Failed to generate DOT");

        // Should mention filter in complexity metrics
        assert!(
            dot.contains("Filters:"),
            "Should mention Filters in metrics"
        );
        assert!(dot.contains("digraph QueryPlan"), "Should contain digraph");
        // Check for FILTER node
        assert!(
            dot.contains("FILTER") || complexity.filters > 0,
            "Should have filter node or complexity"
        );
    }

    #[test]
    fn test_generate_query_plan_dot_with_optional() {
        let query_str =
            "SELECT ?s WHERE { ?s ?p ?o OPTIONAL { ?s <http://example.org/name> ?name } }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        let dot = generate_query_plan_dot(&query, &complexity).expect("Failed to generate DOT");

        // Should mention optionals in complexity metrics
        assert!(
            dot.contains("Optionals:"),
            "Should mention Optionals in metrics"
        );
        assert!(dot.contains("digraph QueryPlan"), "Should contain digraph");
        // Check that optional pattern is detected
        assert!(complexity.optionals > 0, "Should detect optional patterns");
    }

    #[test]
    fn test_generate_query_plan_dot_with_modifiers() {
        // Test with DISTINCT which is supported by the parser
        let query_str = "SELECT DISTINCT ?s WHERE { ?s ?p ?o }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        let dot = generate_query_plan_dot(&query, &complexity).expect("Failed to generate DOT");

        // Should contain DISTINCT modifier
        assert!(dot.contains("DISTINCT"), "Should contain DISTINCT node");
        assert!(dot.contains("Select"), "Should contain Select node");
        assert!(dot.contains("BGP"), "Should contain BGP node");
        assert!(
            dot.contains("digraph QueryPlan"),
            "Should be valid DOT format"
        );
    }

    #[test]
    fn test_escape_dot_label() {
        assert_eq!(escape_dot_label("test"), "test");
        assert_eq!(escape_dot_label("test & test"), "test &amp; test");
        assert_eq!(escape_dot_label("test < test"), "test &lt; test");
        assert_eq!(escape_dot_label("test > test"), "test &gt; test");
        assert_eq!(escape_dot_label("test \"quote\""), "test &quot;quote&quot;");
        assert_eq!(escape_dot_label("test\nline"), "test\\nline");
    }

    #[test]
    fn test_complexity_estimation_bgp() {
        let query_str = "SELECT ?s ?p ?o WHERE { ?s ?p ?o }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        assert_eq!(complexity.score, 1);
        assert_eq!(complexity.joins, 0);
        assert_eq!(complexity.filters, 0);
        assert_eq!(complexity.optionals, 0);
    }

    #[test]
    fn test_complexity_estimation_with_filter() {
        let query_str = "SELECT ?s WHERE { ?s ?p ?o FILTER(?o > 10) }";
        let query = parse_query(query_str).expect("Failed to parse query");
        let complexity = estimate_complexity(&query.where_clause);

        assert!(complexity.filters > 0, "Should detect filter");
        assert!(
            complexity.score > 1,
            "Filtered queries should have higher complexity"
        );
    }
}