grafeo-engine 0.5.38

Query engine and database management for Grafeo
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
//! GraphQL to RDF LogicalPlan translator.
//!
//! Translates GraphQL queries to the common logical plan representation for RDF.
//!
//! # Mapping Strategy
//!
//! GraphQL's hierarchical structure maps to RDF triple patterns:
//! - Root fields → Triple patterns with `rdf:type` predicate
//! - Field arguments → Additional triple patterns for filtering
//! - Nested selections → Predicate-object traversals
//! - Scalar fields → Select variables from triple bindings

use super::common::{
    VarGen, capitalize_first, graphql_directives_allow, wrap_filter, wrap_limit, wrap_skip,
};
use crate::query::plan::{
    BinaryOp, CountExpr, JoinOp, JoinType, LogicalExpression, LogicalOperator, LogicalPlan,
    ProjectOp, Projection, TripleComponent, TripleScanOp, UnionOp,
};
use grafeo_adapters::query::graphql::{self, ast};
use grafeo_common::utils::error::{Error, QueryError, QueryErrorKind, Result};
use std::collections::HashMap;

/// RDF namespace constants.
const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";

/// Translates a GraphQL query string to an RDF logical plan.
///
/// # Errors
///
/// Returns an error if the query cannot be parsed or translated.
pub fn translate(query: &str, namespace: &str) -> Result<LogicalPlan> {
    let doc = graphql::parse(query)?;
    let translator = GraphQLRdfTranslator::new(namespace);
    translator.translate_document(&doc)
}

/// Translator from GraphQL AST to RDF LogicalPlan.
struct GraphQLRdfTranslator {
    /// Generator for anonymous variable names.
    var_gen: VarGen,
    /// Base namespace for type IRIs.
    namespace: String,
    /// Fragment definitions for resolution.
    fragments: HashMap<String, ast::FragmentDefinition>,
    /// Default values from variable declarations (e.g., `query($limit: Int = 2)`).
    variable_defaults: HashMap<String, grafeo_common::types::Value>,
}

impl GraphQLRdfTranslator {
    fn new(namespace: &str) -> Self {
        Self {
            var_gen: VarGen::new(),
            namespace: namespace.to_string(),
            fragments: HashMap::new(),
            variable_defaults: HashMap::new(),
        }
    }

    fn translate_document(&self, doc: &ast::Document) -> Result<LogicalPlan> {
        // First, collect all fragment definitions
        let mut fragments = HashMap::new();
        for def in &doc.definitions {
            if let ast::Definition::Fragment(frag) = def {
                fragments.insert(frag.name.clone(), frag.clone());
            }
        }

        // Find the first operation
        let operation = doc
            .definitions
            .iter()
            .find_map(|def| match def {
                ast::Definition::Operation(op) => Some(op),
                _ => None,
            })
            .ok_or_else(|| {
                Error::Query(QueryError::new(
                    QueryErrorKind::Syntax,
                    "No operation found in document",
                ))
            })?;

        // Only Query operations are supported
        if operation.operation != ast::OperationType::Query {
            return Err(Error::Query(QueryError::new(
                QueryErrorKind::Semantic,
                "Only Query operations are supported for RDF",
            )));
        }

        // Collect default values from variable declarations
        let variable_defaults: HashMap<String, grafeo_common::types::Value> = operation
            .variables
            .iter()
            .filter_map(|v| {
                v.default_value
                    .as_ref()
                    .map(|dv| (v.name.clone(), dv.clone()))
            })
            .collect();

        // Create translator with fragments and variable defaults
        let translator = GraphQLRdfTranslator {
            var_gen: VarGen::new(),
            namespace: self.namespace.clone(),
            fragments,
            variable_defaults,
        };

        translator.translate_operation(operation)
    }

