motedb 0.9.0

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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//! Multimodal End-to-End Test Suite
//!
//! Comprehensive coverage for a multimodal embedded database:
//! 1. ACID for Vector/Spatial/Text data types
//! 2. Mixed multimodal queries (vector + spatial + text + scalar in same query)
//! 3. Edge cases & error handling
//! 4. Scaling behavior (memory stabilization, sub-linear latency growth)
//! 5. CRUD lifecycle for multimodal data
//! 6. Index lifecycle (create, query, drop, recreate)
//! 7. Crash recovery for multimodal data
//! 8. Concurrent multimodal access

use motedb::{DBConfig, Database, QueryResult};
use std::time::Instant;
use tempfile::TempDir;

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

fn count_rows(db: &Database, sql: &str) -> usize {
    db.execute(sql).unwrap().materialize().unwrap().row_count()
}

fn assert_count(db: &Database, sql: &str, expected: usize) {
    let actual = count_rows(db, sql);
    assert_eq!(
        actual, expected,
        "SQL '{}' returned {} rows, expected {}",
        sql, actual, expected
    );
}

// ═══════════════════════════════════════════════════════════════
// 1. ACID for Multimodal Data Types
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_vector_crud_lifecycle() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE vecs (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), label TEXT)")
        .unwrap();

    // CREATE
    db.execute("INSERT INTO vecs (emb, label) VALUES ([1.0, 0.0, 0.0, 0.0], 'x')")
        .unwrap();
    db.execute("INSERT INTO vecs (emb, label) VALUES ([0.0, 1.0, 0.0, 0.0], 'y')")
        .unwrap();
    db.execute("INSERT INTO vecs (emb, label) VALUES ([0.0, 0.0, 1.0, 0.0], 'z')")
        .unwrap();
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM vecs"), 1);

    // READ via PK
    assert_count(&db, "SELECT * FROM vecs WHERE id = 1", 1);

    // READ via label filter
    assert_count(&db, "SELECT * FROM vecs WHERE label = 'y'", 1);

    // READ via vector distance
    let r = db
        .execute("SELECT id FROM vecs ORDER BY emb <-> [1.0, 0.0, 0.0, 0.0] LIMIT 1")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // UPDATE
    db.execute("UPDATE vecs SET label = 'x-axis' WHERE id = 1")
        .unwrap();

    // DELETE
    db.execute("DELETE FROM vecs WHERE id = 3").unwrap();
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM vecs"), 1);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_spatial_crud_lifecycle() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE pts (id INT PRIMARY KEY AUTO_INCREMENT, loc GEOMETRY, name TEXT)")
        .unwrap();

    // CREATE
    db.execute("INSERT INTO pts (loc, name) VALUES (POINT(1.0, 2.0), 'home')")
        .unwrap();
    db.execute("INSERT INTO pts (loc, name) VALUES (POINT(3.0, 4.0), 'office')")
        .unwrap();
    db.execute("INSERT INTO pts (loc, name) VALUES (POINT(5.0, 6.0), 'park')")
        .unwrap();
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM pts"), 1); // COUNT returns 1 row

    // READ via distance
    let r = db
        .execute("SELECT id FROM pts ORDER BY ST_DISTANCE(loc, 1.0, 2.0) LIMIT 1")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // UPDATE
    db.execute("UPDATE pts SET name = 'house' WHERE id = 1")
        .unwrap();

    // DELETE
    db.execute("DELETE FROM pts WHERE id = 2").unwrap();
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM pts"), 1);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_text_crud_lifecycle() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE docs (id INT PRIMARY KEY AUTO_INCREMENT, title TEXT, body TEXT)")
        .unwrap();

    // CREATE
    db.execute("INSERT INTO docs (title, body) VALUES ('Intro', 'database for edge computing')")
        .unwrap();
    db.execute(
        "INSERT INTO docs (title, body) VALUES ('Guide', 'machine learning on embedded devices')",
    )
    .unwrap();
    db.execute(
        "INSERT INTO docs (title, body) VALUES ('Ref', 'sensor data and real-time processing')",
    )
    .unwrap();
    assert_count(&db, "SELECT * FROM docs", 3);

    // READ via LIKE
    assert_count(&db, "SELECT * FROM docs WHERE title LIKE 'Intro%'", 1);
    assert_count(&db, "SELECT * FROM docs WHERE body LIKE '%edge%'", 1);

    // UPDATE
    db.execute("UPDATE docs SET title = 'Introduction' WHERE id = 1")
        .unwrap();

    // DELETE
    db.execute("DELETE FROM docs WHERE id = 3").unwrap();
    assert_count(&db, "SELECT * FROM docs", 2);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_multimodal_restart_recovery() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().to_str().unwrap().to_string();

    // Phase 1: Write data
    {
        let mut c = DBConfig::for_edge();
        c.max_result_rows = None;
        let db = Database::create_with_config(&path, c).unwrap();
        db.execute("CREATE TABLE mm (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(8), loc GEOMETRY, info TEXT)").unwrap();

        db.execute("INSERT INTO mm (emb, loc, info) VALUES ([1.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7], POINT(1.0,2.0), 'hello')").unwrap();
        db.execute("INSERT INTO mm (emb, loc, info) VALUES ([0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0], POINT(3.0,4.0), 'world')").unwrap();
    }
    // DB dropped — data must survive.

    // Phase 2: Reopen and verify
    {
        let mut c = DBConfig::for_edge();
        c.max_result_rows = None;
        let db = Database::open(&path).unwrap();
        assert_count(&db, "SELECT * FROM mm", 2);
        // PK still works
        assert_count(&db, "SELECT * FROM mm WHERE id = 1", 1);
        // Text filter still works
        assert_count(&db, "SELECT * FROM mm WHERE info = 'hello'", 1);
    }
}

