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
use anyhow::{anyhow, Result};
use std::collections::VecDeque;

use super::{ArgCount, FunctionCategory, FunctionSignature, SqlFunction};
use crate::data::datatable::DataValue;

/// DELTAS function - returns the difference from the previous value in a series
/// Similar to q/kdb's deltas function
pub struct DeltasFunction;

impl SqlFunction for DeltasFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "DELTAS",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns differences from previous values in a series",
            returns: "ARRAY",
            examples: vec![
                "SELECT DELTAS(1, 3, 6, 10, 15) -- Returns: 1, 2, 3, 4, 5",
                "SELECT DELTAS(price) as price_changes FROM stocks ORDER BY date",
                "SELECT DELTAS(100, 95, 97, 92, 98) -- Returns: 100, -5, 2, -5, 6",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        let mut deltas = Vec::new();
        let mut prev: Option<f64> = None;

        for arg in args {
            let current = match arg {
                DataValue::Integer(i) => *i as f64,
                DataValue::Float(f) => *f,
                DataValue::Null => {
                    deltas.push("null".to_string());
                    prev = None;
                    continue;
                }
                _ => return Err(anyhow!("DELTAS expects numeric values")),
            };

            match prev {
                None => {
                    // First value is returned as-is
                    if current.fract() == 0.0 && current.abs() < 1e10 {
                        deltas.push(format!("{}", current as i64));
                    } else {
                        deltas.push(format!("{}", current));
                    }
                }
                Some(p) => {
                    let delta = current - p;
                    if delta.fract() == 0.0 && delta.abs() < 1e10 {
                        deltas.push(format!("{}", delta as i64));
                    } else {
                        deltas.push(format!("{}", delta));
                    }
                }
            }
            prev = Some(current);
        }

        // Return as array-like string for now
        Ok(DataValue::String(format!("[{}]", deltas.join(", "))))
    }
}

/// SUMS function - returns running sum of a series
/// Similar to q/kdb's sums function
pub struct SumsFunction;

impl SqlFunction for SumsFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "SUMS",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns running sum of values in a series",
            returns: "ARRAY",
            examples: vec![
                "SELECT SUMS(1, 2, 3, 4, 5) -- Returns: 1, 3, 6, 10, 15",
                "SELECT SUMS(daily_revenue) as cumulative_revenue FROM sales ORDER BY date",
                "SELECT SUMS(10, -5, 8, -3, 6) -- Returns: 10, 5, 13, 10, 16",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        let mut sums = Vec::new();
        let mut running_sum = 0.0;

        for arg in args {
            match arg {
                DataValue::Integer(i) => {
                    running_sum += *i as f64;
                }
                DataValue::Float(f) => {
                    running_sum += f;
                }
                DataValue::Null => {
                    sums.push("null".to_string());
                    continue;
                }
                _ => return Err(anyhow!("SUMS expects numeric values")),
            }

            if running_sum.fract() == 0.0 && running_sum.abs() < 1e10 {
                sums.push(format!("{}", running_sum as i64));
            } else {
                sums.push(format!("{}", running_sum));
            }
        }

        Ok(DataValue::String(format!("[{}]", sums.join(", "))))
    }
}

/// MAVG function - returns moving average over a window
pub struct MovingAverageFunction;

impl SqlFunction for MovingAverageFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "MAVG",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns moving average with specified window size (first arg is window)",
            returns: "ARRAY",
            examples: vec![
                "SELECT MAVG(3, 10, 20, 30, 40, 50) -- 3-period moving avg",
                "SELECT MAVG(5, price) as ma5 FROM stocks ORDER BY date",
                "SELECT MAVG(2, 1, 2, 3, 4, 5) -- Returns: 1, 1.5, 2.5, 3.5, 4.5",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() < 2 {
            return Err(anyhow!("MAVG requires window size and at least one value"));
        }

        // First argument is the window size
        let window_size = match &args[0] {
            DataValue::Integer(w) if *w > 0 => *w as usize,
            _ => return Err(anyhow!("MAVG window size must be a positive integer")),
        };

        let mut averages = Vec::new();
        let mut window: VecDeque<f64> = VecDeque::with_capacity(window_size);

        // Process the remaining values
        for arg in &args[1..] {
            let value = match arg {
                DataValue::Integer(i) => *i as f64,
                DataValue::Float(f) => *f,
                DataValue::Null => {
                    averages.push("null".to_string());
                    continue;
                }
                _ => return Err(anyhow!("MAVG expects numeric values")),
            };

            window.push_back(value);
            if window.len() > window_size {
                window.pop_front();
            }

            let avg = window.iter().sum::<f64>() / window.len() as f64;
            
            if avg.fract() == 0.0 && avg.abs() < 1e10 {
                averages.push(format!("{}", avg as i64));
            } else {
                averages.push(format!("{:.2}", avg));
            }
        }

        Ok(DataValue::String(format!("[{}]", averages.join(", "))))
    }
}

