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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::sync::Arc;

use spacetimedb_schema::schema::{ColumnSchema, TableSchema};
use spacetimedb_sql_parser::{
    ast::{
        self,
        sub::{SqlAst, SqlSelect},
        SqlFrom, SqlIdent, SqlJoin,
    },
    parser::sub::parse_subscription,
};

use crate::ty::TyId;

use super::{
    assert_eq_types,
    errors::{DuplicateName, TypingError, Unresolved, Unsupported},
    expr::{Expr, Let, RelExpr},
    ty::{Symbol, TyCtx, TyEnv},
    type_expr, type_proj, type_select,
};

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

pub trait SchemaView {
    fn schema(&self, name: &str) -> Option<Arc<TableSchema>>;
}

pub trait TypeChecker {
    type Ast;
    type Set;

    fn type_ast(ctx: &mut TyCtx, ast: Self::Ast, tx: &impl SchemaView) -> TypingResult<RelExpr>;

    fn type_set(ctx: &mut TyCtx, ast: Self::Set, tx: &impl SchemaView) -> TypingResult<RelExpr>;

    fn type_from(
        ctx: &mut TyCtx,
        from: SqlFrom<Self::Ast>,
        tx: &impl SchemaView,
    ) -> TypingResult<(RelExpr, Option<Symbol>)> {
        match from {
            SqlFrom::Expr(expr, None) => Self::type_rel(ctx, expr, tx),
            SqlFrom::Expr(expr, Some(SqlIdent(alias))) => {
                let (expr, _) = Self::type_rel(ctx, expr, tx)?;
                let symbol = ctx.gen_symbol(alias);
                Ok((expr, Some(symbol)))
            }
            SqlFrom::Join(r, SqlIdent(alias), joins) => {
                // The type environment with which to type the join expressions
                let mut env = TyEnv::default();
                // The lowered inputs to the join operator
                let mut inputs = Vec::new();
                // The join expressions or predicates
                let mut exprs = Vec::new();
                // The types of the join variables or aliases
                let mut types = Vec::new();

                let input = Self::type_rel(ctx, r, tx)?.0;
                let ty = input.ty_id();
                let name = ctx.gen_symbol(alias);

                env.add(name, ty);
                inputs.push(input);
                types.push((name, ty));

                for SqlJoin {
                    expr,
                    alias: SqlIdent(alias),
                    on,
                } in joins
                {
                    let input = Self::type_rel(ctx, expr, tx)?.0;
                    let ty = input.ty_id();
                    let name = ctx.gen_symbol(&alias);

                    // New join variable is now in scope
                    if env.add(name, ty).is_some() {
                        return Err(DuplicateName(alias.into_string()).into());
                    }

                    inputs.push(input);
                    types.push((name, ty));

                    // Type check join expression with current type environment
                    if let Some(on) = on {
                        exprs.push(type_expr(ctx, &env, on, Some(TyId::BOOL))?);
                    }
                }

                let ty = ctx.add_row_type(types.clone());
                let input = RelExpr::Join(inputs.into(), ty);
                let vars = types
                    .into_iter()
                    .enumerate()
                    .map(|(i, (name, ty))| (name, Expr::Field(Box::new(Expr::Input(input.ty_id())), i, ty)))
                    .collect();
                Ok((RelExpr::select(input, Let { vars, exprs }), None))
            }
        }
    }

    fn type_rel(
        ctx: &mut TyCtx,
        expr: ast::RelExpr<Self::Ast>,
        tx: &impl SchemaView,
    ) -> TypingResult<(RelExpr, Option<Symbol>)> {
        match expr {
            ast::RelExpr::Var(SqlIdent(var)) => {
                let schema = tx
                    .schema(&var)
                    .ok_or_else(|| Unresolved::table(&var))
                    .map_err(TypingError::from)?;
                let mut types = Vec::new();
                for ColumnSchema { col_name, col_type, .. } in schema.columns() {
                    let id = ctx.add_algebraic_type(col_type);
                    let name = ctx.gen_symbol(col_name);
                    types.push((name, id));
                }
                let id = ctx.add_var_type(schema.table_id, types);
                let symbol = ctx.gen_symbol(var);
                Ok((RelExpr::RelVar(schema, id), Some(symbol)))
            }
            ast::RelExpr::Ast(ast) => Ok((Self::type_ast(ctx, *ast, tx)?, None)),
        }
    }
}