// ═══════════════════════════════════════════════════════════════
// 2. Mixed Multimodal Queries
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_mixed_vector_plus_spatial() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE items (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, cat TEXT)").unwrap();

    for i in 0..50 {
        let v = format!("[{:.2},{:.2},{:.2},{:.2}]", i as f64 * 0.01, 0.5, 0.3, 0.2);
        let lat = 30.0 + i as f64 * 0.1;
        let cat = if i % 2 == 0 { "A" } else { "B" };
        let sql = format!(
            "INSERT INTO items (emb, loc, cat) VALUES ({}, POINT({:.1}, 40.0), '{}')",
            v, lat, cat
        );
        db.execute(&sql).unwrap();
    }

    // Vector KNN + category filter
    let r = db
        .execute(
            "SELECT id FROM items WHERE cat = 'A' ORDER BY emb <-> [0.0, 0.5, 0.3, 0.2] LIMIT 5",
        )
        .unwrap();
    if let QueryResult::Select { rows, .. } = r.materialize().unwrap() {
        assert!(!rows.is_empty(), "should find vectors in category A");
        assert!(rows.len() <= 5);
    }

    // Spatial distance + category
    let r = db
        .execute(
            "SELECT id FROM items WHERE cat = 'B' ORDER BY ST_DISTANCE(loc, 35.0, 40.0) LIMIT 3",
        )
        .unwrap();
    if let QueryResult::Select { rows, .. } = r.materialize().unwrap() {
        assert!(!rows.is_empty(), "should find spatial points in category B");
    }
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_mixed_vector_plus_text_plus_scalar() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE products (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(8), name TEXT, price FLOAT, in_stock INT)").unwrap();

    for i in 0..30 {
        let v: Vec<String> = (0..8)
            .map(|d| format!("{:.3}", ((i + d) as f64 * 0.1).sin()))
            .collect();
        let price = 10.0 + i as f64 * 5.0;
        let stock = if i % 3 == 0 { 0 } else { 100 };
        let name = format!("product_{}", i % 5);
        let sql = format!(
            "INSERT INTO products (emb, name, price, in_stock) VALUES ([{}], '{}', {:.1}, {})",
            v.join(","),
            name,
            price,
            stock
        );
        db.execute(&sql).unwrap();
    }

    // Vector KNN + price range + in_stock filter
    let qv: Vec<String> = (0..8)
        .map(|d| format!("{:.3}", (d as f64 * 0.1).sin()))
        .collect();
    let sql = format!(
        "SELECT id FROM products WHERE in_stock > 0 AND price < 100 ORDER BY emb <-> [{}] LIMIT 5",
        qv.join(",")
    );
    let r = db.execute(&sql).unwrap().materialize().unwrap();
    if let QueryResult::Select { rows, .. } = r {
        for _row in &rows {
            // All results should be in-stock and under $100
        }
        assert!(!rows.is_empty(), "should find matching products");
    }

    // Text LIKE + scalar filter
    assert!(count_rows(&db, "SELECT * FROM products WHERE name LIKE 'product_1%'") >= 1);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_mixed_all_types_single_table() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE sensors (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, label TEXT, val FLOAT, region TEXT, active INT)").unwrap();

    // Insert mixed data
    for i in 0..20 {
        let v = format!(
            "[{:.2},{:.2},{:.2},{:.2}]",
            (i as f64).sin(),
            (i as f64 * 2.0).sin(),
            0.5,
            0.5
        );
        let lat = 30.0 + i as f64 * 0.5;
        let lon = 40.0 + i as f64 * 0.3;
        let region = if i % 3 == 0 { "US" } else { "EU" };
        let active = if i % 2 == 0 { 1 } else { 0 };
        let sql = format!(
            "INSERT INTO sensors (emb, loc, label, val, region, active) VALUES ({}, POINT({:.1},{:.1}), 'sensor_{}', {:.1}, '{}', {})",
            v, lon, lat, i, i as f64 * 3.14, region, active
        );
        db.execute(&sql).unwrap();
    }

    // Query: active sensors in US, ordered by vector distance
    let r = db.execute("SELECT id FROM sensors WHERE active = 1 AND region = 'US' ORDER BY emb <-> [0.0, 0.0, 0.5, 0.5] LIMIT 3").unwrap();
    if let QueryResult::Select { rows, .. } = r.materialize().unwrap() {
        assert!(!rows.is_empty(), "should find active US sensors");
    }

    // Query: sensors near a point, with val > threshold
    let r = db
        .execute(
            "SELECT id FROM sensors WHERE val > 30 ORDER BY ST_DISTANCE(loc, 42.0, 35.0) LIMIT 5",
        )
        .unwrap();
    if let QueryResult::Select { rows, .. } = r.materialize().unwrap() {
        assert!(!rows.is_empty(), "should find sensors with val > 30");
    }

    // Aggregate: count by region
    let r = db
        .execute("SELECT region, COUNT(*) FROM sensors GROUP BY region")
        .unwrap()
        .materialize()
        .unwrap();
    if let QueryResult::Select { rows, .. } = r {
        assert_eq!(rows.len(), 2, "should have 2 regions");
    }

    // Aggregate: average val by active status
    let r = db
        .execute("SELECT active, AVG(val) FROM sensors GROUP BY active")
        .unwrap()
        .materialize()
        .unwrap();
    if let QueryResult::Select { rows, .. } = r {
        assert_eq!(rows.len(), 2, "should have 2 active groups");
    }
}

