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
use crate::call_defs::CallDef;
use partiql_value::Value;
use std::borrow::Cow;

use std::collections::HashMap;
use std::error::Error;
use std::fmt::Debug;
use std::sync::atomic::{AtomicU64, Ordering};
use thiserror::Error;
use unicase::UniCase;

pub mod call_defs;

pub trait Extension: Debug {
    fn name(&self) -> String;
    fn load(&self, catalog: &mut dyn Catalog) -> Result<(), Box<dyn Error>>;
}

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)]
struct CatalogId(u64);

impl From<u64> for CatalogId {
    fn from(value: u64) -> Self {
        CatalogId(value)
    }
}

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)]
struct EntryId(u64);

impl From<u64> for EntryId {
    fn from(value: u64) -> Self {
        EntryId(value)
    }
}

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)]
pub struct ObjectId {
    catalog_id: CatalogId,
    entry_id: EntryId,
}

pub type BaseTableExprResultError = Box<dyn Error>;
pub type BaseTableExprResultValueIter<'a> =
    Box<dyn 'a + Iterator<Item = Result<Value, BaseTableExprResultError>>>;
pub type BaseTableExprResult<'a> =
    Result<BaseTableExprResultValueIter<'a>, BaseTableExprResultError>;

pub trait BaseTableExpr: Debug {
    fn evaluate(&self, args: &[Cow<Value>]) -> BaseTableExprResult;
}

pub trait BaseTableFunctionInfo: Debug {
    fn call_def(&self) -> &CallDef;
    fn plan_eval(&self) -> Box<dyn BaseTableExpr>;
}

#[derive(Debug)]
pub struct TableFunction {
    info: Box<dyn BaseTableFunctionInfo>,
}

impl TableFunction {
    pub fn new(info: Box<dyn BaseTableFunctionInfo>) -> Self {
        TableFunction { info }
    }
}

/// Catalog Errors.
///
/// ### Notes
/// This is marked `#[non_exhaustive]`, to reserve the right to add more variants in the future.
#[derive(Error, Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum CatalogError {
    /// Entry exists error.
    #[error("Catalog error: entry already exists for `{}`", .0)]
    EntryExists(String),

    /// Entry error.
    #[error("Catalog error: `{}`", .0)]
    EntryError(String),

    /// Any other catalog error.
    #[error("Catalog error: unknown error")]
    Unknown,
}

pub trait Catalog: Debug {
    fn add_table_function(&mut self, info: TableFunction) -> Result<ObjectId, CatalogError>;

    fn get_function(&self, name: &str) -> Option<FunctionEntry>;
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct FunctionEntry<'a> {
    id: ObjectId,
    function: &'a FunctionEntryFunction,
}

#[derive(Debug)]
pub enum FunctionEntryFunction {
    Table(TableFunction),
    Scalar(),
    Aggregate(),
}

impl<'a> FunctionEntry<'a> {
    pub fn call_def(&'a self) -> &'a CallDef {
        match &self.function {
            FunctionEntryFunction::Table(tf) => tf.info.call_def(),
            FunctionEntryFunction::Scalar() => todo!(),
            FunctionEntryFunction::Aggregate() => todo!(),
        }
    }

    pub fn plan_eval(&'a self) -> Box<dyn BaseTableExpr> {
        match &self.function {
            FunctionEntryFunction::Table(tf) => tf.info.plan_eval(),
            FunctionEntryFunction::Scalar() => todo!(),
            FunctionEntryFunction::Aggregate() => todo!(),
        }
    }
}

#[derive(Debug)]
pub struct PartiqlCatalog {
    functions: CatalogEntrySet<FunctionEntryFunction>,

    id: CatalogId,
}

impl Default for PartiqlCatalog {
    fn default() -> Self {
        PartiqlCatalog {
            functions: Default::default(),

            id: CatalogId(1),
        }
    }
}

impl PartiqlCatalog {}

impl Catalog for PartiqlCatalog {
    fn add_table_function(&mut self, info: TableFunction) -> Result<ObjectId, CatalogError> {
        let call_def = info.info.call_def();
        let names = call_def.names.clone();
        if let Some((name, aliases)) = names.split_first() {
            let id = self
                .functions
                .add(name, aliases, FunctionEntryFunction::Table(info))?;
            Ok(ObjectId {
                catalog_id: self.id,
                entry_id: id,
            })
        } else {
            Err(CatalogError::EntryError(
                "Function definition has no name".into(),
            ))
        }
    }

    fn get_function(&self, name: &str) -> Option<FunctionEntry> {
        self.functions
            .find_by_name(name)
            .map(|(eid, entry)| FunctionEntry {
                id: ObjectId {
                    catalog_id: self.id,
                    entry_id: eid,
                },
                function: entry,
            })
    }
}

#[derive(Debug)]
struct CatalogEntrySet<T> {
    entries: HashMap<EntryId, T>,
    by_name: HashMap<UniCase<String>, EntryId>,

    next_id: AtomicU64,
}

impl<T> Default for CatalogEntrySet<T> {
    fn default() -> Self {
        CatalogEntrySet {
            entries: Default::default(),
            by_name: Default::default(),
            next_id: 1.into(),
        }
    }
}

impl<T> CatalogEntrySet<T> {
    fn add(&mut self, name: &str, _aliases: &[&str], info: T) -> Result<EntryId, CatalogError> {
        let name = UniCase::from(name);
        if self.by_name.contains_key(&name) {
            return Err(CatalogError::EntryExists(name.to_string()));
        }

        let id = self.next_id.fetch_add(1, Ordering::SeqCst).into();
        if let Some(_old_val) = self.entries.insert(id, info) {
            return Err(CatalogError::Unknown);
        }

        self.by_name.insert(name, id);

        Ok(id)
    }

    fn find_by_name(&self, name: &str) -> Option<(EntryId, &T)> {
        let name = UniCase::from(name);
        if let Some(eid) = self.by_name.get(&name) {
            self.entries.get(eid).map(|e| (*eid, e))
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn todo() {}
}