reddb-io-server 1.2.5

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
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
//! Window Functions Executor
//!
//! Provides SQL standard window functions for analytical queries.
//!
//! # Window Function Types
//!
//! **Ranking Functions:**
//! - `ROW_NUMBER()`: Sequential number within partition
//! - `RANK()`: Rank with gaps for ties
//! - `DENSE_RANK()`: Rank without gaps for ties
//! - `NTILE(n)`: Divide partition into n buckets
//! - `PERCENT_RANK()`: Relative rank as percentage
//! - `CUME_DIST()`: Cumulative distribution
//!
//! **Value Functions:**
//! - `FIRST_VALUE(x)`: First value in frame
//! - `LAST_VALUE(x)`: Last value in frame
//! - `NTH_VALUE(x, n)`: Nth value in frame
//! - `LAG(x, n, default)`: Value n rows before current
//! - `LEAD(x, n, default)`: Value n rows after current
//!
//! **Aggregate Functions (with OVER):**
//! - All standard aggregates (SUM, AVG, COUNT, MIN, MAX, etc.)
//!
//! # Frame Specification
//!
//! Frames define the subset of partition rows for each computation:
//! - `ROWS`: Physical row-based boundaries
//! - `RANGE`: Value-based logical boundaries
//! - `GROUPS`: Groups of peer rows
//!
//! # Implementation
//!
//! Window functions are evaluated in three phases:
//! 1. **Partition**: Group rows by PARTITION BY columns
//! 2. **Order**: Sort each partition by ORDER BY columns
//! 3. **Compute**: Apply window function with frame for each row

use std::cmp::Ordering;
use std::collections::HashMap;

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

// ============================================================================
// Window Function Types
// ============================================================================

/// Type of window function
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WindowFuncType {
    // Ranking functions
    RowNumber,
    Rank,
    DenseRank,
    Ntile(i64),
    PercentRank,
    CumeDist,

    // Value functions
    FirstValue(Var),
    LastValue(Var),
    NthValue(Var, i64),
    Lag(Var, i64, Option<Value>),
    Lead(Var, i64, Option<Value>),

    // Aggregate functions with OVER
    Aggregate(String, Var),
}

impl WindowFuncType {
    /// Create a ROW_NUMBER function
    pub fn row_number() -> Self {
        Self::RowNumber
    }

    /// Create a RANK function
    pub fn rank() -> Self {
        Self::Rank
    }

    /// Create a DENSE_RANK function
    pub fn dense_rank() -> Self {
        Self::DenseRank
    }

    /// Create an NTILE function
    pub fn ntile(n: i64) -> Self {
        Self::Ntile(n)
    }

    /// Create a LAG function
    pub fn lag(var: Var, offset: i64, default: Option<Value>) -> Self {
        Self::Lag(var, offset, default)
    }

    /// Create a LEAD function
    pub fn lead(var: Var, offset: i64, default: Option<Value>) -> Self {
        Self::Lead(var, offset, default)
    }

    /// Create a FIRST_VALUE function
    pub fn first_value(var: Var) -> Self {
        Self::FirstValue(var)
    }

    /// Create a LAST_VALUE function
    pub fn last_value(var: Var) -> Self {
        Self::LastValue(var)
    }

    /// Create an aggregate window function
    pub fn aggregate(name: &str, var: Var) -> Self {
        Self::Aggregate(name.to_uppercase(), var)
    }
}

// ============================================================================
// Frame Specification
// ============================================================================

/// Frame type for window functions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameType {
    /// Physical row boundaries
    Rows,
    /// Value-based boundaries (for RANGE)
    Range,
    /// Groups of peer rows
    Groups,
}

/// Frame boundary specification
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum FrameBound {
    /// UNBOUNDED PRECEDING
    UnboundedPreceding,
    /// UNBOUNDED FOLLOWING
    UnboundedFollowing,
    /// CURRENT ROW
    #[default]
    CurrentRow,
    /// n PRECEDING
    Preceding(i64),
    /// n FOLLOWING
    Following(i64),
}