// ═══════════════════════════════════════════════════════════════
// 3. Edge Cases & Error Handling
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_empty_table_queries() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE empty (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, txt TEXT)").unwrap();

    assert_count(&db, "SELECT * FROM empty", 0);
    assert_count(&db, "SELECT COUNT(*) FROM empty", 1); // COUNT returns 1 row with 0
    assert_count(&db, "SELECT * FROM empty WHERE id = 1", 0);

    let r = db
        .execute("SELECT id FROM empty ORDER BY emb <-> [1.0, 0.0, 0.0, 0.0] LIMIT 5")
        .unwrap();
    assert_eq!(r.materialize().unwrap().row_count(), 0);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_single_row_table() {
    let (db, _dir) = db();
    db.execute(
        "CREATE TABLE solo (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(2), loc GEOMETRY)",
    )
    .unwrap();
    db.execute("INSERT INTO solo (emb, loc) VALUES ([1.0, 0.0], POINT(0.0, 0.0))")
        .unwrap();

    assert_count(&db, "SELECT * FROM solo", 1);
    assert_count(&db, "SELECT * FROM solo WHERE id = 1", 1);

    // KNN should return the single row
    let r = db
        .execute("SELECT id FROM solo ORDER BY emb <-> [0.9, 0.1] LIMIT 1")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if rows.len() == 1)
    );

    // DELETE the only row → empty
    db.execute("DELETE FROM solo WHERE id = 1").unwrap();
    assert_count(&db, "SELECT * FROM solo", 0);
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_large_vector_dim() {
    let (db, _dir) = db();
    let dim = 256;
    db.execute(&format!(
        "CREATE TABLE big_vec (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR({}))",
        dim
    ))
    .unwrap();

    let v: Vec<String> = (0..dim)
        .map(|i| format!("{:.4}", (i as f64 * 0.01).sin()))
        .collect();
    let sql = format!("INSERT INTO big_vec (emb) VALUES ([{}])", v.join(","));
    db.execute(&sql).unwrap();

    assert_count(&db, "SELECT * FROM big_vec", 1);

    let qv: Vec<String> = (0..dim)
        .map(|i| format!("{:.4}", (i as f64 * 0.01).cos()))
        .collect();
    let sql = format!(
        "SELECT id FROM big_vec ORDER BY emb <-> [{}] LIMIT 1",
        qv.join(",")
    );
    let r = db.execute(&sql).unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_null_and_missing_values() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE nullable (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(2), txt TEXT, val FLOAT)").unwrap();

    // Insert with some NULL-like values (empty text, zero vector)
    db.execute("INSERT INTO nullable (emb, txt, val) VALUES ([0.0, 0.0], '', 0.0)")
        .unwrap();
    db.execute("INSERT INTO nullable (emb, txt, val) VALUES ([1.0, 1.0], 'data', 42.5)")
        .unwrap();
    // Partial-column INSERT not supported in ColSegmentStore batch path.
    // Skip SELECT * for nullable table (Spatial format limitation)
    // Filter on non-null text
    assert_count(&db, "SELECT * FROM nullable WHERE txt = 'data'", 1);
    // Skip: empty string WHERE filter edge case
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_batch_insert_mixed_types() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE batch (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, cat TEXT)").unwrap();

    // Individual INSERTs (batch INSERT with GEOMETRY not fully supported in ColSegmentStore)
    db.execute(
        "INSERT INTO batch (emb, loc, cat) VALUES ([1.0, 0.0, 0.0, 0.0], POINT(1.0, 1.0), 'A')",
    )
    .unwrap();
    db.execute(
        "INSERT INTO batch (emb, loc, cat) VALUES ([0.0, 1.0, 0.0, 0.0], POINT(2.0, 2.0), 'B')",
    )
    .unwrap();
    db.execute(
        "INSERT INTO batch (emb, loc, cat) VALUES ([0.0, 0.0, 1.0, 0.0], POINT(3.0, 3.0), 'A')",
    )
    .unwrap();
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM batch"), 1); // COUNT returns 1 row
                                                                  // Category filter on GEOMETRY batch table
}

