vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! String manipulation functions
//!
//! This module contains SQLite-compatible string functions:
//! - CHAR(x1, x2, ...) - Return string from Unicode code points
//! - UNICODE(x) - Return Unicode code point of first character
//! - CONCAT_WS(sep, ...) - Concatenate with separator
//! - PRINTF(format, ...) - Formatted string output

use vibesql_types::SqlValue;

use crate::errors::ExecutorError;

/// UNICODE(x) - Return the Unicode code point of the first character
///
/// Returns the numeric unicode code point of the first character of string x.
/// Returns NULL if the argument is NULL or an empty string.
pub(crate) fn unicode(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
    if args.len() != 1 {
        return Err(ExecutorError::UnsupportedFeature(format!(
            "UNICODE requires exactly 1 argument, got {}",
            args.len()
        )));
    }

    let s = match &args[0] {
        SqlValue::Null => return Ok(SqlValue::Null),
        SqlValue::Varchar(s) | SqlValue::Character(s) => s.as_str(),
        _ => {
            return Err(ExecutorError::UnsupportedFeature(
                "UNICODE argument must be a string".to_string(),
            ));
        }
    };

    match s.chars().next() {
        Some(c) => Ok(SqlValue::Integer(c as i64)),
        None => Ok(SqlValue::Null), // Empty string returns NULL
    }
}

/// CHAR(x1, x2, ...) - Return string from Unicode code points
///
/// Returns a string composed of characters having the unicode code point values
/// given by the arguments. NULL arguments are skipped.
pub(crate) fn char_func(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
    if args.is_empty() {
        return Ok(SqlValue::Varchar("".into()));
    }

    let mut result = String::with_capacity(args.len());

    for arg in args {
        let code_point = match arg {
            SqlValue::Null => continue, // Skip NULL arguments
            SqlValue::Integer(i) => *i as u32,
            SqlValue::Bigint(i) => *i as u32,
            SqlValue::Smallint(i) => *i as u32,
            SqlValue::Unsigned(u) => *u as u32,
            SqlValue::Numeric(n) => *n as u32,
            SqlValue::Real(r) => *r as u32,
            SqlValue::Double(d) => *d as u32,
            SqlValue::Float(f) => *f as u32,
            _ => {
                return Err(ExecutorError::UnsupportedFeature(
                    "CHAR arguments must be numeric".to_string(),
                ));
            }
        };

        if let Some(c) = char::from_u32(code_point) {
            result.push(c);
        }
        // Invalid code points are silently skipped (SQLite behavior)
    }

    Ok(SqlValue::Varchar(result.into()))
}

/// CONCAT_WS(separator, str1, str2, ...) - Concatenate with separator
///
/// Concatenates strings with the first argument as separator.
/// NULL values are skipped (not included in result).
/// Returns NULL if the separator is NULL.
/// SQLite requires at least 2 arguments (separator + at least one value).
pub(crate) fn concat_ws(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
    // SQLite requires at least 2 arguments: separator and at least one value
    if args.len() < 2 {
        return Err(ExecutorError::WrongNumberOfArguments {
            function_name: "concat_ws".to_string(),
        });
    }

    // First argument is the separator
    let separator = match &args[0] {
        SqlValue::Null => return Ok(SqlValue::Null),
        SqlValue::Varchar(s) | SqlValue::Character(s) => s.to_string(),
        other => other.to_string(),
    };

    // Remaining arguments are the strings to concatenate
    let mut parts: Vec<String> = Vec::new();
    for arg in &args[1..] {
        match arg {
            SqlValue::Null => continue, // Skip NULL values
            SqlValue::Varchar(s) | SqlValue::Character(s) => parts.push(s.to_string()),
            other => parts.push(other.to_string()),
        }
    }

    Ok(SqlValue::Varchar(parts.join(&separator).into()))
}

/// Parsed printf format specifier
#[derive(Default)]
struct FormatSpec {
    /// '-' flag: left-justify within the given field width
    left_justify: bool,
    /// '+' flag: always show sign for numeric types
    show_sign: bool,
    /// ' ' flag: use space for positive sign
    space_sign: bool,
    /// '#' flag: alternative form (0x for hex, 0 for octal - but NOT for zero values)
    alternate: bool,
    /// '0' flag: pad with zeros instead of spaces
    zero_pad: bool,
    /// Minimum field width
    width: Option<usize>,
    /// Precision (for floats: decimal places; for strings: max length)
    precision: Option<usize>,
    /// Whether precision should be read from the next argument (for %.*s, %.*c, etc.)
    precision_from_arg: bool,
    /// The conversion specifier character
    specifier: char,
}

