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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! Interactive SPARQL Query Builder
//!
//! Provides a fluent, type-safe API for constructing SPARQL queries programmatically.
//! Supports SELECT, ASK, CONSTRUCT, DESCRIBE queries with comprehensive pattern building.

use crate::algebra::{
    Aggregate, Algebra, Expression, GroupCondition, Iri, Literal, OrderCondition, Term,
    TriplePattern, Variable,
};
use anyhow::{anyhow, Result};
use std::collections::HashMap;

/// Interactive query builder for SPARQL queries
#[derive(Debug, Clone)]
pub struct InteractiveQueryBuilder {
    query_type: Option<QueryType>,
    variables: Vec<Variable>,
    patterns: Vec<PatternBuilder>,
    filters: Vec<Expression>,
    optional_patterns: Vec<Vec<PatternBuilder>>,
    unions: Vec<Vec<PatternBuilder>>,
    bindings: Vec<(Variable, Expression)>,
    order_by: Vec<OrderCondition>,
    group_by: Vec<GroupCondition>,
    aggregates: Vec<(Variable, Aggregate)>,
    having: Option<Expression>,
    limit: Option<usize>,
    offset: Option<usize>,
    distinct: bool,
    reduced: bool,
    prefixes: HashMap<String, String>,
}

/// Query type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryType {
    Select,
    Ask,
    Construct,
    Describe,
}

/// Pattern builder for triple patterns
#[derive(Debug, Clone)]
pub struct PatternBuilder {
    subject: Option<Term>,
    predicate: Option<Term>,
    object: Option<Term>,
}

impl InteractiveQueryBuilder {
    /// Create a new query builder
    pub fn new() -> Self {
        Self {
            query_type: None,
            variables: Vec::new(),
            patterns: Vec::new(),
            filters: Vec::new(),
            optional_patterns: Vec::new(),
            unions: Vec::new(),
            bindings: Vec::new(),
            order_by: Vec::new(),
            group_by: Vec::new(),
            aggregates: Vec::new(),
            having: None,
            limit: None,
            offset: None,
            distinct: false,
            reduced: false,
            prefixes: HashMap::new(),
        }
    }

    /// Start building a SELECT query
    pub fn select(mut self, variables: Vec<&str>) -> Result<Self> {
        self.query_type = Some(QueryType::Select);
        self.variables = variables
            .into_iter()
            .map(Variable::new)
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(self)
    }

    /// Start building a SELECT * query
    pub fn select_all(mut self) -> Self {
        self.query_type = Some(QueryType::Select);
        self.variables = Vec::new(); // Empty means SELECT *
        self
    }

    /// Start building an ASK query
    pub fn ask(mut self) -> Self {
        self.query_type = Some(QueryType::Ask);
        self
    }

    /// Start building a CONSTRUCT query
    pub fn construct(mut self, variables: Vec<&str>) -> Result<Self> {
        self.query_type = Some(QueryType::Construct);
        self.variables = variables
            .into_iter()
            .map(Variable::new)
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(self)
    }

    /// Start building a DESCRIBE query
    pub fn describe(mut self, resources: Vec<&str>) -> Result<Self> {
        self.query_type = Some(QueryType::Describe);
        self.variables = resources
            .into_iter()
            .map(Variable::new)
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(self)
    }

