surrealdb-core 3.3.1

A scalable, distributed, collaborative, document-graph database, for the realtime web
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
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
//! Aggregate operator for GROUP BY processing.
//!
//! Collects all input rows into groups keyed by GROUP BY expressions,
//! then applies aggregate functions (COUNT, SUM, array::group, etc.)
//! to each group. This is a pipeline-breaking operator: the entire
//! input stream must be consumed before any output is produced.

use std::collections::BTreeMap;
use std::sync::Arc;

use common::future::stream::{self, Yielder};
use futures::StreamExt;

use crate::err::EngineError;
use crate::exec::function::{Accumulator, AggregateFunction};
use crate::exec::{
	AccessMode, ContextLevel, EvalContext, ExecOperator, ExecutionContext, FlowResult,
	FlowResultExt as _, OperatorMetrics, PhysicalExpr, ValueBatch, ValueBatchStream, buffer_stream,
	monitor_stream,
};
use crate::expr::idiom::Idiom;
use crate::val::{Object, Value};

/// Aggregates values by grouping keys.
///
/// GROUP BY collects all values into groups based on the specified keys,
/// then applies aggregate functions (COUNT, SUM, array::group, etc.) to each group.
///
/// This is a pipeline breaking operator - it must consume the entire input stream
/// before producing any output.
#[derive(Debug, Clone)]
pub struct Aggregate {
	pub(crate) input: Arc<dyn ExecOperator>,
	/// The original GROUP BY idioms (for display/debugging purposes).
	pub(crate) group_by: Vec<Idiom>,
	/// Physical expressions to evaluate for computing group keys.
	/// These are the actual expressions that determine grouping.
	/// For `GROUP BY country, year` where `year` is an alias for `time::year(time)`,
	/// this would contain expressions for `country` and `time::year(time)`.
	pub(crate) group_by_exprs: Vec<Arc<dyn PhysicalExpr>>,
	/// The aggregate expressions to compute for each group.
	/// These are the selected fields that may contain aggregate functions.
	pub(crate) aggregates: Vec<AggregateField>,
	/// Per-operator runtime metrics for EXPLAIN ANALYZE.
	pub(crate) metrics: Arc<OperatorMetrics>,
}

impl Aggregate {
	/// Create a new Aggregate operator.
	pub(crate) fn new(
		input: Arc<dyn ExecOperator>,
		group_by: Vec<Idiom>,
		group_by_exprs: Vec<Arc<dyn PhysicalExpr>>,
		aggregates: Vec<AggregateField>,
	) -> Self {
		Self {
			input,
			group_by,
			group_by_exprs,
			aggregates,
			metrics: Arc::new(OperatorMetrics::new()),
		}
	}
}

/// Represents a field in the SELECT that may be an aggregate.
#[derive(Debug, Clone)]
pub struct AggregateField {
	/// The output path for this field (e.g., ["address", "city"] for "address.city")
	/// This allows proper nested object construction.
	pub output_path: Vec<String>,
	/// Whether this field is a group-by key (passed through unchanged)
	pub is_group_key: bool,
	/// If this is a group-by key, the index into the group key vector.
	/// This allows retrieving the computed group key value directly.
	pub group_key_index: Option<usize>,
	/// Information about aggregate functions in this expression (if any).
	/// When set, the accumulator-based evaluation is used.
	/// Supports multiple aggregates per expression (e.g., `SUM(a) + AVG(a)`).
	pub aggregate_expr_info: Option<AggregateExprInfo>,
	/// Expression to evaluate for non-aggregate fields (e.g., group-by keys or first-value
	/// fields). This is used when aggregate_expr_info is None.
	pub fallback_expr: Option<Arc<dyn PhysicalExpr>>,
}

impl AggregateField {
	/// Create a new AggregateField from an output name string.
	///
	/// The name is treated as an opaque flat key. Callers that need a
	/// nested output path (e.g. for `AS foo.bar`) must supply the path
	/// components directly via [`AggregateField::with_output_path`] —
	/// typically by walking the parsed alias idiom's [`crate::expr::part::Part`]s
	/// so that `AS foo.bar` nests as `[foo, bar]` while
	/// `` AS `foo.bar` `` stays a single flat key `"foo.bar"`.
	pub fn new(
		name: String,
		is_group_key: bool,
		group_key_index: Option<usize>,
		aggregate_expr_info: Option<AggregateExprInfo>,
		fallback_expr: Option<Arc<dyn PhysicalExpr>>,
	) -> Self {
		Self::with_output_path(
			vec![name],
			is_group_key,
			group_key_index,
			aggregate_expr_info,
			fallback_expr,
		)
	}

	/// Create a new AggregateField from a pre-built nested output path.
	///
	/// Used when the output path was derived structurally from the parsed
	/// alias idiom, preserving the distinction between multi-part aliases
	/// (`AS foo.bar` → `["foo", "bar"]`) and single-part aliases whose
	/// identifier happens to contain a dot (`` AS `foo.bar` `` →
	/// `["foo.bar"]`).
	pub fn with_output_path(
		output_path: Vec<String>,
		is_group_key: bool,
		group_key_index: Option<usize>,
		aggregate_expr_info: Option<AggregateExprInfo>,
		fallback_expr: Option<Arc<dyn PhysicalExpr>>,
	) -> Self {
		Self {
			output_path,
			is_group_key,
			group_key_index,
			aggregate_expr_info,
			fallback_expr,
		}
	}

	/// Check if this is an empty name (used for SELECT VALUE with GROUP BY)
	pub fn is_empty_name(&self) -> bool {
		self.output_path.len() == 1 && self.output_path[0].is_empty()
	}
}

/// Information about all aggregates extracted from a single SELECT expression.
///
/// Supports expressions with multiple aggregates like `SUM(a) + AVG(a)`.
/// Each aggregate is extracted and assigned a synthetic field name (`_a0`, `_a1`, etc.).
/// The original expression is transformed to reference these fields.
#[derive(Clone)]
pub struct AggregateExprInfo {
	/// All extracted aggregate functions, indexed by their position.
	/// For `SUM(a) + AVG(a)`, this would contain `[SUM(a), AVG(a)]`.
	pub aggregates: Vec<ExtractedAggregate>,

	/// The transformed expression with aggregates replaced by field references.
	/// Uses synthetic field names like `_a0`, `_a1` that correspond to
	/// indices in the `aggregates` vector.
	/// None if the expression is a direct single aggregate (no transformation needed).
	pub post_expr: Option<Arc<dyn PhysicalExpr>>,
}

impl std::fmt::Debug for AggregateExprInfo {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("AggregateExprInfo")
			.field("num_aggregates", &self.aggregates.len())
			.field("has_post_expr", &self.post_expr.is_some())
			.finish()
	}
}

/// A single aggregate function extracted from an expression.
#[derive(Clone)]
pub struct ExtractedAggregate {
	/// The aggregate function from the registry.
	pub function: Arc<dyn AggregateFunction>,
	/// The expression to evaluate per-row to get the value to accumulate.
	/// For `math::mean(a)`, this would be the expression for `a`.
	pub argument_expr: Arc<dyn PhysicalExpr>,
	/// Additional arguments (evaluated once per group, not per-row).
	/// For `array::join(txt, " ")`, this would contain the separator expression.
	pub extra_args: Vec<Arc<dyn PhysicalExpr>>,
}

impl std::fmt::Debug for ExtractedAggregate {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("ExtractedAggregate").field("function", &self.function.name()).finish()
	}
}

/// Generate a synthetic field name for an aggregate at the given index.
/// These names are used in the transformed expression to reference aggregate results.
pub fn aggregate_field_name(idx: usize) -> String {
	format!("_a{}", idx)
}

/// Key for grouping - a tuple of values corresponding to GROUP BY expressions
type GroupKey = Vec<Value>;

/// Per-group aggregate state using accumulators
struct GroupState {
	/// Accumulators for each aggregate field.
	/// For fields with multiple aggregates (e.g., `SUM(a) + AVG(a)`),
	/// this contains a Vec of accumulators, one per extracted aggregate.
	/// Empty Vec for non-aggregate fields.
	accumulators: Vec<Vec<Box<dyn Accumulator>>>,
	/// First values seen for non-aggregate fields
	first_values: Vec<Value>,
}

