linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
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
//! PATH Query Planner
//! Planning for handling Nebula PATH query requests
//!
//! ## Explanation of the improvements
//!
//! Implementing shortest path planning
//! Implement all path planning functions.
//! Support for the shortest path with weights
//! Improve the logic for path filtering.

use crate::binder::BoundStatement;
use crate::parser::ast::Stmt;
use crate::planning::plan::core::next_node_id;
use crate::planning::plan::core::nodes::traversal::path_algorithms::PathUniqueness;
use crate::planning::plan::core::nodes::traversal::{AllPathsNode, ShortestPathNode};
use crate::planning::plan::core::PlanNode;
use crate::planning::plan::logical::logical_nodes::access::LogicalStartNode;
use crate::planning::plan::logical::logical_nodes::algorithm::{
    LogicalAllPathsNode, LogicalShortestPathNode,
};
use crate::planning::plan::logical::LogicalNodeEnum;
use crate::planning::plan::SubPlan;
use crate::planning::planner::{Planner, PlannerError, ValidatedStatement};
use crate::planning::statements::plan_combiner::wrap_logical_filter;
use crate::QueryContext;
use linkrs_core::types::VertexId;
use linkrs_core::Value;
use std::sync::Arc;

pub use crate::planning::plan::core::nodes::{
    ArgumentNode, DedupNode, ExpandAllNode, FilterNode, GetNeighborsNode, ProjectNode, StartNode,
};
pub use crate::planning::plan::core::PlanNodeEnum;

/// PATH Query Planner
/// Responsible for converting PATH queries into execution plans.
#[derive(Debug, Clone)]
pub struct PathPlanner {}

impl PathPlanner {
    /// Create a new PATH planner.
    pub fn new() -> Self {
        Self {}
    }
}

impl Planner for PathPlanner {
    fn transform(
        &mut self,
        validated: &ValidatedStatement,
        qctx: Arc<QueryContext>,
    ) -> Result<SubPlan, PlannerError> {
        let find_path_stmt = match validated.stmt() {
            Stmt::FindPath(find_path_stmt) => find_path_stmt,
            _ => {
                return Err(PlannerError::InvalidOperation(
                    "PathPlanner requires the FindPath statement.".to_string(),
                ));
            }
        };

        let space_id = qctx.space_id().ok_or_else(|| {
            PlannerError::InvalidOperation(
                "No graph space selected, please execute USE <space> first".to_string(),
            )
        })?;

        // FIND PATH syntax carries no vertex label: path vertices are
        // materialized at execution from the edge type schema (or from tagged
        // vertex values), so the plan leaves the vertex label empty instead
        // of guessing one.
        let start_node = StartNode::new();
        let start_node_enum = PlanNodeEnum::Start(start_node);

        let edge_types = self.get_edge_types_from_stmt(find_path_stmt);
        let max_steps = self.get_max_steps_from_stmt(find_path_stmt);

        let start_vertex_ids = self.extract_vertex_ids_from_exprs(&find_path_stmt.from.vertices);
        let end_vertex_ids = self.extract_vertex_ids_from_expr(&find_path_stmt.to);

        let (mut root_node, mut logical_root) = if self.is_shortest_path_stmt(find_path_stmt) {
            // A cycle can never produce a shorter path, so WITH LOOP /
            // WITH TRAIL / WITH WALK are no-ops on this branch; report the
            // ignored modifiers instead of folding them into the plan.
            if find_path_stmt.with_loop || find_path_stmt.with_trail || find_path_stmt.with_walk {
                log::debug!(
                    "FIND SHORTEST PATH ignores WITH LOOP/TRAIL/WALK: \
                     revisiting a vertex or edge cannot yield a shorter path"
                );
            }
            let root = self.build_shortest_path_plan(PathPlanParams {
                left_input: start_node_enum.clone(),
                space_id,
                edge_types: edge_types.clone(),
                max_steps,
                start_vertex_ids: start_vertex_ids.clone(),
                end_vertex_ids: end_vertex_ids.clone(),
                weight_expression: find_path_stmt.weight_expression.clone(),
            })?;
            let logical = Self::shortest_path_logical(
                space_id,
                edge_types,
                max_steps,
                start_vertex_ids,
                end_vertex_ids,
                find_path_stmt.weight_expression.clone(),
            );
            (root, logical)
        } else {
            let root = self.build_all_paths_plan(
                PathPlanParams {
                    left_input: start_node_enum.clone(),
                    space_id,
                    edge_types: edge_types.clone(),
                    max_steps,
                    start_vertex_ids: start_vertex_ids.clone(),
                    end_vertex_ids: end_vertex_ids.clone(),
                    // Rejected at bind time; WEIGHT never reaches ALL PATH.
                    weight_expression: None,
                },
                find_path_stmt,
            )?;
            let logical = Self::all_paths_logical(
                space_id,
                edge_types,
                max_steps,
                find_path_stmt,
                start_vertex_ids,
                end_vertex_ids,
            );
            (root, logical)
        };

        // Apply the WHERE clause on top of the path result.
        if let Some(where_expr) = find_path_stmt.where_clause.clone() {
            let filter_node =
                FilterNode::new(root_node.clone(), where_expr.clone()).map_err(|e| {
                    PlannerError::PlanGenerationFailed(format!(
                        "Failed to create FilterNode: {}",
                        e
                    ))
                })?;
            let col_names = filter_node.col_names().to_vec();
            logical_root = wrap_logical_filter(logical_root, where_expr.clone(), col_names);
            root_node = PlanNodeEnum::Filter(filter_node);
        }

        let sub_plan = SubPlan {
            root: Some(root_node),
            tail: Some(start_node_enum),
            logical_root: Some(logical_root),
        };

        Ok(sub_plan)
    }

