reddb-io-engine 1.27.3

Shared RedDB query execution and storage primitives for native and WebAssembly runtimes.
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
#[path = "support/codec_corpus.rs"]
#[allow(dead_code)]
mod codec_corpus;
#[path = "support/pager.rs"]
#[allow(dead_code)]
mod pager;

use reddb_engine::query::{local_database::LocalDatabase, local_read::LocalReadDatabase};
use reddb_types::Value;

type Database = LocalDatabase<pager::MemoryFile>;
fn create() -> Database {
    Database::create(pager::MemoryFile::default(), "sql.rdb", 1000).expect("create local database")
}
fn run(database: &mut Database, sql: &str) {
    database.query(sql, 2000).expect(sql);
}
fn rows(database: &mut Database, sql: &str) -> Vec<Vec<Value>> {
    database.query(sql, 2000).expect(sql).rows
}
fn seed() -> Database {
    let mut database = create();
    run(&mut database, "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL, total INTEGER DEFAULT = 10)");
    run(
        &mut database,
        "INSERT INTO people (id, name) VALUES (1, 'Ana'), (2, 'Bia')",
    );
    database
}

#[test]
fn inspection_filters_sources_before_caller_predicates_and_limits() {
    use reddb_engine::query::local_read::ReadScope;
    let mut database = create();
    run(
        &mut database,
        "CREATE TABLE files (id INTEGER PRIMARY KEY, tenant_id TEXT NOT NULL, body TEXT)",
    );
    run(&mut database, "INSERT INTO files VALUES (1, 'other', 'hidden'), (2, 'mine', 'clue'), (3, 'other', 'hidden too')");
    let scopes = [ReadScope {
        table: "files".into(),
        field: "tenant_id".into(),
        value: Value::text("mine"),
    }];
    for sql in [
        "SELECT body FROM files WHERE tenant_id = 'other' OR 1 = 1 ORDER BY id LIMIT 1",
        "SELECT f.body FROM files f ORDER BY f.id",
        "SELECT body FROM files WHERE body = $1",
    ] {
        let parameters = if sql.contains("$1") {
            vec![Value::text("clue")]
        } else {
            vec![]
        };
        assert_eq!(
            database
                .read_query(sql, &parameters, &scopes)
                .expect("scoped read")
                .rows,
            [vec![Value::text("clue")]]
        );
    }
    assert!(database
        .read_query("SELECT * FROM files", &[], &[])
        .is_err());
    let wrong = [ReadScope {
        table: "files".into(),
        field: "missing".into(),
        value: Value::text("mine"),
    }];
    assert!(database
        .read_query("SELECT * FROM files", &[], &wrong)
        .is_err());
    let duplicate = [
        ReadScope {
            table: "files".into(),
            field: "tenant_id".into(),
            value: Value::text("mine"),
        },
        ReadScope {
            table: "files".into(),
            field: "tenant_id".into(),
            value: Value::text("other"),
        },
    ];
    assert!(database
        .read_query("SELECT * FROM files", &[], &duplicate)
        .is_err());
}

#[test]
fn inspection_cannot_mutate_the_database_or_control_transactions() {
    use reddb_engine::query::local_read::ReadScope;
    let mut database = seed();
    let scopes = [ReadScope {
        table: "people".into(),
        field: "id".into(),
        value: Value::Integer(1),
    }];
    for sql in [
        "DELETE FROM people",
        "UPDATE people SET name = 'lost'",
        "INSERT INTO people VALUES (3, 'C', 1)",
        "CREATE TABLE stolen (id INTEGER)",
        "BEGIN",
        "COMMIT",
        "ROLLBACK",
        "SELECT * FROM people; DELETE FROM people",
    ] {
        assert!(
            database.read_query(sql, &[], &scopes).is_err(),
            "must reject {sql}"
        );
    }
    assert_eq!(
        rows(&mut database, "SELECT name FROM people ORDER BY id"),
        [vec![Value::text("Ana")], vec![Value::text("Bia")]]
    );
    assert_eq!(database.catalog().expect("catalog").rows.len(), 3);
}

