1use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12pub enum QueryExecutionState {
13 #[default]
15 Pending,
16 Running,
18 Completed,
20 Failed,
22 Cancelled,
24 Timeout,
26}
27
28impl QueryExecutionState {
29 pub fn is_terminal(&self) -> bool {
31 matches!(
32 self,
33 QueryExecutionState::Completed
34 | QueryExecutionState::Failed
35 | QueryExecutionState::Cancelled
36 | QueryExecutionState::Timeout
37 )
38 }
39
40 pub fn can_cancel(&self) -> bool {
42 matches!(
43 self,
44 QueryExecutionState::Pending | QueryExecutionState::Running
45 )
46 }
47
48 pub fn description(&self) -> &'static str {
50 match self {
51 QueryExecutionState::Pending => "Pending",
52 QueryExecutionState::Running => "Running",
53 QueryExecutionState::Completed => "Completed",
54 QueryExecutionState::Failed => "Failed",
55 QueryExecutionState::Cancelled => "Cancelled",
56 QueryExecutionState::Timeout => "Timeout",
57 }
58 }
59}
60
61impl fmt::Display for QueryExecutionState {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(f, "{}", self.description())
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
71pub enum ExecutorState {
72 #[default]
74 Initialized,
75 Executing,
77 Completed,
79 Failed,
81 Cancelled,
83 Paused,
85}
86
87impl ExecutorState {
88 pub fn can_transition_to(&self, target: ExecutorState) -> bool {
90 match (self, target) {
91 (ExecutorState::Initialized, ExecutorState::Executing) => true,
93 (ExecutorState::Initialized, ExecutorState::Failed) => true,
94 (ExecutorState::Initialized, ExecutorState::Cancelled) => true,
95 (ExecutorState::Executing, ExecutorState::Completed) => true,
97 (ExecutorState::Executing, ExecutorState::Failed) => true,
98 (ExecutorState::Executing, ExecutorState::Cancelled) => true,
99 (ExecutorState::Executing, ExecutorState::Paused) => true,
100 (ExecutorState::Paused, ExecutorState::Executing) => true,
102 (ExecutorState::Paused, ExecutorState::Failed) => true,
103 (ExecutorState::Paused, ExecutorState::Cancelled) => true,
104 (ExecutorState::Completed, _) => false,
106 (ExecutorState::Failed, _) => false,
107 (ExecutorState::Cancelled, _) => false,
108 _ => false,
109 }
110 }
111
112 pub fn description(&self) -> &'static str {
114 match self {
115 ExecutorState::Initialized => "Initialized",
116 ExecutorState::Executing => "Executing",
117 ExecutorState::Completed => "Completed",
118 ExecutorState::Failed => "Failed",
119 ExecutorState::Cancelled => "Cancelled",
120 ExecutorState::Paused => "Paused",
121 }
122 }
123}
124
125impl fmt::Display for ExecutorState {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "{}", self.description())
128 }
129}
130
131#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
135pub enum LoopExecutionState {
136 #[default]
138 NotStarted,
139 Running { iteration: usize },
141 Finished,
143 Error(String),
145 MaxIterationsReached { max: usize },
147}
148
149impl LoopExecutionState {
150 pub fn iteration(&self) -> Option<usize> {
152 match self {
153 LoopExecutionState::Running { iteration } => Some(*iteration),
154 _ => None,
155 }
156 }
157
158 pub fn is_finished(&self) -> bool {
160 matches!(
161 self,
162 LoopExecutionState::Finished
163 | LoopExecutionState::Error(_)
164 | LoopExecutionState::MaxIterationsReached { .. }
165 )
166 }
167
168 pub fn description(&self) -> String {
170 match self {
171 LoopExecutionState::NotStarted => "Not Started".to_string(),
172 LoopExecutionState::Running { iteration } => {
173 format!("Running (iteration {})", iteration)
174 }
175 LoopExecutionState::Finished => "Finished".to_string(),
176 LoopExecutionState::Error(msg) => format!("Error: {}", msg),
177 LoopExecutionState::MaxIterationsReached { max } => {
178 format!("Max iterations reached ({})", max)
179 }
180 }
181 }
182}
183
184impl fmt::Display for LoopExecutionState {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 write!(f, "{}", self.description())
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
194pub enum RowStatus {
195 #[default]
197 Valid,
198 Filtered,
200 Invalid,
202 TagFiltered,
204}
205
206impl RowStatus {
207 pub fn is_valid(&self) -> bool {
209 matches!(self, RowStatus::Valid)
210 }
211
212 pub fn to_i32(&self) -> i32 {
214 match self {
215 RowStatus::Valid => 0,
216 RowStatus::Invalid => -1,
217 RowStatus::Filtered => -2,
218 RowStatus::TagFiltered => -3,
219 }
220 }
221}
222
223impl fmt::Display for RowStatus {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 match self {
226 RowStatus::Valid => write!(f, "Effective"),
227 RowStatus::Filtered => write!(f, "Filtered"),
228 RowStatus::Invalid => write!(f, "Invalid"),
229 RowStatus::TagFiltered => write!(f, "Tag filtering"),
230 }
231 }
232}
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
238pub enum OptimizationState {
239 #[default]
241 NotStarted,
242 Rewriting,
244 LogicalOptimizing,
246 PhysicalOptimizing,
248 Completed,
250 Failed,
252}
253
254impl OptimizationState {
255 pub fn description(&self) -> &'static str {
257 match self {
258 OptimizationState::NotStarted => "Not Started",
259 OptimizationState::Rewriting => "Rewriting Phase",
260 OptimizationState::LogicalOptimizing => "Logical Optimizing",
261 OptimizationState::PhysicalOptimizing => "Physical Optimizing",
262 OptimizationState::Completed => "Completed",
263 OptimizationState::Failed => "Failed",
264 }
265 }
266
267 pub fn is_optimizing(&self) -> bool {
269 matches!(
270 self,
271 OptimizationState::Rewriting
272 | OptimizationState::LogicalOptimizing
273 | OptimizationState::PhysicalOptimizing
274 )
275 }
276}
277
278impl fmt::Display for OptimizationState {
279 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280 write!(f, "{}", self.description())
281 }
282}
283
284#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
288pub enum OptimizationPhase {
289 Rewrite,
291 Logical,
293 Physical,
295 #[default]
297 Unknown,
298}
299
300impl OptimizationPhase {
301 pub fn description(&self) -> &'static str {
303 match self {
304 OptimizationPhase::Rewrite => "Rewrite Phase",
305 OptimizationPhase::Logical => "Logical Optimizing",
306 OptimizationPhase::Physical => "Physical Optimizing",
307 OptimizationPhase::Unknown => "Unknown Phase",
308 }
309 }
310
311 pub fn is_logical(&self) -> bool {
313 matches!(
314 self,
315 OptimizationPhase::Rewrite | OptimizationPhase::Logical
316 )
317 }
318
319 pub fn is_physical(&self) -> bool {
321 matches!(self, OptimizationPhase::Physical)
322 }
323}
324
325impl fmt::Display for OptimizationPhase {
326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327 write!(f, "{}", self.description())
328 }
329}
330
331#[cfg(test)]
332mod tests {
333 use super::*;
334
335 #[test]
336 fn test_query_execution_state_transitions() {
337 assert!(!QueryExecutionState::Running.is_terminal());
338 assert!(QueryExecutionState::Completed.is_terminal());
339 assert!(QueryExecutionState::Pending.can_cancel());
340 assert!(!QueryExecutionState::Completed.can_cancel());
341 }
342
343 #[test]
344 fn test_executor_state_transitions() {
345 assert!(ExecutorState::Initialized.can_transition_to(ExecutorState::Executing));
346 assert!(!ExecutorState::Completed.can_transition_to(ExecutorState::Executing));
347 assert!(ExecutorState::Executing.can_transition_to(ExecutorState::Paused));
348 assert!(ExecutorState::Paused.can_transition_to(ExecutorState::Executing));
349 }
350
351 #[test]
352 fn test_loop_execution_state() {
353 let state = LoopExecutionState::Running { iteration: 5 };
354 assert_eq!(state.iteration(), Some(5));
355 assert!(!state.is_finished());
356
357 let finished = LoopExecutionState::Finished;
358 assert!(finished.is_finished());
359 }
360
361 #[test]
362 fn test_row_status_conversion() {
363 assert_eq!(RowStatus::Valid.to_i32(), 0);
364 assert_eq!(RowStatus::Invalid.to_i32(), -1);
365 assert_eq!(RowStatus::Filtered.to_i32(), -2);
366 assert_eq!(RowStatus::TagFiltered.to_i32(), -3);
367 }
368}