uqa-sql 0.5.2

PostgreSQL-compatible SQL compiler built on libpg_query
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Caller-plan lifetime and eligibility, against `PostgreSQL` 18 `inline_function`
//! and the independently captured `routine_parser_lifetime` oracle.

use super::*;
use crate::ast::Statement;
use std::sync::{atomic::Ordering, Arc};
use uqa_core::Value;
mod fixture;
use fixture::Catalog;

fn integer(value: i64) -> ScalarExpr {
    ScalarExpr::TypedLiteral {
        value: Value::Int(value),
        ty: "integer".into(),
        bound_type: Some(ColumnType::Integer),
        parameter_index: None,
    }
}
fn value(expression: &ScalarExpr) -> Option<&Value> {
    match expression {
        ScalarExpr::Literal(value) | ScalarExpr::TypedLiteral { value, .. } => Some(value),
        _ => None,
    }
}
fn call(binding: FunctionBinding, args: Vec<ScalarExpr>) -> ScalarExpr {
    ScalarExpr::Func {
        name: binding.name.clone(),
        binding: Some(binding),
        args,
        distinct: false,
        order_by: vec![],
        filter: None,
        order_syntax: crate::ast::FunctionCallSyntax::Ordinary,
    }
}

#[test]
fn ordinary_planning_reparses_source_but_retained_replacements_keep_their_values() {
    let catalog =
        Catalog::new(r"CREATE FUNCTION fresh() RETURNS text LANGUAGE sql AS $$SELECT 'a\nb'$$");
    catalog.settings.lock().unwrap().standard_conforming_strings = false;
    let binding = catalog.binding("fresh", &[]);
    let first = catalog
        .context()
        .prepare(&binding, &[], &[])
        .unwrap()
        .unwrap();
    assert_eq!(value(&first.expression), Some(&Value::Str("a\nb".into())));
    assert_eq!(catalog.take_notices().len(), 1);
    let second = catalog
        .context()
        .prepare(&binding, &[], &[])
        .unwrap()
        .unwrap();
    assert_eq!(value(&second.expression), value(&first.expression));
    assert_eq!(catalog.take_notices().len(), 1);
    catalog.settings.lock().unwrap().standard_conforming_strings = true;
    let changed = catalog
        .context()
        .prepare(&binding, &[], &[])
        .unwrap()
        .unwrap();
    assert_eq!(
        value(&changed.expression),
        Some(&Value::Str(r"a\nb".into()))
    );
    assert_eq!(catalog.take_notices().len(), 0);
    assert_eq!(value(&first.expression), Some(&Value::Str("a\nb".into())));
    assert_eq!(catalog.evaluations.load(Ordering::SeqCst), 0);
}

#[test]
fn metadata_exclusions_precede_source_parse_but_multiple_statements_are_parsed() {
    let catalog = Catalog::new(
        r"CREATE FUNCTION configured() RETURNS text LANGUAGE sql SET search_path=public AS $$SELECT 'a\nb'$$;
        CREATE FUNCTION secured() RETURNS text LANGUAGE sql SECURITY DEFINER AS $$SELECT 'a\nb'$$;
        CREATE FUNCTION set_result() RETURNS SETOF text LANGUAGE sql AS $$SELECT 'a\nb'$$;
        CREATE FUNCTION multiple() RETURNS text LANGUAGE sql AS $$SELECT 1; SELECT 'a\nb'$$",
    );
    catalog.settings.lock().unwrap().standard_conforming_strings = false;
    for name in ["configured", "secured", "set_result"] {
        assert!(catalog
            .context()
            .prepare(&catalog.binding(name, &[]), &[], &[])
            .unwrap()
            .is_none());
    }
    assert_eq!(catalog.take_notices().len(), 0);
    for _ in 0..2 {
        assert!(catalog
            .context()
            .prepare(&catalog.binding("multiple", &[]), &[], &[])
            .unwrap()
            .is_none());
        assert_eq!(catalog.take_notices().len(), 1);
    }
}

