use super::notification_wire::NotificationKind;
use super::plan_keys;
pub(crate) const WARNING_ESTIMATION_MISS: &str = "EstimationMiss";
pub(crate) const WARNING_CARTESIAN_PRODUCT: &str = "CartesianProduct";
pub(crate) const WARNING_ALL_NODES_SCAN: &str = "AllNodesScan";
pub(crate) const WARNING_LABEL_DISJUNCTION_SCAN: &str = "LabelDisjunctionScan";
const ESTIMATION_MISS_FACTOR: u64 = 10;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PlanWarning {
EstimationMiss,
CartesianProduct,
AllNodesScan,
LabelDisjunctionScan {
indexes: String,
},
}
const PAYLOAD_LESS: [PlanWarning; 3] = [
PlanWarning::EstimationMiss,
PlanWarning::CartesianProduct,
PlanWarning::AllNodesScan,
];
impl PlanWarning {
fn identity(&self) -> (&'static str, NotificationKind) {
match self {
PlanWarning::EstimationMiss => {
(WARNING_ESTIMATION_MISS, NotificationKind::EstimationMiss)
}
PlanWarning::CartesianProduct => (
WARNING_CARTESIAN_PRODUCT,
NotificationKind::CartesianProduct,
),
PlanWarning::AllNodesScan => (WARNING_ALL_NODES_SCAN, NotificationKind::AllNodesScan),
PlanWarning::LabelDisjunctionScan { .. } => (
WARNING_LABEL_DISJUNCTION_SCAN,
NotificationKind::LabelDisjunctionScan,
),
}
}
fn base_text(&self) -> String {
match self {
PlanWarning::EstimationMiss => {
format!("estimated rows {ESTIMATION_MISS_FACTOR}x or more above the actual rows")
}
PlanWarning::CartesianProduct => "the MATCH patterns share no join anchor".to_string(),
PlanWarning::AllNodesScan => {
"the scan matches every node; add a label to narrow it".to_string()
}
PlanWarning::LabelDisjunctionScan { .. } => {
"the disjunction scan answers per label; consider the property index".to_string()
}
}
}
pub fn name(&self) -> &'static str {
self.identity().0
}
pub fn suggested_index(&self) -> Option<&str> {
match self {
PlanWarning::LabelDisjunctionScan { indexes } => Some(indexes.as_str()),
PlanWarning::EstimationMiss
| PlanWarning::CartesianProduct
| PlanWarning::AllNodesScan => None,
}
}
pub fn notification_kind(&self) -> NotificationKind {
self.identity().1
}
pub fn message(&self) -> String {
match self.suggested_index() {
Some(index) => format!("{} ({index})", self.base_text()),
None => self.base_text(),
}
}
pub(crate) fn from_wire_parts(name: &str, index: Option<&str>) -> Option<Self> {
match index {
None => PAYLOAD_LESS.into_iter().find(|w| w.name() == name),
Some(indexes) => {
let candidate = PlanWarning::LabelDisjunctionScan {
indexes: indexes.to_string(),
};
(candidate.name() == name).then_some(candidate)
}
}
}
}
pub fn ratio_defined(actual: u64) -> bool {
actual > 0
}
pub fn estimation_miss(estimated: u64, actual: u64) -> bool {
ratio_defined(actual) && estimated / ESTIMATION_MISS_FACTOR >= actual
}
pub fn warning_wire_json(warning: &PlanWarning) -> serde_json::Value {
let mut out = serde_json::Map::new();
out.insert(
plan_keys::WARNING_NAME.to_string(),
serde_json::Value::String(warning.name().to_string()),
);
if let Some(index) = warning.suggested_index() {
out.insert(
plan_keys::WARNING_INDEX.to_string(),
serde_json::Value::String(index.to_string()),
);
}
serde_json::Value::Object(out)
}