#[test]
fn catalog_tracks_real_contracts_and_transaction_rollback() {
    let mut database = seed();
    let expected = database.catalog().expect("catalog");
    assert_eq!(
        expected.columns,
        ["table_name", "column_name", "data_type", "nullable"]
    );
    assert!(expected
        .rows
        .iter()
        .all(|row| row[0] == Value::text("people")));
    run(&mut database, "BEGIN");
    run(
        &mut database,
        "CREATE TABLE provisional (id INTEGER PRIMARY KEY)",
    );
    assert_eq!(database.catalog().expect("staged catalog").rows.len(), 4);
    assert!(database.query("SELECT missing FROM people", 2000).is_err());
    assert!(database.catalog().is_err());
    run(&mut database, "ROLLBACK");
    let database = Database::open(database.into_inner(), "sql.rdb").expect("reopen");
    assert_eq!(
        database.catalog().expect("reopened catalog").rows,
        expected.rows
    );
}

#[test]
fn sql_crud_recovers_canonical_artifact() {
    let mut database = seed();
    assert_eq!(
        rows(&mut database, "SELECT rid FROM people ORDER BY id"),
        [
            vec![Value::UnsignedInteger(1024)],
            vec![Value::UnsignedInteger(1025)]
        ]
    );
    run(
        &mut database,
        "UPDATE people SET name = UPPER(name), total = total + 5 WHERE id = 1",
    );
    run(&mut database, "DELETE FROM people WHERE id = 2");
    let expected = vec![vec![
        Value::Integer(1),
        Value::text("ANA"),
        Value::Integer(15),
    ]];
    assert_eq!(
        rows(&mut database, "SELECT id, name, total FROM people"),
        expected
    );
    let mut database =
        Database::open(database.into_inner(), "sql.rdb").expect("reopen snapshot and WAL");
    assert_eq!(
        rows(&mut database, "SELECT id, name, total FROM people"),
        expected
    );
    let read = LocalReadDatabase::open(database.into_inner(), "sql.rdb")
        .expect("canonical reader interoperability");
    assert_eq!(
        read.query("SELECT id, name, total FROM people")
            .expect("shared SELECT")
            .rows,
        expected
    );
}

#[test]
fn commit_rollback_and_close_have_real_transaction_boundaries() {
    let mut database = seed();
    run(&mut database, "BEGIN");
    run(&mut database, "UPDATE people SET total = 99 WHERE id = 1");
    assert_eq!(
        rows(&mut database, "SELECT total FROM people WHERE id = 1"),
        [vec![Value::Integer(99)]]
    );
    run(&mut database, "ROLLBACK");
    assert_eq!(
        rows(&mut database, "SELECT total FROM people WHERE id = 1"),
        [vec![Value::Integer(10)]]
    );
    run(&mut database, "BEGIN");
    run(&mut database, "UPDATE people SET total = 22 WHERE id = 2");
    run(&mut database, "DELETE FROM people WHERE id = 1");
    run(&mut database, "COMMIT");
    run(&mut database, "BEGIN");
    run(&mut database, "UPDATE people SET total = 999");
    let mut database =
        Database::open(database.into_inner(), "sql.rdb").expect("close discards uncommitted state");
    assert_eq!(
        rows(&mut database, "SELECT id, total FROM people"),
        [vec![Value::Integer(2), Value::Integer(22)]]
    );
}

