sql-cli 1.77.2

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
use anyhow::{anyhow, Result};
use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};

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

/// FORMAT_NUMBER function - Format numbers with specified decimal places and separators
pub struct FormatNumberFunction;

impl SqlFunction for FormatNumberFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "FORMAT_NUMBER",
            category: FunctionCategory::String,
            arg_count: ArgCount::Range(1, 3),
            description: "Format a number with decimal places and thousand separators",
            returns: "STRING",
            examples: vec![
                "SELECT FORMAT_NUMBER(1234567.89, 2)",    // "1,234,567.89"
                "SELECT FORMAT_NUMBER(1234.5, 2, false)", // "1234.50" (no thousands sep)
                "SELECT FORMAT_NUMBER(1234567)",          // "1,234,567"
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let value = match &args[0] {
            DataValue::Integer(n) => *n as f64,
            DataValue::Float(f) => *f,
            DataValue::Null => return Ok(DataValue::Null),
            _ => return Err(anyhow!("FORMAT_NUMBER requires a numeric argument")),
        };

        let decimals = if args.len() >= 2 {
            match &args[1] {
                DataValue::Integer(n) => *n as usize,
                DataValue::Float(f) => *f as usize,
                _ => 2,
            }
        } else {
            0
        };

        let use_separator = if args.len() >= 3 {
            match &args[2] {
                DataValue::Boolean(b) => *b,
                DataValue::Integer(n) => *n != 0,
                _ => true,
            }
        } else {
            true
        };

        // Format with decimal places
        let formatted = if decimals > 0 {
            format!("{:.prec$}", value, prec = decimals)
        } else {
            format!("{:.0}", value)
        };

        // Add thousand separators if requested
        let result = if use_separator {
            add_thousand_separators(&formatted)
        } else {
            formatted
        };

        Ok(DataValue::String(result))
    }
}

fn add_thousand_separators(s: &str) -> String {
    let parts: Vec<&str> = s.split('.').collect();
    let integer_part = parts[0];
    let decimal_part = parts.get(1);

    let mut result = String::new();
    let mut count = 0;

    for ch in integer_part.chars().rev() {
        if count > 0 && count % 3 == 0 && ch != '-' {
            result.push(',');
        }
        result.push(ch);
        if ch != '-' {
            count += 1;
        }
    }

    let formatted_integer: String = result.chars().rev().collect();

    if let Some(dec) = decimal_part {
        format!("{}.{}", formatted_integer, dec)
    } else {
        formatted_integer
    }
}

/// LPAD function - Left pad a string to a specified length
pub struct LPadFunction;

impl SqlFunction for LPadFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "LPAD",
            category: FunctionCategory::String,
            arg_count: ArgCount::Range(2, 3),
            description: "Left pad a string to a specified length with a fill character",
            returns: "STRING",
            examples: vec![
                "SELECT LPAD('123', 5)",         // "  123"
                "SELECT LPAD('123', 5, '0')",    // "00123"
                "SELECT LPAD('hello', 10, '.')", // ".....hello"
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => return Err(anyhow!("LPAD requires a string or numeric argument")),
        };

        let length = match &args[1] {
            DataValue::Integer(n) if *n >= 0 => *n as usize,
            _ => return Err(anyhow!("LPAD length must be a non-negative integer")),
        };

        let pad_char = if args.len() >= 3 {
            match &args[2] {
                DataValue::String(s) if !s.is_empty() => s.chars().next().unwrap(),
                _ => ' ',
            }
        } else {
            ' '
        };

        if text.len() >= length {
            Ok(DataValue::String(text[..length].to_string()))
        } else {
            let padding = pad_char.to_string().repeat(length - text.len());
            Ok(DataValue::String(format!("{}{}", padding, text)))
        }
    }
}

/// RPAD function - Right pad a string to a specified length
pub struct RPadFunction;

impl SqlFunction for RPadFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "RPAD",
            category: FunctionCategory::String,
            arg_count: ArgCount::Range(2, 3),
            description: "Right pad a string to a specified length with a fill character",
            returns: "STRING",
            examples: vec![
                "SELECT RPAD('123', 5)",         // "123  "
                "SELECT RPAD('123', 5, '0')",    // "12300"
                "SELECT RPAD('hello', 10, '.')", // "hello....."
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => return Err(anyhow!("RPAD requires a string or numeric argument")),
        };

        let length = match &args[1] {
            DataValue::Integer(n) if *n >= 0 => *n as usize,
            _ => return Err(anyhow!("RPAD length must be a non-negative integer")),
        };

        let pad_char = if args.len() >= 3 {
            match &args[2] {
                DataValue::String(s) if !s.is_empty() => s.chars().next().unwrap(),
                _ => ' ',
            }
        } else {
            ' '
        };

        if text.len() >= length {
            Ok(DataValue::String(text[..length].to_string()))
        } else {
            let padding = pad_char.to_string().repeat(length - text.len());
            Ok(DataValue::String(format!("{}{}", text, padding)))
        }
    }
}

