use crate::db::{
access::LoweredAccess,
direction::Direction,
executor::{
EntityAuthority, ExecutionPreparation,
planning::preparation::slot_map_for_model_plan,
route::{
direct_primary_key_lookup_shape_supported, primary_key_stream_window_shape_supported,
},
},
query::plan::{
AccessPlannedQuery, CoveringReadExecutionPlan, covering_strict_predicate_compatible,
index_covering_existing_rows_terminal_eligible,
},
};
use crate::value::Value;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum BytesTerminalFastPathContract {
PrimaryKeyWindow(Direction),
OrderedKeyStreamWindow(Direction),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum CountTerminalFastPathContract {
PrimaryKeyCardinality,
PrimaryKeyExistingRows(Direction),
IndexCoveringExistingRows(Direction),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum ExistsTerminalFastPathContract {
IndexCoveringExistingRows(Direction),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::executor) enum LoadTerminalFastPathContract {
CoveringRead(CoveringReadExecutionPlan),
}
pub(in crate::db::executor) fn derive_count_terminal_fast_path_contract_for_model(
plan: &AccessPlannedQuery,
lowered_access: &LoweredAccess<'_, Value>,
strict_predicate_compatible: bool,
) -> Option<CountTerminalFastPathContract> {
let capabilities = lowered_access
.executable()
.capabilities()
.single_path_capabilities()?;
(plan.has_no_distinct()
&& !plan.has_any_residual_filter()
&& primary_key_stream_window_shape_supported(capabilities))
.then_some(CountTerminalFastPathContract::PrimaryKeyCardinality)
.or_else(|| {
let direction = plan.unordered_or_primary_key_order_direction()?;
(!plan.has_any_residual_filter() && direct_primary_key_lookup_shape_supported(capabilities))
.then_some(CountTerminalFastPathContract::PrimaryKeyExistingRows(
direction,
))
})
.or_else(|| {
index_covering_existing_rows_terminal_eligible(plan, strict_predicate_compatible).then_some(
CountTerminalFastPathContract::IndexCoveringExistingRows(Direction::Asc),
)
})
}
pub(in crate::db::executor) fn derive_exists_terminal_fast_path_contract_for_model(
plan: &AccessPlannedQuery,
strict_predicate_compatible: bool,
) -> Option<ExistsTerminalFastPathContract> {
index_covering_existing_rows_terminal_eligible(plan, strict_predicate_compatible).then_some(
ExistsTerminalFastPathContract::IndexCoveringExistingRows(Direction::Asc),
)
}
pub(in crate::db::executor) fn derive_load_terminal_fast_path_contract(
authority: EntityAuthority,
plan: &AccessPlannedQuery,
strict_predicate_compatible: bool,
) -> Option<LoadTerminalFastPathContract> {
authority
.covering_read_execution_plan(plan, strict_predicate_compatible)
.map(LoadTerminalFastPathContract::CoveringRead)
}
pub(in crate::db::executor) fn derive_load_terminal_fast_path_contract_for_plan(
authority: EntityAuthority,
plan: &AccessPlannedQuery,
) -> Option<LoadTerminalFastPathContract> {
if !plan.scalar_plan().mode.is_load() {
return None;
}
let execution_preparation =
ExecutionPreparation::from_covering_route_plan(plan, slot_map_for_model_plan(plan));
let strict_predicate_compatible = covering_strict_predicate_compatible(
plan,
execution_preparation
.predicate_capability_profile()
.map(crate::db::predicate::PredicateCapabilityProfile::index),
);
derive_load_terminal_fast_path_contract(authority, plan, strict_predicate_compatible)
}