/// Frame specification for window function
#[derive(Debug, Clone)]
pub struct FrameSpec {
    /// Frame type (ROWS, RANGE, GROUPS)
    pub frame_type: FrameType,
    /// Start boundary
    pub start: FrameBound,
    /// End boundary
    pub end: FrameBound,
    /// Exclude option (CURRENT ROW, GROUP, TIES, NO OTHERS)
    pub exclude: FrameExclude,
}

/// Frame exclusion option
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrameExclude {
    /// EXCLUDE NO OTHERS (default)
    #[default]
    NoOthers,
    /// EXCLUDE CURRENT ROW
    CurrentRow,
    /// EXCLUDE GROUP
    Group,
    /// EXCLUDE TIES
    Ties,
}

impl Default for FrameSpec {
    /// Default frame: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    fn default() -> Self {
        Self {
            frame_type: FrameType::Range,
            start: FrameBound::UnboundedPreceding,
            end: FrameBound::CurrentRow,
            exclude: FrameExclude::NoOthers,
        }
    }
}

impl FrameSpec {
    /// Create ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    pub fn entire_partition() -> Self {
        Self {
            frame_type: FrameType::Rows,
            start: FrameBound::UnboundedPreceding,
            end: FrameBound::UnboundedFollowing,
            exclude: FrameExclude::NoOthers,
        }
    }

    /// Create ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    pub fn running() -> Self {
        Self {
            frame_type: FrameType::Rows,
            start: FrameBound::UnboundedPreceding,
            end: FrameBound::CurrentRow,
            exclude: FrameExclude::NoOthers,
        }
    }

    /// Create ROWS BETWEEN n PRECEDING AND CURRENT ROW (sliding window)
    pub fn sliding(n: i64) -> Self {
        Self {
            frame_type: FrameType::Rows,
            start: FrameBound::Preceding(n),
            end: FrameBound::CurrentRow,
            exclude: FrameExclude::NoOthers,
        }
    }
}

// ============================================================================
// Window Definition
// ============================================================================

/// Sort direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortDirection {
    #[default]
    Asc,
    Desc,
}

/// Null handling in sorting
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NullsOrder {
    #[default]
    First,
    Last,
}

/// Order-by specification for window
#[derive(Debug, Clone)]
pub struct WindowOrderBy {
    /// Variable to sort by
    pub var: Var,
    /// Sort direction
    pub direction: SortDirection,
    /// Null handling
    pub nulls: NullsOrder,
}

impl WindowOrderBy {
    pub fn new(var: Var) -> Self {
        Self {
            var,
            direction: SortDirection::Asc,
            nulls: NullsOrder::Last,
        }
    }

    pub fn desc(mut self) -> Self {
        self.direction = SortDirection::Desc;
        self
    }

    pub fn nulls_first(mut self) -> Self {
        self.nulls = NullsOrder::First;
        self
    }
}

/// Complete window definition
#[derive(Debug, Clone, Default)]
pub struct WindowDef {
    /// Optional window name
    pub name: Option<String>,
    /// Partition by variables
    pub partition_by: Vec<Var>,
    /// Order by specifications
    pub order_by: Vec<WindowOrderBy>,
    /// Frame specification
    pub frame: FrameSpec,
}

impl WindowDef {
    /// Create a window with partition by
    pub fn partition_by(vars: Vec<Var>) -> Self {
        Self {
            partition_by: vars,
            ..Default::default()
        }
    }

    /// Add order by
    pub fn with_order_by(mut self, order: Vec<WindowOrderBy>) -> Self {
        self.order_by = order;
        self
    }

    /// Set frame specification
    pub fn with_frame(mut self, frame: FrameSpec) -> Self {
        self.frame = frame;
        self
    }
}

