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