Skip to main content

icydb_core/db/session/query/
dynamic.rs

1//! Module: db::session::query::dynamic
2//! Responsibility: lower and execute public dynamic reads against accepted schema.
3//! Does not own: query planning, accepted schema construction, or row projection.
4//! Boundary: entity-name requests converge on the shared structural read lane.
5
6use crate::{
7    db::{
8        DbSession, DynamicQuery, DynamicTypedEntityBinding, ExhaustiveQueryPageOutput,
9        ExhaustiveReadError, GroupedQueryOutput, LiveQueryPageOutput, MissingRowPolicy, QueryError,
10        ReadSetRevisionError, ReadSetRevisionProof, ScalarPageWork,
11        codec::{finalize_hash_sha256, new_hash_sha256_prefixed},
12        commit::{cursor_authentication_key, database_incarnation_id},
13        cursor::{
14            CursorBoundary, CursorBoundarySlot, CursorPlanError, ScalarOrderTermContract,
15            ScalarPageMode, ScalarPageToken, ScalarPageTokenAuthority, ScalarPageTokenProgress,
16            ScalarPageTokenWindow, decode_optional_cursor_token, encode_cursor,
17        },
18        data::{DecodedDataStoreKey, RawDataStoreKey},
19        executor::{
20            PageWorkEnvelope, ScalarContinuationContext, StructuralProjectionRequest,
21            execute_structural_projection_page,
22        },
23        query::{
24            admission::{
25                QueryAdmissionPolicy, QueryAdmissionSummary, input::validate_dynamic_query_input,
26            },
27            expr::{CompareOperator, FilterExpr, SetOperator},
28            intent::{IntentError, StructuralQuery},
29            plan::{
30                CardinalityTiebreakRoutePin, GroupAggregateSpec, OrderDirection, OrderSpec,
31                OrderTerm,
32                expr::{FieldId, ProjectionSelection},
33            },
34            preparation::PreparationWork,
35        },
36        session::AcceptedSchemaCatalogContext,
37    },
38    traits::CanisterKind,
39};
40use icydb_diagnostic_code::{
41    DiagnosticDecodeReason, DiagnosticExecutionBudgetResource, DiagnosticExecutionLane,
42    QueryReadAdmissionCode,
43};
44use sha2::Digest;
45#[cfg(test)]
46use std::cell::Cell;
47
48#[cfg(not(test))]
49const SCALAR_PAGE_OUTPUT_ROWS: usize = 1_024;
50#[cfg(test)]
51const SCALAR_PAGE_OUTPUT_ROWS: usize = 2;
52#[cfg(test)]
53const SCALAR_PAGE_KEY_ENTRIES: u64 = 4;
54
55#[cfg(test)]
56std::thread_local! {
57    static SCALAR_PAGE_RESULT_BYTES_LIMIT_OVERRIDE: Cell<Option<u64>> = const { Cell::new(None) };
58}
59
60#[cfg(test)]
61struct ScalarPageResultBytesLimitGuard(Option<u64>);
62
63#[cfg(test)]
64impl Drop for ScalarPageResultBytesLimitGuard {
65    fn drop(&mut self) {
66        SCALAR_PAGE_RESULT_BYTES_LIMIT_OVERRIDE.with(|limit| limit.set(self.0));
67    }
68}
69
70#[derive(Clone, Copy)]
71enum DynamicReadLane {
72    Public,
73    Trusted,
74}
75
76impl DynamicReadLane {
77    const fn execution_lane(self) -> DiagnosticExecutionLane {
78        match self {
79            Self::Public => DiagnosticExecutionLane::PublicRead,
80            Self::Trusted => DiagnosticExecutionLane::TrustedRead,
81        }
82    }
83}
84
85struct ScalarCursorContract {
86    signature: crate::db::cursor::ContinuationSignature,
87    authority: ScalarPageTokenAuthority,
88    route_pin: Option<CardinalityTiebreakRoutePin>,
89    window: ScalarPageTokenWindow,
90    order_terms: Vec<ScalarOrderTermContract>,
91}
92
93impl<C: CanisterKind> DbSession<C> {
94    fn may_select_exact_single_primary_key(
95        request: &DynamicQuery,
96        catalog: &AcceptedSchemaCatalogContext,
97    ) -> bool {
98        let [primary_key] = catalog.accepted_schema_info().primary_key_names() else {
99            return false;
100        };
101        // This is only a cheap candidate check, not normalization. Multi-value
102        // lists (including duplicates) use the ordinary paged preparation once.
103        let field = match request.filter_expr() {
104            Some(FilterExpr::Compare {
105                operator: CompareOperator::Eq,
106                field,
107                ..
108            }) => field,
109            Some(FilterExpr::Set {
110                operator: SetOperator::In,
111                field,
112                values,
113            }) if values.len() == 1 => field,
114            _ => return false,
115        };
116        field.eq_ignore_ascii_case(primary_key)
117    }
118
119    fn exact_primary_key_candidate_bound(
120        prepared_plan: &crate::db::executor::SharedPreparedExecutionPlan,
121    ) -> Option<usize> {
122        let access = &prepared_plan.logical_plan().access;
123        if access.as_by_key_path().is_some() {
124            return Some(1);
125        }
126
127        access.as_by_keys_path().map(<[crate::value::Value]>::len)
128    }
129
130    pub(super) fn structural_query_from_dynamic_request(
131        &self,
132        request: &DynamicQuery,
133        catalog: &AcceptedSchemaCatalogContext,
134        lane: DiagnosticExecutionLane,
135    ) -> Result<StructuralQuery, QueryError> {
136        self.structural_query_from_dynamic_request_with_page_limit(
137            request, catalog, None, false, lane,
138        )
139    }
140
141    fn structural_query_from_dynamic_request_with_page_limit(
142        &self,
143        request: &DynamicQuery,
144        catalog: &AcceptedSchemaCatalogContext,
145        page_limit: Option<u32>,
146        require_total_order: bool,
147        lane: DiagnosticExecutionLane,
148    ) -> Result<StructuralQuery, QueryError> {
149        validate_dynamic_query_input(request)?;
150        Self::validate_dynamic_query_shape(request)?;
151        PreparationWork::run(self.db.request_execution_scope(), lane, |work| {
152            let schema = catalog.accepted_schema_info();
153            let mut query = StructuralQuery::new(MissingRowPolicy::Ignore);
154            if let Some(filter) = request.filter_expr() {
155                query = query.filter_for_schema(schema, filter, work)?;
156            }
157            // Materialize each known-length clause once, then move it into
158            // intent. An absent order remains absent unless paging needs one.
159            let order_fields = if !request.order_terms().is_empty() {
160                work.copy_slice(request.order_terms(), |order| {
161                    Ok(order.copy_for_preparation(work)?.lower())
162                })?
163            } else if require_total_order {
164                work.copy_slice(schema.primary_key_names(), |field| {
165                    Ok(OrderTerm::field(
166                        work.copy_text(field)?,
167                        OrderDirection::Asc,
168                    ))
169                })?
170            } else {
171                Vec::new()
172            };
173            if !order_fields.is_empty() {
174                query = query.order_spec(OrderSpec {
175                    fields: order_fields,
176                });
177            }
178            if !request.selected_fields().is_empty() {
179                query = query.projection_selection(ProjectionSelection::Fields(
180                    work.copy_slice(request.selected_fields(), |field| {
181                        Ok(FieldId::new(work.copy_text(field)?))
182                    })?,
183                ));
184            }
185            #[cfg(test)]
186            if request.projection_is_distinct() {
187                query = query.distinct();
188            }
189            if let Some(limit) = page_limit.or_else(|| request.row_limit()) {
190                query = query.limit(limit);
191            }
192            if !request.group_fields().is_empty() {
193                query = query.group_fields_with_schema(request.group_fields(), schema, work)?;
194            }
195            if !request.aggregates().is_empty() {
196                query =
197                    query.group_aggregates(work.copy_slice(request.aggregates(), |aggregate| {
198                        Ok(GroupAggregateSpec::from_aggregate_expr(
199                            aggregate
200                                .copy_for_preparation(work)
201                                .map_err(QueryError::execute)?,
202                        ))
203                    })?);
204            }
205            if let Some((max_groups, max_group_bytes)) = request.grouped_execution_limits() {
206                if max_groups == 0 || max_group_bytes == 0 {
207                    return Err(QueryReadAdmissionCode::GroupedQueryRequiresLimits.into());
208                }
209                query = query.grouped_limits(u64::from(max_groups), u64::from(max_group_bytes));
210            }
211
212            Ok(query)
213        })
214    }
215
216    // Dynamic execution and typed diagnostics accept the same grouped shape.
217    // Execution-only scan/sort policy remains at the execution terminal.
218    fn validate_dynamic_query_shape(request: &DynamicQuery) -> Result<(), QueryError> {
219        if request.has_grouping() {
220            if request.grouped_execution_limits().is_none() {
221                return Err(QueryReadAdmissionCode::GroupedQueryRequiresLimits.into());
222            }
223            if !request.selected_fields().is_empty() {
224                return Err(QueryError::intent(
225                    IntentError::grouped_output_defined_by_group_and_aggregates(),
226                ));
227            }
228        } else if request.grouped_execution_limits().is_some() {
229            return Err(QueryError::intent(
230                IntentError::scalar_terminal_requires_scalar_query(),
231            ));
232        }
233        Ok(())
234    }
235
236    /// Project a logical query under the binding's current accepted authority.
237    /// `None` means the binding is stale. No row execution or cursor decoding occurs.
238    #[doc(hidden)]
239    pub fn explain_query_for_typed_binding(
240        &self,
241        binding: &DynamicTypedEntityBinding,
242        request: &DynamicQuery,
243    ) -> Result<Option<crate::db::ExplainPlan>, QueryError> {
244        let Some(catalog) = self
245            .current_typed_entity_binding_catalog(binding)
246            .map_err(QueryError::execute)?
247        else {
248            return Ok(None);
249        };
250        if request.continuation_cursor().is_some() {
251            return Err(QueryReadAdmissionCode::ExplainDoesNotAcceptCursor.into());
252        }
253        let lane = DiagnosticExecutionLane::Diagnostic;
254        let query = self.structural_query_from_dynamic_request(request, &catalog, lane)?;
255        let plan = self.cached_shared_query_plan_for_accepted_authority_with_catalog(
256            catalog.accepted_entity_authority(),
257            &catalog,
258            &query,
259            lane,
260        )?;
261        PreparationWork::run(self.db.request_execution_scope(), lane, |work| {
262            plan.explain(work).map(Some)
263        })
264    }
265
266    fn scalar_page_cursor_error() -> QueryError {
267        QueryError::from_cursor_plan_error(CursorPlanError::invalid_continuation_cursor_payload(
268            DiagnosticDecodeReason::CursorTokenDecode,
269        ))
270    }
271
272    fn scalar_cursor_contract(
273        request: &DynamicQuery,
274        catalog: &AcceptedSchemaCatalogContext,
275        envelope: PageWorkEnvelope,
276        prepared_plan: &crate::db::executor::SharedPreparedExecutionPlan,
277        mode: ScalarPageMode,
278        proof: Option<&ReadSetRevisionProof>,
279    ) -> Result<ScalarCursorContract, QueryError> {
280        let mut signature = prepared_plan
281            .continuation_signature_for_runtime()
282            .map_err(QueryError::execute)?;
283        match (mode, proof) {
284            (ScalarPageMode::Live, None) => {}
285            (ScalarPageMode::Exhaustive, Some(proof)) => {
286                let mut hasher = new_hash_sha256_prefixed(b"icydb.exhaustive-cursor-proof.v1");
287                hasher.update(signature.into_bytes());
288                hasher.update(proof.signature_bytes());
289                signature = crate::db::cursor::ContinuationSignature::from_bytes(
290                    finalize_hash_sha256(hasher),
291                );
292            }
293            _ => return Err(Self::scalar_page_cursor_error()),
294        }
295        let root_identity = catalog.runtime_root_identity();
296        let (root_fingerprint_method, root_fingerprint) = root_identity.fingerprint();
297        let authority = ScalarPageTokenAuthority::new(
298            database_incarnation_id()
299                .map_err(QueryError::execute)?
300                .to_bytes(),
301            root_identity.accepted_root_revision().get(),
302            root_fingerprint_method,
303            root_fingerprint,
304            catalog.fingerprint(),
305            prepared_plan.authority_ref().entity_tag(),
306        );
307        let window =
308            ScalarPageTokenWindow::new(0, request.row_limit(), envelope.profile_identity());
309        let canonical_order = prepared_plan
310            .logical_plan()
311            .scalar_plan()
312            .order
313            .as_ref()
314            .ok_or_else(Self::scalar_page_cursor_error)?;
315        let order_terms = canonical_order
316            .fields
317            .iter()
318            .map(|term| ScalarOrderTermContract::new(term.rendered_label(), term.direction()))
319            .collect::<Vec<_>>();
320
321        Ok(ScalarCursorContract {
322            signature,
323            authority,
324            route_pin: prepared_plan
325                .logical_plan()
326                .cardinality_tiebreak_route_pin(),
327            window,
328            order_terms,
329        })
330    }
331
332    fn validate_scalar_page_token(
333        token: &ScalarPageToken,
334        mode: ScalarPageMode,
335        contract: &ScalarCursorContract,
336    ) -> Result<(), QueryError> {
337        if token.signature() != contract.signature {
338            return Err(QueryError::from_cursor_plan_error(
339                CursorPlanError::continuation_cursor_signature_mismatch(
340                    &contract.signature,
341                    &token.signature(),
342                ),
343            ));
344        }
345        if token.mode() != mode
346            || token.authority() != contract.authority
347            || token.route_pin() != contract.route_pin
348            || token.window() != contract.window
349            || token.order_terms() != contract.order_terms.as_slice()
350        {
351            return Err(Self::scalar_page_cursor_error());
352        }
353
354        Ok(())
355    }
356
357    fn physical_primary_key_boundary(
358        bytes: &[u8],
359        catalog: &AcceptedSchemaCatalogContext,
360    ) -> Result<CursorBoundary, QueryError> {
361        let raw = RawDataStoreKey::from_persisted_bytes(bytes.to_vec());
362        let key = DecodedDataStoreKey::try_from_raw(&raw)
363            .map_err(|_| Self::scalar_page_cursor_error())?;
364        if key.entity_tag() != catalog.accepted_entity_authority().entity_tag() {
365            return Err(Self::scalar_page_cursor_error());
366        }
367        let primary_key_arity = catalog.accepted_schema_info().primary_key_names().len();
368        let mut slots = Vec::with_capacity(primary_key_arity);
369        for component_index in 0..primary_key_arity {
370            slots.push(CursorBoundarySlot::Present(
371                key.primary_key_component_runtime_value(component_index)
372                    .map_err(|_| Self::scalar_page_cursor_error())?,
373            ));
374        }
375
376        Ok(CursorBoundary { slots })
377    }
378
379    fn execute_dynamic_grouped_query_against_catalog(
380        &self,
381        request: &DynamicQuery,
382        lane: DynamicReadLane,
383        catalog: AcceptedSchemaCatalogContext,
384    ) -> Result<GroupedQueryOutput, QueryError> {
385        if !request.has_grouping() {
386            return Err(QueryError::intent(
387                IntentError::grouped_terminal_requires_grouped_query(),
388            ));
389        }
390        let query =
391            self.structural_query_from_dynamic_request(request, &catalog, lane.execution_lane())?;
392        let public_admission = match lane {
393            DynamicReadLane::Public => Some(QueryAdmissionPolicy::default_bounded_read()),
394            DynamicReadLane::Trusted => None,
395        };
396
397        self.execute_structural_grouped_from_query(
398            &query,
399            &catalog,
400            public_admission.as_ref(),
401            request.continuation_cursor(),
402        )
403    }
404
405    #[expect(
406        clippy::too_many_lines,
407        reason = "live-page orchestration keeps planning, cursor validation, execution, and response proof in one auditable boundary"
408    )]
409    fn execute_scalar_page_against_catalog(
410        &self,
411        request: &DynamicQuery,
412        continuation: Option<&str>,
413        lane: DynamicReadLane,
414        catalog: AcceptedSchemaCatalogContext,
415        mode: ScalarPageMode,
416        supplied_proof: Option<&ReadSetRevisionProof>,
417    ) -> Result<(LiveQueryPageOutput, Option<ReadSetRevisionProof>), ExhaustiveReadError> {
418        if request.has_grouping()
419            || request.grouped_execution_limits().is_some()
420            || request.continuation_cursor().is_some()
421        {
422            return Err(
423                QueryError::intent(IntentError::scalar_terminal_requires_scalar_query()).into(),
424            );
425        }
426
427        let exhaustive_proof = match mode {
428            ScalarPageMode::Live => None,
429            ScalarPageMode::Exhaustive => {
430                if continuation.is_some() && supplied_proof.is_none() {
431                    return Err(ReadSetRevisionError::ResumeProofRequired.into());
432                }
433                let proof = supplied_proof.cloned().map_or_else(
434                    || self.capture_entity_read_set_revision_proof(catalog.identity().store_path()),
435                    Ok,
436                )?;
437                Self::ensure_read_set_contains_store(&proof, catalog.identity().store_path())?;
438                self.verify_read_set_revision_proof(&proof)?;
439                Some(proof)
440            }
441        };
442
443        let envelope = match lane {
444            DynamicReadLane::Public => PageWorkEnvelope::public_scalar(),
445            DynamicReadLane::Trusted => PageWorkEnvelope::default_scalar(),
446        };
447        #[cfg(test)]
448        let envelope = SCALAR_PAGE_RESULT_BYTES_LIMIT_OVERRIDE.with(|limit| {
449            limit.get().map_or(envelope, |limit| {
450                envelope.with_limit_for_tests(DiagnosticExecutionBudgetResource::ResultBytes, limit)
451            })
452        });
453        #[cfg(test)]
454        let envelope = envelope.with_limit_for_tests(
455            DiagnosticExecutionBudgetResource::KeyIndexEntriesVisited,
456            SCALAR_PAGE_KEY_ENTRIES,
457        );
458        let page_row_limit = envelope
459            .limit(DiagnosticExecutionBudgetResource::ResultRows)
460            .and_then(|limit| usize::try_from(limit).ok())
461            .unwrap_or(SCALAR_PAGE_OUTPUT_ROWS)
462            .min(SCALAR_PAGE_OUTPUT_ROWS);
463        let decoded_token = decode_optional_cursor_token(continuation)
464            .map_err(QueryError::from_cursor_plan_error)?
465            .map(|bytes| {
466                ScalarPageToken::decode(
467                    bytes.as_slice(),
468                    &cursor_authentication_key().map_err(QueryError::execute)?,
469                )
470                .map_err(|error| {
471                    QueryError::from_cursor_plan_error(CursorPlanError::from_token_wire_error(
472                        error,
473                    ))
474                })
475            })
476            .transpose()?;
477        let prior_rows_emitted = decoded_token
478            .as_ref()
479            .map_or(0, |token| token.progress().rows_emitted());
480        let remaining_limit = request
481            .row_limit()
482            .map(|limit| u64::from(limit).saturating_sub(prior_rows_emitted));
483        let page_output_limit = remaining_limit
484            .unwrap_or(page_row_limit as u64)
485            .min(page_row_limit as u64);
486        let page_output_limit = usize::try_from(page_output_limit).unwrap_or(page_row_limit);
487        // Bound physical execution as well as outward projection. Use the
488        // authored total, not the remaining total, to reuse the same prepared
489        // plan across pages. The cursor authenticates that total separately.
490        let execution_limit = request
491            .row_limit()
492            .unwrap_or(u32::MAX)
493            .min(u32::try_from(page_row_limit).unwrap_or(u32::MAX));
494        let execution_lane = lane.execution_lane();
495        let exact_candidate = decoded_token.is_none()
496            && page_output_limit > 0
497            && Self::may_select_exact_single_primary_key(request, &catalog);
498        let initial_plan = if exact_candidate {
499            let query =
500                self.structural_query_from_dynamic_request(request, &catalog, execution_lane)?;
501            Some(
502                self.structural_projection_prepared_plan_for_accepted_authority(
503                    &query,
504                    catalog.accepted_entity_authority(),
505                    catalog.snapshot(),
506                    execution_lane,
507                )?,
508            )
509        } else {
510            None
511        };
512        let initial_is_exact_exhaustion =
513            initial_plan.as_ref().is_some_and(|(prepared_plan, _)| {
514                Self::exact_primary_key_candidate_bound(prepared_plan)
515                    .is_some_and(|bound| bound == 1 && bound <= page_output_limit)
516            });
517        let (prepared_plan, projection) = if initial_is_exact_exhaustion {
518            initial_plan.ok_or_else(Self::scalar_page_cursor_error)?
519        } else {
520            let query = self.structural_query_from_dynamic_request_with_page_limit(
521                request,
522                &catalog,
523                Some(execution_limit),
524                true,
525                execution_lane,
526            )?;
527            if let Some(route_pin) = decoded_token.as_ref().and_then(ScalarPageToken::route_pin) {
528                self.structural_projection_prepared_plan_for_accepted_authority_with_route_pin(
529                    &query,
530                    catalog.accepted_entity_authority(),
531                    execution_lane,
532                    route_pin,
533                )?
534                .ok_or_else(Self::scalar_page_cursor_error)?
535            } else {
536                self.structural_projection_prepared_plan_for_accepted_authority(
537                    &query,
538                    catalog.accepted_entity_authority(),
539                    catalog.snapshot(),
540                    execution_lane,
541                )?
542            }
543        };
544        if matches!(lane, DynamicReadLane::Public) {
545            let policy = QueryAdmissionPolicy::default_bounded_read();
546            let summary = policy.evaluate(
547                QueryAdmissionSummary::from_plan(policy.lane(), prepared_plan.logical_plan())
548                    .map_err(QueryError::execute)?,
549            );
550            if let Some(rejection) = summary.rejection() {
551                return Err(QueryError::from(rejection.code()).into());
552            }
553        }
554        let exact_initial_exhaustion = initial_is_exact_exhaustion;
555        let cursor_contract = decoded_token
556            .as_ref()
557            .map(|token| {
558                let contract = Self::scalar_cursor_contract(
559                    request,
560                    &catalog,
561                    envelope,
562                    &prepared_plan,
563                    mode,
564                    exhaustive_proof.as_ref(),
565                )?;
566                Self::validate_scalar_page_token(token, mode, &contract)?;
567                Ok::<_, QueryError>(contract)
568            })
569            .transpose()?;
570        let deferred_cursor_plan =
571            (!exact_initial_exhaustion && decoded_token.is_none()).then(|| prepared_plan.clone());
572        let continuation_context = match decoded_token.as_ref() {
573            None => ScalarContinuationContext::initial(),
574            Some(token) if token.progress().unconsumed_lookahead().is_some() => {
575                return Err(Self::scalar_page_cursor_error().into());
576            }
577            Some(token) => {
578                let logical = token.progress().last_emitted_logical().cloned();
579                match token.progress().last_consumed_physical() {
580                    Some(physical) => ScalarContinuationContext::resumed_with_primary_progress(
581                        logical,
582                        Self::physical_primary_key_boundary(physical, &catalog)?,
583                    ),
584                    None => logical.map_or_else(
585                        ScalarContinuationContext::initial,
586                        ScalarContinuationContext::resumed,
587                    ),
588                }
589            }
590        };
591        if decoded_token.is_some() && !continuation_context.has_progress() {
592            return Err(Self::scalar_page_cursor_error().into());
593        }
594
595        let value_catalog = prepared_plan
596            .authority_ref()
597            .accepted_schema_info()
598            .value_catalog_handle()
599            .clone();
600        let (columns, _fixed_scales) = projection.into_components();
601        let projection_request = StructuralProjectionRequest::new(prepared_plan, execution_lane)
602            .with_distinct_output_offset(usize::try_from(prior_rows_emitted).unwrap_or(usize::MAX))
603            .with_page_work_envelope(envelope);
604        let projection_request = if exact_initial_exhaustion {
605            projection_request
606        } else {
607            projection_request
608                .with_continuation(continuation_context)
609                .with_cursor_emission(page_output_limit)
610        };
611        let page = execute_structural_projection_page(&self.db, projection_request)
612            .map_err(QueryError::execute)?;
613        let row_count = page.rows.row_count();
614        let rows = page
615            .rows
616            .into_value_rows()
617            .into_iter()
618            .map(|row| {
619                row.into_iter()
620                    .map(|value| {
621                        crate::db::schema::output_value_from_runtime(
622                            value_catalog.enum_catalog(),
623                            value,
624                        )
625                        .map_err(|_| QueryError::invariant())
626                    })
627                    .collect::<Result<Vec<_>, _>>()
628            })
629            .collect::<Result<Vec<_>, _>>()?;
630        let rows_emitted = prior_rows_emitted.saturating_add(u64::from(row_count));
631        let total_limit_reached = request
632            .row_limit()
633            .is_some_and(|limit| rows_emitted >= u64::from(limit));
634        let continuation = if page.has_more && !total_limit_reached {
635            if page.last_emitted_logical.is_none() && page.last_consumed_physical.is_none() {
636                return Err(Self::scalar_page_cursor_error().into());
637            }
638            let cursor_contract = if let Some(contract) = cursor_contract {
639                contract
640            } else {
641                let prepared_plan = deferred_cursor_plan
642                    .as_ref()
643                    .ok_or_else(Self::scalar_page_cursor_error)?;
644                Self::scalar_cursor_contract(
645                    request,
646                    &catalog,
647                    envelope,
648                    prepared_plan,
649                    mode,
650                    exhaustive_proof.as_ref(),
651                )?
652            };
653            let token = ScalarPageToken::new(
654                mode,
655                cursor_contract.signature,
656                cursor_contract.authority,
657                cursor_contract.route_pin,
658                cursor_contract.window,
659                cursor_contract.order_terms,
660                ScalarPageTokenProgress::new(
661                    page.last_emitted_logical,
662                    page.last_consumed_physical,
663                    None,
664                    decoded_token
665                        .as_ref()
666                        .map_or(0, |token| token.progress().matching_rows_skipped()),
667                    rows_emitted,
668                ),
669            );
670            Some(encode_cursor(
671                token
672                    .encode(&cursor_authentication_key().map_err(QueryError::execute)?)
673                    .map_err(|error| {
674                        QueryError::from_cursor_plan_error(CursorPlanError::from_token_wire_error(
675                            error,
676                        ))
677                    })?
678                    .as_slice(),
679            ))
680        } else {
681            None
682        };
683
684        if let Some(proof) = exhaustive_proof.as_ref() {
685            self.verify_read_set_revision_proof(proof)?;
686        }
687
688        Ok((
689            LiveQueryPageOutput {
690                entity: catalog.snapshot().entity_name().to_string(),
691                columns,
692                rows,
693                row_count,
694                continuation,
695                work: ScalarPageWork {
696                    envelope_identity: envelope.identity(),
697                    entries_visited: page.scanned_keys as u64,
698                    result_rows: row_count,
699                },
700            },
701            exhaustive_proof,
702        ))
703    }
704
705    /// Execute one revision-tolerant bounded scalar page.
706    pub fn execute_public_live_page(
707        &self,
708        request: &DynamicQuery,
709        continuation: Option<&str>,
710    ) -> Result<LiveQueryPageOutput, QueryError> {
711        let catalog = self
712            .accepted_schema_catalog_context_for_entity_name(Some(request.entity()))
713            .map_err(QueryError::execute)?;
714        self.execute_scalar_page_against_catalog(
715            request,
716            continuation,
717            DynamicReadLane::Public,
718            catalog,
719            ScalarPageMode::Live,
720            None,
721        )
722        .map(|(page, _)| page)
723        .map_err(Self::live_page_error)
724    }
725
726    /// Execute one live page through a typed binding's immutable accepted
727    /// entity identity. `None` means the opaque binding is stale.
728    #[doc(hidden)]
729    pub fn execute_public_live_page_for_typed_binding(
730        &self,
731        binding: &DynamicTypedEntityBinding,
732        request: &DynamicQuery,
733        continuation: Option<&str>,
734    ) -> Result<Option<LiveQueryPageOutput>, QueryError> {
735        let Some(catalog) = self
736            .current_typed_entity_binding_catalog(binding)
737            .map_err(QueryError::execute)?
738        else {
739            return Ok(None);
740        };
741        self.execute_scalar_page_against_catalog(
742            request,
743            continuation,
744            DynamicReadLane::Public,
745            catalog,
746            ScalarPageMode::Live,
747            None,
748        )
749        .map(|(page, _)| Some(page))
750        .map_err(Self::live_page_error)
751    }
752
753    /// Execute one ordinary entity-name-driven bounded grouped read.
754    pub fn execute_public_dynamic_grouped_query(
755        &self,
756        request: &DynamicQuery,
757    ) -> Result<GroupedQueryOutput, QueryError> {
758        let catalog = self
759            .accepted_schema_catalog_context_for_entity_name(Some(request.entity()))
760            .map_err(QueryError::execute)?;
761        self.execute_dynamic_grouped_query_against_catalog(
762            request,
763            DynamicReadLane::Public,
764            catalog,
765        )
766    }
767
768    /// Execute one grouped typed read through the binding's immutable accepted
769    /// entity identity. `None` means the opaque binding is stale.
770    #[doc(hidden)]
771    pub fn execute_public_dynamic_grouped_query_for_typed_binding(
772        &self,
773        binding: &DynamicTypedEntityBinding,
774        request: &DynamicQuery,
775    ) -> Result<Option<GroupedQueryOutput>, QueryError> {
776        let Some(catalog) = self
777            .current_typed_entity_binding_catalog(binding)
778            .map_err(QueryError::execute)?
779        else {
780            return Ok(None);
781        };
782        self.execute_dynamic_grouped_query_against_catalog(
783            request,
784            DynamicReadLane::Public,
785            catalog,
786        )
787        .map(Some)
788    }
789
790    /// Execute one trusted entity-name-driven grouped read.
791    ///
792    /// This bypasses ordinary public admission but retains accepted-schema
793    /// planning, explicit grouped limits, cursor validation, and execution.
794    pub fn execute_trusted_dynamic_grouped_query(
795        &self,
796        request: &DynamicQuery,
797    ) -> Result<GroupedQueryOutput, QueryError> {
798        let catalog = self
799            .accepted_schema_catalog_context_for_entity_name(Some(request.entity()))
800            .map_err(QueryError::execute)?;
801        self.execute_dynamic_grouped_query_against_catalog(
802            request,
803            DynamicReadLane::Trusted,
804            catalog,
805        )
806    }
807
808    /// Execute one trusted revision-tolerant bounded dynamic page.
809    ///
810    /// Trusted execution bypasses public admission but retains the same
811    /// physical and aggregate request budgets as every other read lane.
812    pub fn execute_trusted_live_page(
813        &self,
814        request: &DynamicQuery,
815        continuation: Option<&str>,
816    ) -> Result<LiveQueryPageOutput, QueryError> {
817        let catalog = self
818            .accepted_schema_catalog_context_for_entity_name(Some(request.entity()))
819            .map_err(QueryError::execute)?;
820        self.execute_scalar_page_against_catalog(
821            request,
822            continuation,
823            DynamicReadLane::Trusted,
824            catalog,
825            ScalarPageMode::Live,
826            None,
827        )
828        .map(|(page, _)| page)
829        .map_err(Self::live_page_error)
830    }
831
832    #[cfg(test)]
833    pub(in crate::db) fn execute_trusted_live_page_with_result_bytes_limit_for_tests(
834        &self,
835        request: &DynamicQuery,
836        continuation: Option<&str>,
837        result_bytes_limit: u64,
838    ) -> Result<LiveQueryPageOutput, QueryError> {
839        let previous = SCALAR_PAGE_RESULT_BYTES_LIMIT_OVERRIDE
840            .with(|limit| limit.replace(Some(result_bytes_limit)));
841        let _guard = ScalarPageResultBytesLimitGuard(previous);
842        self.execute_trusted_live_page(request, continuation)
843    }
844
845    /// Execute one revision-strict bounded dynamic page.
846    pub fn execute_public_exhaustive_page(
847        &self,
848        request: &DynamicQuery,
849        continuation: Option<&str>,
850        proof: Option<&ReadSetRevisionProof>,
851    ) -> Result<ExhaustiveQueryPageOutput, ExhaustiveReadError> {
852        let catalog =
853            self.accepted_schema_catalog_context_for_entity_name(Some(request.entity()))?;
854        let (page, proof) = self.execute_scalar_page_against_catalog(
855            request,
856            continuation,
857            DynamicReadLane::Public,
858            catalog,
859            ScalarPageMode::Exhaustive,
860            proof,
861        )?;
862        let proof = proof.ok_or(ReadSetRevisionError::NonCanonical)?;
863        Ok(ExhaustiveQueryPageOutput::from_live_page(page, proof))
864    }
865
866    /// Execute one exhaustive page through a typed binding's accepted identity.
867    #[doc(hidden)]
868    pub fn execute_public_exhaustive_page_for_typed_binding(
869        &self,
870        binding: &DynamicTypedEntityBinding,
871        request: &DynamicQuery,
872        continuation: Option<&str>,
873        proof: Option<&ReadSetRevisionProof>,
874    ) -> Result<Option<ExhaustiveQueryPageOutput>, ExhaustiveReadError> {
875        let Some(catalog) = self.current_typed_entity_binding_catalog(binding)? else {
876            return Ok(None);
877        };
878        let (page, proof) = self.execute_scalar_page_against_catalog(
879            request,
880            continuation,
881            DynamicReadLane::Public,
882            catalog,
883            ScalarPageMode::Exhaustive,
884            proof,
885        )?;
886        let proof = proof.ok_or(ReadSetRevisionError::NonCanonical)?;
887        Ok(Some(ExhaustiveQueryPageOutput::from_live_page(page, proof)))
888    }
889
890    /// Execute one trusted revision-strict bounded dynamic page.
891    pub fn execute_trusted_exhaustive_page(
892        &self,
893        request: &DynamicQuery,
894        continuation: Option<&str>,
895        proof: Option<&ReadSetRevisionProof>,
896    ) -> Result<ExhaustiveQueryPageOutput, ExhaustiveReadError> {
897        let catalog =
898            self.accepted_schema_catalog_context_for_entity_name(Some(request.entity()))?;
899        let (page, proof) = self.execute_scalar_page_against_catalog(
900            request,
901            continuation,
902            DynamicReadLane::Trusted,
903            catalog,
904            ScalarPageMode::Exhaustive,
905            proof,
906        )?;
907        let proof = proof.ok_or(ReadSetRevisionError::NonCanonical)?;
908        Ok(ExhaustiveQueryPageOutput::from_live_page(page, proof))
909    }
910
911    fn live_page_error(error: ExhaustiveReadError) -> QueryError {
912        match error {
913            ExhaustiveReadError::Query(error) => error,
914            ExhaustiveReadError::Revision(_) => QueryError::invariant(),
915        }
916    }
917}