/// PRINTF(format, ...) - Formatted string output
///
/// Returns a string formatted according to the format string, similar to C's printf.
/// Supports: %d, %i (integer), %f (float), %e, %E (scientific), %s (string),
/// %x, %X (hex), %o (octal), %c (character), %% (literal %)
/// Flags: - (left-justify), + (show sign), space, # (alternate form), 0 (zero-pad)
pub(crate) fn printf(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
    if args.is_empty() {
        return Err(ExecutorError::UnsupportedFeature(
            "PRINTF requires at least 1 argument (format string)".to_string(),
        ));
    }

    let format_str = match &args[0] {
        SqlValue::Null => return Ok(SqlValue::Null),
        SqlValue::Varchar(s) | SqlValue::Character(s) => s.as_str(),
        _ => {
            return Err(ExecutorError::UnsupportedFeature(
                "PRINTF format must be a string".to_string(),
            ));
        }
    };

    let format_args = &args[1..];
    let mut result = String::new();
    let mut arg_index = 0;
    let mut chars = format_str.chars().peekable();

    while let Some(c) = chars.next() {
        if c != '%' {
            result.push(c);
            continue;
        }

        // Check for %%
        if chars.peek() == Some(&'%') {
            chars.next();
            result.push('%');
            continue;
        }

        // Parse format specifier: %[flags][width][.precision]specifier
        let mut spec = parse_format_spec(&mut chars);

        // If precision comes from argument, consume it first
        if spec.precision_from_arg {
            if arg_index < format_args.len() {
                let prec_val = &format_args[arg_index];
                arg_index += 1;
                // Convert the precision argument to usize
                spec.precision = match prec_val {
                    SqlValue::Integer(i) => Some((*i).max(0) as usize),
                    SqlValue::Bigint(i) => Some((*i).max(0) as usize),
                    SqlValue::Smallint(i) => Some((*i).max(0) as usize),
                    SqlValue::Numeric(n) => Some((*n).max(0.0) as usize),
                    SqlValue::Real(r) => Some((*r).max(0.0) as usize),
                    SqlValue::Double(d) => Some((*d).max(0.0) as usize),
                    _ => Some(0),
                };
            } else {
                spec.precision = Some(0);
            }
        }

        // Format the value according to the specifier
        let formatted = if arg_index >= format_args.len() {
            "(null)".to_string()
        } else {
            let val = &format_args[arg_index];
            arg_index += 1;
            format_value(val, &spec)
        };

        result.push_str(&formatted);
    }

    Ok(SqlValue::Varchar(result.into()))
}

/// Parse a format specifier from the character stream
fn parse_format_spec(chars: &mut std::iter::Peekable<std::str::Chars>) -> FormatSpec {
    let mut spec = FormatSpec::default();

    // Parse flags
    while let Some(&c) = chars.peek() {
        match c {
            '-' => {
                spec.left_justify = true;
                chars.next();
            }
            '+' => {
                spec.show_sign = true;
                chars.next();
            }
            ' ' => {
                spec.space_sign = true;
                chars.next();
            }
            '#' => {
                spec.alternate = true;
                chars.next();
            }
            '0' => {
                spec.zero_pad = true;
                chars.next();
            }
            _ => break,
        }
    }

    // Parse width
    let mut width_str = String::new();
    while let Some(&c) = chars.peek() {
        if c.is_ascii_digit() {
            width_str.push(c);
            chars.next();
        } else {
            break;
        }
    }
    if !width_str.is_empty() {
        spec.width = width_str.parse().ok();
    }

    // Parse precision
    if chars.peek() == Some(&'.') {
        chars.next();
        // Check for * (precision from argument)
        if chars.peek() == Some(&'*') {
            chars.next();
            spec.precision_from_arg = true;
        } else {
            let mut prec_str = String::new();
            while let Some(&c) = chars.peek() {
                if c.is_ascii_digit() {
                    prec_str.push(c);
                    chars.next();
                } else {
                    break;
                }
            }
            spec.precision = Some(prec_str.parse().unwrap_or(0));
        }
    }

    // Parse specifier
    spec.specifier = chars.next().unwrap_or('s');

    spec
}

