graphitesql 0.1.0

A pure, safe, no_std Rust re-implementation of SQLite, compatible with the SQLite 3 file format.
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
//! Direct-DML write path for external-content and contentless FTS5 tables:
//! `INSERT INTO ft(rowid, <cols>)` adds a document's postings, the special
//! `INSERT INTO ft(ft, rowid, <cols>) VALUES('delete', …)` command subtracts them,
//! `'delete-all'` clears the index, and a contentless table (`content=''`) reads
//! its columns back as NULL (with `highlight`/`snippet` also NULL) while `MATCH`
//! still works off the inverted index.
//!
//! Every case is differential vs stock `sqlite3` 3.50.4 (same script through both
//! engines against a file database; ROW output must match byte-for-byte). The
//! on-disk cases additionally have `sqlite3` MATCH and `PRAGMA integrity_check` a
//! graphite-written database. Skipped when `sqlite3` is absent.

#![cfg(all(feature = "std", feature = "fts5"))]

use graphitesql::Connection;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

fn have_sqlite() -> bool {
    Command::new("sqlite3").arg("--version").output().is_ok()
}

fn tmp_path(tag: &str) -> String {
    static SEQ: AtomicU64 = AtomicU64::new(0);
    let p = std::env::temp_dir().join(format!(
        "gsql-fts5-clw-{tag}-{}-{}.db",
        std::process::id(),
        SEQ.fetch_add(1, Ordering::Relaxed)
    ));
    let p = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&p);
    p
}

/// Run `setup` (DDL/DML, no output) then `query` through stock sqlite3 against a
/// fresh file, returning the query's `|`-joined lines (row order kept).
fn sqlite_out(tag: &str, setup: &str, query: &str) -> String {
    let path = tmp_path(&format!("s-{tag}"));
    let script = format!("{setup}\n{query}");
    let o = Command::new("sqlite3")
        .arg(&path)
        .arg(&script)
        .output()
        .unwrap();
    let _ = std::fs::remove_file(&path);
    assert!(
        o.status.success(),
        "sqlite3 failed for {tag}: {}",
        String::from_utf8_lossy(&o.stderr)
    );
    String::from_utf8_lossy(&o.stdout).trim_end().to_string()
}

/// Run `setup` then `query` through graphite against a fresh file, returning the
/// query's `|`-joined lines in the same `sqlite3 -list` shape.
fn graphite_out(tag: &str, setup: &str, query: &str) -> String {
    let path = tmp_path(&format!("g-{tag}"));
    let mut c = Connection::create(&path).unwrap();
    for stmt in split_statements(setup) {
        c.execute(&stmt)
            .unwrap_or_else(|e| panic!("graphite setup {stmt:?}: {e}"));
    }
    let res = c
        .query(query)
        .unwrap_or_else(|e| panic!("graphite query {query:?}: {e}"));
    drop(c);
    let _ = std::fs::remove_file(&path);
    render_rows(&res.rows)
}

fn render_rows(rows: &[Vec<graphitesql::Value>]) -> String {
    let mut out = String::new();
    for (ri, row) in rows.iter().enumerate() {
        if ri > 0 {
            out.push('\n');
        }
        for (ci, v) in row.iter().enumerate() {
            if ci > 0 {
                out.push('|');
            }
            out.push_str(&render(v));
        }
    }
    out
}

fn render(v: &graphitesql::Value) -> String {
    use graphitesql::Value::*;
    match v {
        Null => String::new(),
        Integer(i) => i.to_string(),
        Real(r) => format!("{r}"),
        Text(t) => t.clone(),
        Blob(b) => String::from_utf8_lossy(b).into_owned(),
    }
}

/// Split a `;`-terminated setup script into individual statements.
fn split_statements(script: &str) -> Vec<String> {
    script
        .split(';')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(|s| format!("{s};"))
        .collect()
}

