use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use crate::distributed::statistics::TableStatistics;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExplainFormat {
Text,
Json,
Dot,
}
#[derive(Debug, Clone)]
pub struct ExplainOptions {
pub format: ExplainFormat,
pub with_statistics: bool,
pub optimized: bool,
pub analyze: bool,
}
impl Default for ExplainOptions {
fn default() -> Self {
Self {
format: ExplainFormat::Text,
with_statistics: false,
optimized: true,
analyze: false,
}
}
}
#[derive(Debug, Clone)]
pub enum PlanNode {
Scan {
table: String,
columns: Vec<String>,
statistics: Option<TableStatistics>,
},
Project {
columns: Vec<String>,
input: Box<PlanNode>,
},
Filter {
predicate: String,
input: Box<PlanNode>,
selectivity: Option<f64>,
},
Join {
join_type: String,
left: Box<PlanNode>,
right: Box<PlanNode>,
keys: Vec<(String, String)>,
},
Aggregate {
keys: Vec<String>,
aggregates: Vec<String>,
input: Box<PlanNode>,
},
Sort {
sort_exprs: Vec<String>,
input: Box<PlanNode>,
},
Limit {
limit: usize,
input: Box<PlanNode>,
},
Window {
window_functions: Vec<String>,
input: Box<PlanNode>,
},
Custom {
name: String,
params: HashMap<String, String>,
input: Box<PlanNode>,
},
}
impl Display for PlanNode {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.explain(&ExplainOptions::default()))
}
}
impl PlanNode {
pub fn explain(&self, options: &ExplainOptions) -> String {
match options.format {
ExplainFormat::Text => self.explain_text(options, 0),
ExplainFormat::Json => self.explain_json(options),
ExplainFormat::Dot => self.explain_dot(options),
}
}
}