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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use anyhow::Result;
use enum_as_inner::EnumAsInner;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug};

use super::module::{Module, NS_DEFAULT_DB, NS_NO_RESOLVE, NS_STD};
use crate::ast::pl::*;
use crate::error::Span;

/// Context of the pipeline.
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Context {
    /// Map of all accessible names (for each namespace)
    pub(crate) root_mod: Module,

    pub(crate) span_map: HashMap<usize, Span>,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Decl {
    pub declared_at: Option<usize>,

    pub kind: DeclKind,
}

#[derive(Debug, Serialize, Deserialize, Clone, EnumAsInner)]
pub enum DeclKind {
    /// A nested namespace
    Module(Module),

    /// Nested namespaces that do lookup in layers from top to bottom, stoping at first match.
    LayeredModules(Vec<Module>),

    TableDecl(TableDecl),

    Column(usize),

    /// Contains a default value to be created in parent namespace matched.
    Wildcard(Box<DeclKind>),

    FuncDef(FuncDef),

    Expr(Box<Expr>),

    NoResolve,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TableDecl {
    /// Columns layout
    pub frame: TableFrame,

    /// None means that this is an extern table (actual table in database)
    /// Some means a CTE
    pub expr: Option<Box<Expr>>,
}

#[derive(Clone, Default, Eq, Debug, PartialEq, Serialize, Deserialize)]
pub struct TableFrame {
    pub columns: Vec<TableColumn>,
}

#[derive(Clone, Eq, Debug, PartialEq, Serialize, Deserialize)]
pub enum TableColumn {
    Wildcard,
    Single(Option<String>),
}

impl Context {
    pub fn declare_func(&mut self, func_def: FuncDef, id: Option<usize>) {
        let name = func_def.name.clone();

        let path = vec![NS_STD.to_string()];
        let ident = Ident { name, path };

        let decl = Decl {
            kind: DeclKind::FuncDef(func_def),
            declared_at: id,
        };
        self.root_mod.insert(ident, decl).unwrap();
    }

    pub fn declare_table(&mut self, table_def: TableDef, id: Option<usize>) {
        let name = table_def.name;
        let path = vec![NS_DEFAULT_DB.to_string()];
        let ident = Ident { name, path };

        let frame = table_def.value.ty.clone().unwrap().into_table().unwrap();
        let frame = TableFrame {
            columns: (frame.columns.into_iter())
                .map(|col| match col {
                    FrameColumn::Wildcard { .. } => TableColumn::Wildcard,
                    FrameColumn::Single { name, .. } => TableColumn::Single(name.map(|n| n.name)),
                })
                .collect(),
        };

        let expr = Some(table_def.value);
        let decl = Decl {
            declared_at: id,
            kind: DeclKind::TableDecl(TableDecl { frame, expr }),
        };

        self.root_mod.insert(ident, decl).unwrap();
    }

    pub fn resolve_ident(&mut self, ident: &Ident) -> Result<Ident, String> {
        // lookup the name
        if ident.name != "*" {
            let decls = self.root_mod.lookup(ident);

            match decls.len() {
                // no match: try match *
                0 => {}

                // single match, great!
                1 => return Ok(decls.into_iter().next().unwrap()),

                // ambiguous
                _ => {
                    let decls = decls.into_iter().map(|d| d.to_string()).join(", ");
                    return Err(format!("Ambiguous reference. Could be from any of {decls}"));
                }
            }
        }

        // this variable can be from a namespace that we don't know all columns of
        let decls = self.root_mod.lookup(&Ident {
            path: ident.path.clone(),
            name: "*".to_string(),
        });

        match decls.len() {
            0 => Err(format!("Unknown name {ident}")),

            // single match, great!
            1 => {
                let wildcard_ident = decls.into_iter().next().unwrap();

                let wildcard = self.root_mod.get(&wildcard_ident).unwrap();
                let wildcard_default = wildcard.kind.as_wildcard().cloned().unwrap();

                let module_ident = wildcard_ident.pop().unwrap();
                let module = self.root_mod.get_mut(&module_ident).unwrap();
                let module = module.kind.as_module_mut().unwrap();

                // insert default
                module
                    .names
                    .insert(ident.name.clone(), Decl::from(*wildcard_default));

                // table columns
                if let Some(table_ident) = module.instance_of_table.clone() {
                    log::debug!("infering {ident} to be from table {table_ident}");
                    self.infer_table_column(&table_ident, &ident.name)?;
                }

                Ok(module_ident + Ident::from_name(ident.name.clone()))
            }

            // don't report ambiguous variable, database may be able to resolve them
            _ => {
                // insert default
                let ident = NS_NO_RESOLVE.to_string();
                self.root_mod
                    .names
                    .insert(ident, Decl::from(DeclKind::NoResolve));

                log::debug!(
                    "... could either of {:?}",
                    decls.iter().map(|x| x.to_string()).collect_vec()
                );

                Ok(Ident::from_name(NS_NO_RESOLVE))
            }
        }
    }

    fn infer_table_column(&mut self, table_ident: &Ident, col_name: &str) -> Result<(), String> {
        let table = self.root_mod.get_mut(table_ident).unwrap();
        let table_decl = table.kind.as_table_decl_mut().unwrap();

        let has_wildcard =
            (table_decl.frame.columns.iter()).any(|c| matches!(c, TableColumn::Wildcard));
        if !has_wildcard {
            return Err(format!("Table {table_ident:?} does not have wildcard."));
        }

        let exists = table_decl.frame.columns.iter().any(|c| match c {
            TableColumn::Single(Some(n)) => n == col_name,
            _ => false,
        });
        if exists {
            return Ok(());
        }

        let col = TableColumn::Single(Some(col_name.to_string()));
        table_decl.frame.columns.push(col);

        // also add into input tables of this table expression
        if let Some(expr) = &table_decl.expr {
            if let Some(Ty::Table(frame)) = expr.ty.as_ref() {
                let wildcard_inputs = (frame.columns.iter())
                    .filter_map(|c| c.as_wildcard())
                    .collect_vec();

                match wildcard_inputs.len() {
                    0 => return Err(format!("Cannot infer where {table_ident}.{col_name} is from")),
                    1 => {
                        let input_name = wildcard_inputs.into_iter().next().unwrap();

                        let input = frame.find_input(input_name).unwrap();
                        let table_ident = input.table.clone();
                        self.infer_table_column(&table_ident, col_name)?;
                    }
                    _ => {
                        return Err(format!("Cannot infer where {table_ident}.{col_name} is from. It could be any of {wildcard_inputs:?}"))
                    }
                }
            }
        }

        Ok(())
    }
}

impl Default for DeclKind {
    fn default() -> Self {
        DeclKind::Module(Module::default())
    }
}

impl Debug for Context {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.root_mod.fmt(f)
    }
}

impl From<DeclKind> for Decl {
    fn from(kind: DeclKind) -> Self {
        Decl {
            kind,
            declared_at: None,
        }
    }
}

impl std::fmt::Display for Decl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.kind, f)
    }
}

impl std::fmt::Display for DeclKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Module(arg0) => f.debug_tuple("Module").field(arg0).finish(),
            Self::LayeredModules(arg0) => f.debug_tuple("LayeredModules").field(arg0).finish(),
            Self::TableDecl(TableDecl { frame, expr }) => write!(f, "TableDef: {frame} {expr:?}"),
            Self::Column(arg0) => write!(f, "Column (target {arg0})"),
            Self::Wildcard(arg0) => write!(f, "Wildcard (default: {arg0})"),
            Self::FuncDef(arg0) => write!(f, "FuncDef: {arg0}"),
            Self::Expr(arg0) => write!(f, "Expr: {arg0}"),
            Self::NoResolve => write!(f, "NoResolve"),
        }
    }
}

impl std::fmt::Display for TableFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("[")?;
        for (index, col) in self.columns.iter().enumerate() {
            let is_last = index == self.columns.len() - 1;

            let col = match col {
                TableColumn::Wildcard => "*",
                TableColumn::Single(name) => name.as_deref().unwrap_or("<unnamed>"),
            };
            f.write_str(col)?;
            if !is_last {
                f.write_str(", ")?;
            }
        }
        f.write_str("]")
    }
}