1#[cfg(feature = "sql")]
7use crate::db::query::plan::expr::ProjectionSelection;
8#[cfg(any(test, feature = "sql"))]
9use crate::db::{
10 predicate::Predicate,
11 query::plan::{OrderSpec, expr::Expr},
12};
13use crate::{
14 db::{
15 predicate::{CompareOp, MissingRowPolicy},
16 query::{
17 builder::AggregateExpr,
18 explain::ExplainPlan,
19 expr::FilterExpr,
20 expr::OrderTerm as FluentOrderTerm,
21 intent::{AccessRequirements, QueryError, QueryModel, RequiredAccessPath},
22 plan::{
23 AccessPlannedQuery, LoadSpec, PreparedScalarPlanningState, QueryMode,
24 VisibleIndexes,
25 },
26 },
27 schema::SchemaInfo,
28 },
29 traits::{EntityKind, KeyValueCodec, SingletonEntity},
30 value::{InputValue, Value},
31};
32use std::sync::OnceLock;
33
34use core::marker::PhantomData;
35
36#[derive(Clone, Debug)]
45pub(in crate::db) struct StructuralQuery {
46 intent: QueryModel<'static, Value>,
47 access_requirements: AccessRequirements,
48 structural_cache_key: OnceLock<crate::db::query::intent::StructuralQueryCacheKey>,
49}
50
51impl StructuralQuery {
52 #[must_use]
53 pub(in crate::db) const fn new(
54 model: &'static crate::model::entity::EntityModel,
55 consistency: MissingRowPolicy,
56 ) -> Self {
57 Self {
58 intent: QueryModel::new(model, consistency),
59 access_requirements: AccessRequirements::new(),
60 structural_cache_key: OnceLock::new(),
61 }
62 }
63
64 const fn from_intent_and_access_requirements(
68 intent: QueryModel<'static, Value>,
69 access_requirements: AccessRequirements,
70 ) -> Self {
71 Self {
72 intent,
73 access_requirements,
74 structural_cache_key: OnceLock::new(),
75 }
76 }
77
78 fn map_intent(
81 self,
82 map: impl FnOnce(QueryModel<'static, Value>) -> QueryModel<'static, Value>,
83 ) -> Self {
84 let Self {
85 intent,
86 access_requirements,
87 ..
88 } = self;
89
90 Self::from_intent_and_access_requirements(map(intent), access_requirements)
91 }
92
93 fn try_map_intent(
96 self,
97 map: impl FnOnce(QueryModel<'static, Value>) -> Result<QueryModel<'static, Value>, QueryError>,
98 ) -> Result<Self, QueryError> {
99 let Self {
100 intent,
101 access_requirements,
102 ..
103 } = self;
104
105 map(intent)
106 .map(|intent| Self::from_intent_and_access_requirements(intent, access_requirements))
107 }
108
109 #[must_use]
110 const fn mode(&self) -> QueryMode {
111 self.intent.mode()
112 }
113
114 #[must_use]
115 fn has_explicit_order(&self) -> bool {
116 self.intent.has_explicit_order()
117 }
118
119 #[must_use]
120 pub(in crate::db) const fn has_grouping(&self) -> bool {
121 self.intent.has_grouping()
122 }
123
124 #[must_use]
125 pub(in crate::db) const fn has_scalar_filter(&self) -> bool {
126 self.intent.has_scalar_filter()
127 }
128
129 #[must_use]
130 #[cfg(feature = "sql")]
131 pub(in crate::db) fn direct_count_cardinality_prefix_candidate(&self) -> bool {
132 if self.intent.validate_policy_shape().is_err() {
133 return false;
134 }
135
136 let access_inputs = self.intent.planning_access_inputs();
137 let logical_inputs = self.intent.planning_logical_inputs();
138 if access_inputs.order().is_some()
139 || access_inputs.has_key_access_override()
140 || logical_inputs.distinct()
141 || logical_inputs.has_group()
142 || logical_inputs.has_having_expr()
143 || (logical_inputs.has_filter_expr() && !logical_inputs.filter_predicate_covers_expr())
144 {
145 return false;
146 }
147
148 let QueryMode::Load(load_spec) = self.intent.mode() else {
149 return false;
150 };
151 load_spec.limit().is_none()
152 && load_spec.offset() == 0
153 && access_inputs.predicate().is_some()
154 }
155
156 #[must_use]
157 const fn load_spec(&self) -> Option<LoadSpec> {
158 match self.intent.mode() {
159 QueryMode::Load(spec) => Some(spec),
160 QueryMode::Delete(_) => None,
161 }
162 }
163
164 #[must_use]
166 #[cfg(test)]
167 pub(in crate::db) fn filter_predicate(mut self, predicate: Predicate) -> Self {
168 self.intent = self.intent.filter_predicate(predicate);
169 self
170 }
171
172 #[must_use]
174 #[cfg(any(test, feature = "sql"))]
175 pub(in crate::db) fn filter_normalized_predicate(mut self, predicate: Predicate) -> Self {
176 self.intent = self.intent.filter_normalized_predicate(predicate);
177 self
178 }
179
180 #[must_use]
181 pub(in crate::db) fn filter(mut self, expr: impl Into<FilterExpr>) -> Self {
182 self.intent = self.intent.filter(expr.into());
183 self
184 }
185
186 #[must_use]
187 #[cfg(feature = "sql")]
188 pub(in crate::db) fn filter_expr_with_normalized_predicate(
189 mut self,
190 expr: Expr,
191 predicate: Predicate,
192 ) -> Self {
193 self.intent = self
194 .intent
195 .filter_expr_with_normalized_predicate(expr, predicate);
196 self
197 }
198 pub(in crate::db) fn order_term(mut self, term: FluentOrderTerm) -> Self {
199 self.intent = self.intent.order_term(term);
200 self
201 }
202
203 #[must_use]
207 #[cfg(feature = "sql")]
208 pub(in crate::db) fn filter_expr(mut self, expr: Expr) -> Self {
209 self.intent = self.intent.filter_expr(expr);
210 self
211 }
212
213 #[must_use]
214 #[cfg(any(test, feature = "sql"))]
215 pub(in crate::db) fn order_spec(mut self, order: OrderSpec) -> Self {
216 self.intent = self.intent.order_spec(order);
217 self
218 }
219
220 #[must_use]
221 pub(in crate::db) fn distinct(mut self) -> Self {
222 self.intent = self.intent.distinct();
223 self
224 }
225
226 #[cfg(feature = "sql")]
227 #[must_use]
228 pub(in crate::db) fn select_fields<I, S>(mut self, fields: I) -> Self
229 where
230 I: IntoIterator<Item = S>,
231 S: Into<String>,
232 {
233 self.intent = self.intent.select_fields(fields);
234 self
235 }
236
237 #[cfg(feature = "sql")]
238 #[must_use]
239 pub(in crate::db) fn projection_selection(mut self, selection: ProjectionSelection) -> Self {
240 self.intent = self.intent.projection_selection(selection);
241 self
242 }
243
244 pub(in crate::db) fn group_by(self, field: impl AsRef<str>) -> Result<Self, QueryError> {
245 self.try_map_intent(|intent| intent.push_group_field(field.as_ref()))
246 }
247
248 pub(in crate::db) fn group_by_with_schema(
249 self,
250 field: impl AsRef<str>,
251 schema: &SchemaInfo,
252 ) -> Result<Self, QueryError> {
253 self.try_map_intent(|intent| intent.push_group_field_with_schema(field.as_ref(), schema))
254 }
255
256 #[must_use]
257 pub(in crate::db) fn aggregate(mut self, aggregate: AggregateExpr) -> Self {
258 self.intent = self.intent.push_group_aggregate(aggregate);
259 self
260 }
261
262 #[must_use]
263 fn grouped_limits(mut self, max_groups: u64, max_group_bytes: u64) -> Self {
264 self.intent = self.intent.grouped_limits(max_groups, max_group_bytes);
265 self
266 }
267
268 pub(in crate::db) fn having_group(
269 self,
270 field: impl AsRef<str>,
271 op: CompareOp,
272 value: Value,
273 ) -> Result<Self, QueryError> {
274 let field = field.as_ref().to_owned();
275 self.try_map_intent(|intent| intent.push_having_group_clause(&field, op, value))
276 }
277
278 pub(in crate::db) fn having_group_with_schema(
279 self,
280 field: impl AsRef<str>,
281 schema: &SchemaInfo,
282 op: CompareOp,
283 value: Value,
284 ) -> Result<Self, QueryError> {
285 let field = field.as_ref().to_owned();
286 self.try_map_intent(|intent| {
287 intent.push_having_group_clause_with_schema(&field, schema, op, value)
288 })
289 }
290
291 pub(in crate::db) fn having_aggregate(
292 self,
293 aggregate_index: usize,
294 op: CompareOp,
295 value: Value,
296 ) -> Result<Self, QueryError> {
297 self.try_map_intent(|intent| {
298 intent.push_having_aggregate_clause(aggregate_index, op, value)
299 })
300 }
301
302 #[cfg(test)]
303 pub(in crate::db) fn having_expr(self, expr: Expr) -> Result<Self, QueryError> {
304 self.try_map_intent(|intent| intent.push_having_expr(expr))
305 }
306
307 #[cfg(feature = "sql")]
308 pub(in crate::db) fn having_expr_preserving_shape(
309 self,
310 expr: Expr,
311 ) -> Result<Self, QueryError> {
312 self.try_map_intent(|intent| intent.push_having_expr_preserving_shape(expr))
313 }
314
315 #[must_use]
316 fn by_id(self, id: Value) -> Self {
317 self.map_intent(|intent| intent.by_id(id))
318 }
319
320 #[must_use]
321 fn by_ids<I>(self, ids: I) -> Self
322 where
323 I: IntoIterator<Item = Value>,
324 {
325 self.map_intent(|intent| intent.by_ids(ids))
326 }
327
328 #[must_use]
329 fn only(self, id: Value) -> Self {
330 self.map_intent(|intent| intent.only(id))
331 }
332
333 #[must_use]
334 pub(in crate::db) fn delete(mut self) -> Self {
335 self.intent = self.intent.delete();
336 self
337 }
338
339 #[must_use]
340 pub(in crate::db) fn limit(mut self, limit: u32) -> Self {
341 self.intent = self.intent.limit(limit);
342 self
343 }
344
345 #[must_use]
346 pub(in crate::db) fn offset(mut self, offset: u32) -> Self {
347 self.intent = self.intent.offset(offset);
348 self
349 }
350
351 pub(in crate::db) fn build_plan(&self) -> Result<AccessPlannedQuery, QueryError> {
352 let mut plan = self.intent.build_plan_model()?;
353 self.validate_access_requirements_for_visibility(&mut plan, None)?;
354
355 Ok(plan)
356 }
357
358 pub(in crate::db) fn build_plan_with_visible_indexes(
359 &self,
360 visible_indexes: &VisibleIndexes<'_>,
361 ) -> Result<AccessPlannedQuery, QueryError> {
362 let mut plan = self.intent.build_plan_model_with_indexes(visible_indexes)?;
363 self.validate_access_requirements_for_visibility(&mut plan, Some(visible_indexes))?;
364
365 Ok(plan)
366 }
367
368 pub(in crate::db) fn prepare_scalar_planning_state_with_schema_info(
369 &self,
370 schema_info: SchemaInfo,
371 ) -> Result<PreparedScalarPlanningState<'_>, QueryError> {
372 self.intent
373 .prepare_scalar_planning_state_with_schema_info(schema_info)
374 }
375
376 pub(in crate::db) fn build_plan_with_visible_indexes_from_scalar_planning_state(
377 &self,
378 visible_indexes: &VisibleIndexes<'_>,
379 planning_state: PreparedScalarPlanningState<'_>,
380 ) -> Result<AccessPlannedQuery, QueryError> {
381 let mut plan = self
382 .intent
383 .build_plan_model_with_indexes_from_scalar_planning_state(
384 visible_indexes,
385 planning_state,
386 )?;
387 self.validate_access_requirements_for_visibility(&mut plan, Some(visible_indexes))?;
388
389 Ok(plan)
390 }
391
392 #[cfg(feature = "sql")]
393 pub(in crate::db) fn try_build_count_cardinality_prefix_access_with_schema_info(
394 &self,
395 visible_indexes: &VisibleIndexes<'_>,
396 schema_info: &SchemaInfo,
397 ) -> Result<Option<crate::db::query::plan::CountCardinalityPrefixAccess<'_>>, QueryError> {
398 crate::db::query::plan::try_build_count_cardinality_prefix_access_from_query_model(
399 &self.intent,
400 visible_indexes,
401 schema_info,
402 )
403 }
404
405 pub(in crate::db) fn try_build_trivial_scalar_load_plan_with_schema_info(
406 &self,
407 schema_info: SchemaInfo,
408 ) -> Result<Option<AccessPlannedQuery>, QueryError> {
409 let mut plan = self
410 .intent
411 .try_build_trivial_scalar_load_plan_with_schema_info(schema_info)?;
412 if let Some(plan) = &mut plan {
413 self.validate_access_requirements_for_visibility(plan, None)?;
414 }
415
416 Ok(plan)
417 }
418
419 #[must_use]
420 pub(in crate::db) fn trivial_scalar_load_fast_path_eligible_with_schema(
421 &self,
422 schema_info: &SchemaInfo,
423 ) -> bool {
424 self.intent
425 .trivial_scalar_load_fast_path_eligible_with_schema(schema_info)
426 }
427
428 #[must_use]
429 #[cfg(test)]
430 pub(in crate::db) fn structural_cache_key(
431 &self,
432 ) -> crate::db::query::intent::StructuralQueryCacheKey {
433 crate::db::query::intent::StructuralQueryCacheKey::from_query_model(&self.intent)
434 }
435
436 #[must_use]
437 pub(in crate::db) fn structural_cache_key_with_normalized_predicate_fingerprint(
438 &self,
439 predicate_fingerprint: Option<[u8; 32]>,
440 ) -> crate::db::query::intent::StructuralQueryCacheKey {
441 if predicate_fingerprint.is_none() {
442 return self
443 .structural_cache_key
444 .get_or_init(|| {
445 self.intent
446 .structural_cache_key_with_normalized_predicate_fingerprint(None)
447 })
448 .clone();
449 }
450
451 self.intent
452 .structural_cache_key_with_normalized_predicate_fingerprint(predicate_fingerprint)
453 }
454
455 fn build_plan_for_visibility(
458 &self,
459 visible_indexes: Option<&VisibleIndexes<'_>>,
460 ) -> Result<AccessPlannedQuery, QueryError> {
461 match visible_indexes {
462 Some(visible_indexes) => self.build_plan_with_visible_indexes(visible_indexes),
463 None => self.build_plan(),
464 }
465 }
466
467 fn finalize_access_choice_for_visibility(
468 &self,
469 plan: &mut AccessPlannedQuery,
470 visible_indexes: Option<&VisibleIndexes<'_>>,
471 ) {
472 match visible_indexes {
473 Some(visible_indexes) => {
474 if let Some(schema_info) = visible_indexes.accepted_schema_info() {
475 plan.finalize_access_choice_for_model_with_semantic_indexes_and_schema(
476 self.intent.model(),
477 visible_indexes.accepted_semantic_index_contracts(),
478 schema_info,
479 );
480 } else {
481 plan.finalize_access_choice_for_model_only_with_indexes(
482 self.intent.model(),
483 visible_indexes.generated_model_only_indexes(),
484 );
485 }
486 }
487 None => {
488 plan.finalize_access_choice_for_model_only_with_indexes(
489 self.intent.model(),
490 self.intent.model().indexes(),
491 );
492 }
493 }
494 }
495
496 fn validate_access_requirements_for_visibility(
497 &self,
498 plan: &mut AccessPlannedQuery,
499 visible_indexes: Option<&VisibleIndexes<'_>>,
500 ) -> Result<(), QueryError> {
501 if self.access_requirements.is_empty() {
502 return Ok(());
503 }
504
505 self.finalize_access_choice_for_visibility(plan, visible_indexes);
506 self.access_requirements.validate(plan)
507 }
508
509 const fn require_index(mut self) -> Self {
510 self.access_requirements.require_index();
511 self
512 }
513
514 fn require_index_named(mut self, index_name: impl Into<String>) -> Self {
515 self.access_requirements.require_index_named(index_name);
516 self
517 }
518
519 const fn require_access_path(mut self, path: RequiredAccessPath) -> Self {
520 self.access_requirements.require_access_path(path);
521 self
522 }
523
524 const fn require_no_residual_filter(mut self) -> Self {
525 self.access_requirements.require_no_residual_filter();
526 self
527 }
528
529 #[must_use]
530 pub(in crate::db) const fn model(&self) -> &'static crate::model::entity::EntityModel {
531 self.intent.model()
532 }
533}
534
535#[derive(Clone, Debug)]
544struct QueryPlanHandle {
545 plan: Box<AccessPlannedQuery>,
546}
547
548impl QueryPlanHandle {
549 #[must_use]
550 fn from_plan(plan: AccessPlannedQuery) -> Self {
551 Self {
552 plan: Box::new(plan),
553 }
554 }
555
556 #[must_use]
557 const fn logical_plan(&self) -> &AccessPlannedQuery {
558 &self.plan
559 }
560
561 #[must_use]
562 #[cfg(test)]
563 fn into_inner(self) -> AccessPlannedQuery {
564 *self.plan
565 }
566}
567
568#[derive(Debug)]
576pub struct PlannedQuery<E: EntityKind> {
577 plan: QueryPlanHandle,
578 _marker: PhantomData<E>,
579}
580
581impl<E: EntityKind> PlannedQuery<E> {
582 #[must_use]
583 pub(in crate::db) fn from_plan(plan: AccessPlannedQuery) -> Self {
584 Self {
585 plan: QueryPlanHandle::from_plan(plan),
586 _marker: PhantomData,
587 }
588 }
589
590 #[must_use]
591 pub fn explain(&self) -> ExplainPlan {
592 self.plan.logical_plan().explain()
593 }
594
595 #[must_use]
597 pub fn plan_hash_hex(&self) -> String {
598 self.plan.logical_plan().fingerprint().to_string()
599 }
600}
601
602#[derive(Clone, Debug)]
612pub struct CompiledQuery<E: EntityKind> {
613 plan: QueryPlanHandle,
614 _marker: PhantomData<E>,
615}
616
617impl<E: EntityKind> CompiledQuery<E> {
618 #[must_use]
619 pub(in crate::db) fn from_plan(plan: AccessPlannedQuery) -> Self {
620 Self {
621 plan: QueryPlanHandle::from_plan(plan),
622 _marker: PhantomData,
623 }
624 }
625
626 #[must_use]
627 pub fn explain(&self) -> ExplainPlan {
628 self.plan.logical_plan().explain()
629 }
630
631 #[must_use]
633 pub fn plan_hash_hex(&self) -> String {
634 self.plan.logical_plan().fingerprint().to_string()
635 }
636
637 #[must_use]
638 #[cfg(test)]
639 pub(in crate::db) fn projection_spec(&self) -> crate::db::query::plan::expr::ProjectionSpec {
640 self.plan.logical_plan().projection_spec(E::MODEL)
641 }
642
643 #[cfg(test)]
645 pub(in crate::db) fn into_plan(self) -> AccessPlannedQuery {
646 self.plan.into_inner()
647 }
648
649 #[must_use]
650 #[cfg(test)]
651 pub(in crate::db) fn into_inner(self) -> AccessPlannedQuery {
652 self.plan.into_inner()
653 }
654}
655
656#[derive(Debug)]
668pub struct Query<E: EntityKind> {
669 inner: StructuralQuery,
670 _marker: PhantomData<E>,
671}
672
673impl<E: EntityKind> Query<E> {
674 pub(in crate::db) const fn from_inner(inner: StructuralQuery) -> Self {
676 Self {
677 inner,
678 _marker: PhantomData,
679 }
680 }
681
682 #[must_use]
686 pub const fn new(consistency: MissingRowPolicy) -> Self {
687 Self::from_inner(StructuralQuery::new(E::MODEL, consistency))
688 }
689
690 #[must_use]
692 pub const fn mode(&self) -> QueryMode {
693 self.inner.mode()
694 }
695
696 #[cfg(test)]
697 pub(in crate::db) fn explain_with_visible_indexes(
698 &self,
699 visible_indexes: &VisibleIndexes<'_>,
700 ) -> Result<ExplainPlan, QueryError> {
701 let mut plan = self.build_plan_for_visibility(Some(visible_indexes))?;
702 self.inner
703 .finalize_access_choice_for_visibility(&mut plan, Some(visible_indexes));
704
705 Ok(plan.explain())
706 }
707
708 #[cfg(test)]
709 pub(in crate::db) fn plan_hash_hex_with_visible_indexes(
710 &self,
711 visible_indexes: &VisibleIndexes<'_>,
712 ) -> Result<String, QueryError> {
713 let plan = self.build_plan_for_visibility(Some(visible_indexes))?;
714
715 Ok(plan.fingerprint().to_string())
716 }
717
718 fn build_plan_for_visibility(
721 &self,
722 visible_indexes: Option<&VisibleIndexes<'_>>,
723 ) -> Result<AccessPlannedQuery, QueryError> {
724 self.inner.build_plan_for_visibility(visible_indexes)
725 }
726
727 fn map_plan_for_visibility<T>(
731 &self,
732 visible_indexes: Option<&VisibleIndexes<'_>>,
733 map: impl FnOnce(AccessPlannedQuery) -> T,
734 ) -> Result<T, QueryError> {
735 let plan = self.build_plan_for_visibility(visible_indexes)?;
736
737 Ok(map(plan))
738 }
739
740 pub(in crate::db) fn planned_query_from_plan(plan: AccessPlannedQuery) -> PlannedQuery<E> {
742 PlannedQuery::from_plan(plan)
743 }
744
745 pub(in crate::db) fn compiled_query_from_plan(plan: AccessPlannedQuery) -> CompiledQuery<E> {
747 CompiledQuery::from_plan(plan)
748 }
749
750 #[must_use]
751 pub(in crate::db::query) fn has_explicit_order(&self) -> bool {
752 self.inner.has_explicit_order()
753 }
754
755 #[must_use]
756 pub(in crate::db) const fn structural(&self) -> &StructuralQuery {
757 &self.inner
758 }
759
760 #[must_use]
761 pub const fn has_grouping(&self) -> bool {
762 self.inner.has_grouping()
763 }
764
765 #[must_use]
766 pub(in crate::db::query) const fn load_spec(&self) -> Option<LoadSpec> {
767 self.inner.load_spec()
768 }
769
770 #[must_use]
772 pub fn filter(mut self, expr: impl Into<FilterExpr>) -> Self {
773 self.inner = self.inner.filter(expr);
774 self
775 }
776
777 #[cfg(test)]
781 #[must_use]
782 pub(in crate::db) fn filter_expr(mut self, expr: Expr) -> Self {
783 self.inner = self.inner.filter_expr(expr);
784 self
785 }
786
787 #[cfg(test)]
791 #[must_use]
792 pub(in crate::db) fn filter_predicate(mut self, predicate: Predicate) -> Self {
793 self.inner = self.inner.filter_predicate(predicate);
794 self
795 }
796
797 #[must_use]
799 pub fn order_term(mut self, term: FluentOrderTerm) -> Self {
800 self.inner = self.inner.order_term(term);
801 self
802 }
803
804 #[must_use]
806 pub fn order_terms<I>(mut self, terms: I) -> Self
807 where
808 I: IntoIterator<Item = FluentOrderTerm>,
809 {
810 for term in terms {
811 self.inner = self.inner.order_term(term);
812 }
813
814 self
815 }
816
817 #[must_use]
819 pub fn distinct(mut self) -> Self {
820 self.inner = self.inner.distinct();
821 self
822 }
823
824 #[cfg(all(test, feature = "sql"))]
827 #[must_use]
828 pub(in crate::db) fn select_fields<I, S>(mut self, fields: I) -> Self
829 where
830 I: IntoIterator<Item = S>,
831 S: Into<String>,
832 {
833 self.inner = self.inner.select_fields(fields);
834 self
835 }
836
837 pub fn group_by(self, field: impl AsRef<str>) -> Result<Self, QueryError> {
839 let Self { inner, .. } = self;
840 let inner = inner.group_by(field)?;
841
842 Ok(Self::from_inner(inner))
843 }
844
845 pub(in crate::db) fn group_by_with_schema(
846 self,
847 field: impl AsRef<str>,
848 schema: &SchemaInfo,
849 ) -> Result<Self, QueryError> {
850 let Self { inner, .. } = self;
851 let inner = inner.group_by_with_schema(field, schema)?;
852
853 Ok(Self::from_inner(inner))
854 }
855
856 #[must_use]
858 pub fn aggregate(mut self, aggregate: AggregateExpr) -> Self {
859 self.inner = self.inner.aggregate(aggregate);
860 self
861 }
862
863 #[must_use]
865 pub fn grouped_limits(mut self, max_groups: u64, max_group_bytes: u64) -> Self {
866 self.inner = self.inner.grouped_limits(max_groups, max_group_bytes);
867 self
868 }
869
870 pub fn having_group(
872 self,
873 field: impl AsRef<str>,
874 op: CompareOp,
875 value: InputValue,
876 ) -> Result<Self, QueryError> {
877 let Self { inner, .. } = self;
878 let inner = inner.having_group(field, op, value.into())?;
879
880 Ok(Self::from_inner(inner))
881 }
882
883 pub(in crate::db) fn having_group_with_schema(
884 self,
885 field: impl AsRef<str>,
886 schema: &SchemaInfo,
887 op: CompareOp,
888 value: InputValue,
889 ) -> Result<Self, QueryError> {
890 let Self { inner, .. } = self;
891 let inner = inner.having_group_with_schema(field, schema, op, value.into())?;
892
893 Ok(Self::from_inner(inner))
894 }
895
896 pub fn having_aggregate(
898 self,
899 aggregate_index: usize,
900 op: CompareOp,
901 value: InputValue,
902 ) -> Result<Self, QueryError> {
903 let Self { inner, .. } = self;
904 let inner = inner.having_aggregate(aggregate_index, op, value.into())?;
905
906 Ok(Self::from_inner(inner))
907 }
908
909 #[cfg(test)]
913 pub(in crate::db) fn having_expr(self, expr: Expr) -> Result<Self, QueryError> {
914 let Self { inner, .. } = self;
915 let inner = inner.having_expr(expr)?;
916
917 Ok(Self::from_inner(inner))
918 }
919
920 pub(in crate::db) fn by_id(self, id: E::Key) -> Self {
922 let Self { inner, .. } = self;
923
924 Self::from_inner(inner.by_id(id.to_key_value()))
925 }
926
927 pub(in crate::db) fn by_ids<I>(self, ids: I) -> Self
929 where
930 I: IntoIterator<Item = E::Key>,
931 {
932 let Self { inner, .. } = self;
933
934 Self::from_inner(inner.by_ids(ids.into_iter().map(|id| id.to_key_value())))
935 }
936
937 #[must_use]
939 pub fn delete(mut self) -> Self {
940 self.inner = self.inner.delete();
941 self
942 }
943
944 #[must_use]
951 pub fn limit(mut self, limit: u32) -> Self {
952 self.inner = self.inner.limit(limit);
953 self
954 }
955
956 #[must_use]
962 pub fn offset(mut self, offset: u32) -> Self {
963 self.inner = self.inner.offset(offset);
964 self
965 }
966
967 #[must_use]
972 pub fn require_index(mut self) -> Self {
973 self.inner = self.inner.require_index();
974 self
975 }
976
977 #[must_use]
982 pub fn require_index_named(mut self, index_name: impl Into<String>) -> Self {
983 self.inner = self.inner.require_index_named(index_name);
984 self
985 }
986
987 #[must_use]
991 pub fn require_access_path(mut self, path: RequiredAccessPath) -> Self {
992 self.inner = self.inner.require_access_path(path);
993 self
994 }
995
996 #[must_use]
1001 pub fn require_no_residual_filter(mut self) -> Self {
1002 self.inner = self.inner.require_no_residual_filter();
1003 self
1004 }
1005
1006 pub fn explain(&self) -> Result<ExplainPlan, QueryError> {
1008 let mut plan = self.build_plan_for_visibility(None)?;
1009 self.inner
1010 .finalize_access_choice_for_visibility(&mut plan, None);
1011
1012 Ok(plan.explain())
1013 }
1014
1015 pub fn plan_hash_hex(&self) -> Result<String, QueryError> {
1020 let plan = self.inner.build_plan()?;
1021
1022 Ok(plan.fingerprint().to_string())
1023 }
1024
1025 pub fn planned(&self) -> Result<PlannedQuery<E>, QueryError> {
1027 self.map_plan_for_visibility(None, Self::planned_query_from_plan)
1028 }
1029
1030 pub fn plan(&self) -> Result<CompiledQuery<E>, QueryError> {
1034 self.map_plan_for_visibility(None, Self::compiled_query_from_plan)
1035 }
1036
1037 #[cfg(test)]
1038 pub(in crate::db) fn plan_with_visible_indexes(
1039 &self,
1040 visible_indexes: &VisibleIndexes<'_>,
1041 ) -> Result<CompiledQuery<E>, QueryError> {
1042 self.map_plan_for_visibility(Some(visible_indexes), Self::compiled_query_from_plan)
1043 }
1044}
1045
1046impl<E> Query<E>
1047where
1048 E: EntityKind + SingletonEntity,
1049 E::Key: Default,
1050{
1051 pub(in crate::db) fn only(self) -> Self {
1053 let Self { inner, .. } = self;
1054
1055 Self::from_inner(inner.only(E::Key::default().to_key_value()))
1056 }
1057}