uqa-engine 0.6.2

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

//! Runtime catalog readers reuse their selected inputs without freezing mutable session state.

use super::{query_scope, Engine, EngineHook, ScopedEngineHook, CATALOG_CAPTURES};
use std::sync::Arc;

use uqa_core::Value;
use uqa_sql::{ast::FunctionBinding, expr::composites::CompositeTypeCatalog};
use uqa_sql::{routines::resolution::RoutineOverloadCatalog, ColumnType};

fn lower_binding() -> FunctionBinding {
    FunctionBinding {
        name: "pg_catalog.lower".into(),
        argument_types: vec!["text".into()],
        builtin: true,
        object_id: None,
        dispatch: None,
        invocation: None,
        value_binding: None,
        resolution_error: None,
        input_collation: None,
    }
}

#[test]
fn scoped_collation_lookup_keeps_catalog_permissions_without_recapturing() {
    use uqa_sql::FunctionTypeResolver;
    let engine = Engine::new();
    engine
        .sql(
            "CREATE SCHEMA collate_private; CREATE ROLE collate_reader",
            &[],
        )
        .unwrap();
    for (role, private_state) in [("uqa", "42704"), ("collate_reader", "42501")] {
        engine.sql(&format!("SET ROLE {role}"), &[]).unwrap();
        let scope = query_scope::new_for_current_routine(&engine);
        let hook = ScopedEngineHook::new(&engine, &scope);
        scope.catalog_read_view().unwrap();
        for resolver in [&engine as &dyn FunctionTypeResolver, &hook] {
            assert_eq!(resolver.resolve_collation_name("\"C\"").unwrap(), Some(950));
            let error = resolver
                .resolve_collation_name("collate_private.\"C\"")
                .unwrap_err();
            assert_eq!(error.sqlstate(), Some(private_state));
            assert_eq!(
                resolver
                    .resolve_collation_name("collate_missing.\"C\"")
                    .unwrap_err()
                    .sqlstate(),
                Some("3F000")
            );
        }
        CATALOG_CAPTURES.set(0);
        for _ in 0..16 {
            assert_eq!(
                hook.resolve_collation_name("pg_catalog.\"C\"").unwrap(),
                Some(950)
            );
            assert_eq!(hook.resolve_collation_name("\"POSIX\"").unwrap(), Some(951));
        }
        assert_eq!(CATALOG_CAPTURES.get(), 0, "{role}");
    }
}

#[test]
fn scoped_permissions_and_overloads_reuse_catalog_inputs() {
    let engine = Engine::new();
    engine
        .sql(
            "CREATE DOMAIN amount AS integer; CREATE TABLE items (id amount)",
            &[],
        )
        .unwrap();
    let scope = query_scope::new_for_current_routine(&engine);
    let hook = ScopedEngineHook::new(&engine, &scope);
    let domains = scope.catalog_read_view().unwrap().domain_snapshot();
    let binding = lower_binding();
    CATALOG_CAPTURES.set(0);
    for _ in 0..16 {
        hook.require_builtin_execute(&binding).unwrap();
        let current = engine.routine_type_snapshot();
        assert!(Arc::ptr_eq(&domains, &current));
    }
    assert_eq!(CATALOG_CAPTURES.get(), 0);
}

#[rstest::rstest]
#[case::memory(false)]
#[case::sqlite(true)]
fn scalar_catalog_captures_do_not_grow_with_rows(#[case] persistent: bool) {
    let directory = tempfile::tempdir().unwrap();
    let engine = if persistent {
        Engine::open(&directory.path().join("catalog.db")).unwrap()
    } else {
        Engine::new()
    };
    super::catalog_capture::pause_automatic_statistics(&engine);
    engine.sql("CREATE TABLE items (id integer, note text); INSERT INTO items SELECT i, 'PAYLOAD' FROM generate_series(1, 9) AS g(i)", &[]).unwrap();
    for expression in [
        "lower(note)",
        "current_schema()",
        "current_schemas(false)",
        "to_regclass(note)",
        "to_regtype('integer')",
        "format_type(23, NULL)",
        "has_type_privilege('integer', 'USAGE')",
    ] {
        let captures = [1, 9].map(|rows| {
            engine.clear_regtype_output_cache();
            CATALOG_CAPTURES.set(0);
            let result = engine
                .sql(
                    &format!("SELECT {expression} AS value FROM items WHERE id <= {rows}"),
                    &[],
                )
                .unwrap();
            let count = CATALOG_CAPTURES.get();
            assert_eq!(result.rows.len(), rows);
            assert!(result
                .rows
                .iter()
                .all(|row| row["value"] == result.rows[0]["value"]));
            if expression == "lower(note)" {
                assert_eq!(result.rows[0]["value"], Value::Str("payload".into()));
            }
            count
        });
        assert!(
            captures[1] <= captures[0],
            "{expression}: captures grew with rows: {captures:?}"
        );
    }
}