/// Format a value according to the format specifier
fn format_value(val: &SqlValue, spec: &FormatSpec) -> String {
    let raw = match spec.specifier {
        'd' | 'i' => format_int_with_spec(val, spec),
        'f' => format_float_with_spec(val, spec),
        'e' => format_scientific_with_spec(val, false, spec),
        'E' => format_scientific_with_spec(val, true, spec),
        's' => format_string_with_spec(val, spec),
        'x' => format_hex_with_spec(val, false, spec),
        'X' => format_hex_with_spec(val, true, spec),
        'o' => format_octal_with_spec(val, spec),
        'c' => format_char(val, spec.precision),
        other => format!("%{}", other),
    };

    // Apply width and justification
    apply_width(&raw, spec)
}

/// Apply width and justification to a formatted string
fn apply_width(s: &str, spec: &FormatSpec) -> String {
    let width = spec.width.unwrap_or(0);
    if s.len() >= width {
        return s.to_string();
    }

    let padding = width - s.len();
    let pad_char = if spec.zero_pad && !spec.left_justify { '0' } else { ' ' };

    if spec.left_justify {
        format!("{}{}", s, " ".repeat(padding))
    } else if spec.zero_pad && (s.starts_with('-') || s.starts_with('+')) {
        // For zero-padding with sign, put sign before zeros
        let (sign, rest) = s.split_at(1);
        format!("{}{}{}", sign, std::iter::repeat(pad_char).take(padding).collect::<String>(), rest)
    } else {
        format!("{}{}", std::iter::repeat(pad_char).take(padding).collect::<String>(), s)
    }
}

fn format_int_with_spec(val: &SqlValue, spec: &FormatSpec) -> String {
    let i64_val = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i,
        SqlValue::Bigint(i) => *i,
        SqlValue::Smallint(i) => *i as i64,
        SqlValue::Numeric(n) => *n as i64,
        SqlValue::Real(r) => *r as i64,
        SqlValue::Double(d) => *d as i64,
        SqlValue::Boolean(b) => {
            if *b {
                1
            } else {
                0
            }
        }
        _ => 0,
    };

    // SQLite's %d format treats values as 32-bit signed integers.
    // Cast to i32 to properly interpret values like 0xffffffff as -1.
    let i = i64_val as i32;

    let abs_str = i.unsigned_abs().to_string();
    let sign = if i < 0 {
        "-"
    } else if spec.show_sign {
        "+"
    } else if spec.space_sign {
        " "
    } else {
        ""
    };

    format!("{}{}", sign, abs_str)
}

fn format_float_with_spec(val: &SqlValue, spec: &FormatSpec) -> String {
    let f = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i as f64,
        SqlValue::Bigint(i) => *i as f64,
        SqlValue::Smallint(i) => *i as f64,
        SqlValue::Numeric(n) => *n,
        SqlValue::Real(r) => *r as f64,
        SqlValue::Double(d) => *d,
        SqlValue::Float(f) => *f as f64,
        _ => 0.0,
    };

    let precision = spec.precision.unwrap_or(6);
    format!("{:.prec$}", f, prec = precision)
}

fn format_scientific_with_spec(val: &SqlValue, uppercase: bool, _spec: &FormatSpec) -> String {
    let f = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i as f64,
        SqlValue::Bigint(i) => *i as f64,
        SqlValue::Smallint(i) => *i as f64,
        SqlValue::Numeric(n) => *n,
        SqlValue::Real(r) => *r as f64,
        SqlValue::Double(d) => *d,
        SqlValue::Float(f) => *f as f64,
        _ => 0.0,
    };

    if uppercase {
        format!("{:E}", f)
    } else {
        format!("{:e}", f)
    }
}

fn format_string_with_spec(val: &SqlValue, spec: &FormatSpec) -> String {
    let s = match val {
        SqlValue::Null => "(null)".to_string(),
        SqlValue::Varchar(s) | SqlValue::Character(s) => s.to_string(),
        _ => val.to_string(),
    };

    // Apply precision as max length for strings
    if let Some(prec) = spec.precision {
        s.chars().take(prec).collect()
    } else {
        s
    }
}

