sql-cli 1.73.1

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
use anyhow::{anyhow, Result};
use lazy_static::lazy_static;
use std::collections::HashSet;
use std::sync::RwLock;

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

// Include the generated prime data
include!(concat!(env!("OUT_DIR"), "/primes_data.rs"));

// Create a HashSet for O(1) primality testing up to 100K-th prime
lazy_static! {
    static ref PRIME_SET: HashSet<u32> = PRIMES_100K.iter().copied().collect();
    static ref EXTENDED_PRIME_CACHE: RwLock<Vec<u64>> = RwLock::new(Vec::new());
}

/// Engine for prime number operations
pub struct PrimeEngine;

impl PrimeEngine {
    /// Get the nth prime number (1-indexed)
    pub fn nth_prime(n: usize) -> Result<u64> {
        if n == 0 {
            return Err(anyhow!("Prime index must be >= 1"));
        }

        // Fast path: use pre-computed tables
        if n <= 1_000 {
            return Ok(u64::from(PRIMES_1K[n - 1]));
        }
        if n <= 10_000 {
            return Ok(u64::from(PRIMES_10K[n - 1]));
        }
        if n <= 100_000 {
            return Ok(u64::from(PRIMES_100K[n - 1]));
        }

        // Slow path: generate on demand and cache
        Self::generate_nth_prime(n)
    }

    /// Check if a number is prime
    #[must_use]
    pub fn is_prime(n: u64) -> bool {
        if n < 2 {
            return false;
        }
        if n == 2 {
            return true;
        }
        if n % 2 == 0 {
            return false;
        }

        // Fast path: check pre-computed set (up to 1,299,709)
        if n <= 1_299_709 {
            return PRIME_SET.contains(&(n as u32));
        }

        // Medium numbers: trial division with pre-computed primes
        if n < 1_000_000_000_000 {
            let sqrt_n = (n as f64).sqrt() as u64;

            // Use our pre-computed primes for trial division
            for &p in PRIMES_100K {
                let p64 = u64::from(p);
                if p64 > sqrt_n {
                    return true;
                }
                if n % p64 == 0 {
                    return false;
                }
            }

            // If we've exhausted our prime list and haven't found a factor,
            // continue with wheel factorization
            Self::is_prime_wheel(n, u64::from(PRIMES_100K[PRIMES_100K.len() - 1]))
        } else {
            // Very large numbers: Miller-Rabin test
            Self::miller_rabin(n)
        }
    }

    /// Count primes up to n (prime counting function π(n))
    #[must_use]
    pub fn prime_count(n: u64) -> usize {
        if n < 2 {
            return 0;
        }

        // Fast path: binary search in pre-computed list
        if n <= 1_299_709 {
            match PRIMES_100K.binary_search(&(n as u32)) {
                Ok(idx) => idx + 1, // Found exact match
                Err(idx) => idx,    // Found insertion point
            }
        } else {
            // For large n, use approximation or actual counting
            // For now, use prime number theorem approximation
            Self::approximate_prime_count(n)
        }
    }

    /// Find the next prime >= n
    #[must_use]
    pub fn next_prime(n: u64) -> u64 {
        if n <= 2 {
            return 2;
        }

        // Fast path: binary search in pre-computed primes
        if n <= 1_299_709 {
            let target = n as u32;
            match PRIMES_100K.binary_search(&target) {
                Ok(_) => n, // n is prime
                Err(idx) => {
                    if idx < PRIMES_100K.len() {
                        u64::from(PRIMES_100K[idx])
                    } else {
                        // n is larger than our biggest pre-computed prime
                        Self::find_next_prime_slow(n)
                    }
                }
            }
        } else {
            Self::find_next_prime_slow(n)
        }
    }

    /// Find the previous prime <= n
    #[must_use]
    pub fn prev_prime(n: u64) -> Option<u64> {
        if n < 2 {
            return None;
        }
        if n == 2 {
            return Some(2);
        }

        // Fast path: binary search in pre-computed primes
        if n <= 1_299_709 {
            let target = n as u32;
            match PRIMES_100K.binary_search(&target) {
                Ok(_) => Some(n), // n is prime
                Err(idx) => {
                    if idx > 0 {
                        Some(u64::from(PRIMES_100K[idx - 1]))
                    } else {
                        None // n < 2
                    }
                }
            }
        } else {
            Self::find_prev_prime_slow(n)
        }
    }

