reddb-io-server 1.0.7

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Aggregation Framework
//!
//! Provides aggregation functions for query results.
//!
//! # Supported Functions
//!
//! - **COUNT**: Count rows (including COUNT(*) and COUNT DISTINCT)
//! - **SUM**: Sum numeric values
//! - **AVG**: Average of numeric values
//! - **MIN**: Minimum value
//! - **MAX**: Maximum value
//! - **STDDEV**: Standard deviation
//! - **VARIANCE**: Statistical variance
//! - **PERCENTILE**: Nth percentile value
//!
//! # GROUP BY
//!
//! Aggregations can be grouped by one or more columns.
//! HAVING clause filters groups after aggregation.

use std::collections::HashMap;

use super::super::engine::binding::{Binding, Value, Var};
use super::value_compare::total_compare_values;

// ============================================================================
// Aggregator Trait
// ============================================================================

/// Trait for aggregation functions
pub trait Aggregator: Send + Sync {
    /// Process a single value
    fn accumulate(&mut self, value: Option<&Value>);

    /// Get the final aggregated result
    fn finalize(&self) -> Value;

    /// Reset for new group
    fn reset(&mut self);

    /// Create a fresh copy for a new group
    fn new_instance(&self) -> Box<dyn Aggregator>;

    /// Name of the aggregator
    fn name(&self) -> &'static str;
}

// ============================================================================
// COUNT Aggregator
// ============================================================================

/// COUNT aggregator
#[derive(Debug, Clone, Default)]
pub struct CountAggregator {
    count: i64,
    count_all: bool, // COUNT(*) vs COUNT(column)
}

impl CountAggregator {
    /// Create COUNT(*) aggregator
    pub fn count_all() -> Self {
        Self {
            count: 0,
            count_all: true,
        }
    }

    /// Create COUNT(column) aggregator (ignores nulls)
    pub fn count_column() -> Self {
        Self {
            count: 0,
            count_all: false,
        }
    }
}

impl Aggregator for CountAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if self.count_all || (value.is_some() && !matches!(value, Some(Value::Null))) {
            self.count += 1;
        }
    }

    fn finalize(&self) -> Value {
        Value::Integer(self.count)
    }

    fn reset(&mut self) {
        self.count = 0;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self {
            count: 0,
            count_all: self.count_all,
        })
    }

    fn name(&self) -> &'static str {
        "COUNT"
    }
}

// ============================================================================
// COUNT DISTINCT Aggregator
// ============================================================================

/// COUNT DISTINCT aggregator
#[derive(Debug, Clone, Default)]
pub struct CountDistinctAggregator {
    seen: std::collections::HashSet<String>,
}

impl CountDistinctAggregator {
    pub fn new() -> Self {
        Self {
            seen: std::collections::HashSet::new(),
        }
    }
}

impl Aggregator for CountDistinctAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if !matches!(v, Value::Null) {
                self.seen.insert(value_to_string(v));
            }
        }
    }

    fn finalize(&self) -> Value {
        Value::Integer(self.seen.len() as i64)
    }

    fn reset(&mut self) {
        self.seen.clear();
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "COUNT_DISTINCT"
    }
}

// ============================================================================
// SUM Aggregator
// ============================================================================

/// SUM aggregator
#[derive(Debug, Clone, Default)]
pub struct SumAggregator {
    sum: f64,
    has_value: bool,
    all_integers: bool,
}

impl SumAggregator {
    pub fn new() -> Self {
        Self {
            sum: 0.0,
            has_value: false,
            all_integers: true,
        }
    }
}

impl Aggregator for SumAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            match v {
                Value::Integer(i) => {
                    self.sum += *i as f64;
                    self.has_value = true;
                }
                Value::Float(f) => {
                    self.sum += *f;
                    self.has_value = true;
                    if f.fract() != 0.0 {
                        self.all_integers = false;
                    }
                }
                _ => {}
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.has_value {
            if self.all_integers && self.sum.fract() == 0.0 {
                Value::Integer(self.sum as i64)
            } else {
                Value::Float(self.sum)
            }
        } else {
            Value::Null
        }
    }

    fn reset(&mut self) {
        self.sum = 0.0;
        self.has_value = false;
        self.all_integers = true;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "SUM"
    }
}

