tapirus 1.2.1

Embedded multi-model database with SQL, vector search and local AI memory
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
//! Pure-Rust SQLite-compatible date and time scalar evaluation utilities.
//!
//! Uses proleptic Gregorian calendar arithmetic (`#![forbid(unsafe_code)]`,
//! `wasm32-unknown-unknown` and `edition-lite` compatible via `web_time`).

use crate::error::{Error, Result};
use crate::traits::Value;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct DateTimeParts {
    year: i32,
    month: u32,
    day: u32,
    hour: u32,
    minute: u32,
    second: u32,
    millis: u32,
}

fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

fn days_in_month(year: i32, month: u32) -> u32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => 30,
    }
}

/// Howard Hinnant civil-from-days algorithm (days since 1970-01-01).
fn civil_from_days(days: i64) -> (i32, u32, u32) {
    let z = days + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z.rem_euclid(146_097);
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
    let m = (mp + if mp < 10 { 3 } else { -9 }) as u32;
    let year = (y + i64::from(m <= 2)) as i32;
    (year, m, d)
}

/// Howard Hinnant days-from-civil algorithm (days since 1970-01-01).
fn days_from_civil(year: i32, month: u32, day: u32) -> i64 {
    let y = i64::from(year) - i64::from(month <= 2);
    let m = i64::from(month);
    let d = i64::from(day);
    let era = y.div_euclid(400);
    let yoe = y.rem_euclid(400);
    let doy = (153 * (m + if m > 2 { -3 } else { 9 }) + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + doe - 719_468
}

fn unix_ms_to_parts(total_ms: i64) -> DateTimeParts {
    let total_secs = total_ms.div_euclid(1000);
    let millis = total_ms.rem_euclid(1000) as u32;
    let days = total_secs.div_euclid(86_400);
    let rem = total_secs.rem_euclid(86_400);
    let hour = (rem / 3600) as u32;
    let minute = ((rem % 3600) / 60) as u32;
    let second = (rem % 60) as u32;
    let (year, month, day) = civil_from_days(days);
    DateTimeParts {
        year,
        month,
        day,
        hour,
        minute,
        second,
        millis,
    }
}

fn parts_to_unix_ms(p: DateTimeParts) -> i64 {
    let days = days_from_civil(p.year, p.month, p.day);
    let secs =
        days * 86_400 + i64::from(p.hour) * 3600 + i64::from(p.minute) * 60 + i64::from(p.second);
    secs * 1000 + i64::from(p.millis)
}

fn now_unix_ms() -> i64 {
    match web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH) {
        Ok(d) => (d.as_secs() as i64) * 1000 + i64::from(d.subsec_millis()),
        Err(_) => 0,
    }
}

fn parse_hms(s: &str) -> Option<(u32, u32, u32, u32)> {
    let trimmed = s.trim_end_matches('Z').trim_end_matches("+00:00").trim();
    let (hms, frac) = match trimmed.split_once('.') {
        Some((main, f)) => {
            let digits: String = f.chars().take_while(|c| c.is_ascii_digit()).collect();
            let mut padded = digits;
            while padded.len() < 3 {
                padded.push('0');
            }
            let ms = padded[..3].parse::<u32>().ok()?;
            (main, ms)
        }
        None => (trimmed, 0),
    };
    let mut parts = hms.split(':');
    let hour: u32 = parts.next()?.parse().ok()?;
    let minute: u32 = parts.next()?.parse().ok()?;
    let second: u32 = parts.next().map_or(Some(0), |sec| sec.parse().ok())?;
    if parts.next().is_some() || hour > 23 || minute > 59 || second > 59 {
        return None;
    }
    Some((hour, minute, second, frac))
}

fn parse_ymd(s: &str) -> Option<(i32, u32, u32)> {
    let mut parts = s.trim().split('-');
    let year: i32 = parts.next()?.parse().ok()?;
    let month: u32 = parts.next()?.parse().ok()?;
    let day: u32 = parts.next()?.parse().ok()?;
    if parts.next().is_some() || !(1..=12).contains(&month) {
        return None;
    }
    if day < 1 || day > days_in_month(year, month) {
        return None;
    }
    Some((year, month, day))
}

