uqa-sql 0.6.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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

use super::*;
use crate::ast::{
    FunctionBinding, FunctionOrderSyntax, FunctionResolutionError, OperatorResolutionError,
    RoutineInvocationBinding, RoutineVariadicMode, Statement,
};
use crate::plan::ExpressionPlan;
use uqa_core::{memory::MemoryError, Value};

fn expression(sql: &str) -> Expr {
    let Statement::Select(mut query) = crate::compile(sql).unwrap().remove(0) else {
        panic!("expected SELECT expression");
    };
    query.projections.remove(0).expr
}

#[test]
fn reference_origins_survive_owned_and_admitted_plan_production() {
    let budget = MemoryBudget::new(1 << 20);
    let cancellation = CancellationToken::new();
    let control = uqa_core::memory::ProductionControl::new(&budget, &cancellation, &cancellation);
    for sql in [
        "SELECT a",
        "SELECT t.a",
        "SELECT t.*",
        "/* ν•œκΈ€πŸ™‚ */ SELECT t.a",
    ] {
        let source = expression(sql);
        let expected = u32::try_from(sql[..=sql.rfind(' ').unwrap()].chars().count() + 1).unwrap();
        assert_eq!(source.reference_position(), Some(expected));
        let owned = ExpressionPlan::lower(source.clone());
        assert_eq!(owned.scalar.reference_position(), Some(expected));
        let admitted =
            ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation)
                .unwrap();
        assert_eq!(admitted.reference_position(), Some(expected));
        let copied = admitted.clone_with_control(&control).unwrap();
        assert_eq!(copied.reference_position(), Some(expected));
    }
    assert_eq!(budget.used(), 0);
}

#[test]
fn controlled_column_lowering_preserves_existing_scalar_shapes_and_input() {
    let budget = MemoryBudget::new(1 << 20);
    let original = CancellationToken::new();
    let invoking = CancellationToken::new();
    for sql in [
        "SELECT a",
        "SELECT t.a",
        "SELECT 'literal'",
        "SELECT $1",
        "SELECT concat(a, 'ν•œπŸ™‚')",
        "SELECT ARRAY[1, 2, 3]",
        "SELECT ROW(a, 'text')",
        "SELECT a + 1",
        "SELECT -a",
        "SELECT NOT a",
        "SELECT a AND b AND c",
        "SELECT a OR b OR c",
        "SELECT a IS NOT NULL",
        "SELECT a BETWEEN 1 AND 2",
        "SELECT a BETWEEN SYMMETRIC 1 AND 2",
        "SELECT a NOT BETWEEN SYMMETRIC 1 AND 2",
        "SELECT a NOT IN (1, 2, 3)",
        "SELECT CASE a WHEN 1 THEN 'first' ELSE 'last' END",
        "SELECT CASE WHEN a THEN 'yes' END",
        "SELECT CAST(a AS text)",
        "SELECT sum(a ORDER BY b DESC NULLS FIRST) FILTER (WHERE c)",
        "SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY a DESC NULLS LAST) FILTER (WHERE b)",
        "SELECT sum(a) OVER (PARTITION BY b ORDER BY c ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING)",
        "SELECT sum(a) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)",
        "SELECT sum(a) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)",
    ] {
        let source = expression(sql);
        let before = source.clone();
        let expected = ExpressionPlan::lower(source.clone());
        let lowered =
            ExpressionPlan::lower_column_budgeted(&source, &budget, &original, &invoking).unwrap();
        assert_eq!(*lowered, expected.scalar, "{sql}");
        assert_eq!(source, before, "{sql}");
        assert_eq!(budget.used(), lowered.reserved_bytes());
        drop(lowered);
        assert_eq!(budget.used(), 0);
    }
}

#[test]
fn function_order_syntax_preserves_direct_arguments_through_lowering() {
    for (sql, expected, direct_count, order_count) in [
        ("SELECT f(a)", FunctionOrderSyntax::Ordinary, 1, 0),
        (
            "SELECT f(a ORDER BY b DESC NULLS FIRST)",
            FunctionOrderSyntax::Ordinary,
            1,
            1,
        ),
        (
            "SELECT f(a) WITHIN GROUP (ORDER BY b DESC NULLS FIRST)",
            FunctionOrderSyntax::WithinGroup,
            1,
            1,
        ),
        (
            "SELECT mode() WITHIN GROUP (ORDER BY b)",
            FunctionOrderSyntax::WithinGroup,
            0,
            1,
        ),
    ] {
        let source = expression(sql);
        let Expr::Func {
            args,
            order_by,
            order_syntax,
            ..
        } = &source
        else {
            panic!("function expression expected");
        };
        assert_eq!(*order_syntax, expected, "{sql}");
        assert_eq!(args.len(), direct_count, "{sql}");
        assert_eq!(order_by.len(), order_count, "{sql}");
        let ScalarExpr::Func {
            args,
            order_by,
            order_syntax,
            ..
        } = ExpressionPlan::lower(source).scalar
        else {
            panic!("function scalar expected");
        };
        assert_eq!(order_syntax, expected, "{sql}");
        assert_eq!(args.len(), direct_count, "{sql}");
        assert_eq!(order_by.len(), order_count, "{sql}");
    }
}