// ============================================================================
// AVG Aggregator
// ============================================================================

/// AVG aggregator
#[derive(Debug, Clone, Default)]
pub struct AvgAggregator {
    sum: f64,
    count: i64,
}

impl AvgAggregator {
    pub fn new() -> Self {
        Self { sum: 0.0, count: 0 }
    }
}

impl Aggregator for AvgAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if let Some(n) = value_to_number(v) {
                self.sum += n;
                self.count += 1;
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.count > 0 {
            Value::Float(self.sum / self.count as f64)
        } else {
            Value::Null
        }
    }

    fn reset(&mut self) {
        self.sum = 0.0;
        self.count = 0;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "AVG"
    }
}

// ============================================================================
// MIN Aggregator
// ============================================================================

/// MIN aggregator
#[derive(Debug, Clone, Default)]
pub struct MinAggregator {
    min: Option<Value>,
}

impl MinAggregator {
    pub fn new() -> Self {
        Self { min: None }
    }
}

impl Aggregator for MinAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if matches!(v, Value::Null) {
                return;
            }
            match &self.min {
                None => self.min = Some(v.clone()),
                Some(current) => {
                    if total_compare_values(v, current) == std::cmp::Ordering::Less {
                        self.min = Some(v.clone());
                    }
                }
            }
        }
    }

    fn finalize(&self) -> Value {
        self.min.clone().unwrap_or(Value::Null)
    }

    fn reset(&mut self) {
        self.min = None;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "MIN"
    }
}

// ============================================================================
// MAX Aggregator
// ============================================================================

/// MAX aggregator
#[derive(Debug, Clone, Default)]
pub struct MaxAggregator {
    max: Option<Value>,
}

impl MaxAggregator {
    pub fn new() -> Self {
        Self { max: None }
    }
}

impl Aggregator for MaxAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if matches!(v, Value::Null) {
                return;
            }
            match &self.max {
                None => self.max = Some(v.clone()),
                Some(current) => {
                    if total_compare_values(v, current) == std::cmp::Ordering::Greater {
                        self.max = Some(v.clone());
                    }
                }
            }
        }
    }

    fn finalize(&self) -> Value {
        self.max.clone().unwrap_or(Value::Null)
    }

    fn reset(&mut self) {
        self.max = None;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "MAX"
    }
}

// ============================================================================
// SAMPLE Aggregator
// ============================================================================

/// SAMPLE aggregator (returns first non-null value)
#[derive(Debug, Clone, Default)]
pub struct SampleAggregator {
    value: Option<Value>,
}

impl SampleAggregator {
    pub fn new() -> Self {
        Self { value: None }
    }
}

impl Aggregator for SampleAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if self.value.is_some() {
            return;
        }
        if let Some(v) = value {
            if !matches!(v, Value::Null) {
                self.value = Some(v.clone());
            }
        }
    }

    fn finalize(&self) -> Value {
        self.value.clone().unwrap_or(Value::Null)
    }

    fn reset(&mut self) {
        self.value = None;
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "SAMPLE"
    }
}

// ============================================================================
// GROUP_CONCAT Aggregator
// ============================================================================

/// GROUP_CONCAT aggregator
#[derive(Debug, Clone)]
pub struct GroupConcatAggregator {
    separator: String,
    values: Vec<String>,
}

impl GroupConcatAggregator {
    pub fn new(separator: Option<String>) -> Self {
        Self {
            separator: separator.unwrap_or_else(|| " ".to_string()),
            values: Vec::new(),
        }
    }
}

impl Aggregator for GroupConcatAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if !matches!(v, Value::Null) {
                self.values.push(value_to_string(v));
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.values.is_empty() {
            Value::Null
        } else {
            Value::String(self.values.join(&self.separator))
        }
    }

    fn reset(&mut self) {
        self.values.clear();
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new(Some(self.separator.clone())))
    }

    fn name(&self) -> &'static str {
        "GROUP_CONCAT"
    }
}

