sql-cli 1.69.3

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
786
787
788
789
790
791
792
793
794
795
796
// Window Function Registry
// Provides a clean API for window computations with syntactic sugar

use anyhow::{anyhow, Result};
use std::collections::HashMap;
use std::sync::Arc;

use crate::data::datatable::DataValue;
use crate::sql::parser::ast::{SqlExpression, WindowSpec};
use crate::sql::window_context::WindowContext;

// Aggregate window functions module
mod aggregates;
use aggregates::*;

/// Window function computation trait
/// Each window function receives:
/// - The window context (partitions, ordering, frames)
/// - The current row index
/// - Arguments (column names, parameters)
pub trait WindowFunction: Send + Sync {
    /// Function name (e.g., "MOVING_AVG")
    fn name(&self) -> &str;

    /// Description for help system
    fn description(&self) -> &str;

    /// Signature for documentation (e.g., "MOVING_AVG(column, window_size)")
    fn signature(&self) -> &str;

    /// Compute the function value for a specific row
    /// This is called once per row in the result set
    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue>;

    /// Optional: Transform/expand the window specification
    /// This allows functions to modify the window (e.g., MOVING_AVG sets ROWS n PRECEDING)
    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        _args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        // Default: use the base spec unchanged
        Ok(base_spec.clone())
    }

    /// Validate arguments at parse time
    fn validate_args(&self, _args: &[SqlExpression]) -> Result<()> {
        Ok(())
    }
}

/// Expression evaluator trait for evaluating arguments
/// This allows window functions to evaluate expressions without depending on ArithmeticEvaluator
pub trait ExpressionEvaluator {
    fn evaluate(&mut self, expr: &SqlExpression, row_index: usize) -> Result<DataValue>;
}

/// Registry for window functions
pub struct WindowFunctionRegistry {
    functions: HashMap<String, Arc<Box<dyn WindowFunction>>>,
}

impl WindowFunctionRegistry {
    pub fn new() -> Self {
        let mut registry = Self {
            functions: HashMap::new(),
        };
        registry.register_builtin_functions();
        registry
    }

    /// Register a window function
    pub fn register(&mut self, function: Box<dyn WindowFunction>) {
        let name = function.name().to_uppercase();
        self.functions.insert(name, Arc::new(function));
    }

    /// Get a window function by name
    pub fn get(&self, name: &str) -> Option<Arc<Box<dyn WindowFunction>>> {
        self.functions.get(&name.to_uppercase()).cloned()
    }

    /// Check if a function exists
    pub fn contains(&self, name: &str) -> bool {
        self.functions.contains_key(&name.to_uppercase())
    }

    /// List all registered functions
    pub fn list_functions(&self) -> Vec<String> {
        self.functions.keys().cloned().collect()
    }

    /// Register built-in syntactic sugar functions
    fn register_builtin_functions(&mut self) {
        // Window aggregate functions that can handle expressions
        self.register(Box::new(WindowSumFunction));
        self.register(Box::new(WindowAvgFunction));
        self.register(Box::new(WindowMinFunction));
        self.register(Box::new(WindowMaxFunction));
        self.register(Box::new(WindowCountFunction));
        self.register(Box::new(WindowStddevFunction));
        self.register(Box::new(WindowStdevFunction)); // Alias for STDDEV
        self.register(Box::new(WindowVarianceFunction));
        self.register(Box::new(WindowVarFunction)); // Alias for VARIANCE

        // Moving average and statistics
        self.register(Box::new(MovingAvgFunction));
        self.register(Box::new(RollingStddevFunction));
        self.register(Box::new(CumulativeSumFunction));
        self.register(Box::new(CumulativeAvgFunction));
        self.register(Box::new(ZScoreFunction));

        // Bollinger Bands
        self.register(Box::new(BollingerUpperFunction));
        self.register(Box::new(BollingerLowerFunction));

        // Financial calculations
        self.register(Box::new(PercentChangeFunction));

        // Add more as we implement them
    }
}

// ============= Syntactic Sugar Implementations =============

/// MOVING_AVG(column, window_size)
/// Expands to: AVG(column) OVER (ORDER BY <inherited> ROWS window_size-1 PRECEDING)
struct MovingAvgFunction;

impl WindowFunction for MovingAvgFunction {
    fn name(&self) -> &str {
        "MOVING_AVG"
    }

    fn description(&self) -> &str {
        "Calculate moving average over specified window size"
    }

    fn signature(&self) -> &str {
        "MOVING_AVG(column, window_size)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        // Extract column name
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => {
                return Err(anyhow::anyhow!(
                    "MOVING_AVG first argument must be a column"
                ))
            }
        };

