motedb 0.8.2

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
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
//! v0.5.0 regression & feature tests.
//!
//! Covers the bugs fixed in v0.5.0 and the performance features added:
//! - Integer columns read correctly (not reinterpreted as Float)
//! - UPDATE produces a single newest version (no duplicate rows)
//! - SELECT * routes through the ColSegmentStore path for non-WHERE queries
//! - UPDATE value survives checkpoint + reopen (segment dedup)
//! - DELETE then COUNT(*) / SELECT sees the deletion
//! - ORDER BY col LIMIT K returns correct top-K + preserves other rows
//! - SELECT DISTINCT returns unique values
//! - bulk_load multi-page column index (300+ entries) round-trips correctly
//! - secondary column index point-lookup for high-selectivity WHERE

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

fn make_db() -> (TempDir, Database) {
    let dir = TempDir::new().unwrap();
    let mut config = DBConfig::for_edge();
    config.max_result_rows = None;
    let db = Database::create_with_config(dir.path(), config).unwrap();
    (dir, db)
}

fn select_rows(db: &Database, sql: &str) -> Vec<Vec<Value>> {
    match db.execute(sql).unwrap().materialize().unwrap() {
        QueryResult::Select { rows, .. } => rows,
        other => panic!("expected Select, got {:?}", std::mem::discriminant(&other)),
    }
}

fn count(db: &Database, table: &str) -> i64 {
    let rows = select_rows(db, &format!("SELECT COUNT(*) FROM {}", table));
    match rows.first().and_then(|r| r.first()) {
        Some(Value::Integer(n)) => *n,
        other => panic!("COUNT returned {:?}", other),
    }
}

// ═══════════════════════════════════════════════════════════════════════
// Integer/Float type correctness (was: Integer read as Float)
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_integer_columns_not_read_as_float() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, age INTEGER, score INTEGER)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 30, 95)").unwrap();

    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 1);
    // All three columns are INTEGER — none should decode as Float.
    assert_eq!(rows[0][0], Value::Integer(1), "id should be Integer");
    assert_eq!(rows[0][1], Value::Integer(30), "age should be Integer");
    assert_eq!(rows[0][2], Value::Integer(95), "score should be Integer");
}

#[test]
fn test_mixed_integer_and_float_columns() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, ival INTEGER, fval FLOAT)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 42, 3.14)").unwrap();

    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows[0][0], Value::Integer(1));
    assert_eq!(
        rows[0][1],
        Value::Integer(42),
        "integer column must stay Integer"
    );
    assert!(
        matches!(rows[0][2], Value::Float(_)),
        "float column must be Float"
    );
}

// ═══════════════════════════════════════════════════════════════════════
// UPDATE correctness (was: duplicate rows after UPDATE)
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_update_no_duplicate_rows() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 100)").unwrap();
    db.execute("UPDATE t SET v = 200 WHERE id = 1").unwrap();

    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 1, "UPDATE must not duplicate the row");
    assert_eq!(rows[0][1], Value::Integer(200), "must see the new value");
}

#[test]
fn test_update_preserves_other_rows() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    for i in 1..=5 {
        db.execute(&format!("INSERT INTO t VALUES ({}, {})", i, i * 10))
            .unwrap();
    }
    db.execute("UPDATE t SET v = 999 WHERE id = 3").unwrap();

    let rows = select_rows(&db, "SELECT * FROM t ORDER BY id");
    assert_eq!(rows.len(), 5, "all 5 rows must survive");
    // id=3 updated, others unchanged
    assert_eq!(
        rows[2].iter().find(|v| matches!(v, Value::Integer(999))),
        Some(&Value::Integer(999))
    );
    assert_eq!(count(&db, "t"), 5);
}

#[test]
fn test_repeated_updates_same_row() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    db.execute("INSERT INTO t VALUES (1, 1)").unwrap();
    for new_v in 2..=20 {
        db.execute(&format!("UPDATE t SET v = {} WHERE id = 1", new_v))
            .unwrap();
    }
    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 1, "20 updates must not create duplicates");
    assert_eq!(
        rows[0][1],
        Value::Integer(20),
        "must reflect the last update"
    );
}