    /// Get prime factorization of n
    #[must_use]
    pub fn factor(mut n: u64) -> Vec<(u64, u32)> {
        if n <= 1 {
            return vec![];
        }

        let mut factors = Vec::new();

        // Trial division with pre-computed primes
        for &p in PRIMES_10K {
            let p64 = u64::from(p);
            if p64 * p64 > n {
                break;
            }

            let mut count = 0;
            while n % p64 == 0 {
                n /= p64;
                count += 1;
            }

            if count > 0 {
                factors.push((p64, count));
            }
        }

        // If n > 1 at this point, it's either prime or has large prime factors
        if n > 1 {
            // Check if it's prime
            if Self::is_prime(n) {
                factors.push((n, 1));
            } else {
                // Try to factor using Pollard's rho for large composites
                // For now, just add as prime (simplified)
                factors.push((n, 1));
            }
        }

        factors
    }

    // Helper functions

    /// Generate the nth prime for n > 100,000
    fn generate_nth_prime(n: usize) -> Result<u64> {
        // Check cache first
        let cache = EXTENDED_PRIME_CACHE.read().unwrap();
        let cache_start = 100_001;
        let cache_idx = n - cache_start;

        if cache_idx < cache.len() {
            return Ok(cache[cache_idx]);
        }
        drop(cache);

        // Generate primes beyond our pre-computed range
        let mut cache = EXTENDED_PRIME_CACHE.write().unwrap();

        // Start from the last pre-computed prime
        let mut candidate = u64::from(PRIMES_100K[PRIMES_100K.len() - 1]) + 2;
        let mut count = 100_000 + cache.len();

        while count < n {
            if Self::is_prime(candidate) {
                cache.push(candidate);
                count += 1;
            }
            candidate += 2;
        }

        Ok(cache[cache_idx])
    }

    /// Wheel factorization for primality testing
    fn is_prime_wheel(n: u64, start: u64) -> bool {
        // Use wheel factorization mod 30 (2*3*5)
        const WHEEL: &[u64] = &[1, 7, 11, 13, 17, 19, 23, 29];

        let sqrt_n = (n as f64).sqrt() as u64;
        let mut base = ((start / 30) + 1) * 30;

        while base <= sqrt_n {
            for &offset in WHEEL {
                let candidate = base + offset;
                if candidate > sqrt_n {
                    return true;
                }
                if candidate > start && n % candidate == 0 {
                    return false;
                }
            }
            base += 30;
        }
        true
    }

    /// Miller-Rabin primality test for very large numbers
    fn miller_rabin(n: u64) -> bool {
        // Witnesses for deterministic test up to 2^64
        const WITNESSES: &[u64] = &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];

        // Write n-1 as 2^r * d
        let mut d = n - 1;
        let mut r = 0;
        while d % 2 == 0 {
            d /= 2;
            r += 1;
        }

        'witness: for &a in WITNESSES {
            if a >= n {
                continue;
            }

            let mut x = Self::mod_pow(a, d, n);
            if x == 1 || x == n - 1 {
                continue;
            }

            for _ in 0..r - 1 {
                x = Self::mod_mul(x, x, n);
                if x == n - 1 {
                    continue 'witness;
                }
            }

            return false;
        }

        true
    }

    /// Modular exponentiation: base^exp mod m
    fn mod_pow(mut base: u64, mut exp: u64, m: u64) -> u64 {
        let mut result = 1;
        base %= m;

        while exp > 0 {
            if exp % 2 == 1 {
                result = Self::mod_mul(result, base, m);
            }
            base = Self::mod_mul(base, base, m);
            exp /= 2;
        }

        result
    }

    /// Modular multiplication avoiding overflow
    fn mod_mul(a: u64, b: u64, m: u64) -> u64 {
        ((u128::from(a) * u128::from(b)) % u128::from(m)) as u64
    }

    /// Find next prime slowly (for n > largest pre-computed)
    fn find_next_prime_slow(mut n: u64) -> u64 {
        if n % 2 == 0 {
            n += 1;
        }

        while !Self::is_prime(n) {
            n += 2;
        }

        n
    }

    /// Find previous prime slowly
    fn find_prev_prime_slow(mut n: u64) -> Option<u64> {
        if n % 2 == 0 {
            n -= 1;
        }

        while n > 2 {
            if Self::is_prime(n) {
                return Some(n);
            }
            n -= 2;
        }

        if n == 2 {
            Some(2)
        } else {
            None
        }
    }

    /// Approximate prime count using prime number theorem
    fn approximate_prime_count(n: u64) -> usize {
        if n < 2 {
            return 0;
        }

        let n_f = n as f64;
        let ln_n = n_f.ln();

        // Use improved approximation
        let approx = n_f / (ln_n - 1.0);
        approx as usize
    }
}

