vibesql-types 0.2.0

Type system for vibesql SQL database engine
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
//! SQL Data Type definitions

use crate::temporal::IntervalField;

/// SQLite Type Affinity
///
/// SQLite uses type affinity to determine how values are compared and stored.
/// This enum represents the five affinity types defined by SQLite:
/// - TEXT: String comparison semantics
/// - NUMERIC: Numeric comparison, converts strings to numbers when possible
/// - INTEGER: Prefers integer storage
/// - REAL: Floating-point storage
/// - BLOB/NONE: No affinity, uses type ordering for cross-type comparisons
///
/// See: https://www.sqlite.org/datatype3.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeAffinity {
    /// TEXT affinity - string comparison semantics
    /// When comparing TEXT vs INTEGER, converts INTEGER to TEXT
    Text,
    /// NUMERIC affinity - tries to convert to number first
    Numeric,
    /// INTEGER affinity - prefers integer storage
    Integer,
    /// REAL affinity - floating-point storage
    Real,
    /// BLOB/NONE affinity - no type preference, uses type ordering
    /// This is used for bare columns with no declared type
    None,
}

/// SQL:1999 Data Types
///
/// Represents the type of a column or expression in SQL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataType {
    // Exact numeric types
    Integer,
    Smallint,
    Bigint,
    Unsigned, // 64-bit unsigned integer (MySQL compatibility)
    Numeric { precision: u8, scale: u8 },
    Decimal { precision: u8, scale: u8 },

    // Approximate numeric types
    Float { precision: u8 }, // SQL:1999 FLOAT(p), default 53 (double precision)
    Real,
    DoublePrecision,

    // Character string types
    Character { length: usize },
    Varchar { max_length: Option<usize> }, // None = default length (255)
    CharacterLargeObject,                  // CLOB
    Name,                                  /* NAME type for SQL identifiers (SQL:1999), maps to
                                            * VARCHAR(128) */

    // Boolean type (SQL:1999)
    Boolean,

    // Date/time types
    Date,
    Time { with_timezone: bool },
    Timestamp { with_timezone: bool },

    // Interval types
    // Single field: INTERVAL YEAR, INTERVAL MONTH, etc. (end_field is None)
    // Multi-field: INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND, etc.
    Interval { start_field: IntervalField, end_field: Option<IntervalField> },

    // Binary types
    BinaryLargeObject,             // BLOB
    Bit { length: Option<usize> }, // BIT or BIT(n), MySQL compatibility, default length is 1

    // Vector types (for AI/ML workloads)
    Vector { dimensions: u32 },

    // User-defined types (SQL:1999)
    UserDefined { type_name: String },

    // Special type for NULL
    Null,
}

impl DataType {
    /// Returns the type precedence for SQL:1999 type coercion
    ///
    /// Higher precedence types are preferred in type coercion.
    /// Based on SQL:1999 Section 9.5 (Result of data type combinations).
    ///
    /// Precedence order (highest to lowest):
    /// 1. Character strings (VARCHAR, CHAR, CLOB, NAME)
    /// 2. Approximate numerics (DOUBLE PRECISION, REAL, FLOAT)
    /// 3. Exact numerics with scale (DECIMAL, NUMERIC)
    /// 4. Exact numerics without scale (BIGINT > INTEGER > SMALLINT, UNSIGNED)
    /// 5. Boolean
    /// 6. Temporal types (TIMESTAMP > TIME > DATE)
    /// 7. Interval types
    /// 8. Binary types (BLOB)
    fn type_precedence(&self) -> u8 {
        match self {
            // NULL has lowest precedence - coerces to anything
            DataType::Null => 0,

            // Binary types
            DataType::BinaryLargeObject => 10,
            DataType::Bit { .. } => 11, // BIT type, slightly higher than BLOB

            // Interval types
            DataType::Interval { .. } => 20,

            // Temporal types (ordered by precision)
            DataType::Date => 30,
            DataType::Time { .. } => 31,
            DataType::Timestamp { .. } => 32,

            // Boolean
            DataType::Boolean => 40,

            // Exact numerics without scale (ordered by size)
            DataType::Smallint => 50,
            DataType::Integer => 51,
            DataType::Bigint => 52,
            DataType::Unsigned => 52, // Same as BIGINT (both 64-bit)

            // Exact numerics with scale
            DataType::Decimal { .. } => 60,
            DataType::Numeric { .. } => 60,

            // Approximate numerics (ordered by precision)
            DataType::Real => 70,
            DataType::Float { .. } => 71,
            DataType::DoublePrecision => 72,

            // Character strings (highest precedence)
            DataType::Character { .. } => 80,
            DataType::Varchar { .. } => 81,
            DataType::Name => 81, // NAME is equivalent to VARCHAR
            DataType::CharacterLargeObject => 82,

            // Vector types (specialized for AI/ML operations, don't coerce with other types)
            DataType::Vector { .. } => 65,

            // User-defined types don't participate in implicit coercion
            DataType::UserDefined { .. } => 255,
        }
    }