        // The window has already been configured by transform_window_spec
        // Just compute the average over the frame
        context
            .get_frame_avg(row_index, &column.name)
            .ok_or_else(|| anyhow::anyhow!("Failed to compute moving average"))
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        // Extract window size from second argument
        let window_size = match &args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i64>()
                .map_err(|_| anyhow::anyhow!("Invalid window size"))?,
            _ => return Err(anyhow::anyhow!("MOVING_AVG requires numeric window_size")),
        };

        // Create a new spec with ROWS n-1 PRECEDING frame
        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::Preceding(window_size - 1),
            end: None, // Defaults to CURRENT ROW
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() != 2 {
            return Err(anyhow::anyhow!("MOVING_AVG requires exactly 2 arguments"));
        }
        Ok(())
    }
}

/// ROLLING_STDDEV(column, window_size)
/// Expands to: STDDEV(column) OVER (ORDER BY <inherited> ROWS window_size-1 PRECEDING)
struct RollingStddevFunction;

impl WindowFunction for RollingStddevFunction {
    fn name(&self) -> &str {
        "ROLLING_STDDEV"
    }

    fn description(&self) -> &str {
        "Calculate rolling standard deviation over specified window"
    }

    fn signature(&self) -> &str {
        "ROLLING_STDDEV(column, window_size)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => {
                return Err(anyhow::anyhow!(
                    "ROLLING_STDDEV first argument must be a column"
                ))
            }
        };

        context
            .get_frame_stddev(row_index, &column.name)
            .ok_or_else(|| anyhow::anyhow!("Failed to compute rolling stddev"))
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let window_size = match &args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i64>()
                .map_err(|_| anyhow::anyhow!("Invalid window size"))?,
            _ => {
                return Err(anyhow::anyhow!(
                    "ROLLING_STDDEV requires numeric window_size"
                ))
            }
        };

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::Preceding(window_size - 1),
            end: None,
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() != 2 {
            return Err(anyhow::anyhow!(
                "ROLLING_STDDEV requires exactly 2 arguments"
            ));
        }
        Ok(())
    }
}

/// CUMULATIVE_SUM(column)
/// Expands to: SUM(column) OVER (ORDER BY <inherited> ROWS UNBOUNDED PRECEDING)
struct CumulativeSumFunction;

impl WindowFunction for CumulativeSumFunction {
    fn name(&self) -> &str {
        "CUMULATIVE_SUM"
    }

    fn description(&self) -> &str {
        "Calculate cumulative sum from beginning to current row"
    }

    fn signature(&self) -> &str {
        "CUMULATIVE_SUM(column)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow::anyhow!("CUMULATIVE_SUM argument must be a column")),
        };

        context
            .get_frame_sum(row_index, &column.name)
            .ok_or_else(|| anyhow::anyhow!("Failed to compute cumulative sum"))
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        _args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::UnboundedPreceding,
            end: None, // CURRENT ROW
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() != 1 {
            return Err(anyhow::anyhow!(
                "CUMULATIVE_SUM requires exactly 1 argument"
            ));
        }
        Ok(())
    }
}

/// CUMULATIVE_AVG(column)
/// Expands to: AVG(column) OVER (ORDER BY <inherited> ROWS UNBOUNDED PRECEDING)
struct CumulativeAvgFunction;

impl WindowFunction for CumulativeAvgFunction {
    fn name(&self) -> &str {
        "CUMULATIVE_AVG"
    }

    fn description(&self) -> &str {
        "Calculate cumulative average from beginning to current row"
    }

    fn signature(&self) -> &str {
        "CUMULATIVE_AVG(column)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow::anyhow!("CUMULATIVE_AVG argument must be a column")),
        };

        context
            .get_frame_avg(row_index, &column.name)
            .ok_or_else(|| anyhow::anyhow!("Failed to compute cumulative average"))
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        _args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::UnboundedPreceding,
            end: None,
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() != 1 {
            return Err(anyhow::anyhow!(
                "CUMULATIVE_AVG requires exactly 1 argument"
            ));
        }
        Ok(())
    }
}

/// Z_SCORE(column, window_size)
/// Calculates: (value - mean) / stddev over the window
struct ZScoreFunction;

impl WindowFunction for ZScoreFunction {
    fn name(&self) -> &str {
        "Z_SCORE"
    }

    fn description(&self) -> &str {
        "Calculate Z-score (standard deviations from mean) over window"
    }

    fn signature(&self) -> &str {
        "Z_SCORE(column, window_size)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow::anyhow!("Z_SCORE first argument must be a column")),
        };

        // Get current value
        let current_value = {
            let source = context.source();
            let col_idx = source
                .get_column_index(&column.name)
                .ok_or_else(|| anyhow::anyhow!("Column {} not found", column))?;
            source
                .get_value(row_index, col_idx)
                .cloned()
                .unwrap_or(DataValue::Null)
        };

