Skip to main content

uqa_sql/catalog/
foreign_relations.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! `PostgreSQL` foreign catalog row descriptors, shared by relation and type projection.
8
9use super::{
10    schema_production::CatalogColumnType::{self, Array, Inline},
11    VirtualRelation,
12};
13use crate::ColumnType;
14
15pub struct ForeignCatalogDescriptor {
16    pub relation: VirtualRelation,
17    pub row_type: u32,
18    pub array_type: u32,
19    pub required_columns: usize,
20}
21
22pub const FOREIGN_CATALOGS: &[ForeignCatalogDescriptor] = &[
23    ForeignCatalogDescriptor {
24        relation: VirtualRelation::PgForeignServer,
25        row_type: 10_078,
26        array_type: 10_077,
27        required_columns: 4,
28    },
29    ForeignCatalogDescriptor {
30        relation: VirtualRelation::PgForeignDataWrapper,
31        row_type: 10_076,
32        array_type: 10_075,
33        required_columns: 5,
34    },
35    ForeignCatalogDescriptor {
36        relation: VirtualRelation::PgForeignTable,
37        row_type: 10_082,
38        array_type: 10_081,
39        required_columns: 2,
40    },
41];
42
43pub(super) fn visit_columns(
44    relation: VirtualRelation,
45    visit: &mut dyn FnMut(&'static str, CatalogColumnType),
46) {
47    use ColumnType::{AclItem, Name, Oid, Text};
48    macro_rules! columns {
49        ($($name:literal => $ty:expr),* $(,)?) => {{ $(visit($name, $ty);)* }};
50    }
51    match relation {
52        VirtualRelation::PgForeignDataWrapper => columns![
53            "oid" => Inline(Oid), "fdwname" => Inline(Name), "fdwowner" => Inline(Oid),
54            "fdwhandler" => Inline(Oid), "fdwvalidator" => Inline(Oid),
55            "fdwacl" => Array(AclItem), "fdwoptions" => Array(Text),
56        ],
57        VirtualRelation::PgForeignServer => columns![
58            "oid" => Inline(Oid), "srvname" => Inline(Name), "srvowner" => Inline(Oid),
59            "srvfdw" => Inline(Oid), "srvtype" => Inline(Text), "srvversion" => Inline(Text),
60            "srvacl" => Array(AclItem), "srvoptions" => Array(Text),
61        ],
62        VirtualRelation::PgForeignTable => columns![
63            "ftrelid" => Inline(Oid), "ftserver" => Inline(Oid), "ftoptions" => Array(Text),
64        ],
65        _ => unreachable!("foreign catalog descriptor"),
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn foreign_catalog_descriptors_match_postgresql() {
75        let reference: serde_json::Value = serde_json::from_str(include_str!(concat!(
76            env!("CARGO_MANIFEST_DIR"),
77            "/../../tests/parity/pg18/foreign_catalog_oracle.expected.json"
78        )))
79        .unwrap();
80        let expected = reference["cases"]
81            .as_array()
82            .unwrap()
83            .iter()
84            .find(|case| case["id"] == "column_types")
85            .unwrap()["results"][0]["rows"]
86            .clone();
87        let mut rows = Vec::new();
88        for descriptor in FOREIGN_CATALOGS {
89            assert!(descriptor.relation.accepts_row_lock());
90            for (index, (name, ty)) in descriptor.relation.schema().into_iter().enumerate() {
91                rows.push(vec![
92                    descriptor.relation.oid().to_string(),
93                    (index + 1).to_string(),
94                    name,
95                    crate::catalog::type_metadata::pg_type_oid(&ty).to_string(),
96                    if index < descriptor.required_columns {
97                        "t"
98                    } else {
99                        "f"
100                    }
101                    .to_owned(),
102                    "f".into(),
103                ]);
104            }
105        }
106        assert_eq!(serde_json::json!(rows), expected);
107    }
108}