    /// Determines if implicit type coercion is possible between two types
    ///
    /// Returns true if SQL:1999 allows implicit conversion between the types.
    /// This is more permissive than exact type equality.
    fn can_implicitly_coerce(&self, other: &DataType) -> bool {
        // NULL can coerce to/from anything
        if matches!(self, DataType::Null) || matches!(other, DataType::Null) {
            return true;
        }

        match (self, other) {
            // Same types can always coerce
            (a, b) if a == b => true,

            // DECIMAL/NUMERIC with different precision/scale can coerce
            (DataType::Decimal { .. }, DataType::Decimal { .. }) => true,
            (DataType::Numeric { .. }, DataType::Numeric { .. }) => true,

            // VARCHAR with different lengths can coerce
            (DataType::Varchar { .. }, DataType::Varchar { .. }) => true,

            // BIT types with different lengths can coerce
            (DataType::Bit { .. }, DataType::Bit { .. }) => true,

            // BIT can coerce to/from integer types (numeric interpretation)
            (
                DataType::Bit { .. },
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
            ) => true,
            (
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
                DataType::Bit { .. },
            ) => true,

            // BIT can coerce to/from binary types
            (DataType::Bit { .. }, DataType::BinaryLargeObject) => true,
            (DataType::BinaryLargeObject, DataType::Bit { .. }) => true,

            // Numeric types can coerce among themselves
            (DataType::Smallint, DataType::Integer | DataType::Bigint | DataType::Unsigned) => true,
            (DataType::Integer, DataType::Bigint | DataType::Unsigned) => true,
            (DataType::Bigint, DataType::Unsigned) => true,
            (DataType::Unsigned, DataType::Bigint) => true,
            (
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
                DataType::Decimal { .. } | DataType::Numeric { .. },
            ) => true,
            (
                DataType::Decimal { .. } | DataType::Numeric { .. },
                DataType::Real | DataType::Float { .. } | DataType::DoublePrecision,
            ) => true,
            (
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
                DataType::Real | DataType::Float { .. } | DataType::DoublePrecision,
            ) => true,

            // Numeric types are bidirectionally coercible (widening and narrowing allowed)
            (DataType::Bigint | DataType::Unsigned, DataType::Integer | DataType::Smallint) => true,
            (
                DataType::Real | DataType::Float { .. } | DataType::DoublePrecision,
                DataType::Decimal { .. } | DataType::Numeric { .. },
            ) => true,
            (
                DataType::Real | DataType::Float { .. } | DataType::DoublePrecision,
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
            ) => true,
            (
                DataType::Decimal { .. } | DataType::Numeric { .. },
                DataType::Integer | DataType::Bigint | DataType::Unsigned | DataType::Smallint,
            ) => true,

            // Character string types can coerce among themselves
            (
                DataType::Character { .. },
                DataType::Varchar { .. } | DataType::Name | DataType::CharacterLargeObject,
            ) => true,
            (
                DataType::Varchar { .. },
                DataType::Character { .. } | DataType::Name | DataType::CharacterLargeObject,
            ) => true,
            (
                DataType::Name,
                DataType::Character { .. }
                | DataType::Varchar { .. }
                | DataType::CharacterLargeObject,
            ) => true,
            (
                DataType::CharacterLargeObject,
                DataType::Character { .. } | DataType::Varchar { .. } | DataType::Name,
            ) => true,

            // Temporal types can coerce among themselves
            (DataType::Date, DataType::Timestamp { .. }) => true,
            (DataType::Time { .. }, DataType::Timestamp { .. }) => true,
            (DataType::Timestamp { .. }, DataType::Date | DataType::Time { .. }) => true,

            // Intervals with different fields can coerce if compatible
            (DataType::Interval { .. }, DataType::Interval { .. }) => true,

            // Vectors with matching dimensions can coerce
            (DataType::Vector { dimensions: d1 }, DataType::Vector { dimensions: d2 }) => d1 == d2,

            // User-defined types only coerce to themselves (checked above with ==)
            // All other combinations cannot coerce
            _ => false,
        }
    }