#[rstest::rstest]
#[case::memory(false)]
#[case::sqlite(true)]
fn view_catalog_captures_do_not_grow_with_unrelated_views(#[case] persistent: bool) {
    let captures = [1, 4].map(|views| {
        let directory = tempfile::tempdir().unwrap();
        let engine = if persistent {
            Engine::open(&directory.path().join("catalog.db")).unwrap()
        } else {
            Engine::new()
        };
        super::catalog_capture::pause_automatic_statistics(&engine);
        engine.sql("CREATE TABLE items (id integer, note text, active boolean)", &[]).unwrap();
        for view in 0..views {
            engine.sql(&format!("CREATE VIEW item_view_{view} AS SELECT * FROM items"), &[]).unwrap();
        }
        CATALOG_CAPTURES.set(0);
        let result = engine.sql("SELECT column_name, is_updatable FROM information_schema.columns WHERE table_name = 'items' ORDER BY ordinal_position", &[]).unwrap();
        let count = CATALOG_CAPTURES.get();
        assert_eq!(result.rows.len(), 3);
        for (row, name) in result.rows.iter().zip(["id", "note", "active"]) {
            assert_eq!(row["column_name"], Value::Str(name.into()));
            assert_eq!(row["is_updatable"], Value::Str("YES".into()));
        }
        count
    });
    assert!(
        captures[1] <= captures[0],
        "captures grew with views: {captures:?}"
    );
}

#[test]
fn scoped_relation_descriptors_reuse_the_selected_generation() {
    let engine = Engine::new();
    engine
        .sql(
            "CREATE TABLE items (id integer); CREATE VIEW item_view AS SELECT id FROM items",
            &[],
        )
        .unwrap();
    let scope = query_scope::new_for_current_routine(&engine);
    let hook = ScopedEngineHook::new(&engine, &scope);
    for name in ["items", "item_view"] {
        let Some(ColumnType::Composite(ty)) = hook.resolve_type_name(name).unwrap() else {
            panic!("expected relation type");
        };
        CATALOG_CAPTURES.set(0);
        let descriptor = hook.composite_type(ty.oid).unwrap().unwrap();
        for _ in 0..16 {
            assert!(Arc::ptr_eq(
                &descriptor,
                &hook.composite_type(ty.oid).unwrap().unwrap()
            ));
            assert!(hook.composite_type(u32::MAX).unwrap().is_none());
        }
        assert_eq!(descriptor.attributes.len(), 1);
        assert_eq!(descriptor.attributes[0].name, "id");
        assert_eq!(CATALOG_CAPTURES.get(), 0);
    }
}