// ============================================================================
// STDDEV Aggregator (Population Standard Deviation)
// ============================================================================

/// Standard deviation aggregator
#[derive(Debug, Clone, Default)]
pub struct StdDevAggregator {
    values: Vec<f64>,
}

impl StdDevAggregator {
    pub fn new() -> Self {
        Self { values: Vec::new() }
    }
}

impl Aggregator for StdDevAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if let Some(n) = value_to_number(v) {
                self.values.push(n);
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.values.is_empty() {
            return Value::Null;
        }

        let n = self.values.len() as f64;
        let mean = self.values.iter().sum::<f64>() / n;
        let variance = self.values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;

        Value::Float(variance.sqrt())
    }

    fn reset(&mut self) {
        self.values.clear();
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "STDDEV"
    }
}

// ============================================================================
// VARIANCE Aggregator
// ============================================================================

/// Variance aggregator
#[derive(Debug, Clone, Default)]
pub struct VarianceAggregator {
    values: Vec<f64>,
}

impl VarianceAggregator {
    pub fn new() -> Self {
        Self { values: Vec::new() }
    }
}

impl Aggregator for VarianceAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if let Some(n) = value_to_number(v) {
                self.values.push(n);
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.values.is_empty() {
            return Value::Null;
        }

        let n = self.values.len() as f64;
        let mean = self.values.iter().sum::<f64>() / n;
        let variance = self.values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;

        Value::Float(variance)
    }

    fn reset(&mut self) {
        self.values.clear();
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new())
    }

    fn name(&self) -> &'static str {
        "VARIANCE"
    }
}

// ============================================================================
// PERCENTILE Aggregator
// ============================================================================

/// Percentile aggregator
#[derive(Debug, Clone)]
pub struct PercentileAggregator {
    values: Vec<f64>,
    percentile: f64, // 0.0 to 1.0 (e.g., 0.5 for median)
}

impl PercentileAggregator {
    pub fn new(percentile: f64) -> Self {
        Self {
            values: Vec::new(),
            percentile: percentile.clamp(0.0, 1.0),
        }
    }

    /// Create median aggregator (50th percentile)
    pub fn median() -> Self {
        Self::new(0.5)
    }
}

impl Aggregator for PercentileAggregator {
    fn accumulate(&mut self, value: Option<&Value>) {
        if let Some(v) = value {
            if let Some(n) = value_to_number(v) {
                self.values.push(n);
            }
        }
    }

    fn finalize(&self) -> Value {
        if self.values.is_empty() {
            return Value::Null;
        }

        let mut sorted = self.values.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let index = (self.percentile * (sorted.len() - 1) as f64).round() as usize;
        Value::Float(sorted[index])
    }

    fn reset(&mut self) {
        self.values.clear();
    }

    fn new_instance(&self) -> Box<dyn Aggregator> {
        Box::new(Self::new(self.percentile))
    }

    fn name(&self) -> &'static str {
        "PERCENTILE"
    }
}

// ============================================================================
// GROUP BY Executor
// ============================================================================

/// Definition of an aggregation to compute
pub struct AggregationDef {
    /// Source variable to aggregate
    pub source_var: Var,
    /// Result variable name
    pub result_var: Var,
    /// Aggregator factory
    pub aggregator: Box<dyn Aggregator>,
}

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

/// Soft memory cap for in-process hash aggregation.
///
/// When the groups HashMap grows beyond this threshold, an OOM-guard
/// warning fires. Full spill-to-disk requires changing the calling
/// convention to a row-at-a-time streaming API (tracked separately).
const WORK_MEM_BYTES: usize = 64 * 1024 * 1024; // 64 MiB

/// Estimated heap cost per group entry in the streaming HashMap.
///
/// In practice each entry holds:
///   - a String group-key (~32 B avg)
///   - group key Var/Value pairs (~64 B)
///   - one Box<dyn Aggregator> per agg_def (~64 B each, assume ≤4 defs → ~256 B)
///
/// 512 B is deliberately conservative to avoid premature eviction
/// in the common case.
const AVG_GROUP_ENTRY_BYTES: usize = 512;