// SQL Function implementations

/// PRIME(n) - Returns the nth prime number
pub struct PrimeFunction;

impl SqlFunction for PrimeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "PRIME",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns the Nth prime number (1-indexed)",
            returns: "INTEGER",
            examples: vec![
                "SELECT PRIME(1)",     // Returns 2
                "SELECT PRIME(100)",   // Returns 541
                "SELECT PRIME(10000)", // Returns 104729
            ],
        }
    }

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

        let n = match &args[0] {
            DataValue::Integer(i) if *i > 0 => *i as usize,
            DataValue::Integer(_) => return Err(anyhow!("PRIME index must be positive")),
            DataValue::Float(f) if *f > 0.0 => *f as usize,
            _ => return Err(anyhow!("PRIME requires a positive integer argument")),
        };

        let prime = PrimeEngine::nth_prime(n)?;
        Ok(DataValue::Integer(prime as i64))
    }
}

/// `NTH_PRIME(n)` - Alias for PRIME function (more descriptive name)
pub struct NthPrimeFunction;

impl SqlFunction for NthPrimeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "NTH_PRIME",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns the Nth prime number (1-indexed) - alias for PRIME",
            returns: "INTEGER",
            examples: vec![
                "SELECT NTH_PRIME(1)",     // Returns 2
                "SELECT NTH_PRIME(100)",   // Returns 541
                "SELECT NTH_PRIME(10000)", // Returns 104729
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        // Delegate to PRIME implementation
        PrimeFunction.evaluate(args)
    }
}

/// `IS_PRIME(n)` - Check if a number is prime
pub struct IsPrimeFunction;

impl SqlFunction for IsPrimeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "IS_PRIME",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns true if the number is prime, false otherwise",
            returns: "BOOLEAN",
            examples: vec![
                "SELECT IS_PRIME(17)",     // Returns true
                "SELECT IS_PRIME(100)",    // Returns false
                "SELECT IS_PRIME(104729)", // Returns true
            ],
        }
    }

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

        let n = match &args[0] {
            DataValue::Integer(i) if *i >= 0 => *i as u64,
            DataValue::Integer(_) => return Ok(DataValue::Boolean(false)),
            DataValue::Float(f) if *f >= 0.0 => *f as u64,
            _ => return Err(anyhow!("IS_PRIME requires a non-negative integer argument")),
        };

        Ok(DataValue::Boolean(PrimeEngine::is_prime(n)))
    }
}

/// `PRIME_COUNT(n)` - Count primes up to n
pub struct PrimeCountFunction;

impl SqlFunction for PrimeCountFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "PRIME_COUNT",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns the count of prime numbers up to n (Ï€(n))",
            returns: "INTEGER",
            examples: vec![
                "SELECT PRIME_COUNT(10)",   // Returns 4 (2,3,5,7)
                "SELECT PRIME_COUNT(100)",  // Returns 25
                "SELECT PRIME_COUNT(1000)", // Returns 168
            ],
        }
    }

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

        let n = match &args[0] {
            DataValue::Integer(i) if *i >= 0 => *i as u64,
            DataValue::Integer(_) => return Ok(DataValue::Integer(0)),
            DataValue::Float(f) if *f >= 0.0 => *f as u64,
            _ => {
                return Err(anyhow!(
                    "PRIME_COUNT requires a non-negative integer argument"
                ))
            }
        };

        Ok(DataValue::Integer(PrimeEngine::prime_count(n) as i64))
    }
}

/// `PRIME_PI(n)` - Alias for PRIME_COUNT (standard mathematical notation π(n))
pub struct PrimePiFunction;

impl SqlFunction for PrimePiFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "PRIME_PI",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description:
                "Returns the count of prime numbers up to n (Ï€(n)) - alias for PRIME_COUNT",
            returns: "INTEGER",
            examples: vec![
                "SELECT PRIME_PI(10)",   // Returns 4 (2,3,5,7)
                "SELECT PRIME_PI(100)",  // Returns 25
                "SELECT PRIME_PI(1000)", // Returns 168
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        // Delegate to PRIME_COUNT implementation
        PrimeCountFunction.evaluate(args)
    }
}

