uqa-sql 0.2.1

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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! CTAS, prepared statements, foreign relations, views, and schemas.

use super::dispatch::compile_stmt;
use super::{
    compile_expr, compile_on_commit, compile_select, extract_string, range_var_name,
    relation_persistence, validate_create_table_envelope, Expr, Node, NodeEnum, Result, SQLError,
    Statement,
};
use crate::ast::{OnCommitAction, RelationPersistence};

pub(super) fn defer_create_table(
    stmt: &pg_query::protobuf::CreateStmt,
) -> Result<crate::ast::DeferredCreateTable> {
    let relation = stmt
        .relation
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE TABLE without relation".into()))?;
    let persistence = relation_persistence(relation, "CREATE TABLE")?;
    let name = range_var_name(relation);
    if name.is_empty() {
        return Err(SQLError::Internal("CREATE TABLE without name".into()));
    }
    let definition_sql = NodeEnum::CreateStmt(stmt.clone()).deparse()?;
    Ok(crate::ast::DeferredCreateTable {
        name,
        persistence,
        definition_sql,
    })
}

struct IntoTarget {
    name: String,
    column_names: Vec<String>,
    skip_data: bool,
    persistence: RelationPersistence,
    on_commit: OnCommitAction,
    options: Vec<(String, String)>,
}

fn compile_into_target(into: &pg_query::protobuf::IntoClause, command: &str) -> Result<IntoTarget> {
    let relation = into
        .rel
        .as_ref()
        .ok_or_else(|| SQLError::Internal(format!("{command} target has no name")))?;
    let persistence = relation_persistence(relation, command)?;
    let on_commit = compile_on_commit(into.on_commit(), persistence, command)?;
    let column_names = into
        .col_names
        .iter()
        .map(extract_string)
        .collect::<Result<Vec<_>>>()?;
    if !into.access_method.is_empty() {
        return Err(SQLError::Unsupported(format!(
            "{command} USING access methods are not supported"
        )));
    }
    let options = collect_def_elem_options(&into.options)?;
    if !into.table_space_name.is_empty() {
        return Err(SQLError::Unsupported(format!(
            "{command} TABLESPACE is not supported"
        )));
    }
    if into.view_query.is_some() {
        return Err(SQLError::Unsupported(format!(
            "{command} view-query payloads are not supported"
        )));
    }
    Ok(IntoTarget {
        name: range_var_name(relation),
        column_names,
        skip_data: into.skip_data,
        persistence,
        on_commit,
        options,
    })
}

fn take_select_into_clause(
    stmt: &mut pg_query::protobuf::SelectStmt,
) -> Option<Box<pg_query::protobuf::IntoClause>> {
    stmt.into_clause
        .take()
        .or_else(|| stmt.larg.as_deref_mut().and_then(take_select_into_clause))
}

fn select_has_into_clause(stmt: &pg_query::protobuf::SelectStmt) -> bool {
    stmt.into_clause.is_some() || stmt.larg.as_deref().is_some_and(select_has_into_clause)
}

pub(super) fn compile_top_level_select(stmt: &pg_query::protobuf::SelectStmt) -> Result<Statement> {
    if !select_has_into_clause(stmt) {
        return compile_select(stmt).map(|select| Statement::Select(Box::new(select)));
    }
    let mut body = stmt.clone();
    let into = take_select_into_clause(&mut body)
        .ok_or_else(|| SQLError::Internal("SELECT INTO target disappeared".into()))?;
    let target = compile_into_target(&into, "SELECT INTO")?;
    if target.skip_data {
        return Err(SQLError::Internal(
            "SELECT INTO unexpectedly requested WITH NO DATA".into(),
        ));
    }
    Ok(Statement::CreateTableAs {
        name: target.name,
        if_not_exists: false,
        column_names: target.column_names,
        with_no_data: false,
        persistence: target.persistence,
        on_commit: target.on_commit,
        body: Box::new(compile_select(&body)?),
    })
}