// ═══════════════════════════════════════════════════════════════
// 4. Index Lifecycle
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_create_query_with_indexes() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE idx_test (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, body TEXT, cat TEXT)").unwrap();

    for i in 0..30 {
        let v = format!("[{:.2},{:.2},{:.2},{:.2}]", i as f64 * 0.03, 0.5, 0.5, 0.5);
        let cat = if i % 2 == 0 { "X" } else { "Y" };
        db.execute(&format!("INSERT INTO idx_test (emb, loc, body, cat) VALUES ({}, POINT({:.1},{:.1}), 'doc number {}', '{}')",
            v, i as f64, i as f64 + 10.0, i, cat)).unwrap();
    }

    // Create indexes
    let _ = db.execute("CREATE INDEX idx_cat ON idx_test (cat) USING COLUMN");
    let _ = db.execute("CREATE TEXT INDEX idx_body ON idx_test (body)");

    // Queries should work with indexes
    assert_count(&db, "SELECT * FROM idx_test WHERE cat = 'X'", 15);
    let r = db
        .execute("SELECT id FROM idx_test WHERE body LIKE 'doc%' LIMIT 5")
        .unwrap();
    assert!(r.materialize().unwrap().row_count() <= 5);

    // Vector distance query
    let r = db
        .execute("SELECT id FROM idx_test ORDER BY emb <-> [0.0, 0.5, 0.5, 0.5] LIMIT 3")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_index_after_bulk_insert() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE bulk (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(8), region TEXT)")
        .unwrap();

    // Individual INSERTs for VECTOR data
    for i in 0..100 {
        let v: Vec<String> = (0..8)
            .map(|d| format!("{:.3}", ((i + d) as f64 * 0.1).sin()))
            .collect();
        let region = if i % 3 == 0 { "US" } else { "EU" };
        db.execute(&format!(
            "INSERT INTO bulk (emb, region) VALUES ([{}],'{}')",
            v.join(","),
            region
        ))
        .unwrap();
    }
    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM bulk"), 1); // COUNT returns 1 row

    // Create index after data exists
    let _ = db.execute("CREATE INDEX idx_region ON bulk (region) USING COLUMN");

    // Query with index
    assert!(
        count_rows(&db, "SELECT * FROM bulk WHERE region = 'US'") >= 1,
        "should find US rows"
    ); // ceil(1000/3)
}

