turso_core 0.6.1

The Turso database library
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
use either::Either;
use turso_parser::ast::{Expr, Literal};

use crate::{
    numeric::{format_float, DoubleDouble, Numeric},
    types::AsValueRef,
    Value, ValueRef,
};

/// # SQLite Column Type Affinities
///
/// Each column in an SQLite 3 database is assigned one of the following type affinities:
///
/// - **TEXT**
/// - **NUMERIC**
/// - **INTEGER**
/// - **REAL**
/// - **BLOB**
///
/// > **Note:** Historically, the "BLOB" type affinity was called "NONE". However, this term was renamed to avoid confusion with "no affinity".
///
/// ## Affinity Descriptions
///
/// ### **TEXT**
/// - Stores data using the NULL, TEXT, or BLOB storage classes.
/// - Numerical data inserted into a column with TEXT affinity is converted into text form before being stored.
/// - **Example:**
///   ```sql
///   CREATE TABLE example (col TEXT);
///   INSERT INTO example (col) VALUES (123); -- Stored as '123' (text)
///   SELECT typeof(col) FROM example; -- Returns 'text'
///   ```
///
/// ### **NUMERIC**
/// - Can store values using all five storage classes.
/// - Text data is converted to INTEGER or REAL (in that order of preference) if it is a well-formed integer or real literal.
/// - If the text represents an integer too large for a 64-bit signed integer, it is converted to REAL.
/// - If the text is not a well-formed literal, it is stored as TEXT.
/// - Hexadecimal integer literals are stored as TEXT for historical compatibility.
/// - Floating-point values that can be exactly represented as integers are converted to integers.
/// - **Example:**
///   ```sql
///   CREATE TABLE example (col NUMERIC);
///   INSERT INTO example (col) VALUES ('3.0e+5'); -- Stored as 300000 (integer)
///   SELECT typeof(col) FROM example; -- Returns 'integer'
///   ```
///
/// ### **INTEGER**
/// - Behaves like NUMERIC affinity but differs in `CAST` expressions.
/// - **Example:**
///   ```sql
///   CREATE TABLE example (col INTEGER);
///   INSERT INTO example (col) VALUES (4.0); -- Stored as 4 (integer)
///   SELECT typeof(col) FROM example; -- Returns 'integer'
///   ```
///
/// ### **REAL**
/// - Similar to NUMERIC affinity but forces integer values into floating-point representation.
/// - **Optimization:** Small floating-point values with no fractional component may be stored as integers on disk to save space. This is invisible at the SQL level.
/// - **Example:**
///   ```sql
///   CREATE TABLE example (col REAL);
///   INSERT INTO example (col) VALUES (4); -- Stored as 4.0 (real)
///   SELECT typeof(col) FROM example; -- Returns 'real'
///   ```
///
/// ### **BLOB**
/// - Does not prefer any storage class.
/// - No coercion is performed between storage classes.
/// - **Example:**
///   ```sql
///   CREATE TABLE example (col BLOB);
///   INSERT INTO example (col) VALUES (x'1234'); -- Stored as a binary blob
///   SELECT typeof(col) FROM example; -- Returns 'blob'
///   ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Affinity {
    Blob = 0,
    Text = 1,
    Numeric = 2,
    Integer = 3,
    Real = 4,
}

pub const SQLITE_AFF_NONE: char = 'A'; // Historically called NONE, but it's the same as BLOB
pub const SQLITE_AFF_TEXT: char = 'B';
pub const SQLITE_AFF_NUMERIC: char = 'C';
pub const SQLITE_AFF_INTEGER: char = 'D';
pub const SQLITE_AFF_REAL: char = 'E';

impl Affinity {
    /// This is meant to be used in opcodes like Eq, which state:
    ///
    /// "The SQLITE_AFF_MASK portion of P5 must be an affinity character - SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth.
    /// An attempt is made to coerce both inputs according to this affinity before the comparison is made.
    /// If the SQLITE_AFF_MASK is 0x00, then numeric affinity is used.
    /// Note that the affinity conversions are stored back into the input registers P1 and P3.
    /// So this opcode can cause persistent changes to registers P1 and P3.""
    pub fn aff_mask(&self) -> char {
        match self {
            Affinity::Integer => SQLITE_AFF_INTEGER,
            Affinity::Text => SQLITE_AFF_TEXT,
            Affinity::Blob => SQLITE_AFF_NONE,
            Affinity::Real => SQLITE_AFF_REAL,
            Affinity::Numeric => SQLITE_AFF_NUMERIC,
        }
    }