/// CENTER function - Center a string within a specified width
pub struct CenterFunction;

impl SqlFunction for CenterFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "CENTER",
            category: FunctionCategory::String,
            arg_count: ArgCount::Range(2, 3),
            description: "Center a string within a specified width",
            returns: "STRING",
            examples: vec![
                "SELECT CENTER('hello', 11)",     // "   hello   "
                "SELECT CENTER('test', 10, '.')", // "...test..."
                "SELECT CENTER('SQL', 7, '-')",   // "--SQL--"
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let text = match &args[0] {
            DataValue::String(s) => s.clone(),
            DataValue::Integer(n) => n.to_string(),
            DataValue::Float(f) => f.to_string(),
            DataValue::Null => return Ok(DataValue::Null),
            _ => return Err(anyhow!("CENTER requires a string or numeric argument")),
        };

        let width = match &args[1] {
            DataValue::Integer(n) if *n >= 0 => *n as usize,
            _ => return Err(anyhow!("CENTER width must be a non-negative integer")),
        };

        let pad_char = if args.len() >= 3 {
            match &args[2] {
                DataValue::String(s) if !s.is_empty() => s.chars().next().unwrap(),
                _ => ' ',
            }
        } else {
            ' '
        };

        if text.len() >= width {
            Ok(DataValue::String(text[..width].to_string()))
        } else {
            let total_padding = width - text.len();
            let left_padding = total_padding / 2;
            let right_padding = total_padding - left_padding;

            let left = pad_char.to_string().repeat(left_padding);
            let right = pad_char.to_string().repeat(right_padding);

            Ok(DataValue::String(format!("{}{}{}", left, text, right)))
        }
    }
}

/// FORMAT_DATE function - Format dates using format strings
pub struct FormatDateFunction;

impl SqlFunction for FormatDateFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "FORMAT_DATE",
            category: FunctionCategory::Date,
            arg_count: ArgCount::Fixed(2),
            description: "Format a date using a format string",
            returns: "STRING",
            examples: vec![
                "SELECT FORMAT_DATE(NOW(), '%Y-%m-%d')",      // "2024-03-15"
                "SELECT FORMAT_DATE(NOW(), '%B %d, %Y')",     // "March 15, 2024"
                "SELECT FORMAT_DATE(NOW(), '%Y%m%d_%H%M%S')", // "20240315_143022"
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let datetime_str = match &args[0] {
            DataValue::DateTime(dt) => dt,
            DataValue::String(s) => s, // Also accept string dates
            DataValue::Null => return Ok(DataValue::Null),
            _ => {
                return Err(anyhow!(
                    "FORMAT_DATE requires a datetime or string argument"
                ))
            }
        };

        let format_str = match &args[1] {
            DataValue::String(s) => s,
            _ => return Err(anyhow!("FORMAT_DATE format must be a string")),
        };

        // Try to parse as ISO 8601 or common date formats

        let formatted = if let Ok(dt) = datetime_str.parse::<DateTime<Utc>>() {
            dt.format(format_str).to_string()
        } else if let Ok(dt) = NaiveDateTime::parse_from_str(datetime_str, "%Y-%m-%d %H:%M:%S") {
            dt.format(format_str).to_string()
        } else if let Ok(dt) = NaiveDate::parse_from_str(datetime_str, "%Y-%m-%d") {
            dt.format(format_str).to_string()
        } else if let Ok(dt) = NaiveDateTime::parse_from_str(datetime_str, "%Y-%m-%dT%H:%M:%S") {
            dt.format(format_str).to_string()
        } else {
            return Err(anyhow!("Cannot parse datetime: {}", datetime_str));
        };

        Ok(DataValue::String(formatted))
    }
}

