Skip to main content

sql_fun_sqlast/sem/schema_file_context/
func_info.rs

1use crate::{
2    TrieMap,
3    sem::{
4        AnalysisError, BaseParseContext, CreateFunction, FullName, FunctionInfoRead,
5        FunctionInfoWrite, FunctionOverloadCollection,
6    },
7};
8
9use super::SchemaFileContext;
10
11#[derive(Debug, Default)]
12pub struct FunctionDefinitionCollection(TrieMap<FunctionOverloadCollection>);
13
14impl FunctionDefinitionCollection {
15    pub fn get(&self, name: &FullName) -> Option<&FunctionOverloadCollection> {
16        self.0.get(&name.to_string())
17    }
18
19    pub fn insert(&mut self, name: &FullName, create_function: &CreateFunction) {
20        let key = &name.to_string();
21        if let Some(overload_collection) = self.0.get_mut(key) {
22            overload_collection.append(create_function.overloads());
23        } else {
24            let mut overload_collection = FunctionOverloadCollection::default();
25            overload_collection.append(create_function.overloads());
26            self.0.insert(key, overload_collection);
27        }
28    }
29}
30
31impl<TBaseContext> FunctionInfoRead for SchemaFileContext<TBaseContext>
32where
33    TBaseContext: BaseParseContext + std::fmt::Debug,
34{
35    fn get_function_impl(&self, name: &FullName) -> Option<FunctionOverloadCollection> {
36        let self_funcs = self.functions.get(name);
37        let base_funcs = self.base.get_function_impl(name);
38
39        match (self_funcs, base_funcs) {
40            (Some(s), Some(b)) => Some(s.clone().merged(b)),
41            (Some(s), None) => Some(s.clone()),
42            (None, Some(b)) => Some(b.clone()),
43            (None, None) => None,
44        }
45    }
46}
47
48impl<TBaseContext> FunctionInfoWrite for SchemaFileContext<TBaseContext>
49where
50    TBaseContext: BaseParseContext + std::fmt::Debug,
51{
52    #[tracing::instrument(skip(self))]
53    fn apply_create_function_impl(
54        mut self,
55        name: &FullName,
56        create_function: &CreateFunction,
57    ) -> Result<Self, AnalysisError> {
58        self.functions.insert(name, create_function);
59        Ok(self)
60    }
61}