use crate::{
db::{
DbSession, EntityResponse, PagedGroupedExecutionWithTrace, PagedLoadExecutionWithTrace,
PersistedRow, Query, QueryError, QueryTracePlan, TraceExecutionStrategy,
access::AccessStrategy,
cursor::CursorPlanError,
executor::{ExecutablePlan, ExecutionStrategy, LoadExecutor},
query::plan::QueryMode,
session::decode_optional_cursor_bytes,
},
error::InternalError,
traits::{CanisterKind, EntityKind, EntityValue},
};
impl<C: CanisterKind> DbSession<C> {
fn ensure_scalar_paged_execution_strategy(
strategy: ExecutionStrategy,
) -> Result<(), QueryError> {
match strategy {
ExecutionStrategy::PrimaryKey => Err(QueryError::invariant(
CursorPlanError::cursor_requires_explicit_or_grouped_ordering_message(),
)),
ExecutionStrategy::Ordered => Ok(()),
ExecutionStrategy::Grouped => Err(QueryError::invariant(
"grouped plans require execute_grouped(...)",
)),
}
}
fn ensure_grouped_execution_strategy(strategy: ExecutionStrategy) -> Result<(), QueryError> {
match strategy {
ExecutionStrategy::Grouped => Ok(()),
ExecutionStrategy::PrimaryKey | ExecutionStrategy::Ordered => Err(
QueryError::invariant("execute_grouped requires grouped logical plans"),
),
}
}
pub fn execute_query<E>(&self, query: &Query<E>) -> Result<EntityResponse<E>, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
let mode = query.mode();
let plan = query.plan()?.into_executable();
self.execute_query_dyn(mode, plan)
}
#[doc(hidden)]
pub fn execute_delete_count<E>(&self, query: &Query<E>) -> Result<u32, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
if !query.mode().is_delete() {
return Err(QueryError::unsupported_query(
"delete count execution requires delete query mode",
));
}
let plan = query.plan()?.into_executable();
self.with_metrics(|| self.delete_executor::<E>().execute_count(plan))
.map_err(QueryError::execute)
}
pub(in crate::db) fn execute_query_dyn<E>(
&self,
mode: QueryMode,
plan: ExecutablePlan<E>,
) -> Result<EntityResponse<E>, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
let result = match mode {
QueryMode::Load(_) => self.with_metrics(|| self.load_executor::<E>().execute(plan)),
QueryMode::Delete(_) => self.with_metrics(|| self.delete_executor::<E>().execute(plan)),
};
result.map_err(QueryError::execute)
}
pub(in crate::db) fn execute_load_query_with<E, T>(
&self,
query: &Query<E>,
op: impl FnOnce(LoadExecutor<E>, ExecutablePlan<E>) -> Result<T, InternalError>,
) -> Result<T, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
let plan = query.plan()?.into_executable();
self.with_metrics(|| op(self.load_executor::<E>(), plan))
.map_err(QueryError::execute)
}
pub fn trace_query<E>(&self, query: &Query<E>) -> Result<QueryTracePlan, QueryError>
where
E: EntityKind<Canister = C>,
{
let compiled = query.plan()?;
let explain = compiled.explain();
let plan_hash = compiled.plan_hash_hex();
let executable = compiled.into_executable();
let access_strategy = AccessStrategy::from_plan(executable.access()).debug_summary();
let execution_strategy = match query.mode() {
QueryMode::Load(_) => Some(trace_execution_strategy(
executable
.execution_strategy()
.map_err(QueryError::execute)?,
)),
QueryMode::Delete(_) => None,
};
Ok(QueryTracePlan::new(
plan_hash,
access_strategy,
execution_strategy,
explain,
))
}
pub(crate) fn execute_load_query_paged_with_trace<E>(
&self,
query: &Query<E>,
cursor_token: Option<&str>,
) -> Result<PagedLoadExecutionWithTrace<E>, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
let plan = query.plan()?.into_executable();
Self::ensure_scalar_paged_execution_strategy(
plan.execution_strategy().map_err(QueryError::execute)?,
)?;
let cursor_bytes = decode_optional_cursor_bytes(cursor_token)?;
let cursor = plan
.prepare_cursor(cursor_bytes.as_deref())
.map_err(QueryError::from_executor_plan_error)?;
let (page, trace) = self
.with_metrics(|| {
self.load_executor::<E>()
.execute_paged_with_cursor_traced(plan, cursor)
})
.map_err(QueryError::execute)?;
let next_cursor = page
.next_cursor
.map(|token| {
let Some(token) = token.as_scalar() else {
return Err(QueryError::scalar_paged_emitted_grouped_continuation());
};
token.encode().map_err(|err| {
QueryError::serialize_internal(format!(
"failed to serialize continuation cursor: {err}"
))
})
})
.transpose()?;
Ok(PagedLoadExecutionWithTrace::new(
page.items,
next_cursor,
trace,
))
}
pub fn execute_grouped<E>(
&self,
query: &Query<E>,
cursor_token: Option<&str>,
) -> Result<PagedGroupedExecutionWithTrace, QueryError>
where
E: PersistedRow<Canister = C> + EntityValue,
{
let plan = query.plan()?.into_executable();
Self::ensure_grouped_execution_strategy(
plan.execution_strategy().map_err(QueryError::execute)?,
)?;
let cursor_bytes = decode_optional_cursor_bytes(cursor_token)?;
let cursor = plan
.prepare_grouped_cursor(cursor_bytes.as_deref())
.map_err(QueryError::from_executor_plan_error)?;
let (page, trace) = self
.with_metrics(|| {
self.load_executor::<E>()
.execute_grouped_paged_with_cursor_traced(plan, cursor)
})
.map_err(QueryError::execute)?;
let next_cursor = page
.next_cursor
.map(|token| {
let Some(token) = token.as_grouped() else {
return Err(QueryError::grouped_paged_emitted_scalar_continuation());
};
token.encode().map_err(|err| {
QueryError::serialize_internal(format!(
"failed to serialize grouped continuation cursor: {err}"
))
})
})
.transpose()?;
Ok(PagedGroupedExecutionWithTrace::new(
page.rows,
next_cursor,
trace,
))
}
}
const fn trace_execution_strategy(strategy: ExecutionStrategy) -> TraceExecutionStrategy {
match strategy {
ExecutionStrategy::PrimaryKey => TraceExecutionStrategy::PrimaryKey,
ExecutionStrategy::Ordered => TraceExecutionStrategy::Ordered,
ExecutionStrategy::Grouped => TraceExecutionStrategy::Grouped,
}
}