sql-cli 1.68.0

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
//! Concrete implementations of aggregate functions

use anyhow::Result;

use super::{
    AggregateFunction, AggregateState, AvgState, MinMaxState, ModeState, PercentileState,
    StringAggState, SumState, VarianceState,
};
use crate::data::datatable::DataValue;

/// COUNT(*) - counts all rows including nulls
pub struct CountStarFunction;

impl AggregateFunction for CountStarFunction {
    fn name(&self) -> &'static str {
        "COUNT_STAR"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Count(0)
    }

    fn accumulate(&self, state: &mut AggregateState, _value: &DataValue) -> Result<()> {
        if let AggregateState::Count(ref mut count) = state {
            *count += 1;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Count(count) = state {
            DataValue::Integer(count)
        } else {
            DataValue::Null
        }
    }
}

/// COUNT(column) - counts non-null values
pub struct CountFunction;

impl AggregateFunction for CountFunction {
    fn name(&self) -> &'static str {
        "COUNT"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Count(0)
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Count(ref mut count) = state {
            if !matches!(value, DataValue::Null) {
                *count += 1;
            }
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Count(count) = state {
            DataValue::Integer(count)
        } else {
            DataValue::Null
        }
    }
}

/// SUM(column) - sums numeric values
pub struct SumFunction;

