uqa-engine 0.3.0

Engine: schema-aware table store, catalog restore, transactions
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

use std::sync::mpsc;
use std::time::Duration;

use super::*;

fn integer_column(result: &SQLResult, name: &str) -> Vec<i64> {
    result
        .rows
        .iter()
        .map(|row| match row.get(name) {
            Some(crate::Value::Int(value)) => *value,
            other => panic!("expected integer column {name}, got {other:?}"),
        })
        .collect()
}

fn end_backend_transaction_early(engine: &Engine) {
    engine
        .storage
        .backend
        .as_ref()
        .expect("persistent test engine")
        .rollback_transaction()
        .expect("end backend transaction early");
}

fn assert_combined_panic_and_rollback_error(error: &str) {
    assert!(error.contains("rollback"), "{error}");
    assert!(error.contains("original panic: callback panic"), "{error}");
}

#[test]
fn dropped_transaction_scope_rolls_back_data_and_releases_writer_ownership() {
    let directory = tempfile::tempdir().unwrap();
    let engine = Engine::open(&directory.path().join("scope-cleanup.db")).unwrap();
    engine
        .sql(
            "CREATE TABLE items (id INTEGER PRIMARY KEY, value INTEGER)",
            &[],
        )
        .unwrap();
    engine.sql("INSERT INTO items VALUES (1, 10)", &[]).unwrap();
    let sibling = engine.new_session().unwrap();

    {
        let _scope = TransactionScope::begin(&engine).unwrap();
        engine
            .sql("UPDATE items SET value = 20 WHERE id = 1", &[])
            .unwrap();
        assert_eq!(engine.transaction_depth(), 1);
    }

    assert_eq!(engine.transaction_depth(), 0);
    sibling
        .sql("UPDATE items SET value = value + 1 WHERE id = 1", &[])
        .unwrap();
    assert_eq!(
        integer_column(
            &engine
                .sql("SELECT value FROM items WHERE id = 1", &[])
                .unwrap(),
            "value",
        ),
        [11]
    );
}

#[test]
fn unclosed_nested_callback_frames_are_rejected_and_rolled_back() {
    let directory = tempfile::tempdir().unwrap();
    let engine = Engine::open(&directory.path().join("unbalanced-scope.db")).unwrap();
    engine
        .sql(
            "CREATE TABLE items (id INTEGER PRIMARY KEY, value INTEGER)",
            &[],
        )
        .unwrap();
    engine.sql("INSERT INTO items VALUES (1, 10)", &[]).unwrap();

    let error = engine
        .transaction(|engine| {
            engine
                .sql("UPDATE items SET value = 20 WHERE id = 1", &[])
                .unwrap();
            engine.begin()?;
            engine
                .sql("UPDATE items SET value = 30 WHERE id = 1", &[])
                .unwrap();
            Ok(())
        })
        .unwrap_err();

    assert!(error.to_string().contains("changed scoped frame depth"));
    assert_eq!(engine.transaction_depth(), 0);
    assert_eq!(
        integer_column(
            &engine
                .sql("SELECT value FROM items WHERE id = 1", &[])
                .unwrap(),
            "value",
        ),
        [10]
    );
}

#[test]
fn implicit_read_transaction_rolls_back_an_unclassified_storage_write() {
    let directory = tempfile::tempdir().unwrap();
    let engine = Engine::open(&directory.path().join("read-only-guard.db")).unwrap();

    engine.begin_implicit_statement_transaction(true).unwrap();
    engine
        .storage
        .catalog
        .as_ref()
        .unwrap()
        .set_metadata("hidden-write", "x")
        .unwrap();

    let error = engine.commit().unwrap_err().to_string();
    assert!(error.contains("read-only SQL execution"), "{error}");
    assert_eq!(engine.transaction_depth(), 0);
    assert_eq!(
        engine
            .storage
            .catalog
            .as_ref()
            .unwrap()
            .get_metadata("hidden-write")
            .unwrap(),
        None
    );
}

