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, projection_field_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 = "diagnostics"))]
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 = compile_optional_predicate_with_model(
406        model,
407        plan.execution_preparation_predicate().as_ref(),
408    );
409    let effective_runtime_compiled_predicate =
410        compile_optional_predicate_with_model(model, plan.effective_execution_predicate().as_ref());
411    let scalar_projection_plan =
412        if plan.grouped_plan().is_none() {
413            Some(compile_scalar_projection_plan(model, &projection_spec).ok_or_else(|| {
414            InternalError::query_executor_invariant(
415                "scalar projection program must compile during static planning finalization",
416            )
417        })?)
418        } else {
419            None
420        };
421    let (grouped_aggregate_execution_specs, grouped_distinct_execution_strategy) =
422        resolve_grouped_static_planning_semantics(model, plan, &projection_spec)?;
423    let projection_direct_slots =
424        lower_direct_projection_slots(model, &plan.logical, &plan.projection_selection);
425    let projection_referenced_slots =
426        projection_referenced_slots_for_spec(model, &projection_spec)?;
427    let projected_slot_mask =
428        projected_slot_mask_for_spec(model, &projection_spec, projection_direct_slots.as_deref());
429    let projection_is_model_identity =
430        projection_is_model_identity_for_spec(model, &projection_spec);
431    let resolved_order = resolved_order_for_plan(model, plan)?;
432    let order_referenced_slots = order_referenced_slots_for_resolved_order(resolved_order.as_ref());
433    let slot_map = slot_map_for_model_plan(model, plan);
434    let index_compile_targets = index_compile_targets_for_model_plan(model, plan);
435
436    Ok(StaticPlanningShape {
437        primary_key_name: model.primary_key.name,
438        projection_spec,
439        execution_preparation_compiled_predicate,
440        effective_runtime_compiled_predicate,
441        scalar_projection_plan,
442        grouped_aggregate_execution_specs,
443        grouped_distinct_execution_strategy,
444        projection_direct_slots,
445        projection_referenced_slots,
446        projected_slot_mask,
447        projection_is_model_identity,
448        resolved_order,
449        order_referenced_slots,
450        slot_map,
451        index_compile_targets,
452    })
453}
454
455// Compile one optional planner-frozen predicate program while keeping the
456// static planning assembly path free of repeated `Option` mapping boilerplate.
457fn compile_optional_predicate_with_model(
458    model: &EntityModel,
459    predicate: Option<&PredicateExecutionModel>,
460) -> Option<PredicateProgram> {
461    predicate.map(|predicate| PredicateProgram::compile_with_model(model, predicate))
462}
463
464// Resolve the grouped-only static planning semantics bundle once so grouped
465// aggregate execution specs and grouped DISTINCT strategy stay derived under
466// one shared grouped-plan branch.
467fn resolve_grouped_static_planning_semantics(
468    model: &EntityModel,
469    plan: &AccessPlannedQuery,
470    projection_spec: &ProjectionSpec,
471) -> Result<
472    (
473        Option<Vec<GroupedAggregateExecutionSpec>>,
474        Option<GroupedDistinctExecutionStrategy>,
475    ),
476    InternalError,
477> {
478    let Some(grouped) = plan.grouped_plan() else {
479        return Ok((None, None));
480    };
481
482    #[cfg(not(test))]
483    let aggregate_projection_specs = grouped_aggregate_projection_specs_from_projection_spec(
484        projection_spec,
485        grouped.group.group_fields.as_slice(),
486        grouped.group.aggregates.as_slice(),
487    );
488    #[cfg(test)]
489    let aggregate_projection_specs = grouped_aggregate_projection_specs_from_projection_spec(
490        projection_spec,
491        grouped.group.group_fields.as_slice(),
492        grouped.group.aggregates.as_slice(),
493    )?;
494
495    let grouped_aggregate_execution_specs = Some(grouped_aggregate_execution_specs_with_model(
496        model,
497        aggregate_projection_specs.as_slice(),
498    )?);
499    let grouped_distinct_execution_strategy =
500        Some(resolved_grouped_distinct_execution_strategy_for_model(
501            model,
502            grouped.group.group_fields.as_slice(),
503            grouped.group.aggregates.as_slice(),
504            grouped.having.as_ref(),
505        )?);
506
507    Ok((
508        grouped_aggregate_execution_specs,
509        grouped_distinct_execution_strategy,
510    ))
511}
512
513fn projection_referenced_slots_for_spec(
514    model: &EntityModel,
515    projection: &ProjectionSpec,
516) -> Result<Vec<usize>, InternalError> {
517    let mut referenced = vec![false; model.fields().len()];
518
519    for field in projection.fields() {
520        mark_projection_expr_slots(
521            model,
522            projection_field_expr(field),
523            referenced.as_mut_slice(),
524        )?;
525    }
526
527    Ok(referenced
528        .into_iter()
529        .enumerate()
530        .filter_map(|(slot, required)| required.then_some(slot))
531        .collect())
532}
533
534fn mark_projection_expr_slots(
535    model: &EntityModel,
536    expr: &Expr,
537    referenced: &mut [bool],
538) -> Result<(), InternalError> {
539    match expr {
540        Expr::Field(field_id) => {
541            let field_name = field_id.as_str();
542            let slot = resolve_required_field_slot(model, field_name, || {
543                InternalError::query_invalid_logical_plan(format!(
544                    "projection expression references unknown field '{field_name}'",
545                ))
546            })?;
547            referenced[slot] = true;
548        }
549        Expr::Literal(_) => {}
550        Expr::FunctionCall { args, .. } => {
551            for arg in args {
552                mark_projection_expr_slots(model, arg, referenced)?;
553            }
554        }
555        Expr::Aggregate(_) => {}
556        #[cfg(test)]
557        Expr::Alias { expr, .. } => {
558            mark_projection_expr_slots(model, expr.as_ref(), referenced)?;
559        }
560        #[cfg(test)]
561        Expr::Unary { expr, .. } => {
562            mark_projection_expr_slots(model, expr.as_ref(), referenced)?;
563        }
564        Expr::Binary { left, right, .. } => {
565            mark_projection_expr_slots(model, left.as_ref(), referenced)?;
566            mark_projection_expr_slots(model, right.as_ref(), referenced)?;
567        }
568    }
569
570    Ok(())
571}
572
573fn projected_slot_mask_for_spec(
574    model: &EntityModel,
575    projection: &ProjectionSpec,
576    direct_projection_slots: Option<&[usize]>,
577) -> Vec<bool> {
578    let mut projected_slots = vec![false; model.fields().len()];
579
580    let Some(direct_projection_slots) = direct_projection_slots else {
581        return projected_slots;
582    };
583
584    for (field, slot) in projection
585        .fields()
586        .zip(direct_projection_slots.iter().copied())
587    {
588        if matches!(field, ProjectionField::Scalar { .. })
589            && let Some(projected) = projected_slots.get_mut(slot)
590        {
591            *projected = true;
592        }
593    }
594
595    projected_slots
596}
597
598fn projection_is_model_identity_for_spec(model: &EntityModel, projection: &ProjectionSpec) -> bool {
599    if projection.len() != model.fields().len() {
600        return false;
601    }
602
603    for (field_model, projected_field) in model.fields().iter().zip(projection.fields()) {
604        match projected_field {
605            ProjectionField::Scalar {
606                expr: Expr::Field(field_id),
607                alias: None,
608            } if field_id.as_str() == field_model.name() => {}
609            ProjectionField::Scalar { .. } => return false,
610        }
611    }
612
613    true
614}
615
616fn resolved_order_for_plan(
617    model: &EntityModel,
618    plan: &AccessPlannedQuery,
619) -> Result<Option<ResolvedOrder>, InternalError> {
620    let Some(order) = plan.scalar_plan().order.as_ref() else {
621        return Ok(None);
622    };
623
624    let mut fields = Vec::with_capacity(order.fields.len());
625    for (field, direction) in &order.fields {
626        fields.push(ResolvedOrderField::new(
627            resolved_order_value_source_for_field(model, field)?,
628            *direction,
629        ));
630    }
631
632    Ok(Some(ResolvedOrder::new(fields)))
633}
634
635fn resolved_order_value_source_for_field(
636    model: &EntityModel,
637    field: &str,
638) -> Result<ResolvedOrderValueSource, InternalError> {
639    if let Some(expr) = parse_supported_computed_order_expr(field) {
640        validate_resolved_order_expr_fields(model, &expr, field)?;
641        let compiled = compile_scalar_projection_expr(model, &expr)
642            .ok_or_else(|| order_expression_scalar_seam_error(field))?;
643
644        return Ok(ResolvedOrderValueSource::expression(compiled));
645    }
646
647    let slot = resolve_required_field_slot(model, field, || {
648        InternalError::query_invalid_logical_plan(format!(
649            "order expression references unknown field '{field}'",
650        ))
651    })?;
652
653    Ok(ResolvedOrderValueSource::direct_field(slot))
654}
655
656fn validate_resolved_order_expr_fields(
657    model: &EntityModel,
658    expr: &Expr,
659    rendered: &str,
660) -> Result<(), InternalError> {
661    match expr {
662        Expr::Field(field_id) => {
663            resolve_required_field_slot(model, field_id.as_str(), || {
664                InternalError::query_invalid_logical_plan(format!(
665                    "order expression references unknown field '{rendered}'",
666                ))
667            })?;
668        }
669        Expr::Literal(_) => {}
670        Expr::FunctionCall { args, .. } => {
671            for arg in args {
672                validate_resolved_order_expr_fields(model, arg, rendered)?;
673            }
674        }
675        Expr::Binary { left, right, .. } => {
676            validate_resolved_order_expr_fields(model, left.as_ref(), rendered)?;
677            validate_resolved_order_expr_fields(model, right.as_ref(), rendered)?;
678        }
679        Expr::Aggregate(_) => {
680            return Err(order_expression_scalar_seam_error(rendered));
681        }
682        #[cfg(test)]
683        Expr::Alias { .. } | Expr::Unary { .. } => {
684            return Err(order_expression_scalar_seam_error(rendered));
685        }
686    }
687
688    Ok(())
689}
690
691// Resolve one model field slot while keeping planner invalid-logical-plan
692// error construction at the callsite that owns the diagnostic wording.
693fn resolve_required_field_slot<F>(
694    model: &EntityModel,
695    field: &str,
696    invalid_plan_error: F,
697) -> Result<usize, InternalError>
698where
699    F: FnOnce() -> InternalError,
700{
701    resolve_field_slot(model, field).ok_or_else(invalid_plan_error)
702}
703
704// Keep the scalar-order expression seam violation text under one helper so the
705// parse validation and compile validation paths do not drift.
706fn order_expression_scalar_seam_error(rendered: &str) -> InternalError {
707    InternalError::query_invalid_logical_plan(format!(
708        "order expression '{rendered}' did not stay on the scalar expression seam",
709    ))
710}
711
712fn order_referenced_slots_for_resolved_order(
713    resolved_order: Option<&ResolvedOrder>,
714) -> Option<Vec<usize>> {
715    let resolved_order = resolved_order?;
716    let mut referenced = Vec::new();
717
718    // Keep one stable slot list without re-parsing order expressions after the
719    // planner has already frozen structural ORDER BY sources.
720    for field in resolved_order.fields() {
721        field.source().extend_referenced_slots(&mut referenced);
722    }
723
724    Some(referenced)
725}
726
727fn slot_map_for_model_plan(model: &EntityModel, plan: &AccessPlannedQuery) -> Option<Vec<usize>> {
728    let access_strategy = plan.access.resolve_strategy();
729    let executable = access_strategy.executable();
730
731    resolved_index_slots_for_access_path(model, executable)
732}
733
734fn resolved_index_slots_for_access_path(
735    model: &EntityModel,
736    access: &ExecutableAccessPlan<'_, crate::value::Value>,
737) -> Option<Vec<usize>> {
738    let path = access.as_path()?;
739    let path_capabilities = path.capabilities();
740    let index_fields = path_capabilities.index_fields_for_slot_map()?;
741    let mut slots = Vec::with_capacity(index_fields.len());
742
743    for field_name in index_fields {
744        let slot = resolve_field_slot(model, field_name)?;
745        slots.push(slot);
746    }
747
748    Some(slots)
749}
750
751fn index_compile_targets_for_model_plan(
752    model: &EntityModel,
753    plan: &AccessPlannedQuery,
754) -> Option<Vec<IndexCompileTarget>> {
755    let index = plan.access.as_path()?.selected_index_model()?;
756    let mut targets = Vec::new();
757
758    match index.key_items() {
759        IndexKeyItemsRef::Fields(fields) => {
760            for (component_index, &field_name) in fields.iter().enumerate() {
761                let field_slot = resolve_field_slot(model, field_name)?;
762                targets.push(IndexCompileTarget {
763                    component_index,
764                    field_slot,
765                    key_item: crate::model::index::IndexKeyItem::Field(field_name),
766                });
767            }
768        }
769        IndexKeyItemsRef::Items(items) => {
770            for (component_index, &key_item) in items.iter().enumerate() {
771                let field_slot = resolve_field_slot(model, key_item.field())?;
772                targets.push(IndexCompileTarget {
773                    component_index,
774                    field_slot,
775                    key_item,
776                });
777            }
778        }
779    }
780
781    Some(targets)
782}