uqa-storage-sqlite 0.5.4

SQLite catalog, indexes, compressed storage, graph and key/value providers
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

use super::*;

mod column_acls;
mod foreign_table_ownership;
mod index_relations;
mod log_count;
mod schema_security;
mod schema_version;
mod sequences;
mod table_ownership;
mod tuple_metadata;
mod view_ownership;

#[test]
fn migration_is_idempotent() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let _cat1 = Catalog::open(mc.clone()).unwrap();
    // Reopen on the same handle: should not re-run migrations or
    // raise an error.
    let _cat2 = Catalog::open(mc).unwrap();
}

#[test]
fn migration_15_creates_document_blob_storage_for_existing_catalogs() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let _current = Catalog::open(mc.clone()).unwrap();
    mc.with(|conn| {
        conn.execute("DROP TABLE _document_blobs", [])?;
        conn.execute(
            "UPDATE _metadata SET value = '14' WHERE key = 'schema_version'",
            [],
        )?;
        Ok(())
    })
    .unwrap();

    let _upgraded = Catalog::open(mc.clone()).unwrap();
    mc.with(|conn| {
        let count: u32 = conn.query_row(
            "SELECT COUNT(*) FROM sqlite_master
             WHERE type = 'table' AND name = '_document_blobs'",
            [],
            |row| row.get(0),
        )?;
        assert_eq!(count, 1);
        Ok(())
    })
    .unwrap();
}

#[test]
fn migration_16_adds_backward_compatible_table_constraints() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(mc.clone()).unwrap();
    current
        .save_table(&TableSchema {
            relation: RelationIdentity::new("public", "legacy"),
            security: uqa_storage::RelationSecurityRow::legacy("uqa"),
            object_id: [1; 16],
            storage_generation: [1; 16],
            analyzer_json: "{}".into(),
            fts_fields: Vec::new(),
            vector_fields: Vec::new(),
            columns_json: "[]".into(),
            constraints_json: String::new(),
        })
        .unwrap();
    drop(current);
    mc.with(|conn| {
        conn.execute("ALTER TABLE _tables DROP COLUMN constraints", [])?;
        conn.execute(
            "UPDATE _metadata SET value = '15' WHERE key = 'schema_version'",
            [],
        )?;
        Ok(())
    })
    .unwrap();

    let upgraded = Catalog::open(mc).unwrap();
    let schemas = upgraded.load_tables().unwrap();
    assert_eq!(schemas.len(), 1);
    assert_eq!(schemas[0].relation.qualified_name(), "public.legacy");
    assert_eq!(schemas[0].constraints_json, "");
}

#[test]
fn migration_24_adds_persistent_table_storage_generations() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(mc.clone()).unwrap();
    current
        .save_table(&TableSchema {
            relation: RelationIdentity::new("public", "legacy_generation"),
            security: uqa_storage::RelationSecurityRow::legacy("uqa"),
            object_id: [7; 16],
            storage_generation: [7; 16],
            analyzer_json: "{}".into(),
            fts_fields: Vec::new(),
            vector_fields: Vec::new(),
            columns_json: "[]".into(),
            constraints_json: String::new(),
        })
        .unwrap();
    drop(current);
    mc.with(|connection| {
        connection.execute("ALTER TABLE _tables DROP COLUMN storage_generation", [])?;
        connection.execute(
            "UPDATE _metadata SET value = '23' WHERE key = 'schema_version'",
            [],
        )?;
        Ok(())
    })
    .unwrap();

    let upgraded = Catalog::open(mc).unwrap();
    let mut schema = upgraded.load_tables().unwrap().remove(0);
    assert_eq!(schema.storage_generation, [0; 16]);
    schema.storage_generation = [9; 16];
    upgraded.save_table(&schema).unwrap();
    assert_eq!(
        upgraded.load_tables().unwrap()[0].storage_generation,
        [9; 16]
    );
}

#[test]
fn migration_24_preserves_a_storage_generation_installed_before_its_version_marker() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(mc.clone()).unwrap();
    current
        .save_table(&TableSchema {
            relation: RelationIdentity::new("public", "early_generation"),
            security: uqa_storage::RelationSecurityRow::legacy("uqa"),
            object_id: [11; 16],
            storage_generation: [11; 16],
            analyzer_json: "{}".into(),
            fts_fields: Vec::new(),
            vector_fields: Vec::new(),
            columns_json: "[]".into(),
            constraints_json: String::new(),
        })
        .unwrap();
    drop(current);
    mc.with(|connection| {
        connection.execute(
            "UPDATE _metadata SET value = '23' WHERE key = 'schema_version'",
            [],
        )?;
        Ok(())
    })
    .unwrap();

    let upgraded = Catalog::open(mc.clone()).unwrap();
    assert_eq!(
        upgraded.load_tables().unwrap()[0].storage_generation,
        [11; 16]
    );
    mc.with(|connection| {
        let version: String = connection.query_row(
            "SELECT value FROM _metadata WHERE key = 'schema_version'",
            [],
            |row| row.get(0),
        )?;
        assert_eq!(version, CURRENT_SCHEMA_VERSION.to_string());
        Ok(())
    })
    .unwrap();
}

