spacetimedb_expr/
check.rs

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
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

use crate::expr::{Expr, Project};
use crate::{expr::LeftDeepJoin, statement::Statement};
use spacetimedb_lib::AlgebraicType;
use spacetimedb_schema::schema::TableSchema;
use spacetimedb_sql_parser::ast::BinOp;
use spacetimedb_sql_parser::{
    ast::{sub::SqlSelect, SqlFrom, SqlIdent, SqlJoin},
    parser::sub::parse_subscription,
};

use super::{
    errors::{DuplicateName, TypingError, Unresolved, Unsupported},
    expr::RelExpr,
    type_expr, type_proj, type_select, StatementCtx, StatementSource,
};

/// The result of type checking and name resolution
pub type TypingResult<T> = core::result::Result<T, TypingError>;

/// A view of the database schema
pub trait SchemaView {
    fn schema(&self, name: &str) -> Option<Arc<TableSchema>>;
}

#[derive(Default)]
pub struct Relvars(HashMap<Box<str>, Arc<TableSchema>>);

impl Deref for Relvars {
    type Target = HashMap<Box<str>, Arc<TableSchema>>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Relvars {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

pub trait TypeChecker {
    type Ast;
    type Set;

    fn type_ast(ast: Self::Ast, tx: &impl SchemaView) -> TypingResult<Project>;

    fn type_set(ast: Self::Set, vars: &mut Relvars, tx: &impl SchemaView) -> TypingResult<Project>;

    fn type_from(from: SqlFrom, vars: &mut Relvars, tx: &impl SchemaView) -> TypingResult<RelExpr> {
        match from {
            SqlFrom::Expr(SqlIdent(name), SqlIdent(alias)) => {
                let schema = Self::type_relvar(tx, &name)?;
                vars.insert(alias.clone(), schema.clone());
                Ok(RelExpr::RelVar(schema, alias))
            }
            SqlFrom::Join(SqlIdent(name), SqlIdent(alias), joins) => {
                let schema = Self::type_relvar(tx, &name)?;
                vars.insert(alias.clone(), schema.clone());
                let mut join = RelExpr::RelVar(schema, alias);

                for SqlJoin {
                    var: SqlIdent(name),
                    alias: SqlIdent(alias),
                    on,
                } in joins
                {
                    // Check for duplicate aliases
                    if vars.contains_key(&alias) {
                        return Err(DuplicateName(alias.into_string()).into());
                    }

                    let rhs = Self::type_relvar(tx, &name)?;
                    let lhs = Box::new(join);
                    let var = alias;

                    vars.insert(var.clone(), rhs.clone());

                    if let Some(on) = on {
                        if let Expr::BinOp(BinOp::Eq, a, b) = type_expr(vars, on, Some(&AlgebraicType::Bool))? {
                            if let (Expr::Field(a), Expr::Field(b)) = (*a, *b) {
                                join = RelExpr::EqJoin(LeftDeepJoin { lhs, rhs, var }, a, b);
                                continue;
                            }
                        }
                        unreachable!("Unreachability guaranteed by parser")
                    }

                    join = RelExpr::LeftDeepJoin(LeftDeepJoin { lhs, rhs, var });
                }

                Ok(join)
            }
        }
    }

    fn type_relvar(tx: &impl SchemaView, name: &str) -> TypingResult<Arc<TableSchema>> {
        tx.schema(name)
            .ok_or_else(|| Unresolved::table(name))
            .map_err(TypingError::from)
    }
}

/// Type checker for subscriptions
struct SubChecker;

impl TypeChecker for SubChecker {
    type Ast = SqlSelect;
    type Set = SqlSelect;

    fn type_ast(ast: Self::Ast, tx: &impl SchemaView) -> TypingResult<Project> {
        Self::type_set(ast, &mut Relvars::default(), tx)
    }

    fn type_set(ast: Self::Set, vars: &mut Relvars, tx: &impl SchemaView) -> TypingResult<Project> {
        match ast {
            SqlSelect {
                project,
                from,
                filter: None,
            } => {
                let input = Self::type_from(from, vars, tx)?;
                type_proj(input, project, vars)
            }
            SqlSelect {
                project,
                from,
                filter: Some(expr),
            } => {
                let input = Self::type_from(from, vars, tx)?;
                type_proj(type_select(input, expr, vars)?, project, vars)
            }
        }
    }
}

/// Parse and type check a subscription query
pub fn parse_and_type_sub(sql: &str, tx: &impl SchemaView) -> TypingResult<Project> {
    expect_table_type(SubChecker::type_ast(parse_subscription(sql)?, tx)?)
}

/// Parse and type check a *subscription* query into a `StatementCtx`
pub fn compile_sql_sub<'a>(sql: &'a str, tx: &impl SchemaView) -> TypingResult<StatementCtx<'a>> {
    let expr = parse_and_type_sub(sql, tx)?;
    Ok(StatementCtx {
        statement: Statement::Select(expr),
        sql,
        source: StatementSource::Subscription,
    })
}

