pgevolve-core 0.4.3

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
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
//! Parser for `CREATE CAST` and `COMMENT ON CAST`.
//!
//! `pg_query` 6.x encodes `CREATE CAST` as a [`CreateCastStmt`]:
//! - `sourcetype` / `targettype`: [`TypeName`] nodes for the source and target types.
//! - `func`: `Option<ObjectWithArgs>` — present for `WITH FUNCTION`, absent for
//!   `WITHOUT FUNCTION` (Binary) or `WITH INOUT` (distinguished by `inout: true`).
//! - `context`: a [`CoercionContext`] integer: `CoercionImplicit` (1), `CoercionAssignment`
//!   (2), `CoercionPlpgsql` (3 — rejected), `CoercionExplicit` (4).
//! - `inout`: `true` when the source has `WITH INOUT`.
//!
//! Cast identity is `(source, target)` — unlike aggregates, casts are not
//! overloadable; two `CREATE CAST` statements with the same `(source, target)`
//! pair are rejected as duplicates.
//!
//! `COMMENT ON CAST (src AS tgt) IS '…'` arrives as a `CommentStmt` with
//! `objtype = ObjectCast` and `object` = a `List` of two [`TypeName`] nodes
//! (source first, target second). This is handled by `apply_comment`.
//!
//! `DROP CAST` in source is rejected (drops are produced by the diff).
//!
//! ## `TypeName` → `QualifiedName` convention
//!
//! A cast's source and target types are always schema-qualified by `pg_query` when
//! the user writes a SQL-keyword type alias (e.g. `integer` → `pg_catalog.int4`),
//! but unqualified user-type names like `text` arrive as a single String node.
//! To ensure round-trip consistency with the future reader, we apply the
//! following rule:
//! - Two String nodes in `TypeName.names` → use them directly as `(schema, name)`.
//! - One String node → prefix with `pg_catalog` (the implicit catalog for all
//!   built-in types; user types must always be written as `schema.typename` in
//!   `CREATE CAST`).
//!
//! This is the same resolution the reader will apply: it reads `pg_catalog.text`,
//! `pg_catalog.int4`, etc.

use pg_query::NodeEnum;
use pg_query::protobuf::{CoercionContext, CommentStmt, CreateCastStmt, TypeName};

use crate::identifier::{Identifier, QualifiedName};
use crate::ir::cast::{Cast, CastContext, CastMethod};
use crate::ir::column_type::ColumnType;
use crate::parse::builder::shared;
use crate::parse::error::{ParseError, SourceLocation};

/// Build a [`Cast`] from a `CREATE CAST` AST node and append it to the
/// accumulator.
///
/// Rejects `CoercionPlpgsql` context, duplicate `(source, target)` identities,
/// and missing source/target type nodes.
pub(crate) fn parse_create(
    stmt: &CreateCastStmt,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
    existing: &mut Vec<Cast>,
) -> Result<(), ParseError> {
    let source_tn = stmt
        .sourcetype
        .as_ref()
        .ok_or_else(|| ParseError::Structural {
            location: location.clone(),
            message: "CREATE CAST: missing source type".into(),
        })?;
    let target_tn = stmt
        .targettype
        .as_ref()
        .ok_or_else(|| ParseError::Structural {
            location: location.clone(),
            message: "CREATE CAST: missing target type".into(),
        })?;

    let source = type_name_to_qname(source_tn, default_schema, location)?;
    let target = type_name_to_qname(target_tn, default_schema, location)?;

    let context = coercion_context(stmt.context, location)?;

    let method = if let Some(func) = stmt.func.as_ref() {
        let name = shared::qname_from_string_list(&func.objname, default_schema, location)?;
        let mut arg_types: Vec<ColumnType> = Vec::with_capacity(func.objargs.len());
        for node in &func.objargs {
            match node.node.as_ref() {
                Some(NodeEnum::TypeName(tn)) => {
                    arg_types.push(shared::type_name_to_column_type(tn, location)?);
                }
                other => {
                    return Err(ParseError::Structural {
                        location: location.clone(),
                        message: format!(
                            "CREATE CAST ({source} AS {target}): expected TypeName in function \
                             argument list, got {:?}",
                            other.map(std::mem::discriminant)
                        ),
                    });
                }
            }
        }
        CastMethod::Function { name, arg_types }
    } else if stmt.inout {
        CastMethod::Inout
    } else {
        CastMethod::Binary
    };

    // Reject duplicate (source, target) identity.
    if existing
        .iter()
        .any(|c| c.source == source && c.target == target)
    {
        return Err(ParseError::Structural {
            location: location.clone(),
            message: format!("duplicate cast ({source} AS {target})"),
        });
    }

    existing.push(Cast {
        source,
        target,
        method,
        context,
        comment: None,
    });
    Ok(())
}