/// Assert graphite and sqlite produce identical `query` output after `setup`.
fn assert_same(tag: &str, setup: &str, query: &str) {
    let s = sqlite_out(tag, setup, query);
    let g = graphite_out(tag, setup, query);
    assert_eq!(
        g, s,
        "\n[{tag}] query: {query}\ngraphite:\n{g}\nsqlite:\n{s}"
    );
}

// ---------------------------------------------------------------------------
// Gap 1: direct INSERT on an external-content table indexes the supplied text.
// ---------------------------------------------------------------------------

const EXT_SETUP: &str = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
INSERT INTO src VALUES(1,'hello');
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO ft(ft) VALUES('rebuild');
INSERT INTO src VALUES(2,'world');
INSERT INTO ft(rowid, body) VALUES(2,'world');";

#[test]
fn external_direct_insert_then_match() {
    if !have_sqlite() {
        return;
    }
    assert_same(
        "ext-ins-world",
        EXT_SETUP,
        "SELECT rowid FROM ft WHERE ft MATCH 'world';",
    );
    assert_same(
        "ext-ins-hello",
        EXT_SETUP,
        "SELECT rowid FROM ft WHERE ft MATCH 'hello';",
    );
    // The supplied text is indexed even when it diverges from the content table.
    let setup = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO src VALUES(2,'world');
INSERT INTO ft(rowid, body) VALUES(2,'zzz');";
    assert_same(
        "ext-supplied-zzz",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'zzz';",
    );
    assert_same(
        "ext-supplied-noworld",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'world';",
    );
}

// ---------------------------------------------------------------------------
// Gap 2: the `'delete'` command (contentless & external content).
// ---------------------------------------------------------------------------

#[test]
fn delete_command_contentless() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'a b'),(2,'c d');
INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'a b');";
    assert_same(
        "del-cl-a",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'a';",
    );
    assert_same(
        "del-cl-b",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'b';",
    );
    assert_same(
        "del-cl-c",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'c';",
    );
}

#[test]
fn delete_command_external() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
INSERT INTO src VALUES(1,'a b'),(2,'c d');
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO ft(ft) VALUES('rebuild');
INSERT INTO ft(ft, rowid, body) VALUES('delete', 1, 'a b');";
    assert_same(
        "del-ext-a",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'a';",
    );
    assert_same(
        "del-ext-c",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'c';",
    );
}

#[test]
fn delete_command_partial_old_text_matches_sqlite() {
    if !have_sqlite() {
        return;
    }
    // A partial `'delete'` (subset of the doc's tokens) subtracts exactly those
    // tokens' postings; the rest of the doc stays matchable — like sqlite.
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'a b c');
INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'a');";
    assert_same(
        "del-partial-a",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'a';",
    );
    assert_same(
        "del-partial-b",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'b';",
    );
    assert_same(
        "del-partial-c",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'c';",
    );
}

#[test]
fn delete_all_command_contentless_and_external() {
    if !have_sqlite() {
        return;
    }
    let cl = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'a'),(2,'b');
INSERT INTO ft(ft) VALUES('delete-all');";
    assert_same(
        "delall-cl",
        cl,
        "SELECT rowid FROM ft WHERE ft MATCH 'a OR b';",
    );
    let ext = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
INSERT INTO src VALUES(1,'a'),(2,'b');
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO ft(ft) VALUES('rebuild');
INSERT INTO ft(ft) VALUES('delete-all');";
    assert_same(
        "delall-ext",
        ext,
        "SELECT rowid FROM ft WHERE ft MATCH 'a OR b';",
    );
}

#[test]
fn additive_insert_same_rowid_unions_terms() {
    if !have_sqlite() {
        return;
    }
    // Re-inserting the same rowid adds the new tokens (union of terms); each term's
    // positions come from the latest insert containing it — matching sqlite.
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'a b');
INSERT INTO ft(rowid, x) VALUES(1,'c');";
    assert_same(
        "add-a",
        setup,
        "SELECT count(*) FROM ft WHERE ft MATCH 'a';",
    );
    assert_same(
        "add-b",
        setup,
        "SELECT count(*) FROM ft WHERE ft MATCH 'b';",
    );
    assert_same(
        "add-c",
        setup,
        "SELECT count(*) FROM ft WHERE ft MATCH 'c';",
    );
}