#[test]
fn authority_recursion_and_strict_null_do_not_parse_source() {
    let mut catalog = Catalog::new(
        r"CREATE FUNCTION denied(x integer) RETURNS text LANGUAGE sql STRICT AS $$SELECT 'a\nb'$$",
    );
    Arc::make_mut(&mut catalog.functions[0]).def.execute_acl = Some(vec![]);
    catalog.allowed.store(false, Ordering::SeqCst);
    catalog.settings.lock().unwrap().standard_conforming_strings = false;
    let binding = catalog.binding("denied", &[Some(ColumnType::Integer)]);
    assert!(catalog
        .context()
        .prepare(&binding, &[integer(1)], &[])
        .unwrap()
        .is_none());
    let null = catalog
        .context()
        .prepare(&binding, &[ScalarExpr::Literal(Value::Null)], &[])
        .unwrap()
        .unwrap();
    assert_eq!(value(&null.expression), Some(&Value::Null));
    assert!(catalog
        .context()
        .prepare(&binding, &[integer(1)], &[binding.object_id.unwrap()])
        .unwrap()
        .is_none());
    assert_eq!(catalog.take_notices().len(), 0);
}

#[test]
fn immutable_constant_calls_use_existing_executor_before_inline_attempts() {
    let catalog = Catalog::new("CREATE FUNCTION folded(x integer) RETURNS integer LANGUAGE sql IMMUTABLE AS $$SELECT x+1$$");
    let binding = catalog.binding("folded", &[Some(ColumnType::Integer)]);
    let folded = catalog
        .context()
        .prepare(&binding, &[integer(7)], &[])
        .unwrap()
        .unwrap();
    assert_eq!(value(&folded.expression), Some(&Value::Int(37)));
    assert_eq!(catalog.evaluations.load(Ordering::SeqCst), 1);
    assert_eq!(catalog.take_notices().len(), 0);
}

#[test]
fn typed_substitution_uses_invocation_positions_and_preserves_caller_parameters() {
    let catalog = Catalog::new("CREATE FUNCTION reordered(a integer,b integer DEFAULT 8) RETURNS integer LANGUAGE sql AS $$SELECT a-b$$");
    let mut binding = catalog.binding(
        "reordered",
        &[Some(ColumnType::Integer), Some(ColumnType::Integer)],
    );
    binding
        .invocation
        .as_mut()
        .unwrap()
        .argument_positions
        .swap(0, 1);
    let replacement = catalog
        .context()
        .prepare(&binding, &[ScalarExpr::Param(2), integer(9)], &[])
        .unwrap()
        .unwrap();
    let ScalarExpr::Binary { lhs, rhs, .. } = replacement.expression else {
        panic!("expected subtraction")
    };
    assert_eq!(value(&lhs), Some(&Value::Int(9)));
    assert!(matches!(*rhs, ScalarExpr::Param(2)));
    let omitted = catalog.binding("reordered", &[Some(ColumnType::Integer)]);
    let replacement = catalog
        .context()
        .prepare(&omitted, &[integer(4)], &[])
        .unwrap()
        .unwrap();
    let ScalarExpr::Binary { rhs, .. } = replacement.expression else {
        panic!("expected subtraction")
    };
    assert!(matches!(
        *rhs,
        ScalarExpr::Cast { .. }
            | ScalarExpr::Literal(Value::Int(8))
            | ScalarExpr::TypedLiteral {
                value: Value::Int(8),
                ..
            }
    ));
}

