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,
},
}
struct VocabularyRow {
name: &'static str,
text: String,
kind: NotificationKind,
}
impl PlanWarning {
fn vocabulary(&self) -> VocabularyRow {
match self {
PlanWarning::EstimationMiss => VocabularyRow {
name: WARNING_ESTIMATION_MISS,
text: format!(
"estimated rows {ESTIMATION_MISS_FACTOR}x or more above the actual rows"
),
kind: NotificationKind::EstimationMiss,
},
PlanWarning::CartesianProduct => VocabularyRow {
name: WARNING_CARTESIAN_PRODUCT,
text: "the MATCH patterns share no join anchor".to_string(),
kind: NotificationKind::CartesianProduct,
},
PlanWarning::AllNodesScan => VocabularyRow {
name: WARNING_ALL_NODES_SCAN,
text: "the scan matches every node; add a label to narrow it".to_string(),
kind: NotificationKind::AllNodesScan,
},
PlanWarning::LabelDisjunctionScan { .. } => VocabularyRow {
name: WARNING_LABEL_DISJUNCTION_SCAN,
text: "the disjunction scan answers per label; consider the property index"
.to_string(),
kind: NotificationKind::LabelDisjunctionScan,
},
}
}
pub fn name(&self) -> &'static str {
self.vocabulary().name
}
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.vocabulary().kind
}
pub fn message(&self) -> String {
match self.suggested_index() {
Some(index) => format!("{} ({index})", self.vocabulary().text),
None => self.vocabulary().text.to_string(),
}
}
pub(crate) fn from_wire_parts(name: &str, index: Option<&str>) -> Option<Self> {
match (name, index) {
(WARNING_ESTIMATION_MISS, None) => Some(PlanWarning::EstimationMiss),
(WARNING_CARTESIAN_PRODUCT, None) => Some(PlanWarning::CartesianProduct),
(WARNING_ALL_NODES_SCAN, None) => Some(PlanWarning::AllNodesScan),
(WARNING_LABEL_DISJUNCTION_SCAN, Some(indexes)) => {
Some(PlanWarning::LabelDisjunctionScan {
indexes: indexes.to_string(),
})
}
_ => None,
}
}
}
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)
}