1use crate::{
7 db::{
8 cursor::ContinuationSignature,
9 direction::Direction,
10 predicate::{MissingRowPolicy, Predicate},
11 query::{
12 builder::scalar_projection::render_scalar_projection_expr_plan_label,
13 plan::{
14 expr::{Expr, FieldId, normalize_bool_expr},
15 order_contract::DeterministicSecondaryOrderContract,
16 semantics::LogicalPushdownEligibility,
17 },
18 },
19 schema::AcceptedFieldKind,
20 },
21 model::field::FieldKind,
22};
23
24#[derive(Clone, Copy, Debug, Eq, PartialEq)]
33pub enum QueryMode {
34 Load(LoadSpec),
35 Delete(DeleteSpec),
36}
37
38#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
45pub struct LoadSpec {
46 pub(in crate::db) limit: Option<u32>,
47 pub(in crate::db) offset: u32,
48}
49
50impl LoadSpec {
51 #[must_use]
53 pub const fn limit(&self) -> Option<u32> {
54 self.limit
55 }
56
57 #[must_use]
59 pub const fn offset(&self) -> u32 {
60 self.offset
61 }
62}
63
64#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
72pub struct DeleteSpec {
73 pub(in crate::db) limit: Option<u32>,
74 pub(in crate::db) offset: u32,
75}
76
77impl DeleteSpec {
78 #[must_use]
80 pub const fn limit(&self) -> Option<u32> {
81 self.limit
82 }
83
84 #[must_use]
86 pub const fn offset(&self) -> u32 {
87 self.offset
88 }
89}
90
91#[derive(Clone, Copy, Debug, Eq, PartialEq)]
96pub enum OrderDirection {
97 Asc,
98 Desc,
99}
100
101#[derive(Clone, Eq, PartialEq)]
111pub(in crate::db) struct OrderTerm {
112 pub(in crate::db) expr: Expr,
113 pub(in crate::db) direction: OrderDirection,
114}
115
116impl OrderTerm {
117 #[must_use]
119 pub(in crate::db) const fn new(expr: Expr, direction: OrderDirection) -> Self {
120 Self { expr, direction }
121 }
122
123 #[must_use]
125 pub(in crate::db) fn field(field: impl Into<String>, direction: OrderDirection) -> Self {
126 Self::new(Expr::Field(FieldId::new(field.into())), direction)
127 }
128
129 #[must_use]
131 pub(in crate::db) const fn expr(&self) -> &Expr {
132 &self.expr
133 }
134
135 #[must_use]
137 pub(in crate::db) const fn direct_field(&self) -> Option<&str> {
138 let Expr::Field(field) = &self.expr else {
139 return None;
140 };
141
142 Some(field.as_str())
143 }
144
145 #[must_use]
147 pub(in crate::db) fn rendered_label(&self) -> String {
148 render_scalar_projection_expr_plan_label(&self.expr)
149 }
150
151 #[must_use]
153 pub(in crate::db) const fn direction(&self) -> OrderDirection {
154 self.direction
155 }
156}
157
158impl std::fmt::Debug for OrderTerm {
159 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160 f.debug_struct("OrderTerm")
161 .field("label", &self.rendered_label())
162 .field("expr", &self.expr)
163 .field("direction", &self.direction)
164 .finish()
165 }
166}
167
168impl PartialEq<(String, OrderDirection)> for OrderTerm {
169 fn eq(&self, other: &(String, OrderDirection)) -> bool {
170 self.rendered_label() == other.0 && self.direction == other.1
171 }
172}
173
174impl PartialEq<OrderTerm> for (String, OrderDirection) {
175 fn eq(&self, other: &OrderTerm) -> bool {
176 self.0 == other.rendered_label() && self.1 == other.direction
177 }
178}
179
180#[must_use]
183pub(in crate::db) fn render_scalar_filter_expr_plan_label(expr: &Expr) -> String {
184 render_scalar_projection_expr_plan_label(&normalize_bool_expr(expr.clone()))
185}
186
187#[derive(Clone, Debug, Eq, PartialEq)]
194pub(in crate::db) struct OrderSpec {
195 pub(in crate::db) fields: Vec<OrderTerm>,
196}
197
198#[derive(Clone, Copy, Debug, Eq, PartialEq)]
204pub(in crate::db) struct DeleteLimitSpec {
205 pub(in crate::db) limit: Option<u32>,
206 pub(in crate::db) offset: u32,
207}
208
209#[derive(Clone, Copy, Debug, Eq, PartialEq)]
218pub(in crate::db) enum DistinctExecutionStrategy {
219 None,
220 PreOrdered,
221 HashMaterialize,
222}
223
224impl DistinctExecutionStrategy {
225 #[must_use]
227 pub(in crate::db) const fn is_enabled(self) -> bool {
228 !matches!(self, Self::None)
229 }
230}
231
232#[derive(Clone, Debug, Eq, PartialEq)]
241pub(in crate::db) struct PlannerRouteProfile {
242 continuation_policy: ContinuationPolicy,
243 logical_pushdown_eligibility: LogicalPushdownEligibility,
244 secondary_order_contract: Option<DeterministicSecondaryOrderContract>,
245}
246
247impl PlannerRouteProfile {
248 #[must_use]
250 pub(in crate::db) const fn new(
251 continuation_policy: ContinuationPolicy,
252 logical_pushdown_eligibility: LogicalPushdownEligibility,
253 secondary_order_contract: Option<DeterministicSecondaryOrderContract>,
254 ) -> Self {
255 Self {
256 continuation_policy,
257 logical_pushdown_eligibility,
258 secondary_order_contract,
259 }
260 }
261
262 #[must_use]
265 pub(in crate::db) const fn seeded_unfinalized(is_grouped: bool) -> Self {
266 Self {
267 continuation_policy: ContinuationPolicy::new(true, true, !is_grouped),
268 logical_pushdown_eligibility: LogicalPushdownEligibility::new(false, is_grouped, false),
269 secondary_order_contract: None,
270 }
271 }
272
273 #[must_use]
275 pub(in crate::db) const fn continuation_policy(&self) -> &ContinuationPolicy {
276 &self.continuation_policy
277 }
278
279 #[must_use]
281 pub(in crate::db) const fn logical_pushdown_eligibility(&self) -> LogicalPushdownEligibility {
282 self.logical_pushdown_eligibility
283 }
284
285 #[must_use]
287 pub(in crate::db) const fn secondary_order_contract(
288 &self,
289 ) -> Option<&DeterministicSecondaryOrderContract> {
290 self.secondary_order_contract.as_ref()
291 }
292}
293
294#[derive(Clone, Copy, Debug, Eq, PartialEq)]
303pub(in crate::db) struct ContinuationPolicy {
304 requires_anchor: bool,
305 requires_strict_advance: bool,
306 is_grouped_safe: bool,
307}
308
309impl ContinuationPolicy {
310 #[must_use]
312 pub(in crate::db) const fn new(
313 requires_anchor: bool,
314 requires_strict_advance: bool,
315 is_grouped_safe: bool,
316 ) -> Self {
317 Self {
318 requires_anchor,
319 requires_strict_advance,
320 is_grouped_safe,
321 }
322 }
323
324 #[must_use]
326 pub(in crate::db) const fn requires_anchor(self) -> bool {
327 self.requires_anchor
328 }
329
330 #[must_use]
332 pub(in crate::db) const fn requires_strict_advance(self) -> bool {
333 self.requires_strict_advance
334 }
335
336 #[must_use]
338 pub(in crate::db) const fn is_grouped_safe(self) -> bool {
339 self.is_grouped_safe
340 }
341}
342
343#[derive(Clone, Copy, Debug, Eq, PartialEq)]
352pub(in crate::db) struct ExecutionShapeSignature {
353 continuation_signature: ContinuationSignature,
354}
355
356impl ExecutionShapeSignature {
357 #[must_use]
359 pub(in crate::db) const fn new(continuation_signature: ContinuationSignature) -> Self {
360 Self {
361 continuation_signature,
362 }
363 }
364
365 #[must_use]
367 pub(in crate::db) const fn continuation_signature(self) -> ContinuationSignature {
368 self.continuation_signature
369 }
370}
371
372#[derive(Clone, Debug, Eq, PartialEq)]
378pub(in crate::db) struct PageSpec {
379 pub(in crate::db) limit: Option<u32>,
380 pub(in crate::db) offset: u32,
381}
382
383#[derive(Clone, Copy, Debug, Eq, PartialEq)]
393pub enum AggregateKind {
394 Count,
395 Sum,
396 Avg,
397 Exists,
398 Min,
399 Max,
400 First,
401 Last,
402}
403
404#[derive(Clone, Copy, Debug, Eq, PartialEq)]
414pub(in crate::db) enum GlobalDistinctAggregateKind {
415 Count,
416 Sum,
417 Avg,
418}
419
420#[derive(Clone, Copy, Debug, Eq, PartialEq)]
431pub(in crate::db) enum GroupedPlanAggregateFamily {
432 CountRowsOnly,
433 FieldTargetRows,
434 GenericRows,
435}
436
437impl GroupedPlanAggregateFamily {
438 #[must_use]
440 pub(in crate::db) const fn code(self) -> &'static str {
441 match self {
442 Self::CountRowsOnly => "count_rows_only",
443 Self::FieldTargetRows => "field_target_rows",
444 Self::GenericRows => "generic_rows",
445 }
446 }
447}
448
449impl AggregateKind {
450 #[must_use]
452 pub(in crate::db) const fn canonical_label(self) -> &'static str {
453 match self {
454 Self::Count => "COUNT",
455 Self::Sum => "SUM",
456 Self::Avg => "AVG",
457 Self::Exists => "EXISTS",
458 Self::First => "FIRST",
459 Self::Last => "LAST",
460 Self::Min => "MIN",
461 Self::Max => "MAX",
462 }
463 }
464
465 #[must_use]
467 pub(in crate::db) const fn is_count(self) -> bool {
468 matches!(self, Self::Count)
469 }
470
471 #[must_use]
473 pub(in crate::db) const fn is_sum(self) -> bool {
474 matches!(self, Self::Sum | Self::Avg)
475 }
476
477 #[must_use]
479 pub(in crate::db) const fn is_extrema(self) -> bool {
480 matches!(self, Self::Min | Self::Max)
481 }
482
483 #[must_use]
485 pub(in crate::db) const fn supports_field_target(self) -> bool {
486 matches!(
487 self,
488 Self::Count | Self::Sum | Self::Avg | Self::Min | Self::Max
489 )
490 }
491
492 #[must_use]
494 pub(in crate::db) const fn requires_decoded_id(self) -> bool {
495 !matches!(self, Self::Count | Self::Sum | Self::Avg | Self::Exists)
496 }
497
498 #[must_use]
500 pub(in crate::db) const fn supports_grouped_distinct(self) -> bool {
501 matches!(self, Self::Count | Self::Sum | Self::Avg)
502 }
503
504 #[must_use]
507 pub(in crate::db::query) const fn fingerprint_tag(self) -> u8 {
508 match self {
509 Self::Count => 0x01,
510 Self::Sum => 0x02,
511 Self::Exists => 0x03,
512 Self::Min => 0x04,
513 Self::Max => 0x05,
514 Self::First => 0x06,
515 Self::Last => 0x07,
516 Self::Avg => 0x08,
517 }
518 }
519
520 #[must_use]
522 pub(in crate::db) const fn global_distinct_kind(self) -> Option<GlobalDistinctAggregateKind> {
523 match self {
524 Self::Count => Some(GlobalDistinctAggregateKind::Count),
525 Self::Sum => Some(GlobalDistinctAggregateKind::Sum),
526 Self::Avg => Some(GlobalDistinctAggregateKind::Avg),
527 Self::Exists | Self::Min | Self::Max | Self::First | Self::Last => None,
528 }
529 }
530
531 #[must_use]
533 pub(in crate::db) const fn supports_global_distinct_without_group_keys(self) -> bool {
534 self.global_distinct_kind().is_some()
535 }
536
537 #[must_use]
539 pub(in crate::db) const fn grouped_plan_family(
540 self,
541 has_target_field: bool,
542 ) -> GroupedPlanAggregateFamily {
543 if has_target_field && self.supports_field_target() {
544 GroupedPlanAggregateFamily::FieldTargetRows
545 } else {
546 GroupedPlanAggregateFamily::GenericRows
547 }
548 }
549
550 #[must_use]
552 pub(in crate::db) const fn supports_grouped_streaming(
553 self,
554 has_target_field: bool,
555 distinct: bool,
556 ) -> bool {
557 if self.supports_field_target() {
558 return !distinct && (self.is_count() || has_target_field);
559 }
560
561 !has_target_field && (!distinct || self.supports_grouped_distinct())
562 }
563
564 #[must_use]
566 pub(in crate::db) const fn extrema_direction(self) -> Option<Direction> {
567 match self {
568 Self::Min => Some(Direction::Asc),
569 Self::Max => Some(Direction::Desc),
570 Self::Count | Self::Sum | Self::Avg | Self::Exists | Self::First | Self::Last => None,
571 }
572 }
573
574 #[must_use]
576 pub(in crate::db) const fn materialized_fold_direction(self) -> Direction {
577 match self {
578 Self::Min => Direction::Desc,
579 Self::Count
580 | Self::Sum
581 | Self::Avg
582 | Self::Exists
583 | Self::Max
584 | Self::First
585 | Self::Last => Direction::Asc,
586 }
587 }
588
589 #[must_use]
591 pub(in crate::db) const fn supports_bounded_probe_hint(self) -> bool {
592 !self.is_count() && !self.is_sum()
593 }
594
595 #[must_use]
597 pub(in crate::db) fn bounded_probe_fetch_hint(
598 self,
599 direction: Direction,
600 offset: usize,
601 page_limit: Option<usize>,
602 ) -> Option<usize> {
603 match self {
604 Self::Exists | Self::First => Some(offset.saturating_add(1)),
605 Self::Min if direction == Direction::Asc => Some(offset.saturating_add(1)),
606 Self::Max if direction == Direction::Desc => Some(offset.saturating_add(1)),
607 Self::Last => page_limit.map(|limit| offset.saturating_add(limit)),
608 Self::Count | Self::Sum | Self::Avg | Self::Min | Self::Max => None,
609 }
610 }
611
612 #[must_use]
614 pub(in crate::db) const fn explain_projection_mode_label(
615 self,
616 has_projected_field: bool,
617 covering_projection: bool,
618 ) -> &'static str {
619 if has_projected_field {
620 if covering_projection {
621 "field_idx"
622 } else {
623 "field_mat"
624 }
625 } else if matches!(self, Self::Min | Self::Max | Self::First | Self::Last) {
626 "entity_term"
627 } else {
628 "scalar_agg"
629 }
630 }
631
632 #[must_use]
634 pub(in crate::db) const fn supports_covering_existing_rows_terminal(self) -> bool {
635 matches!(self, Self::Count | Self::Exists)
636 }
637}
638
639#[derive(Clone, Debug)]
650pub(in crate::db) struct GroupAggregateSpec {
651 pub(in crate::db) kind: AggregateKind,
652 pub(in crate::db) input_expr: Option<Box<Expr>>,
653 pub(in crate::db) filter_expr: Option<Box<Expr>>,
654 pub(in crate::db) distinct: bool,
655}
656
657impl PartialEq for GroupAggregateSpec {
658 fn eq(&self, other: &Self) -> bool {
659 self.semantic_key() == other.semantic_key()
660 }
661}
662
663impl Eq for GroupAggregateSpec {}
664
665impl GroupedPlanAggregateFamily {
666 #[must_use]
668 pub(in crate::db) fn from_grouped_aggregates(aggregates: &[GroupAggregateSpec]) -> Self {
669 if matches!(aggregates, [aggregate] if aggregate.identity().is_count_rows_only()) {
670 return Self::CountRowsOnly;
671 }
672
673 if aggregates.iter().all(|aggregate| {
674 aggregate
675 .kind()
676 .grouped_plan_family(aggregate.target_field().is_some())
677 == Self::FieldTargetRows
678 }) {
679 return Self::FieldTargetRows;
680 }
681
682 Self::GenericRows
683 }
684}
685
686#[derive(Clone, Debug)]
697pub(in crate::db::query::plan) enum FieldSlotAuthority {
698 Unresolved,
699 ModelOnly(FieldKind),
700 Accepted(AcceptedFieldKind),
701}
702
703#[derive(Clone, Debug)]
704pub(crate) struct FieldSlot {
705 pub(in crate::db) index: usize,
706 pub(in crate::db) field: String,
707 pub(in crate::db::query::plan) authority: FieldSlotAuthority,
708}
709
710impl PartialEq for FieldSlot {
711 fn eq(&self, other: &Self) -> bool {
712 self.index == other.index && self.field == other.field
713 }
714}
715
716impl Eq for FieldSlot {}
717
718#[derive(Clone, Copy, Debug, Eq, PartialEq)]
727pub(in crate::db) struct GroupedExecutionConfig {
728 pub(in crate::db) max_groups: u64,
729 pub(in crate::db) max_group_bytes: u64,
730}
731
732#[derive(Clone, Debug, Eq, PartialEq)]
741pub(in crate::db) struct GroupSpec {
742 pub(in crate::db) group_fields: Vec<FieldSlot>,
743 pub(in crate::db) aggregates: Vec<GroupAggregateSpec>,
744 pub(in crate::db) execution: GroupedExecutionConfig,
745}
746
747#[derive(Clone, Debug, Eq, PartialEq)]
768pub(in crate::db) struct ScalarPlan {
769 pub(in crate::db) mode: QueryMode,
771
772 pub(in crate::db) filter_expr: Option<Expr>,
774
775 pub(in crate::db) predicate_covers_filter_expr: bool,
777
778 pub(in crate::db) predicate: Option<Predicate>,
780
781 pub(in crate::db) order: Option<OrderSpec>,
783
784 pub(in crate::db) distinct: bool,
786
787 pub(in crate::db) delete_limit: Option<DeleteLimitSpec>,
789
790 pub(in crate::db) page: Option<PageSpec>,
792
793 pub(in crate::db) consistency: MissingRowPolicy,
795}
796
797#[derive(Clone, Debug, Eq, PartialEq)]
805pub(in crate::db) struct GroupPlan {
806 pub(in crate::db) scalar: ScalarPlan,
807 pub(in crate::db) group: GroupSpec,
808 pub(in crate::db) having_expr: Option<Expr>,
809}
810
811#[derive(Clone, Debug, Eq, PartialEq)]
821pub(in crate::db) enum LogicalPlan {
822 Scalar(ScalarPlan),
823 Grouped(GroupPlan),
824}