// ---------------------------------------------------------------------------
// Gap 3: contentless `SELECT <col>` → NULL; highlight/snippet → NULL; MATCH ok.
// ---------------------------------------------------------------------------

#[test]
fn contentless_select_column_is_null() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'hello world');";
    assert_same("cl-col", setup, "SELECT quote(x) FROM ft;");
    assert_same(
        "cl-star",
        setup,
        "SELECT quote(x) FROM ft WHERE ft MATCH 'hello';",
    );
    assert_same(
        "cl-rowid",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'world';",
    );
    assert_same(
        "cl-highlight",
        setup,
        "SELECT quote(highlight(ft,0,'[',']')) FROM ft WHERE ft MATCH 'hello';",
    );
    assert_same(
        "cl-snippet",
        setup,
        "SELECT quote(snippet(ft,0,'[',']','...',5)) FROM ft WHERE ft MATCH 'hello';",
    );
}

#[test]
fn delete_then_reinsert() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(x, content='');
INSERT INTO ft(rowid, x) VALUES(1,'apple');
INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'apple');
INSERT INTO ft(rowid, x) VALUES(1,'banana');";
    assert_same(
        "dr-apple",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'apple';",
    );
    assert_same(
        "dr-banana",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'banana';",
    );
}

#[test]
fn contentless_multi_column_and_query_shapes() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE VIRTUAL TABLE ft USING fts5(a, b, content='');
INSERT INTO ft(rowid, a, b) VALUES(1,'red apple','fresh fruit'),(2,'blue sky','the sky is blue');";
    assert_same(
        "cl-mc-phrase",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH '\"fresh fruit\"';",
    );
    assert_same(
        "cl-mc-colscope",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'b:sky';",
    );
    assert_same(
        "cl-mc-and",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'red AND apple';",
    );
    assert_same(
        "cl-mc-prefix",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'app*';",
    );
    assert_same(
        "cl-mc-cols-null",
        setup,
        "SELECT quote(a), quote(b) FROM ft ORDER BY rowid;",
    );
}

// ---------------------------------------------------------------------------
// External DELETE / UPDATE (via WHERE) — supported; contentless rejects them.
// ---------------------------------------------------------------------------

#[test]
fn external_delete_via_where() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
INSERT INTO src VALUES(1,'a b'),(2,'c');
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO ft(ft) VALUES('rebuild');
DELETE FROM ft WHERE rowid=1;";
    assert_same(
        "ext-del-a",
        setup,
        "SELECT count(*) FROM ft WHERE ft MATCH 'a';",
    );
    assert_same(
        "ext-del-c",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'c';",
    );
}

#[test]
fn external_update_via_where() {
    if !have_sqlite() {
        return;
    }
    let setup = "\
CREATE TABLE src(id INTEGER PRIMARY KEY, body);
INSERT INTO src VALUES(1,'old');
CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');
INSERT INTO ft(ft) VALUES('rebuild');
UPDATE ft SET body='new' WHERE rowid=1;";
    assert_same(
        "ext-upd-old",
        setup,
        "SELECT count(*) FROM ft WHERE ft MATCH 'old';",
    );
    assert_same(
        "ext-upd-new",
        setup,
        "SELECT rowid FROM ft WHERE ft MATCH 'new';",
    );
}

#[test]
fn contentless_delete_and_update_are_rejected() {
    if !have_sqlite() {
        return;
    }
    // sqlite rejects `DELETE`/`UPDATE` on a contentless table; graphite errors too.
    let path = tmp_path("cl-reject");
    let mut c = Connection::create(&path).unwrap();
    c.execute("CREATE VIRTUAL TABLE ft USING fts5(x, content='');")
        .unwrap();
    c.execute("INSERT INTO ft(rowid, x) VALUES(1,'a b');")
        .unwrap();
    assert!(
        c.execute("DELETE FROM ft WHERE rowid=1;").is_err(),
        "contentless DELETE should be rejected"
    );
    assert!(
        c.execute("UPDATE ft SET x='c' WHERE rowid=1;").is_err(),
        "contentless UPDATE should be rejected"
    );
    drop(c);
    let _ = std::fs::remove_file(&path);
}