    fn translate_operation(&self, op: &ast::OperationDefinition) -> Result<LogicalPlan> {
        // Each field in the root selection set is a separate query
        let selections = &op.selection_set.selections;
        if selections.is_empty() {
            return Err(Error::Query(QueryError::new(
                QueryErrorKind::Syntax,
                "Empty selection set",
            )));
        }

        // Collect all root fields from the selection set
        let root_fields: Vec<&ast::Field> = selections
            .iter()
            .filter_map(|sel| {
                if let ast::Selection::Field(field) = sel {
                    Some(field)
                } else {
                    None
                }
            })
            .collect();

        if root_fields.is_empty() {
            return Err(Error::Query(QueryError::new(
                QueryErrorKind::Syntax,
                "No field found in selection set",
            )));
        }

        // Translate each root field independently
        let mut plans: Vec<LogicalOperator> = Vec::with_capacity(root_fields.len());
        for field in &root_fields {
            plans.push(self.translate_root_field(field)?);
        }

        // If there is only one root field, use it directly; otherwise union them
        let root = if plans.len() == 1 {
            plans.remove(0)
        } else {
            LogicalOperator::Union(UnionOp { inputs: plans })
        };

        let mut logical_plan = LogicalPlan::new(root);
        logical_plan
            .default_params
            .clone_from(&self.variable_defaults);
        Ok(logical_plan)
    }

    fn translate_root_field(&self, field: &ast::Field) -> Result<LogicalOperator> {
        // Root field name becomes the RDF type
        let subject_var = self.var_gen.next();
        let type_iri = self.make_type_iri(&field.name);

        // Create triple pattern: ?subject rdf:type <Type>
        let mut plan = LogicalOperator::TripleScan(TripleScanOp {
            subject: TripleComponent::Variable(subject_var.clone()),
            predicate: TripleComponent::Iri(RDF_TYPE.to_string()),
            object: TripleComponent::Iri(type_iri),
            graph: None,
            input: None,
            dataset: None,
        });

        // Extract pagination (first/limit, skip/offset) from regular filter arguments
        let (pagination_first, pagination_skip, filter_args) =
            Self::extract_pagination_args(&field.arguments);

        // Apply filter arguments (excluding pagination)
        if !filter_args.is_empty() {
            plan = self.translate_arguments(&filter_args, &subject_var, plan)?;
        }

        // Process nested selection set
        let mut projections = Vec::new();
        if let Some(selection_set) = &field.selection_set {
            let (new_plan, new_projections) =
                self.translate_selection_set(selection_set, plan, &subject_var)?;
            plan = new_plan;
            projections = new_projections;
        }

        // Add projection if we have fields to return
        if !projections.is_empty() {
            plan = LogicalOperator::Project(ProjectOp {
                projections,
                input: Box::new(plan),
                pass_through_input: false,
            });
        }

        // Apply pagination
        if let Some(skip) = pagination_skip {
            plan = wrap_skip(plan, skip);
        }
        if let Some(first) = pagination_first {
            plan = wrap_limit(plan, first);
        }

        Ok(plan)
    }

    fn translate_selection_set(
        &self,
        selection_set: &ast::SelectionSet,
        input: LogicalOperator,
        subject_var: &str,
    ) -> Result<(LogicalOperator, Vec<Projection>)> {
        let mut projections = Vec::new();
        let mut plan = input;

        for selection in &selection_set.selections {
            match selection {
                ast::Selection::Field(field) => {
                    // Evaluate @skip / @include directives
                    if !graphql_directives_allow(&field.directives) {
                        continue;
                    }

                    if field.selection_set.is_some() {
                        // This is a nested object - requires another triple pattern
                        let (new_plan, nested_projections) =
                            self.translate_nested_field(field, plan, subject_var)?;
                        plan = new_plan;
                        projections.extend(nested_projections);
                    } else {
                        // Scalar field - create a triple pattern to fetch the property
                        let (new_plan, prop_var) =
                            self.translate_scalar_field(field, plan, subject_var)?;
                        plan = new_plan;

                        let alias = field.alias.clone().unwrap_or(field.name.clone());
                        projections.push(Projection {
                            expression: LogicalExpression::Variable(prop_var),
                            alias: Some(alias),
                        });
                    }
                }
                ast::Selection::FragmentSpread(spread) => {
                    // Evaluate @skip / @include directives on the spread
                    if !graphql_directives_allow(&spread.directives) {
                        continue;
                    }
                    // Resolve fragment and include its fields
                    if let Some(frag) = self.fragments.get(&spread.name) {
                        let (new_plan, frag_projections) =
                            self.expand_fragment(frag, plan, subject_var)?;
                        plan = new_plan;
                        projections.extend(frag_projections);
                    }
                }
                ast::Selection::InlineFragment(inline) => {
                    // Evaluate @skip / @include directives on the inline fragment
                    if !graphql_directives_allow(&inline.directives) {
                        continue;
                    }
                    // Inline fragment with type condition
                    if let Some(type_cond) = &inline.type_condition {
                        // Add type check as a triple pattern
                        let type_iri = self.make_type_iri(type_cond);
                        let type_check = LogicalOperator::TripleScan(TripleScanOp {
                            subject: TripleComponent::Variable(subject_var.to_string()),
                            predicate: TripleComponent::Iri(RDF_TYPE.to_string()),
                            object: TripleComponent::Iri(type_iri),
                            graph: None,
                            input: None,
                            dataset: None,
                        });
                        plan = self.join_patterns(plan, type_check);
                    }

                    // Process inline fragment's selection set
                    let (new_plan, inline_projections) =
                        self.translate_selection_set(&inline.selection_set, plan, subject_var)?;
                    plan = new_plan;
                    projections.extend(inline_projections);
                }
            }
        }

        Ok((plan, projections))
    }