/// Apply a `COMMENT ON CAST (src AS tgt) IS '…'` against the accumulator.
///
/// `pg_query` encodes the cast reference as `object = List[TypeName(src), TypeName(tgt)]`.
pub(crate) fn apply_comment(
    stmt: &CommentStmt,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
    existing: &mut [Cast],
) -> Result<(), ParseError> {
    let (source, target) = identity_from_comment(stmt, default_schema, location)?;
    let comment = if stmt.comment.is_empty() {
        None
    } else {
        Some(stmt.comment.clone())
    };
    let cast = find_mut(existing, &source, &target, location)?;
    cast.comment = comment;
    Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Extract `(source, target)` `QualifiedName`s from a `COMMENT ON CAST` object
/// reference. The object is a `List` of exactly two `TypeName` nodes.
fn identity_from_comment(
    stmt: &CommentStmt,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<(QualifiedName, QualifiedName), ParseError> {
    let obj = stmt
        .object
        .as_ref()
        .and_then(|o| o.node.as_ref())
        .ok_or_else(|| ParseError::Structural {
            location: location.clone(),
            message: "COMMENT ON CAST: missing object reference".into(),
        })?;
    let NodeEnum::List(list) = obj else {
        return Err(ParseError::Structural {
            location: location.clone(),
            message: format!(
                "COMMENT ON CAST: expected a List node, got {:?}",
                std::mem::discriminant(obj)
            ),
        });
    };
    if list.items.len() != 2 {
        return Err(ParseError::Structural {
            location: location.clone(),
            message: format!(
                "COMMENT ON CAST: expected exactly 2 TypeName items, got {}",
                list.items.len()
            ),
        });
    }
    let src_tn = extract_type_name(&list.items[0], location)?;
    let tgt_tn = extract_type_name(&list.items[1], location)?;
    let source = type_name_to_qname(src_tn, default_schema, location)?;
    let target = type_name_to_qname(tgt_tn, default_schema, location)?;
    Ok((source, target))
}

/// Borrow a `TypeName` from a list item node.
fn extract_type_name<'a>(
    node: &'a pg_query::protobuf::Node,
    location: &SourceLocation,
) -> Result<&'a TypeName, ParseError> {
    match node.node.as_ref() {
        Some(NodeEnum::TypeName(tn)) => Ok(tn),
        other => Err(ParseError::Structural {
            location: location.clone(),
            message: format!(
                "COMMENT ON CAST: expected TypeName in cast identity list, got {:?}",
                other.map(std::mem::discriminant)
            ),
        }),
    }
}

/// Convert a cast [`TypeName`] to a [`QualifiedName`].
///
/// - Two String nodes → `(schema, name)` directly.
/// - One String node  → `(pg_catalog, name)` (unqualified built-in type).
///
/// This matches the representation the reader (Task 7) will produce: both sides
/// of a built-in cast like `(text AS integer)` will be stored as
/// `pg_catalog.text` / `pg_catalog.int4`.
fn type_name_to_qname(
    type_name: &TypeName,
    default_schema: Option<&Identifier>,
    location: &SourceLocation,
) -> Result<QualifiedName, ParseError> {
    let 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!(
                    "CREATE CAST: expected String node in type name, got {:?}",
                    other.map(std::mem::discriminant)
                ),
            }),
        })
        .collect::<Result<Vec<_>, _>>()?;

    match strings.as_slice() {
        [schema, name] => {
            let schema_id = shared::ident(schema, location)?;
            let name_id = shared::ident(name, location)?;
            Ok(QualifiedName::new(schema_id, name_id))
        }
        [name] => {
            // Single-segment: check if there is a user-supplied default schema.
            // If so, use that (user-defined type written unqualified is unusual
            // but accepted if a `-- @pgevolve schema=` directive is present).
            // Otherwise, fall back to `pg_catalog` which is the implicit home
            // for all built-in type aliases (e.g. `text`, `bool`).
            let name_id = shared::ident(name, location)?;
            let schema_id = default_schema.cloned().unwrap_or_else(|| {
                Identifier::from_unquoted("pg_catalog").expect("static identifier")
            });
            Ok(QualifiedName::new(schema_id, name_id))
        }
        _ => Err(ParseError::Structural {
            location: location.clone(),
            message: format!(
                "CREATE CAST: type name has {} components (expected 1 or 2)",
                strings.len()
            ),
        }),
    }
}