#[test]
fn scoped_runtime_catalog_observes_private_ddl_acl_and_rollback() {
    let engine = Engine::new();
    engine.sql("CREATE ROLE reader; CREATE DOMAIN amount AS integer; CREATE TABLE items (id integer); SET search_path = pending, public", &[]).unwrap();
    let scope = query_scope::new_for_current_routine(&engine);
    let hook = ScopedEngineHook::new(&engine, &scope);
    let binding = lower_binding();
    let original = hook.resolve_regtype_input("amount").unwrap();
    assert_eq!(hook.current_schema().unwrap().as_deref(), Some("public"));
    engine.sql("BEGIN; CREATE SCHEMA pending; DROP DOMAIN amount; CREATE DOMAIN amount AS text; REVOKE EXECUTE ON FUNCTION lower(text) FROM PUBLIC; SET ROLE reader", &[]).unwrap();
    assert_eq!(
        hook.require_builtin_execute(&binding)
            .unwrap_err()
            .sqlstate(),
        Some("42501")
    );
    engine.sql("RESET ROLE", &[]).unwrap();
    assert_ne!(hook.resolve_regtype_input("amount").unwrap(), original);
    assert_eq!(hook.current_schema().unwrap().as_deref(), Some("pending"));
    engine.sql("ROLLBACK; SET ROLE reader", &[]).unwrap();
    hook.require_builtin_execute(&binding).unwrap();
    assert_eq!(hook.resolve_regtype_input("amount").unwrap(), original);
    assert_eq!(hook.current_schema().unwrap().as_deref(), Some("public"));
}

#[test]
fn scoped_runtime_catalog_keeps_the_portal_generation() {
    let mut engine = Engine::new();
    engine
        .sql(
            "CREATE DOMAIN amount AS integer; CREATE TABLE items (id integer)",
            &[],
        )
        .unwrap();
    let retained = Arc::new(crate::session::RetainedCatalogSnapshot {
        durable: engine.durable.snapshot(),
        read_view: engine.catalog_read_view(),
    });
    let domains = retained.read_view.domain_snapshot();
    let original = engine.resolve_regtype_input("amount").unwrap();
    engine.sql("DROP DOMAIN amount; CREATE DOMAIN amount AS text; ALTER TABLE items ADD COLUMN later text", &[]).unwrap();
    engine.query_catalog_snapshot = Some(retained);
    let scope = query_scope::new_for_current_routine(&engine);
    let hook = ScopedEngineHook::new(&engine, &scope);
    CATALOG_CAPTURES.set(0);
    assert_eq!(hook.resolve_regtype_input("amount").unwrap(), original);
    assert!(Arc::ptr_eq(&domains, &engine.routine_type_snapshot()));
    let Some(ColumnType::Composite(ty)) = hook.resolve_type_name("items").unwrap() else {
        panic!("expected relation type");
    };
    assert_eq!(
        hook.composite_type(ty.oid)
            .unwrap()
            .unwrap()
            .attributes
            .len(),
        1
    );
    assert_eq!(CATALOG_CAPTURES.get(), 0);
}

#[test]
fn scoped_catalog_scalars_see_nested_volatile_commands() {
    let engine = Engine::new();
    engine.sql("CREATE FUNCTION nested_catalog_change(i integer) RETURNS integer VOLATILE LANGUAGE plpgsql AS $$BEGIN IF i = 1 THEN EXECUTE 'CREATE SCHEMA scoped_callbacks_new'; PERFORM set_config('search_path', 'scoped_callbacks_new, public', false); END IF; RETURN i; END$$", &[]).unwrap();
    let result = engine.sql("SELECT nested_catalog_change(i) AS id, current_schema() AS schema FROM generate_series(1, 2) AS g(i)", &[]).unwrap();
    // PostgreSQL 18.4 in Docker returns the newly created schema for both rows.
    assert_eq!(result.rows.len(), 2);
    for (row, id) in result.rows.iter().zip([1, 2]) {
        assert_eq!(row["id"], Value::Int(id));
        assert_eq!(row["schema"], Value::Str("scoped_callbacks_new".into()));
    }
}

#[test]
fn runtime_catalog_does_not_mistake_an_older_scope_for_the_current_revision() {
    let engine = Engine::new();
    engine
        .sql("SET search_path = pending, public", &[])
        .unwrap();
    let scope = query_scope::new_for_current_routine(&engine);
    engine.sql("CREATE SCHEMA pending", &[]).unwrap();
    let hook = ScopedEngineHook::new(&engine, &scope);
    assert_eq!(hook.current_schema().unwrap().as_deref(), Some("pending"));
    CATALOG_CAPTURES.set(0);
    for _ in 0..16 {
        assert_eq!(hook.current_schema().unwrap().as_deref(), Some("pending"));
    }
    assert_eq!(CATALOG_CAPTURES.get(), 0);
}