/// `NEXT_PRIME(n)` - Find the next prime >= n
pub struct NextPrimeFunction;

impl SqlFunction for NextPrimeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "NEXT_PRIME",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns the smallest prime number >= n",
            returns: "INTEGER",
            examples: vec![
                "SELECT NEXT_PRIME(100)",  // Returns 101
                "SELECT NEXT_PRIME(97)",   // Returns 97 (already prime)
                "SELECT NEXT_PRIME(1000)", // Returns 1009
            ],
        }
    }

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

        let n = match &args[0] {
            DataValue::Integer(i) if *i >= 0 => *i as u64,
            DataValue::Integer(_) => return Ok(DataValue::Integer(2)),
            DataValue::Float(f) if *f >= 0.0 => *f as u64,
            _ => {
                return Err(anyhow!(
                    "NEXT_PRIME requires a non-negative integer argument"
                ))
            }
        };

        Ok(DataValue::Integer(PrimeEngine::next_prime(n) as i64))
    }
}

/// `PREV_PRIME(n)` - Find the previous prime <= n
pub struct PrevPrimeFunction;

impl SqlFunction for PrevPrimeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "PREV_PRIME",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Returns the largest prime number <= n",
            returns: "INTEGER",
            examples: vec![
                "SELECT PREV_PRIME(100)",  // Returns 97
                "SELECT PREV_PRIME(97)",   // Returns 97 (already prime)
                "SELECT PREV_PRIME(1000)", // Returns 997
            ],
        }
    }

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

        let n = match &args[0] {
            DataValue::Integer(i) if *i >= 0 => *i as u64,
            DataValue::Integer(_) => return Ok(DataValue::Null),
            DataValue::Float(f) if *f >= 0.0 => *f as u64,
            _ => {
                return Err(anyhow!(
                    "PREV_PRIME requires a non-negative integer argument"
                ))
            }
        };

        match PrimeEngine::prev_prime(n) {
            Some(p) => Ok(DataValue::Integer(p as i64)),
            None => Ok(DataValue::Null),
        }
    }
}

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

    #[test]
    fn test_nth_prime() {
        assert_eq!(PrimeEngine::nth_prime(1).unwrap(), 2);
        assert_eq!(PrimeEngine::nth_prime(10).unwrap(), 29);
        assert_eq!(PrimeEngine::nth_prime(100).unwrap(), 541);
        assert_eq!(PrimeEngine::nth_prime(1000).unwrap(), 7919);
        assert_eq!(PrimeEngine::nth_prime(10000).unwrap(), 104729);
    }

    #[test]
    fn test_is_prime() {
        assert!(!PrimeEngine::is_prime(0));
        assert!(!PrimeEngine::is_prime(1));
        assert!(PrimeEngine::is_prime(2));
        assert!(PrimeEngine::is_prime(17));
        assert!(!PrimeEngine::is_prime(100));
        assert!(PrimeEngine::is_prime(104729));
        assert!(PrimeEngine::is_prime(1299709)); // 100,000th prime
    }

    #[test]
    fn test_prime_count() {
        assert_eq!(PrimeEngine::prime_count(10), 4); // 2, 3, 5, 7
        assert_eq!(PrimeEngine::prime_count(100), 25);
        assert_eq!(PrimeEngine::prime_count(1000), 168);
    }

    #[test]
    fn test_next_prev_prime() {
        assert_eq!(PrimeEngine::next_prime(100), 101);
        assert_eq!(PrimeEngine::next_prime(97), 97);

        assert_eq!(PrimeEngine::prev_prime(100), Some(97));
        assert_eq!(PrimeEngine::prev_prime(97), Some(97));
        assert_eq!(PrimeEngine::prev_prime(1), None);
    }

    #[test]
    fn test_factorization() {
        let factors = PrimeEngine::factor(60);
        assert_eq!(factors, vec![(2, 2), (3, 1), (5, 1)]);

        let factors = PrimeEngine::factor(97);
        assert_eq!(factors, vec![(97, 1)]);

        let factors = PrimeEngine::factor(1001);
        assert_eq!(factors, vec![(7, 1), (11, 1), (13, 1)]);
    }
}