// ============================================================================
// Window Function Application
// ============================================================================

/// A window function to apply
#[derive(Debug, Clone)]
pub struct WindowFunc {
    /// The function type
    pub func_type: WindowFuncType,
    /// Result variable name
    pub result_var: Var,
    /// Window definition
    pub window: WindowDef,
}

impl WindowFunc {
    /// Create a new window function
    pub fn new(func_type: WindowFuncType, result_var: Var, window: WindowDef) -> Self {
        Self {
            func_type,
            result_var,
            window,
        }
    }
}

// ============================================================================
// Window Executor
// ============================================================================

/// Window function executor
pub struct WindowExecutor;

#[derive(Debug, Clone)]
struct IndexedBinding {
    index: usize,
    binding: Binding,
}

impl WindowExecutor {
    /// Execute window functions on bindings
    pub fn execute(bindings: Vec<Binding>, functions: &[WindowFunc]) -> Vec<Binding> {
        if bindings.is_empty() || functions.is_empty() {
            return bindings;
        }

        // We need to execute each window function
        let mut result = bindings;

        for func in functions {
            result = Self::apply_window_function(&result, func);
        }

        result
    }

    /// Apply a single window function
    fn apply_window_function(bindings: &[Binding], func: &WindowFunc) -> Vec<Binding> {
        // Step 1: Partition the data
        let partitions = Self::partition_bindings(bindings, &func.window.partition_by);

        // Step 2: For each partition, sort and compute
        let mut result: Vec<Option<Binding>> = vec![None; bindings.len()];

        for (_key, mut partition) in partitions {
            // Sort within partition
            Self::sort_partition(&mut partition, &func.window.order_by);

            // Compute window function for each row
            let computed = Self::compute_for_partition(&partition, func);
            for entry in computed {
                if entry.index < result.len() {
                    result[entry.index] = Some(entry.binding);
                }
            }
        }

        result
            .into_iter()
            .enumerate()
            .map(|(idx, binding)| binding.unwrap_or_else(|| bindings[idx].clone()))
            .collect()
    }

    /// Partition bindings by partition-by variables
    fn partition_bindings(
        bindings: &[Binding],
        partition_by: &[Var],
    ) -> Vec<(Vec<Option<Value>>, Vec<IndexedBinding>)> {
        if partition_by.is_empty() {
            // No partitioning - single partition with all rows
            let entries = bindings
                .iter()
                .cloned()
                .enumerate()
                .map(|(index, binding)| IndexedBinding { index, binding })
                .collect();
            return vec![(vec![], entries)];
        }

        let mut partitions: HashMap<Vec<Option<Value>>, Vec<IndexedBinding>> = HashMap::new();
        let mut key_order: Vec<Vec<Option<Value>>> = Vec::new();

        for (index, binding) in bindings.iter().cloned().enumerate() {
            let key_values: Vec<Option<Value>> = partition_by
                .iter()
                .map(|v| binding.get(v).cloned())
                .collect();

            if !partitions.contains_key(&key_values) {
                key_order.push(key_values.clone());
            }

            partitions
                .entry(key_values)
                .or_default()
                .push(IndexedBinding { index, binding });
        }

        // Maintain insertion order
        key_order
            .into_iter()
            .filter_map(|values| partitions.remove(&values).map(|rows| (values, rows)))
            .collect()
    }

    /// Sort partition by order-by specifications
    fn sort_partition(partition: &mut [IndexedBinding], order_by: &[WindowOrderBy]) {
        if order_by.is_empty() {
            return;
        }

        partition.sort_by(|a, b| {
            for spec in order_by {
                let val_a = a.binding.get(&spec.var);
                let val_b = b.binding.get(&spec.var);

                let cmp = match (val_a, val_b) {
                    (None, None) => Ordering::Equal,
                    (None, Some(_)) => match spec.nulls {
                        NullsOrder::First => Ordering::Less,
                        NullsOrder::Last => Ordering::Greater,
                    },
                    (Some(_), None) => match spec.nulls {
                        NullsOrder::First => Ordering::Greater,
                        NullsOrder::Last => Ordering::Less,
                    },
                    (Some(a), Some(b)) => total_compare_values(a, b),
                };

                if cmp != Ordering::Equal {
                    return match spec.direction {
                        SortDirection::Asc => cmp,
                        SortDirection::Desc => cmp.reverse(),
                    };
                }
            }
            a.index.cmp(&b.index)
        });
    }