#[test]
fn strictness_volatility_and_multiple_uses_preserve_effects() {
    let catalog = Catalog::new("CREATE FUNCTION twice(x bigint) RETURNS bigint LANGUAGE sql AS $$SELECT x+x$$;
        CREATE FUNCTION unused(x integer) RETURNS integer LANGUAGE sql STRICT AS $$SELECT 1$$;
        CREATE FUNCTION nonstrict(x integer) RETURNS integer LANGUAGE sql STRICT AS $$SELECT coalesce(x,1)$$;
        CREATE FUNCTION between_strict(x integer) RETURNS boolean LANGUAGE sql STRICT AS $$SELECT x BETWEEN 1 AND 3$$;
        CREATE FUNCTION in_strict(x integer) RETURNS boolean LANGUAGE sql STRICT AS $$SELECT x IN (1,2)$$;
        CREATE FUNCTION stable_body() RETURNS double precision LANGUAGE sql STABLE AS $$SELECT random()$$;
        CREATE FUNCTION expensive(x bigint) RETURNS bigint LANGUAGE sql COST 11 AS $$SELECT x$$");
    let volatile = crate::plan::ExpressionPlan::lower(
        crate::compile("SELECT nextval('seq')")
            .unwrap()
            .into_iter()
            .find_map(|statement| {
                if let Statement::Select(select) = statement {
                    Some(select.projections[0].expr.clone())
                } else {
                    None
                }
            })
            .unwrap(),
    )
    .scalar;
    let binding = catalog.binding("twice", &[Some(ColumnType::BigInteger)]);
    assert!(catalog
        .context()
        .prepare(&binding, &[volatile], &[])
        .unwrap()
        .is_none());
    let expensive = call(
        catalog.binding("expensive", &[Some(ColumnType::BigInteger)]),
        vec![ScalarExpr::Param(1)],
    );
    assert!(catalog
        .context()
        .prepare(&binding, &[expensive], &[])
        .unwrap()
        .is_none());
    for name in ["unused", "nonstrict", "between_strict", "in_strict"] {
        assert!(catalog
            .context()
            .prepare(
                &catalog.binding(name, &[Some(ColumnType::Integer)]),
                &[integer(1)],
                &[]
            )
            .unwrap()
            .is_none());
    }
    assert!(catalog
        .context()
        .prepare(&catalog.binding("stable_body", &[]), &[], &[])
        .unwrap()
        .is_none());
}

#[test]
fn retained_identity_and_return_coercion_survive_inline_analysis() {
    let catalog = Catalog::new("CREATE FUNCTION inner_value(x integer) RETURNS integer LANGUAGE sql AS $$SELECT x$$;
        CREATE FUNCTION outer_value(x integer) RETURNS bigint LANGUAGE sql AS $$SELECT inner_value(x)$$");
    let binding = catalog.binding("outer_value", &[Some(ColumnType::Integer)]);
    let replacement = catalog
        .context()
        .prepare(&binding, &[ScalarExpr::Param(3)], &[])
        .unwrap()
        .unwrap();
    let ScalarExpr::Cast { expr, ty, .. } = replacement.expression else {
        panic!("result assignment is required")
    };
    assert_eq!(
        ColumnType::from_sql_name(&ty).unwrap(),
        ColumnType::BigInteger
    );
    let ScalarExpr::Func {
        binding: Some(selected),
        args,
        ..
    } = *expr
    else {
        panic!("selected nested call")
    };
    assert_eq!(selected.object_id, catalog.functions[0].def.object_id);
    assert!(matches!(args.as_slice(), [ScalarExpr::Param(3)]));
}

#[test]
fn ordinary_cache_replanning_is_separate_from_the_execution_body_cache() {
    let catalog = Catalog::new("CREATE FUNCTION fresh() RETURNS integer LANGUAGE sql AS $$SELECT 1$$;
        CREATE FUNCTION configured() RETURNS integer LANGUAGE sql SET search_path=public AS $$SELECT 1$$");
    for (name, expected) in [("fresh", true), ("configured", false)] {
        let mut plan = UnifiedPlan::lower(crate::compile("SELECT 1").unwrap().remove(0));
        plan.rewrite_scalar_expressions(&mut |expression| {
            *expression = ScalarExpr::Cast {
                implicit: false,
                expr: Box::new(call(catalog.binding(name, &[]), vec![])),
                ty: "bigint".into(),
            };
        });
        assert_eq!(catalog.context().requires_replanning(&plan), expected);
    }
}

#[test]
fn materialized_variadic_arguments_keep_the_selected_array_signature() {
    let catalog = Catalog::new("CREATE FUNCTION packed(VARIADIC items integer[]) RETURNS integer[] LANGUAGE sql AS $$SELECT items$$");
    let binding = catalog.binding(
        "packed",
        &[Some(ColumnType::Integer), Some(ColumnType::Integer)],
    );
    let (selected, arguments) = catalog
        .context()
        .materialize_call(&binding, &[integer(4), integer(7)])
        .unwrap()
        .unwrap();
    let signature = crate::function_call_argument_signature(
        &arguments,
        &crate::RowSchema::default(),
        &[],
        Some(&catalog),
    )
    .unwrap();
    assert!(signature.2);
    let resolved = crate::FunctionTypeResolver::resolve_function_type(
        &catalog,
        "packed",
        Some(&selected),
        &signature.0,
        &signature.1,
        signature.2,
    )
    .unwrap();
    assert_eq!(
        resolved,
        Some(ColumnType::Array(Box::new(ColumnType::Integer)))
    );
    let replacement = catalog
        .context()
        .prepare(&selected, &arguments, &[])
        .unwrap()
        .unwrap();
    assert!(matches!(replacement.expression, ScalarExpr::Cast { .. }));
}

#[test]
fn stable_operator_and_output_casts_prevent_immutable_body_expansion() {
    let catalog = Catalog::new("CREATE FUNCTION zone_add(x timestamptz) RETURNS timestamptz LANGUAGE sql IMMUTABLE AS $$SELECT x+interval '1 day'$$;
        CREATE FUNCTION zone_compare(x timestamptz) RETURNS boolean LANGUAGE sql IMMUTABLE AS $$SELECT x=timestamp '2024-01-01'$$;
        CREATE FUNCTION zone_output(x timestamptz) RETURNS text LANGUAGE sql IMMUTABLE AS $$SELECT x::text$$;
        CREATE FUNCTION local_add(x timestamp) RETURNS timestamp LANGUAGE sql IMMUTABLE AS $$SELECT x+interval '1 day'$$");
    for name in ["zone_add", "zone_compare", "zone_output"] {
        assert!(
            catalog
                .context()
                .prepare(
                    &catalog.binding(name, &[Some(ColumnType::TimestampTz)]),
                    &[ScalarExpr::Param(1)],
                    &[]
                )
                .unwrap()
                .is_none(),
            "{name}"
        );
    }
    assert!(catalog
        .context()
        .prepare(
            &catalog.binding("local_add", &[Some(ColumnType::Timestamp)]),
            &[ScalarExpr::Param(1)],
            &[]
        )
        .unwrap()
        .is_some());
}

#[test]
fn expression_mutability_uses_its_own_column_types() {
    use crate::ast::FunctionVolatility::{Immutable, Stable};
    let catalog = Catalog::new("");
    for (ty, second, expression, expected) in [
        ("integer", "integer", "v + 1", Immutable),
        ("integer", "integer", "v::text", Immutable),
        ("timestamp", "integer", "v::timestamptz", Stable),
        ("timestamptz", "integer", "v::timestamp", Stable),
        ("timestamp", "interval", "v + w", Immutable),
        ("timestamptz", "interval", "v + w", Stable),
        ("timestamptz", "timestamp", "v < w", Stable),
        ("timestamptz", "timestamp", "v IN (w)", Stable),
        ("timestamptz", "timestamp", "v BETWEEN w AND w", Stable),
    ] {
        let Statement::Select(query) = crate::compile(&format!("SELECT {expression}"))
            .unwrap()
            .remove(0)
        else {
            panic!("scalar query");
        };
        let scalar = crate::plan::ExpressionPlan::lower(query.projections[0].expr.clone()).scalar;
        let schema = crate::RowSchema::with_types(
            vec!["v".into(), "w".into()],
            vec![
                Some(ColumnType::from_sql_name(ty).unwrap()),
                Some(ColumnType::from_sql_name(second).unwrap()),
            ],
        );
        let scalar = crate::type_resolution::bind_type_introspection_with_resolver(
            scalar,
            &schema,
            &[],
            &catalog,
        );
        assert_eq!(
            catalog
                .context()
                .expression_volatility(&scalar, &schema)
                .unwrap(),
            expected,
            "{ty}: {expression}"
        );
    }
}

#[test]
fn assigned_default_type_does_not_replace_the_default_expression_type() {
    for (declaration, target) in [
        (
            "CREATE FUNCTION f(v integer DEFAULT 1.6) RETURNS integer LANGUAGE SQL AS 'SELECT v'",
            ColumnType::Integer,
        ),
        (
            "CREATE FUNCTION f(v text DEFAULT 12) RETURNS text LANGUAGE SQL AS 'SELECT v'",
            ColumnType::Text,
        ),
    ] {
        let mut catalog = Catalog::new(declaration);
        Arc::make_mut(&mut catalog.functions[0]).def.params[0].default_type =
            Some(crate::ast::RoutineDefaultType::Concrete(target.clone()));
        let expanded = catalog
            .context()
            .prepare(&catalog.binding("f", &[]), &[], &[])
            .unwrap()
            .unwrap();
        assert_eq!(
            crate::scalar_type(&expanded.expression, &crate::RowSchema::default(), &[]).unwrap(),
            Some(target)
        );
        assert!(matches!(
            expanded.expression,
            ScalarExpr::Cast { implicit: true, .. }
        ));
    }
}