icydb-core 0.257.18

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Module: query::plan::group_field
//! Responsibility: canonical direct and scalar record-path grouping-key identity.
//! Does not own: grouped execution strategy or runtime row traversal.
//! Boundary: resolves authored group fields once through accepted schema authority.

use std::{mem, slice};

use crate::db::{
    QueryError,
    query::plan::{
        FieldSlot,
        expr::{Expr, FieldId, FieldPath, PathSpec},
    },
    query::preparation::PreparationWork,
    schema::{AcceptedFieldKind, SchemaInfo, classify_accepted_field_kind},
};
use icydb_diagnostic_code::DiagnosticExecutionBudgetResource as Resource;

/// One group key source used only after a query contains an accepted scalar path.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum GroupField {
    Direct(FieldSlot),
    ScalarPath(ScalarGroupPath),
}

/// One accepted scalar record path compiled to its root row slot.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct ScalarGroupPath {
    label: String,
    path: PathSpec,
    root_slot: usize,
    identity_group_canonical_form: bool,
}

impl ScalarGroupPath {
    /// Borrow the normalized diagnostic label retained for public surfaces.
    #[must_use]
    pub(in crate::db) const fn label(&self) -> &str {
        self.label.as_str()
    }

    /// Borrow the structural path identity.
    #[must_use]
    pub(in crate::db) const fn path(&self) -> &PathSpec {
        &self.path
    }

    /// Return the accepted root row slot used by grouped runtime traversal.
    #[must_use]
    pub(in crate::db) const fn root_slot(&self) -> usize {
        self.root_slot
    }

    /// Return whether the persisted scalar leaf is already group-canonical.
    #[must_use]
    pub(in crate::db) const fn has_identity_group_canonical_form(&self) -> bool {
        self.identity_group_canonical_form
    }
}

impl GroupField {
    #[cfg(test)]
    pub(in crate::db) fn scalar_path_for_test(
        label: impl Into<String>,
        root: impl Into<FieldId>,
        segments: Vec<String>,
        root_slot: usize,
        accepted_kind: AcceptedFieldKind,
    ) -> Self {
        let identity_group_canonical_form =
            classify_accepted_field_kind(&accepted_kind).has_identity_group_canonical_form();
        Self::ScalarPath(ScalarGroupPath {
            label: label.into(),
            path: PathSpec::new(root, segments),
            root_slot,
            identity_group_canonical_form,
        })
    }

    /// Resolve one normalized direct field or scalar record path.
    pub(in crate::db) fn resolve_with_schema(
        schema: &SchemaInfo,
        field: &str,
        work: &PreparationWork<'_>,
    ) -> Result<Option<Self>, QueryError> {
        work.charge(Resource::PredicateExpressionSteps, 1 + field.len() as u64)?;
        let Some((root, nested)) = field.split_once('.') else {
            return Ok(FieldSlot::resolve_with_schema(schema, field).map(Self::Direct));
        };
        let Some(accepted_kind) = Self::accepted_kind_for_label(schema, field) else {
            return Ok(None);
        };
        let Some(root_slot) = schema.field_slot_index(root) else {
            return Ok(None);
        };
        let semantics = classify_accepted_field_kind(accepted_kind);
        // Admit destination backing and each owned string before copying.
        let mut segments = work.vec_with_capacity(nested.split('.').count())?;
        for segment in nested.split('.') {
            segments.push(work.copy_text(segment)?);
        }
        Ok(Some(Self::ScalarPath(ScalarGroupPath {
            label: work.copy_text(field)?,
            path: PathSpec::new(FieldId::new(work.copy_text(root)?), segments),
            root_slot,
            identity_group_canonical_form: semantics.has_identity_group_canonical_form(),
        })))
    }