/// 1-pass streaming GROUP BY.
///
/// Previous implementation accumulated ALL input bindings per group
/// (`HashMap<String, Vec<Binding>>`), then ran aggregations in a
/// second pass. Memory cost: O(input_rows) in the groups map.
///
/// This version keeps only the incremental aggregation state per group
/// — one `Box<dyn Aggregator>` per `AggregationDef`. Memory cost drops
/// to O(distinct_groups × agg_defs), which is dramatically lower for
/// high-cardinality inputs with few distinct groups.
pub fn execute_group_by(
    bindings: Vec<Binding>,
    group_vars: &[Var],
    aggregations: &[AggregationDef],
) -> Vec<Binding> {
    // Each entry: (snapshot of group-key values from first binding,
    //              incremental aggregator state for each agg_def)
    let mut groups: HashMap<String, (Binding, Vec<Box<dyn Aggregator>>)> = HashMap::new();

    for binding in &bindings {
        let key = make_group_key(binding, group_vars);
        let entry = groups.entry(key).or_insert_with(|| {
            // Capture group key values once from the first binding in this group.
            let mut key_binding = Binding::empty();
            for var in group_vars {
                if let Some(value) = binding.get(var) {
                    let partial = Binding::one(var.clone(), value.clone());
                    key_binding = key_binding.merge(&partial).unwrap_or(key_binding);
                }
            }
            // Allocate one fresh aggregator instance per agg def.
            let agg_instances = aggregations
                .iter()
                .map(|a| a.aggregator.new_instance())
                .collect();
            (key_binding, agg_instances)
        });

        // Accumulate each aggregation in a single pass over the binding.
        for (i, agg_def) in aggregations.iter().enumerate() {
            entry.1[i].accumulate(binding.get(&agg_def.source_var));
        }

        // Memory guard: O(1) check, avoids estimating actual heap usage.
        // When the number of distinct groups × avg cost exceeds WORK_MEM,
        // we've likely exhausted the intended budget. For now we continue
        // (the data is already in memory via the input Vec<Binding>) but
        // emit a debug trace so operators can see when this fires.
        #[cfg(debug_assertions)]
        if groups.len() * AVG_GROUP_ENTRY_BYTES > WORK_MEM_BYTES {
            // Only log once — on entry count crossing the threshold,
            // not on every subsequent row.
            static WARNED: std::sync::atomic::AtomicBool =
                std::sync::atomic::AtomicBool::new(false);
            if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
                eprintln!(
                    "[reddb] hash-agg: {} distinct groups × {} B ≈ {} MiB exceeds WORK_MEM {}  MiB; \
                     disk spill not yet wired — upgrade calling convention to streaming for OOM safety",
                    groups.len(),
                    AVG_GROUP_ENTRY_BYTES,
                    (groups.len() * AVG_GROUP_ENTRY_BYTES) / (1024 * 1024),
                    WORK_MEM_BYTES / (1024 * 1024),
                );
            }
        }
    }

    // Finalize: emit one output Binding per distinct group.
    let mut results = Vec::with_capacity(groups.len());
    for (_, (key_binding, agg_instances)) in groups {
        let mut result = key_binding;
        for (i, agg_def) in aggregations.iter().enumerate() {
            let agg_result = agg_instances[i].finalize();
            let partial = Binding::one(agg_def.result_var.clone(), agg_result);
            result = result.merge(&partial).unwrap_or(result);
        }
        results.push(result);
    }
    results
}

