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