    /// Check grouping eligibility without retaining a key for validation-only consumers.
    #[must_use]
    pub(in crate::db) fn accepted_kind_for_label<'a>(
        schema: &'a SchemaInfo,
        field: &str,
    ) -> Option<&'a AcceptedFieldKind> {
        Self::accepted_kind_for_components(schema, field.split('.'))
    }

    // Borrow terminal authority without constructing an execution key. Keep
    // nested grouping eligibility shared with retained-key resolution; direct
    // fields deliberately retain their existing, broader resolution contract.
    fn accepted_kind_for_components<'schema, 'path>(
        schema: &'schema SchemaInfo,
        mut components: impl Iterator<Item = &'path str> + Clone,
    ) -> Option<&'schema AcceptedFieldKind> {
        let root = components.next()?;
        if components.clone().next().is_none() {
            return schema.accepted_query_field_kind(root);
        }
        if root.is_empty() || components.clone().any(str::is_empty) {
            return None;
        }
        let accepted_kind = schema.accepted_nested_query_field_kind(root, components)?;
        let semantics = classify_accepted_field_kind(accepted_kind);
        if !semantics.is_scalar()
            || !semantics.is_sql_comparable()
            || !semantics.supports_stable_group_key()
        {
            return None;
        }
        Some(accepted_kind)
    }

    /// Borrow the normalized field/path label.
    #[must_use]
    pub(in crate::db) fn field(&self) -> &str {
        match self {
            Self::Direct(field) => field.field(),
            Self::ScalarPath(path) => path.label(),
        }
    }

    /// Return the root row slot required by grouped execution.
    #[must_use]
    pub(in crate::db) const fn root_slot(&self) -> usize {
        match self {
            Self::Direct(field) => field.index(),
            Self::ScalarPath(path) => path.root_slot(),
        }
    }

    /// Borrow a field expression's current accepted grouping kind without
    /// retaining a key, label or schema-kind copy.
    #[must_use]
    pub(in crate::db) fn accepted_kind_for_expr<'a>(
        schema: &'a SchemaInfo,
        expr: &Expr,
    ) -> Option<&'a AcceptedFieldKind> {
        match expr {
            Expr::Field(field) => {
                Self::accepted_kind_for_components(schema, field.as_str().split('.'))
            }
            Expr::FieldPath(path) => {
                let path = path.path_spec();
                // Preserve dotted-name interpretation without rendering and
                // reparsing a temporary label (including empty components).
                let components = std::iter::once(path.root().as_str())
                    .chain(path.segments().iter().map(String::as_str))
                    .flat_map(|component| component.split('.'));
                Self::accepted_kind_for_components(schema, components)
            }
            _ => None,
        }
    }

    /// Return whether this source carries accepted grouping authority.
    #[must_use]
    pub(in crate::db) const fn is_resolved(&self) -> bool {
        match self {
            Self::Direct(field) => !field.is_unresolved(),
            Self::ScalarPath(_) => true,
        }
    }

    /// Return whether the persisted value is already group-canonical.
    #[must_use]
    pub(in crate::db) fn has_identity_group_canonical_form(&self) -> bool {
        match self {
            Self::Direct(field) => field.accepted_kind().is_some_and(|kind| {
                classify_accepted_field_kind(kind).has_identity_group_canonical_form()
            }),
            Self::ScalarPath(path) => path.has_identity_group_canonical_form(),
        }
    }

    /// Borrow the direct slot when this source stays on the direct fast path.
    #[must_use]
    pub(in crate::db) const fn as_direct(&self) -> Option<&FieldSlot> {
        match self {
            Self::Direct(field) => Some(field),
            Self::ScalarPath(_) => None,
        }
    }

    /// Borrow the scalar path when this source requires path-aware execution.
    #[must_use]
    pub(in crate::db) const fn as_scalar_path(&self) -> Option<&ScalarGroupPath> {
        match self {
            Self::Direct(_) => None,
            Self::ScalarPath(path) => Some(path),
        }
    }

    /// Build the canonical planner expression for grouped projection identity.
    #[must_use]
    pub(in crate::db) fn projection_expr(&self) -> Expr {
        match self {
            Self::Direct(field) => Expr::Field(FieldId::new(field.field())),
            Self::ScalarPath(path) => Expr::FieldPath(FieldPath::new(
                path.path().root().as_str(),
                path.path().segments().to_vec(),
            )),
        }
    }

    /// Return whether one expression leaf has this structural group identity.
    #[must_use]
    pub(in crate::db) fn matches_expr(&self, expr: &Expr) -> bool {
        match (self, expr) {
            (Self::Direct(field), Expr::Field(candidate)) => field.field() == candidate.as_str(),
            (Self::ScalarPath(path), Expr::FieldPath(candidate)) => {
                path.path() == candidate.path_spec()
            }
            _ => false,
        }
    }
}