    /// Compute window function for each row in a partition
    fn compute_for_partition(
        partition: &[IndexedBinding],
        func: &WindowFunc,
    ) -> Vec<IndexedBinding> {
        let partition_size = partition.len();

        // Pre-compute peer groups for ranking functions
        let peer_groups = Self::compute_peer_groups(partition, &func.window.order_by);

        partition
            .iter()
            .enumerate()
            .map(|(row_idx, indexed)| {
                let value =
                    Self::compute_value(partition, row_idx, &peer_groups, partition_size, func);

                // Add result to binding
                let result_binding = Binding::one(func.result_var.clone(), value);
                let binding = indexed
                    .binding
                    .merge(&result_binding)
                    .unwrap_or_else(|| indexed.binding.clone());
                IndexedBinding {
                    index: indexed.index,
                    binding,
                }
            })
            .collect()
    }

    /// Compute peer groups (rows with same ORDER BY values)
    fn compute_peer_groups(partition: &[IndexedBinding], order_by: &[WindowOrderBy]) -> Vec<usize> {
        if order_by.is_empty() {
            // No ordering - all rows are peers (single group)
            return vec![0; partition.len()];
        }

        let mut groups = Vec::with_capacity(partition.len());
        let mut current_group = 0;

        for (idx, indexed) in partition.iter().enumerate() {
            if idx == 0 {
                groups.push(0);
                continue;
            }

            let prev = &partition[idx - 1].binding;
            let binding = &indexed.binding;
            let is_peer = order_by.iter().all(|spec| {
                let a = prev.get(&spec.var);
                let b = binding.get(&spec.var);
                match (a, b) {
                    (None, None) => true,
                    (Some(va), Some(vb)) => values_equal(va, vb),
                    _ => false,
                }
            });

            if !is_peer {
                current_group += 1;
            }
            groups.push(current_group);
        }

        groups
    }