#[test]
fn migration_25_adds_persistent_table_object_identities() {
    let mc = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(mc.clone()).unwrap();
    current
        .save_table(&TableSchema {
            relation: RelationIdentity::new("public", "legacy_object"),
            security: uqa_storage::RelationSecurityRow::legacy("uqa"),
            object_id: [7; 16],
            storage_generation: [8; 16],
            analyzer_json: "{}".into(),
            fts_fields: Vec::new(),
            vector_fields: Vec::new(),
            columns_json: "[]".into(),
            constraints_json: String::new(),
        })
        .unwrap();
    drop(current);
    mc.with(|connection| {
        connection.execute("ALTER TABLE _tables DROP COLUMN object_id", [])?;
        connection.execute(
            "UPDATE _metadata SET value = '24' WHERE key = 'schema_version'",
            [],
        )?;
        Ok(())
    })
    .unwrap();

    let upgraded = Catalog::open(mc).unwrap();
    let mut schema = upgraded.load_tables().unwrap().remove(0);
    assert_eq!(schema.object_id, [0; 16]);
    schema.object_id = [9; 16];
    upgraded.save_table(&schema).unwrap();
    assert_eq!(upgraded.load_tables().unwrap()[0].object_id, [9; 16]);
}

#[test]
fn migration_19_creates_complete_hnsw_storage_schema() {
    let connection = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(connection.clone()).unwrap();
    drop(current);
    connection
        .with(|conn| {
            conn.execute_batch(
                "DROP TABLE _hnsw_edges;
                 DROP TABLE _hnsw_nodes;
                 DROP TABLE _hnsw_indexes;
                 UPDATE _metadata SET value = '18' WHERE key = 'schema_version';",
            )?;
            Ok(())
        })
        .unwrap();

    let _upgraded = Catalog::open(connection.clone()).unwrap();
    connection
        .with(|conn| {
            for table in ["_hnsw_indexes", "_hnsw_nodes", "_hnsw_edges"] {
                let count: i64 = conn.query_row(
                    "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?1",
                    [table],
                    |row| row.get(0),
                )?;
                assert_eq!(count, 1, "missing HNSW migration table {table}");
            }
            let mut statement = conn.prepare("PRAGMA table_info(_hnsw_indexes)")?;
            let columns = statement
                .query_map([], |row| row.get::<_, String>(1))?
                .collect::<rusqlite::Result<std::collections::BTreeSet<_>>>()?;
            assert!(columns.contains("revision"));
            assert!(columns.contains("format_version"));
            let version: String = conn.query_row(
                "SELECT value FROM _metadata WHERE key = 'schema_version'",
                [],
                |row| row.get(0),
            )?;
            assert_eq!(version, CURRENT_SCHEMA_VERSION.to_string());
            Ok(())
        })
        .unwrap();
}

