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