radixdb-storage 1.1.0

Storage contracts and physical persistence engine for RadixDB
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
//! Bounded cold and mixed-source aggregate execution.

use super::*;

impl SegmentedTable {
    pub(super) fn add_cold_projection_column(
        columns: &mut Vec<usize>,
        positions: &mut [Option<usize>],
        col_idx: usize,
    ) {
        if col_idx < positions.len() && positions[col_idx].is_none() {
            positions[col_idx] = Some(columns.len());
            columns.push(col_idx);
        }
    }

    pub(super) fn build_cold_aggregate_projection(
        schema_len: usize,
        aggregates: &[(AggregateOp, usize)],
        extra_columns: &[usize],
    ) -> Option<ColdAggregateProjection> {
        let mut columns = Vec::new();
        let mut positions = vec![None; schema_len];

        for &col_idx in extra_columns {
            if col_idx >= schema_len {
                return None;
            }
            Self::add_cold_projection_column(&mut columns, &mut positions, col_idx);
        }

        for &(op, col_idx) in aggregates {
            if op == AggregateOp::CountStar {
                continue;
            }
            if col_idx >= schema_len {
                return None;
            }
            Self::add_cold_projection_column(&mut columns, &mut positions, col_idx);
        }

        let mut mapped_aggregates = Vec::with_capacity(aggregates.len());
        for &(op, col_idx) in aggregates {
            let mapped_col = if op == AggregateOp::CountStar {
                0
            } else {
                positions.get(col_idx).and_then(|pos| *pos)?
            };
            mapped_aggregates.push((op, mapped_col));
        }

        Some(ColdAggregateProjection {
            columns,
            positions,
            aggregates: mapped_aggregates,
        })
    }

    /// Build the physical contract for the DATA artifact grouped-aggregate path.
    ///
    /// Mapping identity is a correctness boundary, not an optimisation hint:
    /// defaults, renamed columns, and ALTER TABLE mappings remain on the
    /// generic scanner until the columnar operator supports them explicitly.
    pub(super) fn build_artifact_columnar_group_plan(
        schema: &Schema,
        mapping: &crate::volume::writer::ColumnMapping,
        group_by_indices: &[usize],
        aggregates: &[(AggregateOp, usize)],
    ) -> std::result::Result<
        ArtifactColumnarGroupPlan,
        crate::instrumentation::ArtifactColumnarGroupFallback,
    > {
        use crate::instrumentation::ArtifactColumnarGroupFallback;

        if group_by_indices.len() != 1 {
            return Err(ArtifactColumnarGroupFallback::GroupKey);
        }
        if mapping.sources.len() != schema.columns.len() {
            return Err(ArtifactColumnarGroupFallback::Schema);
        }
        if !mapping.is_identity {
            return Err(ArtifactColumnarGroupFallback::Schema);
        }

        // `is_identity` is cached metadata. Validate its observable contract
        // before using logical indices as physical block IDs.
        for (logical_idx, source) in mapping.sources.iter().enumerate() {
            if !matches!(source, ColSource::Volume(physical_idx) if *physical_idx == logical_idx) {
                return Err(ArtifactColumnarGroupFallback::Schema);
            }
        }

        let group_idx = group_by_indices[0];
        let group_data_type = schema
            .columns
            .get(group_idx)
            .ok_or(ArtifactColumnarGroupFallback::GroupKey)?
            .data_type;
        if !matches!(group_data_type, DataType::Integer | DataType::Timestamp) {
            return Err(ArtifactColumnarGroupFallback::GroupKey);
        }

        let mut physical_projection = vec![group_idx];
        let mut resolved_aggregates = Vec::with_capacity(aggregates.len());
        for &(operation, logical_col) in aggregates {
            let data_type = if operation == AggregateOp::CountStar {
                DataType::Null
            } else {
                schema
                    .columns
                    .get(logical_col)
                    .ok_or(ArtifactColumnarGroupFallback::Aggregate)?
                    .data_type
            };
            let projection_pos = if operation == AggregateOp::CountStar {
                None
            } else {
                let physical_idx = match mapping
                    .sources
                    .get(logical_col)
                    .ok_or(ArtifactColumnarGroupFallback::Aggregate)?
                {
                    ColSource::Volume(physical_idx) => *physical_idx,
                    ColSource::Default(_) => return Err(ArtifactColumnarGroupFallback::Schema),
                };
                let pos = physical_projection
                    .iter()
                    .position(|&idx| idx == physical_idx)
                    .unwrap_or_else(|| {
                        physical_projection.push(physical_idx);
                        physical_projection.len() - 1
                    });
                Some(pos)
            };

            // COUNT only inspects the null bitmap.  Numeric aggregate work is
            // intentionally restricted to native DATA representations; string
            // and extension ordering stay with the semantic scanner.
            if matches!(
                operation,
                AggregateOp::Sum | AggregateOp::Avg | AggregateOp::Min | AggregateOp::Max
            ) && !matches!(
                data_type,
                DataType::Integer | DataType::Timestamp | DataType::Float
            ) {
                return Err(ArtifactColumnarGroupFallback::Aggregate);
            }

            resolved_aggregates.push(ArtifactColumnarGroupAggregate {
                operation,
                data_type,
                projection_pos,
            });
        }

        Ok(ArtifactColumnarGroupPlan {
            group_data_type,
            group_projection_pos: 0,
            physical_projection,
            aggregates: resolved_aggregates,
        })
    }