fn parse_timestring(val: &Value, use_unixepoch: bool) -> Option<DateTimeParts> {
    if use_unixepoch {
        let secs_f = match val {
            Value::Integer(i) => *i as f64,
            Value::Real(f) => *f,
            Value::Text(s) => s.trim().parse::<f64>().ok()?,
            _ => return None,
        };
        return Some(unix_ms_to_parts((secs_f * 1000.0).round() as i64));
    }

    match val {
        Value::Null => None,
        Value::Integer(jd) => {
            let ms = ((*jd as f64 - 2_440_587.5) * 86_400_000.0).round() as i64;
            Some(unix_ms_to_parts(ms))
        }
        Value::Real(jd) => {
            let ms = ((*jd - 2_440_587.5) * 86_400_000.0).round() as i64;
            Some(unix_ms_to_parts(ms))
        }
        Value::Text(raw) => {
            let s = raw.trim();
            if s.eq_ignore_ascii_case("now") {
                return Some(unix_ms_to_parts(now_unix_ms()));
            }
            if let Some((date_part, time_part)) = s.split_once('T').or_else(|| s.split_once(' ')) {
                let (year, month, day) = parse_ymd(date_part)?;
                let (hour, minute, second, millis) = parse_hms(time_part)?;
                Some(DateTimeParts {
                    year,
                    month,
                    day,
                    hour,
                    minute,
                    second,
                    millis,
                })
            } else if s.contains(':') {
                let (hour, minute, second, millis) = parse_hms(s)?;
                Some(DateTimeParts {
                    year: 2000,
                    month: 1,
                    day: 1,
                    hour,
                    minute,
                    second,
                    millis,
                })
            } else if let Some((year, month, day)) = parse_ymd(s) {
                Some(DateTimeParts {
                    year,
                    month,
                    day,
                    hour: 0,
                    minute: 0,
                    second: 0,
                    millis: 0,
                })
            } else {
                None
            }
        }
        _ => None,
    }
}

fn shift_months(parts: DateTimeParts, delta_months: i32) -> DateTimeParts {
    let total_months = (parts.year * 12 + (parts.month as i32 - 1)) + delta_months;
    let new_year = total_months.div_euclid(12);
    let new_month = (total_months.rem_euclid(12) + 1) as u32;
    let max_d = days_in_month(new_year, new_month);
    DateTimeParts {
        year: new_year,
        month: new_month,
        day: parts.day.min(max_d),
        ..parts
    }
}

fn apply_modifier(mut parts: DateTimeParts, modifier: &str) -> Result<DateTimeParts> {
    let m = modifier.trim().to_ascii_lowercase();
    if m.is_empty() || m == "unixepoch" || m == "utc" {
        return Ok(parts);
    }
    match m.as_str() {
        "start of day" => {
            parts.hour = 0;
            parts.minute = 0;
            parts.second = 0;
            parts.millis = 0;
            return Ok(parts);
        }
        "start of month" => {
            parts.day = 1;
            parts.hour = 0;
            parts.minute = 0;
            parts.second = 0;
            parts.millis = 0;
            return Ok(parts);
        }
        "start of year" => {
            parts.month = 1;
            parts.day = 1;
            parts.hour = 0;
            parts.minute = 0;
            parts.second = 0;
            parts.millis = 0;
            return Ok(parts);
        }
        _ => {}
    }

    let mut tokens = m.split_whitespace();
    if let (Some(num_str), Some(unit)) = (tokens.next(), tokens.next())
        && tokens.next().is_none()
        && let Ok(val) = num_str.parse::<f64>()
    {
        match unit {
            "second" | "seconds" => {
                let ms = parts_to_unix_ms(parts) + (val * 1000.0).round() as i64;
                return Ok(unix_ms_to_parts(ms));
            }
            "minute" | "minutes" => {
                let ms = parts_to_unix_ms(parts) + (val * 60_000.0).round() as i64;
                return Ok(unix_ms_to_parts(ms));
            }
            "hour" | "hours" => {
                let ms = parts_to_unix_ms(parts) + (val * 3_600_000.0).round() as i64;
                return Ok(unix_ms_to_parts(ms));
            }
            "day" | "days" => {
                let ms = parts_to_unix_ms(parts) + (val * 86_400_000.0).round() as i64;
                return Ok(unix_ms_to_parts(ms));
            }
            "month" | "months" => {
                return Ok(shift_months(parts, val.trunc() as i32));
            }
            "year" | "years" => {
                return Ok(shift_months(parts, (val.trunc() as i32) * 12));
            }
            _ => {}
        }
    }

    Err(Error::SqlSyntax(format!(
        "Unsupported datetime modifier '{modifier}'"
    )))
}

fn resolve_datetime_args(args: &[Value]) -> Result<Option<DateTimeParts>> {
    let Some(first) = args.first() else {
        return Ok(Some(unix_ms_to_parts(now_unix_ms())));
    };
    if first.is_null() {
        return Ok(None);
    }
    let modifiers = &args[1..];
    let has_unixepoch = modifiers.iter().any(|m| match m {
        Value::Text(s) => s.trim().eq_ignore_ascii_case("unixepoch"),
        _ => false,
    });
    let Some(mut parts) = parse_timestring(first, has_unixepoch) else {
        return Ok(None);
    };
    for m in modifiers {
        match m {
            Value::Null => return Ok(None),
            Value::Text(s) => parts = apply_modifier(parts, s)?,
            other => parts = apply_modifier(parts, &other.to_string())?,
        }
    }
    Ok(Some(parts))
}

/// Evaluate SQLite `DATE(timestring, [modifier...])` -> `'YYYY-MM-DD'`.
pub(crate) fn eval_date(args: &[Value]) -> Result<Value> {
    let Some(p) = resolve_datetime_args(args)? else {
        return Ok(Value::Null);
    };
    Ok(Value::Text(format!(
        "{:04}-{:02}-{:02}",
        p.year, p.month, p.day
    )))
}

