Skip to main content

sql_fun_sqlast/sem/problem/
name_resolution.rs

1mod column_not_found;
2use miette::GraphicalReportHandler;
3
4use super::{AnalysisProblem, Problem, RenderProblemReport};
5
6use crate::{
7    StringSpan,
8    sem::{
9        AnalysisError, CteName, FullName, TableName, TypeReference,
10        create_table::{ColumnName, ViewName},
11        data_source::AliasName,
12    },
13};
14
15/// Domain base type name not found
16#[derive(Debug, Clone)]
17pub struct DomainBaseTypeNameNotFound {
18    domain_type_name: FullName,
19    base_type_name: String,
20    base_type_name_span: StringSpan,
21}
22
23impl DomainBaseTypeNameNotFound {
24    /// create instance
25    pub fn new(
26        domain_type_name: &FullName,
27        base_type_name: &str,
28        base_type_name_span: &StringSpan,
29    ) -> Self {
30        Self {
31            domain_type_name: domain_type_name.clone(),
32            base_type_name: base_type_name.to_string(),
33            base_type_name_span: *base_type_name_span,
34        }
35    }
36
37    /// domain type name
38    pub fn domain_type_name(&self) -> &FullName {
39        &self.domain_type_name
40    }
41
42    /// base type name
43    pub fn base_type_name(&self) -> &str {
44        &self.base_type_name
45    }
46}
47
48impl Problem for DomainBaseTypeNameNotFound {
49    const CODE: &'static str = "SF000001";
50    const SEVERITY: miette::Severity = miette::Severity::Error;
51    const MESSAGE: &'static str = "Domain base type not found";
52    const SPAN_LABEL: &'static str = "base type name";
53
54    fn get_problem_span(&self) -> StringSpan {
55        self.base_type_name_span
56    }
57}
58
59impl RenderProblemReport for DomainBaseTypeNameNotFound {}
60
61#[derive(Debug, Clone)]
62pub struct FunctionNotFound {
63    function_name: FullName,
64    function_span: StringSpan,
65}
66
67impl FunctionNotFound {
68    /// create instance
69    pub fn new(function_name: &FullName, function_span: &StringSpan) -> Self {
70        Self {
71            function_name: function_name.clone(),
72            function_span: *function_span,
73        }
74    }
75
76    /// specified function name
77    pub fn function_name(&self) -> &FullName {
78        &self.function_name
79    }
80
81    /// span of function name
82    pub fn function_span(&self) -> &StringSpan {
83        &self.function_span
84    }
85}
86
87impl Problem for FunctionNotFound {
88    const CODE: &'static str = "SF00002";
89    const MESSAGE: &'static str = "Function not found";
90    const SEVERITY: miette::Severity = miette::Severity::Error;
91    const SPAN_LABEL: &'static str = "function name";
92
93    fn get_problem_span(&self) -> StringSpan {
94        self.function_span
95    }
96}
97
98impl RenderProblemReport for FunctionNotFound {}
99
100/// Name resolution problems
101#[derive(Debug, Clone)]
102pub enum NameResolution {
103    #[doc = include_str!("domain_type_name_not_found.md")]
104    DomainTypeNameNotFound(DomainBaseTypeNameNotFound),
105
106    #[doc = include_str!("function_not_found.md")]
107    FunctionNotFound(FunctionNotFound),
108
109    /// table function not found
110    TableFunctionNotFound(FullName),
111
112    /// column reference not resolved
113    UndefinedColumn(String),
114
115    /// type not found in column definition
116    ColumnTypeNotFound(TableName, ColumnName, FullName),
117
118    /// column not found in table function
119    ColumnNotFoundInTableFunction(AliasName, ColumnName),
120
121    /// column not found in table
122    ColumnNotFoundInTable(TableName, ColumnName),
123
124    /// column not found in CTE
125    ColumnNotFoundInCte(CteName, ColumnName),
126
127    /// column not found in subquery
128    ColumnNotFoundInSubQuery(AliasName, ColumnName),
129
130    /// column not found in source
131    ColumnNotFoundInSource(ColumnName),
132
133    /// column not found in view
134    ColumnNotFoundInView(ViewName, ColumnName),
135
136    /// column reference ambiguous
137    AmbiguousColumn(ColumnName),
138
139    /// same alias defined
140    DuplicateAlias(AliasName),
141
142    /// data source not defined in from clause
143    UndefinedAlias(String),
144
145    /// table .0 not found in current context
146    RelationNotFound(String),
147
148    /// view not found
149    ViewNotFound(ViewName),
150
151    /// index not found
152    IndexNotFound(String),
153
154    /// sequence not found
155    SequenceNotFound(FullName),
156
157    /// table not found
158    TableNotFound(TableName),
159
160    /// composite type field not found
161    CompositeTypeFieldNotFound(TypeReference, String),
162}
163
164impl NameResolution {
165    pub(crate) fn render_report(
166        &self,
167        report_handler: &GraphicalReportHandler,
168        source: String,
169    ) -> Result<String, AnalysisError> {
170        match self {
171            Self::DomainTypeNameNotFound(d) => d.generate_report(report_handler, source),
172            Self::FunctionNotFound(d) => d.generate_report(report_handler, source),
173            _ => todo!(),
174        }
175    }
176
177    pub(crate) fn duplicate_alias(alias_name: &AliasName) -> Self {
178        Self::DuplicateAlias(alias_name.clone())
179    }
180
181    pub(crate) fn composite_type_field_not_found(
182        type_ref: &TypeReference,
183        field_name: &str,
184    ) -> Self {
185        Self::CompositeTypeFieldNotFound(type_ref.clone(), field_name.to_string())
186    }
187
188    pub(crate) fn ambiguous_column(column_name: &ColumnName) -> Self {
189        Self::AmbiguousColumn(column_name.clone())
190    }
191
192    pub(crate) fn table_not_found(table_name: &TableName) -> Self {
193        Self::TableNotFound(table_name.clone())
194    }
195
196    pub(crate) fn sequence_not_found(sequence_name: &FullName) -> Self {
197        Self::SequenceNotFound(sequence_name.clone())
198    }
199
200    pub(crate) fn index_not_found(index_name: &str) -> Self {
201        Self::IndexNotFound(index_name.to_string())
202    }
203
204    pub(crate) fn view_not_found(view_name: &ViewName) -> Self {
205        Self::ViewNotFound(view_name.clone())
206    }
207
208    /// domain not found
209    #[tracing::instrument()]
210    pub fn domain_type_name_not_found(
211        domain_name: &FullName,
212        type_name: &crate::syn::TypeName,
213        type_name_span: &StringSpan,
214    ) -> Self {
215        let problem = DomainBaseTypeNameNotFound::new(
216            domain_name,
217            type_name.full_name().as_str(),
218            type_name_span,
219        );
220
221        Self::DomainTypeNameNotFound(problem)
222    }
223
224    /// specified table not found
225    #[tracing::instrument()]
226    pub fn relation_not_found(table_name: &TableName) -> Self {
227        Self::RelationNotFound(table_name.to_string())
228    }
229
230    /// calling function not found
231    pub fn function_not_found(func_name: &FullName, func_span: &StringSpan) -> Self {
232        Self::FunctionNotFound(FunctionNotFound::new(func_name, func_span))
233    }
234
235    /// specified table function not found
236    pub fn table_function_not_found(call: &FullName) -> Self {
237        Self::TableFunctionNotFound(call.clone())
238    }
239
240    /// table column type
241    #[tracing::instrument()]
242    pub fn column_type_not_found(
243        table_name: &TableName,
244        colname: &ColumnName,
245        type_name: &FullName,
246    ) -> Self {
247        Self::ColumnTypeNotFound(table_name.clone(), colname.clone(), type_name.clone())
248    }
249
250    /// alias name undefined
251    pub fn undefined_alias(alias_name: &str) -> Self {
252        Self::UndefinedAlias(alias_name.to_owned())
253    }
254}
255
256impl AnalysisProblem {
257    /// alias name duplicate in `FROM` clause
258    #[tracing::instrument()]
259    pub fn duplicate_alias(alias_name: &AliasName) -> Self {
260        Self::NameResolution(NameResolution::duplicate_alias(alias_name))
261    }
262
263    /// accessing member not found in complex type
264    #[track_caller]
265    #[tracing::instrument()]
266    pub fn composite_type_field_not_found(
267        composite_type: &TypeReference,
268        field_name: &str,
269    ) -> Self {
270        tracing::warn!(
271            "complex type member not found {composite_type:?} member_name: {field_name}"
272        );
273        Self::NameResolution(NameResolution::composite_type_field_not_found(
274            composite_type,
275            field_name,
276        ))
277    }
278
279    /// same column name existing from clause
280    #[tracing::instrument()]
281    pub fn ambiguous_column(column: &ColumnName) -> Self {
282        Self::NameResolution(NameResolution::ambiguous_column(column))
283    }
284
285    /// alter table not found
286    #[tracing::instrument()]
287    pub fn table_not_found(table_name: &TableName) -> Self {
288        Self::NameResolution(NameResolution::table_not_found(table_name))
289    }
290
291    /// sequence not found
292    #[tracing::instrument()]
293    pub fn sequence_not_found(seq_name: &FullName) -> Self {
294        Self::NameResolution(NameResolution::sequence_not_found(seq_name))
295    }
296
297    /// specified index not found
298    #[tracing::instrument()]
299    pub fn index_not_found(index_name: &str) -> Self {
300        Self::NameResolution(NameResolution::index_not_found(index_name))
301    }
302
303    /// specified view not found
304    #[track_caller]
305    #[tracing::instrument()]
306    pub fn view_not_found(view_name: &ViewName) -> Self {
307        Self::NameResolution(NameResolution::view_not_found(view_name))
308    }
309
310    /// domain not found
311    #[tracing::instrument()]
312    pub fn domain_type_name_not_found(
313        domain_name: &FullName,
314        type_name: &crate::syn::TypeName,
315        type_name_span: &StringSpan,
316    ) -> Self {
317        Self::NameResolution(NameResolution::domain_type_name_not_found(
318            domain_name,
319            type_name,
320            type_name_span,
321        ))
322    }
323
324    /// specified table not found
325    #[tracing::instrument()]
326    pub fn relation_not_found(table_name: &TableName) -> Self {
327        Self::NameResolution(NameResolution::relation_not_found(table_name))
328    }
329
330    /// calling function not found
331    #[track_caller]
332    #[tracing::instrument()]
333    pub fn function_not_found(func_name: &FullName, func_span: &StringSpan) -> Self {
334        Self::NameResolution(NameResolution::function_not_found(func_name, func_span))
335    }
336
337    /// specified table function not found
338    #[track_caller]
339    #[tracing::instrument()]
340    pub fn table_function_not_found(func: &FullName) -> Self {
341        Self::NameResolution(NameResolution::table_function_not_found(func))
342    }
343
344    /// table column type
345    #[tracing::instrument()]
346    pub fn column_type_not_found(
347        table_name: &TableName,
348        colname: &ColumnName,
349        type_name: &FullName,
350    ) -> Self {
351        Self::NameResolution(NameResolution::column_type_not_found(
352            table_name, colname, type_name,
353        ))
354    }
355
356    /// undefined alias
357    #[must_use]
358    pub fn undefined_alias(alias_name: String) -> Self {
359        Self::NameResolution(NameResolution::undefined_alias(&alias_name))
360    }
361}