    fn plan_bound(
        &mut self,
        ctx: &crate::planning::context::PlanContext<'_>,
    ) -> Result<SubPlan, PlannerError> {
        let bound = ctx.bound;
        let qctx = ctx.qctx.clone();
        let metadata = ctx.metadata;
        let validated = ctx.validated;
        let _ = (&bound, &qctx, &metadata, &validated);
        let find_path = match bound {
            BoundStatement::FindPath(fp) => fp,
            _ => {
                return Err(PlannerError::InvalidOperation(
                    "PathPlanner requires the FindPath statement.".to_string(),
                ));
            }
        };

        let space_id = qctx.space_id().ok_or_else(|| {
            PlannerError::InvalidOperation(
                "No graph space selected, please execute USE <space> first".to_string(),
            )
        })?;

        // FIND PATH syntax carries no vertex label: path vertices are
        // materialized at execution from the edge type schema (or from tagged
        // vertex values), so the plan leaves the vertex label empty instead
        // of guessing one.
        let start_node = StartNode::new();
        let start_node_enum = PlanNodeEnum::Start(start_node);

        let edge_types = find_path
            .over
            .as_ref()
            .map(|(types, _)| types.clone())
            .unwrap_or_default();

        let max_steps = find_path.max_steps.unwrap_or(10);

        let start_vertex_ids: Vec<Value> = find_path
            .from
            .iter()
            .filter_map(|expr| match expr {
                crate::binder::bound::BoundExpression::Literal(v, _) => Some(v.clone()),
                _ => None,
            })
            .collect();

        let end_vertex_ids: Vec<Value> = match &find_path.to {
            crate::binder::bound::BoundExpression::Literal(v, _) => vec![v.clone()],
            _ => vec![],
        };

        let (mut root_node, mut logical_root) = if find_path.shortest {
            // A cycle can never produce a shorter path, so WITH LOOP /
            // WITH TRAIL / WITH WALK are no-ops here; report the ignored
            // modifiers instead of folding them into the plan.
            if find_path.with_loop || find_path.with_trail || find_path.with_walk {
                log::debug!(
                    "FIND SHORTEST PATH ignores WITH LOOP/TRAIL/WALK: \
                     revisiting a vertex or edge cannot yield a shorter path"
                );
            }
            let root = self.build_shortest_path_plan(PathPlanParams {
                left_input: start_node_enum.clone(),
                space_id,
                edge_types: edge_types.clone(),
                max_steps,
                start_vertex_ids: start_vertex_ids.clone(),
                end_vertex_ids: end_vertex_ids.clone(),
                weight_expression: find_path.weight_expression.clone(),
            })?;
            let logical = Self::shortest_path_logical(
                space_id,
                edge_types,
                max_steps,
                start_vertex_ids,
                end_vertex_ids,
                find_path.weight_expression.clone(),
            );
            (root, logical)
        } else {
            let direction = find_path
                .over
                .as_ref()
                .map(|(_, dir)| *dir)
                .unwrap_or(linkrs_core::EdgeDirection::Out);

            // Default paths do not repeat vertices; WITH LOOP / WITH TRAIL /
            // WITH WALK each relax a distinct part of that constraint (see
            // PathUniqueness).
            let path_uniqueness = PathUniqueness::from_flags(
                find_path.with_loop,
                find_path.with_trail,
                find_path.with_walk,
            );

            let right_node = StartNode::new();
            let right_node_enum = PlanNodeEnum::Start(right_node);

            let mut all_paths_node = AllPathsNode::new(
                start_node_enum.clone(),
                right_node_enum,
                space_id,
                max_steps,
                edge_types.clone(),
                1,
                max_steps,
                path_uniqueness,
            );
            all_paths_node.set_direction(direction);
            let start_vids: Vec<VertexId> = start_vertex_ids
                .iter()
                .filter_map(|v| VertexId::try_from(v).ok())
                .collect();
            let end_vids: Vec<VertexId> = end_vertex_ids
                .iter()
                .filter_map(|v| VertexId::try_from(v).ok())
                .collect();
            all_paths_node.set_start_vertex_ids(start_vids.clone());
            all_paths_node.set_end_vertex_ids(end_vids.clone());

            let logical = Self::all_paths_logical_from_parts(
                space_id,
                edge_types,
                max_steps,
                path_uniqueness,
                direction,
                start_vids,
                end_vids,
            );
            (all_paths_node.into_enum(), logical)
        };

        // Apply the bound WHERE clause on top of the path result.
        if let Some(where_clause) = &find_path.where_clause {
            let expr_ctx = Arc::new(
                linkrs_core::types::expr::expression_context::ExpressionAnalysisContext::new(),
            );
            let condition = crate::binder::expr_converter::bound_expr_to_contextual(
                &where_clause.condition,
                &expr_ctx,
            )
            .map_err(PlannerError::PlanGenerationFailed)?;
            let filter_node =
                FilterNode::new(root_node.clone(), condition.clone()).map_err(|e| {
                    PlannerError::PlanGenerationFailed(format!(
                        "Failed to create FilterNode: {}",
                        e
                    ))
                })?;
            let col_names = filter_node.col_names().to_vec();
            logical_root = wrap_logical_filter(logical_root, condition, col_names);
            root_node = PlanNodeEnum::Filter(filter_node);
        }

        let sub_plan = SubPlan {
            root: Some(root_node),
            tail: Some(start_node_enum),
            logical_root: Some(logical_root),
        };

        Ok(sub_plan)
    }

