sql-cli 1.69.4

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
use crate::data::datatable::DataValue;
use crate::sql::functions::{ArgCount, FunctionCategory, FunctionSignature, SqlFunction};
use anyhow::{anyhow, Result};
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};

/// BIGINT - Convert to arbitrary precision integer
pub struct BigIntFunction;

impl SqlFunction for BigIntFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BIGINT",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Convert value to arbitrary precision integer",
            returns: "String representation of big integer",
            examples: vec![
                "SELECT BIGINT('123456789012345678901234567890')",
                "SELECT BIGINT(2) ^ BIGINT(100)  -- 2^100",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("BIGINT expects 1 argument"));
        }

        match &args[0] {
            DataValue::String(s) => {
                match s.parse::<BigInt>() {
                    Ok(_) => Ok(DataValue::String(s.clone())), // Valid bigint string
                    Err(_) => Err(anyhow!("Invalid big integer format: {}", s)),
                }
            }
            DataValue::Integer(n) => Ok(DataValue::String(n.to_string())),
            DataValue::Float(f) => Ok(DataValue::String((*f as i64).to_string())),
            DataValue::Null => Ok(DataValue::Null),
            _ => Err(anyhow!("BIGINT expects numeric or string input")),
        }
    }
}

/// BIGADD - Add two arbitrary precision integers
pub struct BigAddFunction;

impl SqlFunction for BigAddFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BIGADD",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Add two arbitrary precision integers",
            returns: "String representation of sum",
            examples: vec![
                "SELECT BIGADD('999999999999999999999', '1')",
                "SELECT BIGADD('123456789012345678901234567890', '987654321098765432109876543210')",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BIGADD expects 2 arguments"));
        }

        let a = parse_bigint(&args[0])?;
        let b = parse_bigint(&args[1])?;

        Ok(DataValue::String((a + b).to_string()))
    }
}

/// BIGMUL - Multiply two arbitrary precision integers
pub struct BigMulFunction;

impl SqlFunction for BigMulFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BIGMUL",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Multiply two arbitrary precision integers",
            returns: "String representation of product",
            examples: vec![
                "SELECT BIGMUL('999999999999999999999', '999999999999999999999')",
                "SELECT BIGMUL('123456789', '987654321')",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BIGMUL expects 2 arguments"));
        }

        let a = parse_bigint(&args[0])?;
        let b = parse_bigint(&args[1])?;

        Ok(DataValue::String((a * b).to_string()))
    }
}

/// BIGPOW - Raise arbitrary precision integer to a power
pub struct BigPowFunction;

impl SqlFunction for BigPowFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BIGPOW",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Raise big integer to a power (base^exponent)",
            returns: "String representation of result",
            examples: vec![
                "SELECT BIGPOW('2', 100)    -- 2^100",
                "SELECT BIGPOW('10', 50)    -- 10^50",
                "SELECT BIGPOW('99', 99)    -- 99^99",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BIGPOW expects 2 arguments"));
        }

        let base = parse_bigint(&args[0])?;
        let exp = match &args[1] {
            DataValue::Integer(n) => *n as u32,
            DataValue::String(s) => s
                .parse::<u32>()
                .map_err(|_| anyhow!("Exponent must be a non-negative integer"))?,
            _ => return Err(anyhow!("Exponent must be a non-negative integer")),
        };

        let result = base.pow(exp);
        Ok(DataValue::String(result.to_string()))
    }
}

/// BIGFACT - Calculate factorial of large numbers
pub struct BigFactorialFunction;

impl SqlFunction for BigFactorialFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BIGFACT",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Calculate factorial of a number (n!)",
            returns: "String representation of factorial",
            examples: vec![
                "SELECT BIGFACT(50)     -- 50!",
                "SELECT BIGFACT(100)    -- 100!",
                "SELECT LENGTH(BIGFACT(1000))  -- How many digits in 1000!",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("BIGFACT expects 1 argument"));
        }

        let n = match &args[0] {
            DataValue::Integer(n) => *n as u32,
            DataValue::String(s) => s
                .parse::<u32>()
                .map_err(|_| anyhow!("Factorial input must be a non-negative integer"))?,
            _ => return Err(anyhow!("Factorial input must be a non-negative integer")),
        };

        if n > 10000 {
            return Err(anyhow!("Factorial input too large (max 10000)"));
        }

        let mut result = BigInt::one();
        for i in 2..=n {
            result *= i;
        }

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