/// Evaluate SQLite `TIME(timestring, [modifier...])` -> `'HH:MM:SS'`.
pub(crate) fn eval_time(args: &[Value]) -> Result<Value> {
    let Some(p) = resolve_datetime_args(args)? else {
        return Ok(Value::Null);
    };
    Ok(Value::Text(format!(
        "{:02}:{:02}:{:02}",
        p.hour, p.minute, p.second
    )))
}

/// Evaluate SQLite `DATETIME(timestring, [modifier...])` -> `'YYYY-MM-DD HH:MM:SS'`.
pub(crate) fn eval_datetime(args: &[Value]) -> Result<Value> {
    let Some(p) = resolve_datetime_args(args)? else {
        return Ok(Value::Null);
    };
    Ok(Value::Text(format!(
        "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
        p.year, p.month, p.day, p.hour, p.minute, p.second
    )))
}

/// Evaluate SQLite `UNIXEPOCH(timestring, [modifier...])` -> integer seconds.
pub(crate) fn eval_unixepoch(args: &[Value]) -> Result<Value> {
    let Some(p) = resolve_datetime_args(args)? else {
        return Ok(Value::Null);
    };
    Ok(Value::Integer(parts_to_unix_ms(p).div_euclid(1000)))
}

/// Evaluate SQLite `JULIANDAY(timestring, [modifier...])` -> float Julian day.
pub(crate) fn eval_julianday(args: &[Value]) -> Result<Value> {
    let Some(p) = resolve_datetime_args(args)? else {
        return Ok(Value::Null);
    };
    let ms = parts_to_unix_ms(p) as f64;
    Ok(Value::Real(ms / 86_400_000.0 + 2_440_587.5))
}

/// Evaluate SQLite `STRFTIME(format, timestring, [modifier...])`.
pub(crate) fn eval_strftime(args: &[Value]) -> Result<Value> {
    let Some(fmt_val) = args.first() else {
        return Ok(Value::Null);
    };
    if fmt_val.is_null() {
        return Ok(Value::Null);
    }
    let fmt = match fmt_val {
        Value::Text(s) => s.clone(),
        other => other.to_string(),
    };
    let Some(p) = resolve_datetime_args(&args[1..])? else {
        return Ok(Value::Null);
    };

    let days_since_epoch = days_from_civil(p.year, p.month, p.day);
    // 1970-01-01 was Thursday (4); Sunday=0..Saturday=6
    let weekday = (days_since_epoch + 4).rem_euclid(7) as u32;
    let jan1_days = days_from_civil(p.year, 1, 1);
    let day_of_year = (days_since_epoch - jan1_days + 1) as u32;
    let week_sunday_first = (day_of_year - 1 + ((7 - weekday) % 7)) / 7;
    let unix_secs = parts_to_unix_ms(p).div_euclid(1000);

    let mut out = String::with_capacity(fmt.len() + 16);
    let mut chars = fmt.chars();
    while let Some(ch) = chars.next() {
        if ch == '%' {
            match chars.next() {
                Some('Y') => out.push_str(&format!("{:04}", p.year)),
                Some('m') => out.push_str(&format!("{:02}", p.month)),
                Some('d') => out.push_str(&format!("{:02}", p.day)),
                Some('H') => out.push_str(&format!("{:02}", p.hour)),
                Some('M') => out.push_str(&format!("{:02}", p.minute)),
                Some('S') => out.push_str(&format!("{:02}", p.second)),
                Some('f') => out.push_str(&format!("{:02}.{:03}", p.second, p.millis)),
                Some('w') => out.push_str(&weekday.to_string()),
                Some('W') => out.push_str(&format!("{week_sunday_first:02}")),
                Some('j') => out.push_str(&format!("{day_of_year:03}")),
                Some('s') => out.push_str(&unix_secs.to_string()),
                Some('%') => out.push('%'),
                Some(other) => {
                    out.push('%');
                    out.push(other);
                }
                None => out.push('%'),
            }
        } else {
            out.push(ch);
        }
    }
    Ok(Value::Text(out))
}

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

    #[test]
    fn test_date_time_strftime_and_modifiers() {
        assert_eq!(
            eval_date(&[Value::Text("2026-11-16 14:35:09".into())]).unwrap(),
            Value::Text("2026-11-16".into())
        );
        assert_eq!(
            eval_time(&[Value::Text("2026-11-16 14:35:09".into())]).unwrap(),
            Value::Text("14:35:09".into())
        );
        assert_eq!(
            eval_datetime(&[
                Value::Text("2026-01-31 10:00:00".into()),
                Value::Text("+1 month".into()),
                Value::Text("+2 hours".into()),
            ])
            .unwrap(),
            Value::Text("2026-02-28 12:00:00".into())
        );
        assert_eq!(
            eval_strftime(&[
                Value::Text("%Y/%m/%d %H:%M".into()),
                Value::Integer(1_700_000_000),
                Value::Text("unixepoch".into()),
            ])
            .unwrap(),
            Value::Text("2023/11/14 22:13".into())
        );
        assert_eq!(
            eval_unixepoch(&[Value::Text("2026-11-16 00:00:00".into())]).unwrap(),
            Value::Integer(1_794_787_200)
        );
    }
}