    /// Compute value for a single row
    fn compute_value(
        partition: &[IndexedBinding],
        row_idx: usize,
        peer_groups: &[usize],
        partition_size: usize,
        func: &WindowFunc,
    ) -> Value {
        match &func.func_type {
            // Ranking functions
            WindowFuncType::RowNumber => Value::Integer((row_idx + 1) as i64),

            WindowFuncType::Rank => {
                // Rank = position of first row in current peer group + 1
                let current_group = peer_groups[row_idx];
                let first_in_group = peer_groups
                    .iter()
                    .position(|&g| g == current_group)
                    .unwrap();
                Value::Integer((first_in_group + 1) as i64)
            }

            WindowFuncType::DenseRank => {
                // Dense rank = peer group number + 1
                Value::Integer((peer_groups[row_idx] + 1) as i64)
            }

            WindowFuncType::Ntile(n) => {
                // Divide into n buckets
                let n = *n as usize;
                if n == 0 || partition_size == 0 {
                    return Value::Null;
                }
                let bucket_size = partition_size / n;
                let remainder = partition_size % n;

                // Rows are distributed: first `remainder` buckets get one extra row
                let mut row = 0;
                let mut bucket = 1;
                for i in 0..n {
                    let size = bucket_size + if i < remainder { 1 } else { 0 };
                    if row_idx < row + size {
                        bucket = i + 1;
                        break;
                    }
                    row += size;
                }
                Value::Integer(bucket as i64)
            }

            WindowFuncType::PercentRank => {
                // (rank - 1) / (partition_size - 1)
                if partition_size <= 1 {
                    return Value::Float(0.0);
                }
                let current_group = peer_groups[row_idx];
                let first_in_group = peer_groups
                    .iter()
                    .position(|&g| g == current_group)
                    .unwrap();
                let rank = first_in_group as f64;
                Value::Float(rank / (partition_size - 1) as f64)
            }

            WindowFuncType::CumeDist => {
                // count of rows <= current row / partition_size
                let current_group = peer_groups[row_idx];
                // Count all rows up to and including current peer group
                let count = peer_groups.iter().filter(|&&g| g <= current_group).count();
                Value::Float(count as f64 / partition_size as f64)
            }

            // Value functions
            WindowFuncType::FirstValue(var) => {
                // Get frame bounds
                let (start, _) = Self::get_frame_bounds(
                    row_idx,
                    partition_size,
                    peer_groups,
                    &func.window.frame,
                );
                partition
                    .get(start)
                    .and_then(|b| b.binding.get(var))
                    .cloned()
                    .unwrap_or(Value::Null)
            }

            WindowFuncType::LastValue(var) => {
                let (_, end) = Self::get_frame_bounds(
                    row_idx,
                    partition_size,
                    peer_groups,
                    &func.window.frame,
                );
                // End is exclusive, so use end - 1
                if end > 0 {
                    partition
                        .get(end - 1)
                        .and_then(|b| b.binding.get(var))
                        .cloned()
                        .unwrap_or(Value::Null)
                } else {
                    Value::Null
                }
            }

            WindowFuncType::NthValue(var, n) => {
                let (start, end) = Self::get_frame_bounds(
                    row_idx,
                    partition_size,
                    peer_groups,
                    &func.window.frame,
                );
                let n = *n as usize;
                if n == 0 {
                    return Value::Null;
                }
                let target_idx = start + n - 1;
                if target_idx < end {
                    partition
                        .get(target_idx)
                        .and_then(|b| b.binding.get(var))
                        .cloned()
                        .unwrap_or(Value::Null)
                } else {
                    Value::Null
                }
            }

            WindowFuncType::Lag(var, offset, default) => {
                let offset = *offset as usize;
                if row_idx >= offset {
                    partition
                        .get(row_idx - offset)
                        .and_then(|b| b.binding.get(var))
                        .cloned()
                        .unwrap_or_else(|| default.clone().unwrap_or(Value::Null))
                } else {
                    default.clone().unwrap_or(Value::Null)
                }
            }

            WindowFuncType::Lead(var, offset, default) => {
                let offset = *offset as usize;
                let target = row_idx + offset;
                if target < partition_size {
                    partition
                        .get(target)
                        .and_then(|b| b.binding.get(var))
                        .cloned()
                        .unwrap_or_else(|| default.clone().unwrap_or(Value::Null))
                } else {
                    default.clone().unwrap_or(Value::Null)
                }
            }

            // Aggregate functions
            WindowFuncType::Aggregate(agg_name, var) => {
                let (start, end) = Self::get_frame_bounds(
                    row_idx,
                    partition_size,
                    peer_groups,
                    &func.window.frame,
                );

                if let Some(mut aggregator) = create_aggregator(agg_name) {
                    for i in start..end {
                        if let Some(binding) = partition.get(i) {
                            let value = binding.binding.get(var);
                            aggregator.accumulate(value);
                        }
                    }
                    aggregator.finalize()
                } else {
                    Value::Null
                }
            }
        }
    }

