use crate::{
db::query::plan::{AccessPlannedQuery, ScalarPlan},
model::entity::EntityModel,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db) struct LogicalPushdownEligibility {
secondary_order_allowed: bool,
grouped_aggregate_allowed: bool,
requires_full_materialization: bool,
}
impl LogicalPushdownEligibility {
#[must_use]
pub(in crate::db) const fn new(
secondary_order_allowed: bool,
grouped_aggregate_allowed: bool,
requires_full_materialization: bool,
) -> Self {
Self {
secondary_order_allowed,
grouped_aggregate_allowed,
requires_full_materialization,
}
}
#[must_use]
pub(in crate::db) const fn secondary_order_allowed(self) -> bool {
self.secondary_order_allowed
}
#[must_use]
pub(in crate::db) const fn grouped_aggregate_allowed(self) -> bool {
self.grouped_aggregate_allowed
}
#[must_use]
pub(in crate::db) const fn requires_full_materialization(self) -> bool {
self.requires_full_materialization
}
}
#[must_use]
pub(in crate::db) fn derive_logical_pushdown_eligibility(
model: &EntityModel,
plan: &AccessPlannedQuery,
) -> LogicalPushdownEligibility {
LogicalPushdownEligibility::new(
secondary_order_contract_is_deterministic(model, plan.scalar_plan()),
plan.grouped_plan().is_some(),
false,
)
}
#[must_use]
pub(in crate::db) fn secondary_order_contract_is_deterministic(
model: &EntityModel,
scalar: &ScalarPlan,
) -> bool {
let Some(order) = scalar.order.as_ref() else {
return false;
};
order
.deterministic_secondary_order_direction(model.primary_key.name)
.is_some()
}