#[test]
fn scoped_runtime_catalog_refreshes_peer_ddl_in_each_isolation_level() {
    for provider in 0..3 {
        for isolation in ["READ COMMITTED", "REPEATABLE READ", "SERIALIZABLE"] {
            let directory = tempfile::tempdir().unwrap();
            let path = directory.path().join("scoped.db");
            let engine = match provider {
                0 => Engine::open(&path).unwrap(),
                1 => Engine::from_persistent_provider(Arc::new(
                    uqa_storage_sqlite::SQLiteKeyValueStorage::open(&path).unwrap(),
                ))
                .unwrap(),
                _ => Engine::from_persistent_provider(Arc::new(
                    uqa_storage_redb::RedbStorage::open(&path).unwrap(),
                ))
                .unwrap(),
            };
            engine
                .sql(
                    "CREATE TABLE items (id integer); INSERT INTO items VALUES (1)",
                    &[],
                )
                .unwrap();
            let peer = engine.new_session().unwrap();
            engine.sql(&format!("BEGIN ISOLATION LEVEL {isolation}; SELECT * FROM items; SET LOCAL search_path = pending, public"), &[]).unwrap();
            let scope = query_scope::new_for_current_routine(&engine);
            let hook = ScopedEngineHook::new(&engine, &scope);
            assert_eq!(hook.current_schema().unwrap().as_deref(), Some("public"));
            peer.sql("CREATE SCHEMA pending", &[]).unwrap();
            // PostgreSQL 18.4 observes peer DDL at the next SQL command in all three isolation levels.
            engine.sql("SELECT 1", &[]).unwrap();
            assert_eq!(
                hook.current_schema().unwrap().as_deref(),
                Some("pending"),
                "{provider}: {isolation}"
            );
            engine.sql("ROLLBACK", &[]).unwrap();
        }
    }
}

#[test]
fn native_cast_descriptors_keep_qualified_identity_and_live_domain_checks() {
    use uqa_core::memory::ProductionControl;
    use uqa_sql::expr::cast_value_with_scoped_resolution;
    let engine = Engine::new();
    engine.sql("CREATE DOMAIN public.amount AS integer CHECK (VALUE > 0); CREATE SCHEMA other_types; CREATE DOMAIN other_types.amount AS integer CHECK (VALUE < 0)", &[]).unwrap();
    let scope = query_scope::new_for_current_routine(&engine);
    let hook = ScopedEngineHook::new(&engine, &scope);
    let control = ProductionControl::uncontrolled();
    let first = cast_value_with_scoped_resolution(
        &Value::Int(1),
        Some("pg_catalog.int4"),
        "public.amount",
        Some(&hook),
        &control,
    )
    .unwrap();
    assert_eq!(*first.value, Value::Int(1));
    let retained = first.catalog.unwrap();
    engine
        .sql("SET search_path = other_types, public", &[])
        .unwrap();
    assert_eq!(
        *retained
            .evaluate(
                &Value::Int(2),
                Some("pg_catalog.int4"),
                "public.amount",
                Some(&hook),
                &control
            )
            .unwrap()
            .unwrap(),
        Value::Int(2)
    );
    assert_eq!(
        retained
            .evaluate(
                &Value::Int(-1),
                Some("pg_catalog.int4"),
                "public.amount",
                Some(&hook),
                &control
            )
            .unwrap()
            .unwrap_err()
            .sqlstate(),
        Some("23514")
    );
    let unqualified = cast_value_with_scoped_resolution(
        &Value::Int(-1),
        Some("pg_catalog.int4"),
        "amount",
        Some(&hook),
        &control,
    )
    .unwrap();
    assert_eq!(*unqualified.value, Value::Int(-1));
    assert!(unqualified.catalog.is_none());
    let new_scope = query_scope::new_for_current_routine(&engine);
    let new_hook = ScopedEngineHook::new(&engine, &new_scope);
    assert!(retained
        .evaluate(
            &Value::Int(3),
            Some("pg_catalog.int4"),
            "public.amount",
            Some(&new_hook),
            &control
        )
        .is_none());
}