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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
use super::*;
impl<'host, H: AggregationHost + ?Sized> AggregationExecutor<'host, H> {
/// Fast SUM implementation that bypasses the generic aggregate function
/// Uses loop unrolling for better performance
#[inline]
pub(super) fn fast_sum_column(&self, rows: &[(i64, Row)], col_idx: usize) -> Value {
if rows.is_empty() {
return Value::null_unknown();
}
// Use parallel processing for large datasets
#[cfg(feature = "parallel")]
if rows.len() >= 10_000 {
return self.fast_sum_column_parallel(rows, col_idx);
}
let mut sum_int: i64 = 0;
let mut sum_float: f64 = 0.0;
let mut has_float = false;
let mut has_value = false;
// Unroll loop by 4 for better CPU pipelining
let chunks = rows.chunks_exact(4);
let remainder = chunks.remainder();
for chunk in chunks {
for (_, row) in chunk {
if let Some(val) = row.get(col_idx) {
match val {
Value::Integer(i) => {
has_value = true;
if has_float {
sum_float += *i as f64;
} else {
match sum_int.checked_add(*i) {
Some(value) => sum_int = value,
None => {
has_float = true;
sum_float = sum_int as f64 + *i as f64;
}
}
}
}
Value::Float(f) => {
has_value = true;
if !has_float {
has_float = true;
sum_float = sum_int as f64;
}
sum_float += f;
}
_ => {}
}
}
}
}
// Handle remainder
for (_, row) in remainder {
if let Some(val) = row.get(col_idx) {
match val {
Value::Integer(i) => {
has_value = true;
if has_float {
sum_float += *i as f64;
} else {
match sum_int.checked_add(*i) {
Some(value) => sum_int = value,
None => {
has_float = true;
sum_float = sum_int as f64 + *i as f64;
}
}
}
}
Value::Float(f) => {
has_value = true;
if !has_float {
has_float = true;
sum_float = sum_int as f64;
}
sum_float += f;
}
_ => {}
}
}
}
if !has_value {
Value::null_unknown()
} else if has_float {
Value::Float(sum_float)
} else {
Value::Integer(sum_int)
}
}
/// Parallel SUM implementation using Rayon
#[cfg(feature = "parallel")]
#[inline]
pub(super) fn fast_sum_column_parallel(&self, rows: &[(i64, Row)], col_idx: usize) -> Value {
let chunk_size = (rows.len() / rayon::current_num_threads()).max(1000);
// Process in parallel, collecting (sum_int, sum_float, has_float, has_value)
let results: Vec<(i64, f64, bool, bool)> = rows
.par_chunks(chunk_size)
.map(|chunk| {
let mut sum_int: i64 = 0;
let mut sum_float: f64 = 0.0;
let mut has_float = false;
let mut has_value = false;
for (_, row) in chunk {
if let Some(val) = row.get(col_idx) {
match val {
Value::Integer(i) => {
has_value = true;
if has_float {
sum_float += *i as f64;
} else {
match sum_int.checked_add(*i) {
Some(value) => sum_int = value,
None => {
has_float = true;
sum_float = sum_int as f64 + *i as f64;
}
}
}
}
Value::Float(f) => {
has_value = true;
if !has_float {
has_float = true;
sum_float = sum_int as f64;
}
sum_float += f;
}
_ => {}
}
}
}
(sum_int, sum_float, has_float, has_value)
})
.collect();
// Merge results
let mut total_int: i64 = 0;
let mut total_float: f64 = 0.0;
let mut any_float = false;
let mut any_value = false;
for (si, sf, hf, hv) in results {
if hv {
any_value = true;
if hf || any_float {
any_float = true;
if hf {
total_float += sf;
} else {
total_float += si as f64;
}
} else {
match total_int.checked_add(si) {
Some(value) => total_int = value,
None => {
any_float = true;
total_float += total_int as f64 + si as f64;
total_int = 0;
}
}
}
}
}
// If we switched to float mid-way, add the integer total
if any_float && total_int != 0 {
total_float += total_int as f64;
}
if !any_value {
Value::null_unknown()
} else if any_float {
Value::Float(total_float)
} else {
Value::Integer(total_int)
}
}
/// Fast AVG implementation
#[inline]
pub(super) fn fast_avg_column(&self, rows: &[(i64, Row)], col_idx: usize) -> Value {
if rows.is_empty() {
return Value::null_unknown();
}
// Use parallel processing for large datasets
#[cfg(feature = "parallel")]
if rows.len() >= 10_000 {
return self.fast_avg_column_parallel(rows, col_idx);
}
let mut sum: f64 = 0.0;
let mut count: i64 = 0;
for (_, row) in rows {
if let Some(val) = row.get(col_idx) {
match val {
Value::Integer(i) => {
sum += *i as f64;
count += 1;
}
Value::Float(f) => {
sum += f;
count += 1;
}
_ => {}
}
}
}
if count == 0 {
Value::null_unknown()
} else {
Value::Float(sum / count as f64)
}
}
/// Parallel AVG implementation
#[cfg(feature = "parallel")]
#[inline]
pub(super) fn fast_avg_column_parallel(&self, rows: &[(i64, Row)], col_idx: usize) -> Value {
let chunk_size = (rows.len() / rayon::current_num_threads()).max(1000);
// Process in parallel, collecting (sum, count)
let results: Vec<(f64, i64)> = rows
.par_chunks(chunk_size)
.map(|chunk| {
let mut sum: f64 = 0.0;
let mut count: i64 = 0;
for (_, row) in chunk {
if let Some(val) = row.get(col_idx) {
match val {
Value::Integer(i) => {
sum += *i as f64;
count += 1;
}
Value::Float(f) => {
sum += f;
count += 1;
}
_ => {}
}
}
}
(sum, count)
})
.collect();
// Merge results
let mut total_sum: f64 = 0.0;
let mut total_count: i64 = 0;
for (s, c) in results {
total_sum += s;
total_count += c;
}
if total_count == 0 {
Value::null_unknown()
} else {
Value::Float(total_sum / total_count as f64)
}
}
/// Check if an expression is a PURE aggregate function call.
///
/// Returns true only for:
/// - `COUNT(*)`, `SUM(col)`, `MIN(col)`, `MAX(col)`, `AVG(col)` directly
/// - Same with alias: `COUNT(*) AS cnt`
///
/// Returns false for:
/// - `SUM(col) + 10` (wrapped in Infix)
/// - `-SUM(col)` (wrapped in Prefix)
/// - `col * 2` (not an aggregate)
pub(super) fn is_pure_aggregate_expression(expr: &Expression) -> bool {
match expr {
Expression::FunctionCall(func) => {
// Check if it's an aggregate function
matches!(
func.function.to_uppercase().as_str(),
"COUNT" | "SUM" | "MIN" | "MAX" | "AVG"
)
}
Expression::Aliased(aliased) => {
// Check the inner expression
Self::is_pure_aggregate_expression(&aliased.expression)
}
_ => false,
}
}
/// Try to compute aggregates directly on the table without materializing rows.
///
/// This is the "deferred aggregation" optimization for simple queries like:
/// - `SELECT COUNT(*) FROM table`
/// - `SELECT SUM(col), MIN(col), MAX(col) FROM table`
///
/// Returns the original source untouched when the optimization cannot be
/// applied. If the first row has already been inspected, the rejection
/// wraps it so the caller can continue the same source exactly once.
/// Returns Some(result) if the aggregates were computed directly.
///
/// # Eligibility
/// - No WHERE clause (or simplified to nothing)
/// - No GROUP BY clause
/// - No HAVING clause
/// - No window functions
/// - Simple column aggregates only (no expressions like SUM(a+b))
/// - No DISTINCT
/// - No ORDER BY on aggregates (like STRING_AGG with ORDER BY)
/// - No FILTER clause on aggregates
pub(crate) fn try_aggregation_pushdown(
&self,
table: &dyn radixdb_storage::traits::Table,
stmt: &SelectStatement,
_ctx: &ExecutionContext,
classification: &std::sync::Arc<QueryClassification>,
) -> Result<Option<Box<dyn radixdb_storage::traits::QueryResult>>> {
// classification is passed from caller to avoid redundant cache lookups
// Quick eligibility checks using cached classification
if classification.has_where {
return Ok(None);
}
if classification.has_group_by {
return Ok(None);
}
if classification.has_having {
return Ok(None);
}
if classification.has_window_functions {
return Ok(None);
}
// CRITICAL: Check that each column expression is a PURE aggregate function
// We cannot pushdown expressions like SUM(val) + 10, -SUM(val), etc.
// Only handle direct function calls: SUM(val), COUNT(*), MAX(val), etc.
for col in &stmt.columns {
if !Self::is_pure_aggregate_expression(col) {
return Ok(None);
}
}
// Parse aggregations
let (aggregations, non_agg_columns) = self.parse_aggregations(stmt)?;
// Must have only aggregations, no regular columns
if !non_agg_columns.is_empty() {
return Ok(None);
}
if aggregations.is_empty() {
return Ok(None);
}
// Check all aggregations are simple (no expression, no ORDER BY, no FILTER)
// COUNT(DISTINCT col) is allowed if column has an index
for agg in &aggregations {
if agg.expression.is_some() || !agg.order_by.is_empty() || agg.filter.is_some() {
return Ok(None);
}
// COUNT(DISTINCT col) is allowed, other DISTINCT aggregates are not
if agg.distinct && agg.name != "COUNT" {
return Ok(None);
}
// Only support COUNT, SUM, MIN, MAX, AVG
match agg.name.as_str() {
"COUNT" | "SUM" | "MIN" | "MAX" | "AVG" => {}
_ => return Ok(None),
}
}
// Build column index map using schema's cached lowercase column names
let schema_lower = table.schema().column_names_lower_arc();
let col_index_map: StringMap<usize> = schema_lower
.iter()
.enumerate()
.map(|(i, c)| (c.clone(), i))
.collect();
// Compute each aggregate
// Use CompactVec directly to avoid Vec→CompactVec conversion
let mut result_values: CompactVec<Value> = CompactVec::with_capacity(aggregations.len());
let mut result_columns: Vec<String> = Vec::with_capacity(aggregations.len());
for agg in &aggregations {
result_columns.push(agg.get_column_name());
match agg.name.as_str() {
"COUNT" => {
if agg.distinct {
// COUNT(DISTINCT col) - try to get count from index without cloning values
if let Some(count) = table.get_partition_count(&agg.column_lower) {
// get_partition_count already excludes NULL values per SQL standard
result_values.push(Value::Integer(count as i64));
} else {
// No index on this column, can't pushdown
return Ok(None);
}
} else if agg.column == "*" {
// COUNT(*) may use metadata only when storage can prove
// the exact count for this transaction. Snapshot-backed
// segmented tables deliberately decline that proof:
// calling row_count() there used to materialize the
// entire visible table before returning one scalar.
// Decline this pushdown so the bounded exact-empty
// streaming path below owns the fallback instead.
let Some(count) = table.fast_row_count() else {
return Ok(None);
};
result_values.push(Value::Integer(count as i64));
} else {
// COUNT(col) - need to count non-null values, can't pushdown easily
return Ok(None);
}
}
"SUM" => {
let col_idx = col_index_map.get(&agg.column_lower).copied();
if let Some(idx) = col_idx {
if let Some(sum) = table.sum_column(idx) {
if sum.count() == 0 {
result_values.push(Value::null(radixdb_core::DataType::Float));
} else {
result_values.push(sum.into_value()?);
}
} else {
return Ok(None); // Pushdown not available
}
} else {
return Ok(None); // Column not found
}
}
"AVG" => {
let col_idx = col_index_map.get(&agg.column_lower).copied();
if let Some(idx) = col_idx {
if let Some(sum) = table.avg_column(idx) {
if sum.count() == 0 {
result_values.push(Value::null(radixdb_core::DataType::Float));
} else {
result_values.push(Value::Float(sum.as_f64() / sum.count() as f64));
}
} else {
return Ok(None); // Pushdown not available
}
} else {
return Ok(None); // Column not found
}
}
"MIN" => {
let col_idx = col_index_map.get(&agg.column_lower).copied();
if let Some(idx) = col_idx {
if let Some(min_val) = table.min_column(idx) {
result_values.push(
min_val.unwrap_or_else(|| {
Value::null(radixdb_core::DataType::Integer)
}),
);
} else {
return Ok(None); // Pushdown not available
}
} else {
return Ok(None); // Column not found
}
}
"MAX" => {
let col_idx = col_index_map.get(&agg.column_lower).copied();
if let Some(idx) = col_idx {
if let Some(max_val) = table.max_column(idx) {
result_values.push(
max_val.unwrap_or_else(|| {
Value::null(radixdb_core::DataType::Integer)
}),
);
} else {
return Ok(None); // Pushdown not available
}
} else {
return Ok(None); // Column not found
}
}
_ => return Ok(None),
}
}
// Build result
let row = Row::from_compact_vec(result_values);
let mut rows = RowVec::with_capacity(1);
rows.push((0, row));
Ok(Some(Box::new(crate::result::ExecutorResult::new(
result_columns,
rows,
))))
}
/// Try to push filtered aggregation (WHERE + aggregates) directly to the storage layer.
///
/// This handles queries like:
/// - `SELECT COUNT(*) FROM orders WHERE status = 'shipped'`
/// - `SELECT SUM(amount), AVG(amount) FROM sales WHERE region = 'US'`
///
/// The WHERE clause is converted to a storage expression and passed alongside the
/// aggregate operations to `Table::compute_filtered_aggregates`, which can scan and
/// aggregate in a single pass without materializing Row objects in the executor.
///
/// # Eligibility
/// - Must have WHERE and aggregation, no HAVING/window functions/joins/GROUP BY
/// - No DISTINCT aggregates, no subqueries or parameters in WHERE
/// - All SELECT columns must be pure aggregate function calls
///
/// # Returns
/// - `Ok(Some(result))` if the pushdown was applied
/// - `Ok(None)` if the query is not eligible (falls through to next path)
pub(crate) fn try_filtered_aggregation_pushdown(
&self,
table: &dyn radixdb_storage::traits::Table,
stmt: &SelectStatement,
ctx: &ExecutionContext,
classification: &std::sync::Arc<QueryClassification>,
columns: &[String],
) -> Result<Option<Box<dyn radixdb_storage::traits::QueryResult>>> {
use radixdb_storage::mvcc::version_store::AggregateOp;
// --- Eligibility checks ---
if !classification.has_where {
return Ok(None);
}
if !classification.has_aggregation {
return Ok(None);
}
if classification.has_having {
return Ok(None);
}
if classification.has_window_functions {
return Ok(None);
}
if classification.has_joins {
return Ok(None);
}
if classification.has_group_by {
return Ok(None);
}
// Parameter syntax alone is not a reason to reject the storage path:
// `try_pushdown(..., Some(ctx))` below resolves only values that are
// actually bound in this execution context. Missing/unresolved values
// cannot become a StorageExpr and therefore still fall back safely.
// Subqueries in WHERE cannot be pushed to storage
if classification.where_has_subqueries {
return Ok(None);
}
// All SELECT columns must be pure aggregate function calls
for col in &stmt.columns {
if !Self::is_pure_aggregate_expression(col) {
return Ok(None);
}
}
// Parse aggregations to validate and extract details
let (aggregations, non_agg_columns) = self.parse_aggregations(stmt)?;
// Must have only aggregations, no regular columns
if !non_agg_columns.is_empty() || aggregations.is_empty() {
return Ok(None);
}
// Check all aggregations are simple (no expression, no ORDER BY, no FILTER, no DISTINCT)
for agg in &aggregations {
if agg.expression.is_some() || !agg.order_by.is_empty() || agg.filter.is_some() {
return Ok(None);
}
if agg.distinct {
return Ok(None);
}
match agg.name.as_str() {
"COUNT" | "SUM" | "MIN" | "MAX" | "AVG" => {}
_ => return Ok(None),
}
}
// --- Convert WHERE clause to storage expression ---
let where_expr = match stmt.where_clause.as_ref() {
Some(expr) => expr,
None => return Ok(None),
};
let schema = table.schema();
let (storage_expr, needs_memory_filter) =
crate::pushdown::try_pushdown(where_expr, schema, Some(ctx));
// Bail when the WHERE clause is only partially pushed to storage.
// The residual predicate would be ignored, producing wrong aggregates.
if needs_memory_filter {
return Ok(None);
}
let storage_expr = match storage_expr {
Some(expr) => expr,
None => return Ok(None), // Cannot convert WHERE to storage expression
};
// --- Build (AggregateOp, column_index) pairs ---
let col_map: FxHashMap<&str, usize> = columns
.iter()
.enumerate()
.map(|(i, name)| (name.as_str(), i))
.collect();
let mut agg_ops: Vec<(AggregateOp, usize)> = Vec::with_capacity(aggregations.len());
let mut result_columns: Vec<String> = Vec::with_capacity(aggregations.len());
for agg in &aggregations {
result_columns.push(agg.get_column_name());
let (op, col_idx) = match agg.name.as_str() {
"COUNT" => {
if agg.column == "*" {
(AggregateOp::CountStar, 0)
} else if let Some(&idx) = col_map.get(agg.column_lower.as_str()) {
(AggregateOp::Count, idx)
} else {
return Ok(None);
}
}
"SUM" => {
if let Some(&idx) = col_map.get(agg.column_lower.as_str()) {
(AggregateOp::Sum, idx)
} else {
return Ok(None);
}
}
"AVG" => {
if let Some(&idx) = col_map.get(agg.column_lower.as_str()) {
(AggregateOp::Avg, idx)
} else {
return Ok(None);
}
}
"MIN" => {
if let Some(&idx) = col_map.get(agg.column_lower.as_str()) {
(AggregateOp::Min, idx)
} else {
return Ok(None);
}
}
"MAX" => {
if let Some(&idx) = col_map.get(agg.column_lower.as_str()) {
(AggregateOp::Max, idx)
} else {
return Ok(None);
}
}
_ => return Ok(None),
};
agg_ops.push((op, col_idx));
}
// Narrow metadata-only operator for the benchmark-critical shape:
// `COUNT(*)` over an exact conjunction of INTEGER primary-key bounds.
// The Table contract returns None when its current MVCC/segment state
// cannot prove the result, so all broader SQL shapes keep the existing
// filtered aggregate path below.
if agg_ops.as_slice() == [(AggregateOp::CountStar, 0)] {
if let Some(pk_idx) = schema.pk_column_index() {
if let Some(pk_column) = schema.columns.get(pk_idx) {
if pk_column.data_type == radixdb_core::DataType::Integer {
let comparisons = storage_expr.collect_comparisons();
if let Some(range) = radixdb_storage::traits::table::IntegerPrimaryKeyRange::from_conjunctive_comparisons(
&comparisons,
pk_column.name_lower.as_str(),
true,
) {
if let Some(metadata_count) =
table.count_visible_integer_primary_key_range(&range)
{
let count = metadata_count?;
let count = i64::try_from(count).map_err(|_| {
radixdb_core::Error::internal(
"metadata primary-key count exceeds INTEGER result range",
)
})?;
let mut rows = RowVec::with_capacity(1);
rows.push((
0,
Row::from_compact_vec(CompactVec::from(vec![Value::Integer(
count,
)])),
));
return Ok(Some(Box::new(crate::result::ExecutorResult::new(
result_columns,
rows,
))));
}
}
}
}
}
}
// --- Call storage-level filtered aggregation ---
let values = match table.compute_filtered_aggregates(&agg_ops, storage_expr.as_ref()) {
Some(v) => v,
None => return Ok(None),
};
// --- Build result ---
let mut result_values: CompactVec<Value> = CompactVec::with_capacity(values.len());
for v in values {
result_values.push(v);
}
let row = Row::from_compact_vec(result_values);
let mut rows = RowVec::with_capacity(1);
rows.push((0, row));
Ok(Some(Box::new(crate::result::ExecutorResult::new(
result_columns,
rows,
))))
}
/// Try to compute global aggregates using streaming (no row materialization).
///
/// This is a fallback for queries that can't use direct aggregation pushdown,
/// but can still avoid collecting all rows by streaming through a scanner.
///
/// Examples of eligible queries:
/// - `SELECT AVG(col) * 100 FROM table` (expression wrapping aggregate)
/// - `SELECT SUM(col), COUNT(*) FROM table` (multiple simple aggregates)
///
/// # Returns
/// - `Some(result)` if streaming aggregation was used
/// - `None` if the query is not eligible for streaming
pub(crate) fn try_streaming_global_aggregation(
&self,
table: &dyn radixdb_storage::traits::Table,
stmt: &SelectStatement,
ctx: &ExecutionContext,
classification: &std::sync::Arc<QueryClassification>,
) -> Result<Option<Box<dyn radixdb_storage::traits::QueryResult>>> {
// Quick eligibility checks using cached classification
if classification.has_where {
return Ok(None);
}
if classification.has_group_by {
return Ok(None);
}
if classification.has_having {
return Ok(None);
}
if classification.has_window_functions {
return Ok(None);
}
if classification.has_order_by {
return Ok(None);
}
if classification.has_limit {
return Ok(None);
}
// Parse aggregations - allow non-pure expressions (like AVG(col) * 100)
let (aggregations, non_agg_columns) = self.parse_aggregations(stmt)?;
// Must have only aggregations, no regular columns
if !non_agg_columns.is_empty() {
return Ok(None);
}
if aggregations.is_empty() {
return Ok(None);
}
// Check all aggregations are simple enough for streaming
// (no ORDER BY, no FILTER, no DISTINCT except COUNT, no expression arguments)
for agg in &aggregations {
if !agg.order_by.is_empty() || agg.filter.is_some() {
return Ok(None);
}
if agg.distinct && agg.name != "COUNT" {
return Ok(None);
}
// Can't handle expression arguments like SUM(a + b) - need full evaluation
if agg.expression.is_some() {
return Ok(None);
}
// Only support COUNT, SUM, MIN, MAX, AVG for streaming
match agg.name.as_str() {
"COUNT" | "SUM" | "MIN" | "MAX" | "AVG" => {}
_ => return Ok(None),
}
}
// Build column index map using schema's cached lowercase column names
let schema_lower = table.schema().column_names_lower_arc();
let col_index_map: StringMap<usize> = schema_lower
.iter()
.enumerate()
.map(|(i, c)| (c.clone(), i))
.collect();
// Pre-compute column indices for each aggregation
let agg_col_indices: Vec<Option<usize>> = aggregations
.iter()
.map(|agg| {
if agg.column == "*" || agg.expression.is_some() {
None
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
}
})
.collect();
let (scan_columns, agg_projected_indices) =
Self::build_streaming_aggregate_projection(&agg_col_indices);
// FAST PATH: Try deferred aggregation (no row materialization)
// This bypasses the lazy scanner entirely for simple aggregates
let can_use_deferred = aggregations.iter().all(|agg| !agg.distinct);
if can_use_deferred {
let mut deferred_values: Vec<Option<Value>> = Vec::with_capacity(aggregations.len());
let mut all_succeeded = true;
for (i, agg) in aggregations.iter().enumerate() {
let col_idx = agg_col_indices[i];
let value = match agg.name.as_str() {
"COUNT" => {
if agg.column == "*" {
// Only metadata-proven counts belong in the
// deferred path. Otherwise keep the scanner-based
// exact-empty fallback bounded and fallible.
table
.fast_row_count()
.map(|count| Value::Integer(count as i64))
} else {
// COUNT(col) - need scanner for NULL checking
// Could optimize with a dedicated count_non_null method
None
}
}
"SUM" => {
if let Some(idx) = col_idx {
if let Some(sum) = table.sum_column(idx) {
if sum.count() == 0 {
Some(Value::null(radixdb_core::DataType::Float))
} else {
Some(sum.into_value()?)
}
} else {
None
}
} else {
None
}
}
"AVG" => {
if let Some(idx) = col_idx {
if let Some(sum) = table.avg_column(idx) {
if sum.count() == 0 {
Some(Value::null(radixdb_core::DataType::Float))
} else {
Some(Value::Float(sum.as_f64() / sum.count() as f64))
}
} else {
None
}
} else {
None
}
}
"MIN" => col_idx.and_then(|idx| {
table.min_column(idx).map(|min_opt| {
min_opt.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer))
})
}),
"MAX" => col_idx.and_then(|idx| {
table.max_column(idx).map(|max_opt| {
max_opt.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer))
})
}),
_ => None,
};
if let Some(v) = value {
deferred_values.push(Some(v));
} else {
all_succeeded = false;
break;
}
}
if all_succeeded && deferred_values.len() == aggregations.len() {
// Build result from deferred values
// Use CompactVec directly to avoid Vec→CompactVec conversion
let mut agg_result_values: CompactVec<Value> =
CompactVec::with_capacity(aggregations.len());
let mut agg_result_columns: Vec<String> = Vec::with_capacity(aggregations.len());
for (i, agg) in aggregations.iter().enumerate() {
agg_result_columns.push(agg.get_column_name());
agg_result_values.push(deferred_values[i].take().unwrap());
}
// Apply post-aggregation expressions if needed
let agg_row = Row::from_compact_vec(agg_result_values);
let mut agg_rows = RowVec::with_capacity(1);
agg_rows.push((0, agg_row));
let (final_columns, final_rows) = self.apply_post_aggregation_expressions(
stmt,
ctx,
agg_result_columns,
agg_rows,
)?;
return Ok(Some(Box::new(crate::result::ExecutorResult::new(
final_columns,
final_rows,
))));
}
}
// SLOW PATH: Fall back to scanner-based streaming
// Initialize aggregate states
struct AggState {
sum: f64,
count: i64,
min: Option<Value>,
max: Option<Value>,
distinct_set: Option<ValueSet>,
}
let mut states: Vec<AggState> = aggregations
.iter()
.map(|agg| AggState {
sum: 0.0,
count: 0,
min: None,
max: None,
distinct_set: agg.distinct.then(ValueSet::default),
})
.collect();
// Get a scanner and stream through rows. Request only columns used by
// aggregate arguments; COUNT(*) has no column dependency and is usually
// handled by the deferred fast path above. If the fallback still reaches
// this point with no column dependencies, use the exact-empty scan
// boundary rather than the historical `scan([]) == SELECT *` contract.
let mut scanner = if scan_columns.is_empty() {
table.scan_exact_projection(&scan_columns, None)?
} else {
table.scan(&scan_columns, None)?
};
while scanner.next() {
let row = scanner.row();
for (i, agg) in aggregations.iter().enumerate() {
let state = &mut states[i];
if agg.column == "*" {
// COUNT(*)
state.count += 1;
continue;
}
let projected_col_idx = match agg_projected_indices[i] {
Some(projected) => projected,
None => continue,
};
let value = match row.get(projected_col_idx) {
Some(v) if !v.is_null() => v,
_ => continue, // Skip NULL values
};
// Handle DISTINCT
if let Some(ref mut distinct_set) = state.distinct_set {
if !track_distinct_value(distinct_set, value) {
continue; // Already seen this value
}
}
match agg.name.as_str() {
"COUNT" => {
state.count += 1;
}
"SUM" | "AVG" => {
let num = match value {
Value::Integer(i) => *i as f64,
Value::Float(f) => *f,
_ => continue,
};
state.sum += num;
state.count += 1;
}
"MIN" => {
let is_smaller = match (&state.min, value) {
(None, _) => true,
(Some(current), new) => {
new.compare(current).unwrap_or(std::cmp::Ordering::Equal)
== std::cmp::Ordering::Less
}
};
if is_smaller {
state.min = Some(value.clone());
}
}
"MAX" => {
let is_larger = match (&state.max, value) {
(None, _) => true,
(Some(current), new) => {
new.compare(current).unwrap_or(std::cmp::Ordering::Equal)
== std::cmp::Ordering::Greater
}
};
if is_larger {
state.max = Some(value.clone());
}
}
_ => {}
}
}
}
if let Some(e) = scanner.err() {
return Err(radixdb_core::Error::internal(format!("scan error: {}", e)));
}
scanner.close()?;
// Build intermediate result columns (raw aggregate values)
// Use CompactVec directly to avoid Vec→CompactVec conversion
let mut agg_result_values: CompactVec<Value> =
CompactVec::with_capacity(aggregations.len());
let mut agg_result_columns: Vec<String> = Vec::with_capacity(aggregations.len());
for (i, agg) in aggregations.iter().enumerate() {
let state = &states[i];
agg_result_columns.push(agg.get_column_name());
let value = match agg.name.as_str() {
"COUNT" => Value::Integer(state.count),
"SUM" => {
if state.count == 0 {
Value::null(radixdb_core::DataType::Float)
} else if state.sum.fract() == 0.0 && state.sum.abs() < i64::MAX as f64 {
Value::Integer(state.sum as i64)
} else {
Value::Float(state.sum)
}
}
"AVG" => {
if state.count == 0 {
Value::null(radixdb_core::DataType::Float)
} else {
Value::Float(state.sum / state.count as f64)
}
}
"MIN" => state
.min
.clone()
.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer)),
"MAX" => state
.max
.clone()
.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer)),
_ => Value::null(radixdb_core::DataType::Integer),
};
agg_result_values.push(value);
}
// Apply post-aggregation expressions if needed
// This handles cases like AVG(col) * 100
let agg_row = Row::from_compact_vec(agg_result_values);
let mut agg_rows = RowVec::with_capacity(1);
agg_rows.push((0, agg_row));
let (final_columns, final_rows) =
self.apply_post_aggregation_expressions(stmt, ctx, agg_result_columns, agg_rows)?;
Ok(Some(Box::new(crate::result::ExecutorResult::new(
final_columns,
final_rows,
))))
}
pub(super) fn build_streaming_aggregate_projection(
agg_col_indices: &[Option<usize>],
) -> (Vec<usize>, Vec<Option<usize>>) {
let mut scan_columns = Vec::new();
let mut projected_positions: FxHashMap<usize, usize> = FxHashMap::default();
let agg_projected_indices: Vec<Option<usize>> = agg_col_indices
.iter()
.map(|col_idx| {
col_idx.map(|idx| {
if let Some(pos) = projected_positions.get(&idx) {
*pos
} else {
let pos = scan_columns.len();
scan_columns.push(idx);
projected_positions.insert(idx, pos);
pos
}
})
})
.collect();
(scan_columns, agg_projected_indices)
}
pub(super) fn plan_streaming_derived_table_aggregation(
&self,
stmt: &SelectStatement,
classification: &std::sync::Arc<QueryClassification>,
source_columns: &[String],
) -> Result<Option<DerivedAggregationPlan>> {
// Quick eligibility checks
if !classification.has_group_by {
return Ok(None);
}
if classification.has_having {
return Ok(None);
}
if classification.has_window_functions {
return Ok(None);
}
if classification.has_order_by
|| classification.has_limit
|| classification.has_offset
|| classification.has_distinct
|| classification.has_distinct_on
|| !stmt.set_operations.is_empty()
{
return Ok(None);
}
// Skip ROLLUP/CUBE/GROUPING SETS
if stmt.group_by.modifier != radixdb_sql::ast::GroupByModifier::None {
return Ok(None);
}
// Only single-column GROUP BY for this optimization
if stmt.group_by.columns.len() != 1 {
return Ok(None);
}
// GROUP BY column must be a simple column reference (identifier)
let group_col_name = match &stmt.group_by.columns[0] {
radixdb_sql::ast::Expression::Identifier(id) => id.value_lower.to_string(),
radixdb_sql::ast::Expression::QualifiedIdentifier(qid) => {
qid.name.value_lower.to_string()
}
_ => return Ok(None),
};
// Build column index map from result columns
let col_index_map: StringMap<usize> = source_columns
.iter()
.enumerate()
.map(|(i, c)| (c.to_lowercase(), i))
.collect();
// Find GROUP BY column index
let group_col_idx = match col_index_map.get(&group_col_name) {
Some(&idx) => idx,
None => return Ok(None),
};
// Parse aggregations
let (aggregations, non_agg_columns) = self.parse_aggregations(stmt)?;
// Must have only aggregations plus the GROUP BY column (no other regular columns)
// Non-agg columns must be exactly the GROUP BY column
if non_agg_columns.len() > 1 {
return Ok(None);
}
if non_agg_columns.len() == 1 && !non_agg_columns[0].eq_ignore_ascii_case(&group_col_name) {
return Ok(None);
}
// Check all aggregations are simple enough for streaming
let simple_aggs: Vec<Option<SimpleAgg>> = aggregations
.iter()
.map(|agg| {
// Must not have DISTINCT, FILTER, ORDER BY, or expression.
// COUNT(DISTINCT col) is deliberately rejected too: the
// current streaming state has no per-group distinct set.
if agg.distinct
|| agg.filter.is_some()
|| !agg.order_by.is_empty()
|| agg.expression.is_some()
{
return None;
}
match agg.name.to_uppercase().as_str() {
"COUNT" => {
if agg.column == "*" {
Some(SimpleAgg::Count(None))
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
.map(|idx| SimpleAgg::Count(Some(idx)))
}
}
"SUM" => {
if agg.column == "*" {
None
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
.map(SimpleAgg::Sum)
}
}
"AVG" => {
if agg.column == "*" {
None
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
.map(SimpleAgg::Avg)
}
}
"MIN" => {
if agg.column == "*" {
None
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
.map(SimpleAgg::Min)
}
}
"MAX" => {
if agg.column == "*" {
None
} else {
Self::lookup_column_index(&agg.column_lower, &col_index_map)
.map(SimpleAgg::Max)
}
}
_ => None,
}
})
.collect();
// All aggregates must be resolved for streaming path
if simple_aggs.iter().any(|a| a.is_none()) {
return Ok(None);
}
let simple_aggs: Vec<SimpleAgg> = simple_aggs.into_iter().map(|a| a.unwrap()).collect();
Ok(Some(DerivedAggregationPlan {
group_col_name,
group_col_idx,
aggregations,
simple_aggs,
}))
}
/// Try streaming aggregation for derived tables (FROM subqueries).
///
/// OPTIMIZATION: For simple GROUP BY + COUNT(*) on derived tables without WHERE clause,
/// stream directly to aggregation HashMap without materializing all rows first.
/// This reduces memory allocations from O(N) to O(groups).
///
/// Returns the original source untouched when the optimization cannot be
/// applied. If the first row has already been inspected, the rejection
/// preserves that row so the caller can continue the same source exactly
/// once.
///
/// Supported patterns:
/// - Single-column GROUP BY (column reference)
/// - Simple aggregates: COUNT(*), COUNT(col), SUM, AVG, MIN, MAX
/// - No HAVING, DISTINCT, FILTER, aggregate ORDER BY, outer ORDER BY/LIMIT
pub(crate) fn try_streaming_derived_table_aggregation(
&self,
mut result: Box<dyn QueryResult>,
stmt: &SelectStatement,
classification: &std::sync::Arc<QueryClassification>,
ctx: &ExecutionContext,
) -> Result<DerivedAggregationAttempt> {
use radixdb_core::SmartString;
use smallvec::SmallVec;
let source_columns = result.columns().to_vec();
let Some(plan) =
self.plan_streaming_derived_table_aggregation(stmt, classification, &source_columns)?
else {
return Ok(DerivedAggregationAttempt::Rejected(result));
};
let DerivedAggregationPlan {
group_col_name,
group_col_idx,
aggregations,
simple_aggs,
} = plan;
let num_aggs = simple_aggs.len();
// State for streaming aggregation
type AggVec<T> = SmallVec<[T; 4]>;
#[derive(Clone)]
struct StreamGroupState {
numeric_states: AggVec<NumericAccumulator>,
counts: AggVec<i64>,
min_values: AggVec<Option<Value>>,
max_values: AggVec<Option<Value>>,
}
// Template for new group state
let state_template = StreamGroupState {
numeric_states: smallvec::smallvec![NumericAccumulator::default(); num_aggs],
counts: smallvec::smallvec![0; num_aggs],
min_values: smallvec::smallvec![None; num_aggs],
max_values: smallvec::smallvec![None; num_aggs],
};
// Sample first row to detect key type
if !result.next() {
if let Some(err) = result.last_error() {
return Err(err);
}
// Empty result - return empty aggregation
let mut result_columns = Vec::with_capacity(1 + aggregations.len());
result_columns.push(group_col_name.clone());
for agg in &aggregations {
let col_name = if let Some(ref alias) = agg.alias {
alias.clone()
} else {
agg.get_expression_name()
};
result_columns.push(col_name);
}
return Ok(DerivedAggregationAttempt::Applied(Box::new(
crate::result::ExecutorResult::new(result_columns, RowVec::new()),
)));
}
// Use string fast path for Text values (common for derived tables with CASE expressions)
let use_string_path = result
.row()
.get(group_col_idx)
.map(|v| matches!(v, Value::Text(_)))
.unwrap_or(false);
if !use_string_path {
let prefetched = result.take_row();
return Ok(DerivedAggregationAttempt::Rejected(Box::new(
crate::result::PrefetchedResult::new(prefetched, result),
)));
}
{
// String GROUP BY streaming path
// OPTIMIZATION: Use hashbrown::HashMap with raw_entry_mut to avoid SmartString allocation on lookup
type FxBuildHasher = std::hash::BuildHasherDefault<FxHasher>;
let mut groups: hashbrown::HashMap<SmartString, StreamGroupState, FxBuildHasher> =
hashbrown::HashMap::with_capacity_and_hasher(64, FxBuildHasher::default());
let mut null_group: Option<StreamGroupState> = None;
let first_row = result.row();
// Process first row
let process_row =
|row: &Row,
groups: &mut hashbrown::HashMap<SmartString, StreamGroupState, FxBuildHasher>,
null_group: &mut Option<StreamGroupState>,
template: &StreamGroupState| {
let key_opt = match row.get(group_col_idx) {
Some(Value::Text(s)) => Some(s),
Some(Value::Null(_)) | None => None,
_ => return, // Skip non-text, non-NULL
};
let state = if let Some(key_str) = key_opt {
// OPTIMIZATION: Use raw_entry_mut to avoid SmartString allocation on lookup
// Only create SmartString when inserting a new group
let mut hasher = FxHasher::default();
std::hash::Hash::hash(key_str, &mut hasher);
let hash = hasher.finish();
let entry = groups
.raw_entry_mut()
.from_hash(hash, |k| k.as_str() == key_str);
match entry {
RawEntryMut::Occupied(o) => o.into_mut(),
RawEntryMut::Vacant(v) => {
v.insert_hashed_nocheck(
hash,
SmartString::new(key_str),
template.clone(),
)
.1
}
}
} else {
if null_group.is_none() {
*null_group = Some(template.clone());
}
null_group.as_mut().unwrap()
};
// Accumulate aggregates
for (i, agg) in simple_aggs.iter().enumerate() {
match agg {
SimpleAgg::Count(_) => {
if agg.count_includes_row(row) {
state.counts[i] += 1;
}
}
SimpleAgg::Sum(col_idx) | SimpleAgg::Avg(col_idx) => {
if let Some(value) = row.get(*col_idx) {
state.numeric_states[i].accumulate(value);
}
}
SimpleAgg::Min(col_idx) => {
if let Some(value) = row.get(*col_idx) {
if !value.is_null() {
match &state.min_values[i] {
None => state.min_values[i] = Some(value.clone()),
Some(current) if value < current => {
state.min_values[i] = Some(value.clone())
}
_ => {}
}
}
}
}
SimpleAgg::Max(col_idx) => {
if let Some(value) = row.get(*col_idx) {
if !value.is_null() {
match &state.max_values[i] {
None => state.max_values[i] = Some(value.clone()),
Some(current) if value > current => {
state.max_values[i] = Some(value.clone())
}
_ => {}
}
}
}
}
}
}
};
// Process first row (already fetched)
process_row(first_row, &mut groups, &mut null_group, &state_template);
// Stream through remaining rows
let mut streamed_rows = 1u64;
while result.next() {
if streamed_rows.is_multiple_of(100) {
ctx.check_cancelled()?;
}
let row = result.row();
process_row(row, &mut groups, &mut null_group, &state_template);
streamed_rows += 1;
}
if let Some(err) = result.last_error() {
return Err(err);
}
// Build result columns
let mut result_columns = Vec::with_capacity(1 + aggregations.len());
result_columns.push(group_col_name.clone());
for agg in &aggregations {
let col_name = if let Some(ref alias) = agg.alias {
alias.clone()
} else {
agg.get_expression_name()
};
result_columns.push(col_name);
}
// Build result rows
let build_row = |key_value: Value, state: StreamGroupState| -> Result<Row> {
let mut values: radixdb_core::CompactVec<Value> =
radixdb_core::CompactVec::with_capacity(1 + simple_aggs.len());
values.push(key_value);
for (i, agg) in simple_aggs.iter().enumerate() {
let value = match agg {
SimpleAgg::Count(_) => Value::Integer(state.counts[i]),
SimpleAgg::Sum(_) => state.numeric_states[i].sum_result()?,
SimpleAgg::Avg(_) => state.numeric_states[i].average_result()?,
SimpleAgg::Min(_) => state.min_values[i]
.clone()
.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer)),
SimpleAgg::Max(_) => state.max_values[i]
.clone()
.unwrap_or_else(|| Value::null(radixdb_core::DataType::Integer)),
};
values.push(value);
}
Ok(Row::from_compact_vec(values))
};
let mut result_rows = RowVec::with_capacity(groups.len() + 1);
let mut row_id = 0i64;
for (key, state) in groups.into_iter() {
result_rows.push((row_id, build_row(Value::Text(key), state)?));
row_id += 1;
}
if let Some(ng) = null_group {
result_rows.push((row_id, build_row(Value::null_unknown(), ng)?));
}
Ok(DerivedAggregationAttempt::Applied(Box::new(
crate::result::ExecutorResult::new(result_columns, result_rows),
)))
}
}
}