mod compare;
mod index_select;
mod order_select;
mod predicate;
mod prefix;
mod range;
mod ranking;
#[cfg(test)]
mod tests;
use crate::{
db::{
access::{AccessPlan, normalize_access_plan_value},
predicate::Predicate,
query::plan::{OrderSpec, PlanError, PlannedNonIndexAccessReason},
schema::SchemaInfo,
},
error::InternalError,
model::{entity::EntityModel, index::IndexModel},
value::Value,
};
use thiserror::Error as ThisError;
pub(in crate::db::query::plan) use index_select::{
index_literal_matches_schema, index_predicate_guarantees_compare, sorted_indexes,
sorted_model_indexes,
};
pub(in crate::db) use index_select::{
residual_query_predicate_after_access_path_bounds,
residual_query_predicate_after_filtered_access,
};
pub(in crate::db::query::plan) use ranking::{
AccessCandidateScore, AndFamilyCandidateScore, AndFamilyPriorityClass,
access_candidate_score_outranks, and_family_candidate_score_outranks,
candidate_satisfies_secondary_order, range_bound_count,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::query) struct PlannedAccessSelection {
access: AccessPlan<Value>,
planned_non_index_reason: Option<PlannedNonIndexAccessReason>,
}
impl PlannedAccessSelection {
#[must_use]
pub(in crate::db::query) const fn new(
access: AccessPlan<Value>,
planned_non_index_reason: Option<PlannedNonIndexAccessReason>,
) -> Self {
Self {
access,
planned_non_index_reason,
}
}
#[must_use]
pub(in crate::db::query) fn into_parts(
self,
) -> (AccessPlan<Value>, Option<PlannedNonIndexAccessReason>) {
(self.access, self.planned_non_index_reason)
}
#[must_use]
pub(in crate::db::query::plan) fn into_access(self) -> AccessPlan<Value> {
self.access
}
}
#[derive(Debug, ThisError)]
pub enum PlannerError {
#[error("{0}")]
Plan(Box<PlanError>),
#[error("{0}")]
Internal(Box<InternalError>),
}
impl From<PlanError> for PlannerError {
fn from(err: PlanError) -> Self {
Self::Plan(Box::new(err))
}
}
impl From<InternalError> for PlannerError {
fn from(err: InternalError) -> Self {
Self::Internal(Box::new(err))
}
}
#[cfg(test)]
pub(in crate::db) fn plan_access(
model: &EntityModel,
visible_indexes: &[&'static IndexModel],
schema: &SchemaInfo,
predicate: Option<&Predicate>,
) -> Result<AccessPlan<Value>, PlannerError> {
plan_access_with_order(model, visible_indexes, schema, predicate, None, false)
}
pub(in crate::db::query) fn plan_access_with_order(
model: &EntityModel,
visible_indexes: &[&'static IndexModel],
schema: &SchemaInfo,
predicate: Option<&Predicate>,
order: Option<&OrderSpec>,
grouped: bool,
) -> Result<AccessPlan<Value>, PlannerError> {
Ok(
plan_access_selection_with_order(
model,
visible_indexes,
schema,
predicate,
order,
grouped,
)?
.into_access(),
)
}
pub(in crate::db::query) fn plan_access_selection_with_order(
model: &EntityModel,
visible_indexes: &[&'static IndexModel],
schema: &SchemaInfo,
predicate: Option<&Predicate>,
order: Option<&OrderSpec>,
grouped: bool,
) -> Result<PlannedAccessSelection, PlannerError> {
let Some(predicate) = predicate else {
let true_predicate = Predicate::True;
let eligible_indexes = sorted_indexes(visible_indexes, &true_predicate);
return Ok(order_fallback_selection(
model,
eligible_indexes.as_slice(),
order,
grouped,
));
};
let eligible_indexes = sorted_indexes(visible_indexes, predicate);
let selection = predicate::plan_predicate(
model,
eligible_indexes.as_slice(),
schema,
predicate,
order,
grouped,
)?;
let (access, planned_non_index_reason) = selection.into_parts();
let plan = normalize_access_plan_value(access);
if !plan.is_single_full_scan() {
return Ok(PlannedAccessSelection::new(plan, planned_non_index_reason));
}
Ok(
order_select::index_range_from_order(model, eligible_indexes.as_slice(), order, grouped)
.map_or_else(
|| {
PlannedAccessSelection::new(
plan,
Some(PlannedNonIndexAccessReason::PlannerFullScanFallback),
)
},
|access| PlannedAccessSelection::new(access, None),
),
)
}
fn order_fallback_selection(
model: &EntityModel,
eligible_indexes: &[&'static IndexModel],
order: Option<&OrderSpec>,
grouped: bool,
) -> PlannedAccessSelection {
order_select::index_range_from_order(model, eligible_indexes, order, grouped).map_or_else(
|| {
PlannedAccessSelection::new(
AccessPlan::full_scan(),
Some(PlannedNonIndexAccessReason::PlannerFullScanFallback),
)
},
|access| PlannedAccessSelection::new(access, None),
)
}