quill_sql/execution/physical_plan/
mod.rs

1mod aggregate;
2mod create_index;
3mod create_table;
4mod delete;
5mod drop_index;
6mod drop_table;
7mod empty;
8mod filter;
9mod index_scan;
10mod insert;
11mod limit;
12mod nested_loop_join;
13mod project;
14mod seq_scan;
15mod sort;
16mod update;
17mod values;
18
19pub use aggregate::PhysicalAggregate;
20pub use create_index::PhysicalCreateIndex;
21pub use create_table::PhysicalCreateTable;
22pub use delete::PhysicalDelete;
23pub use drop_index::PhysicalDropIndex;
24pub use drop_table::PhysicalDropTable;
25pub use empty::PhysicalEmpty;
26pub use filter::PhysicalFilter;
27pub use index_scan::PhysicalIndexScan;
28pub use insert::PhysicalInsert;
29pub use limit::PhysicalLimit;
30pub use nested_loop_join::PhysicalNestedLoopJoin;
31pub use project::PhysicalProject;
32pub use seq_scan::PhysicalSeqScan;
33pub use sort::PhysicalSort;
34pub use update::PhysicalUpdate;
35pub use values::PhysicalValues;
36
37use crate::catalog::SchemaRef;
38use crate::{
39    error::QuillSQLResult,
40    execution::{ExecutionContext, VolcanoExecutor},
41    storage::tuple::Tuple,
42};
43
44#[derive(Debug)]
45pub enum PhysicalPlan {
46    Empty(PhysicalEmpty),
47    Values(PhysicalValues),
48    SeqScan(PhysicalSeqScan),
49    IndexScan(PhysicalIndexScan),
50    Limit(PhysicalLimit),
51    Sort(PhysicalSort),
52    Update(PhysicalUpdate),
53    Delete(PhysicalDelete),
54    Insert(PhysicalInsert),
55    Project(PhysicalProject),
56    Filter(PhysicalFilter),
57    NestedLoopJoin(PhysicalNestedLoopJoin),
58    Aggregate(PhysicalAggregate),
59    CreateTable(PhysicalCreateTable),
60    CreateIndex(PhysicalCreateIndex),
61    DropTable(PhysicalDropTable),
62    DropIndex(PhysicalDropIndex),
63}
64
65impl PhysicalPlan {
66    pub fn inputs(&self) -> Vec<&PhysicalPlan> {
67        match self {
68            PhysicalPlan::Project(PhysicalProject { input, .. }) => vec![input],
69            PhysicalPlan::Filter(PhysicalFilter { input, .. }) => vec![input],
70            PhysicalPlan::Limit(PhysicalLimit { input, .. }) => vec![input],
71            PhysicalPlan::Insert(PhysicalInsert { input, .. }) => vec![input],
72            PhysicalPlan::NestedLoopJoin(PhysicalNestedLoopJoin {
73                left_input,
74                right_input,
75                ..
76            }) => vec![left_input, right_input],
77            PhysicalPlan::Sort(PhysicalSort { input, .. }) => vec![input],
78            PhysicalPlan::Aggregate(PhysicalAggregate { input, .. }) => vec![input],
79            PhysicalPlan::Empty(_)
80            | PhysicalPlan::CreateTable(_)
81            | PhysicalPlan::CreateIndex(_)
82            | PhysicalPlan::DropTable(_)
83            | PhysicalPlan::DropIndex(_)
84            | PhysicalPlan::SeqScan(_)
85            | PhysicalPlan::IndexScan(_)
86            | PhysicalPlan::Update(_)
87            | PhysicalPlan::Delete(_)
88            | PhysicalPlan::Values(_) => vec![],
89        }
90    }
91}
92
93impl VolcanoExecutor for PhysicalPlan {
94    fn init(&self, context: &mut ExecutionContext) -> QuillSQLResult<()> {
95        match self {
96            PhysicalPlan::Empty(op) => op.init(context),
97            PhysicalPlan::CreateTable(op) => op.init(context),
98            PhysicalPlan::CreateIndex(op) => op.init(context),
99            PhysicalPlan::DropTable(op) => op.init(context),
100            PhysicalPlan::DropIndex(op) => op.init(context),
101            PhysicalPlan::Insert(op) => op.init(context),
102            PhysicalPlan::Values(op) => op.init(context),
103            PhysicalPlan::Project(op) => op.init(context),
104            PhysicalPlan::Filter(op) => op.init(context),
105            PhysicalPlan::SeqScan(op) => op.init(context),
106            PhysicalPlan::IndexScan(op) => op.init(context),
107            PhysicalPlan::Limit(op) => op.init(context),
108            PhysicalPlan::NestedLoopJoin(op) => op.init(context),
109            PhysicalPlan::Sort(op) => op.init(context),
110            PhysicalPlan::Aggregate(op) => op.init(context),
111            PhysicalPlan::Update(op) => op.init(context),
112            PhysicalPlan::Delete(op) => op.init(context),
113        }
114    }
115
116    fn next(&self, context: &mut ExecutionContext) -> QuillSQLResult<Option<Tuple>> {
117        match self {
118            PhysicalPlan::Empty(op) => op.next(context),
119            PhysicalPlan::CreateTable(op) => op.next(context),
120            PhysicalPlan::CreateIndex(op) => op.next(context),
121            PhysicalPlan::DropTable(op) => op.next(context),
122            PhysicalPlan::DropIndex(op) => op.next(context),
123            PhysicalPlan::Insert(op) => op.next(context),
124            PhysicalPlan::Values(op) => op.next(context),
125            PhysicalPlan::Project(op) => op.next(context),
126            PhysicalPlan::Filter(op) => op.next(context),
127            PhysicalPlan::SeqScan(op) => op.next(context),
128            PhysicalPlan::IndexScan(op) => op.next(context),
129            PhysicalPlan::Limit(op) => op.next(context),
130            PhysicalPlan::NestedLoopJoin(op) => op.next(context),
131            PhysicalPlan::Sort(op) => op.next(context),
132            PhysicalPlan::Aggregate(op) => op.next(context),
133            PhysicalPlan::Update(op) => op.next(context),
134            PhysicalPlan::Delete(op) => op.next(context),
135        }
136    }
137
138    fn output_schema(&self) -> SchemaRef {
139        match self {
140            Self::Empty(op) => op.output_schema(),
141            Self::CreateTable(op) => op.output_schema(),
142            Self::CreateIndex(op) => op.output_schema(),
143            Self::DropTable(op) => op.output_schema(),
144            Self::DropIndex(op) => op.output_schema(),
145            Self::Insert(op) => op.output_schema(),
146            Self::Values(op) => op.output_schema(),
147            Self::Project(op) => op.output_schema(),
148            Self::Filter(op) => op.output_schema(),
149            Self::SeqScan(op) => op.output_schema(),
150            Self::IndexScan(op) => op.output_schema(),
151            Self::Limit(op) => op.output_schema(),
152            Self::NestedLoopJoin(op) => op.output_schema(),
153            Self::Sort(op) => op.output_schema(),
154            Self::Aggregate(op) => op.output_schema(),
155            Self::Update(op) => op.output_schema(),
156            Self::Delete(op) => op.output_schema(),
157        }
158    }
159}
160
161impl std::fmt::Display for PhysicalPlan {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        match self {
164            Self::Empty(op) => write!(f, "{op}"),
165            Self::CreateTable(op) => write!(f, "{op}"),
166            Self::CreateIndex(op) => write!(f, "{op}"),
167            Self::DropTable(op) => write!(f, "{op}"),
168            Self::DropIndex(op) => write!(f, "{op}"),
169            Self::Insert(op) => write!(f, "{op}"),
170            Self::Values(op) => write!(f, "{op}"),
171            Self::Project(op) => write!(f, "{op}"),
172            Self::Filter(op) => write!(f, "{op}"),
173            Self::SeqScan(op) => write!(f, "{op}"),
174            Self::IndexScan(op) => write!(f, "{op}"),
175            Self::Limit(op) => write!(f, "{op}"),
176            Self::NestedLoopJoin(op) => write!(f, "{op}"),
177            Self::Sort(op) => write!(f, "{op}"),
178            Self::Aggregate(op) => write!(f, "{op}"),
179            Self::Update(op) => write!(f, "{op}"),
180            Self::Delete(op) => write!(f, "{op}"),
181        }
182    }
183}