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::*;
9pub use optimizer::Optimizer;
10pub use physical::*;
11pub use planner::Planner;
12
13use lora_analyzer::resolved::ResolvedQuery;
14
15#[derive(Debug, Clone)]
16pub struct CompiledQuery {
17    pub physical: PhysicalPlan,
18    /// Additional UNION branches, each compiled independently.
19    pub unions: Vec<CompiledUnionBranch>,
20}
21
22#[derive(Debug, Clone)]
23pub struct CompiledUnionBranch {
24    /// If true, UNION ALL (keep duplicates). If false, plain UNION (deduplicate).
25    pub all: bool,
26    pub physical: PhysicalPlan,
27}
28
29pub struct Compiler;
30
31impl Compiler {
32    pub fn compile(query: &ResolvedQuery) -> CompiledQuery {
33        let mut planner = Planner::new();
34        let logical = planner.plan(query);
35
36        let mut optimizer = Optimizer::new();
37        let optimized = optimizer.optimize(logical);
38
39        // Lower by moving the logical plan — it is not needed after lowering.
40        let physical = optimizer.lower_to_physical(optimized);
41
42        let unions = query
43            .unions
44            .iter()
45            .map(|union_part| {
46                let branch_query = ResolvedQuery {
47                    clauses: union_part.clauses.clone(),
48                    unions: Vec::new(),
49                };
50                let mut branch_planner = Planner::new();
51                let branch_logical = branch_planner.plan(&branch_query);
52                let mut branch_optimizer = Optimizer::new();
53                let branch_optimized = branch_optimizer.optimize(branch_logical);
54                let branch_physical = branch_optimizer.lower_to_physical(branch_optimized);
55
56                CompiledUnionBranch {
57                    all: union_part.all,
58                    physical: branch_physical,
59                }
60            })
61            .collect();
62
63        CompiledQuery { physical, unions }
64    }
65}