use crate::{
db::{
executor::aggregate::capability::field_kind_supports_aggregate_ordering,
query::plan::{AggregateKind, GroupedPlanStrategy},
},
model::field::FieldModel,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum FastPathOrder {
PrimaryKey,
SecondaryPrefix,
PrimaryScan,
IndexRange,
Composite,
}
pub(in crate::db::executor) const LOAD_FAST_PATH_ORDER: [FastPathOrder; 3] = [
FastPathOrder::PrimaryKey,
FastPathOrder::SecondaryPrefix,
FastPathOrder::IndexRange,
];
pub(in crate::db::executor) const AGGREGATE_FAST_PATH_ORDER: [FastPathOrder; 5] = [
FastPathOrder::PrimaryKey,
FastPathOrder::SecondaryPrefix,
FastPathOrder::PrimaryScan,
FastPathOrder::IndexRange,
FastPathOrder::Composite,
];
pub(in crate::db::executor) const GROUPED_AGGREGATE_FAST_PATH_ORDER: [FastPathOrder; 0] = [];
pub(in crate::db::executor) const MUTATION_FAST_PATH_ORDER: [FastPathOrder; 0] = [];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db) struct AggregateRouteShape<'a> {
kind: AggregateKind,
target_field: Option<&'a str>,
target_field_known: bool,
target_field_orderable: bool,
target_field_is_primary_key: bool,
}
impl<'a> AggregateRouteShape<'a> {
#[must_use]
pub(in crate::db) const fn new_resolved(
kind: AggregateKind,
target_field: Option<&'a str>,
target_field_known: bool,
target_field_orderable: bool,
target_field_is_primary_key: bool,
) -> Self {
Self {
kind,
target_field,
target_field_known,
target_field_orderable,
target_field_is_primary_key,
}
}
#[must_use]
pub(in crate::db) fn new_from_fields(
kind: AggregateKind,
target_field: Option<&'a str>,
fields: &[FieldModel],
primary_key_name: &str,
) -> Self {
let target_field_known = target_field.is_none_or(|target_field| {
fields
.iter()
.any(|field_model| field_model.name() == target_field)
});
let target_field_orderable = target_field.is_some_and(|target_field| {
fields
.iter()
.find(|field_model| field_model.name() == target_field)
.is_some_and(|field_model| {
field_kind_supports_aggregate_ordering(&field_model.kind())
})
});
let target_field_is_primary_key =
target_field.is_some_and(|target_field| target_field == primary_key_name);
Self::new_resolved(
kind,
target_field,
target_field_known,
target_field_orderable,
target_field_is_primary_key,
)
}
#[must_use]
pub(in crate::db) const fn kind(self) -> AggregateKind {
self.kind
}
#[must_use]
pub(in crate::db) const fn target_field(self) -> Option<&'a str> {
self.target_field
}
#[must_use]
pub(in crate::db) const fn target_field_known(self) -> bool {
self.target_field_known
}
#[must_use]
pub(in crate::db) const fn target_field_orderable(self) -> bool {
self.target_field_orderable
}
#[must_use]
pub(in crate::db) const fn target_field_is_primary_key(self) -> bool {
self.target_field_is_primary_key
}
}
pub(in crate::db::executor::route) enum RouteIntent<'a> {
Load,
Aggregate {
aggregate: AggregateRouteShape<'a>,
aggregate_force_materialized_due_to_predicate_uncertainty: bool,
},
AggregateGrouped {
grouped_plan_strategy: GroupedPlanStrategy,
aggregate_force_materialized_due_to_predicate_uncertainty: bool,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum RouteShapeKind {
LoadScalar,
AggregateCount,
AggregateNonCount,
AggregateGrouped,
MutationDelete,
}