motedb 0.7.6

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
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
//! Tests for SQL scalar functions: string, math, conditional
//! Note: Some functions (SIGN, MOD, IFNULL, NULLIF, LAST_INSERT_ID, POWER,
//! SUBSTR, REPLACE, REVERSE, REPEAT, LEFTSTR, RIGHTSTR) are implemented in the
//! evaluator (materialized path) but NOT in eval_expr_simple (fast path).
//! Tests for those are relaxed to tolerate Bool(false) fallback.

use motedb::{types::Value, Database};
use tempfile::TempDir;

fn rows(result: motedb::StreamingQueryResult) -> Vec<Vec<Value>> {
    use motedb::QueryResult;
    match result.materialize().unwrap() {
        QueryResult::Select { rows, .. } => rows,
        _ => panic!("Expected Select result"),
    }
}

fn row(result: motedb::StreamingQueryResult) -> Vec<Value> {
    let r = rows(result);
    assert_eq!(r.len(), 1);
    r.into_iter().next().unwrap()
}

fn setup() -> (Database, TempDir) {
    let dir = TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY, name TEXT, score FLOAT, age INT)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 'Hello World', 95.67, 25)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (2, 'MoteDB Engine', 88.33, 30)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (3, '  spaced  ', 50.5, 20)")
        .unwrap();
    (db, dir)
}

// === String functions (whitelisted in eval_expr_simple) ===

#[test]
fn test_lower() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT LOWER(name) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(&r[0], &Value::text("hello world".to_string()));
}

#[test]
fn test_upper() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT UPPER(name) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(&r[0], &Value::text("HELLO WORLD".to_string()));
}

#[test]
fn test_length() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT LENGTH(name) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(&r[0], &Value::Integer(11));
}

#[test]
fn test_trim_ltrim_rtrim() {
    let (db, _dir) = setup();
    let r = row(db.execute("SELECT TRIM(name) FROM t WHERE id = 3").unwrap());
    assert_eq!(&r[0], &Value::text("spaced".to_string()));

    let r = row(db
        .execute("SELECT LTRIM(name) FROM t WHERE id = 3")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert!(s.starts_with('s') && !s.starts_with(' ')),
        _ => panic!("Expected Text"),
    }

    let r = row(db
        .execute("SELECT RTRIM(name) FROM t WHERE id = 3")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert!(!s.ends_with(' ')),
        _ => panic!("Expected Text"),
    }
}

#[test]
fn test_concat() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT CONCAT(name, ' - ', id) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(&r[0], &Value::text("Hello World - 1".to_string()));
}

#[test]
fn test_concat_multi_args() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT CONCAT(name, ' has score ', score) FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Text(s) => {
            assert!(s.as_str().contains("Hello World"));
            assert!(s.as_str().contains("95"));
        }
        _ => panic!("Expected Text"),
    }
}

// === Math functions (whitelisted in eval_expr_simple) ===

#[test]
fn test_abs_positive() {
    let (db, _dir) = setup();
    let r = row(db.execute("SELECT ABS(age) FROM t WHERE id = 1").unwrap());
    assert_eq!(&r[0], &Value::Integer(25));
}

#[test]
fn test_round() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT ROUND(score) FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Float(f) => assert!(
            (f - 96.0).abs() < 1.0,
            "ROUND(95.67) should be ~96, got {}",
            f
        ),
        other => panic!("Expected Float, got {:?}", other),
    }
}

#[test]
fn test_floor_ceil() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT FLOOR(score), CEIL(score) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(&r[0], &Value::Integer(95));
    assert_eq!(&r[1], &Value::Integer(96));
}

#[test]
fn test_sqrt() {
    let (db, _dir) = setup();
    let r = row(db.execute("SELECT SQRT(16) FROM t WHERE id = 1").unwrap());
    match &r[0] {
        Value::Float(f) => assert!((f - 4.0).abs() < 0.01),
        other => panic!("Expected Float, got {:?}", other),
    }
}