#[test]
fn rollback_failure_after_callback_panic_is_returned_instead_of_panicking_again() {
    let directory = tempfile::tempdir().unwrap();

    let transaction_engine = Engine::open(&directory.path().join("transaction.db")).unwrap();
    let transaction_result: Result<(), SQLError> = transaction_engine.transaction(|engine| {
        end_backend_transaction_early(engine);
        panic!("callback panic");
    });
    let transaction_error = transaction_result.unwrap_err();
    assert_combined_panic_and_rollback_error(&transaction_error.to_string());
    assert_eq!(transaction_engine.transaction_depth(), 0);

    let storage_engine = Engine::open(&directory.path().join("storage.db")).unwrap();
    let storage_result: StorageBackendResult<()> = storage_engine
        .with_implicit_storage_transaction(|engine| {
            end_backend_transaction_early(engine);
            panic!("callback panic");
        });
    let storage_error = storage_result.unwrap_err();
    assert_combined_panic_and_rollback_error(&storage_error.to_string());
    assert_eq!(storage_engine.transaction_depth(), 0);

    let string_engine = Engine::open(&directory.path().join("string.db")).unwrap();
    let string_result: Result<(), String> =
        string_engine.with_implicit_string_transaction(|engine| {
            end_backend_transaction_early(engine);
            panic!("callback panic");
        });
    let string_error = string_result.unwrap_err();
    assert_combined_panic_and_rollback_error(&string_error);
    assert_eq!(string_engine.transaction_depth(), 0);
}

#[test]
fn waiting_writer_refreshes_when_sqlite_commit_precedes_epoch_publication() {
    for create_sql in [
        "CREATE TABLE fresh.items (id INTEGER)",
        "CREATE TABLE fresh.items AS SELECT 1 AS id",
    ] {
        let directory = tempfile::tempdir().unwrap();
        let root = Engine::open(&directory.path().join("catalog-race.db")).unwrap();
        let writer = root.new_session().unwrap();
        let waiter = root.new_session().unwrap();

        writer.begin().unwrap();
        assert!(!waiter.has_schema("fresh").unwrap());
        writer.sql("CREATE SCHEMA fresh", &[]).unwrap();

        let (started_tx, started_rx) = mpsc::channel();
        let (done_tx, done_rx) = mpsc::channel();
        let waiting_thread = std::thread::spawn(move || {
            started_tx.send(()).unwrap();
            let result = waiter.sql(create_sql, &[]);
            done_tx.send(result).unwrap();
        });
        started_rx.recv_timeout(Duration::from_secs(2)).unwrap();
        match done_rx.recv_timeout(Duration::from_millis(200)) {
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(error) => panic!("waiting writer result channel failed early: {error}"),
            Ok(result) => panic!("waiting writer completed before writer release: {result:?}"),
        }

        // End the physical transaction without publishing the shared epoch. This deterministically models the interval after SQLite COMMIT has released its writer lock but before Engine::commit publishes it. The logical writer registration goes with it, exactly as the real commit path releases the session's locks before publication.
        writer
            .storage
            .backend
            .as_ref()
            .unwrap()
            .commit_transaction()
            .unwrap();
        writer.row_locks.release_session(writer.session_id);

        done_rx
            .recv_timeout(Duration::from_secs(2))
            .unwrap()
            .unwrap();
        waiting_thread.join().unwrap();
        writer.session.transactions.lock().clear();
        assert!(root
            .new_session()
            .unwrap()
            .has_table("fresh.items")
            .unwrap());
    }
}

#[test]
fn unchanged_persistent_statements_keep_their_loaded_catalog_snapshot() {
    let directory = tempfile::tempdir().unwrap();
    let engine = Engine::open(&directory.path().join("stable-snapshot.db")).unwrap();
    engine
        .sql("CREATE TABLE items (id INTEGER PRIMARY KEY)", &[])
        .unwrap();

    // Consume the catalog/data generations published by CREATE TABLE.
    engine.sql("SELECT id FROM items", &[]).unwrap();
    let before = engine.require_table("items").unwrap();

    engine.sql("SELECT id FROM items", &[]).unwrap();
    let after = engine.require_table("items").unwrap();

    assert!(
        std::sync::Arc::ptr_eq(&before, &after),
        "an unchanged statement rebuilt the complete persistent catalog"
    );
}

#[test]
fn compressed_catalog_writer_fence_releases_reader_before_waiting() {
    let directory = tempfile::tempdir().unwrap();
    let writer = Engine::open_compressed(
        &directory.path().join("catalog-fence.db"),
        uqa_storage_sqlite::SQLiteCompressionOptions::default(),
    )
    .unwrap();
    writer
        .sql(
            "CREATE TABLE items (id INTEGER); INSERT INTO items VALUES (1)",
            &[],
        )
        .unwrap();
    let waiter = writer.new_session().unwrap();
    for engine in [&writer, &waiter] {
        engine.release_automatic_statistics_client();
        engine
            .session
            .statistics_worker
            .store(true, std::sync::atomic::Ordering::Release);
    }
    writer
        .sql("BEGIN; INSERT INTO items VALUES (2)", &[])
        .unwrap();
    waiter.begin().unwrap();
    waiter.sql("SAVEPOINT before_fence", &[]).unwrap();
    let waiter_id = waiter.session_id;
    let waiting_thread = std::thread::spawn(move || {
        let result = waiter.fence_catalog_writer_and_refresh_snapshot();
        (waiter, result)
    });
    let deadline = std::time::Instant::now() + Duration::from_secs(2);
    while !writer.row_locks.waiting_for_backend_writer(waiter_id) {
        assert!(
            std::time::Instant::now() < deadline,
            "catalog fence did not wait"
        );
        std::thread::yield_now();
    }
    let committed = writer.sql("COMMIT", &[]);
    let (waiter, fenced) = waiting_thread.join().unwrap();
    committed.unwrap();
    fenced.unwrap();
    waiter.sql("ROLLBACK TO before_fence", &[]).unwrap();
    assert_eq!(
        integer_column(
            &waiter.sql("SELECT id FROM items ORDER BY id", &[]).unwrap(),
            "id"
        ),
        [1, 2]
    );
    waiter.commit().unwrap();
}