/// Returns an error if the input type is not a table type or relvar
fn expect_table_type(expr: Project) -> TypingResult<Project> {
    if let Project::Fields(..) = expr {
        return Err(Unsupported::ReturnType.into());
    }
    Ok(expr)
}

pub mod test_utils {
    use spacetimedb_lib::{db::raw_def::v9::RawModuleDefV9Builder, ProductType};
    use spacetimedb_primitives::TableId;
    use spacetimedb_schema::{
        def::ModuleDef,
        schema::{Schema, TableSchema},
    };
    use std::sync::Arc;

    use super::SchemaView;

    pub fn build_module_def(types: Vec<(&str, ProductType)>) -> ModuleDef {
        let mut builder = RawModuleDefV9Builder::new();
        for (name, ty) in types {
            builder.build_table_with_new_type(name, ty, true);
        }
        builder.finish().try_into().expect("failed to generate module def")
    }

    pub struct SchemaViewer(pub ModuleDef);

    impl SchemaView for SchemaViewer {
        fn schema(&self, name: &str) -> Option<Arc<TableSchema>> {
            self.0.table(name).map(|def| {
                Arc::new(TableSchema::from_module_def(
                    &self.0,
                    def,
                    (),
                    TableId(if *def.name == *"t" { 0 } else { 1 }),
                ))
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::check::test_utils::{build_module_def, SchemaViewer};
    use spacetimedb_lib::{AlgebraicType, ProductType};
    use spacetimedb_schema::def::ModuleDef;

    use super::parse_and_type_sub;

    fn module_def() -> ModuleDef {
        build_module_def(vec![
            (
                "t",
                ProductType::from([
                    ("int", AlgebraicType::U32),
                    ("u32", AlgebraicType::U32),
                    ("f32", AlgebraicType::F32),
                    ("str", AlgebraicType::String),
                    ("arr", AlgebraicType::array(AlgebraicType::String)),
                ]),
            ),
            (
                "s",
                ProductType::from([
                    ("id", AlgebraicType::identity()),
                    ("u32", AlgebraicType::U32),
                    ("arr", AlgebraicType::array(AlgebraicType::String)),
                    ("bytes", AlgebraicType::bytes()),
                ]),
            ),
        ])
    }

    #[test]
    fn valid() {
        let tx = SchemaViewer(module_def());

        for sql in [
            "select * from t",
            "select * from t where true",
            "select * from t where t.u32 = 1",
            "select * from t where u32 = 1",
            "select * from t where t.u32 = 1 or t.str = ''",
            "select * from s where s.bytes = 0xABCD or bytes = X'ABCD'",
            "select * from s as r where r.bytes = 0xABCD or bytes = X'ABCD'",
            "select t.* from t join s",
            "select t.* from t join s join s as r where t.u32 = s.u32 and s.u32 = r.u32",
            "select t.* from t join s on t.u32 = s.u32 where t.f32 = 0.1",
        ] {
            let result = parse_and_type_sub(sql, &tx);
            assert!(result.is_ok());
        }
    }

    #[test]
    fn invalid() {
        let tx = SchemaViewer(module_def());

        for sql in [
            // Table r does not exist
            "select * from r",
            // Field a does not exist on table t
            "select * from t where t.a = 1",
            // Field a does not exist on table t
            "select * from t as r where r.a = 1",
            // Field u32 is not a string
            "select * from t where u32 = 'str'",
            // Field u32 is not a float
            "select * from t where t.u32 = 1.3",
            // t is not in scope after alias
            "select * from t as r where t.u32 = 5",
            // Subscriptions must be typed to a single table
            "select u32 from t",
            // Subscriptions must be typed to a single table
            "select * from t join s",
            // Self join requires aliases
            "select t.* from t join t",
            // Product values are not comparable
            "select t.* from t join s on t.arr = s.arr",
            // Alias r is not in scope when it is referenced
            "select t.* from t join s on t.u32 = r.u32 join s as r",
        ] {
            let result = parse_and_type_sub(sql, &tx);
            assert!(result.is_err());
        }
    }
}