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