pgevolve-core 0.4.2

Postgres declarative schema management — core library (parser, IR, diff, planner) powering the pgevolve CLI.
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
//! Helpers shared by all AST → IR builders.
//!
//! - Schema-qualified name resolution (with `-- @pgevolve schema=` defaulting).
//! - Type-name stringification (so [`crate::ir::column_type::ColumnType::parse_from_pg_type_string`]
//!   can decide the canonical form).
//! - Default-expression classification (literal vs. `nextval` vs. arbitrary expr).

use pg_query::NodeEnum;
use pg_query::protobuf::{AConst, RangeVar, TypeName, a_const};

use crate::identifier::{Identifier, QualifiedName};
use crate::ir::column_type::ColumnType;
use crate::ir::default_expr::{DefaultExpr, LiteralValue};
use crate::parse::error::{ParseError, SourceLocation};
use crate::parse::normalize_expr;

/// Resolve a `RangeVar` into a [`QualifiedName`], filling in `default_schema`
/// when the source did not qualify the object.
///
/// Returns [`ParseError::UnqualifiedName`] if neither the source nor the
/// directive supply a schema.
pub fn resolve_qname(
    range_var: &RangeVar,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<QualifiedName, ParseError> {
    let name = ident(&range_var.relname, location)?;
    if !range_var.schemaname.is_empty() {
        let schema = ident(&range_var.schemaname, location)?;
        return Ok(QualifiedName::new(schema, name));
    }
    default_schema.map_or_else(
        || {
            Err(ParseError::UnqualifiedName {
                location: location.clone(),
            })
        },
        |s| Ok(QualifiedName::new(s.clone(), name)),
    )
}

/// Resolve a `repeated Node` type-name list (as produced by `CreateEnumStmt`,
/// `CreateDomainStmt`, `CompositeTypeStmt`, etc.) into a [`QualifiedName`].
///
/// PG encodes the name as a list of [`pg_query::NodeEnum::String`] nodes:
/// - one element → unqualified name (requires `default_schema`).
/// - two elements → `[schema, name]`.
///
/// Returns [`ParseError::UnqualifiedName`] if neither the source nor the
/// directive supply a schema.
///
/// This helper is shared by the enum, domain, and composite builders (T2–T4).
pub fn qname_from_string_list(
    nodes: &[pg_query::protobuf::Node],
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<QualifiedName, ParseError> {
    let strings: Vec<&str> = nodes
        .iter()
        .map(|n| match n.node.as_ref() {
            Some(NodeEnum::String(s)) => Ok(s.sval.as_str()),
            other => Err(ParseError::Structural {
                location: location.clone(),
                message: format!(
                    "expected String node in type-name list, got {:?}",
                    other.map(std::mem::discriminant),
                ),
            }),
        })
        .collect::<Result<Vec<_>, _>>()?;
    match strings.as_slice() {
        [name] => {
            let name_id = ident(name, location)?;
            let schema = default_schema
                .cloned()
                .ok_or_else(|| ParseError::UnqualifiedName {
                    location: location.clone(),
                })?;
            Ok(QualifiedName::new(schema, name_id))
        }
        [schema, name] => {
            let schema_id = ident(schema, location)?;
            let name_id = ident(name, location)?;
            Ok(QualifiedName::new(schema_id, name_id))
        }
        _ => Err(ParseError::Structural {
            location: location.clone(),
            message: format!(
                "unexpected type-name list length {} (expected 1 or 2 String nodes)",
                nodes.len()
            ),
        }),
    }
}

/// Build an unquoted [`Identifier`], wrapping [`crate::ir::IrError`] into a
/// source-located [`ParseError::Ir`].
pub fn ident(s: &str, location: &SourceLocation) -> Result<Identifier, ParseError> {
    Identifier::from_unquoted(s).map_err(|e| ParseError::Ir {
        location: location.clone(),
        source: crate::ir::IrError::InvalidIdentifier(e.to_string()),
    })
}

/// Render a `pg_query::TypeName` into a string `ColumnType::parse_from_pg_type_string`
/// can consume.
///
/// Strategy: take the *last* segment of `names` (Postgres prefixes types with
/// `pg_catalog.` internally; the parser is alias-aware), append a parenthesized
/// list of typmod arguments, append `[]` for each array dimension.
pub fn render_type_name_to_string(type_name: &TypeName) -> Option<String> {
    let last = type_name.names.last()?.node.as_ref()?;
    let NodeEnum::String(s) = last else {
        return None;
    };
    let bare = s.sval.clone();
    let mut out = bare;
    if !type_name.typmods.is_empty() {
        let mut args: Vec<String> = Vec::with_capacity(type_name.typmods.len());
        for n in &type_name.typmods {
            let Some(NodeEnum::AConst(c)) = &n.node else {
                return None;
            };
            args.push(literal_arg_to_string(c)?);
        }
        out = format!("{out}({})", args.join(","));
    }
    for _ in 0..type_name.array_bounds.len() {
        out.push_str("[]");
    }
    Some(out)
}

/// Convert an [`AConst`] used as a typmod argument to its canonical string form.
fn literal_arg_to_string(c: &AConst) -> Option<String> {
    match c.val.as_ref()? {
        a_const::Val::Ival(i) => Some(i.ival.to_string()),
        a_const::Val::Sval(s) => Some(s.sval.clone()),
        _ => None,
    }
}

/// Convert a `TypeName` into a [`ColumnType`], propagating parser errors.
///
/// When `TypeName.names` contains two String nodes and the first is not
/// `pg_catalog`, the type is schema-qualified by the user (e.g. `app.order_status`).
/// In that case we emit `ColumnType::UserDefined(QualifiedName)` so that the
/// AST resolution pass can validate that the referenced type is declared in source.
///
/// Unqualified single-segment names are handled by the usual string path; if they
/// don't match any built-in they fall through to `ColumnType::Other`, which is
/// intentional — unqualified user types must be resolved via `default_schema` at
/// domain/composite parse time rather than here.
pub fn type_name_to_column_type(
    type_name: &TypeName,
    location: &SourceLocation,
) -> Result<ColumnType, ParseError> {
    // Collect String nodes from names; fail fast on unexpected kinds.
    let name_strings: Vec<&str> = type_name
        .names
        .iter()
        .map(|n| match n.node.as_ref() {
            Some(NodeEnum::String(s)) => Ok(s.sval.as_str()),
            other => Err(ParseError::Structural {
                location: location.clone(),
                message: format!(
                    "expected String node in type-name list, got {:?}",
                    other.map(std::mem::discriminant),
                ),
            }),
        })
        .collect::<Result<Vec<_>, _>>()?;

    // Two-segment, non-pg_catalog prefix → user-defined type reference.
    if let [schema, name] = name_strings.as_slice()
        && *schema != "pg_catalog"
    {
        let schema_id = ident(schema, location)?;
        let name_id = ident(name, location)?;
        return Ok(ColumnType::UserDefined(QualifiedName::new(
            schema_id, name_id,
        )));
    }

    let s = render_type_name_to_string(type_name).ok_or_else(|| ParseError::Structural {
        location: location.clone(),
        message: "could not stringify type name".into(),
    })?;
    ColumnType::parse_from_pg_type_string(&s).map_err(|e| ParseError::Structural {
        location: location.clone(),
        message: format!("invalid column type {s:?}: {e}"),
    })
}

/// Convert a column-default expression node into a [`DefaultExpr`].
///
/// Recognizes:
/// - Bare [`AConst`] integer/float/string/bool/null literals → [`DefaultExpr::Literal`].
/// - `nextval('seq')` and `nextval('schema.seq')` → [`DefaultExpr::Sequence`].
/// - Anything else → [`DefaultExpr::Expr`] via the normalizer.
///
/// `target_type` enables redundant-cast stripping in the `Expr` arm.
pub fn build_default_expr(
    node: &NodeEnum,
    target_type: Option<&ColumnType>,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<DefaultExpr, ParseError> {
    if let Some(lit) = literal_from_node(node) {
        return Ok(DefaultExpr::Literal(lit));
    }
    if let Some(seq) = nextval_target(node, default_schema, location)? {
        return Ok(DefaultExpr::Sequence(seq));
    }
    let normalized = normalize_expr::from_pg_node(node, target_type, location)?;
    Ok(DefaultExpr::Expr(normalized))
}

/// If `node` is a literal `AConst` (possibly wrapped in a `TypeCast` whose target
/// matches the column type), return the equivalent [`LiteralValue`].
fn literal_from_node(node: &NodeEnum) -> Option<LiteralValue> {
    let inner = unwrap_typecast(node);
    match inner {
        NodeEnum::AConst(c) => aconst_to_literal(c),
        _ => None,
    }
}

/// Strip a single layer of `TypeCast` wrapping, if any.
fn unwrap_typecast(node: &NodeEnum) -> &NodeEnum {
    if let NodeEnum::TypeCast(cast) = node
        && let Some(arg) = cast.arg.as_ref()
        && let Some(inner) = arg.node.as_ref()
    {
        return inner;
    }
    node
}

fn aconst_to_literal(c: &AConst) -> Option<LiteralValue> {
    if c.isnull {
        return Some(LiteralValue::Null);
    }
    match c.val.as_ref()? {
        a_const::Val::Ival(i) => Some(LiteralValue::Integer(i64::from(i.ival))),
        a_const::Val::Fval(f) => f.fval.parse::<f64>().ok().map(LiteralValue::Float),
        a_const::Val::Boolval(b) => Some(LiteralValue::Bool(b.boolval)),
        a_const::Val::Sval(s) => Some(LiteralValue::Text(s.sval.clone())),
        a_const::Val::Bsval(_) => None,
    }
}

/// If `node` is `nextval('seq')` (with optional `::regclass` cast on the arg),
/// return the referenced sequence's [`QualifiedName`].
fn nextval_target(
    node: &NodeEnum,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<Option<QualifiedName>, ParseError> {
    let inner = unwrap_typecast(node);
    let NodeEnum::FuncCall(fc) = inner else {
        return Ok(None);
    };
    let func = match fc.funcname.last().and_then(|n| n.node.as_ref()) {
        Some(NodeEnum::String(s)) => s.sval.as_str(),
        _ => return Ok(None),
    };
    if !func.eq_ignore_ascii_case("nextval") {
        return Ok(None);
    }
    let arg = fc.args.first().and_then(|n| n.node.as_ref());
    let Some(arg_node) = arg else {
        return Ok(None);
    };
    // The argument to nextval is normally a string literal cast to regclass:
    // `nextval('app.seq1'::regclass)`. Strip the cast and look for an AConst Sval.
    let inner_arg = unwrap_typecast(arg_node);
    let NodeEnum::AConst(c) = inner_arg else {
        return Ok(None);
    };
    let Some(a_const::Val::Sval(s)) = c.val.as_ref() else {
        return Ok(None);
    };
    Ok(Some(parse_qualified_seq_name(
        &s.sval,
        default_schema,
        location,
    )?))
}

/// Parse a possibly-qualified sequence reference like `schema.seq` or `seq`.
fn parse_qualified_seq_name(
    s: &str,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<QualifiedName, ParseError> {
    let parts: Vec<&str> = s.split('.').collect();
    match parts.len() {
        1 => {
            let name = ident(parts[0], location)?;
            let schema = default_schema
                .cloned()
                .ok_or_else(|| ParseError::UnqualifiedName {
                    location: location.clone(),
                })?;
            Ok(QualifiedName::new(schema, name))
        }
        2 => {
            let schema = ident(parts[0], location)?;
            let name = ident(parts[1], location)?;
            Ok(QualifiedName::new(schema, name))
        }
        _ => Err(ParseError::Structural {
            location: location.clone(),
            message: format!("unsupported qualified sequence reference {s:?}"),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn loc() -> SourceLocation {
        SourceLocation::new(PathBuf::from("test.sql"), 1, 1)
    }

    fn parse_first(sql: &str) -> NodeEnum {
        let parsed = pg_query::parse(sql).expect("parses");
        parsed
            .protobuf
            .stmts
            .into_iter()
            .next()
            .and_then(|raw| raw.stmt)
            .and_then(|n| n.node)
            .expect("at least one statement")
    }

    fn parse_select_expr(expr: &str) -> NodeEnum {
        let n = parse_first(&format!("SELECT {expr};"));
        let NodeEnum::SelectStmt(s) = n else { panic!() };
        let target = s.target_list.into_iter().next().unwrap().node.unwrap();
        let NodeEnum::ResTarget(rt) = target else {
            panic!()
        };
        rt.val.unwrap().node.unwrap()
    }

    #[test]
    fn literal_integer_default() {
        let n = parse_select_expr("42");
        let d = build_default_expr(&n, Some(&ColumnType::Integer), None, &loc()).unwrap();
        assert!(matches!(d, DefaultExpr::Literal(LiteralValue::Integer(42))));
    }

    #[test]
    fn literal_text_default() {
        let n = parse_select_expr("'hello'");
        let d = build_default_expr(&n, Some(&ColumnType::Text), None, &loc()).unwrap();
        assert!(matches!(d, DefaultExpr::Literal(LiteralValue::Text(s)) if s == "hello"));
    }

    #[test]
    fn null_default() {
        let n = parse_select_expr("NULL");
        let d = build_default_expr(&n, None, None, &loc()).unwrap();
        assert!(matches!(d, DefaultExpr::Literal(LiteralValue::Null)));
    }

    #[test]
    fn bool_default() {
        let n = parse_select_expr("true");
        let d = build_default_expr(&n, Some(&ColumnType::Boolean), None, &loc()).unwrap();
        // Booleans in pg_query may parse as a function call `'t'::boolean` form
        // — accept either Bool literal or Expr containing "true".
        match d {
            DefaultExpr::Literal(LiteralValue::Bool(true)) => {}
            DefaultExpr::Expr(e) => assert!(e.canonical_text.contains("true")),
            other => panic!("unexpected default: {other:?}"),
        }
    }

    #[test]
    fn nextval_qualified_default() {
        let n = parse_select_expr("nextval('app.seq1'::regclass)");
        let d = build_default_expr(&n, None, None, &loc()).unwrap();
        match d {
            DefaultExpr::Sequence(q) => assert_eq!(q.to_string(), "app.seq1"),
            other => panic!("expected Sequence, got {other:?}"),
        }
    }

    #[test]
    fn nextval_unqualified_uses_default_schema() {
        let n = parse_select_expr("nextval('seq1'::regclass)");
        let app = Identifier::from_unquoted("app").unwrap();
        let d = build_default_expr(&n, None, Some(&app), &loc()).unwrap();
        match d {
            DefaultExpr::Sequence(q) => assert_eq!(q.to_string(), "app.seq1"),
            other => panic!("expected Sequence, got {other:?}"),
        }
    }

    #[test]
    fn func_call_other_than_nextval_is_expr() {
        let n = parse_select_expr("now()");
        let d = build_default_expr(&n, None, None, &loc()).unwrap();
        assert!(matches!(d, DefaultExpr::Expr(_)));
    }

    #[test]
    fn cast_to_target_strips_in_expr_arm() {
        // `'a' || 'b'` is an expression — cast stripping has nothing to strip
        // here. This test just checks the Expr arm runs.
        let n = parse_select_expr("'a' || 'b'");
        let d = build_default_expr(&n, Some(&ColumnType::Text), None, &loc()).unwrap();
        assert!(matches!(d, DefaultExpr::Expr(_)));
    }

    #[test]
    fn type_name_renders_with_typmod() {
        // Parse a CREATE TABLE to get a real TypeName.
        let stmt = parse_first("CREATE TABLE t (c varchar(50));");
        let NodeEnum::CreateStmt(create) = stmt else {
            panic!()
        };
        let elt = create.table_elts.into_iter().next().unwrap();
        let NodeEnum::ColumnDef(col) = elt.node.unwrap() else {
            panic!()
        };
        let tn = col.type_name.unwrap();
        let s = render_type_name_to_string(&tn).unwrap();
        let ct = ColumnType::parse_from_pg_type_string(&s).unwrap();
        assert_eq!(ct, ColumnType::Varchar { len: Some(50) });
    }
}