    /// Get frame bounds (start, end) for current row
    /// Returns (inclusive start, exclusive end)
    fn get_frame_bounds(
        row_idx: usize,
        partition_size: usize,
        peer_groups: &[usize],
        frame: &FrameSpec,
    ) -> (usize, usize) {
        let start = match &frame.start {
            FrameBound::UnboundedPreceding => 0,
            FrameBound::CurrentRow => {
                match frame.frame_type {
                    FrameType::Rows => row_idx,
                    FrameType::Range | FrameType::Groups => {
                        // Start of current peer group
                        let group = peer_groups[row_idx];
                        peer_groups
                            .iter()
                            .position(|&g| g == group)
                            .unwrap_or(row_idx)
                    }
                }
            }
            FrameBound::Preceding(n) => {
                match frame.frame_type {
                    FrameType::Rows => row_idx.saturating_sub(*n as usize),
                    FrameType::Groups => {
                        // n groups preceding
                        let current_group = peer_groups[row_idx];
                        let target_group = current_group.saturating_sub(*n as usize);
                        peer_groups
                            .iter()
                            .position(|&g| g == target_group)
                            .unwrap_or(0)
                    }
                    FrameType::Range => row_idx.saturating_sub(*n as usize),
                }
            }
            FrameBound::Following(n) => match frame.frame_type {
                FrameType::Rows => (row_idx + *n as usize).min(partition_size),
                FrameType::Groups => {
                    let current_group = peer_groups[row_idx];
                    let target_group = current_group + *n as usize;
                    peer_groups
                        .iter()
                        .position(|&g| g >= target_group)
                        .unwrap_or(partition_size)
                }
                FrameType::Range => (row_idx + *n as usize).min(partition_size),
            },
            FrameBound::UnboundedFollowing => partition_size,
        };

        let end = match &frame.end {
            FrameBound::UnboundedFollowing => partition_size,
            FrameBound::CurrentRow => {
                match frame.frame_type {
                    FrameType::Rows => row_idx + 1,
                    FrameType::Range | FrameType::Groups => {
                        // End of current peer group (exclusive)
                        let group = peer_groups[row_idx];
                        peer_groups
                            .iter()
                            .position(|&g| g > group)
                            .unwrap_or(partition_size)
                    }
                }
            }
            FrameBound::Preceding(n) => match frame.frame_type {
                FrameType::Rows => row_idx.saturating_sub(*n as usize) + 1,
                FrameType::Groups => {
                    let current_group = peer_groups[row_idx];
                    let target_group = current_group.saturating_sub(*n as usize);
                    peer_groups
                        .iter()
                        .position(|&g| g > target_group)
                        .unwrap_or(partition_size)
                }
                FrameType::Range => row_idx.saturating_sub(*n as usize) + 1,
            },
            FrameBound::Following(n) => match frame.frame_type {
                FrameType::Rows => (row_idx + *n as usize + 1).min(partition_size),
                FrameType::Groups => {
                    let current_group = peer_groups[row_idx];
                    let target_group = current_group + *n as usize;
                    peer_groups
                        .iter()
                        .position(|&g| g > target_group)
                        .unwrap_or(partition_size)
                }
                FrameType::Range => (row_idx + *n as usize + 1).min(partition_size),
            },
            FrameBound::UnboundedPreceding => 0, // Invalid but handle gracefully
        };

        (
            start.min(partition_size),
            end.min(partition_size).max(start),
        )
    }
}