    /// Coerces two types to their common type according to SQL:1999 rules
    ///
    /// Returns the result type that should be used when combining values
    /// of the two input types, or None if no implicit coercion exists.
    ///
    /// Based on SQL:1999 Section 9.5 (Result of data type combinations).
    pub fn coerce_to_common(&self, other: &DataType) -> Option<DataType> {
        // NULL coerces to the other type
        if matches!(self, DataType::Null) {
            return Some(other.clone());
        }
        if matches!(other, DataType::Null) {
            return Some(self.clone());
        }

        // Check if coercion is possible
        if !self.can_implicitly_coerce(other) {
            return None;
        }

        // Same types return themselves
        if self == other {
            return Some(self.clone());
        }

        // Special handling for types with precision/scale parameters
        match (self, other) {
            // For DECIMAL/NUMERIC, combine precision and scale appropriately
            (
                DataType::Decimal { precision: p1, scale: s1 },
                DataType::Decimal { precision: p2, scale: s2 },
            ) => {
                let max_scale = (*s1).max(*s2);
                let max_precision = (*p1).max(*p2);
                Some(DataType::Decimal { precision: max_precision, scale: max_scale })
            }
            (
                DataType::Numeric { precision: p1, scale: s1 },
                DataType::Numeric { precision: p2, scale: s2 },
            ) => {
                let max_scale = (*s1).max(*s2);
                let max_precision = (*p1).max(*p2);
                Some(DataType::Numeric { precision: max_precision, scale: max_scale })
            }

            // For VARCHAR, use the larger length (or None for unlimited)
            (DataType::Varchar { max_length: l1 }, DataType::Varchar { max_length: l2 }) => {
                let max_length = match (l1, l2) {
                    (None, _) | (_, None) => None,
                    (Some(a), Some(b)) => Some((*a).max(*b)),
                };
                Some(DataType::Varchar { max_length })
            }

            // For BIT, use the larger length (or None for unlimited)
            (DataType::Bit { length: l1 }, DataType::Bit { length: l2 }) => {
                let max_length = match (l1, l2) {
                    (None, _) | (_, None) => None,
                    (Some(a), Some(b)) => Some((*a).max(*b)),
                };
                Some(DataType::Bit { length: max_length })
            }

            // Vectors with matching dimensions coerce to themselves
            (DataType::Vector { dimensions: d1 }, DataType::Vector { dimensions: d2 })
                if d1 == d2 =>
            {
                Some(self.clone())
            }

            // For all other cases, choose the type with higher precedence
            _ => {
                let result = if self.type_precedence() > other.type_precedence() {
                    self.clone()
                } else {
                    other.clone()
                };
                Some(result)
            }
        }
    }

    /// Check if this type is compatible with another type for operations
    ///
    /// This now uses SQL:1999 type coercion rules. Types are compatible if
    /// they can be implicitly coerced to a common type.
    pub fn is_compatible_with(&self, other: &DataType) -> bool {
        self.coerce_to_common(other).is_some()
    }

    /// Returns an estimated size in bytes for values of this type.
    ///
    /// This is used for adaptive morsel sizing to maintain cache efficiency.
    /// Estimates are conservative (may overestimate) to ensure cache fitting.
    ///
    /// Returns the estimated size in bytes for a single value of this type,
    /// including any overhead from the SqlValue enum representation.
    pub fn estimated_size_bytes(&self) -> usize {
        // Base overhead for SqlValue enum discriminant + alignment
        const ENUM_OVERHEAD: usize = 8;

        let value_size = match self {
            // Fixed-size numeric types
            DataType::Smallint => 2,
            DataType::Integer => 4,
            DataType::Bigint | DataType::Unsigned => 8,
            DataType::Real => 4,
            DataType::Float { .. } | DataType::DoublePrecision => 8,

            // Decimal/Numeric - stored as rust_decimal::Decimal (16 bytes)
            DataType::Decimal { .. } | DataType::Numeric { .. } => 16,

            // Boolean
            DataType::Boolean => 1,

            // Character types - estimate based on length, with ArcStr overhead
            DataType::Character { length } => {
                const ARCSTR_OVERHEAD: usize = 16; // Arc pointer + length
                ARCSTR_OVERHEAD + length
            }
            DataType::Varchar { max_length } => {
                const ARCSTR_OVERHEAD: usize = 16;
                // Use max_length if specified, otherwise assume 50 bytes average
                ARCSTR_OVERHEAD + max_length.unwrap_or(50)
            }
            DataType::Name => {
                const ARCSTR_OVERHEAD: usize = 16;
                ARCSTR_OVERHEAD + 128 // NAME is VARCHAR(128)
            }
            DataType::CharacterLargeObject => {
                const ARCSTR_OVERHEAD: usize = 16;
                ARCSTR_OVERHEAD + 1000 // Conservative estimate for CLOB
            }

            // Date/time types
            DataType::Date => 4,             // i32 for days
            DataType::Time { .. } => 8,      // i64 for nanoseconds
            DataType::Timestamp { .. } => 8, // i64 for timestamp
            DataType::Interval { .. } => 16, // IntervalValue struct

            // Binary types
            DataType::BinaryLargeObject => 1000, // Conservative estimate
            DataType::Bit { length } => {
                // Bits stored as bytes, rounded up
                length.unwrap_or(1).div_ceil(8)
            }

            // Vector types
            DataType::Vector { dimensions } => {
                const VEC_OVERHEAD: usize = 24; // Vec header
                VEC_OVERHEAD + (*dimensions as usize * 4) // f32 per dimension
            }

            // Special types
            DataType::Null => 0,
            DataType::UserDefined { .. } => 100, // Conservative estimate
        };

        ENUM_OVERHEAD + value_size
    }

