Skip to main content

icydb_core/db/query/plan/semantics/
logical.rs

1//! Module: query::plan::semantics::logical
2//! Responsibility: logical-plan semantic lowering from planner contracts to access-planned queries.
3//! Does not own: access-path index selection internals or runtime execution behavior.
4//! Boundary: derives planner-owned execution semantics, shape signatures, and continuation policy.
5
6use crate::{
7    db::{
8        access::{AccessPlan, ExecutableAccessPlan},
9        predicate::IndexCompileTarget,
10        predicate::{PredicateExecutionModel, PredicateProgram},
11        query::plan::{
12            AccessPlannedQuery, ContinuationPolicy, DistinctExecutionStrategy,
13            ExecutionShapeSignature, GroupPlan, GroupedAggregateExecutionSpec,
14            GroupedDistinctExecutionStrategy, LogicalPlan, PlannerRouteProfile, QueryMode,
15            ResolvedOrder, ResolvedOrderField, ResolvedOrderValueSource, ScalarPlan,
16            StaticPlanningShape, derive_logical_pushdown_eligibility,
17            expr::{
18                Expr, ProjectionField, ProjectionSpec, ScalarProjectionExpr,
19                compile_scalar_projection_expr, compile_scalar_projection_plan,
20                parse_supported_computed_order_expr,
21            },
22            grouped_aggregate_execution_specs_with_model,
23            grouped_aggregate_projection_specs_from_projection_spec,
24            grouped_cursor_policy_violation, lower_direct_projection_slots,
25            lower_projection_identity, lower_projection_intent,
26            residual_query_predicate_after_access_path_bounds,
27            residual_query_predicate_after_filtered_access,
28            resolved_grouped_distinct_execution_strategy_for_model,
29        },
30    },
31    error::InternalError,
32    model::{
33        entity::{EntityModel, resolve_field_slot},
34        index::IndexKeyItemsRef,
35    },
36};
37
38impl QueryMode {
39    /// True if this mode represents a load intent.
40    #[must_use]
41    pub const fn is_load(&self) -> bool {
42        match self {
43            Self::Load(_) => true,
44            Self::Delete(_) => false,
45        }
46    }
47
48    /// True if this mode represents a delete intent.
49    #[must_use]
50    pub const fn is_delete(&self) -> bool {
51        match self {
52            Self::Delete(_) => true,
53            Self::Load(_) => false,
54        }
55    }
56}
57
58impl LogicalPlan {
59    /// Borrow scalar semantic fields shared by scalar/grouped logical variants.
60    #[must_use]
61    pub(in crate::db) const fn scalar_semantics(&self) -> &ScalarPlan {
62        match self {
63            Self::Scalar(plan) => plan,
64            Self::Grouped(plan) => &plan.scalar,
65        }
66    }
67
68    /// Borrow scalar semantic fields mutably across logical variants for tests.
69    #[must_use]
70    #[cfg(test)]
71    pub(in crate::db) const fn scalar_semantics_mut(&mut self) -> &mut ScalarPlan {
72        match self {
73            Self::Scalar(plan) => plan,
74            Self::Grouped(plan) => &mut plan.scalar,
75        }
76    }
77
78    /// Test-only shorthand for explicit scalar semantic borrowing.
79    #[must_use]
80    #[cfg(test)]
81    pub(in crate::db) const fn scalar(&self) -> &ScalarPlan {
82        self.scalar_semantics()
83    }
84
85    /// Test-only shorthand for explicit mutable scalar semantic borrowing.
86    #[must_use]
87    #[cfg(test)]
88    pub(in crate::db) const fn scalar_mut(&mut self) -> &mut ScalarPlan {
89        self.scalar_semantics_mut()
90    }
91}
92
93impl AccessPlannedQuery {
94    /// Borrow scalar semantic fields shared by scalar/grouped logical variants.
95    #[must_use]
96    pub(in crate::db) const fn scalar_plan(&self) -> &ScalarPlan {
97        self.logical.scalar_semantics()
98    }
99
100    /// Borrow scalar semantic fields mutably across logical variants for tests.
101    #[must_use]
102    #[cfg(test)]
103    pub(in crate::db) const fn scalar_plan_mut(&mut self) -> &mut ScalarPlan {
104        self.logical.scalar_semantics_mut()
105    }
106
107    /// Test-only shorthand for explicit scalar plan borrowing.
108    #[must_use]
109    #[cfg(test)]
110    pub(in crate::db) const fn scalar(&self) -> &ScalarPlan {
111        self.scalar_plan()
112    }
113
114    /// Test-only shorthand for explicit mutable scalar plan borrowing.
115    #[must_use]
116    #[cfg(test)]
117    pub(in crate::db) const fn scalar_mut(&mut self) -> &mut ScalarPlan {
118        self.scalar_plan_mut()
119    }
120
121    /// Borrow grouped semantic fields when this plan is grouped.
122    #[must_use]
123    pub(in crate::db) const fn grouped_plan(&self) -> Option<&GroupPlan> {
124        match &self.logical {
125            LogicalPlan::Scalar(_) => None,
126            LogicalPlan::Grouped(plan) => Some(plan),
127        }
128    }
129
130    /// Lower this plan into one canonical planner-owned projection semantic spec.
131    #[must_use]
132    pub(in crate::db) fn projection_spec(&self, model: &EntityModel) -> ProjectionSpec {
133        if let Some(static_shape) = &self.static_planning_shape {
134            return static_shape.projection_spec.clone();
135        }
136
137        lower_projection_intent(model, &self.logical, &self.projection_selection)
138    }
139
140    /// Lower this plan into one projection semantic shape for identity hashing.
141    #[must_use]
142    pub(in crate::db::query) fn projection_spec_for_identity(&self) -> ProjectionSpec {
143        lower_projection_identity(&self.logical)
144    }
145
146    /// Return the executor-facing predicate after removing only filtered-index
147    /// guard clauses the chosen access path already proves.
148    ///
149    /// This conservative form is used by preparation/explain surfaces that
150    /// still need to see access-bound equalities as index-predicate input.
151    #[must_use]
152    pub(in crate::db) fn execution_preparation_predicate(&self) -> Option<PredicateExecutionModel> {
153        let query_predicate = self.scalar_plan().predicate.as_ref()?;
154
155        match self.access.selected_index_model() {
156            Some(index) => residual_query_predicate_after_filtered_access(index, query_predicate),
157            None => Some(query_predicate.clone()),
158        }
159    }
160
161    /// Return the executor-facing residual predicate after removing any
162    /// filtered-index guard clauses and fixed access-bound equalities already
163    /// guaranteed by the chosen path.
164    #[must_use]
165    pub(in crate::db) fn effective_execution_predicate(&self) -> Option<PredicateExecutionModel> {
166        // Phase 1: strip only filtered-index guard clauses the chosen access
167        // path already proves.
168        let filtered_residual = self.execution_preparation_predicate();
169        let filtered_residual = filtered_residual.as_ref()?;
170
171        // Phase 2: strip any additional equality clauses already guaranteed by
172        // the concrete access-path bounds, such as `tier = 'gold'` on one
173        // selected `IndexPrefix(tier='gold', ...)` route.
174        residual_query_predicate_after_access_path_bounds(self.access.as_path(), filtered_residual)
175    }
176
177    /// Borrow the planner-compiled execution-preparation predicate program.
178    #[must_use]
179    pub(in crate::db) const fn execution_preparation_compiled_predicate(
180        &self,
181    ) -> Option<&PredicateProgram> {
182        self.static_planning_shape()
183            .execution_preparation_compiled_predicate
184            .as_ref()
185    }
186
187    /// Borrow the planner-compiled effective runtime predicate program.
188    #[must_use]
189    pub(in crate::db) const fn effective_runtime_compiled_predicate(
190        &self,
191    ) -> Option<&PredicateProgram> {
192        self.static_planning_shape()
193            .effective_runtime_compiled_predicate
194            .as_ref()
195    }
196
197    /// Lower scalar DISTINCT semantics into one executor-facing execution strategy.
198    #[must_use]
199    pub(in crate::db) fn distinct_execution_strategy(&self) -> DistinctExecutionStrategy {
200        if !self.scalar_plan().distinct {
201            return DistinctExecutionStrategy::None;
202        }
203
204        // DISTINCT on duplicate-safe single-path access shapes is a planner
205        // no-op for runtime dedup mechanics. Composite shapes can surface
206        // duplicate keys and therefore retain explicit dedup execution.
207        match distinct_runtime_dedup_strategy(&self.access) {
208            Some(strategy) => strategy,
209            None => DistinctExecutionStrategy::None,
210        }
211    }
212
213    /// Freeze one planner-owned route profile after model validation completes.
214    pub(in crate::db) fn finalize_planner_route_profile_for_model(&mut self, model: &EntityModel) {
215        self.set_planner_route_profile(project_planner_route_profile_for_model(model, self));
216    }
217
218    /// Freeze planner-owned executor metadata after logical/access planning completes.
219    pub(in crate::db) fn finalize_static_planning_shape_for_model(
220        &mut self,
221        model: &EntityModel,
222    ) -> Result<(), InternalError> {
223        self.static_planning_shape = Some(project_static_planning_shape_for_model(model, self)?);
224
225        Ok(())
226    }
227
228    /// Build one immutable execution-shape signature contract for runtime layers.
229    #[must_use]
230    pub(in crate::db) fn execution_shape_signature(
231        &self,
232        entity_path: &'static str,
233    ) -> ExecutionShapeSignature {
234        ExecutionShapeSignature::new(self.continuation_signature(entity_path))
235    }
236
237    /// Return whether the chosen access contract fully satisfies the current
238    /// scalar query predicate without any additional post-access filtering.
239    #[must_use]
240    pub(in crate::db) fn predicate_fully_satisfied_by_access_contract(&self) -> bool {
241        self.scalar_plan().predicate.is_some() && self.effective_execution_predicate().is_none()
242    }
243
244    /// Return whether the scalar logical predicate still requires post-access
245    /// filtering after accounting for filtered-index guard predicates and
246    /// access-path equality bounds.
247    #[must_use]
248    pub(in crate::db) fn has_residual_predicate(&self) -> bool {
249        self.scalar_plan().predicate.is_some()
250            && !self.predicate_fully_satisfied_by_access_contract()
251    }
252
253    /// Borrow the planner-frozen compiled scalar projection program.
254    #[must_use]
255    pub(in crate::db) fn scalar_projection_plan(&self) -> Option<&[ScalarProjectionExpr]> {
256        self.static_planning_shape()
257            .scalar_projection_plan
258            .as_deref()
259    }
260
261    /// Borrow the planner-frozen primary-key field name.
262    #[must_use]
263    pub(in crate::db) const fn primary_key_name(&self) -> &'static str {
264        self.static_planning_shape().primary_key_name
265    }
266
267    /// Borrow the planner-frozen projection slot reachability set.
268    #[must_use]
269    pub(in crate::db) const fn projection_referenced_slots(&self) -> &[usize] {
270        self.static_planning_shape()
271            .projection_referenced_slots
272            .as_slice()
273    }
274
275    /// Borrow the planner-frozen mask for direct projected output slots.
276    #[must_use]
277    #[cfg(any(test, feature = "perf-attribution"))]
278    pub(in crate::db) const fn projected_slot_mask(&self) -> &[bool] {
279        self.static_planning_shape().projected_slot_mask.as_slice()
280    }
281
282    /// Return whether projection remains the full model-identity field list.
283    #[must_use]
284    pub(in crate::db) const fn projection_is_model_identity(&self) -> bool {
285        self.static_planning_shape().projection_is_model_identity
286    }
287
288    /// Borrow the planner-frozen ORDER BY slot reachability set, if any.
289    #[must_use]
290    pub(in crate::db) fn order_referenced_slots(&self) -> Option<&[usize]> {
291        self.static_planning_shape()
292            .order_referenced_slots
293            .as_deref()
294    }
295
296    /// Borrow the planner-frozen resolved ORDER BY program, if one exists.
297    #[must_use]
298    pub(in crate::db) const fn resolved_order(&self) -> Option<&ResolvedOrder> {
299        self.static_planning_shape().resolved_order.as_ref()
300    }
301
302    /// Borrow the planner-frozen access slot map used by index predicate compilation.
303    #[must_use]
304    pub(in crate::db) fn slot_map(&self) -> Option<&[usize]> {
305        self.static_planning_shape().slot_map.as_deref()
306    }
307
308    /// Borrow grouped aggregate execution specs already resolved during static planning.
309    #[must_use]
310    pub(in crate::db) fn grouped_aggregate_execution_specs(
311        &self,
312    ) -> Option<&[GroupedAggregateExecutionSpec]> {
313        self.static_planning_shape()
314            .grouped_aggregate_execution_specs
315            .as_deref()
316    }
317
318    /// Borrow the planner-resolved grouped DISTINCT execution strategy when present.
319    #[must_use]
320    pub(in crate::db) const fn grouped_distinct_execution_strategy(
321        &self,
322    ) -> Option<&GroupedDistinctExecutionStrategy> {
323        self.static_planning_shape()
324            .grouped_distinct_execution_strategy
325            .as_ref()
326    }
327
328    /// Borrow the frozen projection semantic shape without reopening model ownership.
329    #[must_use]
330    pub(in crate::db) const fn frozen_projection_spec(&self) -> &ProjectionSpec {
331        &self.static_planning_shape().projection_spec
332    }
333
334    /// Borrow the frozen direct projection slots without reopening model ownership.
335    #[must_use]
336    pub(in crate::db) fn frozen_direct_projection_slots(&self) -> Option<&[usize]> {
337        self.static_planning_shape()
338            .projection_direct_slots
339            .as_deref()
340    }
341
342    /// Borrow the planner-frozen key-item-aware compile targets for the chosen access path.
343    #[must_use]
344    pub(in crate::db) fn index_compile_targets(&self) -> Option<&[IndexCompileTarget]> {
345        self.static_planning_shape()
346            .index_compile_targets
347            .as_deref()
348    }
349
350    const fn static_planning_shape(&self) -> &StaticPlanningShape {
351        self.static_planning_shape
352            .as_ref()
353            .expect("access-planned queries must freeze static planning shape before execution")
354    }
355}
356
357fn distinct_runtime_dedup_strategy<K>(access: &AccessPlan<K>) -> Option<DistinctExecutionStrategy> {
358    match access {
359        AccessPlan::Union(_) | AccessPlan::Intersection(_) => {
360            Some(DistinctExecutionStrategy::PreOrdered)
361        }
362        AccessPlan::Path(path) if path.as_ref().is_index_multi_lookup() => {
363            Some(DistinctExecutionStrategy::HashMaterialize)
364        }
365        AccessPlan::Path(_) => None,
366    }
367}
368
369fn derive_continuation_policy_validated(plan: &AccessPlannedQuery) -> ContinuationPolicy {
370    let is_grouped_safe = plan
371        .grouped_plan()
372        .is_none_or(|grouped| grouped_cursor_policy_violation(grouped, true).is_none());
373
374    ContinuationPolicy::new(
375        true, // Continuation resume windows require anchor semantics for pushdown-safe replay.
376        true, // Continuation resumes must advance strictly to prevent replay/regression loops.
377        is_grouped_safe,
378    )
379}
380
381/// Project one planner-owned route profile from the finalized logical+access plan.
382#[must_use]
383pub(in crate::db) fn project_planner_route_profile_for_model(
384    model: &EntityModel,
385    plan: &AccessPlannedQuery,
386) -> PlannerRouteProfile {
387    let secondary_order_contract = plan
388        .scalar_plan()
389        .order
390        .as_ref()
391        .and_then(|order| order.deterministic_secondary_order_contract(model.primary_key.name));
392
393    PlannerRouteProfile::new(
394        derive_continuation_policy_validated(plan),
395        derive_logical_pushdown_eligibility(plan, secondary_order_contract.as_ref()),
396        secondary_order_contract,
397    )
398}
399
400fn project_static_planning_shape_for_model(
401    model: &EntityModel,
402    plan: &AccessPlannedQuery,
403) -> Result<StaticPlanningShape, InternalError> {
404    let projection_spec = lower_projection_intent(model, &plan.logical, &plan.projection_selection);
405    let execution_preparation_compiled_predicate = plan
406        .execution_preparation_predicate()
407        .as_ref()
408        .map(|predicate| PredicateProgram::compile_with_model(model, predicate));
409    let effective_runtime_compiled_predicate = plan
410        .effective_execution_predicate()
411        .as_ref()
412        .map(|predicate| PredicateProgram::compile_with_model(model, predicate));
413    let scalar_projection_plan =
414        if plan.grouped_plan().is_none() {
415            Some(compile_scalar_projection_plan(model, &projection_spec).ok_or_else(|| {
416            InternalError::query_executor_invariant(
417                "scalar projection program must compile during static planning finalization",
418            )
419        })?)
420        } else {
421            None
422        };
423    let grouped_aggregate_execution_specs = if let Some(grouped) = plan.grouped_plan() {
424        #[cfg(not(test))]
425        let aggregate_projection_specs = grouped_aggregate_projection_specs_from_projection_spec(
426            &projection_spec,
427            grouped.group.group_fields.as_slice(),
428            grouped.group.aggregates.as_slice(),
429        );
430        #[cfg(test)]
431        let aggregate_projection_specs = grouped_aggregate_projection_specs_from_projection_spec(
432            &projection_spec,
433            grouped.group.group_fields.as_slice(),
434            grouped.group.aggregates.as_slice(),
435        )?;
436
437        Some(grouped_aggregate_execution_specs_with_model(
438            model,
439            aggregate_projection_specs.as_slice(),
440        )?)
441    } else {
442        None
443    };
444    let grouped_distinct_execution_strategy = if let Some(grouped) = plan.grouped_plan() {
445        Some(resolved_grouped_distinct_execution_strategy_for_model(
446            model,
447            grouped.group.group_fields.as_slice(),
448            grouped.group.aggregates.as_slice(),
449            grouped.having.as_ref(),
450        )?)
451    } else {
452        None
453    };
454    let projection_direct_slots =
455        lower_direct_projection_slots(model, &plan.logical, &plan.projection_selection);
456    let projection_referenced_slots =
457        projection_referenced_slots_for_spec(model, &projection_spec)?;
458    let projected_slot_mask =
459        projected_slot_mask_for_spec(model, &projection_spec, projection_direct_slots.as_deref());
460    let projection_is_model_identity =
461        projection_is_model_identity_for_spec(model, &projection_spec);
462    let resolved_order = resolved_order_for_plan(model, plan)?;
463    let order_referenced_slots = order_referenced_slots_for_resolved_order(resolved_order.as_ref());
464    let slot_map = slot_map_for_model_plan(model, plan);
465    let index_compile_targets = index_compile_targets_for_model_plan(model, plan);
466
467    Ok(StaticPlanningShape {
468        primary_key_name: model.primary_key.name,
469        projection_spec,
470        execution_preparation_compiled_predicate,
471        effective_runtime_compiled_predicate,
472        scalar_projection_plan,
473        grouped_aggregate_execution_specs,
474        grouped_distinct_execution_strategy,
475        projection_direct_slots,
476        projection_referenced_slots,
477        projected_slot_mask,
478        projection_is_model_identity,
479        resolved_order,
480        order_referenced_slots,
481        slot_map,
482        index_compile_targets,
483    })
484}
485
486fn projection_referenced_slots_for_spec(
487    model: &EntityModel,
488    projection: &ProjectionSpec,
489) -> Result<Vec<usize>, InternalError> {
490    let mut referenced = vec![false; model.fields().len()];
491
492    for field in projection.fields() {
493        match field {
494            ProjectionField::Scalar { expr, .. } => {
495                mark_projection_expr_slots(model, expr, referenced.as_mut_slice())?;
496            }
497        }
498    }
499
500    Ok(referenced
501        .into_iter()
502        .enumerate()
503        .filter_map(|(slot, required)| required.then_some(slot))
504        .collect())
505}
506
507fn mark_projection_expr_slots(
508    model: &EntityModel,
509    expr: &Expr,
510    referenced: &mut [bool],
511) -> Result<(), InternalError> {
512    match expr {
513        Expr::Field(field_id) => {
514            let field_name = field_id.as_str();
515            let slot = resolve_field_slot(model, field_name).ok_or_else(|| {
516                InternalError::query_invalid_logical_plan(format!(
517                    "projection expression references unknown field '{field_name}'",
518                ))
519            })?;
520            referenced[slot] = true;
521        }
522        Expr::Literal(_) => {}
523        Expr::FunctionCall { args, .. } => {
524            for arg in args {
525                mark_projection_expr_slots(model, arg, referenced)?;
526            }
527        }
528        Expr::Aggregate(_) => {}
529        #[cfg(test)]
530        Expr::Alias { expr, .. } => {
531            mark_projection_expr_slots(model, expr.as_ref(), referenced)?;
532        }
533        #[cfg(test)]
534        Expr::Unary { expr, .. } => {
535            mark_projection_expr_slots(model, expr.as_ref(), referenced)?;
536        }
537        Expr::Binary { left, right, .. } => {
538            mark_projection_expr_slots(model, left.as_ref(), referenced)?;
539            mark_projection_expr_slots(model, right.as_ref(), referenced)?;
540        }
541    }
542
543    Ok(())
544}
545
546fn projected_slot_mask_for_spec(
547    model: &EntityModel,
548    projection: &ProjectionSpec,
549    direct_projection_slots: Option<&[usize]>,
550) -> Vec<bool> {
551    let mut projected_slots = vec![false; model.fields().len()];
552
553    let Some(direct_projection_slots) = direct_projection_slots else {
554        return projected_slots;
555    };
556
557    for (field, slot) in projection
558        .fields()
559        .zip(direct_projection_slots.iter().copied())
560    {
561        if matches!(field, ProjectionField::Scalar { .. })
562            && let Some(projected) = projected_slots.get_mut(slot)
563        {
564            *projected = true;
565        }
566    }
567
568    projected_slots
569}
570
571fn projection_is_model_identity_for_spec(model: &EntityModel, projection: &ProjectionSpec) -> bool {
572    if projection.len() != model.fields().len() {
573        return false;
574    }
575
576    for (field_model, projected_field) in model.fields().iter().zip(projection.fields()) {
577        match projected_field {
578            ProjectionField::Scalar {
579                expr: Expr::Field(field_id),
580                alias: None,
581            } if field_id.as_str() == field_model.name() => {}
582            ProjectionField::Scalar { .. } => return false,
583        }
584    }
585
586    true
587}
588
589fn resolved_order_for_plan(
590    model: &EntityModel,
591    plan: &AccessPlannedQuery,
592) -> Result<Option<ResolvedOrder>, InternalError> {
593    let Some(order) = plan.scalar_plan().order.as_ref() else {
594        return Ok(None);
595    };
596
597    let mut fields = Vec::with_capacity(order.fields.len());
598    for (field, direction) in &order.fields {
599        fields.push(ResolvedOrderField::new(
600            resolved_order_value_source_for_field(model, field)?,
601            *direction,
602        ));
603    }
604
605    Ok(Some(ResolvedOrder::new(fields)))
606}
607
608fn resolved_order_value_source_for_field(
609    model: &EntityModel,
610    field: &str,
611) -> Result<ResolvedOrderValueSource, InternalError> {
612    if let Some(expr) = parse_supported_computed_order_expr(field) {
613        validate_resolved_order_expr_fields(model, &expr, field)?;
614        let compiled = compile_scalar_projection_expr(model, &expr).ok_or_else(|| {
615            InternalError::query_invalid_logical_plan(format!(
616                "order expression '{field}' did not stay on the scalar expression seam",
617            ))
618        })?;
619
620        return Ok(ResolvedOrderValueSource::expression(compiled));
621    }
622
623    let slot = resolve_field_slot(model, field).ok_or_else(|| {
624        InternalError::query_invalid_logical_plan(format!(
625            "order expression references unknown field '{field}'",
626        ))
627    })?;
628
629    Ok(ResolvedOrderValueSource::direct_field(slot))
630}
631
632fn validate_resolved_order_expr_fields(
633    model: &EntityModel,
634    expr: &Expr,
635    rendered: &str,
636) -> Result<(), InternalError> {
637    match expr {
638        Expr::Field(field_id) => {
639            resolve_field_slot(model, field_id.as_str()).ok_or_else(|| {
640                InternalError::query_invalid_logical_plan(format!(
641                    "order expression references unknown field '{rendered}'",
642                ))
643            })?;
644        }
645        Expr::Literal(_) => {}
646        Expr::FunctionCall { args, .. } => {
647            for arg in args {
648                validate_resolved_order_expr_fields(model, arg, rendered)?;
649            }
650        }
651        Expr::Binary { left, right, .. } => {
652            validate_resolved_order_expr_fields(model, left.as_ref(), rendered)?;
653            validate_resolved_order_expr_fields(model, right.as_ref(), rendered)?;
654        }
655        Expr::Aggregate(_) => {
656            return Err(InternalError::query_invalid_logical_plan(format!(
657                "order expression '{rendered}' did not stay on the scalar expression seam",
658            )));
659        }
660        #[cfg(test)]
661        Expr::Alias { .. } | Expr::Unary { .. } => {
662            return Err(InternalError::query_invalid_logical_plan(format!(
663                "order expression '{rendered}' did not stay on the scalar expression seam",
664            )));
665        }
666    }
667
668    Ok(())
669}
670
671fn order_referenced_slots_for_resolved_order(
672    resolved_order: Option<&ResolvedOrder>,
673) -> Option<Vec<usize>> {
674    let resolved_order = resolved_order?;
675    let mut referenced = Vec::new();
676
677    // Keep one stable slot list without re-parsing order expressions after the
678    // planner has already frozen structural ORDER BY sources.
679    for field in resolved_order.fields() {
680        field.source().extend_referenced_slots(&mut referenced);
681    }
682
683    Some(referenced)
684}
685
686fn slot_map_for_model_plan(model: &EntityModel, plan: &AccessPlannedQuery) -> Option<Vec<usize>> {
687    let access_strategy = plan.access.resolve_strategy();
688    let executable = access_strategy.executable();
689
690    resolved_index_slots_for_access_path(model, executable)
691}
692
693fn resolved_index_slots_for_access_path(
694    model: &EntityModel,
695    access: &ExecutableAccessPlan<'_, crate::value::Value>,
696) -> Option<Vec<usize>> {
697    let path = access.as_path()?;
698    let path_capabilities = path.capabilities();
699    let index_fields = path_capabilities.index_fields_for_slot_map()?;
700    let mut slots = Vec::with_capacity(index_fields.len());
701
702    for field_name in index_fields {
703        let slot = resolve_field_slot(model, field_name)?;
704        slots.push(slot);
705    }
706
707    Some(slots)
708}
709
710fn index_compile_targets_for_model_plan(
711    model: &EntityModel,
712    plan: &AccessPlannedQuery,
713) -> Option<Vec<IndexCompileTarget>> {
714    let index = plan.access.as_path()?.selected_index_model()?;
715    let mut targets = Vec::new();
716
717    match index.key_items() {
718        IndexKeyItemsRef::Fields(fields) => {
719            for (component_index, &field_name) in fields.iter().enumerate() {
720                let field_slot = resolve_field_slot(model, field_name)?;
721                targets.push(IndexCompileTarget {
722                    component_index,
723                    field_slot,
724                    key_item: crate::model::index::IndexKeyItem::Field(field_name),
725                });
726            }
727        }
728        IndexKeyItemsRef::Items(items) => {
729            for (component_index, &key_item) in items.iter().enumerate() {
730                let field_slot = resolve_field_slot(model, key_item.field())?;
731                targets.push(IndexCompileTarget {
732                    component_index,
733                    field_slot,
734                    key_item,
735                });
736            }
737        }
738    }
739
740    Some(targets)
741}