pub(super) fn compile_create_table_as(
    stmt: &pg_query::protobuf::CreateTableAsStmt,
) -> Result<Statement> {
    use pg_query::protobuf::ObjectType;

    let materialized = match stmt.objtype() {
        ObjectType::ObjectTable => false,
        ObjectType::ObjectMatview => true,
        other => {
            return Err(SQLError::Unsupported(format!(
                "CREATE TABLE AS object type {other:?} is not supported"
            )));
        }
    };
    let into = stmt
        .into
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE TABLE AS without target".into()))?;
    let command = if materialized {
        "CREATE MATERIALIZED VIEW"
    } else if stmt.is_select_into {
        "SELECT INTO"
    } else {
        "CREATE TABLE AS"
    };
    let target = compile_into_target(into, command)?;
    if materialized {
        if target.persistence != RelationPersistence::Permanent {
            return Err(SQLError::Routine {
                sqlstate: "0A000".into(),
                message: "materialized views cannot be temporary or unlogged".into(),
            });
        }
        if target.on_commit != OnCommitAction::PreserveRows {
            return Err(SQLError::Routine {
                sqlstate: "42P16".into(),
                message: "ON COMMIT cannot be used on materialized views".into(),
            });
        }
    }
    let body = stmt
        .query
        .as_deref()
        .ok_or_else(|| SQLError::Internal("CREATE TABLE AS without body".into()))?;
    let inner = body
        .node
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE TABLE AS body empty".into()))?;
    let select = match inner {
        NodeEnum::SelectStmt(s) => compile_select(s)?,
        other => {
            return Err(SQLError::Unsupported(format!(
                "CREATE TABLE AS body must be SELECT, got {other:?}"
            )));
        }
    };
    if materialized {
        Ok(Statement::CreateMaterializedView {
            name: target.name,
            if_not_exists: stmt.if_not_exists,
            column_names: target.column_names,
            with_no_data: target.skip_data,
            options: validate_materialized_view_options(target.options)?,
            body: Box::new(select),
        })
    } else {
        if !target.options.is_empty() {
            return Err(SQLError::Unsupported(
                "CREATE TABLE AS storage options are not supported".into(),
            ));
        }
        Ok(Statement::CreateTableAs {
            name: target.name,
            if_not_exists: stmt.if_not_exists,
            column_names: target.column_names,
            with_no_data: target.skip_data,
            persistence: target.persistence,
            on_commit: target.on_commit,
            body: Box::new(select),
        })
    }
}

pub(super) fn compile_prepare(stmt: &pg_query::protobuf::PrepareStmt) -> Result<Statement> {
    let name = stmt.name.clone();
    let body = stmt
        .query
        .as_deref()
        .ok_or_else(|| SQLError::Internal("PREPARE without body".into()))?;
    let inner = compile_stmt(body)?;
    Ok(Statement::Prepare {
        name,
        body: Box::new(inner),
    })
}

pub(super) fn compile_execute(stmt: &pg_query::protobuf::ExecuteStmt) -> Result<Statement> {
    let name = stmt.name.clone();
    let mut params: Vec<Expr> = Vec::with_capacity(stmt.params.len());
    for p in &stmt.params {
        params.push(compile_expr(p)?);
    }
    Ok(Statement::Execute { name, params })
}

pub(super) fn compile_deallocate(stmt: &pg_query::protobuf::DeallocateStmt) -> Result<Statement> {
    let name = if stmt.name.is_empty() {
        None
    } else {
        Some(stmt.name.clone())
    };
    Ok(Statement::Deallocate { name })
}

pub(super) fn compile_create_foreign_server(
    stmt: &pg_query::protobuf::CreateForeignServerStmt,
) -> Result<crate::ast::CreateForeignServer> {
    use crate::ast::CreateForeignServer;
    Ok(CreateForeignServer {
        name: stmt.servername.clone(),
        fdw_type: stmt.fdwname.clone(),
        options: collect_def_elem_options(&stmt.options)?,
        if_not_exists: stmt.if_not_exists,
    })
}

pub(super) fn compile_create_foreign_table(
    stmt: &pg_query::protobuf::CreateForeignTableStmt,
) -> Result<crate::ast::CreateForeignTable> {
    use crate::ast::CreateForeignTable;
    let base = stmt
        .base_stmt
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE FOREIGN TABLE without base".into()))?;
    validate_create_table_envelope(base, "CREATE FOREIGN TABLE")?;
    let table = super::compile_create_table(base)?;
    if !table.key_constraints.is_empty() {
        let kind = if table
            .key_constraints
            .iter()
            .any(|constraint| constraint.kind == crate::ast::TableKeyConstraintKind::PrimaryKey)
        {
            "primary key"
        } else {
            "unique"
        };
        return Err(SQLError::Unsupported(format!(
            "{kind} constraints are not supported on foreign tables"
        )));
    }
    if !table.foreign_keys.is_empty()
        || table
            .columns
            .iter()
            .any(|column| column.references.is_some())
    {
        return Err(SQLError::Unsupported(
            "foreign key constraints are not supported on foreign tables".into(),
        ));
    }
    if !table.hierarchy.parents.is_empty()
        || table.hierarchy.partition_spec.is_some()
        || table.hierarchy.partition_bound.is_some()
    {
        return Err(SQLError::Unsupported(
            "foreign-table inheritance and partitioning are not supported".into(),
        ));
    }
    Ok(CreateForeignTable {
        name: table.name,
        server_name: stmt.servername.clone(),
        columns: table.columns,
        checks: table.checks,
        options: collect_def_elem_options(&stmt.options)?,
        if_not_exists: base.if_not_exists,
    })
}