        // Get mean and stddev over the window
        let mean = context
            .get_frame_avg(row_index, &column.name)
            .unwrap_or(DataValue::Null);
        let stddev = context
            .get_frame_stddev(row_index, &column.name)
            .unwrap_or(DataValue::Null);

        // Calculate Z-score
        match (current_value, mean, stddev) {
            (DataValue::Integer(v), DataValue::Float(m), DataValue::Float(s)) if s > 0.0 => {
                Ok(DataValue::Float((v as f64 - m) / s))
            }
            (DataValue::Float(v), DataValue::Float(m), DataValue::Float(s)) if s > 0.0 => {
                Ok(DataValue::Float((v - m) / s))
            }
            _ => Ok(DataValue::Null),
        }
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let window_size = match &args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i64>()
                .map_err(|_| anyhow::anyhow!("Invalid window size"))?,
            _ => return Err(anyhow::anyhow!("Z_SCORE requires numeric window_size")),
        };

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::Preceding(window_size - 1),
            end: None,
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() != 2 {
            return Err(anyhow::anyhow!("Z_SCORE requires exactly 2 arguments"));
        }
        Ok(())
    }
}

/// BOLLINGER_UPPER(column, window_size, num_std)
/// Calculates upper Bollinger Band: MA + (num_std * STDDEV)
struct BollingerUpperFunction;

impl WindowFunction for BollingerUpperFunction {
    fn name(&self) -> &str {
        "BOLLINGER_UPPER"
    }

    fn description(&self) -> &str {
        "Calculate upper Bollinger Band (MA + n*STDDEV)"
    }

    fn signature(&self) -> &str {
        "BOLLINGER_UPPER(column, window_size, num_std)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow!("BOLLINGER_UPPER first argument must be a column")),
        };

        // Get num_std from third argument (default 2)
        let num_std = match args.get(2) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<f64>()
                .map_err(|_| anyhow!("Invalid num_std value"))?,
            _ => 2.0, // Default to 2 standard deviations
        };

        // Get mean and stddev over the window
        let mean = context
            .get_frame_avg(row_index, &column.name)
            .unwrap_or(DataValue::Null);
        let stddev = context
            .get_frame_stddev(row_index, &column.name)
            .unwrap_or(DataValue::Null);

        // Calculate upper band: mean + (num_std * stddev)
        match (mean, stddev) {
            (DataValue::Float(m), DataValue::Float(s)) => Ok(DataValue::Float(m + (num_std * s))),
            _ => Ok(DataValue::Null),
        }
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let window_size = match args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i64>()
                .map_err(|_| anyhow!("Invalid window size"))?,
            _ => return Err(anyhow!("BOLLINGER_UPPER requires numeric window_size")),
        };

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::Preceding(window_size - 1),
            end: None,
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() < 2 || args.len() > 3 {
            return Err(anyhow!("BOLLINGER_UPPER requires 2 or 3 arguments"));
        }
        Ok(())
    }
}

/// BOLLINGER_LOWER(column, window_size, num_std)
/// Calculates lower Bollinger Band: MA - (num_std * STDDEV)
struct BollingerLowerFunction;

impl WindowFunction for BollingerLowerFunction {
    fn name(&self) -> &str {
        "BOLLINGER_LOWER"
    }

    fn description(&self) -> &str {
        "Calculate lower Bollinger Band (MA - n*STDDEV)"
    }

    fn signature(&self) -> &str {
        "BOLLINGER_LOWER(column, window_size, num_std)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow!("BOLLINGER_LOWER first argument must be a column")),
        };

        // Get num_std from third argument (default 2)
        let num_std = match args.get(2) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<f64>()
                .map_err(|_| anyhow!("Invalid num_std value"))?,
            _ => 2.0, // Default to 2 standard deviations
        };

        // Get mean and stddev over the window
        let mean = context
            .get_frame_avg(row_index, &column.name)
            .unwrap_or(DataValue::Null);
        let stddev = context
            .get_frame_stddev(row_index, &column.name)
            .unwrap_or(DataValue::Null);

        // Calculate lower band: mean - (num_std * stddev)
        match (mean, stddev) {
            (DataValue::Float(m), DataValue::Float(s)) => Ok(DataValue::Float(m - (num_std * s))),
            _ => Ok(DataValue::Null),
        }
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        use crate::sql::parser::ast::{FrameBound, FrameUnit, WindowFrame};

        let window_size = match args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i64>()
                .map_err(|_| anyhow!("Invalid window size"))?,
            _ => return Err(anyhow!("BOLLINGER_LOWER requires numeric window_size")),
        };

        let mut spec = base_spec.clone();
        spec.frame = Some(WindowFrame {
            unit: FrameUnit::Rows,
            start: FrameBound::Preceding(window_size - 1),
            end: None,
        });

        Ok(spec)
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.len() < 2 || args.len() > 3 {
            return Err(anyhow!("BOLLINGER_LOWER requires 2 or 3 arguments"));
        }
        Ok(())
    }
}