// ═══════════════════════════════════════════════════════════════════════
// Durability: UPDATE/DELETE survive checkpoint + reopen
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_update_survives_restart() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().to_path_buf();
    {
        let db = Database::create(&path).unwrap();
        db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
            .unwrap();
        db.execute("INSERT INTO t VALUES (1, 100)").unwrap();
        db.execute("UPDATE t SET v = 200 WHERE id = 1").unwrap();
        db.checkpoint().unwrap();
        db.close().unwrap();
    }
    let db = Database::open(&path).unwrap();
    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 1);
    assert_eq!(
        rows[0][1],
        Value::Integer(200),
        "updated value must survive restart"
    );
}

#[test]
fn test_delete_survives_restart() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().to_path_buf();
    {
        let db = Database::create(&path).unwrap();
        db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
            .unwrap();
        db.execute("INSERT INTO t VALUES (1, 10)").unwrap();
        db.execute("INSERT INTO t VALUES (2, 20)").unwrap();
        db.execute("DELETE FROM t WHERE id = 1").unwrap();
        db.checkpoint().unwrap();
        db.close().unwrap();
    }
    let db = Database::open(&path).unwrap();
    assert_eq!(
        count(&db, "t"),
        1,
        "deleted row must stay deleted after restart"
    );
    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0][0], Value::Integer(2), "remaining row must be id=2");
}

// ═══════════════════════════════════════════════════════════════════════
// DELETE then COUNT/SELECT consistency
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_delete_then_count_consistent() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    for i in 1..=10 {
        db.execute(&format!("INSERT INTO t VALUES ({}, {})", i, i))
            .unwrap();
    }
    db.execute("DELETE FROM t WHERE id = 5").unwrap();
    assert_eq!(count(&db, "t"), 9, "COUNT must reflect the deletion");
    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 9, "SELECT must reflect the deletion");
}

#[test]
fn test_delete_all_then_count() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    for i in 1..=3 {
        db.execute(&format!("INSERT INTO t VALUES ({}, {})", i, i))
            .unwrap();
    }
    for i in 1..=3 {
        db.execute(&format!("DELETE FROM t WHERE id = {}", i))
            .unwrap();
    }
    assert_eq!(count(&db, "t"), 0, "all rows deleted");
    let rows = select_rows(&db, "SELECT * FROM t");
    assert!(rows.is_empty(), "SELECT must return no rows");
}

// ═══════════════════════════════════════════════════════════════════════
// ORDER BY + LIMIT (top-K) correctness
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_order_by_limit_desc_correct() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, v FLOAT)")
        .unwrap();
    for i in 0..100 {
        db.execute(&format!("INSERT INTO t (v) VALUES ({})", i as f64 * 0.5))
            .unwrap();
    }
    let rows = select_rows(&db, "SELECT v FROM t ORDER BY v DESC LIMIT 5");
    assert_eq!(rows.len(), 5, "LIMIT 5 must return 5 rows");
    // Descending: first row has the largest value
    let first = match &rows[0][0] {
        Value::Float(f) => *f,
        _ => 0.0,
    };
    let last = match &rows[4][0] {
        Value::Float(f) => *f,
        _ => 0.0,
    };
    assert!(
        first >= last,
        "DESC order: first ({}) >= last ({})",
        first,
        last
    );
}

#[test]
fn test_order_by_limit_preserves_count() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, v FLOAT)")
        .unwrap();
    for i in 0..50 {
        db.execute(&format!("INSERT INTO t (v) VALUES ({})", i as f64))
            .unwrap();
    }
    // ORDER BY LIMIT should not affect total table state
    let _ = select_rows(&db, "SELECT * FROM t ORDER BY v DESC LIMIT 3");
    assert_eq!(count(&db, "t"), 50, "table must still have all 50 rows");
}