pub(super) fn defer_create_foreign_table(
    stmt: &pg_query::protobuf::CreateForeignTableStmt,
) -> Result<crate::ast::DeferredCreateForeignTable> {
    let base = stmt
        .base_stmt
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE FOREIGN TABLE without base".into()))?;
    let relation = base
        .relation
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE FOREIGN TABLE without relation".into()))?;
    let persistence = relation_persistence(relation, "CREATE FOREIGN TABLE")?;
    if persistence != RelationPersistence::Permanent {
        return Err(SQLError::Unsupported(
            "CREATE FOREIGN TABLE: temporary and unlogged relations are not supported".into(),
        ));
    }
    let name = range_var_name(relation);
    if name.is_empty() {
        return Err(SQLError::Internal(
            "CREATE FOREIGN TABLE without name".into(),
        ));
    }
    let definition_sql = NodeEnum::CreateForeignTableStmt(stmt.clone()).deparse()?;
    Ok(crate::ast::DeferredCreateForeignTable {
        name,
        server_name: stmt.servername.clone(),
        definition_sql,
    })
}

pub(super) fn collect_def_elem_options(nodes: &[Node]) -> Result<Vec<(String, String)>> {
    let mut out: Vec<(String, String)> = Vec::new();
    for opt in nodes {
        let Some(NodeEnum::DefElem(elem)) = opt.node.as_ref() else {
            return Err(SQLError::Internal("malformed option node".into()));
        };
        let value = match elem
            .arg
            .as_ref()
            .and_then(|argument| argument.node.as_ref())
        {
            Some(NodeEnum::String(value)) => value.sval.clone(),
            Some(NodeEnum::Integer(value)) => value.ival.to_string(),
            Some(NodeEnum::Float(value)) => value.fval.clone(),
            Some(NodeEnum::Boolean(value)) => value.boolval.to_string(),
            Some(NodeEnum::TypeName(value)) => value
                .names
                .iter()
                .map(extract_string)
                .collect::<Result<Vec<_>>>()?
                .join("."),
            None => "true".into(),
            other => {
                return Err(SQLError::TypeMismatch(format!(
                    "option `{}` expects a scalar value, got {other:?}",
                    elem.defname
                )));
            }
        };
        out.push((elem.defname.clone(), value));
    }
    Ok(out)
}

fn invalid_reloption(message: impl Into<String>) -> SQLError {
    SQLError::Routine {
        sqlstate: "22023".into(),
        message: message.into(),
    }
}

fn validate_boolean_reloption(name: &str, value: &str) -> Result<()> {
    if matches!(
        value.to_ascii_lowercase().as_str(),
        "true" | "false" | "on" | "off" | "yes" | "no" | "1" | "0"
    ) {
        Ok(())
    } else {
        Err(invalid_reloption(format!(
            "invalid value for boolean option \"{name}\": {value}"
        )))
    }
}

pub(super) fn validate_view_options(
    options: Vec<(String, String)>,
    check_option: pg_query::protobuf::ViewCheckOption,
) -> Result<Vec<(String, String)>> {
    use pg_query::protobuf::ViewCheckOption;
    use std::collections::BTreeSet;

    let mut out = Vec::with_capacity(options.len() + 1);
    let mut seen = BTreeSet::new();
    for (name, value) in options {
        let name = name.to_ascii_lowercase();
        let value = value.to_ascii_lowercase();
        if !seen.insert(name.clone()) {
            return Err(invalid_reloption(format!(
                "parameter \"{name}\" specified more than once"
            )));
        }
        match name.as_str() {
            "security_barrier" | "security_invoker" => {
                validate_boolean_reloption(&name, &value)?;
            }
            "check_option" if matches!(value.as_str(), "local" | "cascaded") => {}
            "check_option" => {
                return Err(invalid_reloption(format!(
                    "invalid value for enum option \"check_option\": {value}"
                )));
            }
            _ => {
                return Err(invalid_reloption(format!(
                    "unrecognized parameter \"{name}\""
                )));
            }
        }
        out.push((name, value));
    }
    let clause_value = match check_option {
        ViewCheckOption::Undefined | ViewCheckOption::NoCheckOption => None,
        ViewCheckOption::LocalCheckOption => Some("local"),
        ViewCheckOption::CascadedCheckOption => Some("cascaded"),
    };
    if let Some(value) = clause_value {
        if !seen.insert("check_option".into()) {
            return Err(invalid_reloption(
                "parameter \"check_option\" specified more than once",
            ));
        }
        out.push(("check_option".into(), value.into()));
    }
    Ok(out)
}