    /// Add a WHERE pattern
    pub fn r#where(mut self, pattern: PatternBuilder) -> Self {
        self.patterns.push(pattern);
        self
    }

    /// Add multiple WHERE patterns
    pub fn where_all(mut self, patterns: Vec<PatternBuilder>) -> Self {
        self.patterns.extend(patterns);
        self
    }

    /// Add a FILTER condition
    pub fn filter(mut self, condition: Expression) -> Self {
        self.filters.push(condition);
        self
    }

    /// Add an OPTIONAL pattern
    pub fn optional(mut self, patterns: Vec<PatternBuilder>) -> Self {
        self.optional_patterns.push(patterns);
        self
    }

    /// Add a UNION pattern
    pub fn union(mut self, patterns: Vec<PatternBuilder>) -> Self {
        self.unions.push(patterns);
        self
    }

    /// Add a BIND clause
    pub fn bind(mut self, var: &str, expr: Expression) -> Result<Self> {
        self.bindings.push((Variable::new(var)?, expr));
        Ok(self)
    }

    /// Add ORDER BY clause
    pub fn order_by(mut self, conditions: Vec<OrderCondition>) -> Self {
        self.order_by = conditions;
        self
    }

    /// Add GROUP BY clause
    pub fn group_by(mut self, conditions: Vec<GroupCondition>) -> Self {
        self.group_by = conditions;
        self
    }

    /// Add HAVING clause
    pub fn having(mut self, condition: Expression) -> Self {
        self.having = Some(condition);
        self
    }

    /// Add COUNT aggregate
    pub fn count(mut self, var: &str, expr: Option<Expression>, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Count { distinct, expr }));
        Ok(self)
    }

    /// Add SUM aggregate
    pub fn sum(mut self, var: &str, expr: Expression, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Sum { distinct, expr }));
        Ok(self)
    }

    /// Add MIN aggregate
    pub fn min(mut self, var: &str, expr: Expression, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Min { distinct, expr }));
        Ok(self)
    }

    /// Add MAX aggregate
    pub fn max(mut self, var: &str, expr: Expression, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Max { distinct, expr }));
        Ok(self)
    }

    /// Add AVG aggregate
    pub fn avg(mut self, var: &str, expr: Expression, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Avg { distinct, expr }));
        Ok(self)
    }

    /// Add SAMPLE aggregate
    pub fn sample(mut self, var: &str, expr: Expression, distinct: bool) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates
            .push((variable, Aggregate::Sample { distinct, expr }));
        Ok(self)
    }

    /// Add GROUP_CONCAT aggregate
    pub fn group_concat(
        mut self,
        var: &str,
        expr: Expression,
        distinct: bool,
        separator: Option<String>,
    ) -> Result<Self> {
        let variable = Variable::new(var)?;
        self.aggregates.push((
            variable,
            Aggregate::GroupConcat {
                distinct,
                expr,
                separator,
            },
        ));
        Ok(self)
    }

    /// Set LIMIT
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set OFFSET
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Set DISTINCT modifier
    pub fn distinct(mut self) -> Self {
        self.distinct = true;
        self
    }

    /// Set REDUCED modifier
    pub fn reduced(mut self) -> Self {
        self.reduced = true;
        self
    }

    /// Add a prefix declaration
    pub fn prefix(mut self, prefix: &str, uri: &str) -> Self {
        self.prefixes.insert(prefix.to_string(), uri.to_string());
        self
    }

    /// Build the SPARQL query string
    pub fn build_string(&self) -> Result<String> {
        let mut query = String::new();

        // Add prefixes
        for (prefix, uri) in &self.prefixes {
            query.push_str(&format!("PREFIX {}: <{}>\n", prefix, uri));
        }
        if !self.prefixes.is_empty() {
            query.push('\n');
        }

        // Add query type
        let query_type = self
            .query_type
            .ok_or_else(|| anyhow!("Query type not set"))?;

        match query_type {
            QueryType::Select => {
                query.push_str("SELECT ");
                if self.distinct {
                    query.push_str("DISTINCT ");
                }
                if self.reduced {
                    query.push_str("REDUCED ");
                }
                if self.variables.is_empty() {
                    query.push('*');
                } else {
                    for (i, var) in self.variables.iter().enumerate() {
                        if i > 0 {
                            query.push(' ');
                        }
                        query.push_str(&format!("{}", var));
                    }
                }
                query.push('\n');
            }
            QueryType::Ask => {
                query.push_str("ASK\n");
            }
            QueryType::Construct => {
                query.push_str("CONSTRUCT {\n");
                for pattern in &self.patterns {
                    query.push_str(&format!("  {}\n", pattern.to_string()?));
                }
                query.push_str("}\n");
            }
            QueryType::Describe => {
                query.push_str("DESCRIBE ");
                for (i, var) in self.variables.iter().enumerate() {
                    if i > 0 {
                        query.push(' ');
                    }
                    query.push_str(&format!("{}", var));
                }
                query.push('\n');
            }
        }

        // Add WHERE clause
        query.push_str("WHERE {\n");

        // Add patterns
        for pattern in &self.patterns {
            query.push_str(&format!("  {} .\n", pattern.to_string()?));
        }

        // Add FILTERs
        for filter in &self.filters {
            query.push_str(&format!("  FILTER ({:?})\n", filter));
        }

        // Add OPTIONALs
        for optional in &self.optional_patterns {
            query.push_str("  OPTIONAL {\n");
            for pattern in optional {
                query.push_str(&format!("    {} .\n", pattern.to_string()?));
            }
            query.push_str("  }\n");
        }

        // Add UNIONs
        if !self.unions.is_empty() {
            query.push_str("  {\n");
            for (i, union_patterns) in self.unions.iter().enumerate() {
                if i > 0 {
                    query.push_str("  } UNION {\n");
                }
                for pattern in union_patterns {
                    query.push_str(&format!("    {} .\n", pattern.to_string()?));
                }
            }
            query.push_str("  }\n");
        }

        // Add BINDs
        for (var, expr) in &self.bindings {
            query.push_str(&format!("  BIND ({:?} AS {})\n", expr, var));
        }

        query.push_str("}\n");

        // Add GROUP BY
        if !self.group_by.is_empty() {
            query.push_str("GROUP BY ");
            for (i, condition) in self.group_by.iter().enumerate() {
                if i > 0 {
                    query.push(' ');
                }
                query.push_str(&format!("({:?})", condition.expr));
                if let Some(alias) = &condition.alias {
                    query.push_str(&format!(" AS {}", alias));
                }
            }
            query.push('\n');
        }

        // Add HAVING
        if let Some(having) = &self.having {
            query.push_str(&format!("HAVING ({:?})\n", having));
        }

        // Add ORDER BY
        if !self.order_by.is_empty() {
            query.push_str("ORDER BY ");
            for (i, condition) in self.order_by.iter().enumerate() {
                if i > 0 {
                    query.push(' ');
                }
                if condition.ascending {
                    query.push_str(&format!("ASC({:?})", condition.expr));
                } else {
                    query.push_str(&format!("DESC({:?})", condition.expr));
                }
            }
            query.push('\n');
        }

        // Add LIMIT
        if let Some(limit) = self.limit {
            query.push_str(&format!("LIMIT {}\n", limit));
        }

        // Add OFFSET
        if let Some(offset) = self.offset {
            query.push_str(&format!("OFFSET {}\n", offset));
        }

        Ok(query)
    }

    /// Build the algebra representation
    pub fn build_algebra(&self) -> Result<Algebra> {
        let query_type = self
            .query_type
            .ok_or_else(|| anyhow!("Query type not set"))?;

        // Build basic graph pattern from patterns
        let mut algebra = if !self.patterns.is_empty() {
            let triple_patterns: Result<Vec<TriplePattern>> = self
                .patterns
                .iter()
                .map(|p| p.to_triple_pattern())
                .collect();
            Algebra::Bgp(triple_patterns?)
        } else {
            Algebra::Bgp(Vec::new())
        };

        // Add filters
        for filter in &self.filters {
            algebra = Algebra::Filter {
                pattern: Box::new(algebra),
                condition: filter.clone(),
            };
        }

        // Add optionals
        for optional in &self.optional_patterns {
            let optional_patterns: Result<Vec<TriplePattern>> =
                optional.iter().map(|p| p.to_triple_pattern()).collect();
            let optional_algebra = Algebra::Bgp(optional_patterns?);
            algebra = Algebra::LeftJoin {
                left: Box::new(algebra),
                right: Box::new(optional_algebra),
                filter: None,
            };
        }

        // Add unions
        if !self.unions.is_empty() {
            let mut union_algebra = None;
            for union_patterns in &self.unions {
                let patterns: Result<Vec<TriplePattern>> = union_patterns
                    .iter()
                    .map(|p| p.to_triple_pattern())
                    .collect();
                let union_bgp = Algebra::Bgp(patterns?);
                union_algebra = Some(match union_algebra {
                    None => union_bgp,
                    Some(left) => Algebra::Union {
                        left: Box::new(left),
                        right: Box::new(union_bgp),
                    },
                });
            }
            if let Some(union_alg) = union_algebra {
                algebra = Algebra::Join {
                    left: Box::new(algebra),
                    right: Box::new(union_alg),
                };
            }
        }

        // Add bindings
        for (var, expr) in &self.bindings {
            algebra = Algebra::Extend {
                pattern: Box::new(algebra),
                variable: var.clone(),
                expr: expr.clone(),
            };
        }

        // Add group by
        if !self.group_by.is_empty() || !self.aggregates.is_empty() {
            algebra = Algebra::Group {
                pattern: Box::new(algebra),
                variables: self.group_by.clone(),
                aggregates: self.aggregates.clone(),
            };
        }

        // Add having
        if let Some(having) = &self.having {
            algebra = Algebra::Having {
                pattern: Box::new(algebra),
                condition: having.clone(),
            };
        }

        // Add order by
        if !self.order_by.is_empty() {
            algebra = Algebra::OrderBy {
                pattern: Box::new(algebra),
                conditions: self.order_by.clone(),
            };
        }

        // Add projection for SELECT
        if query_type == QueryType::Select && !self.variables.is_empty() {
            algebra = Algebra::Project {
                pattern: Box::new(algebra),
                variables: self.variables.clone(),
            };
        }

        // Add distinct
        if self.distinct {
            algebra = Algebra::Distinct {
                pattern: Box::new(algebra),
            };
        }

        // Add reduced
        if self.reduced {
            algebra = Algebra::Reduced {
                pattern: Box::new(algebra),
            };
        }

        // Add slice
        if self.limit.is_some() || self.offset.is_some() {
            algebra = Algebra::Slice {
                pattern: Box::new(algebra),
                offset: self.offset,
                limit: self.limit,
            };
        }

        Ok(algebra)
    }
}

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