    fn translate_scalar_field(
        &self,
        field: &ast::Field,
        input: LogicalOperator,
        subject_var: &str,
    ) -> Result<(LogicalOperator, String)> {
        let object_var = self.var_gen.next();
        let predicate_iri = self.make_predicate_iri(&field.name);

        // Create triple pattern: ?subject <predicate> ?object
        let triple = LogicalOperator::TripleScan(TripleScanOp {
            subject: TripleComponent::Variable(subject_var.to_string()),
            predicate: TripleComponent::Iri(predicate_iri),
            object: TripleComponent::Variable(object_var.clone()),
            graph: None,
            input: None,
            dataset: None,
        });

        let plan = self.join_patterns(input, triple);
        Ok((plan, object_var))
    }

    fn translate_nested_field(
        &self,
        field: &ast::Field,
        input: LogicalOperator,
        from_var: &str,
    ) -> Result<(LogicalOperator, Vec<Projection>)> {
        let to_var = self.var_gen.next();
        let predicate_iri = self.make_predicate_iri(&field.name);

        // Create triple pattern: ?from <predicate> ?to
        let triple = LogicalOperator::TripleScan(TripleScanOp {
            subject: TripleComponent::Variable(from_var.to_string()),
            predicate: TripleComponent::Iri(predicate_iri),
            object: TripleComponent::Variable(to_var.clone()),
            graph: None,
            input: None,
            dataset: None,
        });

        let mut plan = self.join_patterns(input, triple);

        // Apply argument filters to the target
        if !field.arguments.is_empty() {
            plan = self.translate_arguments(&field.arguments, &to_var, plan)?;
        }

        // Process nested selections
        let mut projections = Vec::new();
        if let Some(selection_set) = &field.selection_set {
            let (new_plan, nested_projections) =
                self.translate_selection_set(selection_set, plan, &to_var)?;
            plan = new_plan;
            projections = nested_projections;
        }

        Ok((plan, projections))
    }

    /// Extracts pagination arguments (first/limit, skip/offset) from field arguments,
    /// returning the pagination values and the remaining filter arguments.
    fn extract_pagination_args(
        args: &[ast::Argument],
    ) -> (Option<CountExpr>, Option<CountExpr>, Vec<ast::Argument>) {
        let mut first = None;
        let mut skip = None;
        let mut filters = Vec::new();
        for arg in args {
            match arg.name.as_str() {
                "first" | "limit" => match &arg.value {
                    ast::InputValue::Int(n) => first = Some(CountExpr::Literal(*n as usize)),
                    ast::InputValue::Variable(name) => {
                        first = Some(CountExpr::Parameter(name.clone()));
                    }
                    _ => {}
                },
                "skip" | "offset" => match &arg.value {
                    ast::InputValue::Int(n) => skip = Some(CountExpr::Literal(*n as usize)),
                    ast::InputValue::Variable(name) => {
                        skip = Some(CountExpr::Parameter(name.clone()));
                    }
                    _ => {}
                },
                _ => filters.push(arg.clone()),
            }
        }
        (first, skip, filters)
    }

