1use crate::{
7 db::{
8 cursor::ContinuationSignature,
9 direction::Direction,
10 predicate::{MissingRowPolicy, Predicate},
11 query::{
12 builder::scalar_projection::render_scalar_projection_expr_sql_label,
13 plan::{
14 expr::{Expr, FieldId, normalize_bool_expr},
15 order_contract::DeterministicSecondaryOrderContract,
16 semantics::LogicalPushdownEligibility,
17 },
18 },
19 },
20 model::field::FieldKind,
21};
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
32pub enum QueryMode {
33 Load(LoadSpec),
34 Delete(DeleteSpec),
35}
36
37#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
44pub struct LoadSpec {
45 pub(crate) limit: Option<u32>,
46 pub(crate) offset: u32,
47}
48
49impl LoadSpec {
50 #[must_use]
52 pub const fn limit(&self) -> Option<u32> {
53 self.limit
54 }
55
56 #[must_use]
58 pub const fn offset(&self) -> u32 {
59 self.offset
60 }
61}
62
63#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
71pub struct DeleteSpec {
72 pub(crate) limit: Option<u32>,
73 pub(crate) offset: u32,
74}
75
76impl DeleteSpec {
77 #[must_use]
79 pub const fn limit(&self) -> Option<u32> {
80 self.limit
81 }
82
83 #[must_use]
85 pub const fn offset(&self) -> u32 {
86 self.offset
87 }
88}
89
90#[derive(Clone, Copy, Debug, Eq, PartialEq)]
95pub enum OrderDirection {
96 Asc,
97 Desc,
98}
99
100#[derive(Clone, Eq, PartialEq)]
110pub(crate) struct OrderTerm {
111 pub(crate) expr: Expr,
112 pub(crate) direction: OrderDirection,
113}
114
115impl OrderTerm {
116 #[must_use]
118 pub(crate) const fn new(expr: Expr, direction: OrderDirection) -> Self {
119 Self { expr, direction }
120 }
121
122 #[must_use]
124 pub(crate) fn field(field: impl Into<String>, direction: OrderDirection) -> Self {
125 Self::new(Expr::Field(FieldId::new(field.into())), direction)
126 }
127
128 #[must_use]
130 pub(crate) const fn expr(&self) -> &Expr {
131 &self.expr
132 }
133
134 #[must_use]
136 pub(crate) const fn direct_field(&self) -> Option<&str> {
137 let Expr::Field(field) = &self.expr else {
138 return None;
139 };
140
141 Some(field.as_str())
142 }
143
144 #[must_use]
146 pub(crate) fn rendered_label(&self) -> String {
147 render_scalar_projection_expr_sql_label(&self.expr)
148 }
149
150 #[must_use]
152 pub(crate) const fn direction(&self) -> OrderDirection {
153 self.direction
154 }
155}
156
157impl std::fmt::Debug for OrderTerm {
158 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159 f.debug_struct("OrderTerm")
160 .field("label", &self.rendered_label())
161 .field("expr", &self.expr)
162 .field("direction", &self.direction)
163 .finish()
164 }
165}
166
167impl PartialEq<(String, OrderDirection)> for OrderTerm {
168 fn eq(&self, other: &(String, OrderDirection)) -> bool {
169 self.rendered_label() == other.0 && self.direction == other.1
170 }
171}
172
173impl PartialEq<OrderTerm> for (String, OrderDirection) {
174 fn eq(&self, other: &OrderTerm) -> bool {
175 self.0 == other.rendered_label() && self.1 == other.direction
176 }
177}
178
179#[must_use]
182pub(in crate::db) fn render_scalar_filter_expr_sql_label(expr: &Expr) -> String {
183 render_scalar_projection_expr_sql_label(&normalize_bool_expr(expr.clone()))
184}
185
186#[derive(Clone, Debug, Eq, PartialEq)]
193pub(crate) struct OrderSpec {
194 pub(crate) fields: Vec<OrderTerm>,
195}
196
197#[derive(Clone, Copy, Debug, Eq, PartialEq)]
203pub(crate) struct DeleteLimitSpec {
204 pub(crate) limit: Option<u32>,
205 pub(crate) offset: u32,
206}
207
208#[derive(Clone, Copy, Debug, Eq, PartialEq)]
217pub(crate) enum DistinctExecutionStrategy {
218 None,
219 PreOrdered,
220 HashMaterialize,
221}
222
223impl DistinctExecutionStrategy {
224 #[must_use]
226 pub(crate) const fn is_enabled(self) -> bool {
227 !matches!(self, Self::None)
228 }
229}
230
231#[derive(Clone, Debug, Eq, PartialEq)]
240pub(in crate::db) struct PlannerRouteProfile {
241 continuation_policy: ContinuationPolicy,
242 logical_pushdown_eligibility: LogicalPushdownEligibility,
243 secondary_order_contract: Option<DeterministicSecondaryOrderContract>,
244}
245
246impl PlannerRouteProfile {
247 #[must_use]
249 pub(in crate::db) const fn new(
250 continuation_policy: ContinuationPolicy,
251 logical_pushdown_eligibility: LogicalPushdownEligibility,
252 secondary_order_contract: Option<DeterministicSecondaryOrderContract>,
253 ) -> Self {
254 Self {
255 continuation_policy,
256 logical_pushdown_eligibility,
257 secondary_order_contract,
258 }
259 }
260
261 #[must_use]
264 pub(in crate::db) const fn seeded_unfinalized(is_grouped: bool) -> Self {
265 Self {
266 continuation_policy: ContinuationPolicy::new(true, true, !is_grouped),
267 logical_pushdown_eligibility: LogicalPushdownEligibility::new(false, is_grouped, false),
268 secondary_order_contract: None,
269 }
270 }
271
272 #[must_use]
274 pub(in crate::db) const fn continuation_policy(&self) -> &ContinuationPolicy {
275 &self.continuation_policy
276 }
277
278 #[must_use]
280 pub(in crate::db) const fn logical_pushdown_eligibility(&self) -> LogicalPushdownEligibility {
281 self.logical_pushdown_eligibility
282 }
283
284 #[must_use]
286 pub(in crate::db) const fn secondary_order_contract(
287 &self,
288 ) -> Option<&DeterministicSecondaryOrderContract> {
289 self.secondary_order_contract.as_ref()
290 }
291}
292
293#[derive(Clone, Copy, Debug, Eq, PartialEq)]
302pub(in crate::db) struct ContinuationPolicy {
303 requires_anchor: bool,
304 requires_strict_advance: bool,
305 is_grouped_safe: bool,
306}
307
308impl ContinuationPolicy {
309 #[must_use]
311 pub(in crate::db) const fn new(
312 requires_anchor: bool,
313 requires_strict_advance: bool,
314 is_grouped_safe: bool,
315 ) -> Self {
316 Self {
317 requires_anchor,
318 requires_strict_advance,
319 is_grouped_safe,
320 }
321 }
322
323 #[must_use]
325 pub(in crate::db) const fn requires_anchor(self) -> bool {
326 self.requires_anchor
327 }
328
329 #[must_use]
331 pub(in crate::db) const fn requires_strict_advance(self) -> bool {
332 self.requires_strict_advance
333 }
334
335 #[must_use]
337 pub(in crate::db) const fn is_grouped_safe(self) -> bool {
338 self.is_grouped_safe
339 }
340}
341
342#[derive(Clone, Copy, Debug, Eq, PartialEq)]
351pub(in crate::db) struct ExecutionShapeSignature {
352 continuation_signature: ContinuationSignature,
353}
354
355impl ExecutionShapeSignature {
356 #[must_use]
358 pub(in crate::db) const fn new(continuation_signature: ContinuationSignature) -> Self {
359 Self {
360 continuation_signature,
361 }
362 }
363
364 #[must_use]
366 pub(in crate::db) const fn continuation_signature(self) -> ContinuationSignature {
367 self.continuation_signature
368 }
369}
370
371#[derive(Clone, Debug, Eq, PartialEq)]
377pub(crate) struct PageSpec {
378 pub(crate) limit: Option<u32>,
379 pub(crate) offset: u32,
380}
381
382#[derive(Clone, Copy, Debug, Eq, PartialEq)]
392pub enum AggregateKind {
393 Count,
394 Sum,
395 Avg,
396 Exists,
397 Min,
398 Max,
399 First,
400 Last,
401}
402
403impl AggregateKind {
404 #[must_use]
406 pub(in crate::db) const fn sql_label(self) -> &'static str {
407 match self {
408 Self::Count => "COUNT",
409 Self::Sum => "SUM",
410 Self::Avg => "AVG",
411 Self::Exists => "EXISTS",
412 Self::First => "FIRST",
413 Self::Last => "LAST",
414 Self::Min => "MIN",
415 Self::Max => "MAX",
416 }
417 }
418
419 #[must_use]
421 pub(crate) const fn is_count(self) -> bool {
422 matches!(self, Self::Count)
423 }
424
425 #[must_use]
427 pub(in crate::db) const fn is_sum(self) -> bool {
428 matches!(self, Self::Sum | Self::Avg)
429 }
430
431 #[must_use]
433 pub(in crate::db) const fn is_extrema(self) -> bool {
434 matches!(self, Self::Min | Self::Max)
435 }
436
437 #[must_use]
439 pub(in crate::db) const fn supports_field_target_v1(self) -> bool {
440 matches!(
441 self,
442 Self::Count | Self::Sum | Self::Avg | Self::Min | Self::Max
443 )
444 }
445
446 #[must_use]
448 pub(in crate::db) const fn requires_decoded_id(self) -> bool {
449 !matches!(self, Self::Count | Self::Sum | Self::Avg | Self::Exists)
450 }
451
452 #[must_use]
454 pub(in crate::db) const fn supports_grouped_distinct_v1(self) -> bool {
455 matches!(
456 self,
457 Self::Count | Self::Min | Self::Max | Self::Sum | Self::Avg
458 )
459 }
460
461 #[must_use]
464 pub(in crate::db) const fn uses_grouped_distinct_value_dedup_v1(self) -> bool {
465 matches!(self, Self::Count | Self::Sum | Self::Avg)
466 }
467
468 #[must_use]
471 pub(in crate::db::query) const fn fingerprint_tag(self) -> u8 {
472 match self {
473 Self::Count => 0x01,
474 Self::Sum => 0x02,
475 Self::Exists => 0x03,
476 Self::Min => 0x04,
477 Self::Max => 0x05,
478 Self::First => 0x06,
479 Self::Last => 0x07,
480 Self::Avg => 0x08,
481 }
482 }
483
484 #[must_use]
486 pub(in crate::db) const fn supports_global_distinct_without_group_keys(self) -> bool {
487 matches!(self, Self::Count | Self::Sum | Self::Avg)
488 }
489
490 #[must_use]
492 pub(crate) const fn extrema_direction(self) -> Option<Direction> {
493 match self {
494 Self::Min => Some(Direction::Asc),
495 Self::Max => Some(Direction::Desc),
496 Self::Count | Self::Sum | Self::Avg | Self::Exists | Self::First | Self::Last => None,
497 }
498 }
499
500 #[must_use]
502 pub(crate) const fn materialized_fold_direction(self) -> Direction {
503 match self {
504 Self::Min => Direction::Desc,
505 Self::Count
506 | Self::Sum
507 | Self::Avg
508 | Self::Exists
509 | Self::Max
510 | Self::First
511 | Self::Last => Direction::Asc,
512 }
513 }
514
515 #[must_use]
517 pub(crate) const fn supports_bounded_probe_hint(self) -> bool {
518 !self.is_count() && !self.is_sum()
519 }
520
521 #[must_use]
523 pub(crate) fn bounded_probe_fetch_hint(
524 self,
525 direction: Direction,
526 offset: usize,
527 page_limit: Option<usize>,
528 ) -> Option<usize> {
529 match self {
530 Self::Exists | Self::First => Some(offset.saturating_add(1)),
531 Self::Min if direction == Direction::Asc => Some(offset.saturating_add(1)),
532 Self::Max if direction == Direction::Desc => Some(offset.saturating_add(1)),
533 Self::Last => page_limit.map(|limit| offset.saturating_add(limit)),
534 Self::Count | Self::Sum | Self::Avg | Self::Min | Self::Max => None,
535 }
536 }
537
538 #[must_use]
540 pub(in crate::db) const fn explain_projection_mode_label(
541 self,
542 has_projected_field: bool,
543 covering_projection: bool,
544 ) -> &'static str {
545 if has_projected_field {
546 if covering_projection {
547 "field_idx"
548 } else {
549 "field_mat"
550 }
551 } else if matches!(self, Self::Min | Self::Max | Self::First | Self::Last) {
552 "entity_term"
553 } else {
554 "scalar_agg"
555 }
556 }
557
558 #[must_use]
560 pub(in crate::db) const fn supports_covering_existing_rows_terminal(self) -> bool {
561 matches!(self, Self::Count | Self::Exists)
562 }
563}
564
565#[derive(Clone, Debug)]
576pub(crate) struct GroupAggregateSpec {
577 pub(crate) kind: AggregateKind,
578 #[cfg(test)]
579 #[cfg(test)]
580 pub(crate) target_field: Option<String>,
581 pub(crate) input_expr: Option<Box<Expr>>,
582 pub(crate) filter_expr: Option<Box<Expr>>,
583 pub(crate) distinct: bool,
584}
585
586impl PartialEq for GroupAggregateSpec {
587 fn eq(&self, other: &Self) -> bool {
588 self.kind == other.kind
589 && self.input_expr == other.input_expr
590 && self.filter_expr == other.filter_expr
591 && self.distinct == other.distinct
592 }
593}
594
595impl Eq for GroupAggregateSpec {}
596
597#[derive(Clone, Debug)]
608pub(crate) struct FieldSlot {
609 pub(crate) index: usize,
610 pub(crate) field: String,
611 pub(crate) kind: Option<FieldKind>,
612}
613
614impl PartialEq for FieldSlot {
615 fn eq(&self, other: &Self) -> bool {
616 self.index == other.index && self.field == other.field
617 }
618}
619
620impl Eq for FieldSlot {}
621
622#[derive(Clone, Copy, Debug, Eq, PartialEq)]
631pub(crate) struct GroupedExecutionConfig {
632 pub(crate) max_groups: u64,
633 pub(crate) max_group_bytes: u64,
634}
635
636#[derive(Clone, Debug, Eq, PartialEq)]
645pub(crate) struct GroupSpec {
646 pub(crate) group_fields: Vec<FieldSlot>,
647 pub(crate) aggregates: Vec<GroupAggregateSpec>,
648 pub(crate) execution: GroupedExecutionConfig,
649}
650
651#[derive(Clone, Debug, Eq, PartialEq)]
672pub(crate) struct ScalarPlan {
673 pub(crate) mode: QueryMode,
675
676 pub(crate) filter_expr: Option<Expr>,
678
679 pub(crate) predicate: Option<Predicate>,
681
682 pub(crate) order: Option<OrderSpec>,
684
685 pub(crate) distinct: bool,
687
688 pub(crate) delete_limit: Option<DeleteLimitSpec>,
690
691 pub(crate) page: Option<PageSpec>,
693
694 pub(crate) consistency: MissingRowPolicy,
696}
697
698#[derive(Clone, Debug, Eq, PartialEq)]
706pub(crate) struct GroupPlan {
707 pub(crate) scalar: ScalarPlan,
708 pub(crate) group: GroupSpec,
709 pub(crate) having_expr: Option<Expr>,
710}
711
712#[derive(Clone, Debug, Eq, PartialEq)]
722pub(crate) enum LogicalPlan {
723 Scalar(ScalarPlan),
724 Grouped(GroupPlan),
725}