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::db::predicate::MissingRowPolicy;
7use crate::{
8    db::{
9        access::{AccessPlan, ExecutableAccessPlan, SemanticIndexKeyItemRef},
10        predicate::{IndexCompileTarget, IndexCompileTargetKind, Predicate, PredicateProgram},
11        query::plan::{
12            AccessPlannedQuery, ContinuationPolicy, DistinctExecutionStrategy,
13            EffectiveRuntimeFilterProgram, ExecutionShapeSignature, GroupPlan,
14            GroupedAggregateExecutionSpec, GroupedDistinctExecutionStrategy, GroupedPlanStrategy,
15            LogicalPlan, PlannerRouteProfile, PredicatePushdownDiagnostics, QueryMode,
16            ResidualFilterContract, ResidualFilterShape, ResolvedOrder, ResolvedOrderField,
17            ResolvedOrderValueSource, ScalarPlan, StaticExecutionPlanningContract,
18            derive_logical_pushdown_eligibility,
19            expr::{
20                CompiledExpr, Expr, ProjectionSpec, compile_scalar_projection_expr_with_schema,
21                compile_scalar_projection_plan_with_schema,
22            },
23            extend_unique_grouped_aggregate_specs_from_expr, grouped_aggregate_execution_specs,
24            grouped_aggregate_specs_from_projection_spec, grouped_cursor_policy_violation,
25            grouped_plan_strategy, lower_data_row_direct_projection_slots_with_schema,
26            lower_direct_projection_slots_with_schema, lower_projection_identity,
27            lower_projection_intent_with_schema, residual_query_predicate_after_access_path_bounds,
28            residual_query_predicate_after_filtered_access_contract,
29            resolved_grouped_distinct_execution_strategy_with_schema_info,
30        },
31        schema::SchemaInfo,
32    },
33    error::InternalError,
34};
35
36impl QueryMode {
37    /// True if this mode represents a load intent.
38    #[must_use]
39    pub const fn is_load(&self) -> bool {
40        match self {
41            Self::Load(_) => true,
42            Self::Delete(_) => false,
43        }
44    }
45
46    /// True if this mode represents a delete intent.
47    #[must_use]
48    pub const fn is_delete(&self) -> bool {
49        match self {
50            Self::Delete(_) => true,
51            Self::Load(_) => false,
52        }
53    }
54}
55
56impl LogicalPlan {
57    /// Borrow scalar semantic fields shared by scalar/grouped logical variants.
58    #[must_use]
59    pub(in crate::db) const fn scalar_semantics(&self) -> &ScalarPlan {
60        match self {
61            Self::Scalar(plan) => plan,
62            Self::Grouped(plan) => &plan.scalar,
63        }
64    }
65}
66
67impl AccessPlannedQuery {
68    /// Borrow scalar semantic fields shared by scalar/grouped logical variants.
69    #[must_use]
70    pub(in crate::db) const fn scalar_plan(&self) -> &ScalarPlan {
71        self.logical.scalar_semantics()
72    }
73
74    /// Borrow scalar missing-row consistency without exposing the full scalar
75    /// plan to executor owners that only need row-presence policy.
76    #[must_use]
77    pub(in crate::db) fn scalar_consistency(&self) -> MissingRowPolicy {
78        if self.access.has_selected_index_access_path() {
79            // An accepted secondary index is a persisted claim that every
80            // emitted key identifies an authoritative row. Ignoring a missing
81            // row would turn accepted-index corruption into an incomplete
82            // successful result.
83            MissingRowPolicy::Error
84        } else {
85            self.scalar_plan().consistency
86        }
87    }
88
89    /// Borrow grouped semantic fields when this plan is grouped.
90    #[must_use]
91    pub(in crate::db) const fn grouped_plan(&self) -> Option<&GroupPlan> {
92        match &self.logical {
93            LogicalPlan::Scalar(_) => None,
94            LogicalPlan::Grouped(plan) => Some(plan),
95        }
96    }
97
98    /// Lower this plan through accepted schema projection authority.
99    #[must_use]
100    pub(in crate::db) fn projection_spec_with_schema(&self, schema: &SchemaInfo) -> ProjectionSpec {
101        if let Some(static_contract) = &self.static_execution_planning_contract {
102            return static_contract.projection_spec.clone();
103        }
104
105        lower_projection_intent_with_schema(schema, &self.logical, &self.projection_selection)
106    }
107
108    /// Lower this plan into one projection semantic shape for identity hashing.
109    #[must_use]
110    pub(in crate::db::query) fn projection_spec_for_identity(&self) -> ProjectionSpec {
111        lower_projection_identity(&self.logical, &self.projection_selection)
112    }
113
114    /// Return the executor-facing predicate after removing only filtered-index
115    /// guard clauses the chosen access path already proves.
116    ///
117    /// This conservative form is used by preparation/explain surfaces that
118    /// still need to see access-bound equalities as index-predicate input.
119    #[must_use]
120    pub(in crate::db) fn execution_preparation_predicate(&self) -> Option<Predicate> {
121        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
122            return static_contract.execution_preparation_predicate.clone();
123        }
124
125        derive_execution_preparation_predicate(self)
126    }
127
128    /// Return the executor-facing residual predicate after removing any
129    /// filtered-index guard clauses and fixed access-bound equalities already
130    /// guaranteed by the chosen path.
131    #[must_use]
132    pub(in crate::db) fn effective_execution_predicate(&self) -> Option<Predicate> {
133        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
134            return static_contract
135                .residual_filter_contract
136                .residual_filter_predicate()
137                .cloned();
138        }
139
140        derive_residual_filter_predicate(self)
141    }
142
143    /// Return whether one explicit residual predicate survives access
144    /// planning and still participates in residual execution.
145    #[must_use]
146    pub(in crate::db) fn has_residual_filter_predicate(&self) -> bool {
147        self.effective_execution_predicate().is_some()
148    }
149
150    /// Borrow the planner-owned residual scalar filter expression when one
151    /// surviving semantic remainder still requires runtime evaluation.
152    #[must_use]
153    pub(in crate::db) fn residual_filter_expr(&self) -> Option<&Expr> {
154        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
155            return static_contract
156                .residual_filter_contract
157                .residual_filter_expr();
158        }
159
160        if !derive_has_residual_filter(self) {
161            return None;
162        }
163
164        self.scalar_plan().filter_expr.as_ref()
165    }
166
167    /// Return whether one explicit residual scalar filter expression survives
168    /// access planning and still requires runtime evaluation.
169    #[must_use]
170    pub(in crate::db) fn has_residual_filter_expr(&self) -> bool {
171        self.residual_filter_expr().is_some()
172    }
173
174    /// Return the planner-owned residual-filter shape used by diagnostics.
175    #[must_use]
176    pub(in crate::db) fn residual_filter_shape(&self) -> ResidualFilterShape {
177        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
178            return static_contract.residual_filter_contract.shape();
179        }
180
181        // Normalize pre-finalization candidate shells to the same execution
182        // shape later frozen in the static contract. A filter expression fully
183        // represented by its predicate executes through that predicate, not a
184        // second expression program.
185        let expression_required = self.scalar_plan().filter_expr.is_some()
186            && !derive_semantic_filter_fully_satisfied_by_access_contract(self);
187        ResidualFilterShape::from_presence(
188            expression_required,
189            self.effective_execution_predicate().is_some(),
190        )
191    }
192
193    /// Return the planner-owned predicate pushdown label consumed by verbose
194    /// execution diagnostics.
195    #[must_use]
196    #[cfg(feature = "sql")]
197    pub(in crate::db) fn predicate_pushdown_label(&self) -> String {
198        self.predicate_pushdown_diagnostics().label()
199    }
200
201    /// Return planner-owned predicate-pushdown diagnostics.
202    #[must_use]
203    #[cfg(feature = "sql")]
204    pub(in crate::db) fn predicate_pushdown_diagnostics(&self) -> PredicatePushdownDiagnostics {
205        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
206            return static_contract.predicate_pushdown_diagnostics;
207        }
208
209        derive_predicate_pushdown_diagnostics(self, self.residual_filter_shape())
210    }
211
212    /// Return the planner-owned predicate-pushdown outcome label.
213    #[must_use]
214    #[cfg(feature = "sql")]
215    pub(in crate::db) fn predicate_pushdown_outcome_label(&self) -> &'static str {
216        self.predicate_pushdown_diagnostics().outcome_label()
217    }
218
219    /// Return the planner-owned predicate-pushdown reason label.
220    #[must_use]
221    #[cfg(feature = "sql")]
222    pub(in crate::db) fn predicate_pushdown_reason_label(&self) -> &'static str {
223        self.predicate_pushdown_diagnostics().reason_label()
224    }
225
226    /// Borrow the planner-compiled execution-preparation predicate program.
227    #[must_use]
228    pub(in crate::db) fn execution_preparation_compiled_predicate(
229        &self,
230    ) -> Option<&PredicateProgram> {
231        self.static_execution_planning_contract()?
232            .execution_preparation_compiled_predicate
233            .as_ref()
234    }
235
236    /// Borrow the planner-compiled effective runtime predicate program.
237    #[must_use]
238    pub(in crate::db) fn effective_runtime_compiled_predicate(&self) -> Option<&PredicateProgram> {
239        match self
240            .static_execution_planning_contract()?
241            .residual_filter_contract
242            .effective_runtime_filter_program()
243        {
244            Some(program) => program.predicate_program(),
245            None => None,
246        }
247    }
248
249    /// Borrow the planner-frozen effective runtime scalar filter program.
250    #[must_use]
251    pub(in crate::db) fn effective_runtime_filter_program(
252        &self,
253    ) -> Option<&EffectiveRuntimeFilterProgram> {
254        self.static_execution_planning_contract()?
255            .residual_filter_contract
256            .effective_runtime_filter_program()
257    }
258
259    /// Lower scalar DISTINCT semantics into one executor-facing execution strategy.
260    #[must_use]
261    pub(in crate::db) fn distinct_execution_strategy(&self) -> DistinctExecutionStrategy {
262        if !self.scalar_plan().distinct {
263            return DistinctExecutionStrategy::None;
264        }
265
266        // DISTINCT on duplicate-safe single-path access shapes is a planner
267        // no-op for runtime dedup mechanics. Composite shapes can surface
268        // duplicate keys and therefore retain explicit dedup execution.
269        match distinct_runtime_dedup_strategy(&self.access) {
270            Some(strategy) => strategy,
271            None => DistinctExecutionStrategy::None,
272        }
273    }
274
275    /// Freeze one planner-owned route profile from accepted schema authority.
276    pub(in crate::db) fn finalize_planner_route_profile_for_model_with_schema(
277        &mut self,
278        schema_info: &SchemaInfo,
279    ) {
280        self.set_planner_route_profile(project_planner_route_profile_for_schema(schema_info, self));
281    }
282
283    /// Freeze planner-owned executor metadata with explicit schema authority.
284    pub(in crate::db) fn finalize_static_execution_planning_contract_with_schema(
285        &mut self,
286        schema_info: &SchemaInfo,
287    ) -> Result<(), InternalError> {
288        self.bind_group_field_slots_to_schema(schema_info)?;
289        self.static_execution_planning_contract = Some(
290            project_static_execution_planning_contract_with_schema(schema_info, self)?,
291        );
292
293        Ok(())
294    }
295
296    // Resolve authoring-time group field names onto accepted slots before the
297    // plan becomes executable.
298    fn bind_group_field_slots_to_schema(
299        &mut self,
300        schema_info: &SchemaInfo,
301    ) -> Result<(), InternalError> {
302        let LogicalPlan::Grouped(grouped) = &mut self.logical else {
303            return Ok(());
304        };
305
306        let accepted_fields = grouped
307            .group
308            .group_fields
309            .resolve_with_schema(schema_info)
310            .ok_or_else(InternalError::planner_executor_invariant)?;
311        grouped.group.group_fields = accepted_fields;
312
313        Ok(())
314    }
315
316    /// Build one immutable execution-shape signature contract for runtime layers.
317    #[must_use]
318    pub(in crate::db) fn execution_shape_signature(
319        &self,
320        entity_path: &str,
321    ) -> ExecutionShapeSignature {
322        ExecutionShapeSignature::new(self.continuation_signature(entity_path))
323    }
324
325    /// Return whether the chosen access contract fully satisfies the current
326    /// scalar query predicate without any additional runtime residual filtering.
327    #[must_use]
328    pub(in crate::db) fn predicate_fully_satisfied_by_access_contract(&self) -> bool {
329        if let Some(static_contract) = self.static_execution_planning_contract.as_ref() {
330            return self.scalar_plan().predicate.is_some()
331                && !static_contract
332                    .residual_filter_contract
333                    .has_residual_filter();
334        }
335
336        derive_predicate_fully_satisfied_by_access_contract(self)
337    }
338
339    /// Borrow the planner-frozen compiled scalar projection program.
340    #[must_use]
341    pub(in crate::db) fn scalar_projection_plan(&self) -> Option<&[CompiledExpr]> {
342        self.static_execution_planning_contract()?
343            .scalar_projection_plan
344            .as_deref()
345    }
346
347    /// Return whether planner-owned static execution metadata has already been frozen.
348    #[must_use]
349    pub(in crate::db) const fn has_static_execution_planning_contract(&self) -> bool {
350        self.static_execution_planning_contract.is_some()
351    }
352
353    /// Borrow the planner-frozen ordered primary-key field names.
354    pub(in crate::db) fn primary_key_names(&self) -> Result<Vec<&str>, InternalError> {
355        Ok(self
356            .require_static_execution_planning_contract()?
357            .primary_key_names
358            .iter()
359            .map(String::as_str)
360            .collect())
361    }
362
363    /// Borrow the planner-frozen projection slot reachability set.
364    pub(in crate::db) fn projection_referenced_slots(&self) -> Result<&[usize], InternalError> {
365        Ok(self
366            .require_static_execution_planning_contract()?
367            .projection_referenced_slots
368            .as_slice())
369    }
370
371    /// Return whether projection remains the full model-identity field list.
372    pub(in crate::db) fn projection_is_model_identity(&self) -> Result<bool, InternalError> {
373        Ok(self
374            .require_static_execution_planning_contract()?
375            .projection_is_model_identity)
376    }
377
378    /// Borrow the planner-frozen ORDER BY slot reachability set, if any.
379    #[must_use]
380    pub(in crate::db) fn order_referenced_slots(&self) -> Option<&[usize]> {
381        self.static_execution_planning_contract()?
382            .order_referenced_slots
383            .as_deref()
384    }
385
386    /// Borrow the planner-frozen resolved ORDER BY program, if one exists.
387    #[must_use]
388    pub(in crate::db) fn resolved_order(&self) -> Option<&ResolvedOrder> {
389        self.static_execution_planning_contract()?
390            .resolved_order
391            .as_ref()
392    }
393
394    /// Borrow the planner-frozen access slot map used by index predicate compilation.
395    #[must_use]
396    pub(in crate::db) fn slot_map(&self) -> Option<&[usize]> {
397        self.static_execution_planning_contract()?
398            .slot_map
399            .as_deref()
400    }
401
402    /// Borrow grouped aggregate execution specs already resolved during static planning.
403    #[must_use]
404    pub(in crate::db) fn grouped_aggregate_execution_specs(
405        &self,
406    ) -> Option<&[GroupedAggregateExecutionSpec]> {
407        self.static_execution_planning_contract()?
408            .grouped_aggregate_execution_specs
409            .as_deref()
410    }
411
412    /// Borrow the planner-resolved grouped DISTINCT execution strategy when present.
413    #[must_use]
414    pub(in crate::db) fn grouped_distinct_execution_strategy(
415        &self,
416    ) -> Option<&GroupedDistinctExecutionStrategy> {
417        self.static_execution_planning_contract()?
418            .grouped_distinct_execution_strategy
419            .as_ref()
420    }
421
422    /// Borrow the frozen projection semantic shape without reopening model ownership.
423    pub(in crate::db) fn frozen_projection_spec(&self) -> Result<&ProjectionSpec, InternalError> {
424        Ok(&self
425            .require_static_execution_planning_contract()?
426            .projection_spec)
427    }
428
429    /// Borrow the frozen direct projection slots without reopening model ownership.
430    #[must_use]
431    pub(in crate::db) fn frozen_direct_projection_slots(&self) -> Option<&[usize]> {
432        self.static_execution_planning_contract()?
433            .projection_direct_slots
434            .as_deref()
435    }
436
437    /// Borrow duplicate-preserving direct projection slots for raw data-row readers.
438    #[must_use]
439    pub(in crate::db) fn frozen_data_row_direct_projection_slots(&self) -> Option<&[usize]> {
440        self.static_execution_planning_contract()?
441            .projection_data_row_direct_slots
442            .as_deref()
443    }
444
445    /// Borrow the planner-frozen key-item-aware compile targets for the chosen access path.
446    #[must_use]
447    pub(in crate::db) fn index_compile_targets(&self) -> Option<&[IndexCompileTarget]> {
448        self.static_execution_planning_contract()?
449            .index_compile_targets
450            .as_deref()
451    }
452
453    const fn static_execution_planning_contract(&self) -> Option<&StaticExecutionPlanningContract> {
454        self.static_execution_planning_contract.as_ref()
455    }
456
457    fn require_static_execution_planning_contract(
458        &self,
459    ) -> Result<&StaticExecutionPlanningContract, InternalError> {
460        self.static_execution_planning_contract
461            .as_ref()
462            .ok_or_else(InternalError::query_executor_invariant)
463    }
464}
465
466fn distinct_runtime_dedup_strategy<K>(access: &AccessPlan<K>) -> Option<DistinctExecutionStrategy> {
467    match access {
468        AccessPlan::Union(_) | AccessPlan::Intersection(_) => {
469            Some(DistinctExecutionStrategy::PreOrdered)
470        }
471        AccessPlan::Path(path) if path.as_ref().is_index_multi_lookup() => {
472            Some(DistinctExecutionStrategy::HashMaterialize)
473        }
474        AccessPlan::Path(_) => None,
475    }
476}
477
478fn derive_continuation_policy_validated(plan: &AccessPlannedQuery) -> ContinuationPolicy {
479    let is_grouped_safe = plan
480        .grouped_plan()
481        .is_none_or(|grouped| grouped_cursor_policy_violation(grouped, true).is_none());
482
483    ContinuationPolicy::new(
484        true, // Continuation resume windows require anchor semantics for pushdown-safe replay.
485        true, // Continuation resumes must advance strictly to prevent replay/regression loops.
486        is_grouped_safe,
487    )
488}
489
490/// Project one planner-owned route profile from accepted schema authority.
491#[must_use]
492pub(in crate::db) fn project_planner_route_profile_for_schema(
493    schema_info: &SchemaInfo,
494    plan: &AccessPlannedQuery,
495) -> PlannerRouteProfile {
496    let primary_key_names = primary_key_names_from_schema(schema_info);
497    let secondary_order_contract = plan.scalar_plan().order.as_ref().and_then(|order| {
498        order.deterministic_secondary_order_contract_fields(primary_key_names.as_slice())
499    });
500
501    PlannerRouteProfile::new(
502        derive_continuation_policy_validated(plan),
503        derive_logical_pushdown_eligibility(plan, secondary_order_contract.as_ref()),
504        secondary_order_contract,
505    )
506}
507
508fn project_static_execution_planning_contract_with_schema(
509    schema_info: &SchemaInfo,
510    plan: &AccessPlannedQuery,
511) -> Result<StaticExecutionPlanningContract, InternalError> {
512    let projection_spec =
513        lower_projection_intent_with_schema(schema_info, &plan.logical, &plan.projection_selection);
514    let execution_preparation_predicate = plan.execution_preparation_predicate();
515    let residual_filter_predicate = derive_residual_filter_predicate_from_preparation(
516        plan,
517        execution_preparation_predicate.as_ref(),
518    );
519    let residual_filter_expr = derive_residual_filter_expr(plan);
520    let effective_runtime_filter_program = compile_effective_runtime_filter_program(
521        schema_info,
522        residual_filter_expr.as_ref(),
523        residual_filter_predicate.as_ref(),
524    )?;
525    let residual_filter_contract = ResidualFilterContract::new(
526        residual_filter_expr,
527        residual_filter_predicate,
528        effective_runtime_filter_program,
529    );
530    let residual_filter_shape = residual_filter_contract.shape();
531    let execution_preparation_compiled_predicate =
532        (should_compile_execution_preparation_predicate(residual_filter_shape)
533            && !planner_predicate_requires_expression_runtime(plan))
534        .then(|| compile_optional_predicate(schema_info, execution_preparation_predicate.as_ref()))
535        .flatten();
536    let predicate_pushdown_diagnostics =
537        derive_predicate_pushdown_diagnostics(plan, residual_filter_shape);
538    let scalar_projection_plan = if plan.grouped_plan().is_none() {
539        Some(
540            compile_scalar_projection_plan_with_schema(schema_info, &projection_spec)
541                .ok_or_else(InternalError::query_executor_invariant)?
542                .iter()
543                .map(CompiledExpr::compile)
544                .collect(),
545        )
546    } else {
547        None
548    };
549    let (grouped_aggregate_execution_specs, grouped_distinct_execution_strategy) =
550        resolve_grouped_static_planning_semantics(schema_info, plan, &projection_spec)?;
551    let projection_direct_slots = lower_direct_projection_slots_with_schema(
552        schema_info,
553        &plan.logical,
554        &plan.projection_selection,
555    );
556    let projection_data_row_direct_slots = lower_data_row_direct_projection_slots_with_schema(
557        schema_info,
558        &plan.logical,
559        &plan.projection_selection,
560    );
561    let projection_referenced_slots = projection_spec.referenced_slots_for_schema(schema_info)?;
562    let projection_is_model_identity = projection_spec.is_schema_identity_for(schema_info);
563    let resolved_order = resolved_order_for_plan(schema_info, plan)?;
564    let order_referenced_slots = order_referenced_slots_for_resolved_order(resolved_order.as_ref());
565    let slot_map = slot_map_for_schema_plan(schema_info, plan);
566    let index_compile_targets = index_compile_targets_for_schema_plan(schema_info, plan);
567
568    Ok(StaticExecutionPlanningContract {
569        primary_key_names: schema_info.primary_key_names().to_vec(),
570        projection_spec,
571        execution_preparation_predicate,
572        execution_preparation_compiled_predicate,
573        residual_filter_contract,
574        predicate_pushdown_diagnostics,
575        scalar_projection_plan,
576        grouped_aggregate_execution_specs,
577        grouped_distinct_execution_strategy,
578        projection_direct_slots,
579        projection_data_row_direct_slots,
580        projection_referenced_slots,
581        projection_is_model_identity,
582        resolved_order,
583        order_referenced_slots,
584        slot_map,
585        index_compile_targets,
586    })
587}
588
589fn primary_key_names_from_schema(schema_info: &SchemaInfo) -> Vec<&str> {
590    schema_info
591        .primary_key_names()
592        .iter()
593        .map(String::as_str)
594        .collect()
595}
596
597// Compile the executor-owned residual scalar filter contract once from the
598// planner-derived residual artifacts so runtime never has to rediscover
599// residual presence or shape from semantic/filter/pushdown state.
600fn compile_effective_runtime_filter_program(
601    schema_info: &SchemaInfo,
602    residual_filter_expr: Option<&Expr>,
603    residual_filter_predicate: Option<&Predicate>,
604) -> Result<Option<EffectiveRuntimeFilterProgram>, InternalError> {
605    // Keep the existing predicate fast path when the residual semantics still
606    // fit the derived predicate contract. The expression-owned lane is only
607    // needed once pushdown loses semantic coverage and a residual predicate no
608    // longer exists.
609    if let Some(predicate) = residual_filter_predicate {
610        return Ok(Some(EffectiveRuntimeFilterProgram::predicate(
611            PredicateProgram::compile_with_schema_info(schema_info, predicate),
612        )));
613    }
614
615    if let Some(filter_expr) = residual_filter_expr {
616        let compiled = compile_scalar_projection_expr_with_schema(schema_info, filter_expr)
617            .ok_or_else(InternalError::query_invalid_logical_plan)?;
618
619        return Ok(Some(EffectiveRuntimeFilterProgram::expression(
620            CompiledExpr::compile(&compiled),
621        )));
622    }
623
624    Ok(None)
625}
626
627// Derive the executor-preparation predicate once from the selected access path.
628// This strips only filtered-index guard clauses while preserving access-bound
629// equalities that still matter to preparation/explain consumers.
630fn derive_execution_preparation_predicate(plan: &AccessPlannedQuery) -> Option<Predicate> {
631    let query_predicate = plan.scalar_plan().predicate.as_ref()?;
632
633    match plan.access.selected_index_contract() {
634        Some(index) => {
635            residual_query_predicate_after_filtered_access_contract(index, query_predicate)
636        }
637        None => Some(query_predicate.clone()),
638    }
639}
640
641// Derive the final residual predicate once from the already-filtered
642// preparation predicate plus any equality bounds guaranteed by the concrete
643// access path.
644fn derive_residual_filter_predicate(plan: &AccessPlannedQuery) -> Option<Predicate> {
645    let filtered_residual = derive_execution_preparation_predicate(plan);
646
647    derive_residual_filter_predicate_from_preparation(plan, filtered_residual.as_ref())
648}
649
650fn derive_residual_filter_predicate_from_preparation(
651    plan: &AccessPlannedQuery,
652    execution_preparation_predicate: Option<&Predicate>,
653) -> Option<Predicate> {
654    let execution_preparation_predicate = execution_preparation_predicate?;
655
656    let residual = residual_query_predicate_after_access_path_bounds(
657        plan.access.as_path(),
658        execution_preparation_predicate,
659    );
660    if residual.is_some() && planner_predicate_requires_expression_runtime(plan) {
661        return None;
662    }
663
664    residual
665}
666
667// Derive the explicit residual semantic expression once for finalized plans.
668// The residual expression remains the planner-owned semantic filter when any
669// runtime filtering still survives access satisfaction.
670fn derive_residual_filter_expr(plan: &AccessPlannedQuery) -> Option<Expr> {
671    let filter_expr = plan.scalar_plan().filter_expr.as_ref()?;
672    if derive_semantic_filter_fully_satisfied_by_access_contract(plan)
673        && (!planner_predicate_requires_expression_runtime(plan)
674            || planner_predicate_is_fully_satisfied_by_access_contract(plan))
675    {
676        return None;
677    }
678
679    Some(filter_expr.clone())
680}
681
682// Nested-path predicate shells are planner facts only: the predicate runtime
683// addresses top-level row slots. Keep the already-compiled expression as the
684// execution authority unless the selected access path proves the predicate in
685// full and no runtime filter remains.
686fn planner_predicate_requires_expression_runtime(plan: &AccessPlannedQuery) -> bool {
687    plan.scalar_plan().predicate_covers_filter_expr
688        && plan
689            .scalar_plan()
690            .filter_expr
691            .as_ref()
692            .is_some_and(Expr::contains_field_path)
693}
694
695fn planner_predicate_is_fully_satisfied_by_access_contract(plan: &AccessPlannedQuery) -> bool {
696    let Some(predicate) = derive_execution_preparation_predicate(plan) else {
697        return false;
698    };
699
700    residual_query_predicate_after_access_path_bounds(plan.access.as_path(), &predicate).is_none()
701}
702
703// Return whether any residual filtering survives after access planning. This
704// helper exists only for pre-finalization assembly; finalized plans must read
705// the explicit residual artifacts frozen in `StaticExecutionPlanningContract`.
706fn derive_has_residual_filter(plan: &AccessPlannedQuery) -> bool {
707    match (
708        plan.scalar_plan().filter_expr.as_ref(),
709        plan.scalar_plan().predicate.as_ref(),
710    ) {
711        (None, None) => false,
712        (Some(_), None) => true,
713        (Some(_) | None, Some(_)) => !plan.predicate_fully_satisfied_by_access_contract(),
714    }
715}
716
717// Freeze predicate-pushdown diagnostics from one logical plan shape. This keeps
718// lazy plan accessors and finalized static planning on the same argument
719// contract while leaving route selection and residual filtering unchanged.
720fn derive_predicate_pushdown_diagnostics(
721    plan: &AccessPlannedQuery,
722    residual_filter_shape: ResidualFilterShape,
723) -> PredicatePushdownDiagnostics {
724    PredicatePushdownDiagnostics::from_plan(
725        plan.scalar_plan().filter_expr.is_some(),
726        plan.scalar_plan().predicate_covers_filter_expr,
727        plan.scalar_plan().predicate.as_ref(),
728        &plan.access,
729        residual_filter_shape,
730    )
731}
732
733// Return true when the planner-owned predicate contract is fully satisfied by
734// access planning and no semantic residual filter expression survives.
735fn derive_predicate_fully_satisfied_by_access_contract(plan: &AccessPlannedQuery) -> bool {
736    plan.scalar_plan().predicate.is_some()
737        && derive_residual_filter_predicate(plan).is_none()
738        && derive_residual_filter_expr(plan).is_none()
739}
740
741// Return true when the semantic filter expression is entirely represented by
742// the planner-owned predicate contract and the chosen access path satisfies
743// that predicate without any runtime remainder.
744const fn derive_semantic_filter_fully_satisfied_by_access_contract(
745    plan: &AccessPlannedQuery,
746) -> bool {
747    plan.scalar_plan().filter_expr.is_some()
748        && plan.scalar_plan().predicate.is_some()
749        && plan.scalar_plan().predicate_covers_filter_expr
750}
751
752// Compile one optional planner-frozen predicate program while keeping the
753// static planning assembly path free of repeated `Option` mapping boilerplate.
754fn compile_optional_predicate(
755    schema_info: &SchemaInfo,
756    predicate: Option<&Predicate>,
757) -> Option<PredicateProgram> {
758    predicate.map(|predicate| PredicateProgram::compile_with_schema_info(schema_info, predicate))
759}
760
761// Avoid compiling large access-proven predicates into executor preparation.
762// When no residual filter survives, the chosen access route already enforces
763// the predicate and route/explain consumers can use the explicit residual
764// contract instead of recompiling access-bound literals.
765const fn should_compile_execution_preparation_predicate(
766    residual_filter_shape: ResidualFilterShape,
767) -> bool {
768    !residual_filter_shape.is_absent()
769}
770
771// Resolve the grouped-only static planning semantics bundle once so grouped
772// aggregate execution specs and grouped DISTINCT strategy stay derived under
773// one shared grouped-plan branch.
774fn resolve_grouped_static_planning_semantics(
775    schema_info: &SchemaInfo,
776    plan: &AccessPlannedQuery,
777    projection_spec: &ProjectionSpec,
778) -> Result<
779    (
780        Option<Vec<GroupedAggregateExecutionSpec>>,
781        Option<GroupedDistinctExecutionStrategy>,
782    ),
783    InternalError,
784> {
785    let Some(grouped) = plan.grouped_plan() else {
786        return Ok((None, None));
787    };
788
789    let mut aggregate_specs = grouped_aggregate_specs_from_projection_spec(
790        projection_spec,
791        &grouped.group.group_fields,
792        grouped.group.aggregates.as_slice(),
793    )?;
794    extend_grouped_having_aggregate_specs(&mut aggregate_specs, grouped)?;
795
796    let grouped_aggregate_execution_specs = Some(grouped_aggregate_execution_specs(
797        schema_info,
798        aggregate_specs.as_slice(),
799    )?);
800    let grouped_distinct_execution_strategy = Some(
801        resolved_grouped_distinct_execution_strategy_with_schema_info(
802            schema_info,
803            &grouped.group.group_fields,
804            grouped.group.aggregates.as_slice(),
805            grouped.having_expr.as_ref(),
806        )?,
807    );
808
809    Ok((
810        grouped_aggregate_execution_specs,
811        grouped_distinct_execution_strategy,
812    ))
813}
814
815fn extend_grouped_having_aggregate_specs(
816    aggregate_specs: &mut Vec<GroupedAggregateExecutionSpec>,
817    grouped: &GroupPlan,
818) -> Result<(), InternalError> {
819    if let Some(having_expr) = grouped.having_expr.as_ref() {
820        extend_unique_grouped_aggregate_specs_from_expr(aggregate_specs, having_expr)?;
821    }
822
823    Ok(())
824}
825
826fn resolved_order_for_plan(
827    schema_info: &SchemaInfo,
828    plan: &AccessPlannedQuery,
829) -> Result<Option<ResolvedOrder>, InternalError> {
830    if grouped_plan_strategy(plan).is_some_and(GroupedPlanStrategy::is_top_k_group) {
831        return Ok(None);
832    }
833
834    let Some(order) = plan.scalar_plan().order.as_ref() else {
835        return Ok(None);
836    };
837
838    let mut fields = Vec::with_capacity(order.fields.len());
839    for term in &order.fields {
840        fields.push(ResolvedOrderField::new(
841            resolved_order_value_source_for_term(schema_info, term)?,
842            term.direction(),
843        ));
844    }
845
846    Ok(Some(ResolvedOrder::new(fields)))
847}
848
849fn resolved_order_value_source_for_term(
850    schema_info: &SchemaInfo,
851    term: &crate::db::query::plan::OrderTerm,
852) -> Result<ResolvedOrderValueSource, InternalError> {
853    if term.direct_field().is_none() {
854        let rendered = term.rendered_label();
855        validate_resolved_order_expr_fields(schema_info, term.expr(), rendered.as_str())?;
856        let compiled = compile_scalar_projection_expr_with_schema(schema_info, term.expr())
857            .ok_or_else(|| order_expression_scalar_seam_error(rendered.as_str()))?;
858
859        return Ok(ResolvedOrderValueSource::expression(CompiledExpr::compile(
860            &compiled,
861        )));
862    }
863
864    let Some(field) = term.direct_field() else {
865        return Err(InternalError::query_invalid_logical_plan());
866    };
867    let slot = resolve_required_schema_slot(
868        schema_info,
869        field,
870        InternalError::query_invalid_logical_plan,
871    )?;
872
873    Ok(ResolvedOrderValueSource::direct_field(slot))
874}
875
876fn validate_resolved_order_expr_fields(
877    schema_info: &SchemaInfo,
878    expr: &Expr,
879    rendered: &str,
880) -> Result<(), InternalError> {
881    expr.try_for_each_tree_expr(&mut |node| match node {
882        Expr::Field(field_id) => resolve_required_schema_slot(
883            schema_info,
884            field_id.as_str(),
885            InternalError::query_invalid_logical_plan,
886        )
887        .map(|_| ()),
888        Expr::Aggregate(_) => Err(order_expression_scalar_seam_error(rendered)),
889        #[cfg(test)]
890        Expr::Alias { .. } => Err(order_expression_scalar_seam_error(rendered)),
891        Expr::Unary { .. } => Err(order_expression_scalar_seam_error(rendered)),
892        _ => Ok(()),
893    })
894}
895
896// Resolve one schema-authoritative field slot while keeping planner
897// invalid-logical-plan error construction at the callsite that owns the
898// diagnostic wording.
899fn resolve_required_schema_slot<F>(
900    schema_info: &SchemaInfo,
901    field: &str,
902    invalid_plan_error: F,
903) -> Result<usize, InternalError>
904where
905    F: FnOnce() -> InternalError,
906{
907    schema_info
908        .field_slot_index(field)
909        .ok_or_else(invalid_plan_error)
910}
911
912// Keep the scalar-order expression seam violation text under one helper so the
913// parse validation and compile validation paths do not drift.
914fn order_expression_scalar_seam_error(_rendered: &str) -> InternalError {
915    InternalError::query_invalid_logical_plan()
916}
917
918// Keep one stable executor-facing slot list for grouped order terms after the
919// planner has frozen the structural `ResolvedOrder`. The grouped Top-K route
920// now consumes this same referenced-slot contract instead of re-deriving order
921// sources from planner strategy at runtime.
922fn order_referenced_slots_for_resolved_order(
923    resolved_order: Option<&ResolvedOrder>,
924) -> Option<Vec<usize>> {
925    Some(resolved_order?.referenced_slots())
926}
927
928fn slot_map_for_schema_plan(
929    schema_info: &SchemaInfo,
930    plan: &AccessPlannedQuery,
931) -> Option<Vec<usize>> {
932    let executable = plan.access.executable_contract();
933
934    resolved_index_slots_for_access_path(schema_info, &executable)
935}
936
937fn resolved_index_slots_for_access_path(
938    schema_info: &SchemaInfo,
939    access: &ExecutableAccessPlan<'_, crate::value::Value>,
940) -> Option<Vec<usize>> {
941    let path = access.as_path()?;
942    let path_facts = path.shape_facts();
943    let key_items = path_facts.index_key_items_for_slot_map()?;
944    let mut slots = Vec::with_capacity(key_items.key_arity());
945    for key_item in key_items.key_items() {
946        let field = key_item.as_ref().field();
947        let root = field.split_once('.').map_or(field, |(root, _)| root);
948        let slot = schema_info.field_slot_index(root)?;
949        slots.push(slot);
950    }
951
952    Some(slots)
953}
954
955fn index_compile_targets_for_schema_plan(
956    schema_info: &SchemaInfo,
957    plan: &AccessPlannedQuery,
958) -> Option<Vec<IndexCompileTarget>> {
959    let executable = plan.access.executable_contract();
960    let path = executable.as_path()?;
961    let key_items = path.shape_facts().index_key_items_for_slot_map()?;
962    let mut targets = Vec::new();
963
964    for (component_index, key_item) in key_items.key_items().iter().enumerate() {
965        let key_item = key_item.as_ref();
966        let field = key_item.field();
967        let root = field.split_once('.').map_or(field, |(root, _)| root);
968        let field_slot = schema_info.field_slot_index(root)?;
969        targets.push(IndexCompileTarget {
970            component_index,
971            field_slot,
972            kind: match key_item {
973                SemanticIndexKeyItemRef::Field(_) => IndexCompileTargetKind::Field,
974                SemanticIndexKeyItemRef::AcceptedExpression(expression) => {
975                    IndexCompileTargetKind::Expression(expression.op())
976                }
977            },
978        });
979    }
980
981    Some(targets)
982}