    /// Apply one DATA artifact batch to a grouped accumulator map.
    ///
    /// A false result means the on-disk column representation disagreed with
    /// the declared plan. The caller discards partial state and uses the
    /// scanner, preserving correctness instead of guessing.
    pub(super) fn accumulate_artifact_columnar_group_batch(
        plan: &ArtifactColumnarGroupPlan,
        batch: &ArtifactColumnBatch,
        groups: &mut ArtifactColumnarGroupState,
    ) -> bool {
        let row_count = batch.row_range().len();
        let mut columns = Vec::with_capacity(plan.physical_projection.len());
        for physical_idx in &plan.physical_projection {
            let Some((_, column)) = batch
                .columns()
                .iter()
                .find(|(batch_idx, _)| batch_idx == physical_idx)
            else {
                return false;
            };
            if column.len() != row_count {
                return false;
            }
            columns.push(column);
        }

        let Some(group_column) = columns.get(plan.group_projection_pos).copied() else {
            return false;
        };
        let (group_values, group_nulls) = match (plan.group_data_type, group_column) {
            (DataType::Integer, ColumnData::Int64 { values, nulls })
            | (DataType::Timestamp, ColumnData::TimestampNanos { values, nulls }) => {
                (values.as_slice(), nulls.as_slice())
            }
            _ => return false,
        };

        for row_idx in 0..row_count {
            let group_key = (!group_nulls[row_idx]).then_some(group_values[row_idx]);
            let Some(accums) = groups.accums_for_key(group_key, plan.aggregates.len()) else {
                return false;
            };

            for (accum, aggregate) in accums.iter_mut().zip(&plan.aggregates) {
                let column = aggregate
                    .projection_pos
                    .and_then(|projection_pos| columns.get(projection_pos).copied());
                match aggregate.operation {
                    AggregateOp::CountStar => accum.count += 1,
                    AggregateOp::Count => {
                        let Some(column) = column else {
                            return false;
                        };
                        if column.data_type() != aggregate.data_type {
                            return false;
                        }
                        if !column.is_null(row_idx) {
                            accum.count += 1;
                        }
                    }
                    AggregateOp::Sum | AggregateOp::Avg | AggregateOp::Min | AggregateOp::Max => {
                        let Some(column) = column else {
                            return false;
                        };
                        match (aggregate.data_type, column) {
                            (DataType::Integer, ColumnData::Int64 { values, nulls })
                            | (DataType::Timestamp, ColumnData::TimestampNanos { values, nulls }) =>
                            {
                                if nulls[row_idx] {
                                    continue;
                                }
                                let value = values[row_idx];
                                match aggregate.operation {
                                    AggregateOp::Sum | AggregateOp::Avg => {
                                        accum.int_sum += value as i128;
                                        accum.count += 1;
                                    }
                                    AggregateOp::Min => {
                                        accum.min_i64 = Some(
                                            accum
                                                .min_i64
                                                .map_or(value, |current| current.min(value)),
                                        );
                                    }
                                    AggregateOp::Max => {
                                        accum.max_i64 = Some(
                                            accum
                                                .max_i64
                                                .map_or(value, |current| current.max(value)),
                                        );
                                    }
                                    _ => unreachable!("numeric aggregate operation was filtered"),
                                }
                            }
                            (DataType::Float, ColumnData::Float64 { values, nulls }) => {
                                if nulls[row_idx] || values[row_idx].is_nan() {
                                    continue;
                                }
                                let value = values[row_idx];
                                match aggregate.operation {
                                    AggregateOp::Sum | AggregateOp::Avg => {
                                        accum.float_sum += value;
                                        accum.count += 1;
                                    }
                                    AggregateOp::Min => {
                                        accum.min_f64 = Some(
                                            accum
                                                .min_f64
                                                .map_or(value, |current| current.min(value)),
                                        );
                                    }
                                    AggregateOp::Max => {
                                        accum.max_f64 = Some(
                                            accum
                                                .max_f64
                                                .map_or(value, |current| current.max(value)),
                                        );
                                    }
                                    _ => unreachable!("numeric aggregate operation was filtered"),
                                }
                            }
                            _ => return false,
                        }
                    }
                }
            }
        }

        true
    }

