Skip to main content

lora_compiler/
lib.rs

1pub mod logical;
2pub mod optimizer;
3pub mod physical;
4pub mod plan_tree;
5
6mod pattern;
7mod planner;
8
9pub use plan_tree::{plan_tree_from_compiled, PlanTree, PlanTreeNode};
10
11pub use logical::{
12    Aggregation, Argument, Create, Delete, Expand, Filter, Foreach, Limit, LogicalOp, LogicalPlan,
13    Merge, NodeByPointScan, NodeByPropertyRangeScan, NodeByPropertyScan, NodeByTextScan, NodeScan,
14    OptionalMatch, PathBuild, PlanNodeId, PointPredicate, Projection, RelByPointScan,
15    RelByPropertyRangeScan, RelByTextScan, Remove, Set, Sort, TextPredicate, Unwind,
16};
17pub use optimizer::Optimizer;
18pub use physical::{
19    ArgumentExec, CreateExec, DeleteExec, ExpandExec, FilterExec, ForeachExec, HashAggregationExec,
20    LimitExec, MergeExec, NodeByLabelScanExec, NodeByPointScanExec, NodeByPropertyRangeScanExec,
21    NodeByPropertyScanExec, NodeByTextScanExec, NodeScanExec, OptionalMatchExec, PathBuildExec,
22    PhysicalNodeId, PhysicalOp, PhysicalPlan, ProjectionExec, RelByPointScanExec,
23    RelByPropertyRangeScanExec, RelByTextScanExec, RemoveExec, SetExec, SortExec, UnwindExec,
24};
25pub use planner::Planner;
26
27use lora_analyzer::resolved::ResolvedQuery;
28use lora_store::GraphStats;
29
30#[derive(Debug, Clone)]
31pub struct CompiledQuery {
32    pub physical: PhysicalPlan,
33    /// Additional UNION branches, each compiled independently.
34    pub unions: Vec<CompiledUnionBranch>,
35}
36
37#[derive(Debug, Clone)]
38pub struct CompiledUnionBranch {
39    /// If true, UNION ALL (keep duplicates). If false, plain UNION (deduplicate).
40    pub all: bool,
41    pub physical: PhysicalPlan,
42}
43
44pub struct Compiler;
45
46impl Compiler {
47    /// Compile a resolved query into an executable plan, using `stats`
48    /// for cost-based rewrite selection. Pass [`GraphStats::default()`]
49    /// when no cardinality information is available — the optimizer
50    /// then falls back to the conservative "commit any matching
51    /// rewrite" behaviour that the runtime executor handles safely.
52    pub fn compile(query: &ResolvedQuery, stats: &GraphStats) -> CompiledQuery {
53        let physical = compile_physical(query, stats);
54        let unions = query
55            .unions
56            .iter()
57            .map(|union_part| {
58                let branch_query = ResolvedQuery {
59                    clauses: union_part.clauses.clone(),
60                    unions: Vec::new(),
61                };
62                CompiledUnionBranch {
63                    all: union_part.all,
64                    physical: compile_physical(&branch_query, stats),
65                }
66            })
67            .collect();
68
69        CompiledQuery { physical, unions }
70    }
71}
72
73fn compile_physical(query: &ResolvedQuery, stats: &GraphStats) -> PhysicalPlan {
74    let mut planner = Planner::new();
75    let logical = planner.plan(query);
76
77    let mut optimizer = Optimizer::new();
78    let optimized = optimizer.optimize(logical, stats);
79
80    // Lower by moving the logical plan; it is not needed after lowering.
81    optimizer.lower_to_physical(optimized)
82}