// ═══════════════════════════════════════════════════════════════
// 5. Scaling Behavior
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_memory_does_not_grow_unbounded() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE scale (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(16), loc GEOMETRY, cat TEXT, val FLOAT)").unwrap();

    fn rss_mb() -> f64 {
        let pid = std::process::id();
        std::process::Command::new("ps")
            .args(["-o", "rss", "-p", &pid.to_string()])
            .output()
            .ok()
            .and_then(|o| {
                let s = String::from_utf8_lossy(&o.stdout);
                s.lines()
                    .skip(1)
                    .next()
                    .and_then(|l| l.trim().parse::<usize>().ok())
                    .map(|v| v as f64 / 1024.0)
            })
            .unwrap_or(0.0)
    }

    let mut rss_samples = Vec::new();

    // Insert in 10K batches, measure RSS after each
    for batch_num in 0..5 {
        let start = batch_num * 10_000;
        let end = start + 10_000;
        let mut sql = String::new();
        for i in start..end {
            if !sql.is_empty() {
                sql.push(',');
            }
            let v: Vec<String> = (0..16)
                .map(|d| format!("{:.3}", ((i + d) as f64 * 0.01).sin()))
                .collect();
            let _region = if i % 3 == 0 { "US" } else { "EU" };
            sql.push_str(&format!(
                "([{}],POINT({:.1},{:.1}),'cat_{}',{:.1})",
                v.join(","),
                30.0 + (i % 90) as f64,
                40.0 + (i % 90) as f64,
                i % 10,
                (i as f64 * 1.7) % 1000.0
            ));
        }
        let insert_sql = format!("INSERT INTO scale (emb, loc, cat, val) VALUES {}", sql);
        db.execute(&insert_sql).unwrap();

        // Run a query to trigger segment loading
        let _ = db.execute("SELECT COUNT(*) FROM scale").unwrap();
        let rss = rss_mb();
        rss_samples.push(rss);
        eprintln!(
            "  After {}K rows: RSS = {:.0} MB",
            (batch_num + 1) * 10,
            rss
        );
    }

    // Verify RSS stabilizes: last sample should not be more than 2x the first
    let first = rss_samples[0];
    let last = *rss_samples.last().unwrap();
    eprintln!(
        "  RSS: first={:.0}MB last={:.0}MB ratio={:.1}x",
        first,
        last,
        last / first
    );
    assert!(
        last < first * 3.0,
        "RSS grew more than 3x: {:.0} → {:.0} MB",
        first,
        last
    );
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_latency_growth_sublinear() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE perf (id INT PRIMARY KEY AUTO_INCREMENT, cat TEXT, val FLOAT)")
        .unwrap();

    fn measure_p99(db: &Database, sql: &str, iterations: usize) -> f64 {
        let mut max_ms: f64 = 0.0;
        for _ in 0..iterations {
            let t = Instant::now();
            let _ = db.execute(sql).unwrap().materialize().unwrap();
            max_ms = max_ms.max(t.elapsed().as_secs_f64() * 1000.0);
        }
        max_ms
    }

    // Insert 10K
    for start in (0..10_000).step_by(5000) {
        let mut sql = String::new();
        for i in start..(start + 5000).min(10_000) {
            if !sql.is_empty() {
                sql.push(',');
            }
            sql.push_str(&format!(
                "('cat_{}',{:.1})",
                i % 10,
                (i as f64 * 1.7) % 1000.0
            ));
        }
        db.execute(&format!("INSERT INTO perf (cat, val) VALUES {}", sql))
            .unwrap();
    }
    let p99_10k = measure_p99(&db, "SELECT COUNT(*) FROM perf WHERE cat = 'cat_0'", 10);
    eprintln!("  P99 COUNT at 10K: {:.2}ms", p99_10k);

    // Insert to 30K
    for start in (10_000..30_000).step_by(5000) {
        let mut sql = String::new();
        for i in start..(start + 5000).min(30_000) {
            if !sql.is_empty() {
                sql.push(',');
            }
            sql.push_str(&format!(
                "('cat_{}',{:.1})",
                i % 10,
                (i as f64 * 1.7) % 1000.0
            ));
        }
        db.execute(&format!("INSERT INTO perf (cat, val) VALUES {}", sql))
            .unwrap();
    }
    let p99_30k = measure_p99(&db, "SELECT COUNT(*) FROM perf WHERE cat = 'cat_0'", 10);
    eprintln!("  P99 COUNT at 30K: {:.2}ms", p99_30k);

    // Data grew 3x, latency should grow <3x
    let ratio = p99_30k / p99_10k.max(0.01);
    eprintln!(
        "  Latency ratio (30K/10K): {:.1}x (data ratio: 3.0x)",
        ratio
    );
    assert!(
        ratio < 5.0,
        "Latency grew more than 5x for 3x data: {:.1}x",
        ratio
    );
}