/// Direct-preserving closed set of declared grouping keys.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum GroupFieldSet {
    Direct(Vec<FieldSlot>),
    PathAware(Vec<GroupField>),
}

impl GroupFieldSet {
    /// Copy already-resolved keys without re-resolving authority or re-running
    /// uniqueness checks. Shared direct slots allocate no label/kind payload.
    pub(in crate::db) fn copy_for_preparation(
        &self,
        work: &PreparationWork<'_>,
    ) -> Result<Self, QueryError> {
        let copy_direct = |field: &FieldSlot| {
            work.charge(Resource::PredicateExpressionSteps, 1)?;
            Ok(field.clone())
        };
        Ok(match self {
            Self::Direct(fields) => Self::Direct(work.copy_slice(fields, copy_direct)?),
            Self::PathAware(fields) => Self::PathAware(work.copy_slice(fields, |field| {
                Ok(match field {
                    GroupField::Direct(field) => GroupField::Direct(copy_direct(field)?),
                    GroupField::ScalarPath(path) => {
                        work.charge(Resource::PredicateExpressionSteps, 1)?;
                        GroupField::ScalarPath(ScalarGroupPath {
                            label: work.copy_text(&path.label)?,
                            path: PathSpec::new(
                                FieldId::new(work.copy_text(path.path.root().as_str())?),
                                work.copy_slice(path.path.segments(), |part| work.copy_text(part))?,
                            ),
                            root_slot: path.root_slot,
                            identity_group_canonical_form: path.identity_group_canonical_form,
                        })
                    }
                })
            })?),
        })
    }

    /// Build the empty direct representation used before any grouping key is added.
    #[must_use]
    pub(in crate::db) const fn empty() -> Self {
        Self::Direct(Vec::new())
    }

    /// Return the declared key count.
    #[must_use]
    pub(in crate::db) const fn len(&self) -> usize {
        match self {
            Self::Direct(fields) => fields.len(),
            Self::PathAware(fields) => fields.len(),
        }
    }

    /// Return whether no grouping key is declared.
    #[must_use]
    pub(in crate::db) const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Borrow the exact direct-only representation when no path was admitted.
    #[must_use]
    pub(in crate::db) const fn as_direct(&self) -> Option<&[FieldSlot]> {
        match self {
            Self::Direct(fields) => Some(fields.as_slice()),
            Self::PathAware(_) => None,
        }
    }

    /// Borrow the path-aware representation selected once per grouped query.
    #[must_use]
    pub(in crate::db) const fn as_path_aware(&self) -> Option<&[GroupField]> {
        match self {
            Self::Direct(_) => None,
            Self::PathAware(fields) => Some(fields.as_slice()),
        }
    }