impl AggregateFunction for SumFunction {
    fn name(&self) -> &'static str {
        "SUM"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Sum(SumState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Sum(ref mut sum_state) = state {
            sum_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Sum(sum_state) = state {
            sum_state.finalize()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

/// AVG(column) - averages numeric values
pub struct AvgFunction;

impl AggregateFunction for AvgFunction {
    fn name(&self) -> &'static str {
        "AVG"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Avg(AvgState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Avg(ref mut avg_state) = state {
            avg_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Avg(avg_state) = state {
            avg_state.finalize()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

/// MIN(column) - finds minimum value
pub struct MinFunction;

impl AggregateFunction for MinFunction {
    fn name(&self) -> &'static str {
        "MIN"
    }

    fn init(&self) -> AggregateState {
        AggregateState::MinMax(MinMaxState::new(true))
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::MinMax(ref mut minmax_state) = state {
            minmax_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::MinMax(minmax_state) = state {
            minmax_state.finalize()
        } else {
            DataValue::Null
        }
    }
}

/// MAX(column) - finds maximum value
pub struct MaxFunction;

impl AggregateFunction for MaxFunction {
    fn name(&self) -> &'static str {
        "MAX"
    }

    fn init(&self) -> AggregateState {
        AggregateState::MinMax(MinMaxState::new(false))
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::MinMax(ref mut minmax_state) = state {
            minmax_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::MinMax(minmax_state) = state {
            minmax_state.finalize()
        } else {
            DataValue::Null
        }
    }
}

/// VARIANCE(column) - computes population variance
pub struct VarianceFunction;

impl AggregateFunction for VarianceFunction {
    fn name(&self) -> &'static str {
        "VARIANCE"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_variance()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

/// STDDEV(column) - computes population standard deviation
pub struct StdDevFunction;

impl AggregateFunction for StdDevFunction {
    fn name(&self) -> &'static str {
        "STDDEV"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_stddev()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

/// STRING_AGG(column, separator) - concatenates strings with separator
pub struct StringAggFunction;

impl AggregateFunction for StringAggFunction {
    fn name(&self) -> &'static str {
        "STRING_AGG"
    }

    fn init(&self) -> AggregateState {
        AggregateState::StringAgg(StringAggState::new(","))
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::StringAgg(ref mut agg_state) = state {
            agg_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::StringAgg(agg_state) = state {
            agg_state.finalize()
        } else {
            DataValue::Null
        }
    }
}

/// MEDIAN(column) - finds the median value
pub struct MedianFunction;

impl AggregateFunction for MedianFunction {
    fn name(&self) -> &'static str {
        "MEDIAN"
    }

    fn init(&self) -> AggregateState {
        AggregateState::CollectList(Vec::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::CollectList(ref mut values) = state {
            // Skip null values
            if !matches!(value, DataValue::Null) {
                values.push(value.clone());
            }
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::CollectList(mut values) = state {
            if values.is_empty() {
                return DataValue::Null;
            }

            // Sort values for median calculation
            values.sort_by(|a, b| {
                use std::cmp::Ordering;
                match (a, b) {
                    (DataValue::Integer(a), DataValue::Integer(b)) => a.cmp(b),
                    (DataValue::Float(a), DataValue::Float(b)) => {
                        a.partial_cmp(b).unwrap_or(Ordering::Equal)
                    }
                    (DataValue::Integer(a), DataValue::Float(b)) => {
                        (*a as f64).partial_cmp(b).unwrap_or(Ordering::Equal)
                    }
                    (DataValue::Float(a), DataValue::Integer(b)) => {
                        a.partial_cmp(&(*b as f64)).unwrap_or(Ordering::Equal)
                    }
                    (DataValue::String(a), DataValue::String(b)) => a.cmp(b),
                    (DataValue::InternedString(a), DataValue::InternedString(b)) => a.cmp(b),
                    (DataValue::String(a), DataValue::InternedString(b)) => a.cmp(&**b),
                    (DataValue::InternedString(a), DataValue::String(b)) => (**a).cmp(b),
                    _ => Ordering::Equal,
                }
            });

            let len = values.len();
            if len % 2 == 1 {
                // Odd number of elements - return middle element
                values[len / 2].clone()
            } else {
                // Even number of elements - return average of two middle elements
                let mid1 = &values[len / 2 - 1];
                let mid2 = &values[len / 2];

                // For numeric values, calculate average
                match (mid1, mid2) {
                    (DataValue::Integer(a), DataValue::Integer(b)) => {
                        let avg = (*a + *b) as f64 / 2.0;
                        if avg.fract() == 0.0 {
                            DataValue::Integer(avg as i64)
                        } else {
                            DataValue::Float(avg)
                        }
                    }
                    (DataValue::Float(a), DataValue::Float(b)) => DataValue::Float((a + b) / 2.0),
                    (DataValue::Integer(a), DataValue::Float(b)) => {
                        DataValue::Float((*a as f64 + b) / 2.0)
                    }
                    (DataValue::Float(a), DataValue::Integer(b)) => {
                        DataValue::Float((a + *b as f64) / 2.0)
                    }
                    // For non-numeric, return the first middle element
                    _ => mid1.clone(),
                }
            }
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        false // MEDIAN works on any sortable type
    }
}

/// PERCENTILE(column, percentile) - finds the nth percentile value
pub struct PercentileFunction;

impl AggregateFunction for PercentileFunction {
    fn name(&self) -> &'static str {
        "PERCENTILE"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Percentile(PercentileState::new(50.0))
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Percentile(ref mut percentile_state) = state {
            percentile_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Percentile(percentile_state) = state {
            percentile_state.finalize()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true // PERCENTILE typically works on numeric data
    }
}

/// MODE(column) - finds the most frequently occurring value
pub struct ModeFunction;

/// STDDEV_POP(column) - computes population standard deviation (same as STDDEV)
pub struct StdDevPopFunction;

/// STDDEV_SAMP(column) - computes sample standard deviation
pub struct StdDevSampFunction;

/// VAR_POP(column) - computes population variance (same as VARIANCE)
pub struct VarPopFunction;

/// VAR_SAMP(column) - computes sample variance
pub struct VarSampFunction;

impl AggregateFunction for StdDevPopFunction {
    fn name(&self) -> &'static str {
        "STDDEV_POP"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_stddev()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

impl AggregateFunction for StdDevSampFunction {
    fn name(&self) -> &'static str {
        "STDDEV_SAMP"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_stddev_sample()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

impl AggregateFunction for VarPopFunction {
    fn name(&self) -> &'static str {
        "VAR_POP"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_variance()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

impl AggregateFunction for VarSampFunction {
    fn name(&self) -> &'static str {
        "VAR_SAMP"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Variance(VarianceState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Variance(ref mut var_state) = state {
            var_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Variance(var_state) = state {
            var_state.finalize_variance_sample()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        true
    }
}

impl AggregateFunction for ModeFunction {
    fn name(&self) -> &'static str {
        "MODE"
    }

    fn init(&self) -> AggregateState {
        AggregateState::Mode(ModeState::new())
    }

    fn accumulate(&self, state: &mut AggregateState, value: &DataValue) -> Result<()> {
        if let AggregateState::Mode(ref mut mode_state) = state {
            mode_state.add(value)?;
        }
        Ok(())
    }

    fn finalize(&self, state: AggregateState) -> DataValue {
        if let AggregateState::Mode(mode_state) = state {
            mode_state.finalize()
        } else {
            DataValue::Null
        }
    }

    fn requires_numeric(&self) -> bool {
        false // MODE works on any data type
    }
}

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

    #[test]
    fn test_count_star() {
        let func = CountStarFunction;
        let mut state = func.init();

        // COUNT(*) counts everything including nulls
        func.accumulate(&mut state, &DataValue::Integer(5)).unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap();
        func.accumulate(&mut state, &DataValue::String("test".to_string()))
            .unwrap();

        let result = func.finalize(state);
        assert_eq!(result, DataValue::Integer(3));
    }

    #[test]
    fn test_count_column() {
        let func = CountFunction;
        let mut state = func.init();

        // COUNT(column) skips nulls
        func.accumulate(&mut state, &DataValue::Integer(5)).unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap();
        func.accumulate(&mut state, &DataValue::String("test".to_string()))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap();

        let result = func.finalize(state);
        assert_eq!(result, DataValue::Integer(2));
    }

    #[test]
    fn test_sum_integers() {
        let func = SumFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(20))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(30))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Ignored

        let result = func.finalize(state);
        assert_eq!(result, DataValue::Integer(60));
    }

    #[test]
    fn test_sum_mixed() {
        let func = SumFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Float(20.5))
            .unwrap(); // Converts to float
        func.accumulate(&mut state, &DataValue::Integer(30))
            .unwrap();

        let result = func.finalize(state);
        match result {
            DataValue::Float(f) => assert!((f - 60.5).abs() < 0.001),
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_avg() {
        let func = AvgFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(20))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(30))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Ignored

        let result = func.finalize(state);
        match result {
            DataValue::Float(f) => assert!((f - 20.0).abs() < 0.001),
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_min() {
        let func = MinFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(30))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(20))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Ignored

        let result = func.finalize(state);
        assert_eq!(result, DataValue::Integer(10));
    }

    #[test]
    fn test_max() {
        let func = MaxFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(30))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(20))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Ignored

        let result = func.finalize(state);
        assert_eq!(result, DataValue::Integer(30));
    }

    #[test]
    fn test_max_strings() {
        let func = MaxFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::String("apple".to_string()))
            .unwrap();
        func.accumulate(&mut state, &DataValue::String("zebra".to_string()))
            .unwrap();
        func.accumulate(&mut state, &DataValue::String("banana".to_string()))
            .unwrap();

        let result = func.finalize(state);
        assert_eq!(result, DataValue::String("zebra".to_string()));
    }

    #[test]
    fn test_variance() {
        let func = VarianceFunction;
        let mut state = func.init();

        // Test data: [2, 4, 6, 8, 10]
        // Mean = 6, Variance = 8
        func.accumulate(&mut state, &DataValue::Integer(2)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(4)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(6)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(8)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();

        let result = func.finalize(state);
        match result {
            DataValue::Float(f) => assert!((f - 8.0).abs() < 0.001),
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_stddev() {
        let func = StdDevFunction;
        let mut state = func.init();

        // Test data: [2, 4, 6, 8, 10]
        // Mean = 6, Variance = 8, StdDev = sqrt(8) ≈ 2.828
        func.accumulate(&mut state, &DataValue::Integer(2)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(4)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(6)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(8)).unwrap();
        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();

        let result = func.finalize(state);
        match result {
            DataValue::Float(f) => assert!((f - 2.8284271247461903).abs() < 0.001),
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_variance_with_nulls() {
        let func = VarianceFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::Integer(5)).unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Should be ignored
        func.accumulate(&mut state, &DataValue::Integer(10))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Integer(15))
            .unwrap();

        let result = func.finalize(state);
        match result {
            DataValue::Float(f) => {
                // Mean = 10, values = [5, 10, 15]
                // Variance = ((5-10)² + (10-10)² + (15-10)²) / 3 = (25 + 0 + 25) / 3 ≈ 16.67
                assert!((f - 16.666666666666668).abs() < 0.001);
            }
            _ => panic!("Expected Float result"),
        }
    }

    #[test]
    fn test_string_agg() {
        let func = StringAggFunction;
        let mut state = func.init();

        func.accumulate(&mut state, &DataValue::String("apple".to_string()))
            .unwrap();
        func.accumulate(&mut state, &DataValue::String("banana".to_string()))
            .unwrap();
        func.accumulate(&mut state, &DataValue::Null).unwrap(); // Should be ignored
        func.accumulate(&mut state, &DataValue::String("cherry".to_string()))
            .unwrap();

        let result = func.finalize(state);
        assert_eq!(result, DataValue::String("apple,banana,cherry".to_string()));
    }
}