/// TO_BINARY - Convert number to binary string
pub struct ToBinaryFunction;

impl SqlFunction for ToBinaryFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "TO_BINARY",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Convert number to binary string representation",
            returns: "Binary string (e.g., '1010' for 10)",
            examples: vec![
                "SELECT TO_BINARY(42)                     -- '101010'",
                "SELECT TO_BINARY('255')                  -- '11111111'",
                "SELECT TO_BINARY(BIGPOW('2', 100))      -- Binary of 2^100",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("TO_BINARY expects 1 argument"));
        }

        let num = parse_bigint(&args[0])?;
        if num.sign() == num_bigint::Sign::Minus {
            // For negative numbers, we'll show two's complement representation
            // with a fixed bit width (64 bits for demonstration)
            return Ok(DataValue::String(format!("-{}", num.abs().to_str_radix(2))));
        }

        Ok(DataValue::String(num.to_str_radix(2)))
    }
}

/// FROM_BINARY - Convert binary string to decimal
pub struct FromBinaryFunction;

impl SqlFunction for FromBinaryFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "FROM_BINARY",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Convert binary string to decimal number",
            returns: "Decimal string representation",
            examples: vec![
                "SELECT FROM_BINARY('101010')             -- '42'",
                "SELECT FROM_BINARY('11111111')           -- '255'",
                "SELECT FROM_BINARY('1' || REPEAT('0', 100)) -- 2^100",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("FROM_BINARY expects 1 argument"));
        }

        let binary_str = match &args[0] {
            DataValue::String(s) => s,
            _ => return Err(anyhow!("FROM_BINARY expects a string argument")),
        };

        // Remove any spaces and validate
        let clean_binary: String = binary_str
            .chars()
            .filter(|c| *c == '0' || *c == '1')
            .collect();

        if clean_binary.is_empty() {
            return Err(anyhow!("Invalid binary string"));
        }

        match BigInt::parse_bytes(clean_binary.as_bytes(), 2) {
            Some(num) => Ok(DataValue::String(num.to_string())),
            None => Err(anyhow!("Invalid binary string: {}", binary_str)),
        }
    }
}

/// TO_HEX - Convert number to hexadecimal string
pub struct ToHexFunction;

impl SqlFunction for ToHexFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "TO_HEX",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Convert number to hexadecimal string",
            returns: "Hexadecimal string (e.g., '2A' for 42)",
            examples: vec![
                "SELECT TO_HEX(255)                       -- 'ff'",
                "SELECT TO_HEX(BIGPOW('16', 10))         -- '10000000000'",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("TO_HEX expects 1 argument"));
        }

        let num = parse_bigint(&args[0])?;
        if num.sign() == num_bigint::Sign::Minus {
            return Ok(DataValue::String(format!(
                "-{}",
                num.abs().to_str_radix(16)
            )));
        }

        Ok(DataValue::String(num.to_str_radix(16)))
    }
}

/// FROM_HEX - Convert hexadecimal string to decimal
pub struct FromHexFunction;

impl SqlFunction for FromHexFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "FROM_HEX",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(1),
            description: "Convert hexadecimal string to decimal",
            returns: "Decimal string representation",
            examples: vec![
                "SELECT FROM_HEX('FF')                    -- '255'",
                "SELECT FROM_HEX('DEADBEEF')              -- '3735928559'",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 1 {
            return Err(anyhow!("FROM_HEX expects 1 argument"));
        }

        let hex_str = match &args[0] {
            DataValue::String(s) => s,
            _ => return Err(anyhow!("FROM_HEX expects a string argument")),
        };

        // Remove 0x prefix if present and clean
        let clean_hex = hex_str
            .trim()
            .trim_start_matches("0x")
            .trim_start_matches("0X");

        match BigInt::parse_bytes(clean_hex.as_bytes(), 16) {
            Some(num) => Ok(DataValue::String(num.to_string())),
            None => Err(anyhow!("Invalid hexadecimal string: {}", hex_str)),
        }
    }
}