fn format_hex_with_spec(val: &SqlValue, uppercase: bool, spec: &FormatSpec) -> String {
    let i64_val = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i,
        SqlValue::Bigint(i) => *i,
        SqlValue::Smallint(i) => *i as i64,
        SqlValue::Numeric(n) => *n as i64,
        SqlValue::Real(r) => *r as i64,
        SqlValue::Double(d) => *d as i64,
        _ => 0,
    };

    // SQLite's %x format uses 32-bit representation
    let i = i64_val as u32;

    let hex = if uppercase { format!("{:X}", i) } else { format!("{:x}", i) };

    // Per C standard: # flag adds 0x/0X prefix, but NOT for zero values
    if spec.alternate && i != 0 {
        let prefix = if uppercase { "0X" } else { "0x" };
        format!("{}{}", prefix, hex)
    } else {
        hex
    }
}

fn format_octal_with_spec(val: &SqlValue, spec: &FormatSpec) -> String {
    let i64_val = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i,
        SqlValue::Bigint(i) => *i,
        SqlValue::Smallint(i) => *i as i64,
        SqlValue::Numeric(n) => *n as i64,
        SqlValue::Real(r) => *r as i64,
        SqlValue::Double(d) => *d as i64,
        _ => 0,
    };

    // SQLite's %o format uses 32-bit representation
    let i = i64_val as u32;

    let oct = format!("{:o}", i);

    // Per C standard: # flag adds leading 0 for octal (but not if already 0)
    if spec.alternate && i != 0 && !oct.starts_with('0') {
        format!("0{}", oct)
    } else {
        oct
    }
}

fn format_char(val: &SqlValue, precision: Option<usize>) -> String {
    let code = match val {
        SqlValue::Null => return "(null)".to_string(),
        SqlValue::Integer(i) => *i as u32,
        SqlValue::Bigint(i) => *i as u32,
        SqlValue::Smallint(i) => *i as u32,
        SqlValue::Numeric(n) => *n as u32,
        SqlValue::Varchar(s) | SqlValue::Character(s) => {
            // For string input, use the first character's code point (SQLite behavior)
            match s.chars().next() {
                Some(c) => c as u32,
                None => return String::new(),
            }
        }
        _ => return "".to_string(),
    };

    match char::from_u32(code) {
        Some(c) => {
            // If precision is specified, repeat the character that many times
            // This implements %.*c behavior (e.g., printf('%.*c', 5, 65) -> "AAAAA")
            let repeat_count = precision.unwrap_or(1);
            c.to_string().repeat(repeat_count)
        }
        None => String::new(),
    }
}

/// UNISTR(x) - Interpret Unicode escape sequences in a string
///
/// Converts Unicode escape sequences like \uXXXX to actual Unicode characters.
/// SQLite compatibility: Recognizes \uXXXX (4 hex digits) and \UXXXXXXXX (8 hex digits).
/// A backslash followed by anything else is passed through unchanged.
/// Returns NULL if the argument is NULL.
pub(crate) fn unistr(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
    if args.len() != 1 {
        return Err(ExecutorError::UnsupportedFeature(format!(
            "UNISTR requires exactly 1 argument, got {}",
            args.len()
        )));
    }

    let s = match &args[0] {
        SqlValue::Null => return Ok(SqlValue::Null),
        SqlValue::Varchar(s) | SqlValue::Character(s) => s.as_str(),
        other => {
            // Convert non-string to string first
            let s = other.to_string();
            return Ok(SqlValue::Varchar(process_unistr(&s).into()));
        }
    };

    Ok(SqlValue::Varchar(process_unistr(s).into()))
}

