use crate::{
db::access::{AccessPathKind, AccessPlan},
db::executor::planning::route::GroupedExecutionMode,
metrics::sink::{GroupedPlanExecutionMode, MetricsEvent, PlanKind, Span, record},
traits::EntityKind,
};
pub(super) fn record_plan_metrics<K>(access: &AccessPlan<K>) {
let kind = access_plan_kind(access);
record(MetricsEvent::Plan {
kind,
grouped_execution_mode: None,
});
}
pub(super) fn record_grouped_plan_metrics<K>(
access: &AccessPlan<K>,
grouped_execution_mode: GroupedExecutionMode,
) {
let kind = access_plan_kind(access);
let grouped_execution_mode = match grouped_execution_mode {
GroupedExecutionMode::HashMaterialized => GroupedPlanExecutionMode::HashMaterialized,
GroupedExecutionMode::OrderedMaterialized => GroupedPlanExecutionMode::OrderedMaterialized,
};
record(MetricsEvent::Plan {
kind,
grouped_execution_mode: Some(grouped_execution_mode),
});
}
fn access_plan_kind<K>(access: &AccessPlan<K>) -> PlanKind {
match access {
AccessPlan::Path(path) => match path.kind() {
AccessPathKind::ByKey | AccessPathKind::ByKeys => PlanKind::Keys,
AccessPathKind::KeyRange => PlanKind::Range,
AccessPathKind::IndexPrefix
| AccessPathKind::IndexMultiLookup
| AccessPathKind::IndexRange => PlanKind::Index,
AccessPathKind::FullScan => PlanKind::FullScan,
},
AccessPlan::Union(_) | AccessPlan::Intersection(_) => PlanKind::FullScan,
}
}
pub(super) const fn set_rows_from_len<E: EntityKind>(span: &mut Span<E>, len: usize) {
span.set_rows(len as u64);
}
pub(super) fn record_rows_scanned_for_path(entity_path: &'static str, rows_scanned: usize) {
record(MetricsEvent::RowsScanned {
entity_path,
rows_scanned: u64::try_from(rows_scanned).unwrap_or(u64::MAX),
});
}
pub(super) fn record_rows_filtered_for_path(entity_path: &'static str, rows_filtered: usize) {
record(MetricsEvent::RowsFiltered {
entity_path,
rows_filtered: u64::try_from(rows_filtered).unwrap_or(u64::MAX),
});
}
pub(super) fn record_rows_aggregated_for_path(entity_path: &'static str, rows_aggregated: usize) {
record(MetricsEvent::RowsAggregated {
entity_path,
rows_aggregated: u64::try_from(rows_aggregated).unwrap_or(u64::MAX),
});
}
pub(super) fn record_rows_emitted_for_path(entity_path: &'static str, rows_emitted: usize) {
record(MetricsEvent::RowsEmitted {
entity_path,
rows_emitted: u64::try_from(rows_emitted).unwrap_or(u64::MAX),
});
}