Skip to main content

icydb_core/db/executor/load/
mod.rs

1mod aggregate;
2mod execute;
3mod fast_stream;
4mod index_range_limit;
5mod page;
6mod pk_stream;
7mod secondary_index;
8mod terminal;
9mod trace;
10
11use self::{
12    execute::{ExecutionInputs, IndexPredicateCompileMode},
13    trace::{access_path_variant, execution_order_direction},
14};
15use crate::{
16    db::{
17        Db,
18        executor::{
19            AccessStreamBindings, KeyOrderComparator, OrderedKeyStreamBox,
20            aggregate::field::{
21                AggregateFieldValueError, FieldSlot, resolve_any_aggregate_target_slot,
22                resolve_numeric_aggregate_target_slot, resolve_orderable_aggregate_target_slot,
23            },
24            compile_predicate_slots,
25            plan::{record_plan_metrics, record_rows_scanned},
26            route::{ExecutionRoutePlan, RouteOrderSlotPolicy, derive_scan_direction},
27        },
28        query::policy,
29        query::{
30            contracts::cursor::{ContinuationToken, CursorBoundary},
31            cursor::continuation::decode_pk_cursor_boundary,
32            plan::{
33                AccessPlan, AccessPlannedQuery, Direction, OrderDirection,
34                cursor::{PlannedCursor, compute_page_window},
35                lowering::ExecutablePlan,
36                validate::validate_executor_plan,
37            },
38        },
39        response::Response,
40    },
41    error::InternalError,
42    obs::sink::{ExecKind, Span},
43    traits::{EntityKind, EntityValue},
44};
45use std::marker::PhantomData;
46
47///
48/// CursorPage
49///
50/// Internal load page result with continuation cursor payload.
51/// Returned by paged executor entrypoints.
52///
53
54#[derive(Debug)]
55pub(crate) struct CursorPage<E: EntityKind> {
56    pub(crate) items: Response<E>,
57
58    pub(crate) next_cursor: Option<ContinuationToken>,
59}
60
61///
62/// ExecutionAccessPathVariant
63///
64/// Coarse access path shape used by the load execution trace surface.
65///
66
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
68pub enum ExecutionAccessPathVariant {
69    ByKey,
70    ByKeys,
71    KeyRange,
72    IndexPrefix,
73    IndexRange,
74    FullScan,
75    Union,
76    Intersection,
77}
78
79///
80/// ExecutionOptimization
81///
82/// Canonical load optimization selected by execution, if any.
83///
84
85#[derive(Clone, Copy, Debug, Eq, PartialEq)]
86pub enum ExecutionOptimization {
87    PrimaryKey,
88    SecondaryOrderPushdown,
89    IndexRangeLimitPushdown,
90}
91
92///
93/// ExecutionTrace
94///
95/// Structured, opt-in load execution introspection snapshot.
96/// Captures plan-shape and execution decisions without changing semantics.
97///
98
99#[derive(Clone, Copy, Debug, Eq, PartialEq)]
100pub struct ExecutionTrace {
101    pub access_path_variant: ExecutionAccessPathVariant,
102    pub direction: OrderDirection,
103    pub optimization: Option<ExecutionOptimization>,
104    pub keys_scanned: u64,
105    pub rows_returned: u64,
106    pub continuation_applied: bool,
107    pub index_predicate_applied: bool,
108    pub index_predicate_keys_rejected: u64,
109    pub distinct_keys_deduped: u64,
110}
111
112impl ExecutionTrace {
113    fn new<K>(access: &AccessPlan<K>, direction: Direction, continuation_applied: bool) -> Self {
114        Self {
115            access_path_variant: access_path_variant(access),
116            direction: execution_order_direction(direction),
117            optimization: None,
118            keys_scanned: 0,
119            rows_returned: 0,
120            continuation_applied,
121            index_predicate_applied: false,
122            index_predicate_keys_rejected: 0,
123            distinct_keys_deduped: 0,
124        }
125    }
126
127    fn set_path_outcome(
128        &mut self,
129        optimization: Option<ExecutionOptimization>,
130        keys_scanned: usize,
131        rows_returned: usize,
132        index_predicate_applied: bool,
133        index_predicate_keys_rejected: u64,
134        distinct_keys_deduped: u64,
135    ) {
136        self.optimization = optimization;
137        self.keys_scanned = u64::try_from(keys_scanned).unwrap_or(u64::MAX);
138        self.rows_returned = u64::try_from(rows_returned).unwrap_or(u64::MAX);
139        self.index_predicate_applied = index_predicate_applied;
140        self.index_predicate_keys_rejected = index_predicate_keys_rejected;
141        self.distinct_keys_deduped = distinct_keys_deduped;
142    }
143}
144
145fn key_stream_comparator_from_plan<K>(
146    plan: &AccessPlannedQuery<K>,
147    fallback_direction: Direction,
148) -> KeyOrderComparator {
149    let derived_direction = plan.order.as_ref().map_or(fallback_direction, |order| {
150        derive_scan_direction(order, RouteOrderSlotPolicy::Last)
151    });
152
153    // Comparator and child-stream monotonicity must stay aligned until access-path
154    // stream production can emit keys under an order-spec-derived comparator.
155    let comparator_direction = if derived_direction == fallback_direction {
156        derived_direction
157    } else {
158        fallback_direction
159    };
160
161    KeyOrderComparator::from_direction(comparator_direction)
162}
163
164///
165/// FastPathKeyResult
166///
167/// Internal fast-path access result.
168/// Carries ordered keys plus observability metadata for shared execution phases.
169///
170
171struct FastPathKeyResult {
172    ordered_key_stream: OrderedKeyStreamBox,
173    rows_scanned: usize,
174    optimization: ExecutionOptimization,
175}
176
177///
178/// LoadExecutor
179///
180/// Load-plan executor with canonical post-access semantics.
181/// Coordinates fast paths, trace hooks, and pagination cursors.
182///
183
184#[derive(Clone)]
185pub(crate) struct LoadExecutor<E: EntityKind> {
186    db: Db<E::Canister>,
187    debug: bool,
188    _marker: PhantomData<E>,
189}
190
191impl<E> LoadExecutor<E>
192where
193    E: EntityKind + EntityValue,
194{
195    #[must_use]
196    pub(crate) const fn new(db: Db<E::Canister>, debug: bool) -> Self {
197        Self {
198            db,
199            debug,
200            _marker: PhantomData,
201        }
202    }
203
204    // Resolve one orderable aggregate target field into a stable slot with
205    // canonical field-error taxonomy mapping.
206    fn resolve_orderable_field_slot(target_field: &str) -> Result<FieldSlot, InternalError> {
207        resolve_orderable_aggregate_target_slot::<E>(target_field)
208            .map_err(AggregateFieldValueError::into_internal_error)
209    }
210
211    // Resolve one aggregate target field into a stable slot with canonical
212    // field-error taxonomy mapping.
213    fn resolve_any_field_slot(target_field: &str) -> Result<FieldSlot, InternalError> {
214        resolve_any_aggregate_target_slot::<E>(target_field)
215            .map_err(AggregateFieldValueError::into_internal_error)
216    }
217
218    // Resolve one numeric aggregate target field into a stable slot with
219    // canonical field-error taxonomy mapping.
220    fn resolve_numeric_field_slot(target_field: &str) -> Result<FieldSlot, InternalError> {
221        resolve_numeric_aggregate_target_slot::<E>(target_field)
222            .map_err(AggregateFieldValueError::into_internal_error)
223    }
224
225    pub(crate) fn execute(&self, plan: ExecutablePlan<E>) -> Result<Response<E>, InternalError> {
226        self.execute_paged_with_cursor(plan, PlannedCursor::none())
227            .map(|page| page.items)
228    }
229
230    pub(in crate::db) fn execute_paged_with_cursor(
231        &self,
232        plan: ExecutablePlan<E>,
233        cursor: impl Into<PlannedCursor>,
234    ) -> Result<CursorPage<E>, InternalError> {
235        self.execute_paged_with_cursor_traced(plan, cursor)
236            .map(|(page, _)| page)
237    }
238
239    #[expect(clippy::too_many_lines)]
240    pub(in crate::db) fn execute_paged_with_cursor_traced(
241        &self,
242        plan: ExecutablePlan<E>,
243        cursor: impl Into<PlannedCursor>,
244    ) -> Result<(CursorPage<E>, Option<ExecutionTrace>), InternalError> {
245        let cursor: PlannedCursor = plan.revalidate_planned_cursor(cursor.into())?;
246        let cursor_boundary = cursor.boundary().cloned();
247        let index_range_anchor = cursor.index_range_anchor().cloned();
248
249        if !plan.mode().is_load() {
250            return Err(InternalError::query_executor_invariant(
251                "load executor requires load plans",
252            ));
253        }
254        debug_assert!(
255            policy::validate_plan_shape(plan.as_inner()).is_ok(),
256            "load executor received a plan shape that bypassed planning validation",
257        );
258
259        let continuation_signature = plan.continuation_signature();
260        let index_prefix_specs = plan.index_prefix_specs()?.to_vec();
261        let index_range_specs = plan.index_range_specs()?.to_vec();
262        let route_plan = Self::build_execution_route_plan_for_load(
263            plan.as_inner(),
264            cursor_boundary.as_ref(),
265            index_range_anchor.as_ref(),
266            None,
267        )?;
268        let continuation_applied = !matches!(
269            route_plan.continuation_mode(),
270            crate::db::executor::route::ContinuationMode::Initial
271        );
272        let direction = route_plan.direction();
273        debug_assert_eq!(
274            route_plan.window().effective_offset,
275            plan.as_inner()
276                .effective_page_offset(cursor_boundary.as_ref()),
277            "route window effective offset must match logical plan offset semantics",
278        );
279        let mut execution_trace = self
280            .debug
281            .then(|| ExecutionTrace::new(plan.access(), direction, continuation_applied));
282        let plan = plan.into_inner();
283        let predicate_slots = compile_predicate_slots::<E>(&plan);
284
285        let result = (|| {
286            let mut span = Span::<E>::new(ExecKind::Load);
287
288            validate_executor_plan::<E>(&plan)?;
289            let ctx = self.db.recovered_context::<E>()?;
290            let execution_inputs = ExecutionInputs {
291                ctx: &ctx,
292                plan: &plan,
293                stream_bindings: AccessStreamBindings {
294                    index_prefix_specs: index_prefix_specs.as_slice(),
295                    index_range_specs: index_range_specs.as_slice(),
296                    index_range_anchor: index_range_anchor.as_ref(),
297                    direction,
298                },
299                predicate_slots: predicate_slots.as_ref(),
300            };
301
302            record_plan_metrics(&plan.access);
303            // Plan execution routing once, then execute in canonical order.
304            // Resolve one canonical key stream, then run shared page materialization/finalization.
305            let mut resolved = Self::resolve_execution_key_stream(
306                &execution_inputs,
307                &route_plan,
308                IndexPredicateCompileMode::ConservativeSubset,
309            )?;
310            let (mut page, keys_scanned, mut post_access_rows) =
311                Self::materialize_key_stream_into_page(
312                    &ctx,
313                    &plan,
314                    predicate_slots.as_ref(),
315                    resolved.key_stream.as_mut(),
316                    route_plan.scan_hints.load_scan_budget_hint,
317                    route_plan.streaming_access_shape_safe(),
318                    cursor_boundary.as_ref(),
319                    direction,
320                    continuation_signature,
321                )?;
322            let mut rows_scanned = resolved.rows_scanned_override.unwrap_or(keys_scanned);
323            let mut optimization = resolved.optimization;
324            let mut index_predicate_applied = resolved.index_predicate_applied;
325            let mut index_predicate_keys_rejected = resolved.index_predicate_keys_rejected;
326            let mut distinct_keys_deduped = resolved
327                .distinct_keys_deduped_counter
328                .as_ref()
329                .map_or(0, |counter| counter.get());
330
331            if Self::index_range_limited_residual_retry_required(
332                &plan,
333                cursor_boundary.as_ref(),
334                &route_plan,
335                rows_scanned,
336                post_access_rows,
337            ) {
338                let mut fallback_route_plan = route_plan;
339                fallback_route_plan.index_range_limit_spec = None;
340                let mut fallback_resolved = Self::resolve_execution_key_stream(
341                    &execution_inputs,
342                    &fallback_route_plan,
343                    IndexPredicateCompileMode::ConservativeSubset,
344                )?;
345                let (fallback_page, fallback_keys_scanned, fallback_post_access_rows) =
346                    Self::materialize_key_stream_into_page(
347                        &ctx,
348                        &plan,
349                        predicate_slots.as_ref(),
350                        fallback_resolved.key_stream.as_mut(),
351                        fallback_route_plan.scan_hints.load_scan_budget_hint,
352                        fallback_route_plan.streaming_access_shape_safe(),
353                        cursor_boundary.as_ref(),
354                        direction,
355                        continuation_signature,
356                    )?;
357                let fallback_rows_scanned = fallback_resolved
358                    .rows_scanned_override
359                    .unwrap_or(fallback_keys_scanned);
360                let fallback_distinct_keys_deduped = fallback_resolved
361                    .distinct_keys_deduped_counter
362                    .as_ref()
363                    .map_or(0, |counter| counter.get());
364
365                // Retry accounting keeps observability faithful to actual work.
366                rows_scanned = rows_scanned.saturating_add(fallback_rows_scanned);
367                optimization = fallback_resolved.optimization;
368                index_predicate_applied =
369                    index_predicate_applied || fallback_resolved.index_predicate_applied;
370                index_predicate_keys_rejected = index_predicate_keys_rejected
371                    .saturating_add(fallback_resolved.index_predicate_keys_rejected);
372                distinct_keys_deduped =
373                    distinct_keys_deduped.saturating_add(fallback_distinct_keys_deduped);
374                page = fallback_page;
375                post_access_rows = fallback_post_access_rows;
376            }
377
378            Ok(Self::finalize_execution(
379                page,
380                optimization,
381                rows_scanned,
382                post_access_rows,
383                index_predicate_applied,
384                index_predicate_keys_rejected,
385                distinct_keys_deduped,
386                &mut span,
387                &mut execution_trace,
388            ))
389        })();
390
391        result.map(|page| (page, execution_trace))
392    }
393
394    // Retry index-range limit pushdown when a bounded residual-filter pass may
395    // have under-filled the requested page window.
396    fn index_range_limited_residual_retry_required(
397        plan: &AccessPlannedQuery<E::Key>,
398        cursor_boundary: Option<&CursorBoundary>,
399        route_plan: &ExecutionRoutePlan,
400        rows_scanned: usize,
401        post_access_rows: usize,
402    ) -> bool {
403        let Some(limit_spec) = route_plan.index_range_limit_spec else {
404            return false;
405        };
406        if plan.predicate.is_none() {
407            return false;
408        }
409        if limit_spec.fetch == 0 {
410            return false;
411        }
412        let Some(limit) = plan.page.as_ref().and_then(|page| page.limit) else {
413            return false;
414        };
415        let keep_count =
416            compute_page_window(plan.effective_page_offset(cursor_boundary), limit, false)
417                .keep_count;
418        if keep_count == 0 {
419            return false;
420        }
421        if rows_scanned < limit_spec.fetch {
422            return false;
423        }
424
425        post_access_rows < keep_count
426    }
427
428    // Record shared observability outcome for any execution path.
429    fn finalize_path_outcome(
430        execution_trace: &mut Option<ExecutionTrace>,
431        optimization: Option<ExecutionOptimization>,
432        rows_scanned: usize,
433        rows_returned: usize,
434        index_predicate_applied: bool,
435        index_predicate_keys_rejected: u64,
436        distinct_keys_deduped: u64,
437    ) {
438        record_rows_scanned::<E>(rows_scanned);
439        if let Some(execution_trace) = execution_trace.as_mut() {
440            execution_trace.set_path_outcome(
441                optimization,
442                rows_scanned,
443                rows_returned,
444                index_predicate_applied,
445                index_predicate_keys_rejected,
446                distinct_keys_deduped,
447            );
448            debug_assert_eq!(
449                execution_trace.keys_scanned,
450                u64::try_from(rows_scanned).unwrap_or(u64::MAX),
451                "execution trace keys_scanned must match rows_scanned metrics input",
452            );
453        }
454    }
455
456    // Preserve PK fast-path cursor-boundary error classification at the executor boundary.
457    pub(in crate::db::executor) fn validate_pk_fast_path_boundary_if_applicable(
458        plan: &AccessPlannedQuery<E::Key>,
459        cursor_boundary: Option<&CursorBoundary>,
460    ) -> Result<(), InternalError> {
461        if !Self::pk_order_stream_fast_path_shape_supported(plan) {
462            return Ok(());
463        }
464        let _ = decode_pk_cursor_boundary::<E>(cursor_boundary)?;
465
466        Ok(())
467    }
468}