Skip to main content

uqa_sql/ast/types/
display.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Type names in diagnostics, as `format_type_be` spells them: a user-defined type is qualified by its schema when the running statement's search path does not include that schema. The statement executor records the search path for the statement it runs on this thread; without a recorded path, `PostgreSQL`'s default search path applies.
8
9use std::cell::RefCell;
10use std::sync::Arc;
11use uqa_core::{
12    memory::{Produced, ProductionControl},
13    ValueRetentionError,
14};
15
16use super::ColumnType;
17
18thread_local! {
19    static SEARCH_PATH: RefCell<Option<Arc<[String]>>> = const { RefCell::new(None) };
20}
21
22/// The search path diagnostics use while a statement runs on this thread. Dropping the scope restores the enclosing statement's path, so nested SQL keeps its own.
23pub struct TypeDisplayScope {
24    previous: Option<Arc<[String]>>,
25}
26
27impl TypeDisplayScope {
28    #[must_use]
29    pub fn enter(search_path: &[String]) -> Self {
30        let previous = SEARCH_PATH.with(|current| current.replace(Some(search_path.into())));
31        Self { previous }
32    }
33
34    /// Record the search path of the next statement in the same scope, which an earlier statement may have changed.
35    pub fn refresh(&self, search_path: &[String]) {
36        SEARCH_PATH.with(|current| {
37            current.replace(Some(search_path.into()));
38        });
39    }
40}
41
42impl Drop for TypeDisplayScope {
43    fn drop(&mut self) {
44        SEARCH_PATH.with(|current| {
45            current.replace(self.previous.take());
46        });
47    }
48}
49
50fn schema_visible(schema: &str) -> bool {
51    schema == "pg_catalog"
52        || SEARCH_PATH.with(|current| {
53            current.borrow().as_ref().map_or_else(
54                || schema == "public",
55                |path| path.iter().any(|entry| entry == schema),
56            )
57        })
58}
59
60/// A user-defined type's name, qualified by its schema when the schema is not visible.
61pub(super) fn visible_type_name(schema: &str, name: &str) -> String {
62    visible_type_name_with_control(schema, name, &ProductionControl::uncontrolled())
63        .expect("ordinary visible type name")
64        .into_uncontrolled()
65        .expect("ordinary type name has no reservation")
66}
67
68pub(super) fn visible_type_name_with_control(
69    schema: &str,
70    name: &str,
71    control: &ProductionControl<'_>,
72) -> Result<Produced<String>, ValueRetentionError> {
73    let local = crate::expr::quote_ident_with_control(name, control)?;
74    if schema_visible(schema) {
75        Ok(local)
76    } else {
77        let schema = crate::expr::quote_ident_with_control(schema, control)?;
78        control.format(format_args!("{}.{}", schema.as_str(), local.as_str()))
79    }
80}
81
82impl ColumnType {
83    /// `format_type_be` of this type for a diagnostic.
84    #[must_use]
85    pub fn display_name(&self) -> String {
86        self.display_name_with_control(&ProductionControl::uncontrolled())
87            .expect("ordinary diagnostic type name")
88            .into_uncontrolled()
89            .expect("ordinary type name has no reservation")
90    }
91
92    pub(crate) fn display_name_with_control(
93        &self,
94        control: &ProductionControl<'_>,
95    ) -> Result<Produced<String>, ValueRetentionError> {
96        control.check()?;
97        match self {
98            ColumnType::Enum(reference) => {
99                visible_type_name_with_control(&reference.schema, &reference.name, control)
100            }
101            ColumnType::Composite(reference) => {
102                visible_type_name_with_control(&reference.schema, &reference.name, control)
103            }
104            ColumnType::DeclaredRange(reference) | ColumnType::DeclaredMultirange(reference) => {
105                visible_type_name_with_control(&reference.schema, &reference.name, control)
106            }
107            ColumnType::Domain { schema, name, .. } => {
108                visible_type_name_with_control(schema, name, control)
109            }
110            // An array type of any dimensionality is the element's one array type.
111            ColumnType::Array(element) => {
112                let mut leaf = element.as_ref();
113                while let ColumnType::Array(inner) = leaf {
114                    control.check()?;
115                    leaf = inner;
116                }
117                if matches!(
118                    leaf,
119                    ColumnType::Enum(_)
120                        | ColumnType::Composite(_)
121                        | ColumnType::Domain { .. }
122                        | ColumnType::DeclaredRange(_)
123                        | ColumnType::DeclaredMultirange(_)
124                ) {
125                    let leaf = leaf.display_name_with_control(control)?;
126                    control.format(format_args!("{}[]", leaf.as_str()))
127                } else {
128                    self.regtype_name_with_control(control)
129                }
130            }
131            other => other.regtype_name_with_control(control),
132        }
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::TypeDisplayScope;
139    use crate::ast::{ColumnType, EnumTypeReference};
140
141    #[test]
142    fn hidden_user_types_are_qualified_in_diagnostics() {
143        let mood = ColumnType::Enum(EnumTypeReference {
144            schema: "hidden".into(),
145            name: "hmood".into(),
146            oid: 1,
147            array_oid: 2,
148        });
149        // Without a statement, the default search path applies.
150        assert_eq!(mood.display_name(), "hidden.hmood");
151        let scope = TypeDisplayScope::enter(&["public".into()]);
152        assert_eq!(mood.display_name(), "hidden.hmood");
153        assert_eq!(
154            ColumnType::Array(Box::new(mood.clone())).display_name(),
155            "hidden.hmood[]"
156        );
157        scope.refresh(&["hidden".into(), "public".into()]);
158        assert_eq!(mood.display_name(), "hmood");
159        drop(scope);
160        assert_eq!(mood.display_name(), "hidden.hmood");
161        assert_eq!(ColumnType::Integer.display_name(), "integer");
162    }
163}