// ---------------------------------------------------------------------------
// Group map
// ---------------------------------------------------------------------------
//
// Two rows belong to the same group iff their group keys compare equal under
// `Value: Ord`, so the map has to be *ordered*. Hash-partitioning the keys is
// not an option, because `Value: Hash` is not consistent with `Value: Eq` for
// numbers: `Number`'s comparison is cross-variant and, between `Float` and
// `Decimal`, approximate — `0.1f == 0.1dec` while `0.11111f != 0.11111dec` —
// whereas `Number: Hash` hashes each variant's exact decimal expansion. So
// `0.1f` and `0.1dec` bucket apart and silently form two groups. No canonical
// form reproduces that comparison, which is why the identity relation here is
// `Ord` and not a hash.
//
// Ordered keys also make the group order deterministic, and they let the drain
// feed results downstream already sorted by key.
//
// `Vec<Value>: Borrow<[Value]>`, so a row probes with the borrowed slice its key
// values already live in and nothing is cloned unless the row opens a new group.
//
// The keys are held apart from the states, mapping each key to an index into
// `states`, because a `BTreeMap<GroupKey, GroupState>` cannot answer
// get-or-insert in one descent on stable Rust: returning the `get_mut` borrow
// from the hit branch keeps it live across the insert branch (E0499, NLL problem
// case 3), so a single map costs either two descents per row (`contains_key`
// then `get_mut`) or a key clone per row (`entry(key.to_vec())`) — the clone
// being the cost this layout exists to avoid. Copying the `usize` out ends the
// borrow, so the hit path is one descent plus one indexed load.
//
// Spill-to-disk for high-cardinality groups is a future change.
struct GroupMap {
	index: BTreeMap<GroupKey, usize>,
	states: Vec<GroupState>,
}

impl GroupMap {
	fn new() -> Self {
		Self {
			index: BTreeMap::new(),
			states: Vec::new(),
		}
	}

	fn is_empty(&self) -> bool {
		self.states.is_empty()
	}

	/// Look up the state for `key`, creating it if absent.
	///
	/// `key` borrows the row's already-evaluated group-by values — empty for
	/// GROUP ALL. An owned [`GroupKey`] is built only when the row opens a new
	/// group.
	fn entry_for_row<F>(&mut self, key: &[Value], create: F) -> &mut GroupState
	where
		F: FnOnce() -> GroupState,
	{
		let idx = match self.index.get(key) {
			Some(idx) => *idx,
			None => {
				let idx = self.states.len();
				self.states.push(create());
				self.index.insert(key.to_vec(), idx);
				idx
			}
		};
		&mut self.states[idx]
	}

	/// Drain the map in group-key order.
	///
	/// `index` is the key-ordered side, so it drives the iteration and claims each
	/// state by index; every index appears exactly once, which is what lets the
	/// states be taken rather than cloned.
	fn into_key_order(self) -> impl ExactSizeIterator<Item = (GroupKey, GroupState)> {
		let mut states: Vec<Option<GroupState>> = self.states.into_iter().map(Some).collect();
		self.index
			.into_iter()
			.map(move |(key, idx)| (key, states[idx].take().expect("each index appears once")))
	}
}

/// Lay per-expression group-key columns out row-major, so each row's key is a
/// contiguous slice the group probe can borrow.
///
/// `columns[expr_idx][row_idx]` is the evaluated value for group-by expression
/// `expr_idx` at row `row_idx`. The result holds `row_count` consecutive runs of
/// `columns.len()` values, so row `i`'s key is
/// `[i * columns.len() .. (i + 1) * columns.len()]` — the one caller-visible
/// invariant, and the reason this function owns the layout rather than the call
/// site. `columns` is consumed: values are moved out, and a single column is
/// taken whole because it is already the row-major form.
fn group_key_rows(columns: &mut Vec<Vec<Value>>, row_count: usize) -> Vec<Value> {
	if columns.len() == 1 {
		return columns.swap_remove(0);
	}
	let mut flat = Vec::with_capacity(row_count * columns.len());
	for row_idx in 0..row_count {
		for col in columns.iter_mut() {
			flat.push(std::mem::take(&mut col[row_idx]));
		}
	}
	flat
}