#[test]
fn schema_and_rows_commit_or_rollback_together() {
    let mut database = seed();
    run(&mut database, "BEGIN");
    run(&mut database, "CREATE TABLE extra (id INTEGER PRIMARY KEY)");
    run(&mut database, "INSERT INTO extra VALUES (7)");
    run(&mut database, "ROLLBACK");
    assert!(database.query("SELECT * FROM extra", 2000).is_err());
    run(&mut database, "BEGIN");
    run(&mut database, "CREATE TABLE extra (id INTEGER PRIMARY KEY)");
    run(&mut database, "INSERT INTO extra VALUES (8)");
    run(&mut database, "UPDATE people SET total = 55");
    run(&mut database, "COMMIT");
    let mut database = Database::open(database.into_inner(), "sql.rdb")
        .expect("catalog + rows recovered together");
    assert_eq!(
        rows(&mut database, "SELECT id FROM extra"),
        [vec![Value::Integer(8)]]
    );
    assert_eq!(
        rows(&mut database, "SELECT total FROM people ORDER BY id"),
        [vec![Value::Integer(55)], vec![Value::Integer(55)]]
    );
}

#[test]
fn rejected_batches_are_atomic_and_abort_explicit_transactions() {
    let mut database = seed();
    for sql in [
        "INSERT INTO people (id, name) VALUES (3, 'ok'), (1, 'duplicate')",
        "INSERT INTO people (id, name) VALUES (3, 'ok'), (4, NULL)",
        "UPDATE people SET id = 1",
        "UPDATE people SET total = 1 / (id - 2)",
        "UPDATE people SET missing = 1",
    ] {
        assert!(database.query(sql, 2000).is_err(), "{sql}");
    }
    assert_eq!(
        rows(&mut database, "SELECT id, total FROM people ORDER BY id"),
        [
            vec![Value::Integer(1), Value::Integer(10)],
            vec![Value::Integer(2), Value::Integer(10)]
        ]
    );
    run(&mut database, "BEGIN");
    run(
        &mut database,
        "INSERT INTO people (id, name) VALUES (3, 'staged')",
    );
    assert!(database
        .query(
            "INSERT INTO people (id, name) VALUES (1, 'duplicate')",
            2000
        )
        .is_err());
    assert!(database.query("COMMIT", 2000).is_err());
    assert!(database.query("SELECT * FROM people", 2000).is_err());
    run(&mut database, "ROLLBACK");
    assert_eq!(
        rows(&mut database, "SELECT id FROM people ORDER BY id"),
        [vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );
}

#[test]
fn native_constraints_defaults_and_generated_fields_are_shared() {
    let mut database = create();
    run(&mut database, "CREATE TABLE items (id INTEGER PRIMARY KEY, qty INTEGER DEFAULT = 3 CHECK (qty > 0), doubled INTEGER GENERATED ALWAYS AS (qty * 2) STORED)");
    run(&mut database, "INSERT INTO items (id) VALUES (1)");
    assert_eq!(
        rows(&mut database, "SELECT qty, doubled FROM items"),
        [vec![Value::Integer(3), Value::Integer(6)]]
    );
    assert!(database
        .query("INSERT INTO items (id, qty) VALUES (2, -1)", 2000)
        .is_err());
    assert!(database
        .query("INSERT INTO items (id, doubled) VALUES (2, 7)", 2000)
        .is_err());
    run(&mut database, "UPDATE items SET qty = 5 WHERE id = 1");
    assert_eq!(
        rows(&mut database, "SELECT qty, doubled FROM items"),
        [vec![Value::Integer(5), Value::Integer(10)]]
    );
}

fn stage_schema_transaction(database: &mut Database) {
    run(database, "BEGIN");
    run(database, "CREATE TABLE extra (id INTEGER)");
    run(database, "INSERT INTO extra VALUES (8)");
    run(database, "UPDATE people SET total = 55");
}

#[test]
fn catalog_transaction_survives_every_interrupted_publication_barrier() {
    let baseline = seed().into_inner().durable.into_inner();
    let file = pager::MemoryFile::from_bytes(baseline.clone());
    let operations = file.operations.clone();
    let mut successful = Database::open(file, "sql.rdb").expect("baseline");
    stage_schema_transaction(&mut successful);
    operations.borrow_mut().clear();
    run(&mut successful, "COMMIT");
    let count = operations.borrow().len();
    assert!(count > 0);
    for fail_at in 1..=count {
        let mut file = pager::MemoryFile::from_bytes(baseline.clone());
        file.fail_at = Some(fail_at);
        let mut database = Database::open(file, "sql.rdb").expect("open before failure");
        stage_schema_transaction(&mut database);
        assert!(
            database.query("COMMIT", 2000).is_err(),
            "failure at {fail_at}"
        );
        assert!(
            database.query("SELECT * FROM people", 2000).is_err(),
            "failed I/O must fence the handle"
        );
        assert!(
            database.check_healthy().is_err(),
            "host backup reads must honor the publication fence"
        );
        let file = database.into_inner();
        for bytes in [file.bytes.into_inner(), file.durable.into_inner()] {
            let mut recovered = Database::open(pager::MemoryFile::from_bytes(bytes), "sql.rdb")
                .expect("old or new root must recover");
            let totals = rows(&mut recovered, "SELECT total FROM people ORDER BY id");
            let extra = recovered.query("SELECT id FROM extra", 2000);
            match extra {
                Ok(extra) => {
                    assert_eq!(extra.rows, [vec![Value::Integer(8)]]);
                    assert_eq!(totals, [vec![Value::Integer(55)], vec![Value::Integer(55)]]);
                }
                Err(_) => assert_eq!(totals, [vec![Value::Integer(10)], vec![Value::Integer(10)]]),
            }
        }
    }
}

#[test]
fn wal_transaction_never_recovers_a_partial_commit() {
    let baseline = seed().into_inner().durable.into_inner();
    let stage = |database: &mut Database| {
        run(database, "BEGIN");
        run(database, "UPDATE people SET total = 42 WHERE id = 1");
        run(database, "DELETE FROM people WHERE id = 2");
        run(
            database,
            "INSERT INTO people (id, name, total) VALUES (3, 'Caio', 77)",
        );
    };
    let file = pager::MemoryFile::from_bytes(baseline.clone());
    let operations = file.operations.clone();
    let mut database = Database::open(file, "sql.rdb").expect("baseline");
    stage(&mut database);
    operations.borrow_mut().clear();
    run(&mut database, "COMMIT");
    let count = operations.borrow().len();
    assert!(count > 0);
    for fail_at in 1..=count {
        let mut file = pager::MemoryFile::from_bytes(baseline.clone());
        file.fail_at = Some(fail_at);
        let mut database =
            Database::open(file, "sql.rdb").expect("baseline before interrupted WAL");
        stage(&mut database);
        assert!(database.query("COMMIT", 2000).is_err());
        assert!(database.query("SELECT * FROM people", 2000).is_err());
        let file = database.into_inner();
        for bytes in [file.bytes.into_inner(), file.durable.into_inner()] {
            let mut recovered = Database::open(pager::MemoryFile::from_bytes(bytes), "sql.rdb")
                .expect("recover interrupted WAL");
            let rows = rows(&mut recovered, "SELECT id, total FROM people ORDER BY id");
            let old = vec![
                vec![Value::Integer(1), Value::Integer(10)],
                vec![Value::Integer(2), Value::Integer(10)],
            ];
            let new = vec![
                vec![Value::Integer(1), Value::Integer(42)],
                vec![Value::Integer(3), Value::Integer(77)],
            ];
            assert!(
                rows == old || rows == new,
                "partial transaction after failure {fail_at}: {rows:?}"
            );
        }
    }
}

#[test]
fn unsupported_writes_fail_before_creating_catalog_or_rows() {
    let mut database = seed();
    for sql in [
        "CREATE TABLE bad (rid INTEGER)",
        "CREATE TABLE bad (value SECRET)",
        "CREATE TABLE red.bad (id INTEGER)",
        "INSERT INTO people (id, name) VALUES (3, 'x') RETURNING id",
        "INSERT INTO people (id, name) VALUES ($1, 'x')",
        "UPDATE people SET total = ABS()",
        "UPDATE people SET total = 12 LIMIT 1",
        "DELETE FROM people RETURNING id",
    ] {
        assert!(database.query(sql, 2000).is_err(), "{sql}");
    }
    assert!(database.query("SELECT * FROM bad", 2000).is_err());
    assert_eq!(
        rows(&mut database, "SELECT id FROM people ORDER BY id"),
        [vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );
}

#[test]
fn positional_parameters_bind_crud_ast_and_recover_without_sql_interpolation() {
    let mut database = seed();
    let text = "O'Reilly'); DELETE FROM people; -- $1 \0 🦊";
    database
        .query_with_params(
            "INSERT INTO people (id, name) VALUES ($1, $2)",
            &[Value::Integer(3), Value::text(text)],
            2000,
        )
        .expect("bind values as literals");
    database
        .query_with_params(
            "UPDATE people SET total = total + $1 WHERE id = $2",
            &[Value::Integer(5), Value::Integer(3)],
            2000,
        )
        .expect("bound assignment and predicate");
    database
        .query_with_params(
            "DELETE FROM people WHERE id = ?1",
            &[Value::Integer(2)],
            2000,
        )
        .expect("numbered question placeholder");
    let mut database =
        Database::open(database.into_inner(), "sql.rdb").expect("recover bound writes");
    assert_eq!(
        database
            .query_with_params(
                "SELECT name, total FROM people WHERE id = $1 OR id = $1",
                &[Value::Integer(3)],
                2000,
            )
            .expect("reused bound index")
            .rows,
        [vec![Value::text(text), Value::Integer(15)]]
    );
    assert_eq!(
        rows(&mut database, "SELECT id FROM people ORDER BY id").len(),
        2
    );
}

#[test]
fn positional_parameters_preserve_scalar_types_and_bind_limit_offset() {
    let mut database = seed();
    for value in [
        Value::Null,
        Value::Boolean(true),
        Value::Integer(i64::MIN),
        Value::UnsignedInteger(u64::MAX),
        Value::Float(0.125),
        Value::text("$1"),
    ] {
        assert_eq!(
            database
                .query_with_params(
                    "SELECT $1 AS value FROM people WHERE id = 1",
                    std::slice::from_ref(&value),
                    2000,
                )
                .expect("bound literal projection")
                .rows,
            [vec![value]]
        );
    }
    assert_eq!(
        database
            .query_with_params(
                "SELECT '$1' AS quoted, id FROM people ORDER BY id LIMIT $1 OFFSET $2",
                &[Value::Integer(1), Value::Integer(1)],
                2000,
            )
            .expect("bind pagination, preserve quoted placeholder")
            .rows,
        [vec![Value::text("$1"), Value::Integer(2)]]
    );
}

#[test]
fn binding_failures_do_not_mutate_rows_and_abort_explicit_transactions() {
    use reddb_engine::query::local_database::MAX_QUERY_PARAMETERS;
    let mut database = seed();
    for (sql, parameters) in [
        (
            "INSERT INTO people (id, name) VALUES ($1, $2)",
            vec![Value::Integer(3)],
        ),
        (
            "DELETE FROM people WHERE id = $1",
            vec![Value::Integer(1), Value::Integer(2)],
        ),
        (
            "UPDATE people SET total = $2",
            vec![Value::Integer(1), Value::Integer(2)],
        ),
        ("SELECT id FROM people LIMIT $1", vec![Value::Integer(-1)]),
    ] {
        let error = database
            .query_with_params(sql, &parameters, 2000)
            .expect_err(sql);
        assert_eq!(error.code(), "INVALID_PARAMS");
    }
    assert!(database
        .query_with_params(
            "SELECT id FROM people",
            &vec![Value::Null; MAX_QUERY_PARAMETERS + 1],
            2000
        )
        .is_err());
    run(&mut database, "BEGIN");
    assert!(database
        .query_with_params("DELETE FROM people WHERE id = $1", &[], 2000)
        .is_err());
    assert!(database.query("COMMIT", 2000).is_err());
    run(&mut database, "ROLLBACK");
    assert_eq!(
        rows(&mut database, "SELECT id FROM people ORDER BY id"),
        [vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );
}