Skip to main content

hawdb_plan_cypher/
root.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::marker::PhantomData;
16
17use crate::{LogicalPlan, PhysicalPlan};
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum PlanPhaseKind {
21    Logical,
22    LoweringReady,
23    Physical,
24    FastPathPhysical,
25}
26
27pub trait PlanPhase {
28    const KIND: PlanPhaseKind;
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct LogicalPhase;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub struct LoweringReadyPhase;
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct PhysicalPhase;
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct FastPathPhysicalPhase;
42
43#[derive(Debug, Clone, PartialEq)]
44pub struct LogicalPlanRoot {
45    plan: LogicalPlan,
46}
47
48#[derive(Debug, Clone, PartialEq)]
49pub struct LoweringReadyLogicalPlanRoot {
50    plan: LogicalPlan,
51}
52
53#[derive(Debug, Clone, PartialEq)]
54pub struct PhysicalPlanRoot<T, P: PlanPhase = PhysicalPhase> {
55    plan: PhysicalPlan,
56    metadata: T,
57    phase: PhantomData<P>,
58}
59
60pub type FastPathPhysicalPlanRoot<T> = PhysicalPlanRoot<T, FastPathPhysicalPhase>;
61
62impl PlanPhase for LogicalPhase {
63    const KIND: PlanPhaseKind = PlanPhaseKind::Logical;
64}
65
66impl PlanPhase for LoweringReadyPhase {
67    const KIND: PlanPhaseKind = PlanPhaseKind::LoweringReady;
68}
69
70impl PlanPhase for PhysicalPhase {
71    const KIND: PlanPhaseKind = PlanPhaseKind::Physical;
72}
73
74impl PlanPhase for FastPathPhysicalPhase {
75    const KIND: PlanPhaseKind = PlanPhaseKind::FastPathPhysical;
76}
77
78impl LogicalPlanRoot {
79    pub fn new(plan: LogicalPlan) -> Self {
80        Self { plan }
81    }
82
83    pub fn plan(&self) -> &LogicalPlan {
84        &self.plan
85    }
86
87    pub fn phase(&self) -> PlanPhaseKind {
88        LogicalPhase::KIND
89    }
90
91    pub fn into_lowering_ready(self) -> LoweringReadyLogicalPlanRoot {
92        LoweringReadyLogicalPlanRoot { plan: self.plan }
93    }
94
95    pub fn into_plan(self) -> LogicalPlan {
96        self.plan
97    }
98}
99
100impl LoweringReadyLogicalPlanRoot {
101    pub fn new(plan: LogicalPlan) -> Self {
102        Self { plan }
103    }
104
105    pub fn plan(&self) -> &LogicalPlan {
106        &self.plan
107    }
108
109    pub fn phase(&self) -> PlanPhaseKind {
110        LoweringReadyPhase::KIND
111    }
112
113    pub fn into_plan(self) -> LogicalPlan {
114        self.plan
115    }
116}
117
118impl<T, P: PlanPhase> PhysicalPlanRoot<T, P> {
119    pub fn new(plan: PhysicalPlan, metadata: T) -> Self {
120        Self {
121            plan,
122            metadata,
123            phase: PhantomData,
124        }
125    }
126
127    pub fn plan(&self) -> &PhysicalPlan {
128        &self.plan
129    }
130
131    pub fn metadata(&self) -> &T {
132        &self.metadata
133    }
134
135    pub fn trace(&self) -> &T {
136        &self.metadata
137    }
138
139    pub fn phase(&self) -> PlanPhaseKind {
140        P::KIND
141    }
142
143    pub fn into_parts(self) -> (PhysicalPlan, T) {
144        (self.plan, self.metadata)
145    }
146}
147
148impl<T> PhysicalPlanRoot<T, PhysicalPhase> {
149    pub fn into_fast_path(self) -> FastPathPhysicalPlanRoot<T> {
150        PhysicalPlanRoot {
151            plan: self.plan,
152            metadata: self.metadata,
153            phase: PhantomData,
154        }
155    }
156}