    pub fn from_char(char: char) -> Self {
        match char {
            SQLITE_AFF_INTEGER => Affinity::Integer,
            SQLITE_AFF_TEXT => Affinity::Text,
            SQLITE_AFF_NONE => Affinity::Blob,
            SQLITE_AFF_REAL => Affinity::Real,
            SQLITE_AFF_NUMERIC => Affinity::Numeric,
            _ => Affinity::Blob,
        }
    }

    pub fn as_char_code(&self) -> u8 {
        self.aff_mask() as u8
    }

    pub fn from_char_code(code: u8) -> Self {
        Self::from_char(code as char)
    }

    pub fn is_numeric(&self) -> bool {
        matches!(self, Affinity::Integer | Affinity::Real | Affinity::Numeric)
    }

    pub fn has_affinity(&self) -> bool {
        !matches!(self, Affinity::Blob)
    }

    /// Returns the canonical short type name for this affinity, matching
    /// SQLite's `azType[]` in `createTableStmt()` (`build.c`).
    ///
    /// Used when generating schema SQL (e.g. for `sqlite_schema.sql`).
    /// Returns an empty string for BLOB affinity (no declared type).
    pub fn short_type_name(&self) -> &'static str {
        match self {
            Affinity::Blob => "",
            Affinity::Text => "TEXT",
            Affinity::Numeric => "NUM",
            Affinity::Integer => "INT",
            Affinity::Real => "REAL",
        }
    }

    /// 3.1. Determination Of Column Affinity
    /// For tables not declared as STRICT, the affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:
    ///
    /// If the declared type contains the string "INT" then it is assigned INTEGER affinity.
    ///
    /// If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.
    ///
    /// If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity BLOB.
    ///
    /// If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.
    ///
    /// Otherwise, the affinity is NUMERIC.
    ///
    /// Note that the order of the rules for determining column affinity is important. A column whose declared type is "CHARINT" will match both rules 1 and 2 but the first rule takes precedence and so the column affinity will be INTEGER.
    #[expect(clippy::self_named_constructors)]
    pub fn affinity(datatype: &str) -> Self {
        let datatype = datatype.to_ascii_uppercase();

        // Rule 1: INT -> INTEGER affinity
        if datatype.contains("INT") {
            return Affinity::Integer;
        }

        // Rule 2: CHAR/CLOB/TEXT -> TEXT affinity
        if datatype.contains("CHAR") || datatype.contains("CLOB") || datatype.contains("TEXT") {
            return Affinity::Text;
        }

        // Rule 3: BLOB or empty -> BLOB affinity (historically called NONE)
        if datatype.contains("BLOB") || datatype.is_empty() {
            return Affinity::Blob;
        }

        // Rule 4: REAL/FLOA/DOUB -> REAL affinity
        if datatype.contains("REAL") || datatype.contains("FLOA") || datatype.contains("DOUB") {
            return Affinity::Real;
        }

        // Rule 5: Otherwise -> NUMERIC affinity
        Affinity::Numeric
    }

    pub fn convert<'a>(&self, val: &'a impl AsValueRef) -> Option<Either<ValueRef<'a>, Value>> {
        let val = val.as_value_ref();
        let is_text = matches!(val, ValueRef::Text(_));
        // Apply affinity conversions
        match self {
            Affinity::Numeric | Affinity::Integer => is_text
                .then(|| apply_numeric_affinity(val, false))
                .flatten()
                .map(Either::Left),

            Affinity::Text => {
                // TEXT affinity: Convert numeric values to their text representation
                match val {
                    ValueRef::Numeric(Numeric::Integer(i)) => {
                        Some(Either::Right(Value::Text(i.to_string().into())))
                    }
                    ValueRef::Numeric(Numeric::Float(f)) => Some(Either::Right(Value::Text(
                        format_float(f64::from(f)).into(),
                    ))),
                    ValueRef::Text(_) => {
                        // If it's already text but looks numeric, ensure it's in canonical text form
                        if is_numeric_value(val) {
                            stringify_register(val).map(Either::Right)
                        } else {
                            None // Already text, no conversion needed
                        }
                    }
                    _ => None, // Blob and Null are not converted
                }
            }

            Affinity::Real => {
                let mut left = is_text
                    .then(|| apply_numeric_affinity(val, false))
                    .flatten();

                if let ValueRef::Numeric(Numeric::Integer(i)) = left.unwrap_or(val) {
                    left = Some(ValueRef::from_f64(i as f64));
                }

                left.map(Either::Left)
            }

            Affinity::Blob => None, // Do nothing for blob affinity.
        }
    }

    /// Like [`Self::convert`] but for the value-pre-conversion that comparison
    /// opcodes apply via the affinity flag in `OP_Eq` and friends.
    ///
    /// Storage and default-value paths use [`Self::convert`], which forces
    /// `Numeric::Integer` operands into `Numeric::Float` for `Affinity::Real`.
    /// That's correct for *storing* into a REAL column, but would silently
    /// lose precision when used to compare a REAL column against a 64-bit
    /// integer literal: `i as f64` rounds the integer onto the nearest
    /// representable double, which can collapse to the same double as the
    /// column's value even when the underlying integer is distinct.
    ///
    /// SQLite avoids this by only applying numeric affinity to TEXT operands
    /// in `OP_Eq` (see `applyNumericAffinity` in `vdbeaux.c`); int-vs-real
    /// comparisons fall through to `sqlite3IntFloatCompare`, which keeps the
    /// integer at full 64-bit precision.
    pub fn convert_for_compare<'a>(
        &self,
        val: &'a impl AsValueRef,
    ) -> Option<Either<ValueRef<'a>, Value>> {
        let val_ref = val.as_value_ref();
        let is_text = matches!(val_ref, ValueRef::Text(_));
        match self {
            Affinity::Numeric | Affinity::Integer | Affinity::Real => is_text
                .then(|| apply_numeric_affinity(val_ref, false))
                .flatten()
                .map(Either::Left),
            Affinity::Text => self.convert(val),
            Affinity::Blob => None,
        }
    }

    /// Return TRUE if the given expression is a constant which would be
    /// unchanged by OP_Affinity with the affinity given in the second
    /// argument.
    ///
    /// This routine is used to determine if the OP_Affinity operation
    /// can be omitted.  When in doubt return FALSE.  A false negative
    /// is harmless.  A false positive, however, can result in the wrong
    /// answer.
    ///
    /// reference https://github.com/sqlite/sqlite/blob/master/src/expr.c#L3000
    pub fn expr_needs_no_affinity_change(&self, expr: &Expr) -> bool {
        if !self.has_affinity() {
            return true;
        }
        // TODO: check for unary minus in the expr, as it may be an additional optimization.
        // This involves mostly likely walking the expression
        match expr {
            Expr::Literal(literal) => match literal {
                Literal::Numeric(_) => self.is_numeric(),
                Literal::String(_) => matches!(self, Affinity::Text),
                Literal::Blob(_) => true,
                _ => false,
            },
            Expr::Column {
                is_rowid_alias: true,
                ..
            } => self.is_numeric(),
            _ => false,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum NumericParseResult {
    NotNumeric,      // not a valid number
    PureInteger,     // pure integer (entire string)
    HasDecimalOrExp, // has decimal point or exponent (entire string)
    ValidPrefixOnly, // valid prefix but not entire string
}

#[derive(Debug)]
pub enum ParsedNumber {
    None,
    Integer(i64),
    Float(f64),
}

impl ParsedNumber {
    fn as_integer(&self) -> Option<i64> {
        match self {
            ParsedNumber::Integer(i) => Some(*i),
            _ => None,
        }
    }

    fn as_float(&self) -> Option<f64> {
        match self {
            ParsedNumber::Float(f) => Some(*f),
            _ => None,
        }
    }
}

pub fn try_for_float(bytes: &[u8]) -> (NumericParseResult, ParsedNumber) {
    if bytes.is_empty() {
        return (NumericParseResult::NotNumeric, ParsedNumber::None);
    }

    let mut pos = 0;
    let len = bytes.len();

    while pos < len && is_space(bytes[pos]) {
        pos += 1;
    }

    if pos >= len {
        return (NumericParseResult::NotNumeric, ParsedNumber::None);
    }

    let mut sign = 1i64;

    if bytes[pos] == b'-' {
        sign = -1;
        pos += 1;
    } else if bytes[pos] == b'+' {
        pos += 1;
    }

    if pos >= len {
        return (NumericParseResult::NotNumeric, ParsedNumber::None);
    }

    let mut significand = 0u64;
    let mut decimal_adjust = 0i32;
    let mut has_digits = false;

    // Parse digits before decimal point
    while pos < len && bytes[pos].is_ascii_digit() {
        has_digits = true;
        let digit = (bytes[pos] - b'0') as u64;

        if significand <= (u64::MAX - 9) / 10 {
            significand = significand * 10 + digit;
        } else {
            // Skip overflow digits but adjust exponent
            decimal_adjust += 1;
        }
        pos += 1;
    }

    let mut has_decimal = false;
    let mut has_exponent = false;

    // Check for decimal point
    if pos < len && bytes[pos] == b'.' {
        has_decimal = true;
        pos += 1;

        // Parse fractional digits
        while pos < len && bytes[pos].is_ascii_digit() {
            has_digits = true;
            let digit = (bytes[pos] - b'0') as u64;

            if significand <= (u64::MAX - 9) / 10 {
                significand = significand * 10 + digit;
                decimal_adjust -= 1;
            }
            pos += 1;
        }
    }

    if !has_digits {
        return (NumericParseResult::NotNumeric, ParsedNumber::None);
    }

    // Check for exponent
    let mut exponent = 0i32;
    if pos < len && (bytes[pos] == b'e' || bytes[pos] == b'E') {
        has_exponent = true;
        pos += 1;

        if pos >= len {
            // Incomplete exponent, but we have valid digits before
            return create_result_from_significand(
                significand,
                sign,
                decimal_adjust,
                has_decimal,
                has_exponent,
                NumericParseResult::ValidPrefixOnly,
            );
        }

        let mut exp_sign = 1i32;
        if bytes[pos] == b'-' {
            exp_sign = -1;
            pos += 1;
        } else if bytes[pos] == b'+' {
            pos += 1;
        }

        if pos >= len || !bytes[pos].is_ascii_digit() {
            // Incomplete exponent
            return create_result_from_significand(
                significand,
                sign,
                decimal_adjust,
                has_decimal,
                false,
                NumericParseResult::ValidPrefixOnly,
            );
        }

        // Parse exponent digits
        while pos < len && bytes[pos].is_ascii_digit() {
            let digit = (bytes[pos] - b'0') as i32;
            if exponent < 10000 {
                exponent = exponent * 10 + digit;
            } else {
                exponent = 10000; // Cap at large value
            }
            pos += 1;
        }
        exponent *= exp_sign;
    }

    // Skip trailing whitespace
    while pos < len && is_space(bytes[pos]) {
        pos += 1;
    }

    // Determine if we consumed the entire string
    let consumed_all = pos >= len;
    let final_exponent = decimal_adjust + exponent;

    let parse_result = if !consumed_all {
        NumericParseResult::ValidPrefixOnly
    } else if has_decimal || has_exponent {
        NumericParseResult::HasDecimalOrExp
    } else {
        NumericParseResult::PureInteger
    };

    create_result_from_significand(
        significand,
        sign,
        final_exponent,
        has_decimal,
        has_exponent,
        parse_result,
    )
}

fn create_result_from_significand(
    significand: u64,
    sign: i64,
    exponent: i32,
    has_decimal: bool,
    has_exponent: bool,
    parse_result: NumericParseResult,
) -> (NumericParseResult, ParsedNumber) {
    if significand == 0 {
        match parse_result {
            NumericParseResult::PureInteger => {
                return (parse_result, ParsedNumber::Integer(0));
            }
            _ => {
                return (parse_result, ParsedNumber::Float(0.0));
            }
        }
    }

    // For pure integers without exponent, try to return as integer
    if !has_decimal && !has_exponent && exponent == 0 && significand <= i64::MAX as u64 {
        let signed_val = (significand as i64).wrapping_mul(sign);
        return (parse_result, ParsedNumber::Integer(signed_val));
    }

    // Convert to float using Dekker double-double arithmetic for precision
    // This matches SQLite's sqlite3AtoF implementation
    let mut result = DoubleDouble::from(significand);

    let mut exp = exponent;
    match exp.cmp(&0) {
        std::cmp::Ordering::Greater => {
            while exp >= 100 {
                result *= DoubleDouble::E100;
                exp -= 100;
            }
            while exp >= 10 {
                result *= DoubleDouble::E10;
                exp -= 10;
            }
            while exp >= 1 {
                result *= DoubleDouble::E1;
                exp -= 1;
            }
        }
        std::cmp::Ordering::Less => {
            while exp <= -100 {
                result *= DoubleDouble::NEG_E100;
                exp += 100;
            }
            while exp <= -10 {
                result *= DoubleDouble::NEG_E10;
                exp += 10;
            }
            while exp <= -1 {
                result *= DoubleDouble::NEG_E1;
                exp += 1;
            }
        }
        std::cmp::Ordering::Equal => {}
    }

    let mut final_result: f64 = result.into();
    if final_result.is_nan() {
        final_result = f64::INFINITY;
    }
    if sign < 0 {
        final_result = -final_result;
    }

    (parse_result, ParsedNumber::Float(final_result))
}

pub fn is_space(byte: u8) -> bool {
    matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0c')
}

pub(crate) fn real_to_i64(r: f64) -> i64 {
    if r < -9223372036854774784.0 {
        i64::MIN
    } else if r > 9223372036854774784.0 {
        i64::MAX
    } else {
        r as i64
    }
}

fn apply_integer_affinity(val: ValueRef) -> Option<ValueRef> {
    let ValueRef::Numeric(Numeric::Float(nn)) = val else {
        return None;
    };

    let f: f64 = nn.into();
    let ix = real_to_i64(f);

    // Only convert if round-trip is exact and not at extreme values
    if f == (ix as f64) && ix > i64::MIN && ix < i64::MAX {
        Some(ValueRef::Numeric(Numeric::Integer(ix)))
    } else {
        None
    }
}

/// Try to convert a value into a numeric representation if we can
/// do so without loss of information. In other words, if the string
/// looks like a number, convert it into a number. If it does not
/// look like a number, leave it alone.
pub fn apply_numeric_affinity(val: ValueRef, try_for_int: bool) -> Option<ValueRef> {
    let ValueRef::Text(text) = val else {
        return None; // Only apply to text values
    };

    let text_str = text.as_str();
    let (parse_result, parsed_value) = try_for_float(text_str.as_bytes());

    // Only convert if we have a complete valid number (not just a prefix)
    match parse_result {
        NumericParseResult::NotNumeric | NumericParseResult::ValidPrefixOnly => {
            None // Leave as text
        }
        NumericParseResult::PureInteger => {
            if let Some(int_val) = parsed_value.as_integer() {
                Some(ValueRef::Numeric(Numeric::Integer(int_val)))
            } else if let Some(float_val) = parsed_value.as_float() {
                let res = ValueRef::from_f64(float_val);
                if try_for_int {
                    apply_integer_affinity(res)
                } else {
                    Some(res)
                }
            } else {
                None
            }
        }
        NumericParseResult::HasDecimalOrExp => {
            if let Some(float_val) = parsed_value.as_float() {
                // Failed parses can occasionally surface as NaN. Treat those as
                // non-convertible so we keep the original text value instead of
                // coercing to NULL during comparison affinity conversion.
                if float_val.is_nan() {
                    return None;
                }

                let res = ValueRef::from_f64(float_val);
                // If try_for_int is true, try to convert float to int if exact
                if try_for_int {
                    apply_integer_affinity(res)
                } else {
                    Some(res)
                }
            } else {
                None
            }
        }
    }
}

fn is_numeric_value(val: ValueRef) -> bool {
    matches!(val, ValueRef::Numeric(_))
}

fn stringify_register(val: ValueRef) -> Option<Value> {
    match val {
        ValueRef::Numeric(Numeric::Integer(i)) => Some(Value::build_text(i.to_string())),
        ValueRef::Numeric(Numeric::Float(f)) => Some(Value::build_text(f64::from(f).to_string())),
        _ => None,
    }
}

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

    #[test]
    fn test_apply_numeric_affinity_partial_numbers() {
        let val = Value::Text("123abc".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert!(res.is_none());

        let val = Value::Text("-53093015420544-15062897".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert!(res.is_none());

        let val = Value::Text("123.45xyz".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert!(res.is_none());
    }

    #[test]
    fn test_apply_numeric_affinity_complete_numbers() {
        let val = Value::Text("123".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert_eq!(res, Some(ValueRef::Numeric(Numeric::Integer(123))));

        let val = Value::Text("123.45".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert_eq!(res, Some(ValueRef::from_f64(123.45)));

        let val = Value::Text("  -456  ".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert_eq!(res, Some(ValueRef::Numeric(Numeric::Integer(-456))));

        let val = Value::Text("0".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert_eq!(res, Some(ValueRef::Numeric(Numeric::Integer(0))));
    }

    #[test]
    fn test_apply_numeric_affinity_extreme_exponent_gives_infinity() {
        let val = Value::Text("3139353734372E383932303939343135".into());
        let res = apply_numeric_affinity(val.as_value_ref(), false);
        assert!(res.is_some());
        match res.unwrap() {
            ValueRef::Numeric(Numeric::Float(f)) => assert!(f64::from(f).is_infinite()),
            other => panic!("expected Float, got {other:?}"),
        }
    }

    #[test]
    fn test_try_for_float_precision() {
        // This test verifies that try_for_float uses high-precision arithmetic
        // to avoid rounding errors when computing significand * 10^exponent.
        // Naive f64 multiplication accumulates errors; Dekker double-double fixes this.
        let (_, parsed) = try_for_float(b"12345678901234567e-5");
        let expected: f64 = "12345678901234567e-5".parse().unwrap();
        assert_eq!(
            parsed.as_float().unwrap().to_bits(),
            expected.to_bits(),
            "try_for_float precision mismatch: got {}, expected {expected}",
            parsed.as_float().unwrap(),
        );
    }
}