    fn translate_arguments(
        &self,
        args: &[ast::Argument],
        subject_var: &str,
        input: LogicalOperator,
    ) -> Result<LogicalOperator> {
        let mut plan = input;

        for arg in args {
            // Each argument creates a filter
            // First, create a triple pattern for the property
            let predicate_iri = self.make_predicate_iri(&arg.name);
            let object_var = self.var_gen.next();

            let triple = LogicalOperator::TripleScan(TripleScanOp {
                subject: TripleComponent::Variable(subject_var.to_string()),
                predicate: TripleComponent::Iri(predicate_iri),
                object: TripleComponent::Variable(object_var.clone()),
                graph: None,
                input: None,
                dataset: None,
            });

            plan = self.join_patterns(plan, triple);

            // Add filter for the value (variable refs become Parameter expressions)
            let right = match &arg.value {
                ast::InputValue::Variable(name) => LogicalExpression::Parameter(name.clone()),
                other => LogicalExpression::Literal(other.to_value()),
            };
            let filter = LogicalExpression::Binary {
                left: Box::new(LogicalExpression::Variable(object_var)),
                op: BinaryOp::Eq,
                right: Box::new(right),
            };

            plan = wrap_filter(plan, filter);
        }

        Ok(plan)
    }

    fn expand_fragment(
        &self,
        frag: &ast::FragmentDefinition,
        input: LogicalOperator,
        subject_var: &str,
    ) -> Result<(LogicalOperator, Vec<Projection>)> {
        // Add type condition if present
        let type_iri = self.make_type_iri(&frag.type_condition);
        let type_check = LogicalOperator::TripleScan(TripleScanOp {
            subject: TripleComponent::Variable(subject_var.to_string()),
            predicate: TripleComponent::Iri(RDF_TYPE.to_string()),
            object: TripleComponent::Iri(type_iri),
            graph: None,
            input: None,
            dataset: None,
        });

        let plan = self.join_patterns(input, type_check);

        // Process fragment's selection set
        self.translate_selection_set(&frag.selection_set, plan, subject_var)
    }

    fn join_patterns(&self, left: LogicalOperator, right: LogicalOperator) -> LogicalOperator {
        if matches!(left, LogicalOperator::Empty) {
            return right;
        }
        if matches!(right, LogicalOperator::Empty) {
            return left;
        }

        LogicalOperator::Join(JoinOp {
            left: Box::new(left),
            right: Box::new(right),
            join_type: JoinType::Inner,
            conditions: vec![], // Shared variables are implicit join conditions
        })
    }

    fn make_type_iri(&self, type_name: &str) -> String {
        format!("{}{}", self.namespace, capitalize_first(type_name))
    }