// ═══════════════════════════════════════════════════════════════════════
// DISTINCT correctness
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_distinct_low_cardinality() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, region TEXT)")
        .unwrap();
    for i in 0..100 {
        let region = if i % 3 == 0 { "US" } else { "EU" };
        db.execute(&format!("INSERT INTO t (region) VALUES ('{}')", region))
            .unwrap();
    }
    let rows = select_rows(&db, "SELECT DISTINCT region FROM t");
    let values: Vec<String> = rows
        .iter()
        .filter_map(|r| {
            if let Value::Text(t) = &r[0] {
                Some(t.as_str().to_string())
            } else {
                None
            }
        })
        .collect();
    assert_eq!(values.len(), 2, "should have exactly 2 distinct regions");
    assert!(values.contains(&"US".to_string()));
    assert!(values.contains(&"EU".to_string()));
}

// ═══════════════════════════════════════════════════════════════════════
// Secondary column index point-lookup (WHERE col = val)
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_indexed_where_equality_correct() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, name TEXT, region TEXT)")
        .unwrap();
    for i in 0..100 {
        let region = if i % 3 == 0 { "US" } else { "EU" };
        db.execute(&format!(
            "INSERT INTO t (name, region) VALUES ('n{}', '{}')",
            i, region
        ))
        .unwrap();
    }
    db.execute("CREATE INDEX idx_region ON t (region) USING COLUMN")
        .unwrap();

    // High-selectivity: WHERE region = 'US' matches ~34 rows
    let rows = select_rows(&db, "SELECT * FROM t WHERE region = 'US'");
    let expected = (0..100).filter(|i| i % 3 == 0).count();
    assert_eq!(
        rows.len(),
        expected,
        "indexed WHERE must return correct count"
    );
    // All returned rows must be US
    for row in &rows {
        let region = match &row[2] {
            Value::Text(t) => t.as_str().to_string(),
            _ => String::new(),
        };
        assert_eq!(region, "US", "all rows must match the filter");
    }
}

#[test]
fn test_indexed_where_returns_no_false_positives() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, cat TEXT)")
        .unwrap();
    for i in 0..30 {
        let cat = match i % 3 {
            0 => "a",
            1 => "b",
            _ => "c",
        };
        db.execute(&format!("INSERT INTO t (cat) VALUES ('{}')", cat))
            .unwrap();
    }
    db.execute("CREATE INDEX idx_cat ON t (cat) USING COLUMN")
        .unwrap();
    let rows = select_rows(&db, "SELECT * FROM t WHERE cat = 'b'");
    for row in &rows {
        assert_eq!(row[1], Value::text("b".to_string()), "no false positives");
    }
    assert_eq!(rows.len(), 10);
}

// ═══════════════════════════════════════════════════════════════════════
// bulk_load multi-page index (300+ entries round-trips correctly)
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_index_lookup_after_bulk_load_500_entries() {
    // 500 distinct values spans multiple leaf pages in the B+Tree (>PAGE_SIZE).
    // This catches the bulk_load multi-page corruption bug.
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, tag TEXT)")
        .unwrap();
    for i in 0..500 {
        db.execute(&format!("INSERT INTO t (tag) VALUES ('val{}')", i))
            .unwrap();
    }
    db.execute("CREATE INDEX idx_key ON t (tag) USING COLUMN")
        .unwrap();
    // Probe a value that lands in the 2nd+ leaf page
    let rows = select_rows(&db, "SELECT * FROM t WHERE tag = 'val450'");
    assert_eq!(rows.len(), 1, "multi-page index lookup must find the value");
    // Probe the first leaf page too
    let rows = select_rows(&db, "SELECT * FROM t WHERE tag = 'val1'");
    assert_eq!(rows.len(), 1);
    // Non-existent value
    let rows = select_rows(&db, "SELECT * FROM t WHERE tag = 'val999'");
    assert!(rows.is_empty(), "non-existent value must return empty");
}

