Skip to main content

lora_compiler/
lib.rs

1pub mod logical;
2pub mod optimizer;
3pub mod physical;
4
5mod pattern;
6mod planner;
7
8pub use logical::{
9    Aggregation, Argument, Create, Delete, Expand, Filter, Limit, LogicalOp, LogicalPlan, Merge,
10    NodeByPropertyScan, NodeScan, OptionalMatch, PathBuild, PlanNodeId, Projection, Remove, Set,
11    Sort, Unwind,
12};
13pub use optimizer::Optimizer;
14pub use physical::{
15    ArgumentExec, CreateExec, DeleteExec, ExpandExec, FilterExec, HashAggregationExec, LimitExec,
16    MergeExec, NodeByLabelScanExec, NodeByPropertyScanExec, NodeScanExec, OptionalMatchExec,
17    PathBuildExec, PhysicalNodeId, PhysicalOp, PhysicalPlan, ProjectionExec, RemoveExec, SetExec,
18    SortExec, UnwindExec,
19};
20pub use planner::Planner;
21
22use lora_analyzer::resolved::ResolvedQuery;
23
24#[derive(Debug, Clone)]
25pub struct CompiledQuery {
26    pub physical: PhysicalPlan,
27    /// Additional UNION branches, each compiled independently.
28    pub unions: Vec<CompiledUnionBranch>,
29}
30
31#[derive(Debug, Clone)]
32pub struct CompiledUnionBranch {
33    /// If true, UNION ALL (keep duplicates). If false, plain UNION (deduplicate).
34    pub all: bool,
35    pub physical: PhysicalPlan,
36}
37
38pub struct Compiler;
39
40impl Compiler {
41    pub fn compile(query: &ResolvedQuery) -> CompiledQuery {
42        let mut planner = Planner::new();
43        let logical = planner.plan(query);
44
45        let mut optimizer = Optimizer::new();
46        let optimized = optimizer.optimize(logical);
47
48        // Lower by moving the logical plan — it is not needed after lowering.
49        let physical = optimizer.lower_to_physical(optimized);
50
51        let unions = query
52            .unions
53            .iter()
54            .map(|union_part| {
55                let branch_query = ResolvedQuery {
56                    clauses: union_part.clauses.clone(),
57                    unions: Vec::new(),
58                };
59                let mut branch_planner = Planner::new();
60                let branch_logical = branch_planner.plan(&branch_query);
61                let mut branch_optimizer = Optimizer::new();
62                let branch_optimized = branch_optimizer.optimize(branch_logical);
63                let branch_physical = branch_optimizer.lower_to_physical(branch_optimized);
64
65                CompiledUnionBranch {
66                    all: union_part.all,
67                    physical: branch_physical,
68                }
69            })
70            .collect();
71
72        CompiledQuery { physical, unions }
73    }
74}