#[test]
fn compressed_write_refresh_uses_the_pinned_transaction_connection() {
    let directory = tempfile::tempdir().unwrap();
    let engine = Engine::open_compressed(
        &directory.path().join("compressed-write.db"),
        uqa_storage_sqlite::SQLiteCompressionOptions::default(),
    )
    .unwrap();

    engine
        .sql("CREATE TABLE items (id INTEGER PRIMARY KEY)", &[])
        .unwrap();
    engine
        .sql("INSERT INTO items (id) VALUES (1)", &[])
        .unwrap();
    let result = engine.sql("SELECT id FROM items", &[]).unwrap();
    assert_eq!(result.rows.len(), 1);
}

#[test]
fn compressed_fixed_snapshot_releases_reader_locks_and_preserves_repeatable_read() {
    let directory = tempfile::tempdir().unwrap();
    let root = Engine::open_compressed(
        &directory.path().join("compressed-fixed-snapshot.db"),
        uqa_storage_sqlite::SQLiteCompressionOptions::default(),
    )
    .unwrap();
    root.sql(
        "CREATE TABLE items (id INTEGER PRIMARY KEY, value INTEGER)",
        &[],
    )
    .unwrap();
    root.sql("INSERT INTO items VALUES (1, 10), (2, 20)", &[])
        .unwrap();
    let reader = root.new_session().unwrap();
    let writer = root.new_session().unwrap();

    reader
        .sql("BEGIN ISOLATION LEVEL REPEATABLE READ", &[])
        .unwrap();
    assert_eq!(
        integer_column(
            &reader
                .sql("SELECT value FROM items ORDER BY id", &[])
                .unwrap(),
            "value",
        ),
        [10, 20]
    );
    writer
        .sql("UPDATE items SET value = 11 WHERE id = 1", &[])
        .unwrap();
    assert_eq!(
        integer_column(
            &writer
                .sql("SELECT value FROM items ORDER BY id", &[])
                .unwrap(),
            "value",
        ),
        [11, 20]
    );
    reader
        .sql("UPDATE items SET value = 21 WHERE id = 2", &[])
        .unwrap();
    assert_eq!(
        integer_column(
            &reader
                .sql("SELECT value FROM items ORDER BY id", &[])
                .unwrap(),
            "value",
        ),
        [10, 21]
    );
    reader.sql("COMMIT", &[]).unwrap();
    let observer = root.new_session().unwrap();
    assert_eq!(
        integer_column(
            &observer
                .sql("SELECT value FROM items ORDER BY id", &[])
                .unwrap(),
            "value",
        ),
        [11, 21]
    );
}

#[test]
fn pinned_reader_defers_sibling_catalog_epochs_until_transaction_end() {
    let directory = tempfile::tempdir().unwrap();
    let root = Engine::open(&directory.path().join("pinned-reader.db")).unwrap();
    let reader = root.new_session().unwrap();
    let writer = root.new_session().unwrap();

    {
        let characteristics = reader.default_transaction_characteristics();
        let mut stack = reader.session.transactions.lock();
        reader
            .begin_transaction_frame(
                &mut stack,
                true,
                true,
                TransactionFrameKind::ExplicitBlock,
                characteristics,
            )
            .unwrap();
    }
    assert!(!reader.has_schema("later").unwrap());
    writer.sql("CREATE SCHEMA later", &[]).unwrap();
    writer.create_graph("later_graph").unwrap();

    assert!(!reader.has_schema("later").unwrap());
    assert!(!reader.has_graph("later_graph").unwrap());
    reader.commit().unwrap();

    assert!(reader.has_schema("later").unwrap());
    assert!(reader.has_graph("later_graph").unwrap());
}