// ---------------------------------------------------------------------------
// On-disk byte compatibility: graphite writes, sqlite reads / integrity-checks.
// ---------------------------------------------------------------------------

fn sqlite_query_file(path: &str, query: &str) -> String {
    let o = Command::new("sqlite3")
        .arg(path)
        .arg(query)
        .output()
        .unwrap();
    assert!(
        o.status.success(),
        "sqlite3 read failed: {}",
        String::from_utf8_lossy(&o.stderr)
    );
    String::from_utf8_lossy(&o.stdout).trim_end().to_string()
}

#[test]
fn graphite_written_contentless_is_sqlite_readable() {
    if !have_sqlite() {
        return;
    }
    let path = tmp_path("bytecompat-cl");
    {
        let mut c = Connection::create(&path).unwrap();
        for stmt in [
            "CREATE VIRTUAL TABLE ft USING fts5(x, content='');",
            "INSERT INTO ft(rowid, x) VALUES(1,'alpha beta'),(2,'gamma delta'),(3,'beta gamma');",
            "INSERT INTO ft(ft, rowid, x) VALUES('delete', 2, 'gamma delta');",
            "INSERT INTO ft(rowid, x) VALUES(4,'delta epsilon');",
        ] {
            c.execute(stmt).unwrap();
        }
        drop(c);
    }
    // sqlite MATCHes the graphite-written contentless index.
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'beta';"),
        "1\n3"
    );
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'gamma';"),
        "3"
    );
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'delta';"),
        "4"
    );
    // A contentless column reads back NULL under sqlite too.
    assert_eq!(
        sqlite_query_file(&path, "SELECT quote(x) FROM ft ORDER BY rowid;"),
        "NULL\nNULL\nNULL"
    );
    // PRAGMA integrity_check and the fts5 internal integrity-check both pass.
    assert_eq!(sqlite_query_file(&path, "PRAGMA integrity_check;"), "ok");
    assert_eq!(
        sqlite_query_file(
            &path,
            "INSERT INTO ft(ft) VALUES('integrity-check'); SELECT 'ok';"
        ),
        "ok"
    );
    let _ = std::fs::remove_file(&path);
}

#[test]
fn graphite_written_external_is_sqlite_readable() {
    if !have_sqlite() {
        return;
    }
    let path = tmp_path("bytecompat-ext");
    {
        let mut c = Connection::create(&path).unwrap();
        for stmt in [
            "CREATE TABLE src(id INTEGER PRIMARY KEY, body);",
            "INSERT INTO src VALUES(1,'hello world'),(2,'foo bar');",
            "CREATE VIRTUAL TABLE ft USING fts5(body, content='src', content_rowid='id');",
            "INSERT INTO ft(ft) VALUES('rebuild');",
            "INSERT INTO src VALUES(3,'baz qux');",
            "INSERT INTO ft(rowid, body) VALUES(3,'baz qux');",
            "INSERT INTO ft(ft, rowid, body) VALUES('delete', 2, 'foo bar');",
        ] {
            c.execute(stmt).unwrap();
        }
        drop(c);
    }
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'baz';"),
        "3"
    );
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'foo';"),
        ""
    );
    assert_eq!(
        sqlite_query_file(&path, "SELECT rowid FROM ft WHERE ft MATCH 'hello';"),
        "1"
    );
    assert_eq!(sqlite_query_file(&path, "PRAGMA integrity_check;"), "ok");
    assert_eq!(
        sqlite_query_file(
            &path,
            "INSERT INTO ft(ft) VALUES('integrity-check'); SELECT 'ok';"
        ),
        "ok"
    );
    let _ = std::fs::remove_file(&path);
}