    fn match_planner(&self, stmt: &Stmt) -> bool {
        matches!(stmt, Stmt::FindPath(_))
    }
}

/// Shared inputs for building a path plan.
struct PathPlanParams {
    left_input: PlanNodeEnum,
    space_id: u64,
    edge_types: Vec<String>,
    max_steps: usize,
    start_vertex_ids: Vec<Value>,
    end_vertex_ids: Vec<Value>,
    weight_expression: Option<String>,
}

impl PathPlanner {
    fn build_shortest_path_plan(
        &self,
        params: PathPlanParams,
    ) -> Result<PlanNodeEnum, PlannerError> {
        let right_node = StartNode::new();
        let right_node_enum = PlanNodeEnum::Start(right_node);

        let mut shortest_path_node = ShortestPathNode::new(
            params.left_input,
            right_node_enum,
            params.space_id,
            params.edge_types,
            params.max_steps,
        );
        shortest_path_node.set_start_vertex_ids(params.start_vertex_ids);
        shortest_path_node.set_end_vertex_ids(params.end_vertex_ids);
        if let Some(weight) = params.weight_expression {
            shortest_path_node.set_weight_expression(weight);
        }

        Ok(shortest_path_node.into_enum())
    }

    fn build_all_paths_plan(
        &self,
        params: PathPlanParams,
        stmt: &crate::parser::ast::FindPathStmt,
    ) -> Result<PlanNodeEnum, PlannerError> {
        let right_node = StartNode::new();
        let right_node_enum = PlanNodeEnum::Start(right_node);

        // Default paths do not repeat vertices; WITH LOOP / WITH TRAIL /
        // WITH WALK each relax a distinct part of that constraint (see
        // PathUniqueness).
        let path_uniqueness =
            crate::planning::plan::core::nodes::traversal::path_algorithms::PathUniqueness::from_flags(
                stmt.with_loop, stmt.with_trail, stmt.with_walk,
            );
        let direction = stmt
            .over
            .as_ref()
            .map(|over| over.direction)
            .unwrap_or(linkrs_core::EdgeDirection::Out);

        let mut all_paths_node = AllPathsNode::new(
            params.left_input,
            right_node_enum,
            params.space_id,
            params.max_steps,
            params.edge_types,
            1,
            params.max_steps,
            path_uniqueness,
        );
        all_paths_node.set_direction(direction);
        let start_vids: Vec<VertexId> = params
            .start_vertex_ids
            .iter()
            .filter_map(|v| VertexId::try_from(v).ok())
            .collect();
        let end_vids: Vec<VertexId> = params
            .end_vertex_ids
            .iter()
            .filter_map(|v| VertexId::try_from(v).ok())
            .collect();
        all_paths_node.set_start_vertex_ids(start_vids);
        all_paths_node.set_end_vertex_ids(end_vids);

        Ok(all_paths_node.into_enum())
    }