impl PatternBuilder {
    /// Create a new pattern builder
    pub fn new() -> Self {
        Self {
            subject: None,
            predicate: None,
            object: None,
        }
    }

    /// Set the subject
    pub fn subject(mut self, subject: Term) -> Self {
        self.subject = Some(subject);
        self
    }

    /// Set the subject as a variable
    pub fn subject_var(self, var: &str) -> Result<Self> {
        Ok(self.subject(Term::Variable(Variable::new(var)?)))
    }

    /// Set the subject as an IRI
    pub fn subject_iri(self, iri: &str) -> Result<Self> {
        Ok(self.subject(Term::Iri(Iri::new(iri)?)))
    }

    /// Set the predicate
    pub fn predicate(mut self, predicate: Term) -> Self {
        self.predicate = Some(predicate);
        self
    }

    /// Set the predicate as a variable
    pub fn predicate_var(self, var: &str) -> Result<Self> {
        Ok(self.predicate(Term::Variable(Variable::new(var)?)))
    }

    /// Set the predicate as an IRI
    pub fn predicate_iri(self, iri: &str) -> Result<Self> {
        Ok(self.predicate(Term::Iri(Iri::new(iri)?)))
    }

    /// Set the object
    pub fn object(mut self, object: Term) -> Self {
        self.object = Some(object);
        self
    }