    /// Iterate all grouping keys without constructing a parallel field list.
    pub(in crate::db) fn iter(&self) -> GroupFieldIter<'_> {
        match self {
            Self::Direct(fields) => GroupFieldIter::Direct(fields.iter()),
            Self::PathAware(fields) => GroupFieldIter::PathAware(fields.iter()),
        }
    }

    /// Borrow one declared grouping key by declaration offset.
    #[must_use]
    pub(in crate::db) fn get(&self, index: usize) -> Option<GroupFieldRef<'_>> {
        match self {
            Self::Direct(fields) => fields.get(index).map(GroupFieldRef::Direct),
            Self::PathAware(fields) => fields.get(index).map(GroupFieldRef::PathAware),
        }
    }

    /// Append one key, promoting to the path-aware representation only when needed.
    pub(in crate::db) fn push(
        &mut self,
        field: GroupField,
        work: &PreparationWork<'_>,
    ) -> Result<(), QueryError> {
        for existing in self.iter() {
            // Direct identities compare slots only. Path identities may inspect
            // all component bytes; reserve that work before the shared comparator.
            let bytes = if existing.as_scalar_path().is_some() && field.as_scalar_path().is_some() {
                existing.field().len().saturating_add(field.field().len()) as u64
            } else {
                0
            };
            work.charge(Resource::PredicateExpressionSteps, 1 + bytes)?;
            if existing.same_identity(GroupFieldRef::PathAware(&field)) {
                return Ok(());
            }
        }
        match self {
            Self::Direct(fields) => match field {
                GroupField::Direct(direct) => {
                    work.reserve_vec(fields, 1)?;
                    fields.push(direct);
                }
                path @ GroupField::ScalarPath(_) => {
                    work.charge(Resource::PredicateExpressionSteps, fields.len() as u64)?;
                    let mut promoted = work.vec_with_capacity(fields.len() + 1)?;
                    promoted.extend(mem::take(fields).into_iter().map(GroupField::Direct));
                    promoted.push(path);
                    *self = Self::PathAware(promoted);
                }
            },
            Self::PathAware(fields) => {
                work.reserve_vec(fields, 1)?;
                fields.push(field);
            }
        }
        Ok(())
    }

    /// Match a declared key, admitting candidate comparisons before inspection.
    pub(in crate::db) fn try_contains_expr<E>(
        &self,
        expr: &Expr,
        observe: &mut impl FnMut(u64) -> Result<(), E>,
    ) -> Result<bool, E> {
        for field in self.iter() {
            if field.try_matches_expr(expr, observe)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    /// Admit visits and field comparisons, stopping at the first missing key.
    /// Aggregate inputs and filters remain outside post-group membership checks.
    pub(in crate::db) fn try_contains_all_expr_references<E>(
        &self,
        expr: &Expr,
        observe: &mut impl FnMut(u64) -> Result<(), E>,
    ) -> Result<bool, E> {
        expr.try_all_tree_expr(&mut |node| {
            observe(1)?;
            Ok(match node {
                Expr::Field(_) | Expr::FieldPath(_) => self.try_contains_expr(node, observe)?,
                Expr::Aggregate(_)
                | Expr::Literal(_)
                | Expr::FunctionCall { .. }
                | Expr::Unary { .. }
                | Expr::Binary { .. }
                | Expr::Case { .. } => true,
                #[cfg(test)]
                Expr::Alias { .. } => true,
            })
        })
    }

    /// Rebind every authored label through the selected accepted schema snapshot.
    pub(in crate::db) fn resolve_with_schema(
        &self,
        schema: &SchemaInfo,
        work: &PreparationWork<'_>,
    ) -> Result<Option<Self>, QueryError> {
        let mut resolved = Self::default();
        for field in self.iter() {
            let Some(key) = GroupField::resolve_with_schema(schema, field.field(), work)? else {
                return Ok(None);
            };
            resolved.push(key, work)?;
        }
        Ok(Some(resolved))
    }
}

impl Default for GroupFieldSet {
    fn default() -> Self {
        Self::Direct(Vec::new())
    }
}

/// Borrowed grouping-key view shared by semantic-only consumers.
#[derive(Clone, Copy)]
pub(in crate::db) enum GroupFieldRef<'a> {
    Direct(&'a FieldSlot),
    PathAware(&'a GroupField),
}

impl<'a> GroupFieldRef<'a> {
    /// Borrow the normalized field/path label.
    #[must_use]
    pub(in crate::db) fn field(&self) -> &'a str {
        match self {
            Self::Direct(field) => field.field(),
            Self::PathAware(field) => field.field(),
        }
    }

    /// Return the root row slot required by execution.
    #[must_use]
    pub(in crate::db) const fn root_slot(&self) -> usize {
        match self {
            Self::Direct(field) => field.index(),
            Self::PathAware(field) => field.root_slot(),
        }
    }

    /// Borrow the direct slot when this source stays on the direct fast path.
    #[must_use]
    pub(in crate::db) const fn as_direct(&self) -> Option<&'a FieldSlot> {
        match self {
            Self::Direct(field) => Some(field),
            Self::PathAware(field) => field.as_direct(),
        }
    }

    /// Borrow the scalar path when this source requires path-aware execution.
    #[must_use]
    pub(in crate::db) const fn as_scalar_path(&self) -> Option<&'a ScalarGroupPath> {
        match self {
            Self::Direct(_) => None,
            Self::PathAware(field) => field.as_scalar_path(),
        }
    }

    /// Return whether this source carries accepted grouping authority.
    #[must_use]
    pub(in crate::db) const fn is_resolved(&self) -> bool {
        match self {
            Self::Direct(field) => !field.is_unresolved(),
            Self::PathAware(field) => field.is_resolved(),
        }
    }

    /// Return whether this source matches one planner expression leaf.
    #[must_use]
    pub(in crate::db) fn matches_expr(&self, expr: &Expr) -> bool {
        match self {
            Self::Direct(field) => {
                matches!(expr, Expr::Field(candidate) if candidate.as_str() == field.field())
            }
            Self::PathAware(field) => field.matches_expr(expr),
        }
    }

    /// Admit borrowed leaf comparison under the caller's work observer.
    /// The retained canonical label covers root/segment bytes and separators,
    /// so its length bounds equal-prefix work without another path traversal.
    /// Representation or length mismatches may consume less than this bound.
    pub(in crate::db) fn try_matches_expr<E>(
        &self,
        expr: &Expr,
        observe: &mut impl FnMut(u64) -> Result<(), E>,
    ) -> Result<bool, E> {
        observe(1_u64.saturating_add(self.field().len() as u64))?;
        Ok(self.matches_expr(expr))
    }

    /// Check current accepted key eligibility and identity without constructing
    /// a replacement key. This does not rebind retained type metadata or grant
    /// execution authority; the planner's existing rebinding still owns that.
    #[must_use]
    pub(in crate::db) fn matches_schema_identity(&self, schema: &SchemaInfo) -> bool {
        if let Some(field) = self.as_direct() {
            // A dotted label resolves as a path, never as a direct key.
            return !field.field().contains('.')
                && schema.field_slot_index(field.field()) == Some(field.index())
                && schema.accepted_query_field_kind(field.field()).is_some();
        }
        let Some(path) = self.as_scalar_path() else {
            return false;
        };
        let Some((root, nested)) = path.label().split_once('.') else {
            return false;
        };
        GroupField::accepted_kind_for_components(schema, path.label().split('.')).is_some()
            && schema.field_slot_index(root) == Some(path.root_slot())
            && path.path().root().as_str() == root
            && nested
                .split('.')
                .eq(path.path().segments().iter().map(String::as_str))
    }

    /// Compare borrowed key identities independently of their container representation.
    #[must_use]
    pub(in crate::db) fn same_identity(&self, other: GroupFieldRef<'_>) -> bool {
        if let Some(field) = self.as_direct() {
            return other
                .as_direct()
                .is_some_and(|other| field.index() == other.index());
        }
        match (self.as_scalar_path(), other.as_scalar_path()) {
            (Some(left), Some(right)) => left.path() == right.path(),
            _ => false,
        }
    }

    /// Build the canonical projection expression for this key.
    #[must_use]
    pub(in crate::db) fn projection_expr(&self) -> Expr {
        match self {
            Self::Direct(field) => Expr::Field(FieldId::new(field.field())),
            Self::PathAware(field) => field.projection_expr(),
        }
    }
}