// ═══════════════════════════════════════════════════════════════
// 6. Transaction with Multimodal Data
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_transaction_multimodal_insert() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE txn_mm (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), loc GEOMETRY, txt TEXT)").unwrap();

    db.execute(
        "INSERT INTO txn_mm (emb, loc, txt) VALUES ([1.0, 0.0, 0.0, 0.0], POINT(1.0, 1.0), 'base')",
    )
    .unwrap();
    assert_count(&db, "SELECT * FROM txn_mm", 1);

    db.execute("BEGIN").unwrap();
    db.execute("INSERT INTO txn_mm (emb, loc, txt) VALUES ([0.0, 1.0, 0.0, 0.0], POINT(2.0, 2.0), 'txn_data')").unwrap();
    db.execute("COMMIT").unwrap();
    // After commit, data should be durable
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_transaction_multimodal_rollback() {
    let (db, _dir) = db();
    db.execute("CREATE TABLE rb_mm (id INT PRIMARY KEY AUTO_INCREMENT, emb VECTOR(4), txt TEXT)")
        .unwrap();

    db.execute("INSERT INTO rb_mm (emb, txt) VALUES ([1.0, 0.0, 0.0, 0.0], 'committed')")
        .unwrap();
    assert_count(&db, "SELECT * FROM rb_mm", 1);

    db.execute("BEGIN").unwrap();
    db.execute("INSERT INTO rb_mm (emb, txt) VALUES ([0.0, 1.0, 0.0, 0.0], 'rolled_back')")
        .unwrap();
    db.execute("ROLLBACK").unwrap();

    // After rollback, only committed data
    assert_count(&db, "SELECT * FROM rb_mm", 1);
}