/// Map a raw [`CoercionContext`] integer to a [`CastContext`], rejecting
/// `CoercionPlpgsql` which cannot appear in a `CREATE CAST` source statement.
fn coercion_context(raw: i32, location: &SourceLocation) -> Result<CastContext, ParseError> {
    match CoercionContext::try_from(raw) {
        Ok(CoercionContext::CoercionImplicit) => Ok(CastContext::Implicit),
        Ok(CoercionContext::CoercionAssignment) => Ok(CastContext::Assignment),
        Ok(CoercionContext::CoercionExplicit) => Ok(CastContext::Explicit),
        Ok(CoercionContext::CoercionPlpgsql) => Err(ParseError::Structural {
            location: location.clone(),
            message: "CoercionPlpgsql context is not valid in a CREATE CAST source statement"
                .into(),
        }),
        Ok(CoercionContext::Undefined) | Err(_) => Err(ParseError::Structural {
            location: location.clone(),
            message: format!("CREATE CAST: unrecognised coercion context value {raw}"),
        }),
    }
}

/// Find the cast matching `(source, target)` for COMMENT, or error.
fn find_mut<'a>(
    existing: &'a mut [Cast],
    source: &QualifiedName,
    target: &QualifiedName,
    location: &SourceLocation,
) -> Result<&'a mut Cast, ParseError> {
    existing
        .iter_mut()
        .find(|c| c.source == *source && c.target == *target)
        .ok_or_else(|| ParseError::Structural {
            location: location.clone(),
            message: format!(
                "cast ({source} AS {target}) referenced before it is created in source"
            ),
        })
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use tempfile::tempdir;

    use super::*;
    use crate::ir::catalog::Catalog;
    use crate::parse::parse_directory;

    fn write(dir: &Path, rel: &str, contents: &str) {
        let p = dir.join(rel);
        if let Some(parent) = p.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(p, contents).unwrap();
    }

    /// Parse SQL through the full `parse_directory` entry point and return the
    /// resulting canonical catalog.
    fn parse_source(sql: &str) -> Result<Catalog, ParseError> {
        let tmp = tempdir().expect("tempdir");
        write(tmp.path(), "schema.sql", sql);
        parse_directory(tmp.path(), &[])
    }

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

    /// Minimal prelude that satisfies `parse_directory`'s resolution pass.
    /// Declares two user-defined types so we can write casts on them.
    const PRELUDE: &str = "\
        CREATE SCHEMA app;\n\
        CREATE TYPE app.a AS ENUM ('x');\n\
        CREATE TYPE app.b AS ENUM ('y');\n\
        CREATE FUNCTION app.conv(app.a) RETURNS app.b \
            AS $$ SELECT 'y'::app.b $$ LANGUAGE sql;\n";

    // ── WITH FUNCTION variants ───────────────────────────────────────────────

    #[test]
    fn create_with_function_explicit() {
        let sql = format!("{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a);");
        let cat = parse_source(&sql).expect("parses");
        assert_eq!(cat.casts.len(), 1);
        let c = &cat.casts[0];
        assert_eq!(c.source.to_string(), "app.a");
        assert_eq!(c.target.to_string(), "app.b");
        assert_eq!(c.context, CastContext::Explicit);
        assert!(matches!(c.method, CastMethod::Function { .. }));
        if let CastMethod::Function { name, arg_types } = &c.method {
            assert_eq!(name.to_string(), "app.conv");
            assert_eq!(arg_types.len(), 1);
        }
        assert!(c.comment.is_none());
    }

    #[test]
    fn create_with_function_assignment() {
        let sql = format!(
            "{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a) AS ASSIGNMENT;"
        );
        let cat = parse_source(&sql).expect("parses");
        assert_eq!(cat.casts[0].context, CastContext::Assignment);
        assert!(matches!(cat.casts[0].method, CastMethod::Function { .. }));
    }

    #[test]
    fn create_with_function_implicit() {
        let sql = format!(
            "{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a) AS IMPLICIT;"
        );
        let cat = parse_source(&sql).expect("parses");
        assert_eq!(cat.casts[0].context, CastContext::Implicit);
        assert!(matches!(cat.casts[0].method, CastMethod::Function { .. }));
    }

    // ── WITHOUT FUNCTION ────────────────────────────────────────────────────

    #[test]
    fn create_without_function() {
        let sql = format!("{PRELUDE}CREATE CAST (app.a AS app.b) WITHOUT FUNCTION;");
        let cat = parse_source(&sql).expect("parses");
        let c = &cat.casts[0];
        assert_eq!(c.method, CastMethod::Binary);
        assert_eq!(c.context, CastContext::Explicit);
    }

    // ── WITH INOUT ──────────────────────────────────────────────────────────

    #[test]
    fn create_with_inout() {
        let sql = format!("{PRELUDE}CREATE CAST (app.a AS app.b) WITH INOUT;");
        let cat = parse_source(&sql).expect("parses");
        let c = &cat.casts[0];
        assert_eq!(c.method, CastMethod::Inout);
        assert_eq!(c.context, CastContext::Explicit);
    }

    // ── COMMENT ON CAST ─────────────────────────────────────────────────────

    #[test]
    fn comment_on_cast_sets_comment() {
        let sql = format!(
            "{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a);\n\
             COMMENT ON CAST (app.a AS app.b) IS 'converts a to b';"
        );
        let cat = parse_source(&sql).expect("parses");
        assert_eq!(cat.casts[0].comment.as_deref(), Some("converts a to b"));
    }

    // ── Error cases ─────────────────────────────────────────────────────────

    #[test]
    fn drop_cast_in_source_is_rejected() {
        let sql = format!(
            "{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a);\n\
             DROP CAST (app.a AS app.b);"
        );
        let err = parse_source(&sql).expect_err("should reject DROP in source");
        assert!(matches!(err, ParseError::Structural { .. }), "got: {err:?}");
    }

    #[test]
    fn duplicate_identity_is_rejected() {
        let sql = format!(
            "{PRELUDE}\
             CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a);\n\
             CREATE CAST (app.a AS app.b) WITH INOUT;"
        );
        let err = parse_source(&sql).expect_err("should reject duplicate");
        let msg = match &err {
            ParseError::Structural { message, .. } => message.clone(),
            other => panic!("expected Structural, got {other:?}"),
        };
        assert!(msg.contains("duplicate cast"), "msg: {msg}");
    }

    // ── Unit-level parse_create ──────────────────────────────────────────────

    #[test]
    fn parse_create_unit_appends() {
        let parsed = pg_query::parse("CREATE CAST (app.a AS app.b) WITH INOUT;").unwrap();
        let node = parsed
            .protobuf
            .stmts
            .into_iter()
            .next()
            .and_then(|r| r.stmt)
            .and_then(|n| n.node)
            .unwrap();
        let NodeEnum::CreateCastStmt(stmt) = node else {
            panic!("expected CreateCastStmt");
        };
        let mut acc: Vec<Cast> = Vec::new();
        parse_create(&stmt, None, &loc(), &mut acc).expect("ok");
        assert_eq!(acc.len(), 1);
        assert_eq!(acc[0].source.to_string(), "app.a");
        assert_eq!(acc[0].target.to_string(), "app.b");
        assert_eq!(acc[0].method, CastMethod::Inout);
        assert_eq!(acc[0].context, CastContext::Explicit);
    }

    /// Verify `type_name_to_qname` for the sanity-check example requested:
    /// `CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a) AS IMPLICIT`.
    #[test]
    fn debug_output_sanity_check() {
        let sql = format!(
            "{PRELUDE}CREATE CAST (app.a AS app.b) WITH FUNCTION app.conv(app.a) AS IMPLICIT;"
        );
        let cat = parse_source(&sql).expect("parses");
        let c = &cat.casts[0];
        // Paste-able debug output for the task report:
        println!("Cast debug: {c:#?}");
        assert_eq!(c.source.to_string(), "app.a");
        assert_eq!(c.target.to_string(), "app.b");
        assert_eq!(c.context, CastContext::Implicit);
        let CastMethod::Function { name, arg_types } = &c.method else {
            panic!("expected Function method");
        };
        assert_eq!(name.to_string(), "app.conv");
        assert_eq!(arg_types.len(), 1);
    }
}