    pub(super) fn finalize_artifact_columnar_group_accums(
        plan: &ArtifactColumnarGroupPlan,
        accums: &[ArtifactColumnarAccum],
    ) -> Option<Vec<Value>> {
        plan.aggregates
            .iter()
            .zip(accums)
            .map(|(aggregate, accum)| {
                Some(match aggregate.operation {
                    AggregateOp::Count | AggregateOp::CountStar => Value::Integer(accum.count),
                    AggregateOp::Sum => {
                        if accum.count == 0 {
                            Value::Null(DataType::Float)
                        } else if aggregate.data_type == DataType::Integer {
                            exact_integer_sum_value(accum.int_sum)?
                        } else {
                            Value::Float(accum.int_sum as f64 + accum.float_sum)
                        }
                    }
                    AggregateOp::Avg => {
                        if accum.count == 0 {
                            Value::Null(DataType::Float)
                        } else {
                            Value::Float(
                                (accum.int_sum as f64 + accum.float_sum) / accum.count as f64,
                            )
                        }
                    }
                    AggregateOp::Min => match (accum.min_i64, accum.min_f64, aggregate.data_type) {
                        (Some(value), None, DataType::Timestamp) => {
                            chrono::DateTime::from_timestamp(
                                value.div_euclid(1_000_000_000),
                                value.rem_euclid(1_000_000_000) as u32,
                            )
                            .map(Value::Timestamp)
                            .unwrap_or(Value::Null(DataType::Timestamp))
                        }
                        (Some(value), None, _) => Value::Integer(value),
                        (None, Some(value), _) => Value::Float(value),
                        (None, None, data_type) => Value::Null(data_type),
                        // A physical column has one type, so this is unreachable
                        // for a valid artifact-backed plan. Retain the scanner's deterministic
                        // comparison if a future mixed representation reaches it.
                        (Some(integer), Some(float), _) if (integer as f64) <= float => {
                            Value::Integer(integer)
                        }
                        (Some(_), Some(float), _) => Value::Float(float),
                    },
                    AggregateOp::Max => match (accum.max_i64, accum.max_f64, aggregate.data_type) {
                        (Some(value), None, DataType::Timestamp) => {
                            chrono::DateTime::from_timestamp(
                                value.div_euclid(1_000_000_000),
                                value.rem_euclid(1_000_000_000) as u32,
                            )
                            .map(Value::Timestamp)
                            .unwrap_or(Value::Null(DataType::Timestamp))
                        }
                        (Some(value), None, _) => Value::Integer(value),
                        (None, Some(value), _) => Value::Float(value),
                        (None, None, data_type) => Value::Null(data_type),
                        (Some(integer), Some(float), _) if (integer as f64) >= float => {
                            Value::Integer(integer)
                        }
                        (Some(_), Some(float), _) => Value::Float(float),
                    },
                })
            })
            .collect()
    }

    pub(super) fn artifact_columnar_group_zone_map(
        segment: &ColdSegment,
        physical_col_idx: usize,
        row_group_idx: usize,
    ) -> Option<&ZoneMap> {
        if segment.volume.meta.row_groups.is_empty() {
            return (row_group_idx == 0)
                .then(|| segment.volume.meta.zone_maps.get(physical_col_idx))
                .flatten();
        }
        segment
            .volume
            .meta
            .row_groups
            .get(row_group_idx)?
            .zone_maps
            .get(physical_col_idx)
    }

    pub(super) fn artifact_columnar_group_bound_value(
        data_type: DataType,
        value: &Value,
    ) -> Option<i64> {
        match (data_type, value) {
            (DataType::Integer, Value::Integer(value)) => Some(*value),
            (DataType::Timestamp, Value::Timestamp(value)) => value.timestamp_nanos_opt(),
            // Some internal paths normalize timestamp predicates to i64 nanos.
            // Accept that representation if a future metadata writer exposes it
            // directly, but keep all other shapes on the hash-map path.
            (DataType::Timestamp, Value::Integer(value)) => Some(*value),
            _ => None,
        }
    }