impl ExecOperator for Aggregate {
	fn name(&self) -> &'static str {
		"Aggregate"
	}

	fn attrs(&self) -> Vec<(String, String)> {
		use surrealdb_types::ToSql;
		if self.group_by.is_empty() {
			vec![("mode".to_string(), "GROUP ALL".to_string())]
		} else {
			vec![(
				"by".to_string(),
				self.group_by.iter().map(|i| i.to_sql()).collect::<Vec<_>>().join(", "),
			)]
		}
	}

	fn required_context(&self) -> ContextLevel {
		// Combine group-by and aggregate expression contexts with child operator context
		let group_ctx = self
			.group_by_exprs
			.iter()
			.map(|e| e.required_context())
			.max()
			.unwrap_or(ContextLevel::Root);
		let agg_ctx = self
			.aggregates
			.iter()
			.map(|agg| {
				let info_ctx = agg
					.aggregate_expr_info
					.as_ref()
					.map(|info| {
						let agg_arg_ctx = info
							.aggregates
							.iter()
							.flat_map(|ext| {
								std::iter::once(ext.argument_expr.required_context())
									.chain(ext.extra_args.iter().map(|e| e.required_context()))
							})
							.max()
							.unwrap_or(ContextLevel::Root);
						let post_ctx = info
							.post_expr
							.as_ref()
							.map(|e| e.required_context())
							.unwrap_or(ContextLevel::Root);
						agg_arg_ctx.max(post_ctx)
					})
					.unwrap_or(ContextLevel::Root);
				let fallback_ctx = agg
					.fallback_expr
					.as_ref()
					.map(|e| e.required_context())
					.unwrap_or(ContextLevel::Root);
				info_ctx.max(fallback_ctx)
			})
			.max()
			.unwrap_or(ContextLevel::Root);
		group_ctx.max(agg_ctx).max(self.input.required_context())
	}

	fn access_mode(&self) -> AccessMode {
		// Combine input's access mode with aggregate expression modes
		let mut mode = self.input.access_mode();
		// Include group-by expressions
		for expr in &self.group_by_exprs {
			mode = mode.combine(expr.access_mode());
		}
		for agg in &self.aggregates {
			if let Some(info) = &agg.aggregate_expr_info {
				// Check all extracted aggregates
				for extracted in &info.aggregates {
					mode = mode.combine(extracted.argument_expr.access_mode());
					for extra_arg in &extracted.extra_args {
						mode = mode.combine(extra_arg.access_mode());
					}
				}
				// Check post-expression
				if let Some(post_expr) = &info.post_expr {
					mode = mode.combine(post_expr.access_mode());
				}
			}
			if let Some(expr) = &agg.fallback_expr {
				mode = mode.combine(expr.access_mode());
			}
		}
		mode
	}

	fn children(&self) -> Vec<&Arc<dyn ExecOperator>> {
		vec![&self.input]
	}

	fn metrics(&self) -> Option<&OperatorMetrics> {
		Some(&self.metrics)
	}

	fn expressions(&self) -> Vec<(&str, &Arc<dyn PhysicalExpr>)> {
		let mut exprs = Vec::new();
		for expr in &self.group_by_exprs {
			exprs.push(("group_by", expr));
		}
		for agg in &self.aggregates {
			if let Some(info) = &agg.aggregate_expr_info {
				for extracted in &info.aggregates {
					exprs.push(("agg_arg", &extracted.argument_expr));
					for extra in &extracted.extra_args {
						exprs.push(("agg_extra", extra));
					}
				}
				if let Some(post) = &info.post_expr {
					exprs.push(("agg_post_expr", post));
				}
			}
			if let Some(fallback) = &agg.fallback_expr {
				exprs.push(("agg_fallback", fallback));
			}
		}
		exprs
	}

	fn execute(&self, ctx: &ExecutionContext) -> FlowResult<ValueBatchStream> {
		let mut input_stream = buffer_stream(
			self.input.execute(ctx)?,
			self.input.access_mode(),
			self.input.cardinality_hint(),
			ctx.root().ctx.config.exec.operator_buffer_size,
		);
		let group_by_exprs = self.group_by_exprs.clone();
		let aggregates = self.aggregates.clone();
		let ctx = ctx.clone();

		// Collect all input batches, then group and aggregate
		let stream = stream::try_async_stream(async move |mut yielder: Yielder<_>| {
			// Pre-evaluate extra_args for each aggregate (evaluated once, not per-row)
			// This is needed for functions like array::join(txt, " ") where " " is evaluated once
			// Structure: evaluated_extra_args[field_idx][aggregate_idx] = Vec<Value>
			let eval_ctx_for_args = EvalContext::from_exec_ctx(&ctx);
			let mut evaluated_extra_args: Vec<Vec<Vec<Value>>> =
				Vec::with_capacity(aggregates.len());
			for agg in &aggregates {
				if let Some(info) = &agg.aggregate_expr_info {
					let mut field_args = Vec::with_capacity(info.aggregates.len());
					for extracted in &info.aggregates {
						let mut args = Vec::with_capacity(extracted.extra_args.len());
						for extra_arg in &extracted.extra_args {
							let value =
								extra_arg.evaluate(eval_ctx_for_args.clone()).await.or_none()?;
							args.push(value);
						}
						field_args.push(args);
					}
					evaluated_extra_args.push(field_args);
				} else {
					evaluated_extra_args.push(vec![]);
				}
			}

			// Accumulate all values into groups. See `GroupMap` for why the group
			// identity relation has to be `Ord` rather than a hash, and how the
			// per-row probe avoids cloning the group-by values.
			let mut groups = GroupMap::new();

			// Consume all input batches
			while let Some(batch_result) = input_stream.next().await {
				// Check for cancellation between batches
				if ctx.cancellation().is_cancelled() {
					Err(crate::expr::ControlFlow::Err(anyhow::anyhow!(
						EngineError::QueryCancelled
					)))?;
				}
				let batch = batch_result?;
				let eval_ctx = EvalContext::from_exec_ctx(&ctx);

				// Phase 1: Batch evaluate group-by key expressions across all rows
				let mut group_key_columns: Vec<Vec<Value>> =
					Vec::with_capacity(group_by_exprs.len());
				for expr in &group_by_exprs {
					let keys = match expr.evaluate_batch(eval_ctx.clone(), batch.values()).await {
						Ok(v) => v,
						Err(_) => {
							// Fallback: evaluate per-row, replacing ignorable errors with None
							let mut keys = Vec::with_capacity(batch.len());
							for value in batch.values() {
								let v =
									expr.evaluate(eval_ctx.with_value(value)).await.or_none()?;
								keys.push(v);
							}
							keys
						}
					};
					group_key_columns.push(keys);
				}

				// Phase 2: Batch evaluate aggregate argument expressions
				let mut agg_arg_columns: Vec<Vec<Vec<Value>>> =
					Vec::with_capacity(aggregates.len());
				for agg in &aggregates {
					if let Some(info) = &agg.aggregate_expr_info {
						let mut field_cols = Vec::with_capacity(info.aggregates.len());
						for extracted in &info.aggregates {
							let col = match extracted
								.argument_expr
								.evaluate_batch(eval_ctx.clone(), batch.values())
								.await
							{
								Ok(v) => v,
								Err(_) => {
									// Fallback: evaluate per-row, replacing ignorable errors with
									// None
									let mut col = Vec::with_capacity(batch.len());
									for value in batch.values() {
										let v = extracted
											.argument_expr
											.evaluate(eval_ctx.with_value(value))
											.await
											.or_none()?;
										col.push(v);
									}
									col
								}
							};
							field_cols.push(col);
						}
						agg_arg_columns.push(field_cols);
					} else {
						agg_arg_columns.push(vec![]);
					}
				}

				// Phase 3: Dispatch rows to groups and update accumulators
				if group_by_exprs.is_empty() {
					// GROUP ALL fast path: single group, pass entire columns
					// to update_batch to avoid per-row virtual dispatch.
					let state = groups.entry_for_row(&[], || {
						create_group_state(&aggregates, &evaluated_extra_args)
					});

					for (field_idx, agg) in aggregates.iter().enumerate() {
						if agg.is_group_key {
							continue;
						}

						if agg.aggregate_expr_info.is_some() {
							for (agg_idx, arg_col) in agg_arg_columns[field_idx].iter().enumerate()
							{
								if let Some(acc) = state.accumulators[field_idx].get_mut(agg_idx)
									&& let Err(e) = acc.update_batch(arg_col)
								{
									tracing::debug!(error = %e, "Accumulator batch update failed, skipping batch");
								}
							}
						} else if let Some(expr) = &agg.fallback_expr {
							// Non-aggregate field - store first value
							if state.first_values[field_idx].is_none()
								&& let Some(first_value) = batch.values().first()
							{
								match expr.evaluate(eval_ctx.with_value(first_value)).await {
									Ok(field_value) => {
										state.first_values[field_idx] = field_value;
									}
									Err(cf) if cf.is_ignorable() => {
										tracing::debug!(error = %cf, "Fallback expression evaluation failed (ignorable)");
									}
									Err(cf) => Err(cf)?,
								}
							}
						}
					}
				} else {
					// GROUP BY: per-row dispatch to separate groups. Each row's
					// key is a contiguous run in `key_rows`, so the probe borrows it
					// and only clones when the row opens a new group.
					let key_width = group_key_columns.len();
					let key_rows = group_key_rows(&mut group_key_columns, batch.len());

					for (row_idx, value) in batch.values().iter().enumerate() {
						let key = &key_rows[row_idx * key_width..(row_idx + 1) * key_width];
						let state = groups.entry_for_row(key, || {
							create_group_state(&aggregates, &evaluated_extra_args)
						});

						for (field_idx, agg) in aggregates.iter().enumerate() {
							if agg.is_group_key {
								continue;
							}

							if agg.aggregate_expr_info.is_some() {
								// Use pre-computed aggregate argument values
								for (agg_idx, arg_col) in
									agg_arg_columns[field_idx].iter().enumerate()
								{
									let arg_value = arg_col[row_idx].clone();
									if let Some(acc) =
										state.accumulators[field_idx].get_mut(agg_idx)
										&& let Err(e) = acc.update(arg_value)
									{
										tracing::debug!(error = %e, "Accumulator update failed, skipping value");
									}
								}
							} else if let Some(expr) = &agg.fallback_expr {
								// Non-aggregate field - store first value (per-row, lazy)
								if state.first_values[field_idx].is_none() {
									match expr.evaluate(eval_ctx.with_value(value)).await {
										Ok(field_value) => {
											state.first_values[field_idx] = field_value;
										}
										Err(cf) if cf.is_ignorable() => {
											tracing::debug!(error = %cf, "Fallback expression evaluation failed (ignorable)");
										}
										Err(cf) => Err(cf)?,
									}
								}
							}
						}
					}
				}
			}

			// GROUP ALL on empty input: produce one row with default aggregate
			// values (e.g. COUNT() = 0) when the scan ran in an authorised
			// context.  When permission checks are active and 0 rows passed
			// filtering the old compute path returns [] — replicate that.
			if group_by_exprs.is_empty() && groups.is_empty() {
				let perms_active = ctx.should_check_perms(crate::iam::Action::View).unwrap_or(true);
				if !perms_active {
					groups.entry_for_row(&[], || {
						create_group_state(&aggregates, &evaluated_extra_args)
					});
				}
			}

			// Now compute final results for each group, in group-key order.
			let ordered_groups = groups.into_key_order();
			let mut results = Vec::with_capacity(ordered_groups.len());
			for (group_key, state) in ordered_groups {
				let result =
					compute_group_result_async(&group_key, state, &aggregates, &ctx).await?;
				results.push(result);
			}

			yielder.emit(ValueBatch::new(results)).await;
			Ok(())
		});

		Ok(monitor_stream(Box::pin(stream), "Aggregate", &self.metrics))
	}
}

/// Create initial group state with accumulators for each aggregate field.
///
/// The `evaluated_extra_args` parameter contains the pre-evaluated extra arguments
/// for each aggregate field: `evaluated_extra_args[field_idx][aggregate_idx] = Vec<Value>`.
fn create_group_state(
	aggregates: &[AggregateField],
	evaluated_extra_args: &[Vec<Vec<Value>>],
) -> GroupState {
	let accumulators = aggregates
		.iter()
		.enumerate()
		.map(|(i, agg)| {
			if let Some(info) = &agg.aggregate_expr_info {
				// Create an accumulator for each extracted aggregate
				info.aggregates
					.iter()
					.enumerate()
					.map(|(agg_idx, extracted)| {
						let extra_args = evaluated_extra_args
							.get(i)
							.and_then(|field_args| field_args.get(agg_idx))
							.map(|v| v.as_slice())
							.unwrap_or(&[]);
						extracted.function.create_accumulator_with_args(extra_args)
					})
					.collect()
			} else {
				// Non-aggregate field - no accumulators
				vec![]
			}
		})
		.collect();

	let first_values = aggregates.iter().map(|_| Value::None).collect();

	GroupState {
		accumulators,
		first_values,
	}
}

/// Compute the value for a single aggregate field.
///
/// This handles three cases:
/// 1. Group-by key: return the key value from the group key vector
/// 2. Aggregate expression: finalize accumulators and optionally evaluate post-expression
/// 3. Non-aggregate field: return the first value seen
async fn compute_single_field_value(
	agg: &AggregateField,
	group_key: &GroupKey,
	accumulators: &[Box<dyn Accumulator>],
	first_value: Value,
	ctx: &ExecutionContext,
) -> FlowResult<Value> {
	if let Some(idx) = agg.group_key_index {
		// For group-by keys, use the key value directly by index
		Ok(group_key.get(idx).cloned().unwrap_or(Value::None))
	} else if let Some(info) = &agg.aggregate_expr_info {
		// Compute the aggregate value(s)
		compute_aggregate_field_value(info, accumulators, ctx).await
	} else {
		// Return first value for non-aggregate fields
		Ok(first_value)
	}
}