/// PERCENT_CHANGE(column, periods)
/// Calculates percentage change from N periods ago
/// Formula: ((current - previous) / previous) * 100
struct PercentChangeFunction;

impl WindowFunction for PercentChangeFunction {
    fn name(&self) -> &str {
        "PERCENT_CHANGE"
    }

    fn description(&self) -> &str {
        "Calculate percentage change from N periods ago"
    }

    fn signature(&self) -> &str {
        "PERCENT_CHANGE(column, periods)"
    }

    fn compute(
        &self,
        context: &WindowContext,
        row_index: usize,
        args: &[SqlExpression],
        _evaluator: &mut dyn ExpressionEvaluator,
    ) -> Result<DataValue> {
        let column = match &args[0] {
            SqlExpression::Column(col) => col,
            _ => return Err(anyhow!("PERCENT_CHANGE first argument must be a column")),
        };

        // Get periods from second argument (default 1)
        let periods = match args.get(1) {
            Some(SqlExpression::NumberLiteral(n)) => n
                .parse::<i32>()
                .map_err(|_| anyhow!("Invalid periods value"))?,
            _ => 1, // Default to 1 period
        };

        // Get current value
        let current_value = {
            let source = context.source();
            let col_idx = source
                .get_column_index(&column.name)
                .ok_or_else(|| anyhow!("Column {} not found", column))?;
            source.get_value(row_index, col_idx).cloned()
        };

        // Get previous value using offset
        let previous_value = context.get_offset_value(row_index, -periods, &column.name);

        // Calculate percent change: ((current - previous) / previous) * 100
        match (current_value, previous_value) {
            (Some(DataValue::Float(curr)), Some(DataValue::Float(prev))) if prev != 0.0 => {
                Ok(DataValue::Float(((curr - prev) / prev) * 100.0))
            }
            (Some(DataValue::Integer(curr)), Some(DataValue::Integer(prev))) if prev != 0 => {
                let curr_f = curr as f64;
                let prev_f = prev as f64;
                Ok(DataValue::Float(((curr_f - prev_f) / prev_f) * 100.0))
            }
            (Some(DataValue::Float(curr)), Some(DataValue::Integer(prev))) if prev != 0 => {
                let prev_f = prev as f64;
                Ok(DataValue::Float(((curr - prev_f) / prev_f) * 100.0))
            }
            (Some(DataValue::Integer(curr)), Some(DataValue::Float(prev))) if prev != 0.0 => {
                let curr_f = curr as f64;
                Ok(DataValue::Float(((curr_f - prev) / prev) * 100.0))
            }
            _ => Ok(DataValue::Null), // Return NULL for invalid comparisons or division by zero
        }
    }

    fn transform_window_spec(
        &self,
        base_spec: &WindowSpec,
        _args: &[SqlExpression],
    ) -> Result<WindowSpec> {
        // PERCENT_CHANGE doesn't need to modify the window frame
        // It uses LAG internally which works within the partition
        Ok(base_spec.clone())
    }

    fn validate_args(&self, args: &[SqlExpression]) -> Result<()> {
        if args.is_empty() || args.len() > 2 {
            return Err(anyhow!("PERCENT_CHANGE requires 1 or 2 arguments"));
        }
        Ok(())
    }
}

// TODO: Add more functions like:
// - EXPONENTIAL_AVG(column, alpha)
// - PERCENT_RANK_IN_WINDOW(column, window)
// - MEDIAN_IN_WINDOW(column, window)

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sql::parser::ast::ColumnRef;

    #[test]
    fn test_registry_creation() {
        let registry = WindowFunctionRegistry::new();
        assert!(registry.contains("MOVING_AVG"));
        assert!(registry.contains("ROLLING_STDDEV"));
        assert!(registry.contains("CUMULATIVE_SUM"));
    }

    #[test]
    fn test_window_spec_transformation() {
        use crate::sql::parser::ast::{FrameBound, WindowSpec};

        let func = MovingAvgFunction;
        let base_spec = WindowSpec {
            partition_by: vec![],
            order_by: vec![],
            frame: None,
        };

        let args = vec![
            SqlExpression::Column(ColumnRef::unquoted("close".to_string())),
            SqlExpression::NumberLiteral("20".to_string()),
        ];

        let transformed = func.transform_window_spec(&base_spec, &args).unwrap();

        assert!(transformed.frame.is_some());
        let frame = transformed.frame.unwrap();
        assert_eq!(frame.start, FrameBound::Preceding(19));
    }
}