    /// Returns the SQLite type affinity for this data type.
    ///
    /// SQLite determines affinity based on the declared type name:
    /// 1. If the type contains "INT" → INTEGER affinity
    /// 2. If the type contains "CHAR", "CLOB", or "TEXT" → TEXT affinity
    /// 3. If the type contains "BLOB" or has no type → BLOB/NONE affinity
    /// 4. If the type contains "REAL", "FLOA", or "DOUB" → REAL affinity
    /// 5. Otherwise → NUMERIC affinity
    ///
    /// See: https://www.sqlite.org/datatype3.html#type_affinity
    pub fn sqlite_affinity(&self) -> TypeAffinity {
        match self {
            // Integer types → INTEGER affinity
            DataType::Integer | DataType::Smallint | DataType::Bigint | DataType::Unsigned => {
                TypeAffinity::Integer
            }

            // Character/Text types → TEXT affinity
            DataType::Character { .. }
            | DataType::Varchar { .. }
            | DataType::CharacterLargeObject
            | DataType::Name => TypeAffinity::Text,

            // Floating-point types → REAL affinity
            DataType::Real | DataType::Float { .. } | DataType::DoublePrecision => {
                TypeAffinity::Real
            }

            // Decimal/Numeric → NUMERIC affinity
            DataType::Decimal { .. } | DataType::Numeric { .. } => TypeAffinity::Numeric,

            // Binary types → NONE/BLOB affinity
            DataType::BinaryLargeObject | DataType::Bit { .. } => TypeAffinity::None,

            // Boolean → NUMERIC affinity (SQLite stores as 0/1)
            DataType::Boolean => TypeAffinity::Numeric,

            // Temporal types → NUMERIC affinity (SQLite stores as numbers or text)
            DataType::Date | DataType::Time { .. } | DataType::Timestamp { .. } => {
                TypeAffinity::Numeric
            }

            // Interval → NUMERIC affinity
            DataType::Interval { .. } => TypeAffinity::Numeric,

            // Vector → NONE affinity (custom type)
            DataType::Vector { .. } => TypeAffinity::None,

            // NULL → NONE affinity
            DataType::Null => TypeAffinity::None,

            // User-defined types: Apply SQLite's affinity rules based on type name.
            // SQLite determines affinity by checking if the type name contains:
            // 1. "INT" → INTEGER affinity
            // 2. "CHAR", "CLOB", or "TEXT" → TEXT affinity
            // 3. "BLOB" or no type → NONE/BLOB affinity
            // 4. "REAL", "FLOA", or "DOUB" → REAL affinity
            // 5. Otherwise → NUMERIC affinity
            //
            // This handles multi-word types like "LARGE BLOB", "NATIVE CHARACTER",
            // "VARYING CHARACTER", "UNSIGNED BIG INT", etc.
            DataType::UserDefined { type_name } => {
                let upper = type_name.to_uppercase();
                if upper.contains("INT") {
                    TypeAffinity::Integer
                } else if upper.contains("CHAR") || upper.contains("CLOB") || upper.contains("TEXT")
                {
                    TypeAffinity::Text
                } else if upper.contains("BLOB") {
                    TypeAffinity::None
                } else if upper.contains("REAL") || upper.contains("FLOA") || upper.contains("DOUB")
                {
                    TypeAffinity::Real
                } else {
                    TypeAffinity::Numeric
                }
            }
        }
    }
}