/// Compute the result value for a single group, with support for multiple aggregates per field.
///
/// For expressions like `SUM(a) + AVG(a)`:
/// 1. Finalize all accumulators to get `{ _a0: sum_value, _a1: avg_value }`
/// 2. Evaluate the post-expression against this document to get the final value
async fn compute_group_result_async(
	group_key: &GroupKey,
	state: GroupState,
	aggregates: &[AggregateField],
	ctx: &ExecutionContext,
) -> FlowResult<Value> {
	// Special case: SELECT VALUE with GROUP BY
	// If there's exactly one aggregate with an empty name, return the raw value
	if aggregates.len() == 1 && aggregates[0].is_empty_name() {
		let agg = &aggregates[0];
		let first_value = state.first_values.into_iter().next().unwrap_or(Value::None);
		let accumulators = state.accumulators.into_iter().next().unwrap_or_default();
		return compute_single_field_value(agg, group_key, &accumulators, first_value, ctx).await;
	}

	// Normal case: return an object with field names
	let mut result = Object::default();

	// Zip aggregates with their corresponding accumulators and first values
	let field_data = aggregates.iter().zip(state.accumulators).zip(state.first_values);

	for ((agg, accumulators), first_value) in field_data {
		let field_value =
			compute_single_field_value(agg, group_key, &accumulators, first_value, ctx).await?;

		// Use nested setting to properly construct nested objects
		// e.g., path ["address", "city"] creates { address: { city: value } }
		set_nested_value(&mut result, &agg.output_path, field_value);
	}

	Ok(Value::Object(result))
}

/// Set a value at a nested path in an object.
///
/// For a path like ["address", "city"], this creates or updates:
/// `{ address: { city: value } }`
fn set_nested_value(obj: &mut Object, path: &[String], value: Value) {
	if path.is_empty() {
		return;
	}

	if path.len() == 1 {
		// Simple case: just insert at this level
		obj.insert(path[0].clone(), value);
		return;
	}

	// Need to traverse/create nested structure
	let key = &path[0];
	let rest = &path[1..];

	// Get or create the nested object
	let nested = obj.entry(key.clone()).or_insert_with(|| Value::Object(Object::default()));

	match nested {
		Value::Object(nested_obj) => {
			set_nested_value(nested_obj, rest, value);
		}
		_ => {
			// Replace non-object with new object containing the nested path
			let mut new_obj = Object::default();
			set_nested_value(&mut new_obj, rest, value);
			*nested = Value::Object(new_obj);
		}
	}
}

/// Compute the final value for a field with aggregate expressions.
///
/// If there's a post_expr, builds a document with all aggregate results
/// and evaluates the expression against it. Otherwise returns the single
/// aggregate value directly.
async fn compute_aggregate_field_value(
	info: &AggregateExprInfo,
	accumulators: &[Box<dyn Accumulator>],
	ctx: &ExecutionContext,
) -> FlowResult<Value> {
	if info.aggregates.is_empty() {
		return Ok(Value::Null);
	}

	// Finalize all accumulators and build the aggregate document
	let mut agg_doc = Object::default();
	for (idx, acc) in accumulators.iter().enumerate() {
		let value = match acc.finalize() {
			Ok(v) => v,
			Err(e) => {
				tracing::debug!(error = %e, idx, "Accumulator finalize failed, using Null");
				Value::Null
			}
		};
		agg_doc.insert(aggregate_field_name(idx), value);
	}

	// If there's a post-expression, evaluate it against the aggregate document
	if let Some(post_expr) = &info.post_expr {
		let doc_value = Value::Object(agg_doc);
		let eval_ctx = EvalContext::from_exec_ctx(ctx).with_value(&doc_value);
		match post_expr.evaluate(eval_ctx).await {
			Ok(v) => Ok(v),
			Err(cf) if cf.is_ignorable() => {
				tracing::debug!(error = %cf, "Post-expression evaluation failed (ignorable), using Null");
				Ok(Value::Null)
			}
			Err(cf) => Err(cf),
		}
	} else {
		// No post-expression means direct single aggregate - return first value
		Ok(agg_doc.0.into_values().next().unwrap_or(Value::Null))
	}
}

#[cfg(test)]
mod tests {
	use std::collections::hash_map::DefaultHasher;
	use std::hash::{Hash, Hasher};
	use std::str::FromStr;

	use rust_decimal::Decimal;

	use super::*;
	use crate::exec::field_path::FieldPath;
	use crate::exec::operators::test_util::{
		ValuesOperator, collect, parse_expr, physical_expr, root_ctx, root_ctx_with_auth,
		try_collect, val,
	};
	use crate::exec::operators::{SortDirection, Union};
	use crate::exec::ordering::SortProperty;
	use crate::exec::{CardinalityHint, OutputOrdering};
	use crate::expr::{ControlFlow, Expr};
	use crate::iam::{Action, Auth, Role};
	use crate::val::Number;

	// =========================================================================
	// Builders
	// =========================================================================

	/// Build the operator over `input`. `group_by` holds the GROUP BY sources;
	/// an empty slice is GROUP ALL. Non-idiom sources are given a display idiom
	/// spelled from the source text, the way the planner does for computed keys.
	async fn aggregate_over(
		input: Arc<dyn ExecOperator>,
		group_by: &[&str],
		fields: Vec<AggregateField>,
		ctx: &ExecutionContext,
	) -> Arc<dyn ExecOperator> {
		let mut idioms = Vec::with_capacity(group_by.len());
		let mut exprs = Vec::with_capacity(group_by.len());
		for src in group_by {
			idioms.push(match parse_expr(src) {
				Expr::Idiom(idiom) => idiom,
				_ => Idiom::field(src.to_string()),
			});
			exprs.push(physical_expr(src, ctx).await);
		}
		Arc::new(Aggregate::new(input, idioms, exprs, fields))
	}

	/// Input rows from SurrealQL object literals.
	async fn rows(srcs: &[&str]) -> Vec<Value> {
		let mut out = Vec::with_capacity(srcs.len());
		for src in srcs {
			out.push(val(src).await);
		}
		out
	}

	/// A field holding one aggregate call and no post-expression — the shape the
	/// planner produces for a bare `func(arg)` selector.
	fn single(name: &str, extracted: ExtractedAggregate) -> AggregateField {
		AggregateField::new(
			name.to_string(),
			false,
			None,
			Some(AggregateExprInfo {
				aggregates: vec![extracted],
				post_expr: None,
			}),
			None,
		)
	}

	/// `count()` — the argument-less form. The planner compiles a NONE literal as
	/// the per-row argument, so the accumulator ticks once per row whatever the
	/// row holds.
	async fn count_star(name: &str, ctx: &ExecutionContext) -> AggregateField {
		single(
			name,
			ExtractedAggregate {
				function: ctx.function_registry().get_count_aggregate(false),
				argument_expr: physical_expr("NONE", ctx).await,
				extra_args: vec![],
			},
		)
	}

	/// `count(arg)` — the truthy-counting form.
	async fn count_of(name: &str, arg: &str, ctx: &ExecutionContext) -> AggregateField {
		single(
			name,
			ExtractedAggregate {
				function: ctx.function_registry().get_count_aggregate(true),
				argument_expr: physical_expr(arg, ctx).await,
				extra_args: vec![],
			},
		)
	}

	/// A registry aggregate applied to one per-row argument, e.g.
	/// `agg_of("total", "math::sum", "score", ctx)`.
	async fn agg_of(name: &str, func: &str, arg: &str, ctx: &ExecutionContext) -> AggregateField {
		single(
			name,
			ExtractedAggregate {
				function: aggregate_fn(func, ctx),
				argument_expr: physical_expr(arg, ctx).await,
				extra_args: vec![],
			},
		)
	}

	fn aggregate_fn(func: &str, ctx: &ExecutionContext) -> Arc<dyn AggregateFunction> {
		Arc::clone(
			ctx.function_registry().get_aggregate(func).expect("aggregate should be registered"),
		)
	}

	/// A pass-through field that republishes group key `idx`.
	fn key(name: &str, idx: usize) -> AggregateField {
		AggregateField::new(name.to_string(), true, Some(idx), None, None)
	}

	/// A non-aggregate field: the first non-NONE value seen in the group.
	async fn first_value(name: &str, src: &str, ctx: &ExecutionContext) -> AggregateField {
		AggregateField::new(
			name.to_string(),
			false,
			None,
			None,
			Some(physical_expr(src, ctx).await),
		)
	}

	/// Read a top-level field from an output row.
	fn field(row: &Value, name: &str) -> Value {
		match row {
			Value::Object(o) => o.get(name).cloned().unwrap_or(Value::None),
			other => panic!("expected an object row, got {other:?}"),
		}
	}

	/// A source that replays a fixed script of batches and signals. Single-use:
	/// `execute` takes the script, so each instance may be executed once.
	struct ScriptedSource {
		script: std::sync::Mutex<Option<Vec<FlowResult<ValueBatch>>>>,
		access_mode: AccessMode,
	}