pub(super) fn validate_materialized_view_options(
    options: Vec<(String, String)>,
) -> Result<Vec<(String, String)>> {
    use std::collections::BTreeSet;

    let mut seen = BTreeSet::new();
    for (name, value) in &options {
        let name = name.to_ascii_lowercase();
        if !seen.insert(name.clone()) {
            return Err(invalid_reloption(format!(
                "parameter \"{name}\" specified more than once"
            )));
        }
        if name != "fillfactor" {
            return Err(invalid_reloption(format!(
                "unrecognized parameter \"{name}\""
            )));
        }
        let fillfactor = value.parse::<u8>().map_err(|_| {
            invalid_reloption(format!(
                "invalid value for integer option \"fillfactor\": {value}"
            ))
        })?;
        if !(10..=100).contains(&fillfactor) {
            return Err(invalid_reloption(format!(
                "value {fillfactor} out of bounds for option \"fillfactor\""
            )));
        }
    }
    Ok(options
        .into_iter()
        .map(|(name, value)| (name.to_ascii_lowercase(), value.to_ascii_lowercase()))
        .collect())
}

pub(super) fn compile_create_view(stmt: &pg_query::protobuf::ViewStmt) -> Result<Statement> {
    let relation = stmt
        .view
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE VIEW without name".into()))?;
    let persistence = relation_persistence(relation, "CREATE VIEW")?;
    if persistence == RelationPersistence::Unlogged {
        return Err(SQLError::Routine {
            sqlstate: "0A000".into(),
            message: "views cannot be unlogged because they do not have storage".into(),
        });
    }
    let column_names = stmt
        .aliases
        .iter()
        .map(extract_string)
        .collect::<Result<Vec<_>>>()?;
    let options = validate_view_options(
        collect_def_elem_options(&stmt.options)?,
        stmt.with_check_option(),
    )?;
    let name = range_var_name(relation);
    let body = stmt
        .query
        .as_deref()
        .ok_or_else(|| SQLError::Internal("CREATE VIEW without body".into()))?;
    let inner = body
        .node
        .as_ref()
        .ok_or_else(|| SQLError::Internal("CREATE VIEW body empty".into()))?;
    let select = match inner {
        NodeEnum::SelectStmt(s) => compile_select(s)?,
        other => {
            return Err(SQLError::Unsupported(format!(
                "CREATE VIEW body must be SELECT, got {other:?}"
            )));
        }
    };
    Ok(Statement::CreateView {
        name,
        column_names,
        body: Box::new(select),
        or_replace: stmt.replace,
        persistence,
        options,
    })
}

pub(super) fn compile_refresh_materialized_view(
    stmt: &pg_query::protobuf::RefreshMatViewStmt,
) -> Result<Statement> {
    let relation = stmt
        .relation
        .as_ref()
        .ok_or_else(|| SQLError::Internal("REFRESH MATERIALIZED VIEW without name".into()))?;
    if !relation.catalogname.is_empty() {
        return Err(SQLError::Unsupported(
            "REFRESH MATERIALIZED VIEW: cross-database names are not supported".into(),
        ));
    }
    Ok(Statement::RefreshMaterializedView {
        name: range_var_name(relation),
        concurrently: stmt.concurrent,
        with_no_data: stmt.skip_data,
    })
}

pub(super) fn compile_create_schema(
    stmt: &pg_query::protobuf::CreateSchemaStmt,
) -> Result<Statement> {
    if stmt.authrole.is_some() {
        return Err(SQLError::Unsupported(
            "CREATE SCHEMA AUTHORIZATION is not supported".into(),
        ));
    }
    if !stmt.schema_elts.is_empty() {
        return Err(SQLError::Unsupported(
            "CREATE SCHEMA containing schema elements is not supported".into(),
        ));
    }
    let name = if stmt.schemaname.is_empty() {
        return Err(SQLError::Internal("CREATE SCHEMA without name".into()));
    } else {
        stmt.schemaname.clone()
    };
    Ok(Statement::CreateSchema {
        name,
        if_not_exists: stmt.if_not_exists,
    })
}