/// Execute HAVING clause (filter on aggregated results)
pub fn execute_having<F>(bindings: Vec<Binding>, predicate: F) -> Vec<Binding>
where
    F: Fn(&Binding) -> bool,
{
    bindings.into_iter().filter(|b| predicate(b)).collect()
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Format a single `Value` as a String. **Cold path** — used by
/// non-hot consumers like GROUP_CONCAT result formatting. The
/// hot group-by key path inlines this logic into a shared buffer
/// in [`make_group_key`] to avoid per-row allocations.
fn value_to_string(value: &Value) -> String {
    match value {
        Value::Node(id) => format!("node:{}", id),
        Value::Edge(id) => format!("edge:{}", id),
        Value::String(s) => s.clone(),
        Value::Integer(i) => i.to_string(),
        Value::Float(f) => f.to_string(),
        Value::Boolean(b) => b.to_string(),
        Value::Uri(u) => u.clone(),
        Value::Null => "null".to_string(),
    }
}

/// Build a group-by key for one row.
///
/// **Hot path** — called once per row in `execute_group_by`. The
/// previous implementation paid `N+2` String allocations per row
/// (one per `value_to_string`, one per `format!`, one for the final
/// `join("|")`). This version writes everything into a single
/// `String` buffer with one allocation.
///
/// On a 3-column GROUP BY the difference is ~5 allocations vs 1,
/// which on a 1M-row aggregation saves ~4M small allocations.
fn make_group_key(binding: &Binding, group_vars: &[Var]) -> String {
    use std::fmt::Write;
    // Tunable initial capacity. 64 bytes covers most numeric / short
    // text group keys in one allocation; longer text grows in place
    // through String's exponential growth.
    let mut key = String::with_capacity(64);
    for (i, var) in group_vars.iter().enumerate() {
        if i > 0 {
            key.push('|');
        }
        key.push_str(var.name());
        key.push('=');
        match binding.get(var) {
            None => key.push_str("NULL"),
            Some(Value::Null) => key.push_str("null"),
            Some(Value::String(s)) => key.push_str(s),
            Some(Value::Integer(n)) => {
                let _ = write!(key, "{n}");
            }
            Some(Value::Float(f)) => {
                let _ = write!(key, "{f}");
            }
            Some(Value::Boolean(b)) => {
                let _ = write!(key, "{b}");
            }
            Some(Value::Node(id)) => {
                key.push_str("node:");
                key.push_str(id);
            }
            Some(Value::Edge(id)) => {
                key.push_str("edge:");
                key.push_str(id);
            }
            Some(Value::Uri(u)) => key.push_str(u),
        }
    }
    key
}

fn value_to_number(value: &Value) -> Option<f64> {
    match value {
        Value::Integer(i) => Some(*i as f64),
        Value::Float(f) => Some(*f),
        Value::String(s) => s.parse().ok(),
        _ => None,
    }
}

// ============================================================================
// Aggregator Factory
// ============================================================================

/// Create an aggregator by name
pub fn create_aggregator(name: &str) -> Option<Box<dyn Aggregator>> {
    match name.to_uppercase().as_str() {
        "COUNT" => Some(Box::new(CountAggregator::count_all())),
        "COUNT_COLUMN" => Some(Box::new(CountAggregator::count_column())),
        "COUNT_DISTINCT" => Some(Box::new(CountDistinctAggregator::new())),
        "SUM" => Some(Box::new(SumAggregator::new())),
        "AVG" => Some(Box::new(AvgAggregator::new())),
        "MIN" => Some(Box::new(MinAggregator::new())),
        "MAX" => Some(Box::new(MaxAggregator::new())),
        "STDDEV" => Some(Box::new(StdDevAggregator::new())),
        "VARIANCE" => Some(Box::new(VarianceAggregator::new())),
        "MEDIAN" => Some(Box::new(PercentileAggregator::median())),
        "SAMPLE" => Some(Box::new(SampleAggregator::new())),
        "GROUP_CONCAT" => Some(Box::new(GroupConcatAggregator::new(None))),
        _ => None,
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn make_binding(pairs: &[(&str, Value)]) -> Binding {
        if pairs.is_empty() {
            return Binding::empty();
        }

        let mut result = Binding::one(Var::new(pairs[0].0), pairs[0].1.clone());

        for (k, v) in pairs.iter().skip(1) {
            let next = Binding::one(Var::new(k), v.clone());
            result = result.merge(&next).unwrap_or(result);
        }

        result
    }

    #[test]
    fn test_count() {
        let mut counter = CountAggregator::count_all();
        counter.accumulate(Some(&Value::Integer(1)));
        counter.accumulate(Some(&Value::Integer(2)));
        counter.accumulate(None);
        counter.accumulate(Some(&Value::Null));

        assert_eq!(counter.finalize(), Value::Integer(4));
    }

    #[test]
    fn test_count_column() {
        let mut counter = CountAggregator::count_column();
        counter.accumulate(Some(&Value::Integer(1)));
        counter.accumulate(None);
        counter.accumulate(Some(&Value::Null));
        counter.accumulate(Some(&Value::Integer(2)));

        assert_eq!(counter.finalize(), Value::Integer(2)); // Only non-null values
    }

    #[test]
    fn test_sum() {
        let mut sum = SumAggregator::new();
        sum.accumulate(Some(&Value::Integer(10)));
        sum.accumulate(Some(&Value::Float(5.5)));
        sum.accumulate(Some(&Value::Integer(4)));

        assert_eq!(sum.finalize(), Value::Float(19.5));
    }

    #[test]
    fn test_avg() {
        let mut avg = AvgAggregator::new();
        avg.accumulate(Some(&Value::Integer(10)));
        avg.accumulate(Some(&Value::Integer(20)));
        avg.accumulate(Some(&Value::Integer(30)));

        assert_eq!(avg.finalize(), Value::Float(20.0));
    }

    #[test]
    fn test_min_max() {
        let mut min = MinAggregator::new();
        let mut max = MaxAggregator::new();

        for val in [5, 2, 8, 1, 9] {
            min.accumulate(Some(&Value::Integer(val)));
            max.accumulate(Some(&Value::Integer(val)));
        }

        assert_eq!(min.finalize(), Value::Integer(1));
        assert_eq!(max.finalize(), Value::Integer(9));
    }

    #[test]
    fn test_count_distinct() {
        let mut distinct = CountDistinctAggregator::new();
        distinct.accumulate(Some(&Value::String("a".to_string())));
        distinct.accumulate(Some(&Value::String("b".to_string())));
        distinct.accumulate(Some(&Value::String("a".to_string())));
        distinct.accumulate(Some(&Value::String("c".to_string())));

        assert_eq!(distinct.finalize(), Value::Integer(3));
    }

    #[test]
    fn test_group_by() {
        let bindings = vec![
            make_binding(&[
                ("dept", Value::String("Sales".to_string())),
                ("salary", Value::Integer(50000)),
            ]),
            make_binding(&[
                ("dept", Value::String("Sales".to_string())),
                ("salary", Value::Integer(60000)),
            ]),
            make_binding(&[
                ("dept", Value::String("Engineering".to_string())),
                ("salary", Value::Integer(80000)),
            ]),
            make_binding(&[
                ("dept", Value::String("Engineering".to_string())),
                ("salary", Value::Integer(90000)),
            ]),
        ];

        let aggs = vec![
            AggregationDef {
                source_var: Var::new("salary"),
                result_var: Var::new("total"),
                aggregator: Box::new(SumAggregator::new()),
            },
            AggregationDef {
                source_var: Var::new("salary"),
                result_var: Var::new("count"),
                aggregator: Box::new(CountAggregator::count_all()),
            },
        ];

        let results = execute_group_by(bindings, &[Var::new("dept")], &aggs);

        assert_eq!(results.len(), 2);

        // Find Sales result
        let sales = results
            .iter()
            .find(|b| b.get(&Var::new("dept")) == Some(&Value::String("Sales".to_string())))
            .expect("Sales group not found");

        assert_eq!(sales.get(&Var::new("total")), Some(&Value::Integer(110000)));
        assert_eq!(sales.get(&Var::new("count")), Some(&Value::Integer(2)));
    }

    #[test]
    fn test_percentile() {
        let mut p50 = PercentileAggregator::median();
        for v in [1, 2, 3, 4, 5, 6, 7, 8, 9] {
            p50.accumulate(Some(&Value::Integer(v)));
        }
        // Median of 1-9 is 5
        assert_eq!(p50.finalize(), Value::Float(5.0));
    }
}