	impl ScriptedSource {
		/// Returns a trait object rather than `Self`: every operator builder in
		/// exec hands back `Arc<dyn ExecOperator>`.
		#[allow(clippy::new_ret_no_self)]
		fn new(script: Vec<FlowResult<ValueBatch>>) -> Arc<dyn ExecOperator> {
			Arc::new(Self {
				script: std::sync::Mutex::new(Some(script)),
				access_mode: AccessMode::ReadOnly,
			})
		}

		fn read_write(script: Vec<FlowResult<ValueBatch>>) -> Arc<dyn ExecOperator> {
			Arc::new(Self {
				script: std::sync::Mutex::new(Some(script)),
				access_mode: AccessMode::ReadWrite,
			})
		}
	}

	impl std::fmt::Debug for ScriptedSource {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			f.write_str("ScriptedSource")
		}
	}

	impl ExecOperator for ScriptedSource {
		fn name(&self) -> &'static str {
			"ScriptedSource"
		}

		fn required_context(&self) -> ContextLevel {
			ContextLevel::Root
		}

		fn access_mode(&self) -> AccessMode {
			self.access_mode
		}

		fn execute(&self, _ctx: &ExecutionContext) -> FlowResult<ValueBatchStream> {
			let script = self
				.script
				.lock()
				.expect("script lock")
				.take()
				.expect("ScriptedSource is single-use");
			Ok(Box::pin(futures::stream::iter(script)))
		}
	}

	fn batch(values: Vec<Value>) -> FlowResult<ValueBatch> {
		Ok(ValueBatch::new(values))
	}

	// =========================================================================
	// Grouping
	// =========================================================================

	#[tokio::test]
	async fn rows_collapse_into_one_output_row_per_group_key() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&[
				"{ country: 'us', score: 1 }",
				"{ country: 'de', score: 2 }",
				"{ country: 'us', score: 3 }",
				"{ country: 'us', score: 4 }",
			])
			.await,
		);
		let op = aggregate_over(
			input,
			&["country"],
			vec![
				key("country", 0),
				count_star("total", &ctx).await,
				agg_of("sum", "math::sum", "score", &ctx).await,
			],
			&ctx,
		)
		.await;

		let out = collect(&op, &ctx).await;
		assert_eq!(
			out,
			rows(&["{ country: 'de', total: 1, sum: 2 }", "{ country: 'us', total: 3, sum: 8 }"])
				.await
		);
	}

	#[tokio::test]
	async fn multi_field_group_keys_partition_on_the_whole_tuple() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&[
				"{ c: 'us', y: 2020 }",
				"{ c: 'us', y: 2021 }",
				"{ c: 'de', y: 2020 }",
				"{ c: 'us', y: 2020 }",
			])
			.await,
		);
		let op = aggregate_over(
			input,
			&["c", "y"],
			vec![key("c", 0), key("y", 1), count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		// Three distinct (c, y) tuples. The key tuple orders the output: the
		// second component only breaks ties within an equal first component.
		let out = collect(&op, &ctx).await;
		assert_eq!(
			out,
			rows(&[
				"{ c: 'de', y: 2020, total: 1 }",
				"{ c: 'us', y: 2020, total: 2 }",
				"{ c: 'us', y: 2021, total: 1 }",
			])
			.await
		);
	}

	#[tokio::test]
	async fn a_row_missing_a_group_field_groups_under_none() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&["{ c: 'us' }", "{ score: 1 }", "{ score: 2 }", "{ c: 'us' }"]).await,
		);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		// The two rows without `c` share the NONE key, and NONE sorts before
		// every other value.
		let out = collect(&op, &ctx).await;
		assert_eq!(out.len(), 2);
		assert_eq!(field(&out[0], "c"), Value::None);
		assert_eq!(field(&out[0], "total"), Value::from(2));
		assert_eq!(field(&out[1], "c"), Value::from("us"));
		assert_eq!(field(&out[1], "total"), Value::from(2));
	}

	#[tokio::test]
	async fn none_and_null_group_keys_stay_distinct_groups() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ c: NULL }", "{ }", "{ c: 'us' }", "{ c: NULL }"]).await);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		// NONE and NULL are different values, so they never share a group; the
		// output follows Value's total order, NONE < NULL < string.
		let out = collect(&op, &ctx).await;
		assert_eq!(out.len(), 3);
		assert_eq!(field(&out[0], "c"), Value::None);
		assert_eq!(field(&out[0], "total"), Value::from(1));
		assert_eq!(field(&out[1], "c"), Value::Null);
		assert_eq!(field(&out[1], "total"), Value::from(2));
		assert_eq!(field(&out[2], "c"), Value::from("us"));
		assert_eq!(field(&out[2], "total"), Value::from(1));
	}

	// =========================================================================
	// GROUP ALL
	// =========================================================================

	#[tokio::test]
	async fn group_all_folds_every_row_into_a_single_output_row() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ score: 1 }", "{ score: 2 }", "{ score: 3 }"]).await);
		let op = aggregate_over(
			input,
			&[],
			vec![count_star("total", &ctx).await, agg_of("sum", "math::sum", "score", &ctx).await],
			&ctx,
		)
		.await;

		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ total: 3, sum: 6 }"]).await);
	}

	#[tokio::test]
	async fn group_all_with_no_input_batches_emits_the_accumulator_identities_when_perms_are_off() {
		// An input that never yields a batch leaves the group map empty. With
		// permission enforcement off, the operator then synthesises the single
		// group, so `count()` reports 0 rather than yielding no row at all.
		let ctx = root_ctx_with_auth(Auth::for_root(Role::Owner));
		assert!(!ctx.should_check_perms(Action::View).unwrap());

		let op = aggregate_over(
			ScriptedSource::new(vec![]),
			&[],
			vec![count_star("total", &ctx).await, agg_of("sum", "math::sum", "score", &ctx).await],
			&ctx,
		)
		.await;

		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ total: 0, sum: 0 }"]).await);
	}

	#[tokio::test]
	async fn group_all_with_no_input_batches_emits_nothing_when_perms_are_on() {
		// Under an identity whose reads are permission-checked, zero rows cannot
		// be distinguished from "everything was filtered out", so the identity
		// row is suppressed and the output is empty.
		let ctx = root_ctx();
		assert!(ctx.should_check_perms(Action::View).unwrap());

		let op = aggregate_over(
			ScriptedSource::new(vec![]),
			&[],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		assert!(collect(&op, &ctx).await.is_empty());
	}

	#[tokio::test]
	async fn group_all_emits_the_identity_row_for_an_empty_batch_whatever_the_perms() {
		// The permission gate above only guards the *no batch at all* case. As
		// soon as one batch arrives the GROUP ALL branch creates its single group
		// before looking at any row, so an input that yields one empty batch
		// produces `count() = 0` even while permission checks are active.
		let ctx = root_ctx();
		assert!(ctx.should_check_perms(Action::View).unwrap());

		let op = aggregate_over(
			ScriptedSource::new(vec![batch(vec![])]),
			&[],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		assert_eq!(collect(&op, &ctx).await, rows(&["{ total: 0 }"]).await);
	}

	#[tokio::test]
	async fn group_by_over_empty_input_emits_nothing() {
		// The identity-row fallback is GROUP ALL only: with grouping keys and no
		// rows there is no group to report.
		let ctx = root_ctx_with_auth(Auth::for_root(Role::Owner));
		let op = aggregate_over(
			ScriptedSource::new(vec![batch(vec![])]),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		assert!(collect(&op, &ctx).await.is_empty());
	}

	// =========================================================================
	// Group ordering
	// =========================================================================

	#[tokio::test]
	async fn output_groups_are_ordered_by_group_key_not_by_arrival() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&["{ c: 'us' }", "{ c: 'de' }", "{ c: 'za' }", "{ c: 'fr' }", "{ c: 'de' }"])
				.await,
		);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		let keys: Vec<Value> = collect(&op, &ctx).await.iter().map(|r| field(r, "c")).collect();
		assert_eq!(
			keys,
			vec![Value::from("de"), Value::from("fr"), Value::from("us"), Value::from("za")]
		);
	}

	#[tokio::test]
	async fn numerically_equal_group_keys_of_different_numeric_types_share_one_group() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ n: 1 }", "{ n: 1.0 }", "{ n: 1.0dec }"]).await);
		let op =
			aggregate_over(input, &["n"], vec![key("n", 0), count_star("total", &ctx).await], &ctx)
				.await;

		// Int 1, Float 1.0 and Decimal 1.0 are equal under Value's equality, and
		// Value's hash canonicalises every numeric variant through the same
		// decimal encoding, so all three rows hash into one bucket and fold into
		// one group.
		let out = collect(&op, &ctx).await;
		assert_eq!(out.len(), 1);
		assert_eq!(field(&out[0], "total"), Value::from(3));
		// The published key is the value carried by the row that created the
		// group — the first one seen, with its original numeric variant.
		assert_eq!(field(&out[0], "n"), val("1").await);
		assert!(matches!(field(&out[0], "n"), Value::Number(crate::val::Number::Int(1))));
	}

	// =========================================================================
	// Field shapes
	// =========================================================================

	#[tokio::test]
	async fn aggregate_and_group_key_fields_share_one_output_object_under_their_own_names() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ c: 'us', score: 2 }", "{ c: 'us', score: 4 }"]).await);
		// Output names are independent of the source fields: the group key is
		// republished as `country` and the sum as `total`.
		let op = aggregate_over(
			input,
			&["c"],
			vec![key("country", 0), agg_of("total", "math::sum", "score", &ctx).await],
			&ctx,
		)
		.await;

		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ country: 'us', total: 6 }"]).await);
	}

	#[tokio::test]
	async fn output_paths_nest_while_a_single_dotted_key_stays_flat() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ score: 2 }", "{ score: 4 }"]).await);

		// A multi-part path nests into objects; a one-element path is an opaque
		// flat key even when the identifier contains a dot.
		let sum = agg_of("ignored", "math::sum", "score", &ctx).await;
		let nested = AggregateField::with_output_path(
			vec!["stats".to_string(), "total".to_string()],
			false,
			None,
			sum.aggregate_expr_info,
			None,
		);
		let flat = count_star("stats.count", &ctx).await;

		let op = aggregate_over(input, &[], vec![nested, flat], &ctx).await;
		let out = collect(&op, &ctx).await;
		assert_eq!(out.len(), 1);
		assert_eq!(field(&out[0], "stats"), val("{ total: 6 }").await);
		assert_eq!(field(&out[0], "stats.count"), Value::from(2));
	}

	#[tokio::test]
	async fn a_single_empty_named_field_returns_the_bare_value_not_an_object() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ score: 2 }", "{ score: 4 }"]).await);
		// SELECT VALUE with GROUP BY: exactly one field, with an empty name.
		let only = agg_of("", "math::sum", "score", &ctx).await;
		assert!(only.is_empty_name());

		let op = aggregate_over(input, &[], vec![only], &ctx).await;
		assert_eq!(collect(&op, &ctx).await, vec![Value::from(6)]);
	}

	#[tokio::test]
	async fn an_aggregate_over_a_field_absent_from_some_rows_skips_the_missing_values() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&["{ c: 'us', score: 2 }", "{ c: 'us' }", "{ c: 'us', score: 5 }"]).await,
		);
		let op = aggregate_over(
			input,
			&["c"],
			vec![
				count_star("rows", &ctx).await,
				count_of("scored", "score", &ctx).await,
				agg_of("sum", "math::sum", "score", &ctx).await,
			],
			&ctx,
		)
		.await;

		// `count()` counts rows, `count(score)` counts truthy values only, and
		// the sum ignores the NONE that the missing field evaluates to.
		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ rows: 3, scored: 2, sum: 7 }"]).await);
	}

	#[tokio::test]
	async fn several_aggregates_in_one_field_are_combined_by_the_post_expression() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ score: 1 }", "{ score: 2 }", "{ score: 3 }"]).await);
		// `math::sum(score) + count()`: both accumulate per row, then the
		// post-expression runs once per group against `{ _a0: sum, _a1: count }`.
		assert_eq!(aggregate_field_name(0), "_a0");
		assert_eq!(aggregate_field_name(1), "_a1");
		let combined = AggregateField::new(
			"mixed".to_string(),
			false,
			None,
			Some(AggregateExprInfo {
				aggregates: vec![
					ExtractedAggregate {
						function: aggregate_fn("math::sum", &ctx),
						argument_expr: physical_expr("score", &ctx).await,
						extra_args: vec![],
					},
					ExtractedAggregate {
						function: ctx.function_registry().get_count_aggregate(false),
						argument_expr: physical_expr("NONE", &ctx).await,
						extra_args: vec![],
					},
				],
				post_expr: Some(physical_expr("_a0 + _a1", &ctx).await),
			}),
			None,
		);

		let op = aggregate_over(input, &[], vec![combined], &ctx).await;
		assert_eq!(collect(&op, &ctx).await, rows(&["{ mixed: 9 }"]).await);
	}

	#[tokio::test]
	async fn a_field_whose_aggregate_list_is_empty_yields_null() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ score: 1 }"]).await);
		let empty = AggregateField::new(
			"nothing".to_string(),
			false,
			None,
			Some(AggregateExprInfo {
				aggregates: vec![],
				post_expr: None,
			}),
			None,
		);

		let op = aggregate_over(input, &[], vec![empty], &ctx).await;
		let out = collect(&op, &ctx).await;
		assert_eq!(field(&out[0], "nothing"), Value::Null);
	}

	#[tokio::test]
	async fn extra_args_are_evaluated_once_and_reused_by_every_group() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&["{ c: 'a', w: 'x' }", "{ c: 'b', w: 'z' }", "{ c: 'a', w: 'y' }"]).await,
		);
		// `array::join(w, '-')`: the separator is an extra argument, evaluated
		// once before any row is read and handed to each group's accumulator.
		let joined = single(
			"joined",
			ExtractedAggregate {
				function: aggregate_fn("array::join", &ctx),
				argument_expr: physical_expr("w", &ctx).await,
				extra_args: vec![physical_expr("'-'", &ctx).await],
			},
		);

		let op = aggregate_over(input, &["c"], vec![key("c", 0), joined], &ctx).await;
		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ c: 'a', joined: 'x-y' }", "{ c: 'b', joined: 'z' }"]).await);
	}

	#[tokio::test]
	async fn extra_args_are_evaluated_without_a_current_row() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ w: 'x', sep: '-' }", "{ w: 'y', sep: '-' }"]).await);
		// Extra arguments are evaluated against a row-less EvalContext, so a
		// row-dependent separator resolves to NONE and the accumulator receives
		// its raw string form.
		let joined = single(
			"joined",
			ExtractedAggregate {
				function: aggregate_fn("array::join", &ctx),
				argument_expr: physical_expr("w", &ctx).await,
				extra_args: vec![physical_expr("sep", &ctx).await],
			},
		);

		let op = aggregate_over(input, &[], vec![joined], &ctx).await;
		let out = collect(&op, &ctx).await;
		assert_eq!(field(&out[0], "joined"), Value::from("xNONEy"));
	}

	#[tokio::test]
	async fn a_non_aggregate_field_reports_the_first_non_none_value_in_the_group() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(
			rows(&["{ c: 'us' }", "{ c: 'us', label: 'second' }", "{ c: 'us', label: 'third' }"])
				.await,
		);
		let op = aggregate_over(
			input,
			&["c"],
			vec![key("c", 0), first_value("label", "label", &ctx).await],
			&ctx,
		)
		.await;

		// The per-row GROUP BY path keeps evaluating until it stores a non-NONE
		// value, so the first row's missing `label` does not win.
		let out = collect(&op, &ctx).await;
		assert_eq!(out, rows(&["{ c: 'us', label: 'second' }"]).await);
	}

	#[tokio::test]
	async fn group_all_first_value_fields_only_read_the_first_row_of_each_batch() {
		let ctx = root_ctx();
		// One batch whose first row lacks the field: the GROUP ALL path evaluates
		// the first-value expression against `batch.values().first()` only, so the
		// later row that does carry the field is never consulted for that batch.
		let one_batch = ValuesOperator::new(rows(&["{ }", "{ label: 'second' }"]).await);
		let op =
			aggregate_over(one_batch, &[], vec![first_value("label", "label", &ctx).await], &ctx)
				.await;
		let out = collect(&op, &ctx).await;
		assert_eq!(field(&out[0], "label"), Value::None);

		// Split across two batches, the second batch's first row does fill it.
		let two_batches = ScriptedSource::new(vec![
			batch(rows(&["{ }"]).await),
			batch(rows(&["{ label: 'second' }"]).await),
		]);
		let op =
			aggregate_over(two_batches, &[], vec![first_value("label", "label", &ctx).await], &ctx)
				.await;
		let out = collect(&op, &ctx).await;
		assert_eq!(field(&out[0], "label"), Value::from("second"));
	}

	// =========================================================================
	// Batching
	// =========================================================================

	#[tokio::test]
	async fn accumulator_state_carries_across_batch_boundaries() {
		let ctx = root_ctx();
		// Union emits each input's batch separately, so the aggregate sees two
		// batches; groups and their accumulators must persist between them.
		let input: Arc<dyn ExecOperator> = Arc::new(Union::new(vec![
			ValuesOperator::new(rows(&["{ c: 'us', score: 1 }", "{ c: 'de', score: 2 }"]).await),
			ValuesOperator::new(rows(&["{ c: 'us', score: 3 }"]).await),
		]));
		let op = aggregate_over(
			input,
			&["c"],
			vec![
				key("c", 0),
				count_star("total", &ctx).await,
				agg_of("sum", "math::sum", "score", &ctx).await,
			],
			&ctx,
		)
		.await;

		// `us` appears in both batches and still yields a single group whose
		// count and sum span them.
		let out = collect(&op, &ctx).await;
		assert_eq!(
			out,
			rows(&["{ c: 'de', total: 1, sum: 2 }", "{ c: 'us', total: 2, sum: 4 }"]).await
		);
	}

	#[tokio::test]
	async fn group_all_accumulates_over_every_batch_and_emits_one_row() {
		let ctx = root_ctx();
		let input: Arc<dyn ExecOperator> = Arc::new(Union::new(vec![
			ValuesOperator::new(rows(&["{ score: 1 }", "{ score: 2 }"]).await),
			ValuesOperator::new(rows(&["{ score: 3 }"]).await),
			ValuesOperator::new(rows(&["{ score: 4 }"]).await),
		]));
		let op = aggregate_over(
			input,
			&[],
			vec![count_star("total", &ctx).await, agg_of("sum", "math::sum", "score", &ctx).await],
			&ctx,
		)
		.await;

		// The GROUP ALL path feeds whole columns to `update_batch`; the single
		// group's accumulators are reused for every batch.
		assert_eq!(collect(&op, &ctx).await, rows(&["{ total: 4, sum: 10 }"]).await);
	}

	#[tokio::test]
	async fn empty_batches_around_a_populated_one_do_not_disturb_the_groups() {
		let ctx = root_ctx();
		let input = ScriptedSource::new(vec![
			batch(vec![]),
			batch(rows(&["{ c: 'us' }", "{ c: 'us' }"]).await),
			batch(vec![]),
		]);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		assert_eq!(collect(&op, &ctx).await, rows(&["{ c: 'us', total: 2 }"]).await);
	}

	// =========================================================================
	// Control flow and errors
	// =========================================================================

	#[tokio::test]
	async fn an_input_error_aborts_the_aggregate_without_emitting_a_batch() {
		let ctx = root_ctx();
		let input = ScriptedSource::new(vec![
			batch(rows(&["{ c: 'us' }"]).await),
			Err(ControlFlow::Err(anyhow::anyhow!("input exploded"))),
		]);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		// Pipeline-breaking: nothing has been emitted when the error arrives, so
		// the partial groups are discarded rather than reported.
		let err = try_collect(&op, &ctx).await.expect_err("input error propagates");
		assert!(err.to_string().contains("input exploded"), "unexpected error: {err}");
	}

	#[tokio::test]
	async fn control_flow_signals_from_the_input_propagate_unchanged() {
		let ctx = root_ctx();
		for signal in
			[ControlFlow::Return(Value::from(7)), ControlFlow::Break, ControlFlow::Continue]
		{
			let expected = format!("{signal:?}");
			let input = ScriptedSource::new(vec![batch(rows(&["{ c: 'us' }"]).await), Err(signal)]);
			let op = aggregate_over(
				input,
				&["c"],
				vec![key("c", 0), count_star("total", &ctx).await],
				&ctx,
			)
			.await;

			// RETURN/BREAK/CONTINUE are not errors and must not be converted into
			// one, nor swallowed into an empty result.
			let err = try_collect(&op, &ctx).await.expect_err("signal propagates");
			assert_eq!(format!("{err:?}"), expected);
		}
	}

	#[tokio::test]
	async fn a_non_ignorable_error_in_an_aggregate_argument_propagates() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ score: 1 }"]).await);
		let op = aggregate_over(
			input,
			&[],
			vec![agg_of("sum", "math::sum", "THROW 'boom'", &ctx).await],
			&ctx,
		)
		.await;

		let err = try_collect(&op, &ctx).await.expect_err("a thrown error is not ignorable");
		assert!(err.to_string().contains("boom"), "unexpected error: {err}");
	}

	#[tokio::test]
	async fn a_non_ignorable_error_in_a_group_key_propagates() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ c: 'us' }"]).await);
		let op = aggregate_over(
			input,
			&["THROW 'boom'"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		let err = try_collect(&op, &ctx).await.expect_err("a thrown error is not ignorable");
		assert!(err.to_string().contains("boom"), "unexpected error: {err}");
	}

	#[tokio::test]
	async fn an_ignorable_error_in_a_group_key_groups_the_row_under_none() {
		let ctx = root_ctx();
		let input =
			ValuesOperator::new(rows(&["{ score: 'x' }", "{ score: 'y' }", "{ score: 2 }"]).await);
		// `score * 2` fails with an arithmetic type error on the string rows.
		// Batch evaluation gives up on the whole column, and the per-row retry
		// resolves each ignorable failure to NONE — so both string rows land in
		// one NONE group while the numeric row keeps its own.
		let op = aggregate_over(
			input,
			&["score * 2"],
			vec![key("doubled", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		let out = collect(&op, &ctx).await;
		assert_eq!(out.len(), 2);
		assert_eq!(field(&out[0], "doubled"), Value::None);
		assert_eq!(field(&out[0], "total"), Value::from(2));
		assert_eq!(field(&out[1], "doubled"), Value::from(4));
		assert_eq!(field(&out[1], "total"), Value::from(1));
	}

	#[tokio::test]
	async fn an_ignorable_error_in_an_aggregate_argument_accumulates_as_none() {
		let ctx = root_ctx();
		let input = ValuesOperator::new(rows(&["{ score: 'x' }", "{ score: 3 }"]).await);
		let op = aggregate_over(
			input,
			&[],
			vec![
				count_star("rows", &ctx).await,
				agg_of("sum", "math::sum", "score * 2", &ctx).await,
			],
			&ctx,
		)
		.await;

		// The failing row contributes NONE, which the sum accumulator skips; the
		// row itself still counts.
		assert_eq!(collect(&op, &ctx).await, rows(&["{ rows: 2, sum: 6 }"]).await);
	}

	#[tokio::test]
	async fn cancellation_between_batches_fails_the_stream() {
		let ctx = root_ctx();
		ctx.cancellation().cancel();
		let input = ValuesOperator::new(rows(&["{ c: 'us' }"]).await);
		let op =
			aggregate_over(input, &["c"], vec![key("c", 0), count_star("total", &ctx).await], &ctx)
				.await;

		let err = try_collect(&op, &ctx).await.expect_err("cancellation is reported");
		let ControlFlow::Err(err) = err else {
			panic!("cancellation surfaces as an error, not a control-flow signal");
		};
		assert!(crate::err::is_query_cancelled(&err), "unexpected error: {err}");
	}

	// =========================================================================
	// Planner-visible metadata
	// =========================================================================

	#[tokio::test]
	async fn required_context_lifts_to_database_when_a_key_or_argument_reads_a_field() {
		let ctx = root_ctx();

		// GROUP ALL count() reads no field, so Root is enough — the executor
		// validates this level before it calls execute().
		let op = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(op.required_context(), ContextLevel::Root);

		// Field access may have to dereference a record id, which needs the
		// database context, so grouping on a field lifts the requirement.
		let key_expr = physical_expr("c", &ctx).await;
		assert_eq!(key_expr.required_context(), ContextLevel::Database);
		let op = aggregate_over(
			ValuesOperator::new(vec![]),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(op.required_context(), ContextLevel::Database);

		// An aggregate argument that reads a field lifts it just the same.
		let op = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![agg_of("sum", "math::sum", "score", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(op.required_context(), ContextLevel::Database);
	}

	#[tokio::test]
	async fn access_mode_promotes_to_read_write_from_the_input_or_from_any_expression() {
		let ctx = root_ctx();

		let read_only = aggregate_over(
			ValuesOperator::new(vec![]),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(read_only.access_mode(), AccessMode::ReadOnly);

		// A mutating input makes the whole aggregate read-write, which is what
		// decides the transaction mode and the dependency-ordering barriers.
		let from_input = aggregate_over(
			ScriptedSource::read_write(vec![]),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(from_input.access_mode(), AccessMode::ReadWrite);

		// So does a mutating expression, wherever it sits.
		let writer = physical_expr("eval::surql('RETURN 1')", &ctx).await;
		assert_eq!(writer.access_mode(), AccessMode::ReadWrite);

		let from_key = aggregate_over(
			ValuesOperator::new(vec![]),
			&["eval::surql('RETURN 1')"],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(from_key.access_mode(), AccessMode::ReadWrite);

		let from_arg = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![agg_of("sum", "math::sum", "eval::surql('RETURN 1')", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(from_arg.access_mode(), AccessMode::ReadWrite);

		let from_extra = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![single(
				"joined",
				ExtractedAggregate {
					function: aggregate_fn("array::join", &ctx),
					argument_expr: physical_expr("w", &ctx).await,
					extra_args: vec![physical_expr("eval::surql('RETURN 1')", &ctx).await],
				},
			)],
			&ctx,
		)
		.await;
		assert_eq!(from_extra.access_mode(), AccessMode::ReadWrite);

		let from_post = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![AggregateField::new(
				"mixed".to_string(),
				false,
				None,
				Some(AggregateExprInfo {
					aggregates: vec![ExtractedAggregate {
						function: aggregate_fn("math::sum", &ctx),
						argument_expr: physical_expr("score", &ctx).await,
						extra_args: vec![],
					}],
					post_expr: Some(physical_expr("eval::surql('RETURN 1')", &ctx).await),
				}),
				None,
			)],
			&ctx,
		)
		.await;
		assert_eq!(from_post.access_mode(), AccessMode::ReadWrite);

		let from_fallback = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![first_value("label", "eval::surql('RETURN 1')", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(from_fallback.access_mode(), AccessMode::ReadWrite);
	}

	#[tokio::test]
	async fn output_ordering_is_unordered_so_a_sort_after_grouping_is_never_eliminated() {
		let ctx = root_ctx();
		let op = aggregate_over(
			ValuesOperator::new(rows(&["{ c: 'us' }"]).await),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;

		// Groups leave the operator sorted by group key, but that is not
		// advertised. `Planner::can_eliminate_sort` reads exactly these two
		// methods, and both deny elimination, so `ORDER BY c` after `GROUP BY c`
		// always keeps its Sort operator.
		assert_eq!(op.output_ordering(), OutputOrdering::Unordered);
		assert!(op.constant_output_fields().is_empty());
		let required = vec![SortProperty {
			path: FieldPath::field("c"),
			direction: SortDirection::Asc,
			collate: false,
			numeric: false,
		}];
		assert!(!op.output_ordering().satisfies(&required));
	}

	#[tokio::test]
	async fn cardinality_hint_stays_unbounded_even_for_group_all() {
		let ctx = root_ctx();
		// `buffer_stream` picks a consumer's buffering strategy from this hint.
		// GROUP ALL emits exactly one row yet still reports Unbounded, so the
		// consumer buffers it through a spawned task rather than inline.
		let group_all = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(group_all.cardinality_hint(), CardinalityHint::Unbounded);

		let grouped = aggregate_over(
			ValuesOperator::new(vec![]),
			&["c"],
			vec![key("c", 0), count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(grouped.cardinality_hint(), CardinalityHint::Unbounded);
	}

	#[tokio::test]
	async fn attrs_report_group_all_only_when_there_are_no_keys() {
		let ctx = root_ctx();
		let group_all = aggregate_over(
			ValuesOperator::new(vec![]),
			&[],
			vec![count_star("total", &ctx).await],
			&ctx,
		)
		.await;
		assert_eq!(group_all.attrs(), vec![("mode".to_string(), "GROUP ALL".to_string())]);

		let grouped = aggregate_over(
			ValuesOperator::new(vec![]),
			&["c", "y"],
			vec![key("c", 0), key("y", 1)],
			&ctx,
		)
		.await;
		assert_eq!(grouped.attrs(), vec![("by".to_string(), "c, y".to_string())]);
	}

	#[tokio::test]
	async fn expressions_expose_every_sub_expression_for_explain() {
		let ctx = root_ctx();
		let combined = AggregateField::new(
			"mixed".to_string(),
			false,
			None,
			Some(AggregateExprInfo {
				aggregates: vec![ExtractedAggregate {
					function: aggregate_fn("array::join", &ctx),
					argument_expr: physical_expr("w", &ctx).await,
					extra_args: vec![physical_expr("'-'", &ctx).await],
				}],
				post_expr: Some(physical_expr("_a0", &ctx).await),
			}),
			None,
		);
		let op = aggregate_over(
			ValuesOperator::new(vec![]),
			&["c"],
			vec![combined, first_value("label", "label", &ctx).await],
			&ctx,
		)
		.await;

		// EXPLAIN walks children + expressions; every compiled expression the
		// operator holds must be reachable, under a label naming its role.
		assert_eq!(op.children().len(), 1);
		let labels: Vec<&str> = op.expressions().into_iter().map(|(label, _)| label).collect();
		assert_eq!(
			labels,
			vec!["group_by", "agg_arg", "agg_extra", "agg_post_expr", "agg_fallback"]
		);
	}

	// =========================================================================
	// set_nested_value
	// =========================================================================

	#[test]
	fn set_nested_value_creates_missing_levels_and_replaces_non_objects() {
		let mut obj = Object::default();
		set_nested_value(&mut obj, &["a".to_string(), "b".to_string()], Value::from(1));
		assert_eq!(
			obj.get("a").cloned(),
			Some(Value::Object(Object::from_iter([("b".to_string(), Value::from(1))])))
		);

		// A scalar already sitting at a prefix of a later path is replaced by the
		// object that path needs.
		let mut obj = Object::default();
		set_nested_value(&mut obj, &["a".to_string()], Value::from(7));
		set_nested_value(&mut obj, &["a".to_string(), "b".to_string()], Value::from(1));
		assert_eq!(
			obj.get("a").cloned(),
			Some(Value::Object(Object::from_iter([("b".to_string(), Value::from(1))])))
		);

		// An empty path is a no-op.
		let mut obj = Object::default();
		set_nested_value(&mut obj, &[], Value::from(1));
		assert!(obj.is_empty());
	}

	// =========================================================================
	// GroupMap — group identity and ordering
	// =========================================================================

	fn dec(s: &str) -> Value {
		Value::Number(Number::Decimal(Decimal::from_str(s).unwrap()))
	}

	fn float(f: f64) -> Value {
		Value::Number(Number::Float(f))
	}

	fn empty_state() -> GroupState {
		create_group_state(&[], &[])
	}

	fn group_count(keys: &[Vec<Value>]) -> usize {
		let mut map = GroupMap::new();
		for key in keys {
			map.entry_for_row(key, empty_state);
		}
		map.into_key_order().len()
	}

	/// Group identity is `Value: Ord`, so every numeric variant carrying the
	/// same value shares one group.
	#[test]
	fn numerically_equal_keys_share_a_group() {
		assert_eq!(
			group_count(&[
				vec![Value::from(1i64)],
				vec![float(1.0)],
				vec![dec("1")],
				vec![dec("1.0")],
			]),
			1
		);
		assert_eq!(group_count(&[vec![float(0.1)], vec![dec("0.1")]]), 1);
		assert_eq!(group_count(&[vec![float(1.5)], vec![dec("1.50")]]), 1);
		// Multi-column keys collapse per column.
		assert_eq!(
			group_count(&[vec![Value::from(1i64), dec("2")], vec![dec("1.0"), float(2.0)],]),
			1
		);
	}

	/// The reason [`GroupMap`] is ordered rather than hash-partitioned:
	/// `Number: Hash` hashes each variant's exact decimal expansion, so it
	/// disagrees with the cross-variant `Number: PartialEq` that decides group
	/// membership. Bucketing by hash splits one group in two.
	#[test]
	fn hash_disagrees_with_the_equality_that_decides_grouping() {
		fn hash(v: &Value) -> u64 {
			let mut hasher = DefaultHasher::new();
			v.hash(&mut hasher);
			hasher.finish()
		}

		for (a, b) in [(float(0.1), dec("0.1")), (float(1.5), dec("1.50"))] {
			assert_eq!(a, b, "{a:?} and {b:?} are one group");
			assert_ne!(hash(&a), hash(&b), "{a:?} and {b:?} would bucket apart");
		}
	}

	/// Distinct keys stay distinct, and draining yields them in key order.
	#[test]
	fn distinct_keys_drain_in_key_order() {
		let mut map = GroupMap::new();
		for key in [dec("2"), float(1.5), Value::from(1i64), float(2.0), dec("1.50")] {
			map.entry_for_row(&[key], empty_state);
		}
		let keys: Vec<Value> =
			map.into_key_order().map(|(k, _)| k.into_iter().next().unwrap()).collect();
		assert_eq!(keys, vec![Value::from(1i64), float(1.5), dec("2")]);
	}

	#[test]
	fn group_key_rows_takes_a_single_column_whole() {
		let mut columns = vec![vec![Value::from(1i64), dec("2")]];
		let flat = group_key_rows(&mut columns, 2);
		assert_eq!(flat, vec![Value::from(1i64), dec("2")]);
	}

	#[test]
	fn group_key_rows_lays_rows_out_contiguously() {
		let mut columns =
			vec![vec![Value::from(1i64), Value::from(2i64)], vec![Value::from(10i64), dec("20")]];
		let flat = group_key_rows(&mut columns, 2);
		assert_eq!(flat, vec![Value::from(1i64), Value::from(10i64), Value::from(2i64), dec("20")]);
	}
}