/// PCT_CHANGE function - returns percentage change from previous value
pub struct PercentChangeFunction;

impl SqlFunction for PercentChangeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "PCT_CHANGE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns percentage change from previous values",
            returns: "ARRAY",
            examples: vec![
                "SELECT PCT_CHANGE(100, 110, 121, 108.9) -- Returns: null, 10%, 10%, -10%",
                "SELECT PCT_CHANGE(price) as price_pct_change FROM stocks ORDER BY date",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        let mut changes = Vec::new();
        let mut prev: Option<f64> = None;

        for arg in args {
            let current = match arg {
                DataValue::Integer(i) => *i as f64,
                DataValue::Float(f) => *f,
                DataValue::Null => {
                    changes.push("null".to_string());
                    prev = None;
                    continue;
                }
                _ => return Err(anyhow!("PCT_CHANGE expects numeric values")),
            };

            match prev {
                None => {
                    changes.push("null".to_string()); // First value has no previous
                }
                Some(p) if p != 0.0 => {
                    let pct = ((current - p) / p) * 100.0;
                    changes.push(format!("{:.2}%", pct));
                }
                Some(_) => {
                    changes.push("inf".to_string()); // Division by zero
                }
            }
            prev = Some(current);
        }

        Ok(DataValue::String(format!("[{}]", changes.join(", "))))
    }
}

/// RANK function - returns rank of values (1 = smallest)
pub struct RankFunction;

impl SqlFunction for RankFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "RANK",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns rank of each value (1 = smallest)",
            returns: "ARRAY",
            examples: vec![
                "SELECT RANK(50, 20, 30, 20, 40) -- Returns: 5, 1, 3, 1, 4",
                "SELECT RANK(score) as score_rank FROM students",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        // Collect values with their original indices
        let mut indexed_values: Vec<(usize, f64)> = Vec::new();
        
        for (i, arg) in args.iter().enumerate() {
            match arg {
                DataValue::Integer(v) => indexed_values.push((i, *v as f64)),
                DataValue::Float(v) => indexed_values.push((i, *v)),
                DataValue::Null => indexed_values.push((i, f64::NAN)),
                _ => return Err(anyhow!("RANK expects numeric values")),
            }
        }

        // Sort by value to determine ranks
        let mut sorted = indexed_values.clone();
        sorted.sort_by(|a, b| {
            if a.1.is_nan() && b.1.is_nan() {
                std::cmp::Ordering::Equal
            } else if a.1.is_nan() {
                std::cmp::Ordering::Greater  // NaN goes to end
            } else if b.1.is_nan() {
                std::cmp::Ordering::Less
            } else {
                a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)
            }
        });

        // Assign ranks (handling ties)
        let mut ranks = vec![0; args.len()];
        let mut current_rank = 1;
        
        for i in 0..sorted.len() {
            if sorted[i].1.is_nan() {
                ranks[sorted[i].0] = 0; // NaN gets rank 0 or could be null
            } else {
                if i > 0 && !sorted[i-1].1.is_nan() && (sorted[i].1 - sorted[i-1].1).abs() > 1e-10 {
                    current_rank = i + 1;
                }
                ranks[sorted[i].0] = current_rank;
            }
        }

        let rank_strings: Vec<String> = ranks.iter()
            .map(|&r| if r == 0 { "null".to_string() } else { r.to_string() })
            .collect();

        Ok(DataValue::String(format!("[{}]", rank_strings.join(", "))))
    }
}

/// CUMMAX function - returns cumulative maximum
pub struct CumulativeMaxFunction;

impl SqlFunction for CumulativeMaxFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "CUMMAX",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns cumulative maximum of values",
            returns: "ARRAY",
            examples: vec![
                "SELECT CUMMAX(3, 1, 4, 1, 5, 9, 2) -- Returns: 3, 3, 4, 4, 5, 9, 9",
                "SELECT CUMMAX(high_price) as all_time_high FROM stocks ORDER BY date",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        let mut results = Vec::new();
        let mut current_max: Option<f64> = None;

        for arg in args {
            let value = match arg {
                DataValue::Integer(i) => *i as f64,
                DataValue::Float(f) => *f,
                DataValue::Null => {
                    if let Some(max) = current_max {
                        if max.fract() == 0.0 && max.abs() < 1e10 {
                            results.push(format!("{}", max as i64));
                        } else {
                            results.push(format!("{}", max));
                        }
                    } else {
                        results.push("null".to_string());
                    }
                    continue;
                }
                _ => return Err(anyhow!("CUMMAX expects numeric values")),
            };

            current_max = match current_max {
                None => Some(value),
                Some(max) => Some(max.max(value)),
            };

            let max = current_max.unwrap();
            if max.fract() == 0.0 && max.abs() < 1e10 {
                results.push(format!("{}", max as i64));
            } else {
                results.push(format!("{}", max));
            }
        }

        Ok(DataValue::String(format!("[{}]", results.join(", "))))
    }
}