    fn logical_start_leaf() -> LogicalNodeEnum {
        LogicalNodeEnum::Start(LogicalStartNode::new())
    }

    fn shortest_path_logical(
        space_id: u64,
        edge_types: Vec<String>,
        max_steps: usize,
        start_vertex_ids: Vec<Value>,
        end_vertex_ids: Vec<Value>,
        weight_expression: Option<String>,
    ) -> LogicalNodeEnum {
        let left = Self::logical_start_leaf();
        let right = Self::logical_start_leaf();
        LogicalNodeEnum::ShortestPath(LogicalShortestPathNode {
            id: next_node_id(),
            left: Box::new(left),
            right: Box::new(right),
            space_id,
            edge_types,
            max_step: max_steps,
            weight_expression,
            heuristic_expression: None,
            no_reverse: false,
            start_vertex_ids,
            end_vertex_ids,
            output_var: None,
            col_names: vec!["path".to_string()],
            column_types: vec![],
        })
    }

    fn all_paths_logical(
        space_id: u64,
        edge_types: Vec<String>,
        max_steps: usize,
        stmt: &crate::parser::ast::FindPathStmt,
        start_vertex_ids: Vec<Value>,
        end_vertex_ids: Vec<Value>,
    ) -> LogicalNodeEnum {
        let path_uniqueness =
            crate::planning::plan::core::nodes::traversal::path_algorithms::PathUniqueness::from_flags(
                stmt.with_loop, stmt.with_trail, stmt.with_walk,
            );
        let direction = stmt
            .over
            .as_ref()
            .map(|over| over.direction)
            .unwrap_or(linkrs_core::EdgeDirection::Out);
        let start_vids: Vec<VertexId> = start_vertex_ids
            .iter()
            .filter_map(|v| VertexId::try_from(v).ok())
            .collect();
        let end_vids: Vec<VertexId> = end_vertex_ids
            .iter()
            .filter_map(|v| VertexId::try_from(v).ok())
            .collect();
        Self::all_paths_logical_from_parts(
            space_id,
            edge_types,
            max_steps,
            path_uniqueness,
            direction,
            start_vids,
            end_vids,
        )
    }

    fn all_paths_logical_from_parts(
        space_id: u64,
        edge_types: Vec<String>,
        max_steps: usize,
        path_uniqueness: crate::planning::plan::core::nodes::traversal::path_algorithms::PathUniqueness,
        direction: linkrs_core::EdgeDirection,
        start_vertex_ids: Vec<VertexId>,
        end_vertex_ids: Vec<VertexId>,
    ) -> LogicalNodeEnum {
        let left = Self::logical_start_leaf();
        let right = Self::logical_start_leaf();
        LogicalNodeEnum::AllPaths(LogicalAllPathsNode {
            id: next_node_id(),
            left: Box::new(left),
            right: Box::new(right),
            space_id,
            steps: max_steps,
            edge_types,
            min_hop: 1,
            max_hop: max_steps,
            path_uniqueness,
            direction,
            has_step_limit: true,
            limit: -1,
            offset: 0,
            filter: None,
            start_vertex_ids,
            end_vertex_ids,
            output_var: None,
            col_names: vec!["path".to_string()],
            column_types: vec![],
        })
    }