#[test]
fn function_order_syntax_round_trips_without_reclassifying_legacy_json() {
    for syntax in [
        FunctionOrderSyntax::Legacy,
        FunctionOrderSyntax::Ordinary,
        FunctionOrderSyntax::WithinGroup,
        FunctionOrderSyntax::Extract,
    ] {
        let mut source = expression("SELECT f(a) WITHIN GROUP (ORDER BY b)");
        let Expr::Func { order_syntax, .. } = &mut source else {
            unreachable!();
        };
        *order_syntax = syntax;
        let encoded = serde_json::to_value(&source).unwrap();
        assert_eq!(
            encoded["Func"].get("order_syntax").is_none(),
            syntax.is_legacy()
        );
        assert_eq!(serde_json::from_value::<Expr>(encoded).unwrap(), source);
        let scalar = ExpressionPlan::lower(source).scalar;
        let encoded = serde_json::to_value(&scalar).unwrap();
        assert_eq!(
            encoded["Func"].get("order_syntax").is_none(),
            syntax.is_legacy()
        );
        assert_eq!(
            serde_json::from_value::<ScalarExpr>(encoded).unwrap(),
            scalar
        );
    }
}

#[test]
fn copied_bindings_keep_invocation_identity_and_both_error_variants() {
    let budget = MemoryBudget::new(1 << 20);
    let cancellation = CancellationToken::new();
    for error in [
        FunctionResolutionError::UndefinedFunction {
            signature: "f(text)".into(),
        },
        FunctionResolutionError::Operator(Box::new(OperatorResolutionError {
            sqlstate: "42883".into(),
            message: "operator missing".into(),
        })),
    ] {
        let source = Expr::Func {
            order_syntax: crate::ast::FunctionOrderSyntax::Ordinary,
            name: "f".into(),
            binding: Some(FunctionBinding {
                object_id: Some([7; 16]),
                name: "app.f".into(),
                argument_types: vec!["text".into()],
                builtin: false,
                dispatch: None,
                invocation: Some(Box::new(RoutineInvocationBinding {
                    argument_positions: vec![1, 0],
                    argument_targets: vec!["text".into(), "integer".into()],
                    argument_sources: vec![Some("varchar".into()), None],
                    parameter_types: vec!["integer".into(), "text".into()],
                    return_type: Some("text".into()),
                    variadic_mode: RoutineVariadicMode::Expanded { parameter_index: 1 },
                })),
                value_binding: Some(Box::new(crate::ast::FunctionValueBinding::CompositeField(
                    crate::ast::CompositeFieldBinding {
                        collation_oid: Some(950),
                        type_oid: 20_001,
                        number: 2,
                        result_type: crate::ColumnType::Array(Box::new(crate::ColumnType::Named(
                            "app.result".into(),
                        ))),
                        dropped: true,
                        changed_type: Some(crate::ColumnType::Array(Box::new(
                            crate::ColumnType::Named("app.changed".into()),
                        ))),
                    },
                ))),
                resolution_error: Some(Box::new(error)),
                input_collation: None,
            }),
            args: vec![Expr::TypedLiteral {
                collation_oid: None,
                composite_source: None,
                value: Value::Str("value".into()),
                ty: "text".into(),
            }],
            distinct: false,
            order_by: Vec::new(),
            filter: None,
        };
        let lowered =
            ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation)
                .unwrap();
        assert_eq!(*lowered, ExpressionPlan::lower(source).scalar);
        assert_eq!(budget.used(), lowered.reserved_bytes());
        drop(lowered);
        assert_eq!(budget.used(), 0);
    }
}

#[test]
fn borrowed_lowering_copies_only_destination_capacity_and_owned_lowering_moves_payloads() {
    let mut text = String::with_capacity(1 << 18);
    text.push_str("small");
    let address = text.as_ptr();
    let source = Expr::Literal(Value::Str(text));
    let budget = MemoryBudget::new(16);
    let cancellation = CancellationToken::new();
    let lowered =
        ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation)
            .unwrap();
    let ScalarExpr::Literal(Value::Str(value)) = &*lowered else {
        panic!("string literal");
    };
    assert_eq!(value, "small");
    assert_ne!(value.as_ptr(), address);
    assert_eq!(lowered.reserved_bytes(), value.capacity());
    let owned = ExpressionPlan::lower(source);
    let ScalarExpr::Literal(Value::Str(value)) = owned.scalar else {
        panic!("string literal");
    };
    assert_eq!(value.as_ptr(), address);
    drop(lowered);
    assert_eq!(budget.used(), 0);
}