    /// Set the object as a variable
    pub fn object_var(self, var: &str) -> Result<Self> {
        Ok(self.object(Term::Variable(Variable::new(var)?)))
    }

    /// Set the object as an IRI
    pub fn object_iri(self, iri: &str) -> Result<Self> {
        Ok(self.object(Term::Iri(Iri::new(iri)?)))
    }

    /// Set the object as a literal
    pub fn object_literal(self, value: &str) -> Self {
        self.object(Term::Literal(Literal {
            value: value.to_string(),
            language: None,
            datatype: None,
        }))
    }

    /// Convert to triple pattern
    pub fn to_triple_pattern(&self) -> Result<TriplePattern> {
        Ok(TriplePattern {
            subject: self
                .subject
                .clone()
                .ok_or_else(|| anyhow!("Subject not set"))?,
            predicate: self
                .predicate
                .clone()
                .ok_or_else(|| anyhow!("Predicate not set"))?,
            object: self
                .object
                .clone()
                .ok_or_else(|| anyhow!("Object not set"))?,
        })
    }

    /// Convert to string representation
    pub fn to_string(&self) -> Result<String> {
        let subject = self
            .subject
            .as_ref()
            .ok_or_else(|| anyhow!("Subject not set"))?;
        let predicate = self
            .predicate
            .as_ref()
            .ok_or_else(|| anyhow!("Predicate not set"))?;
        let object = self
            .object
            .as_ref()
            .ok_or_else(|| anyhow!("Object not set"))?;

        Ok(format!("{} {} {}", subject, predicate, object))
    }
}

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