    fn is_shortest_path_stmt(&self, stmt: &crate::parser::ast::FindPathStmt) -> bool {
        stmt.shortest
    }

    fn get_edge_types_from_stmt(&self, stmt: &crate::parser::ast::FindPathStmt) -> Vec<String> {
        stmt.over
            .as_ref()
            .map(|over| over.edge_types.clone())
            .unwrap_or_default()
    }

    fn get_max_steps_from_stmt(&self, stmt: &crate::parser::ast::FindPathStmt) -> usize {
        stmt.max_steps.unwrap_or(10)
    }

    fn extract_vertex_ids_from_exprs(
        &self,
        exprs: &[linkrs_core::types::ContextualExpression],
    ) -> Vec<Value> {
        let mut ids = Vec::new();
        for expr in exprs {
            if let Some(meta) = expr.expression() {
                if let Some(value) = meta.as_literal() {
                    ids.push(value.clone());
                }
            }
        }
        ids
    }

    fn extract_vertex_ids_from_expr(
        &self,
        expr: &linkrs_core::types::ContextualExpression,
    ) -> Vec<Value> {
        self.extract_vertex_ids_from_exprs(std::slice::from_ref(expr))
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::binder::validation::{ValidatedStatement, ValidationInfo};
    use crate::parser::ast::stmt::Ast;
    use crate::parser::ast::{FindPathStmt, FromClause, OverClause, Span, Stmt};
    use linkrs_core::types::expr::contextual::ContextualExpression;
    use linkrs_core::types::expr::expression_context::ExpressionAnalysisContext;
    use linkrs_core::types::expr::{Expression, ExpressionMeta};

    fn test_query_context() -> Arc<QueryContext> {
        let rctx = Arc::new(crate::QueryRequestContext::new("FIND PATH".to_string()));
        let space_info = linkrs_core::types::SpaceInfo {
            space_id: 1,
            space_name: "test".to_string(),
            ..Default::default()
        };
        Arc::new(
            crate::QueryContext::builder(rctx)
                .with_space_info(space_info)
                .build(),
        )
    }

    fn find_path_stmt(shortest: bool) -> FindPathStmt {
        find_path_stmt_with_modifiers(shortest, false, false, false, None)
    }

    fn find_path_stmt_with_modifiers(
        shortest: bool,
        with_loop: bool,
        with_trail: bool,
        with_walk: bool,
        weight_expression: Option<&str>,
    ) -> FindPathStmt {
        let ctx = Arc::new(ExpressionAnalysisContext::new());
        let id =
            ctx.register_expression(ExpressionMeta::new(Expression::Variable("dst".to_string())));
        FindPathStmt {
            span: Span::default(),
            from: FromClause {
                span: Span::default(),
                vertices: vec![],
            },
            to: ContextualExpression::new(id, ctx.clone()),
            over: Some(OverClause {
                span: Span::default(),
                edge_types: vec!["knows".to_string()],
                direction: linkrs_core::EdgeDirection::Out,
            }),
            where_clause: None,
            shortest,
            max_steps: Some(3),
            limit: None,
            skip: None,
            yield_clause: None,
            weight_expression: weight_expression.map(str::to_string),
            heuristic_expression: None,
            with_loop,
            with_trail,
            with_walk,
        }
    }

    fn validated(stmt: Stmt) -> ValidatedStatement {
        let ctx = Arc::new(ExpressionAnalysisContext::new());
        ValidatedStatement::new(Arc::new(Ast::new(stmt, ctx)), ValidationInfo::new())
    }

    #[test]
    fn shortest_path_plan_carries_native_logical() {
        let stmt = Stmt::FindPath(find_path_stmt(true));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("label-less FIND PATH plans without a vertex label");
        assert!(
            plan.logical_root.is_some(),
            "shortest path plan must carry its native logical root"
        );
    }

    #[test]
    fn all_paths_plan_carries_native_logical_with_direction() {
        let stmt = Stmt::FindPath(find_path_stmt(false));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("label-less FIND PATH plans without a vertex label");
        assert!(
            plan.logical_root.is_some(),
            "all-paths plan must carry its native logical root"
        );
    }

    #[test]
    fn native_logical_converts_back_to_same_physical_shape() {
        let stmt = Stmt::FindPath(find_path_stmt(false));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("label-less FIND PATH plans without a vertex label");
        assert!(
            plan.root.is_some() && plan.logical_root.is_some(),
            "plan must carry both physical and logical shapes"
        );
    }

    #[test]
    fn shortest_path_plan_carries_weight_expression() {
        let stmt = Stmt::FindPath(find_path_stmt_with_modifiers(
            true,
            false,
            false,
            false,
            Some("cost"),
        ));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("weighted shortest path plans");
        match plan.root.as_ref().expect("physical root") {
            PlanNodeEnum::ShortestPath(node) => {
                assert_eq!(node.weight_expression().as_deref(), Some("cost"));
            }
            other => panic!("expected ShortestPath root, got {:?}", other),
        }
        match plan.logical_root.as_ref().expect("logical root") {
            LogicalNodeEnum::ShortestPath(node) => {
                assert_eq!(node.weight_expression.as_deref(), Some("cost"));
            }
            other => panic!("expected ShortestPath logical root, got {:?}", other),
        }
    }

    #[test]
    fn shortest_path_plan_defaults_weight_to_none() {
        let stmt = Stmt::FindPath(find_path_stmt(true));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("plain shortest path plans");
        match plan.root.as_ref().expect("physical root") {
            PlanNodeEnum::ShortestPath(node) => {
                assert!(node.weight_expression().is_none());
            }
            other => panic!("expected ShortestPath root, got {:?}", other),
        }
    }

    #[test]
    fn all_paths_plan_maps_repeat_flags_to_uniqueness() {
        let cases = [
            (false, false, false, PathUniqueness::VertexUnique),
            (true, false, false, PathUniqueness::AllowSelfLoop),
            (false, true, false, PathUniqueness::EdgeUnique),
            (false, false, true, PathUniqueness::Free),
            // A looser modifier dominates the tighter ones.
            (true, true, false, PathUniqueness::EdgeUnique),
            (true, false, true, PathUniqueness::Free),
        ];
        for (with_loop, with_trail, with_walk, expected) in cases {
            let stmt = Stmt::FindPath(find_path_stmt_with_modifiers(
                false, with_loop, with_trail, with_walk, None,
            ));
            let plan = PathPlanner::new()
                .transform(&validated(stmt), test_query_context())
                .expect("all path plans");
            match plan.root.as_ref().expect("physical root") {
                PlanNodeEnum::AllPaths(node) => {
                    assert_eq!(
                        node.path_uniqueness(),
                        expected,
                        "loop={with_loop} trail={with_trail} walk={with_walk} physical policy mismatch"
                    );
                }
                other => panic!("expected AllPaths root, got {:?}", other),
            }
            match plan.logical_root.as_ref().expect("logical root") {
                LogicalNodeEnum::AllPaths(node) => {
                    assert_eq!(
                        node.path_uniqueness, expected,
                        "loop={with_loop} trail={with_trail} walk={with_walk} logical policy mismatch"
                    );
                }
                other => panic!("expected AllPaths logical root, got {:?}", other),
            }
        }
    }

    #[test]
    fn shortest_path_ignores_repeat_flags() {
        // Cycles can never shorten a path, so the shortest-path plan stays a
        // plain ShortestPath node regardless of the modifiers.
        let stmt = Stmt::FindPath(find_path_stmt_with_modifiers(true, true, true, true, None));
        let plan = PathPlanner::new()
            .transform(&validated(stmt), test_query_context())
            .expect("modifiers do not break shortest path planning");
        assert!(
            matches!(plan.root.as_ref(), Some(PlanNodeEnum::ShortestPath(_))),
            "expected ShortestPath root, got {:?}",
            plan.root
        );
    }
}