    pub(super) fn artifact_columnar_group_direct_bounds(
        plan: &ArtifactColumnarGroupPlan,
        volumes: &[(u64, ColdSegment)],
    ) -> Option<(i64, i64)> {
        let group_physical_idx = *plan.physical_projection.get(plan.group_projection_pos)?;
        let mut min_key: Option<i64> = None;
        let mut max_key: Option<i64> = None;

        for (_, segment) in volumes {
            let source = segment.volume.artifact_source()?;
            for row_group_idx in 0..source.row_group_count() {
                let zone = Self::artifact_columnar_group_zone_map(
                    segment,
                    group_physical_idx,
                    row_group_idx,
                )?;
                if zone.null_count == zone.row_count {
                    continue;
                }
                let group_min =
                    Self::artifact_columnar_group_bound_value(plan.group_data_type, &zone.min)?;
                let group_max =
                    Self::artifact_columnar_group_bound_value(plan.group_data_type, &zone.max)?;
                min_key = Some(min_key.map_or(group_min, |current| current.min(group_min)));
                max_key = Some(max_key.map_or(group_max, |current| current.max(group_max)));
            }
        }

        let min_key = min_key?;
        let max_key = max_key?;
        if max_key < min_key {
            return None;
        }
        Some((min_key, max_key))
    }

    pub(super) fn accumulate_artifact_columnar_group_segment(
        plan: &ArtifactColumnarGroupPlan,
        segment: &ColdSegment,
        direct_bounds: Option<(i64, i64)>,
    ) -> std::result::Result<
        ArtifactColumnarGroupSegmentResult,
        crate::instrumentation::ArtifactColumnarGroupFallback,
    > {
        use crate::instrumentation::ArtifactColumnarGroupFallback;

        let Some(source) = segment.volume.artifact_source() else {
            return Err(ArtifactColumnarGroupFallback::Storage);
        };
        let mut groups = ArtifactColumnarGroupState::new(direct_bounds);
        let mut observed_row_groups = 0u64;
        let mut observed_selected_blocks = 0u64;
        let mut observed_input_rows = 0u64;

        for group_idx in 0..source.row_group_count() {
            let batch = source
                .read_columns(group_idx, &plan.physical_projection)
                .map_err(|_| ArtifactColumnarGroupFallback::Storage)?;
            debug_assert_eq!(batch.row_group_index(), group_idx);
            if !Self::accumulate_artifact_columnar_group_batch(plan, &batch, &mut groups) {
                return Err(ArtifactColumnarGroupFallback::ColumnShape);
            }
            observed_row_groups = observed_row_groups.saturating_add(1);
            observed_selected_blocks =
                observed_selected_blocks.saturating_add(plan.physical_projection.len() as u64);
            observed_input_rows =
                observed_input_rows.saturating_add(batch.row_range().len() as u64);
        }

        Ok(ArtifactColumnarGroupSegmentResult {
            groups,
            row_groups: observed_row_groups,
            selected_blocks: observed_selected_blocks,
            input_rows: observed_input_rows,
        })
    }

    pub(super) fn finalize_artifact_columnar_group_state(
        plan: &ArtifactColumnarGroupPlan,
        groups: ArtifactColumnarGroupState,
    ) -> Option<Vec<GroupedAggregateResult>> {
        groups
            .into_key_accums()
            .into_iter()
            .map(|(group_key, accums)| {
                let group_value = match (group_key, plan.group_data_type) {
                    (Some(value), DataType::Timestamp) => chrono::DateTime::from_timestamp(
                        value.div_euclid(1_000_000_000),
                        value.rem_euclid(1_000_000_000) as u32,
                    )
                    .map(Value::Timestamp)
                    .unwrap_or(Value::Null(DataType::Timestamp)),
                    (Some(value), _) => Value::Integer(value),
                    (None, data_type) => Value::Null(data_type),
                };
                Some(GroupedAggregateResult {
                    group_values: vec![group_value],
                    aggregate_values: Self::finalize_artifact_columnar_group_accums(plan, &accums)?,
                })
            })
            .collect()
    }

    /// Aggregate a stable DATA-artifact cold set without creating a
    /// row adapter. This is intentionally narrower than the scanner path:
    /// in-flight DML, tombstones, schema mapping and multi-key GROUP BY stay
    /// on the established generic implementation.
    pub(super) fn compute_grouped_aggregates_artifact_columnar(
        &self,
        group_by_indices: &[usize],
        aggregates: &[(AggregateOp, usize)],
    ) -> Option<Vec<GroupedAggregateResult>> {
        use crate::instrumentation::{
            record_artifact_columnar_group_fallback, ArtifactColumnarGroupFallback,
            ArtifactColumnarGroupRecord,
        };

        if self.hot.row_count() != 0
            || self.segment_mgr.has_pending_tombstones(self.txn_id())
            || !self.segment_mgr.is_tombstone_set_empty()
        {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::RowState);
            return None;
        }