#[test]
fn migration_20_repairs_only_hnsw_rows_backed_by_legacy_ivf_metadata() {
    let connection = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(connection.clone()).unwrap();
    current
        .save_table(&empty_table("public", "legacy"))
        .unwrap();
    current
        .save_table(&empty_table("public", "native"))
        .unwrap();
    drop(current);
    connection
        .with(|conn| {
            conn.execute_batch(
                "DROP TABLE _catalog_indexes;
                 CREATE TABLE _catalog_indexes (
                     name TEXT PRIMARY KEY,
                     index_type TEXT NOT NULL,
                     table_name TEXT NOT NULL,
                     columns TEXT NOT NULL,
                     parameters TEXT NOT NULL
                 );
                 INSERT INTO _catalog_indexes
                     (name, index_type, table_name, columns, parameters)
                 VALUES
                     ('legacy_idx', 'hnsw', 'public.legacy', '[\"embedding\"]', '{}'),
                     ('native_idx', 'hnsw', 'public.native', '[\"embedding\"]', '{}');
                 INSERT INTO _ivf_indexes
                     (table_name, field, dimensions, nlist, nprobe,
                      train_threshold, state, trained_size,
                      deletes_since_train, vector_count)
                 VALUES
                     ('public.legacy', 'embedding', 2, 100, 10,
                      256, 'untrained', 0, 0, 0);
                 INSERT INTO _hnsw_indexes
                     (table_name, field, dimensions, m, ef_construction,
                      ef_search, rebuild_threshold, seed, entry_node_id,
                      max_level, next_node_id, live_count, deleted_count,
                      revision, format_version)
                 VALUES
                     ('public.native', 'embedding', 2, 16, 200,
                      64, 1000, '7', NULL, 0, 0, 0, 0, 1, 1);
                 UPDATE _metadata SET value = '19'
                  WHERE key = 'schema_version';",
            )?;
            Ok(())
        })
        .unwrap();

    let upgraded = Catalog::open(connection.clone()).unwrap();
    let rows = upgraded.load_catalog_indexes().unwrap();
    assert_eq!(
        rows.iter()
            .find(|row| row.relation.name == "legacy_idx")
            .map(|row| row.index_type.as_str()),
        Some("ivf")
    );
    assert_eq!(
        rows.iter()
            .find(|row| row.relation.name == "native_idx")
            .map(|row| row.index_type.as_str()),
        Some("hnsw")
    );
}

#[test]
fn migration_21_schedules_only_invalid_btree_fields_and_installs_guards() {
    let connection = ManagedConnection::open_in_memory().unwrap();
    let current = Catalog::open(connection.clone()).unwrap();
    drop(current);
    connection
        .with(|conn| {
            conn.execute_batch(
                "INSERT INTO _documents (table_name, doc_id, body)
                     VALUES
                     ('public.engine_meta', 1, '{\"key\":\"missing\"}'),
                     ('public.clean', 1, '{\"key\":\"kept\"}');
                 INSERT INTO _btree_indexes (table_name, field)
                     VALUES
                     ('public.engine_meta', 'key'),
                     ('public.clean', 'key');
                 INSERT INTO _btree_index_entries
                     (table_name, field, doc_id, value_json)
                     VALUES
                     ('public.clean', 'key', 1,
                      '{\"type\":\"Str\",\"value\":\"kept\"}');
                 DROP TRIGGER IF EXISTS _btree_documents_delete;
                 DROP TRIGGER IF EXISTS _btree_entries_document_insert;
                 DROP TRIGGER IF EXISTS _btree_entries_document_update;
                 DROP TRIGGER IF EXISTS _btree_documents_doc_id_update;
                 UPDATE _metadata SET value = '20'
                  WHERE key = 'schema_version';",
            )?;
            Ok(())
        })
        .unwrap();

    let _upgraded = Catalog::open(connection.clone()).unwrap();
    connection
        .with(|conn| {
            let definitions: i64 =
                conn.query_row("SELECT COUNT(*) FROM _btree_indexes", [], |row| row.get(0))?;
            let entries: i64 =
                conn.query_row("SELECT COUNT(*) FROM _btree_index_entries", [], |row| {
                    row.get(0)
                })?;
            assert_eq!(definitions, 2);
            assert_eq!(entries, 1);
            let kept: i64 = conn.query_row(
                "SELECT COUNT(*) FROM _btree_indexes
                  WHERE table_name = 'public.clean' AND field = 'key'",
                [],
                |row| row.get(0),
            )?;
            assert_eq!(kept, 1);
            let pending: i64 = conn.query_row(
                "SELECT COUNT(*) FROM _btree_index_repairs
                  WHERE table_name = 'public.engine_meta' AND field = 'key'",
                [],
                |row| row.get(0),
            )?;
            assert_eq!(pending, 1);

            let guards: i64 = conn.query_row(
                "SELECT COUNT(*) FROM sqlite_master
                  WHERE type = 'trigger' AND name IN (
                      '_btree_documents_delete',
                      '_btree_entries_document_insert',
                      '_btree_entries_document_update',
                      '_btree_documents_doc_id_update'
                  )",
                [],
                |row| row.get(0),
            )?;
            assert_eq!(guards, 4);

            conn.pragma_update(None, "foreign_keys", "OFF")?;
            conn.execute(
                "DELETE FROM _documents
                  WHERE table_name = 'public.clean' AND doc_id = 1",
                [],
            )?;
            let cascaded: i64 = conn.query_row(
                "SELECT COUNT(*) FROM _btree_index_entries
                  WHERE table_name = 'public.clean' AND doc_id = 1",
                [],
                |row| row.get(0),
            )?;
            assert_eq!(cascaded, 0);
            Ok(())
        })
        .unwrap();
}