use crate::{
db::executor::route::{
AGGREGATE_FAST_PATH_ORDER, GROUPED_AGGREGATE_FAST_PATH_ORDER, LOAD_FAST_PATH_ORDER,
RouteIntent, RouteShapeKind, planner::RouteIntentStage,
},
db::query::plan::AggregateKind,
};
const fn route_shape_kind_for_intent(grouped: bool, kind: Option<AggregateKind>) -> RouteShapeKind {
match (grouped, kind) {
(true, _) => RouteShapeKind::AggregateGrouped,
(false, Some(aggregate_kind)) if aggregate_kind.is_count() => {
RouteShapeKind::AggregateCount
}
(false, Some(_)) => RouteShapeKind::AggregateNonCount,
(false, None) => RouteShapeKind::LoadScalar,
}
}
pub(in crate::db::executor::route::planner) fn derive_route_intent_stage(
intent: RouteIntent<'_>,
) -> RouteIntentStage<'_> {
let stage = match intent {
RouteIntent::Load => RouteIntentStage {
aggregate_shape: None,
grouped: false,
route_shape_kind: route_shape_kind_for_intent(false, None),
grouped_plan_strategy_hint: None,
fast_path_order: &LOAD_FAST_PATH_ORDER,
aggregate_force_materialized_due_to_predicate_uncertainty: false,
},
RouteIntent::Aggregate {
aggregate,
aggregate_force_materialized_due_to_predicate_uncertainty,
} => {
let aggregate_kind = aggregate.kind();
RouteIntentStage {
aggregate_shape: Some(aggregate),
grouped: false,
route_shape_kind: route_shape_kind_for_intent(false, Some(aggregate_kind)),
grouped_plan_strategy_hint: None,
fast_path_order: &AGGREGATE_FAST_PATH_ORDER,
aggregate_force_materialized_due_to_predicate_uncertainty,
}
}
RouteIntent::AggregateGrouped {
grouped_plan_strategy_hint,
aggregate_force_materialized_due_to_predicate_uncertainty,
} => RouteIntentStage {
aggregate_shape: None,
grouped: true,
route_shape_kind: route_shape_kind_for_intent(true, None),
grouped_plan_strategy_hint: Some(grouped_plan_strategy_hint),
fast_path_order: &GROUPED_AGGREGATE_FAST_PATH_ORDER,
aggregate_force_materialized_due_to_predicate_uncertainty,
},
};
let kind = stage.kind();
debug_assert!(
(kind.is_none()
&& !stage.grouped
&& stage.fast_path_order == LOAD_FAST_PATH_ORDER.as_slice())
|| (kind.is_some()
&& !stage.grouped
&& stage.fast_path_order == AGGREGATE_FAST_PATH_ORDER.as_slice())
|| (kind.is_none()
&& stage.grouped
&& stage.fast_path_order == GROUPED_AGGREGATE_FAST_PATH_ORDER.as_slice()),
"route invariant: route intent must map to the canonical fast-path order contract",
);
debug_assert!(
!stage.grouped || stage.aggregate_shape.is_none() && stage.fast_path_order.is_empty(),
"route invariant: grouped intent must not carry scalar aggregate specs or fast-path routes",
);
let expected_route_shape_kind = route_shape_kind_for_intent(stage.grouped, stage.kind());
debug_assert!(
stage.route_shape_kind == expected_route_shape_kind,
"route invariant: route intent shape kind must remain aligned with grouped + aggregate intent",
);
debug_assert!(
stage.grouped == stage.grouped_plan_strategy_hint.is_some(),
"route invariant: grouped intents must carry planner grouped-strategy hints, scalar intents must not",
);
stage
}