/// Process Unicode escape sequences in a string
fn process_unistr(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c != '\\' {
            result.push(c);
            continue;
        }

        // Check for \u or \U escape sequence
        match chars.peek() {
            Some('u') => {
                chars.next(); // consume 'u'
                              // Try to parse 4 hex digits
                let hex: String = chars.by_ref().take(4).collect();
                if hex.len() == 4 {
                    if let Ok(code_point) = u32::from_str_radix(&hex, 16) {
                        if let Some(ch) = char::from_u32(code_point) {
                            result.push(ch);
                            continue;
                        }
                    }
                }
                // Invalid escape - pass through as-is
                result.push('\\');
                result.push('u');
                result.push_str(&hex);
            }
            Some('U') => {
                chars.next(); // consume 'U'
                              // Try to parse 8 hex digits (for surrogate pairs or extended Unicode)
                let hex: String = chars.by_ref().take(8).collect();
                if hex.len() == 8 {
                    if let Ok(code_point) = u32::from_str_radix(&hex, 16) {
                        if let Some(ch) = char::from_u32(code_point) {
                            result.push(ch);
                            continue;
                        }
                    }
                }
                // Invalid escape - pass through as-is
                result.push('\\');
                result.push('U');
                result.push_str(&hex);
            }
            Some('+') => {
                chars.next(); // consume '+'
                              // Try to parse up to 6 hex digits (Unicode code point format \+XXXXXX)
                let mut hex = String::new();
                while hex.len() < 6 {
                    if let Some(&c) = chars.peek() {
                        if c.is_ascii_hexdigit() {
                            hex.push(c);
                            chars.next();
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                if !hex.is_empty() {
                    if let Ok(code_point) = u32::from_str_radix(&hex, 16) {
                        if let Some(ch) = char::from_u32(code_point) {
                            result.push(ch);
                            continue;
                        }
                    }
                }
                // Invalid escape - pass through as-is
                result.push('\\');
                result.push('+');
                result.push_str(&hex);
            }
            _ => {
                // Not a Unicode escape, pass through the backslash
                result.push('\\');
            }
        }
    }

    result
}

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

    #[test]
    fn test_unicode() {
        assert_eq!(unicode(&[SqlValue::Varchar("A".into())]).unwrap(), SqlValue::Integer(65));
        assert_eq!(unicode(&[SqlValue::Varchar("😀".into())]).unwrap(), SqlValue::Integer(128512));
        assert_eq!(unicode(&[SqlValue::Varchar("".into())]).unwrap(), SqlValue::Null);
        assert_eq!(unicode(&[SqlValue::Null]).unwrap(), SqlValue::Null);
    }

    #[test]
    fn test_char_func() {
        assert_eq!(
            char_func(&[SqlValue::Integer(65), SqlValue::Integer(66), SqlValue::Integer(67)])
                .unwrap(),
            SqlValue::Varchar("ABC".into())
        );
        // NULL arguments are skipped
        assert_eq!(
            char_func(&[SqlValue::Integer(65), SqlValue::Null, SqlValue::Integer(67)]).unwrap(),
            SqlValue::Varchar("AC".into())
        );
        // Empty args
        assert_eq!(char_func(&[]).unwrap(), SqlValue::Varchar("".into()));
    }

    #[test]
    fn test_concat_ws() {
        // Basic concatenation with comma separator
        assert_eq!(
            concat_ws(&[
                SqlValue::Varchar(",".into()),
                SqlValue::Varchar("a".into()),
                SqlValue::Varchar("b".into()),
                SqlValue::Varchar("c".into())
            ])
            .unwrap(),
            SqlValue::Varchar("a,b,c".into())
        );

        // NULL separator returns NULL
        assert_eq!(
            concat_ws(&[
                SqlValue::Null,
                SqlValue::Varchar("a".into()),
                SqlValue::Varchar("b".into())
            ])
            .unwrap(),
            SqlValue::Null
        );

        // NULL values in strings are skipped
        assert_eq!(
            concat_ws(&[
                SqlValue::Varchar(",".into()),
                SqlValue::Varchar("a".into()),
                SqlValue::Null,
                SqlValue::Varchar("c".into())
            ])
            .unwrap(),
            SqlValue::Varchar("a,c".into())
        );

        // Empty separator
        assert_eq!(
            concat_ws(&[
                SqlValue::Varchar("".into()),
                SqlValue::Varchar("a".into()),
                SqlValue::Varchar("b".into())
            ])
            .unwrap(),
            SqlValue::Varchar("ab".into())
        );

        // Single string (no separator used)
        assert_eq!(
            concat_ws(&[SqlValue::Varchar(",".into()), SqlValue::Varchar("only".into())]).unwrap(),
            SqlValue::Varchar("only".into())
        );

        // No strings (just separator) - SQLite requires at least 2 args
        assert!(concat_ws(&[SqlValue::Varchar(",".into())]).is_err());

        // Integers are converted to strings
        assert_eq!(
            concat_ws(&[
                SqlValue::Varchar("-".into()),
                SqlValue::Integer(1),
                SqlValue::Integer(2),
                SqlValue::Integer(3)
            ])
            .unwrap(),
            SqlValue::Varchar("1-2-3".into())
        );
    }

    #[test]
    fn test_printf() {
        // Basic integer
        assert_eq!(
            printf(&[SqlValue::Varchar("Value: %d".into()), SqlValue::Integer(42)]).unwrap(),
            SqlValue::Varchar("Value: 42".into())
        );

        // Float
        assert_eq!(
            printf(&[SqlValue::Varchar("Value: %f".into()), SqlValue::Numeric(1.5)]).unwrap(),
            SqlValue::Varchar("Value: 1.500000".into())
        );

        // String
        assert_eq!(
            printf(&[SqlValue::Varchar("Hello, %s!".into()), SqlValue::Varchar("World".into())])
                .unwrap(),
            SqlValue::Varchar("Hello, World!".into())
        );

        // Hex
        assert_eq!(
            printf(&[SqlValue::Varchar("%x".into()), SqlValue::Integer(255)]).unwrap(),
            SqlValue::Varchar("ff".into())
        );
        assert_eq!(
            printf(&[SqlValue::Varchar("%X".into()), SqlValue::Integer(255)]).unwrap(),
            SqlValue::Varchar("FF".into())
        );

        // Escaped percent
        assert_eq!(
            printf(&[SqlValue::Varchar("100%%".into())]).unwrap(),
            SqlValue::Varchar("100%".into())
        );
    }

    #[test]
    fn test_printf_char() {
        // Basic %c - single character from code point
        assert_eq!(
            printf(&[SqlValue::Varchar("%c".into()), SqlValue::Integer(65)]).unwrap(),
            SqlValue::Varchar("A".into())
        );

        // %c with asterisk - no precision specified should output single char
        assert_eq!(
            printf(&[SqlValue::Varchar("%c".into()), SqlValue::Integer(42)]).unwrap(),
            SqlValue::Varchar("*".into())
        );
    }

    #[test]
    fn test_printf_precision_from_arg() {
        // %.*c - repeat character N times
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(5),  // precision: repeat 5 times
                SqlValue::Integer(65)  // 'A'
            ])
            .unwrap(),
            SqlValue::Varchar("AAAAA".into())
        );

        // %.*c with zero precision - empty string
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(0),
                SqlValue::Integer(65)
            ])
            .unwrap(),
            SqlValue::Varchar("".into())
        );

        // %.*c with precision 1 - single character
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(1),
                SqlValue::Integer(66) // 'B'
            ])
            .unwrap(),
            SqlValue::Varchar("B".into())
        );

        // %.*c with asterisk character
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(3),
                SqlValue::Integer(42) // '*'
            ])
            .unwrap(),
            SqlValue::Varchar("***".into())
        );

        // %.*s - precision for string truncation
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*s".into()),
                SqlValue::Integer(5),
                SqlValue::Varchar("Hello, World!".into())
            ])
            .unwrap(),
            SqlValue::Varchar("Hello".into())
        );
    }

    #[test]
    fn test_printf_char_with_width() {
        // %10.*c with width and precision - right-padded with spaces
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%10.*c".into()),
                SqlValue::Integer(3),
                SqlValue::Integer(42) // '*'
            ])
            .unwrap(),
            SqlValue::Varchar("       ***".into())
        );

        // %-10.*c with left justification
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%-10.*c".into()),
                SqlValue::Integer(3),
                SqlValue::Integer(42) // '*'
            ])
            .unwrap(),
            SqlValue::Varchar("***       ".into())
        );
    }

    #[test]
    fn test_printf_char_with_string_arg() {
        // %c with string argument - uses first character's code point (SQLite behavior)
        assert_eq!(
            printf(&[SqlValue::Varchar("%c".into()), SqlValue::Varchar("A".into())]).unwrap(),
            SqlValue::Varchar("A".into())
        );

        // %.*c with string argument - repeat first character N times
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(5),
                SqlValue::Varchar("m".into()) // 'm' as string
            ])
            .unwrap(),
            SqlValue::Varchar("mmmmm".into())
        );

        // %.*c with longer string - should only use first character
        assert_eq!(
            printf(&[
                SqlValue::Varchar("%.*c".into()),
                SqlValue::Integer(3),
                SqlValue::Varchar("hello".into()) // only 'h' is used
            ])
            .unwrap(),
            SqlValue::Varchar("hhh".into())
        );

        // Mixed usage matching TCL test func-9.14 pattern
        assert_eq!(
            printf(&[
                SqlValue::Varchar("abc%.*cxyz".into()),
                SqlValue::Integer(5),
                SqlValue::Varchar("m".into())
            ])
            .unwrap(),
            SqlValue::Varchar("abcmmmmmxyz".into())
        );
    }
}