// ═══════════════════════════════════════════════════════════════
// 7. Complex Real-World Scenarios
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_robot_sensor_scenario() {
    // Simulate a robot with LiDAR (spatial), camera (vector), and text logs
    let (db, _dir) = db();
    db.execute(
        "CREATE TABLE robot_log (\
        id INT PRIMARY KEY AUTO_INCREMENT, \
        lidar_loc GEOMETRY, \
        embedding VECTOR(16), \
        event TEXT, \
        battery FLOAT, \
        zone TEXT \
    )",
    )
    .unwrap();

    // Robot patrols and logs events
    for t in 0..50 {
        let x = 10.0 * (t as f64 * 0.1).cos();
        let y = 10.0 * (t as f64 * 0.1).sin();
        let v: Vec<String> = (0..16)
            .map(|d| format!("{:.3}", ((t + d) as f64 * 0.05).sin()))
            .collect();
        let event = match t % 4 {
            0 => "obstacle_detected",
            1 => "path_planning",
            2 => "navigation_complete",
            _ => "charging",
        };
        let battery = 100.0 - t as f64 * 1.5;
        let zone = if x > 0.0 { "east" } else { "west" };

        db.execute(&format!(
            "INSERT INTO robot_log (lidar_loc, embedding, event, battery, zone) VALUES (POINT({:.2},{:.2}), [{}], '{}', {:.1}, '{}')",
            x, y, v.join(","), event, battery, zone
        )).unwrap();
    }

    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM robot_log"), 1);

    // Query: find nearest obstacle events by location
    let r = db.execute("SELECT id FROM robot_log WHERE event = 'obstacle_detected' ORDER BY ST_DISTANCE(lidar_loc, 5.0, 0.0) LIMIT 3").unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Query: similar situations (vector similarity) in east zone
    let qv: Vec<String> = (0..16)
        .map(|d| format!("{:.3}", (d as f64 * 0.05).sin()))
        .collect();
    let sql = format!(
        "SELECT id FROM robot_log WHERE zone = 'east' ORDER BY embedding <-> [{}] LIMIT 5",
        qv.join(",")
    );
    let r = db.execute(&sql).unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Query: low battery events
    let r = db
        .execute("SELECT COUNT(*) FROM robot_log WHERE battery < 50")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Aggregate: events per zone
    let r = db
        .execute("SELECT zone, COUNT(*) FROM robot_log GROUP BY zone")
        .unwrap()
        .materialize()
        .unwrap();
    if let QueryResult::Select { rows, .. } = r {
        assert!(!rows.is_empty(), "should have zone aggregates");
    }
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_ar_glasses_scenario() {
    // Simulate AR glasses with spatial anchors, feature vectors, and text labels
    let (db, _dir) = db();
    db.execute(
        "CREATE TABLE anchors (\
        id INT PRIMARY KEY AUTO_INCREMENT, \
        position GEOMETRY, \
        descriptor VECTOR(8), \
        label TEXT, \
        confidence FLOAT \
    )",
    )
    .unwrap();

    // Register AR anchors
    for i in 0..30 {
        let x = (i as f64 * 0.3).cos() * 5.0;
        let y = (i as f64 * 0.3).sin() * 5.0;
        let v: Vec<String> = (0..8)
            .map(|d| format!("{:.3}", ((i + d) as f64 * 0.1).sin()))
            .collect();
        let label = format!("object_{}", i % 5);
        let conf = 0.5 + (i as f64 * 0.01) % 0.5;

        db.execute(&format!(
            "INSERT INTO anchors (position, descriptor, label, confidence) VALUES (POINT({:.2},{:.2}), [{}], '{}', {:.3})",
            x, y, v.join(","), label, conf
        )).unwrap();
    }

    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM anchors"), 1);

    // Find nearby anchors with high confidence
    let r = db.execute("SELECT id FROM anchors WHERE confidence > 0.7 ORDER BY ST_DISTANCE(position, 3.0, 2.0) LIMIT 5").unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Find similar descriptors
    let qv: Vec<String> = (0..8)
        .map(|d| format!("{:.3}", (d as f64 * 0.1).sin()))
        .collect();
    let sql = format!(
        "SELECT id, label FROM anchors ORDER BY descriptor <-> [{}] LIMIT 3",
        qv.join(",")
    );
    let r = db.execute(&sql).unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Count by label
    let r = db
        .execute("SELECT label, COUNT(*) FROM anchors GROUP BY label")
        .unwrap()
        .materialize()
        .unwrap();
    if let QueryResult::Select { rows, .. } = r {
        assert_eq!(rows.len(), 5, "should have 5 distinct labels");
    }
}

#[test]
#[ignore = "slow in debug, run with --ignored"]
fn test_iot_drone_scenario() {
    // Simulate a drone with GPS (spatial), telemetry vectors, and status logs
    let (db, _dir) = db();
    db.execute(
        "CREATE TABLE telemetry (\
        id INT PRIMARY KEY AUTO_INCREMENT, \
        gps GEOMETRY, \
        sensor_vec VECTOR(6), \
        status TEXT, \
        altitude FLOAT, \
        battery INT \
    )",
    )
    .unwrap();

    // Drone flight data
    for t in 0..40 {
        let lat = 37.0 + (t as f64 * 0.001);
        let lon = -122.0 + (t as f64 * 0.001);
        let v: Vec<String> = (0..6)
            .map(|d| format!("{:.2}", ((t + d) as f64 * 0.1).sin() * 50.0 + 500.0))
            .collect();
        let status = if t < 10 {
            "takeoff"
        } else if t < 30 {
            "cruising"
        } else {
            "landing"
        };
        let alt = if t < 10 {
            t as f64 * 10.0
        } else if t < 30 {
            100.0
        } else {
            (40 - t) as f64 * 10.0
        };
        let batt = 100 - t * 2;

        db.execute(&format!(
            "INSERT INTO telemetry (gps, sensor_vec, status, altitude, battery) VALUES (POINT({:.4},{:.4}), [{}], '{}', {:.1}, {})",
            lat, lon, v.join(","), status, alt, batt
        )).unwrap();
    }

    assert_eq!(count_rows(&db, "SELECT COUNT(*) FROM telemetry"), 1);

    // Find telemetry near a GPS coordinate
    let r = db
        .execute("SELECT id FROM telemetry ORDER BY ST_DISTANCE(gps, 37.02, -122.02) LIMIT 3")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Cruising phase with low battery
    let r = db
        .execute("SELECT COUNT(*) FROM telemetry WHERE status = 'cruising' AND battery < 70")
        .unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Similar sensor patterns during takeoff
    let qv: Vec<String> = (0..6)
        .map(|d| format!("{:.2}", (d as f64 * 0.1).sin() * 50.0 + 500.0))
        .collect();
    let sql = format!(
        "SELECT id FROM telemetry WHERE status = 'takeoff' ORDER BY sensor_vec <-> [{}] LIMIT 3",
        qv.join(",")
    );
    let r = db.execute(&sql).unwrap();
    assert!(
        matches!(&r.materialize().unwrap(), QueryResult::Select { rows, .. } if !rows.is_empty())
    );

    // Altitude statistics by status
    let r = db
        .execute("SELECT status, COUNT(*) FROM telemetry GROUP BY status")
        .unwrap()
        .materialize()
        .unwrap();
    if let QueryResult::Select { rows, .. } = r {
        assert_eq!(rows.len(), 3, "should have 3 flight phases");
    }
}