// ============================================================================
// 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
    }

    fn get_values(bindings: &[Binding], var: &str) -> Vec<i64> {
        let v = Var::new(var);
        bindings
            .iter()
            .filter_map(|b| b.get(&v))
            .filter_map(|v| match v {
                Value::Integer(i) => Some(*i),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn test_row_number() {
        let bindings = vec![
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("salary", Value::Integer(100)),
            ]),
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("salary", Value::Integer(200)),
            ]),
            make_binding(&[
                ("dept", Value::String("B".to_string())),
                ("salary", Value::Integer(150)),
            ]),
        ];

        let func = WindowFunc::new(
            WindowFuncType::RowNumber,
            Var::new("rn"),
            WindowDef::partition_by(vec![Var::new("dept")])
                .with_order_by(vec![WindowOrderBy::new(Var::new("salary"))]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);

        // Each partition should have row numbers starting from 1
        let rns = get_values(&result, "rn");
        assert_eq!(rns, vec![1, 2, 1]); // A: 1,2 and B: 1
    }

    #[test]
    fn test_rank_with_ties() {
        let bindings = vec![
            make_binding(&[("score", Value::Integer(100))]),
            make_binding(&[("score", Value::Integer(100))]), // Tie
            make_binding(&[("score", Value::Integer(90))]),
            make_binding(&[("score", Value::Integer(80))]),
        ];

        let func = WindowFunc::new(
            WindowFuncType::Rank,
            Var::new("rank"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("score")).desc()]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let ranks = get_values(&result, "rank");

        // 100 = rank 1, 100 = rank 1 (tie), 90 = rank 3 (skip 2), 80 = rank 4
        assert_eq!(ranks, vec![1, 1, 3, 4]);
    }

    #[test]
    fn test_dense_rank() {
        let bindings = vec![
            make_binding(&[("score", Value::Integer(100))]),
            make_binding(&[("score", Value::Integer(100))]),
            make_binding(&[("score", Value::Integer(90))]),
        ];

        let func = WindowFunc::new(
            WindowFuncType::DenseRank,
            Var::new("drank"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("score")).desc()]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let ranks = get_values(&result, "drank");

        // No gaps: 1, 1, 2
        assert_eq!(ranks, vec![1, 1, 2]);
    }

    #[test]
    fn test_ntile() {
        let bindings: Vec<Binding> = (1..=10)
            .map(|i| make_binding(&[("val", Value::Integer(i))]))
            .collect();

        let func = WindowFunc::new(
            WindowFuncType::Ntile(4),
            Var::new("bucket"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("val"))]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let buckets = get_values(&result, "bucket");

        // 10 rows into 4 buckets: 3, 3, 2, 2 rows per bucket
        assert_eq!(buckets, vec![1, 1, 1, 2, 2, 2, 3, 3, 4, 4]);
    }

    #[test]
    fn test_lag_lead() {
        let bindings: Vec<Binding> = (1..=5)
            .map(|i| make_binding(&[("val", Value::Integer(i))]))
            .collect();

        let lag_func = WindowFunc::new(
            WindowFuncType::Lag(Var::new("val"), 1, Some(Value::Integer(0))),
            Var::new("prev"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("val"))]),
        );

        let lead_func = WindowFunc::new(
            WindowFuncType::Lead(Var::new("val"), 1, Some(Value::Integer(0))),
            Var::new("next"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("val"))]),
        );

        let result = WindowExecutor::execute(bindings, &[lag_func, lead_func]);
        let prevs = get_values(&result, "prev");
        let nexts = get_values(&result, "next");

        assert_eq!(prevs, vec![0, 1, 2, 3, 4]); // LAG(val, 1, 0)
        assert_eq!(nexts, vec![2, 3, 4, 5, 0]); // LEAD(val, 1, 0)
    }

    #[test]
    fn test_running_sum() {
        let bindings: Vec<Binding> = (1..=5)
            .map(|i| make_binding(&[("val", Value::Integer(i))]))
            .collect();

        let func = WindowFunc::new(
            WindowFuncType::Aggregate("SUM".to_string(), Var::new("val")),
            Var::new("running_sum"),
            WindowDef::default()
                .with_order_by(vec![WindowOrderBy::new(Var::new("val"))])
                .with_frame(FrameSpec::running()),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let sums = get_values(&result, "running_sum");

        // Running sum: 1, 1+2=3, 1+2+3=6, ...
        assert_eq!(sums, vec![1, 3, 6, 10, 15]);
    }

    #[test]
    fn test_first_last_value() {
        let bindings: Vec<Binding> = (1..=5)
            .map(|i| make_binding(&[("val", Value::Integer(i))]))
            .collect();

        let first_func = WindowFunc::new(
            WindowFuncType::FirstValue(Var::new("val")),
            Var::new("first"),
            WindowDef::default()
                .with_order_by(vec![WindowOrderBy::new(Var::new("val"))])
                .with_frame(FrameSpec::entire_partition()),
        );

        let last_func = WindowFunc::new(
            WindowFuncType::LastValue(Var::new("val")),
            Var::new("last"),
            WindowDef::default()
                .with_order_by(vec![WindowOrderBy::new(Var::new("val"))])
                .with_frame(FrameSpec::entire_partition()),
        );

        let result = WindowExecutor::execute(bindings, &[first_func, last_func]);
        let firsts = get_values(&result, "first");
        let lasts = get_values(&result, "last");

        assert_eq!(firsts, vec![1, 1, 1, 1, 1]); // First value of partition
        assert_eq!(lasts, vec![5, 5, 5, 5, 5]); // Last value of partition
    }

    #[test]
    fn test_partitioned_sum() {
        let bindings = vec![
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("salary", Value::Integer(100)),
            ]),
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("salary", Value::Integer(200)),
            ]),
            make_binding(&[
                ("dept", Value::String("B".to_string())),
                ("salary", Value::Integer(150)),
            ]),
            make_binding(&[
                ("dept", Value::String("B".to_string())),
                ("salary", Value::Integer(250)),
            ]),
        ];

        let func = WindowFunc::new(
            WindowFuncType::Aggregate("SUM".to_string(), Var::new("salary")),
            Var::new("dept_total"),
            WindowDef::partition_by(vec![Var::new("dept")])
                .with_frame(FrameSpec::entire_partition()),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let totals = get_values(&result, "dept_total");

        // A: 100+200=300, B: 150+250=400
        assert_eq!(totals, vec![300, 300, 400, 400]);
    }

    #[test]
    fn test_window_preserves_input_order() {
        let bindings = vec![
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("seq", Value::Integer(1)),
            ]),
            make_binding(&[
                ("dept", Value::String("B".to_string())),
                ("seq", Value::Integer(1)),
            ]),
            make_binding(&[
                ("dept", Value::String("A".to_string())),
                ("seq", Value::Integer(2)),
            ]),
            make_binding(&[
                ("dept", Value::String("B".to_string())),
                ("seq", Value::Integer(2)),
            ]),
        ];

        let func = WindowFunc::new(
            WindowFuncType::RowNumber,
            Var::new("rn"),
            WindowDef::partition_by(vec![Var::new("dept")])
                .with_order_by(vec![WindowOrderBy::new(Var::new("seq"))]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);
        let dept_var = Var::new("dept");
        let depts: Vec<String> = result
            .iter()
            .filter_map(|b| b.get(&dept_var))
            .filter_map(|v| match v {
                Value::String(s) => Some(s.clone()),
                _ => None,
            })
            .collect();

        assert_eq!(depts, vec!["A", "B", "A", "B"]);
        assert_eq!(get_values(&result, "rn"), vec![1, 1, 2, 2]);
    }

    #[test]
    fn test_percent_rank() {
        let bindings: Vec<Binding> = (1..=4)
            .map(|i| make_binding(&[("val", Value::Integer(i))]))
            .collect();

        let func = WindowFunc::new(
            WindowFuncType::PercentRank,
            Var::new("prank"),
            WindowDef::default().with_order_by(vec![WindowOrderBy::new(Var::new("val"))]),
        );

        let result = WindowExecutor::execute(bindings, &[func]);

        // Check percent ranks: 0, 0.333..., 0.666..., 1.0
        for (i, binding) in result.iter().enumerate() {
            if let Some(Value::Float(pr)) = binding.get(&Var::new("prank")) {
                let expected = i as f64 / 3.0;
                assert!(
                    (pr - expected).abs() < 0.001,
                    "Row {}: expected {}, got {}",
                    i,
                    expected,
                    pr
                );
            }
        }
    }
}