    fn make_predicate_iri(&self, name: &str) -> String {
        format!("{}{}", self.namespace, name)
    }
}

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

    const TEST_NS: &str = "http://example.org/";

    #[test]
    fn test_translate_simple_query() {
        let query = r#"
            query {
                user {
                    id
                    name
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_ok());
    }

    #[test]
    fn test_translate_with_argument() {
        let query = r#"
            query {
                user(id: 123) {
                    name
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_ok());
    }

    #[test]
    fn test_translate_nested_fields() {
        let query = r#"
            query {
                user {
                    name
                    friends {
                        name
                    }
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_ok());
    }

    #[test]
    fn test_reject_mutation() {
        let query = r#"
            mutation {
                createUser(name: "Alix") {
                    id
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_err());
    }

    #[test]
    fn test_creates_rdf_type_triple() {
        let query = r#"
            query {
                person {
                    name
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_ok());
        let plan = result.unwrap();

        // The root should contain a TripleScan with rdf:type
        fn find_type_scan(op: &LogicalOperator) -> bool {
            match op {
                LogicalOperator::TripleScan(scan) => {
                    matches!(&scan.predicate, TripleComponent::Iri(iri) if iri == RDF_TYPE)
                }
                LogicalOperator::Join(join) => {
                    find_type_scan(&join.left) || find_type_scan(&join.right)
                }
                LogicalOperator::Filter(f) => find_type_scan(&f.input),
                LogicalOperator::Project(p) => find_type_scan(&p.input),
                _ => false,
            }
        }

        assert!(find_type_scan(&plan.root));
    }

    // === Projection Tests ===

    #[test]
    fn test_scalar_fields_produce_projections() {
        let query = r#"
            query {
                user {
                    id
                    name
                    email
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        // 3 scalar fields should produce a Project with 3 projections
        fn find_project(op: &LogicalOperator) -> Option<&ProjectOp> {
            match op {
                LogicalOperator::Project(p) => Some(p),
                LogicalOperator::Filter(f) => find_project(&f.input),
                _ => None,
            }
        }
        let project = find_project(&plan.root).expect("Expected Project for scalar fields");
        assert_eq!(
            project.projections.len(),
            3,
            "3 scalar fields should produce 3 projections"
        );
    }

    #[test]
    fn test_field_alias() {
        let query = r#"
            query {
                user {
                    fullName: name
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn find_project(op: &LogicalOperator) -> Option<&ProjectOp> {
            match op {
                LogicalOperator::Project(p) => Some(p),
                LogicalOperator::Filter(f) => find_project(&f.input),
                _ => None,
            }
        }
        let project = find_project(&plan.root).expect("Expected Project");
        assert_eq!(project.projections.len(), 1);
        assert_eq!(
            project.projections[0].alias.as_deref(),
            Some("fullName"),
            "Alias should be 'fullName' not 'name'"
        );
    }

    // === Argument Filter Tests ===

    #[test]
    fn test_argument_creates_filter() {
        let query = r#"
            query {
                user(id: 42) {
                    name
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn has_filter(op: &LogicalOperator) -> bool {
            match op {
                LogicalOperator::Filter(_) => true,
                LogicalOperator::Join(j) => has_filter(&j.left) || has_filter(&j.right),
                LogicalOperator::Project(p) => has_filter(&p.input),
                _ => false,
            }
        }
        assert!(has_filter(&plan.root), "Argument should produce a Filter");
    }

    #[test]
    fn test_multiple_arguments() {
        let query = r#"
            query {
                user(id: 42, active: true) {
                    name
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn count_filters(op: &LogicalOperator) -> usize {
            match op {
                LogicalOperator::Filter(f) => 1 + count_filters(&f.input),
                LogicalOperator::Join(j) => count_filters(&j.left) + count_filters(&j.right),
                LogicalOperator::Project(p) => count_filters(&p.input),
                _ => 0,
            }
        }
        assert!(
            count_filters(&plan.root) >= 2,
            "Two arguments should produce at least 2 filters"
        );
    }

    // === Nested Fields ===

    #[test]
    fn test_nested_field_creates_join() {
        let query = r#"
            query {
                user {
                    name
                    friends {
                        email
                    }
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn count_joins(op: &LogicalOperator) -> usize {
            match op {
                LogicalOperator::Join(j) => 1 + count_joins(&j.left) + count_joins(&j.right),
                LogicalOperator::Project(p) => count_joins(&p.input),
                LogicalOperator::Filter(f) => count_joins(&f.input),
                _ => 0,
            }
        }
        // At minimum: type scan joined with name, joined with friends traversal, joined with email
        assert!(
            count_joins(&plan.root) >= 3,
            "Nested field should produce multiple joins"
        );
    }

    #[test]
    fn test_deep_nesting() {
        let query = r#"
            query {
                user {
                    friends {
                        posts {
                            title
                        }
                    }
                }
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_ok(), "3-level nesting should parse");
    }

    // === Fragment Tests ===

    #[test]
    fn test_fragment_spread() {
        let query = r#"
            query {
                user {
                    ...UserFields
                }
            }
            fragment UserFields on User {
                name
                email
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        // Fragment should expand into projections
        fn find_project(op: &LogicalOperator) -> Option<&ProjectOp> {
            match op {
                LogicalOperator::Project(p) => Some(p),
                LogicalOperator::Filter(f) => find_project(&f.input),
                _ => None,
            }
        }
        let project = find_project(&plan.root).expect("Expected Project from fragment");
        assert_eq!(
            project.projections.len(),
            2,
            "Fragment with 2 fields should produce 2 projections"
        );
    }

    #[test]
    fn test_inline_fragment_with_type_condition() {
        let query = r#"
            query {
                user {
                    name
                    ... on Admin {
                        role
                    }
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        // Should produce type check for Admin
        fn count_type_scans(op: &LogicalOperator) -> usize {
            match op {
                LogicalOperator::TripleScan(scan) => usize::from(
                    matches!(&scan.predicate, TripleComponent::Iri(iri) if iri == RDF_TYPE),
                ),
                LogicalOperator::Join(j) => count_type_scans(&j.left) + count_type_scans(&j.right),
                LogicalOperator::Filter(f) => count_type_scans(&f.input),
                LogicalOperator::Project(p) => count_type_scans(&p.input),
                _ => 0,
            }
        }
        // Should have 2 type scans: one for User, one for Admin
        assert!(
            count_type_scans(&plan.root) >= 2,
            "Inline fragment type condition should add extra rdf:type scan"
        );
    }

    // === Error Handling ===

    #[test]
    fn test_reject_empty_selection() {
        let query = r#"
            query {
            }
        "#;
        let result = translate(query, TEST_NS);
        assert!(result.is_err(), "Empty selection should be rejected");
    }

    // === Namespace and IRI Tests ===

    #[test]
    fn test_type_iri_capitalization() {
        let query = r#"
            query {
                person {
                    name
                }
            }
        "#;
        let plan = translate(query, "http://schema.org/").unwrap();
        fn find_type_iri(op: &LogicalOperator) -> Option<String> {
            match op {
                LogicalOperator::TripleScan(scan) => {
                    if matches!(&scan.predicate, TripleComponent::Iri(iri) if iri == RDF_TYPE)
                        && let TripleComponent::Iri(iri) = &scan.object
                    {
                        return Some(iri.clone());
                    }
                    None
                }
                LogicalOperator::Join(j) => {
                    find_type_iri(&j.left).or_else(|| find_type_iri(&j.right))
                }
                LogicalOperator::Filter(f) => find_type_iri(&f.input),
                LogicalOperator::Project(p) => find_type_iri(&p.input),
                _ => None,
            }
        }
        let type_iri = find_type_iri(&plan.root).expect("Should find type IRI");
        assert_eq!(
            type_iri, "http://schema.org/Person",
            "Root field 'person' should become type IRI 'Person' (capitalized)"
        );
    }

    #[test]
    fn test_predicate_iri_from_field_name() {
        let query = r#"
            query {
                user {
                    name
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn find_predicate_iris(op: &LogicalOperator) -> Vec<String> {
            match op {
                LogicalOperator::TripleScan(scan) => {
                    if let TripleComponent::Iri(iri) = &scan.predicate
                        && iri != RDF_TYPE
                    {
                        return vec![iri.clone()];
                    }
                    Vec::new()
                }
                LogicalOperator::Join(j) => {
                    let mut v = find_predicate_iris(&j.left);
                    v.extend(find_predicate_iris(&j.right));
                    v
                }
                LogicalOperator::Filter(f) => find_predicate_iris(&f.input),
                LogicalOperator::Project(p) => find_predicate_iris(&p.input),
                _ => Vec::new(),
            }
        }
        let iris = find_predicate_iris(&plan.root);
        assert!(
            iris.contains(&format!("{TEST_NS}name")),
            "Field 'name' should produce predicate IRI '{TEST_NS}name'"
        );
    }

    // === Nested Arguments ===

    #[test]
    fn test_nested_field_with_argument() {
        let query = r#"
            query {
                user {
                    friends(active: true) {
                        name
                    }
                }
            }
        "#;
        let plan = translate(query, TEST_NS).unwrap();
        fn has_filter(op: &LogicalOperator) -> bool {
            match op {
                LogicalOperator::Filter(_) => true,
                LogicalOperator::Join(j) => has_filter(&j.left) || has_filter(&j.right),
                LogicalOperator::Project(p) => has_filter(&p.input),
                _ => false,
            }
        }
        assert!(
            has_filter(&plan.root),
            "Nested field argument should produce Filter"
        );
    }
}