/// Concrete iterator over direct or path-aware key storage.
pub(in crate::db) enum GroupFieldIter<'a> {
    Direct(slice::Iter<'a, FieldSlot>),
    PathAware(slice::Iter<'a, GroupField>),
}

impl<'a> Iterator for GroupFieldIter<'a> {
    type Item = GroupFieldRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Direct(fields) => fields.next().map(GroupFieldRef::Direct),
            Self::PathAware(fields) => fields.next().map(GroupFieldRef::PathAware),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::Direct(fields) => fields.size_hint(),
            Self::PathAware(fields) => fields.size_hint(),
        }
    }
}

impl ExactSizeIterator for GroupFieldIter<'_> {}

#[cfg(test)]
mod tests {
    use crate::db::{
        query::plan::{FieldSlot, GroupField, GroupFieldSet},
        schema::AcceptedFieldKind,
    };

    fn rank_path() -> GroupField {
        GroupField::scalar_path_for_test(
            "profile.rank",
            "profile",
            vec!["rank".to_string()],
            1,
            AcceptedFieldKind::Int32,
        )
    }

    #[test]
    fn direct_keys_keep_the_direct_representation() {
        crate::db::query::preparation::with_preparation_work(|work| {
            let mut fields = GroupFieldSet::empty();
            fields
                .push(
                    GroupField::Direct(FieldSlot::from_test_accepted_kind(
                        0,
                        "direct_rank",
                        AcceptedFieldKind::Int32,
                    )),
                    work,
                )
                .unwrap();

            assert_eq!(fields.as_direct().map(<[FieldSlot]>::len), Some(1));
            assert!(fields.as_path_aware().is_none());
        });
    }