/// Translate a MySQL `DATE_FORMAT` format string into chrono's strftime dialect.
///
/// Most specifiers are identical between the two (`%Y`, `%m`, `%d`, `%H`, `%h`,
/// `%p`, `%a`, `%b`, `%y`, `%j`, `%T`, `%r`, …) and pass through untouched. Only
/// the handful that diverge are remapped. This is done in a *single pass* — a
/// sequential string-replace would be wrong because MySQL `%i`→strftime `%M`
/// and MySQL `%M`→strftime `%B` collide on the intermediate `%M`.
///
/// | MySQL | meaning             | strftime |
/// |-------|---------------------|----------|
/// | `%i`  | minutes (00–59)     | `%M`     |
/// | `%M`  | full month name     | `%B`     |
/// | `%W`  | full weekday name   | `%A`     |
/// | `%s`  | seconds (00–59)     | `%S`     |
/// | `%f`  | microseconds        | `%6f`    |
fn translate_mysql_date_format(fmt: &str) -> String {
    let mut out = String::with_capacity(fmt.len());
    let mut chars = fmt.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '%' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('i') => out.push_str("%M"),  // minutes
            Some('M') => out.push_str("%B"),  // full month name
            Some('W') => out.push_str("%A"),  // full weekday name
            Some('s') => out.push_str("%S"),  // seconds (avoid strftime epoch %s)
            Some('f') => out.push_str("%6f"), // microseconds
            // Identical or unrecognised: emit verbatim so chrono handles it
            // (covers %Y %m %d %H %h %p %a %b %y %j %T %r %% and friends).
            Some(other) => {
                out.push('%');
                out.push(other);
            }
            None => out.push('%'),
        }
    }
    out
}

/// DATE_FORMAT — MySQL-compatible date formatting.
///
/// Translates MySQL-only specifiers (`%i`, `%M`, `%W`, `%s`, `%f`) to their
/// chrono strftime equivalents, then delegates to `FormatDateFunction`. All
/// shared specifiers pass through unchanged, so `%Y-%m-%d %H:%i:%s` and
/// `%W, %M %d` both behave as a MySQL user expects.
pub struct DateFormatFunction;

impl SqlFunction for DateFormatFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "DATE_FORMAT",
            category: FunctionCategory::Date,
            arg_count: ArgCount::Fixed(2),
            description: "Format a date using a MySQL-style format string (translates MySQL specifiers like %i, %M, %W to chrono equivalents)",
            returns: "STRING",
            examples: vec![
                "SELECT DATE_FORMAT(trans_date, '%Y-%m')",       // "2018-12"
                "SELECT DATE_FORMAT(NOW(), '%H:%i:%s')",         // "14:05:09"
                "SELECT DATE_FORMAT(NOW(), '%W, %M %d, %Y')",    // "Sunday, March 15, 2024"
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        // Translate the MySQL format string (2nd arg) before delegating.
        if args.len() == 2 {
            if let DataValue::String(fmt) = &args[1] {
                let translated = translate_mysql_date_format(fmt);
                let new_args = [args[0].clone(), DataValue::String(translated)];
                return FormatDateFunction.evaluate(&new_args);
            }
        }
        FormatDateFunction.evaluate(args)
    }
}

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

    #[test]
    fn translates_mysql_only_specifiers() {
        // %i -> minutes, %M -> month name, %W -> weekday name, %s -> seconds
        assert_eq!(translate_mysql_date_format("%H:%i:%s"), "%H:%M:%S");
        assert_eq!(translate_mysql_date_format("%W, %M %d"), "%A, %B %d");
        assert_eq!(translate_mysql_date_format("%f"), "%6f");
    }

    #[test]
    fn passes_through_shared_and_literals() {
        // Identical specifiers and literals are untouched.
        assert_eq!(translate_mysql_date_format("%Y-%m-%d"), "%Y-%m-%d");
        assert_eq!(translate_mysql_date_format("%r"), "%r");
        assert_eq!(translate_mysql_date_format("%a %b 100%%"), "%a %b 100%%");
        assert_eq!(
            translate_mysql_date_format("no specifiers"),
            "no specifiers"
        );
    }

    #[test]
    fn single_pass_avoids_i_to_m_collision() {
        // The classic trap: %i must become %M (minutes) and a real %M must
        // become %B (month name) — never double-translated.
        assert_eq!(translate_mysql_date_format("%i-%M"), "%M-%B");
    }

    #[test]
    fn date_format_end_to_end() {
        // 2026-05-31 14:05:09 is a Sunday.
        let args = [
            DataValue::String("2026-05-31 14:05:09".to_string()),
            DataValue::String("%W, %M %d, %Y %H:%i:%s".to_string()),
        ];
        let out = DateFormatFunction.evaluate(&args).unwrap();
        assert_eq!(
            out,
            DataValue::String("Sunday, May 31, 2026 14:05:09".to_string())
        );
    }
}