/// Type checker for subscriptions
struct SubChecker;

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

    fn type_ast(ctx: &mut TyCtx, ast: Self::Ast, tx: &impl SchemaView) -> TypingResult<RelExpr> {
        Self::type_set(ctx, ast, tx)
    }

    fn type_set(ctx: &mut TyCtx, ast: Self::Set, tx: &impl SchemaView) -> TypingResult<RelExpr> {
        match ast {
            SqlAst::Union(a, b) => {
                let a = Self::type_ast(ctx, *a, tx)?;
                let b = Self::type_ast(ctx, *b, tx)?;
                assert_eq_types(ctx, a.ty_id(), b.ty_id())?;
                Ok(RelExpr::Union(Box::new(a), Box::new(b)))
            }
            SqlAst::Minus(a, b) => {
                let a = Self::type_ast(ctx, *a, tx)?;
                let b = Self::type_ast(ctx, *b, tx)?;
                assert_eq_types(ctx, a.ty_id(), b.ty_id())?;
                Ok(RelExpr::Minus(Box::new(a), Box::new(b)))
            }
            SqlAst::Select(SqlSelect {
                project,
                from,
                filter: None,
            }) => {
                let (input, alias) = Self::type_from(ctx, from, tx)?;
                type_proj(ctx, input, alias, project)
            }
            SqlAst::Select(SqlSelect {
                project,
                from,
                filter: Some(expr),
            }) => {
                let (from, alias) = Self::type_from(ctx, from, tx)?;
                let input = type_select(ctx, from, alias, expr)?;
                type_proj(ctx, input, alias, project)
            }
        }
    }
}

/// Parse and type check a subscription query
pub fn parse_and_type_sub(ctx: &mut TyCtx, sql: &str, tx: &impl SchemaView) -> TypingResult<RelExpr> {
    let expr = SubChecker::type_ast(ctx, parse_subscription(sql)?, tx)?;
    expect_table_type(ctx, expr)
}

/// Returns an error if the input type is not a table type or relvar
fn expect_table_type(ctx: &TyCtx, expr: RelExpr) -> TypingResult<RelExpr> {
    let _ = expr.ty(ctx)?.expect_relvar().map_err(|_| Unsupported::ReturnType)?;
    Ok(expr)
}

#[cfg(test)]
mod tests {
    use spacetimedb_lib::{db::raw_def::v9::RawModuleDefV9Builder, AlgebraicType, ProductType};
    use spacetimedb_primitives::TableId;
    use spacetimedb_schema::{
        def::ModuleDef,
        schema::{Schema, TableSchema},
    };
    use std::sync::Arc;

    use crate::ty::TyCtx;

    use super::{parse_and_type_sub, SchemaView};

    fn module_def() -> ModuleDef {
        let mut builder = RawModuleDefV9Builder::new();
        builder.build_table_with_new_type(
            "t",
            ProductType::from([
                ("u32", AlgebraicType::U32),
                ("f32", AlgebraicType::F32),
                ("str", AlgebraicType::String),
                ("arr", AlgebraicType::array(AlgebraicType::String)),
            ]),
            true,
        );
        builder.build_table_with_new_type(
            "s",
            ProductType::from([
                ("id", AlgebraicType::identity()),
                ("u32", AlgebraicType::U32),
                ("arr", AlgebraicType::array(AlgebraicType::String)),
                ("bytes", AlgebraicType::bytes()),
            ]),
            true,
        );
        builder.finish().try_into().expect("failed to generate module def")
    }

    struct SchemaViewer(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 }),
                ))
            })
        }
    }

    #[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 * from (select t.* from t join s)",
            "select * from (select t.* from t join s join s as r where t.u32 = s.u32 and s.u32 = r.u32)",
            "select * from (select t.* from t join s on t.u32 = s.u32 where t.f32 = 0.1)",
            "select * from (select t.* from t join (select s.u32 from s) s on t.u32 = s.u32)",
            "select * from (select t.* from t join (select u32 as a from s) s on t.u32 = s.a)",
            "select * from (select * from t union all select * from t)",
        ] {
            let result = parse_and_type_sub(&mut TyCtx::default(), 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 * from (select t.* from t join t)",
            // Product values are not comparable
            "select * from (select t.* from t join s on t.arr = s.arr)",
            // Subscriptions must be typed to a single table
            "select * from (select s.* from t join (select u32 from s) s on t.u32 = s.u32)",
            // Field u32 has been renamed
            "select * from (select t.* from t join (select u32 as a from s) s on t.u32 = s.u32)",
            // Field bytes is no longer in scope
            "select * from (select t.* from t join (select u32 from s) s on s.bytes = 0xABCD)",
            // Alias r is not in scope when it is referenced
            "select * from (select t.* from t join s on t.u32 = r.u32 join s as r)",
            // Union arguments are of different types
            "select * from (select * from t union all select * from s)",
        ] {
            let result = parse_and_type_sub(&mut TyCtx::default(), sql, &tx);
            assert!(result.is_err());
        }
    }
}