#[test]
fn partial_ir_failure_releases_allocations_without_disturbing_other_results() {
    let cancellation = CancellationToken::new();
    let source = Expr::Row(vec![
        Expr::Column("first".into()),
        Expr::Literal(Value::Bytes(vec![9; 16384])),
    ]);
    let budget = MemoryBudget::new(8192);
    let first = ExpressionPlan::lower_column_budgeted(
        &Expr::Literal(Value::Str("held".into())),
        &budget,
        &cancellation,
        &cancellation,
    )
    .unwrap();
    let held = budget.used();
    assert!(matches!(
        ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation),
        Err(CatalogRetentionError::Memory(MemoryError::Limit { .. }))
    ));
    assert_eq!(budget.used(), held);
    assert!(budget.peak() > held);
    assert_eq!(*first, ScalarExpr::Literal(Value::Str("held".into())));
    let Expr::Row(items) = source else {
        panic!("row source");
    };
    assert_eq!(items[1], Expr::Literal(Value::Bytes(vec![9; 16384])));
    drop(first);
    assert_eq!(budget.used(), 0);
}

#[test]
fn original_and_invoking_cancellation_abort_before_and_during_container_production() {
    for cancel_original in [true, false] {
        let budget = MemoryBudget::new(4096);
        let original = CancellationToken::new();
        let invoking = CancellationToken::new();
        let token = if cancel_original {
            &original
        } else {
            &invoking
        };
        let mut lowering = Lowering {
            control: Some(Control::new(&budget, &original, &invoking)),
        };
        let mut count = 0;
        let result = lowering.map([1, 2, 3].into_iter(), |_, value| {
            count += 1;
            token.cancel();
            Ok(value)
        });
        assert!(matches!(result, Err(CatalogRetentionError::Cancelled(_))));
        assert_eq!(count, 1);
        drop(lowering);
        assert_eq!(budget.used(), 0);
        assert!(matches!(
            ExpressionPlan::lower_column_budgeted(
                &Expr::Literal(Value::Null),
                &budget,
                &original,
                &invoking
            ),
            Err(CatalogRetentionError::Cancelled(_))
        ));
        assert_eq!(budget.used(), 0);
    }
}

#[test]
fn validated_column_subquery_errors_cleanup_and_inline_literals_need_no_headroom() {
    let cancellation = CancellationToken::new();
    let budget = MemoryBudget::new(1 << 20);
    for sql in [
        "SELECT (SELECT 1)",
        "SELECT EXISTS (SELECT 1)",
        "SELECT 1 IN (SELECT 1)",
    ] {
        let source = expression(sql);
        assert!(matches!(
            ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation),
            Err(CatalogRetentionError::UnexpectedSubquery)
        ));
        assert_eq!(budget.used(), 0);
        assert_eq!(ExpressionPlan::lower(source).subqueries.len(), 1);
    }
    let zero = MemoryBudget::new(0);
    for source in [
        Expr::Literal(Value::Null),
        Expr::Literal(Value::Int(3)),
        Expr::Array(Vec::new()),
        Expr::Param(1),
    ] {
        let result =
            ExpressionPlan::lower_column_budgeted(&source, &zero, &cancellation, &cancellation)
                .unwrap();
        assert_eq!(result.reserved_bytes(), 0);
    }
    assert_eq!(zero.peak(), 0);
}

#[test]
fn retained_ir_lease_equals_destination_containers_and_leaf_payloads() {
    let source = Expr::Row(vec![
        Expr::Column("column".into()),
        Expr::Cast {
            implicit: false,
            expr: Box::new(Expr::Literal(Value::Str("datum".into()))),
            ty: "text".into(),
        },
    ]);
    let budget = MemoryBudget::new(4096);
    let cancellation = CancellationToken::new();
    let lowered =
        ExpressionPlan::lower_column_budgeted(&source, &budget, &cancellation, &cancellation)
            .unwrap();
    let ScalarExpr::Row(items) = &*lowered else {
        panic!("row IR");
    };
    let ScalarExpr::Column(name) = &items[0] else {
        panic!("column IR");
    };
    let ScalarExpr::Cast { expr, ty, .. } = &items[1] else {
        panic!("cast IR");
    };
    let ScalarExpr::Literal(Value::Str(value)) = expr.as_ref() else {
        panic!("literal IR");
    };
    assert_eq!(
        lowered.reserved_bytes(),
        items.capacity() * size_of::<ScalarExpr>()
            + name.capacity()
            + size_of::<ScalarExpr>()
            + ty.capacity()
            + value.capacity()
    );
    assert_eq!(budget.peak(), lowered.reserved_bytes());
    drop(lowered);
    assert_eq!(budget.used(), 0);
}