/// Helper functions for building common patterns
pub mod helpers {
    use super::*;

    /// Create a triple pattern: ?s ?p ?o
    pub fn triple(s: &str, p: &str, o: &str) -> Result<PatternBuilder> {
        PatternBuilder::new()
            .subject_var(s)?
            .predicate_var(p)?
            .object_var(o)
    }

    /// Create a pattern: `?s <predicate> ?o`
    pub fn triple_with_iri_predicate(s: &str, p: &str, o: &str) -> Result<PatternBuilder> {
        PatternBuilder::new()
            .subject_var(s)?
            .predicate_iri(p)?
            .object_var(o)
    }

    /// Create a pattern: `<subject> <predicate> ?o`
    pub fn triple_with_iri_subject_predicate(s: &str, p: &str, o: &str) -> Result<PatternBuilder> {
        PatternBuilder::new()
            .subject_iri(s)?
            .predicate_iri(p)?
            .object_var(o)
    }

    /// Create a pattern with literal object
    pub fn triple_with_literal(s: &str, p: &str, literal: &str) -> Result<PatternBuilder> {
        Ok(PatternBuilder::new()
            .subject_var(s)?
            .predicate_iri(p)?
            .object_literal(literal))
    }
}

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

    #[test]
    fn test_simple_select() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .select(vec!["s", "p", "o"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .build_string()?;

        assert!(query.contains("SELECT ?s ?p ?o"));
        assert!(query.contains("WHERE"));
        Ok(())
    }

    #[test]
    fn test_select_with_filter() -> Result<()> {
        let builder = InteractiveQueryBuilder::new()
            .select(vec!["name"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("person")?
                    .predicate_iri("http://xmlns.com/foaf/0.1/name")?
                    .object_var("name")?,
            )
            .limit(10);

        let query = builder.build_string()?;
        assert!(query.contains("SELECT ?name"));
        assert!(query.contains("LIMIT 10"));
        Ok(())
    }

    #[test]
    fn test_ask_query() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .ask()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .build_string()?;

        assert!(query.contains("ASK"));
        assert!(query.contains("WHERE"));
        Ok(())
    }

    #[test]
    fn test_distinct_modifier() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .select(vec!["s"])?
            .distinct()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .build_string()?;

        assert!(query.contains("SELECT DISTINCT"));
        Ok(())
    }

    #[test]
    fn test_with_prefixes() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .prefix("foaf", "http://xmlns.com/foaf/0.1/")
            .prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
            .select(vec!["name"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("person")?
                    .predicate_iri("http://xmlns.com/foaf/0.1/name")?
                    .object_var("name")?,
            )
            .build_string()?;

        assert!(query.contains("PREFIX foaf:"));
        assert!(query.contains("PREFIX rdf:"));
        Ok(())
    }

    #[test]
    fn test_algebra_generation() -> Result<()> {
        let algebra = InteractiveQueryBuilder::new()
            .select(vec!["s", "p", "o"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .limit(10)
            .build_algebra()?;

        // Should have Slice > Project > Bgp structure
        match algebra {
            Algebra::Slice { .. } => {}
            _ => panic!("Expected Slice algebra"),
        }

        Ok(())
    }

    #[test]
    fn test_helper_functions() -> Result<()> {
        let pattern = helpers::triple("s", "p", "o")?;
        let triple = pattern.to_triple_pattern()?;

        assert!(matches!(triple.subject, Term::Variable(_)));
        assert!(matches!(triple.predicate, Term::Variable(_)));
        assert!(matches!(triple.object, Term::Variable(_)));

        Ok(())
    }

    #[test]
    fn test_optional_pattern() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .select(vec!["name", "email"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("person")?
                    .predicate_iri("http://xmlns.com/foaf/0.1/name")?
                    .object_var("name")?,
            )
            .optional(vec![PatternBuilder::new()
                .subject_var("person")?
                .predicate_iri("http://xmlns.com/foaf/0.1/mbox")?
                .object_var("email")?])
            .build_string()?;

        assert!(query.contains("OPTIONAL"));
        Ok(())
    }

    #[test]
    fn test_limit_offset() -> Result<()> {
        let query = InteractiveQueryBuilder::new()
            .select(vec!["s"])?
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .limit(10)
            .offset(20)
            .build_string()?;

        assert!(query.contains("LIMIT 10"));
        assert!(query.contains("OFFSET 20"));
        Ok(())
    }

    #[test]
    fn test_count_aggregate() -> Result<()> {
        let algebra = InteractiveQueryBuilder::new()
            .select_all()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .count("count", None, false)?
            .build_algebra()?;

        // Check that aggregate is in the algebra
        match algebra {
            Algebra::Group { aggregates, .. } => {
                assert_eq!(aggregates.len(), 1);
                let (var, agg) = &aggregates[0];
                assert_eq!(var.name(), "count");
                matches!(agg, Aggregate::Count { .. });
            }
            _ => panic!("Expected Group algebra"),
        }

        Ok(())
    }

    #[test]
    fn test_sum_aggregate() -> Result<()> {
        let algebra = InteractiveQueryBuilder::new()
            .select_all()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .sum(
                "total",
                Expression::Variable(Variable::new_unchecked("value")),
                false,
            )?
            .build_algebra()?;

        match algebra {
            Algebra::Group { aggregates, .. } => {
                assert_eq!(aggregates.len(), 1);
                let (var, agg) = &aggregates[0];
                assert_eq!(var.name(), "total");
                matches!(agg, Aggregate::Sum { .. });
            }
            _ => panic!("Expected Group algebra"),
        }

        Ok(())
    }

    #[test]
    fn test_multiple_aggregates() -> Result<()> {
        let algebra = InteractiveQueryBuilder::new()
            .select_all()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .count("cnt", None, false)?
            .sum(
                "total",
                Expression::Variable(Variable::new_unchecked("value")),
                false,
            )?
            .avg(
                "avg",
                Expression::Variable(Variable::new_unchecked("value")),
                false,
            )?
            .build_algebra()?;

        match algebra {
            Algebra::Group { aggregates, .. } => {
                assert_eq!(aggregates.len(), 3);
                assert_eq!(aggregates[0].0.name(), "cnt");
                assert_eq!(aggregates[1].0.name(), "total");
                assert_eq!(aggregates[2].0.name(), "avg");
            }
            _ => panic!("Expected Group algebra"),
        }

        Ok(())
    }

    #[test]
    fn test_group_concat_aggregate() -> Result<()> {
        let algebra = InteractiveQueryBuilder::new()
            .select_all()
            .r#where(
                PatternBuilder::new()
                    .subject_var("s")?
                    .predicate_var("p")?
                    .object_var("o")?,
            )
            .group_concat(
                "concat",
                Expression::Variable(Variable::new_unchecked("name")),
                false,
                Some(", ".to_string()),
            )?
            .build_algebra()?;

        match algebra {
            Algebra::Group { aggregates, .. } => {
                assert_eq!(aggregates.len(), 1);
                match &aggregates[0].1 {
                    Aggregate::GroupConcat { separator, .. } => {
                        assert_eq!(separator.as_deref(), Some(", "));
                    }
                    _ => panic!("Expected GroupConcat aggregate"),
                }
            }
            _ => panic!("Expected Group algebra"),
        }

        Ok(())
    }
}