1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Module: query::plan::semantics::logical
//! Responsibility: logical-plan semantic lowering from planner contracts to access-planned queries.
//! Does not own: access-path index selection internals or runtime execution behavior.
//! Boundary: derives planner-owned execution semantics, shape signatures, and continuation policy.
use crate::{
db::{
access::AccessPlan,
predicate::PredicateExecutionModel,
query::plan::{
AccessPlannedQuery, ContinuationPolicy, DistinctExecutionStrategy,
ExecutionShapeSignature, GroupPlan, LogicalPlan, PlannerRouteProfile, QueryMode,
ScalarPlan, derive_logical_pushdown_eligibility, expr::ProjectionSpec,
grouped_cursor_policy_violation, lower_projection_identity, lower_projection_intent,
residual_query_predicate_after_access_path_bounds,
residual_query_predicate_after_filtered_access,
},
},
model::entity::EntityModel,
};
impl QueryMode {
/// True if this mode represents a load intent.
#[must_use]
pub const fn is_load(&self) -> bool {
match self {
Self::Load(_) => true,
Self::Delete(_) => false,
}
}
/// True if this mode represents a delete intent.
#[must_use]
pub const fn is_delete(&self) -> bool {
match self {
Self::Delete(_) => true,
Self::Load(_) => false,
}
}
}
impl LogicalPlan {
/// Borrow scalar semantic fields shared by scalar/grouped logical variants.
#[must_use]
pub(in crate::db) const fn scalar_semantics(&self) -> &ScalarPlan {
match self {
Self::Scalar(plan) => plan,
Self::Grouped(plan) => &plan.scalar,
}
}
/// Borrow scalar semantic fields mutably across logical variants for tests.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar_semantics_mut(&mut self) -> &mut ScalarPlan {
match self {
Self::Scalar(plan) => plan,
Self::Grouped(plan) => &mut plan.scalar,
}
}
/// Test-only shorthand for explicit scalar semantic borrowing.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar(&self) -> &ScalarPlan {
self.scalar_semantics()
}
/// Test-only shorthand for explicit mutable scalar semantic borrowing.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar_mut(&mut self) -> &mut ScalarPlan {
self.scalar_semantics_mut()
}
}
impl AccessPlannedQuery {
/// Borrow scalar semantic fields shared by scalar/grouped logical variants.
#[must_use]
pub(in crate::db) const fn scalar_plan(&self) -> &ScalarPlan {
self.logical.scalar_semantics()
}
/// Borrow scalar semantic fields mutably across logical variants for tests.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar_plan_mut(&mut self) -> &mut ScalarPlan {
self.logical.scalar_semantics_mut()
}
/// Test-only shorthand for explicit scalar plan borrowing.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar(&self) -> &ScalarPlan {
self.scalar_plan()
}
/// Test-only shorthand for explicit mutable scalar plan borrowing.
#[must_use]
#[cfg(test)]
pub(in crate::db) const fn scalar_mut(&mut self) -> &mut ScalarPlan {
self.scalar_plan_mut()
}
/// Borrow grouped semantic fields when this plan is grouped.
#[must_use]
pub(in crate::db) const fn grouped_plan(&self) -> Option<&GroupPlan> {
match &self.logical {
LogicalPlan::Scalar(_) => None,
LogicalPlan::Grouped(plan) => Some(plan),
}
}
/// Lower this plan into one canonical planner-owned projection semantic spec.
#[must_use]
pub(in crate::db) fn projection_spec(&self, model: &EntityModel) -> ProjectionSpec {
lower_projection_intent(model, &self.logical, &self.projection_selection)
}
/// Lower this plan into one projection semantic shape for identity hashing.
#[must_use]
pub(in crate::db::query) fn projection_spec_for_identity(&self) -> ProjectionSpec {
lower_projection_identity(&self.logical)
}
/// Return the executor-facing predicate after removing only filtered-index
/// guard clauses the chosen access path already proves.
///
/// This conservative form is used by preparation/explain surfaces that
/// still need to see access-bound equalities as index-predicate input.
#[must_use]
pub(in crate::db) fn execution_preparation_predicate(&self) -> Option<PredicateExecutionModel> {
let query_predicate = self.scalar_plan().predicate.as_ref()?;
match self.access.selected_index_model() {
Some(index) => residual_query_predicate_after_filtered_access(index, query_predicate),
None => Some(query_predicate.clone()),
}
}
/// Return the executor-facing residual predicate after removing any
/// filtered-index guard clauses and fixed access-bound equalities already
/// guaranteed by the chosen path.
#[must_use]
pub(in crate::db) fn effective_execution_predicate(&self) -> Option<PredicateExecutionModel> {
// Phase 1: strip only filtered-index guard clauses the chosen access
// path already proves.
let filtered_residual = self.execution_preparation_predicate();
let filtered_residual = filtered_residual.as_ref()?;
// Phase 2: strip any additional equality clauses already guaranteed by
// the concrete access-path bounds, such as `tier = 'gold'` on one
// selected `IndexPrefix(tier='gold', ...)` route.
residual_query_predicate_after_access_path_bounds(self.access.as_path(), filtered_residual)
}
/// Lower scalar DISTINCT semantics into one executor-facing execution strategy.
#[must_use]
pub(in crate::db) fn distinct_execution_strategy(&self) -> DistinctExecutionStrategy {
if !self.scalar_plan().distinct {
return DistinctExecutionStrategy::None;
}
// DISTINCT on duplicate-safe single-path access shapes is a planner
// no-op for runtime dedup mechanics. Composite shapes can surface
// duplicate keys and therefore retain explicit dedup execution.
match distinct_runtime_dedup_strategy(&self.access) {
Some(strategy) => strategy,
None => DistinctExecutionStrategy::None,
}
}
/// Project one planner-owned route profile for executor route planning.
#[must_use]
pub(in crate::db) fn planner_route_profile(&self, model: &EntityModel) -> PlannerRouteProfile {
PlannerRouteProfile::new(
derive_continuation_policy_validated(self),
derive_logical_pushdown_eligibility(model, self),
)
}
/// Build one immutable execution-shape signature contract for runtime layers.
#[must_use]
pub(in crate::db) fn execution_shape_signature(
&self,
entity_path: &'static str,
) -> ExecutionShapeSignature {
ExecutionShapeSignature::new(self.continuation_signature(entity_path))
}
/// Return whether the chosen access contract fully satisfies the current
/// scalar query predicate without any additional post-access filtering.
#[must_use]
pub(in crate::db) fn predicate_fully_satisfied_by_access_contract(&self) -> bool {
self.scalar_plan().predicate.is_some() && self.effective_execution_predicate().is_none()
}
/// Return whether the scalar logical predicate still requires post-access
/// filtering after accounting for filtered-index guard predicates and
/// access-path equality bounds.
#[must_use]
pub(in crate::db) fn has_residual_predicate(&self) -> bool {
self.scalar_plan().predicate.is_some()
&& !self.predicate_fully_satisfied_by_access_contract()
}
}
fn distinct_runtime_dedup_strategy<K>(access: &AccessPlan<K>) -> Option<DistinctExecutionStrategy> {
match access {
AccessPlan::Union(_) | AccessPlan::Intersection(_) => {
Some(DistinctExecutionStrategy::PreOrdered)
}
AccessPlan::Path(path) if path.as_ref().is_index_multi_lookup() => {
Some(DistinctExecutionStrategy::HashMaterialize)
}
AccessPlan::Path(_) => None,
}
}
fn derive_continuation_policy_validated(plan: &AccessPlannedQuery) -> ContinuationPolicy {
let is_grouped_safe = plan
.grouped_plan()
.is_none_or(|grouped| grouped_cursor_policy_violation(grouped, true).is_none());
ContinuationPolicy::new(
true, // Continuation resume windows require anchor semantics for pushdown-safe replay.
true, // Continuation resumes must advance strictly to prevent replay/regression loops.
is_grouped_safe,
)
}