/// CUMMIN function - returns cumulative minimum
pub struct CumulativeMinFunction;

impl SqlFunction for CumulativeMinFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "CUMMIN",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Returns cumulative minimum of values",
            returns: "ARRAY",
            examples: vec![
                "SELECT CUMMIN(3, 1, 4, 1, 5, 9, 2) -- Returns: 3, 1, 1, 1, 1, 1, 1",
                "SELECT CUMMIN(low_price) as all_time_low FROM stocks ORDER BY date",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Ok(DataValue::String("[]".to_string()));
        }

        let mut results = Vec::new();
        let mut current_min: Option<f64> = None;

        for arg in args {
            let value = match arg {
                DataValue::Integer(i) => *i as f64,
                DataValue::Float(f) => *f,
                DataValue::Null => {
                    if let Some(min) = current_min {
                        if min.fract() == 0.0 && min.abs() < 1e10 {
                            results.push(format!("{}", min as i64));
                        } else {
                            results.push(format!("{}", min));
                        }
                    } else {
                        results.push("null".to_string());
                    }
                    continue;
                }
                _ => return Err(anyhow!("CUMMIN expects numeric values")),
            };

            current_min = match current_min {
                None => Some(value),
                Some(min) => Some(min.min(value)),
            };

            let min = current_min.unwrap();
            if min.fract() == 0.0 && min.abs() < 1e10 {
                results.push(format!("{}", min as i64));
            } else {
                results.push(format!("{}", min));
            }
        }

        Ok(DataValue::String(format!("[{}]", results.join(", "))))
    }
}

/// Register all analytics functions
pub fn register_analytics_functions(registry: &mut super::FunctionRegistry) {
    registry.register(Box::new(DeltasFunction));
    registry.register(Box::new(SumsFunction));
    registry.register(Box::new(MovingAverageFunction));
    registry.register(Box::new(PercentChangeFunction));
    registry.register(Box::new(RankFunction));
    registry.register(Box::new(CumulativeMaxFunction));
    registry.register(Box::new(CumulativeMinFunction));
}

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

    #[test]
    fn test_deltas() {
        let func = DeltasFunction;
        let args = vec![
            DataValue::Integer(1),
            DataValue::Integer(3),
            DataValue::Integer(6),
            DataValue::Integer(10),
            DataValue::Integer(15),
        ];
        
        let result = func.evaluate(&args).unwrap();
        match result {
            DataValue::String(s) => assert_eq!(s, "[1, 2, 3, 4, 5]"),
            _ => panic!("Expected string result"),
        }
    }

    #[test]
    fn test_sums() {
        let func = SumsFunction;
        let args = vec![
            DataValue::Integer(1),
            DataValue::Integer(2),
            DataValue::Integer(3),
            DataValue::Integer(4),
            DataValue::Integer(5),
        ];
        
        let result = func.evaluate(&args).unwrap();
        match result {
            DataValue::String(s) => assert_eq!(s, "[1, 3, 6, 10, 15]"),
            _ => panic!("Expected string result"),
        }
    }

    #[test]
    fn test_moving_average() {
        let func = MovingAverageFunction;
        let args = vec![
            DataValue::Integer(2), // window size
            DataValue::Integer(1),
            DataValue::Integer(2),
            DataValue::Integer(3),
            DataValue::Integer(4),
            DataValue::Integer(5),
        ];
        
        let result = func.evaluate(&args).unwrap();
        match result {
            DataValue::String(s) => {
                assert!(s.contains("1")); // First value
                assert!(s.contains("1.5")); // Avg of 1,2
                assert!(s.contains("2.5")); // Avg of 2,3
            }
            _ => panic!("Expected string result"),
        }
    }

    #[test]
    fn test_rank() {
        let func = RankFunction;
        let args = vec![
            DataValue::Integer(50),
            DataValue::Integer(20),
            DataValue::Integer(30),
            DataValue::Integer(20),
            DataValue::Integer(40),
        ];
        
        let result = func.evaluate(&args).unwrap();
        match result {
            DataValue::String(s) => {
                assert!(s.contains("5")); // 50 is rank 5
                assert!(s.contains("1")); // 20 is rank 1 (appears twice)
                assert!(s.contains("3")); // 30 is rank 3
            }
            _ => panic!("Expected string result"),
        }
    }
}