    #[test]
    fn first_path_promotes_the_whole_tuple_once_and_preserves_order() {
        crate::db::query::preparation::with_preparation_work(|work| {
            let mut fields = GroupFieldSet::empty();
            fields
                .push(
                    GroupField::Direct(FieldSlot::from_test_accepted_kind(
                        0,
                        "direct_rank",
                        AcceptedFieldKind::Int32,
                    )),
                    work,
                )
                .unwrap();
            fields.push(rank_path(), work).unwrap();
            fields.push(rank_path(), work).unwrap();

            assert!(fields.as_direct().is_none());
            assert_eq!(
                fields.iter().map(|field| field.field()).collect::<Vec<_>>(),
                vec!["direct_rank", "profile.rank"],
            );
        });
    }

    #[test]
    fn preallocated_path_tuple_matches_incremental_identity_without_promotion() {
        crate::db::query::preparation::with_preparation_work(|work| {
            let direct = GroupField::Direct(FieldSlot::from_test_accepted_kind(
                0,
                "direct_rank",
                AcceptedFieldKind::Int32,
            ));
            let mut expected = GroupFieldSet::empty();
            let mut reserved = GroupFieldSet::PathAware(Vec::with_capacity(4));
            for key in [direct.clone(), rank_path(), direct, rank_path()] {
                expected.push(key.clone(), work).unwrap();
                reserved.push(key, work).unwrap();
            }
            assert_eq!(reserved, expected);
            let GroupFieldSet::PathAware(fields) = reserved else {
                panic!("path-aware group tuple");
            };
            assert_eq!(fields.capacity(), 4);
            assert_eq!(fields.len(), 2);
        });
    }

    #[test]
    fn rejected_group_key_insertion_preserves_the_existing_tuple() {
        use crate::db::{
            RequestExecutionRoot,
            executor::budget::{HardExecutionBudget, HardExecutionFailureHeadroom},
            query::preparation::PreparationWork,
        };
        use icydb_diagnostic_code::{
            DiagnosticExecutionBudgetResource as Resource, DiagnosticExecutionLane,
            DiagnosticFactTag,
        };

        let direct = FieldSlot::from_test_accepted_kind(0, "rank", AcceptedFieldKind::Int32);
        for (resource, incoming) in [
            (Resource::TemporaryBytes, rank_path()),
            (
                Resource::PredicateExpressionSteps,
                GroupField::Direct(direct.clone()),
            ),
        ] {
            let mut fields = GroupFieldSet::Direct(vec![direct.clone()]);
            let before = fields.clone();
            let root = RequestExecutionRoot::new_for_tests(
                HardExecutionBudget::uniform_for_tests(
                    16_000_000,
                    HardExecutionFailureHeadroom::new(500_000_000, 64 * 1024),
                )
                .with_limit_for_tests(resource, 0),
            );
            for _ in 0..2 {
                let error = PreparationWork::run(
                    &root.scope(),
                    DiagnosticExecutionLane::PublicRead,
                    |work| fields.push(incoming.clone(), work),
                )
                .unwrap_err();
                assert!(
                    error
                        .diagnostic_facts()
                        .contains(&(DiagnosticFactTag::BudgetResource, resource.raw()))
                );
                assert_eq!(fields, before);
            }
            crate::db::query::preparation::with_preparation_work(|work| {
                fields.push(incoming, work)
            })
            .unwrap();
        }
    }
}

// Exhaustive cache-retention coverage; new owned fields require accounting.
crate::retained::retained_fields!(GroupField {
Self::Direct(field_0) => [field_0],
Self::ScalarPath(field_0) => [field_0],
});
crate::retained::retained_fields!(GroupFieldSet {
Self::Direct(field_0) => [field_0],
Self::PathAware(field_0) => [field_0],
});
crate::retained::retained_fields!(ScalarGroupPath {
Self{label,path,root_slot,identity_group_canonical_form} => [label,path,root_slot,identity_group_canonical_form],
});