        let schema = self.hot.schema();
        let volumes = self.segment_mgr.get_volumes_newest_first_lazy();
        let Some((_, first_segment)) = volumes.first() else {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::NoColdArtifact);
            return None;
        };
        let plan = match Self::build_artifact_columnar_group_plan(
            schema,
            &first_segment.mapping,
            group_by_indices,
            aggregates,
        ) {
            Ok(plan) => plan,
            Err(reason) => {
                record_artifact_columnar_group_fallback(reason);
                return None;
            }
        };

        // Any visibility overlay means a row-level decision is necessary.
        // The scanner already owns that contract, so never approximate it
        // here just to keep the fast path alive.
        for (_, segment) in volumes.iter() {
            if segment.visible.is_some() {
                record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Visibility);
                return None;
            }
            if !segment.mapping.is_identity {
                record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Schema);
                return None;
            }
            if segment.volume.artifact_source().is_none() {
                record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Storage);
                return None;
            }
        }

        let direct_bounds = Self::artifact_columnar_group_direct_bounds(&plan, volumes.as_slice());
        let mut groups = ArtifactColumnarGroupState::new(direct_bounds);
        let uses_direct_accumulator = groups.is_direct_array();
        let mut observed_row_groups = 0u64;
        let mut observed_selected_blocks = 0u64;
        let mut observed_input_rows = 0u64;
        let mut observed_local_merges = 0u64;
        #[cfg(feature = "parallel")]
        let (scheduled_results, observed_scheduler_runs, observed_scheduled_segments) = {
            if volumes.len() > 1 {
                use rayon::prelude::*;
                (
                    Some(
                        volumes
                            .par_iter()
                            .map(|(_, segment)| {
                                Self::accumulate_artifact_columnar_group_segment(
                                    &plan,
                                    segment,
                                    direct_bounds,
                                )
                            })
                            .collect::<Vec<_>>(),
                    ),
                    1,
                    volumes.len() as u64,
                )
            } else {
                (None, 0, 0)
            }
        };
        #[cfg(not(feature = "parallel"))]
        let scheduled_results: Option<
            Vec<
                std::result::Result<
                    ArtifactColumnarGroupSegmentResult,
                    crate::instrumentation::ArtifactColumnarGroupFallback,
                >,
            >,
        > = None;
        #[cfg(not(feature = "parallel"))]
        let (observed_scheduler_runs, observed_scheduled_segments) = (0u64, 0u64);

        if let Some(results) = scheduled_results {
            for result in results {
                let segment = match result {
                    Ok(segment) => segment,
                    Err(reason) => {
                        record_artifact_columnar_group_fallback(reason);
                        return None;
                    }
                };
                observed_row_groups = observed_row_groups.saturating_add(segment.row_groups);
                observed_selected_blocks =
                    observed_selected_blocks.saturating_add(segment.selected_blocks);
                observed_input_rows = observed_input_rows.saturating_add(segment.input_rows);
                if !groups.merge_from(segment.groups, plan.aggregates.len()) {
                    record_artifact_columnar_group_fallback(
                        ArtifactColumnarGroupFallback::Accumulator,
                    );
                    return None;
                }
                observed_local_merges = observed_local_merges.saturating_add(1);
            }
        } else {
            for (_, segment) in volumes.iter() {
                let segment = match Self::accumulate_artifact_columnar_group_segment(
                    &plan,
                    segment,
                    direct_bounds,
                ) {
                    Ok(segment) => segment,
                    Err(reason) => {
                        record_artifact_columnar_group_fallback(reason);
                        return None;
                    }
                };
                observed_row_groups = observed_row_groups.saturating_add(segment.row_groups);
                observed_selected_blocks =
                    observed_selected_blocks.saturating_add(segment.selected_blocks);
                observed_input_rows = observed_input_rows.saturating_add(segment.input_rows);
                if !groups.merge_from(segment.groups, plan.aggregates.len()) {
                    record_artifact_columnar_group_fallback(
                        ArtifactColumnarGroupFallback::Accumulator,
                    );
                    return None;
                }
                observed_local_merges = observed_local_merges.saturating_add(1);
            }
        }

        if observed_local_merges != volumes.len() as u64 {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Accumulator);
            return None;
        }

        let observed_output_groups = groups.group_count() as u64;
        if observed_input_rows == 0 && observed_output_groups != 0 {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Accumulator);
            return None;
        }

        if observed_scheduler_runs > 0 && observed_scheduled_segments != volumes.len() as u64 {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Accumulator);
            return None;
        }

        if observed_scheduler_runs == 0 && observed_scheduled_segments != 0 {
            record_artifact_columnar_group_fallback(ArtifactColumnarGroupFallback::Accumulator);
            return None;
        }
        crate::instrumentation::record_artifact_columnar_group_aggregate(
            ArtifactColumnarGroupRecord {
                row_groups: observed_row_groups,
                selected_blocks: observed_selected_blocks,
                input_rows: observed_input_rows,
                output_groups: observed_output_groups,
                direct_accumulators: u64::from(uses_direct_accumulator),
                hash_accumulators: u64::from(!uses_direct_accumulator),
                local_merges: observed_local_merges,
                merged_groups: observed_output_groups,
                scheduler_runs: observed_scheduler_runs,
                scheduled_segments: observed_scheduled_segments,
            },
        );

        Self::finalize_artifact_columnar_group_state(&plan, groups)
    }

    pub(super) fn compute_filtered_aggregates_scanner(
        &self,
        aggregates: &[(AggregateOp, usize)],
        where_expr: &dyn Expression,
    ) -> Option<Vec<Value>> {
        #[derive(Clone)]
        struct Accum {
            count: i64,
            int_sum: i128,
            float_sum: f64,
            is_int_agg: bool,
            min: Option<Value>,
            max: Option<Value>,
        }

        impl Default for Accum {
            fn default() -> Self {
                Self {
                    count: 0,
                    int_sum: 0,
                    float_sum: 0.0,
                    is_int_agg: true,
                    min: None,
                    max: None,
                }
            }
        }

        fn update_accums(accums: &mut [Accum], aggregates: &[(AggregateOp, usize)], row: &Row) {
            for (agg_idx, (op, col_idx)) in aggregates.iter().enumerate() {
                let acc = &mut accums[agg_idx];
                match op {
                    AggregateOp::CountStar => acc.count += 1,
                    AggregateOp::Count => {
                        if row.get(*col_idx).is_some_and(|value| !value.is_null()) {
                            acc.count += 1;
                        }
                    }
                    AggregateOp::Sum | AggregateOp::Avg => match row.get(*col_idx) {
                        Some(Value::Integer(value)) => {
                            acc.int_sum += *value as i128;
                            acc.count += 1;
                        }
                        Some(Value::Float(value)) if !value.is_nan() => {
                            acc.float_sum += *value;
                            acc.count += 1;
                            acc.is_int_agg = false;
                        }
                        _ => {}
                    },
                    AggregateOp::Min => {
                        let Some(value) = row.get(*col_idx) else {
                            continue;
                        };
                        if value.is_null() {
                            continue;
                        }
                        match &acc.min {
                            None => acc.min = Some(value.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Less) = value.compare(current) {
                                    acc.min = Some(value.clone());
                                }
                            }
                        }
                    }
                    AggregateOp::Max => {
                        let Some(value) = row.get(*col_idx) else {
                            continue;
                        };
                        if value.is_null() {
                            continue;
                        }
                        match &acc.max {
                            None => acc.max = Some(value.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Greater) = value.compare(current) {
                                    acc.max = Some(value.clone());
                                }
                            }
                        }
                    }
                }
            }
        }

        let schema = self.hot.schema().clone();
        let mut accums = vec![Accum::default(); aggregates.len()];

        let cold_projection =
            Self::build_cold_aggregate_projection(schema.columns.len(), aggregates, &[])?;

        let mut hot_scanner = self
            .hot
            .scan(&cold_projection.columns, Some(where_expr))
            .ok()?;
        let mut hot_skip: FxHashSet<i64> = FxHashSet::with_capacity_and_hasher(
            hot_scanner.estimated_count().unwrap_or(1024).max(1024),
            Default::default(),
        );
        while hot_scanner.next() {
            hot_skip.insert(hot_scanner.current_row_id().ok()?);
            update_accums(&mut accums, &cold_projection.aggregates, hot_scanner.row());
        }
        if hot_scanner.err().is_some() {
            return None;
        }
        if hot_scanner.close().is_err() {
            return None;
        }
        self.hot.collect_hot_row_ids_into(&mut hot_skip);
        self.segment_mgr
            .insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);

        let mut scanners = self.create_segment_scanners_filtered_exact_projection(
            &cold_projection.columns,
            Some(where_expr),
            hot_skip,
        );
        for scanner in scanners.iter_mut() {
            while scanner.next() {
                update_accums(&mut accums, &cold_projection.aggregates, scanner.row());
            }
            if scanner.err().is_some() {
                return None;
            }
            if scanner.close().is_err() {
                return None;
            }
        }

        Some(
            aggregates
                .iter()
                .zip(accums.iter())
                .map(|((op, col_idx), acc)| match op {
                    AggregateOp::Count | AggregateOp::CountStar => Value::Integer(acc.count),
                    AggregateOp::Sum => {
                        if acc.count == 0 {
                            Value::Null(DataType::Float)
                        } else if acc.is_int_agg && acc.float_sum == 0.0 {
                            if acc.int_sum >= i64::MIN as i128 && acc.int_sum <= i64::MAX as i128 {
                                Value::Integer(acc.int_sum as i64)
                            } else {
                                Value::Float(acc.int_sum as f64)
                            }
                        } else {
                            Value::Float(acc.int_sum as f64 + acc.float_sum)
                        }
                    }
                    AggregateOp::Avg => {
                        if acc.count == 0 {
                            Value::Null(DataType::Float)
                        } else {
                            Value::Float((acc.int_sum as f64 + acc.float_sum) / acc.count as f64)
                        }
                    }
                    AggregateOp::Min => acc.min.clone().unwrap_or_else(|| {
                        let data_type = schema
                            .columns
                            .get(*col_idx)
                            .map(|col| col.data_type)
                            .unwrap_or(DataType::Null);
                        Value::Null(data_type)
                    }),
                    AggregateOp::Max => acc.max.clone().unwrap_or_else(|| {
                        let data_type = schema
                            .columns
                            .get(*col_idx)
                            .map(|col| col.data_type)
                            .unwrap_or(DataType::Null);
                        Value::Null(data_type)
                    }),
                })
                .collect(),
        )
    }

    pub(super) fn compute_grouped_aggregates_scanner(
        &self,
        group_by_indices: &[usize],
        aggregates: &[(AggregateOp, usize)],
        where_expr: Option<&dyn Expression>,
    ) -> Option<Vec<GroupedAggregateResult>> {
        if group_by_indices.is_empty() {
            return None;
        }

        #[derive(Clone)]
        struct Accum {
            count: i64,
            int_sum: i128,
            float_sum: f64,
            has_float: bool,
            overflowed: bool,
            min: Option<Value>,
            max: Option<Value>,
        }

        impl Default for Accum {
            fn default() -> Self {
                Self {
                    count: 0,
                    int_sum: 0,
                    float_sum: 0.0,
                    has_float: false,
                    overflowed: false,
                    min: None,
                    max: None,
                }
            }
        }

        fn update_accums(accums: &mut [Accum], aggregates: &[(AggregateOp, usize)], row: &Row) {
            for (agg_idx, (op, col_idx)) in aggregates.iter().enumerate() {
                let acc = &mut accums[agg_idx];
                match op {
                    AggregateOp::CountStar => acc.count += 1,
                    AggregateOp::Count => {
                        if row.get(*col_idx).is_some_and(|value| !value.is_null()) {
                            acc.count += 1;
                        }
                    }
                    AggregateOp::Sum | AggregateOp::Avg => match row.get(*col_idx) {
                        Some(Value::Integer(value)) => {
                            match acc.int_sum.checked_add(*value as i128) {
                                Some(sum) => acc.int_sum = sum,
                                None => acc.overflowed = true,
                            }
                            acc.count += 1;
                        }
                        Some(Value::Float(value)) if !value.is_nan() => {
                            acc.float_sum += *value;
                            acc.has_float = true;
                            acc.count += 1;
                        }
                        _ => {}
                    },
                    AggregateOp::Min => {
                        let Some(value) = row.get(*col_idx) else {
                            continue;
                        };
                        if value.is_null() {
                            continue;
                        }
                        match &acc.min {
                            None => acc.min = Some(value.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Less) = value.compare(current) {
                                    acc.min = Some(value.clone());
                                }
                            }
                        }
                    }
                    AggregateOp::Max => {
                        let Some(value) = row.get(*col_idx) else {
                            continue;
                        };
                        if value.is_null() {
                            continue;
                        }
                        match &acc.max {
                            None => acc.max = Some(value.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Greater) = value.compare(current) {
                                    acc.max = Some(value.clone());
                                }
                            }
                        }
                    }
                }
            }
        }

        fn finalize_accums(
            aggregates: &[(AggregateOp, usize)],
            accums: &[Accum],
            schema: &Schema,
        ) -> Option<Vec<Value>> {
            aggregates
                .iter()
                .zip(accums.iter())
                .map(|((op, col_idx), acc)| {
                    Some(match op {
                        AggregateOp::Count | AggregateOp::CountStar => Value::Integer(acc.count),
                        AggregateOp::Sum => {
                            if acc.overflowed {
                                return None;
                            } else if acc.count == 0 {
                                Value::Null(DataType::Float)
                            } else if acc.has_float {
                                Value::Float(acc.int_sum as f64 + acc.float_sum)
                            } else {
                                exact_integer_sum_value(acc.int_sum)?
                            }
                        }
                        AggregateOp::Avg => {
                            if acc.count == 0 {
                                Value::Null(DataType::Float)
                            } else {
                                Value::Float(
                                    (acc.int_sum as f64 + acc.float_sum) / acc.count as f64,
                                )
                            }
                        }
                        AggregateOp::Min => acc.min.clone().unwrap_or_else(|| {
                            let data_type = schema
                                .columns
                                .get(*col_idx)
                                .map(|col| col.data_type)
                                .unwrap_or(DataType::Null);
                            Value::Null(data_type)
                        }),
                        AggregateOp::Max => acc.max.clone().unwrap_or_else(|| {
                            let data_type = schema
                                .columns
                                .get(*col_idx)
                                .map(|col| col.data_type)
                                .unwrap_or(DataType::Null);
                            Value::Null(data_type)
                        }),
                    })
                })
                .collect()
        }

        let schema = self.hot.schema().clone();
        let mut group_types = Vec::with_capacity(group_by_indices.len());
        for &gb_idx in group_by_indices {
            let column = schema.columns.get(gb_idx)?;
            group_types.push(column.data_type);
        }

        fn group_entry_key(
            row: &Row,
            row_group_indices: &[usize],
            group_types: &[DataType],
        ) -> (GroupKey, Vec<Value>) {
            let values: Vec<Value> = row_group_indices
                .iter()
                .enumerate()
                .map(|(pos, &idx)| {
                    row.get(idx)
                        .cloned()
                        .unwrap_or_else(|| Value::Null(group_types[pos]))
                })
                .collect();
            let key = if values.len() == 1 {
                GroupKey::Single(CompactArc::new(values[0].clone()))
            } else {
                GroupKey::Multi(
                    values
                        .iter()
                        .cloned()
                        .map(CompactArc::new)
                        .collect::<Vec<_>>(),
                )
            };
            (key, values)
        }

        let mut groups: GroupKeyMap<(Vec<Value>, Vec<Accum>)> = GroupKeyMap::default();

        let cold_projection = Self::build_cold_aggregate_projection(
            schema.columns.len(),
            aggregates,
            group_by_indices,
        )?;
        let cold_group_indices: Vec<usize> = group_by_indices
            .iter()
            .map(|&idx| cold_projection.positions.get(idx).and_then(|pos| *pos))
            .collect::<Option<Vec<_>>>()?;

        let mut hot_scanner = self.hot.scan(&cold_projection.columns, where_expr).ok()?;
        let mut hot_skip: FxHashSet<i64> = FxHashSet::with_capacity_and_hasher(
            hot_scanner.estimated_count().unwrap_or(1024).max(1024),
            Default::default(),
        );
        while hot_scanner.next() {
            hot_skip.insert(hot_scanner.current_row_id().ok()?);
            let row = hot_scanner.row();
            let (key, group_values) = group_entry_key(row, &cold_group_indices, &group_types);
            let entry = groups
                .entry(key)
                .or_insert_with(|| (group_values, vec![Accum::default(); aggregates.len()]));
            update_accums(&mut entry.1, &cold_projection.aggregates, row);
        }
        if hot_scanner.err().is_some() {
            return None;
        }
        if hot_scanner.close().is_err() {
            return None;
        }
        self.hot.collect_hot_row_ids_into(&mut hot_skip);
        self.segment_mgr
            .insert_pending_tombstones_into(self.txn_id(), &mut hot_skip);

        let mut scanners = self.create_segment_scanners_filtered_exact_projection(
            &cold_projection.columns,
            where_expr,
            hot_skip,
        );
        for scanner in scanners.iter_mut() {
            while scanner.next() {
                let row = scanner.row();
                let (key, group_values) = group_entry_key(row, &cold_group_indices, &group_types);
                let entry = groups
                    .entry(key)
                    .or_insert_with(|| (group_values, vec![Accum::default(); aggregates.len()]));
                update_accums(&mut entry.1, &cold_projection.aggregates, row);
            }
            if scanner.err().is_some() {
                return None;
            }
            if scanner.close().is_err() {
                return None;
            }
        }

        let mut results = Vec::with_capacity(groups.len());
        for (group_values, accums) in groups.into_values() {
            results.push(GroupedAggregateResult {
                group_values,
                aggregate_values: finalize_accums(aggregates, &accums, &schema)?,
            });
        }
        Some(results)
    }
}