#[test]
fn test_index_lookup_after_vacuum_compaction() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, region TEXT)")
        .unwrap();
    for i in 0..300 {
        let region = if i % 3 == 0 { "US" } else { "EU" };
        db.execute(&format!("INSERT INTO t (region) VALUES ('{}')", region))
            .unwrap();
    }
    db.execute("CREATE INDEX idx_region ON t (region) USING COLUMN")
        .unwrap();
    db.vacuum().unwrap(); // triggers compaction — was breaking point lookups
    let rows = select_rows(&db, "SELECT * FROM t WHERE region = 'US'");
    assert_eq!(rows.len(), 100, "lookup must work after vacuum/compaction");
    // PK point lookup must also work after compaction
    let rows = select_rows(&db, "SELECT * FROM t WHERE id = 150");
    assert_eq!(rows.len(), 1, "PK lookup must work after compaction");
}

// ═══════════════════════════════════════════════════════════════════════
// AUTO_INCREMENT + fast batch insert
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_auto_increment_select_all() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, v TEXT)")
        .unwrap();
    for i in 0..10 {
        db.execute(&format!("INSERT INTO t (v) VALUES ('x{}')", i))
            .unwrap();
    }
    let rows = select_rows(&db, "SELECT * FROM t");
    assert_eq!(rows.len(), 10);
    // IDs should be 1..=10
    for (i, row) in rows.iter().enumerate() {
        assert_eq!(
            row[0],
            Value::Integer((i + 1) as i64),
            "auto-increment id at {}",
            i
        );
    }
}

// ═══════════════════════════════════════════════════════════════════════
// LIKE prefix filter
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_like_prefix_filter() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, name TEXT)")
        .unwrap();
    db.execute("INSERT INTO t (name) VALUES ('apple')").unwrap();
    db.execute("INSERT INTO t (name) VALUES ('apricot')")
        .unwrap();
    db.execute("INSERT INTO t (name) VALUES ('banana')")
        .unwrap();
    let rows = select_rows(&db, "SELECT * FROM t WHERE name LIKE 'ap%'");
    assert_eq!(rows.len(), 2, "LIKE 'ap%' matches apple + apricot");
}

// ═══════════════════════════════════════════════════════════════════════
// Cross-cutting: INSERT/UPDATE/DELETE/SELECT interleave
// ═══════════════════════════════════════════════════════════════════════

#[test]
fn test_interleaved_crud_correctness() {
    let (_dir, db) = make_db();
    db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v INTEGER)")
        .unwrap();
    // Insert 5
    for i in 1..=5 {
        db.execute(&format!("INSERT INTO t VALUES ({}, {})", i, i * 10))
            .unwrap();
    }
    // Update 2 and 4
    db.execute("UPDATE t SET v = 200 WHERE id = 2").unwrap();
    db.execute("UPDATE t SET v = 400 WHERE id = 4").unwrap();
    // Delete 3
    db.execute("DELETE FROM t WHERE id = 3").unwrap();
    // Insert new
    db.execute("INSERT INTO t VALUES (6, 60)").unwrap();

    assert_eq!(count(&db, "t"), 5, "5 - 1 delete + 1 insert = 5");
    let rows = select_rows(&db, "SELECT * FROM t ORDER BY id");
    assert_eq!(rows.len(), 5);
    // Verify values: id=1→10, id=2→200, id=4→400, id=5→50, id=6→60 (id=3 deleted)
    let by_id: std::collections::HashMap<i64, i64> = rows
        .iter()
        .filter_map(|r| match (r.first(), r.get(1)) {
            (Some(Value::Integer(id)), Some(Value::Integer(v))) => Some((*id, *v)),
            _ => None,
        })
        .collect();
    assert_eq!(by_id.get(&1), Some(&10));
    assert_eq!(by_id.get(&2), Some(&200), "update id=2");
    assert!(!by_id.contains_key(&3), "id=3 deleted");
    assert_eq!(by_id.get(&4), Some(&400), "update id=4");
    assert_eq!(by_id.get(&5), Some(&50));
    assert_eq!(by_id.get(&6), Some(&60), "new insert");
}