use std::collections::BTreeMap;
use std::sync::Arc;
use mz_compute_types::dataflows::DataflowDescription;
use mz_expr::explain::ExplainContext;
use mz_repr::explain::{Explain, ExplainConfig, ExplainError, ExplainFormat, ExprHumanizer};
use mz_repr::optimize::OptimizerFeatures;
use mz_repr::GlobalId;
use mz_transform::dataflow::DataflowMetainfo;
use mz_transform::notice::OptimizerNotice;
use crate::AdapterError;
pub(crate) mod fast_path;
pub(crate) mod hir;
pub(crate) mod insights;
pub(crate) mod lir;
pub(crate) mod mir;
pub(crate) mod optimizer_trace;
pub(crate) struct Explainable<'a, T>(&'a mut T);
impl<'a, T> Explainable<'a, T> {
pub(crate) fn new(t: &'a mut T) -> Explainable<'a, T> {
Explainable(t)
}
}
pub(crate) fn explain_dataflow<T>(
mut plan: DataflowDescription<T>,
format: ExplainFormat,
config: &ExplainConfig,
features: &OptimizerFeatures,
humanizer: &dyn ExprHumanizer,
cardinality_stats: BTreeMap<GlobalId, usize>,
target_cluster: Option<&str>,
dataflow_metainfo: &DataflowMetainfo<Arc<OptimizerNotice>>,
) -> Result<String, AdapterError>
where
for<'a> Explainable<'a, DataflowDescription<T>>: Explain<'a, Context = ExplainContext<'a>>,
{
let used_indexes = dataflow_metainfo.used_indexes(&plan);
let optimizer_notices = OptimizerNotice::explain(
&dataflow_metainfo.optimizer_notices,
humanizer,
config.redacted,
)
.map_err(ExplainError::FormatError)?;
let context = ExplainContext {
config,
features,
humanizer,
cardinality_stats,
used_indexes,
finishing: Default::default(),
duration: Default::default(),
target_cluster,
optimizer_notices,
};
Ok(Explainable::new(&mut plan).explain(&format, &context)?)
}
pub(crate) fn explain_plan<T>(
mut plan: T,
format: ExplainFormat,
config: &ExplainConfig,
features: &OptimizerFeatures,
humanizer: &dyn ExprHumanizer,
cardinality_stats: BTreeMap<GlobalId, usize>,
target_cluster: Option<&str>,
) -> Result<String, AdapterError>
where
for<'a> Explainable<'a, T>: Explain<'a, Context = ExplainContext<'a>>,
{
let context = ExplainContext {
config,
features,
humanizer,
cardinality_stats,
used_indexes: Default::default(),
finishing: Default::default(),
duration: Default::default(),
target_cluster,
optimizer_notices: Default::default(),
};
Ok(Explainable::new(&mut plan).explain(&format, &context)?)
}