/// BITAND - Bitwise AND of two numbers
pub struct BitAndFunction;

impl SqlFunction for BitAndFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BITAND",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Bitwise AND of two integers",
            returns: "Result of bitwise AND",
            examples: vec![
                "SELECT BITAND(12, 10)                    -- 8 (1100 & 1010 = 1000)",
                "SELECT TO_BINARY(BITAND(255, 15))       -- '1111'",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BITAND expects 2 arguments"));
        }

        let a = parse_bigint(&args[0])?;
        let b = parse_bigint(&args[1])?;

        Ok(DataValue::String((a & b).to_string()))
    }
}

/// BITOR - Bitwise OR of two numbers
pub struct BitOrFunction;

impl SqlFunction for BitOrFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BITOR",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Bitwise OR of two integers",
            returns: "Result of bitwise OR",
            examples: vec![
                "SELECT BITOR(12, 10)                     -- 14 (1100 | 1010 = 1110)",
                "SELECT TO_BINARY(BITOR(8, 4))           -- '1100'",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BITOR expects 2 arguments"));
        }

        let a = parse_bigint(&args[0])?;
        let b = parse_bigint(&args[1])?;

        Ok(DataValue::String((a | b).to_string()))
    }
}

/// BITXOR - Bitwise XOR of two numbers
pub struct BitXorFunction;

impl SqlFunction for BitXorFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BITXOR",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Bitwise XOR of two integers",
            returns: "Result of bitwise XOR",
            examples: vec![
                "SELECT BITXOR(12, 10)                    -- 6 (1100 ^ 1010 = 0110)",
                "SELECT TO_BINARY(BITXOR(255, 170))      -- '01010101'",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BITXOR expects 2 arguments"));
        }

        let a = parse_bigint(&args[0])?;
        let b = parse_bigint(&args[1])?;

        Ok(DataValue::String((a ^ b).to_string()))
    }
}

/// BITSHIFT - Bit shift left (positive) or right (negative)
pub struct BitShiftFunction;

impl SqlFunction for BitShiftFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "BITSHIFT",
            category: FunctionCategory::BigNumber,
            arg_count: ArgCount::Fixed(2),
            description: "Bit shift: left for positive shift, right for negative",
            returns: "Result of bit shift",
            examples: vec![
                "SELECT BITSHIFT(1, 10)                   -- 1024 (1 << 10)",
                "SELECT BITSHIFT(1024, -10)               -- 1 (1024 >> 10)",
                "SELECT TO_BINARY(BITSHIFT(1, 100))       -- 1 followed by 100 zeros",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.len() != 2 {
            return Err(anyhow!("BITSHIFT expects 2 arguments"));
        }

        let num = parse_bigint(&args[0])?;
        let shift = match &args[1] {
            DataValue::Integer(n) => *n,
            DataValue::String(s) => s
                .parse::<i64>()
                .map_err(|_| anyhow!("Shift amount must be an integer"))?,
            _ => return Err(anyhow!("Shift amount must be an integer")),
        };

        let result = if shift >= 0 {
            num << (shift as usize)
        } else {
            num >> ((-shift) as usize)
        };

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

// Helper function to parse DataValue to BigInt
fn parse_bigint(value: &DataValue) -> Result<BigInt> {
    match value {
        DataValue::String(s) => s
            .parse::<BigInt>()
            .map_err(|_| anyhow!("Invalid big integer format: {}", s)),
        DataValue::Integer(n) => Ok(BigInt::from(*n)),
        DataValue::Float(f) => Ok(BigInt::from(*f as i64)),
        DataValue::Null => Ok(BigInt::zero()),
        _ => Err(anyhow!("Expected numeric or string value for big integer")),
    }
}