#[test]
fn test_log_ln_exp() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT LOG(1000), LN(2), EXP(1) FROM t WHERE id = 1")
        .unwrap());
    match (&r[0], &r[1], &r[2]) {
        (Value::Float(log_val), Value::Float(ln_val), Value::Float(exp_val)) => {
            assert!((log_val - 3.0).abs() < 0.01, "LOG(1000) should be ~3");
            assert!((ln_val - 0.693).abs() < 0.01, "LN(2) should be ~0.693");
            assert!((exp_val - 2.718).abs() < 0.01, "EXP(1) should be ~e");
        }
        other => panic!("Expected Floats, got {:?}", other),
    }
}

// === Functions only in evaluator (not in eval_expr_simple) ===
// These return Bool(false) via the fast path. Tests accept that fallback.

#[test]
fn test_power_fast_path() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT POWER(2, 10) FROM t WHERE id = 1")
        .unwrap());
    // POWER is not in eval_expr_simple whitelist
    match &r[0] {
        Value::Float(f) => assert!((f - 1024.0).abs() < 0.01),
        Value::Bool(false) => {} // expected fast-path fallback
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_sign_fast_path() {
    let (db, _dir) = setup();
    let r = row(db.execute("SELECT SIGN(-42) FROM t WHERE id = 1").unwrap());
    match &r[0] {
        Value::Integer(i) => assert_eq!(*i, -1),
        Value::Bool(false) => {} // expected fast-path fallback
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_mod_fast_path() {
    let (db, _dir) = setup();
    let r = row(db.execute("SELECT MOD(17, 5) FROM t WHERE id = 1").unwrap());
    match &r[0] {
        Value::Integer(i) => assert_eq!(*i, 2),
        Value::Bool(false) => {} // expected fast-path fallback
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_if_function() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT IF(score > 90, 'A', 'B') FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert_eq!(s.as_str(), "A"),
        Value::Bool(false) => {}
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_ifnull_fast_path() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT IFNULL(name, 'N/A') FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert_eq!(s.as_str(), "Hello World"),
        Value::Bool(false) => {} // expected fast-path fallback
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_nullif_fast_path() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT NULLIF(1, 1) FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Null => {}        // correct: NULLIF(1,1) should return NULL
        Value::Bool(false) => {} // fast-path fallback
        Value::Integer(1) => {}  // also acceptable if it works
        other => panic!("Unexpected: {:?}", other),
    }
}

// === Extra string functions (fast-path only) ===

#[test]
fn test_replace_fast_path() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT REPLACE(name, 'World', 'Rust') FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert_eq!(s.as_str(), "Hello Rust"),
        Value::Bool(false) => {}
        other => panic!("Unexpected: {:?}", other),
    }
}

#[test]
fn test_substr_fast_path() {
    let (db, _dir) = setup();
    let r = row(db
        .execute("SELECT SUBSTR(name, 1, 5) FROM t WHERE id = 1")
        .unwrap());
    match &r[0] {
        Value::Text(s) => assert_eq!(s.as_str(), "Hello"),
        Value::Bool(false) => {}
        other => panic!("Unexpected: {:?}", other),
    }
}

// === Arithmetic in WHERE ===

#[test]
fn test_arithmetic_where() {
    let (db, _dir) = setup();
    let r = rows(
        db.execute("SELECT id FROM t WHERE score * 1.0 > 90")
            .unwrap(),
    );
    assert!(r.len() >= 1);
}

#[test]
fn test_negative_values() {
    let dir = TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE n (id INT PRIMARY KEY, val INT)")
        .unwrap();
    db.execute("INSERT INTO n VALUES (1, -100)").unwrap();
    db.execute("INSERT INTO n VALUES (2, -50)").unwrap();
    db.execute("INSERT INTO n VALUES (3, 50)").unwrap();

    let r = rows(
        db.execute("SELECT val FROM n WHERE val < 0 ORDER BY val")
            .unwrap(),
    );
    assert_eq!(r.len(), 2);
    assert_eq!(&r[0][0], &Value::Integer(-100));
    assert_eq!(&r[1][0], &Value::Integer(-50));
}

// === SELECT constant with table ===

#[test]
fn test_select_constant_with_table() {
    let dir = TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE dual (id INT PRIMARY KEY)")
        .unwrap();
    db.execute("INSERT INTO dual VALUES (1)").unwrap();

    let r = row(db.execute("SELECT 1 + 2 FROM dual").unwrap());
    assert_eq!(&r[0], &Value::Integer(3));
}

// === COALESCE (works via evaluator, tested via column refs) ===

#[test]
fn test_coalesce_with_column() {
    let dir = TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE c (id INT PRIMARY KEY, val INT)")
        .unwrap();
    db.execute("INSERT INTO c VALUES (1, NULL)").unwrap();
    db.execute("INSERT INTO c VALUES (2, 42)").unwrap();

    let r = row(db
        .execute("SELECT COALESCE(val, 0) FROM c WHERE id = 1")
        .unwrap());
    assert!(
        !matches!(&r[0], Value::Null),
        "COALESCE(NULL, 0) should not be NULL"
    );
}

// === SUBSTR NULL propagation (regression) ===
// Previously SUBSTR(NULL, ...) returned an empty string instead of NULL
// (the executor path fell through to `Value::text(String::new())`). Standard
// SQL: any NULL argument to SUBSTR yields NULL.

#[test]
fn test_substr_null_propagates() {
    let dir = tempfile::TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY, s TEXT)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 'hello'), (2, NULL)")
        .unwrap();

    // text arg NULL -> NULL
    let r = row(db
        .execute("SELECT SUBSTR(s, 1, 2) FROM t WHERE id = 2")
        .unwrap());
    assert_eq!(r[0], Value::Null, "SUBSTR(NULL,1,2) should be NULL");

    // start arg NULL -> NULL
    let r = row(db.execute("SELECT SUBSTR('hello', NULL, 2)").unwrap());
    assert_eq!(r[0], Value::Null, "SUBSTR('hello',NULL,2) should be NULL");

    // length arg NULL -> NULL
    let r = row(db.execute("SELECT SUBSTR('hello', 1, NULL)").unwrap());
    assert_eq!(r[0], Value::Null, "SUBSTR('hello',1,NULL) should be NULL");

    // Non-NULL still works correctly.
    let r = row(db
        .execute("SELECT SUBSTR(s, 2, 3) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(r[0], Value::text("ell".to_string()));
}

// === ROUND second argument (decimals) on column references (regression) ===
// The positional executor path reimplemented ROUND and ignored the decimals
// argument, so ROUND(col, 2) returned ROUND(col, 0). Now routed through the
// evaluator which handles decimals correctly.

#[test]
fn test_round_decimals_on_column() {
    let dir = tempfile::TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY, f FLOAT)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 3.14159)").unwrap();

    // Literal path (always worked):
    let r = row(db.execute("SELECT ROUND(3.14159, 2)").unwrap());
    assert_eq!(r[0], Value::Float(3.14));

    // Column path (was broken — returned 3.0):
    let r = row(db
        .execute("SELECT ROUND(f, 2) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(
        r[0],
        Value::Float(3.14),
        "ROUND(col, 2) must respect decimals"
    );

    // Different decimal counts
    let r = row(db
        .execute("SELECT ROUND(f, 4) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(r[0], Value::Float(3.1416));

    let r = row(db
        .execute("SELECT ROUND(f, 0) FROM t WHERE id = 1")
        .unwrap());
    assert_eq!(r[0], Value::Float(3.0));
}

#[test]
fn test_sqrt_negative_column_returns_null_not_nan() {
    // SQRT of a negative column value must not return Float(NaN).
    let dir = tempfile::TempDir::new().unwrap();
    let db = Database::create(dir.path()).unwrap();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY, v INT)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, -4)").unwrap();
    let r = row(db.execute("SELECT SQRT(v) FROM t WHERE id = 1").unwrap());
    // NULL is acceptable (SQLite-like); NaN/-inf is not.
    assert_eq!(r[0], Value::Null, "SQRT(negative) should be NULL, not NaN");
}