spacetimedb_expr/
statement.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::sync::Arc;

use spacetimedb_lib::{AlgebraicType, AlgebraicValue};
use spacetimedb_primitives::ColId;
use spacetimedb_schema::schema::{ColumnSchema, TableSchema};
use spacetimedb_sql_parser::{
    ast::{
        sql::{SqlAst, SqlDelete, SqlInsert, SqlSelect, SqlSet, SqlShow, SqlUpdate},
        SqlIdent, SqlLiteral,
    },
    parser::sql::parse_sql,
};
use thiserror::Error;

use crate::{check::Relvars, expr::Project};

use super::{
    check::{SchemaView, TypeChecker, TypingResult},
    errors::{InsertFieldsError, InsertValuesError, TypingError, UnexpectedType, Unresolved},
    expr::Expr,
    parse, type_expr, type_proj, type_select, StatementCtx, StatementSource,
};

pub enum Statement {
    Select(Project),
    Insert(TableInsert),
    Update(TableUpdate),
    Delete(TableDelete),
    Set(SetVar),
    Show(ShowVar),
}

/// A resolved row of literal values for an insert
pub type Row = Box<[AlgebraicValue]>;

pub struct TableInsert {
    pub into: Arc<TableSchema>,
    pub rows: Box<[Row]>,
}

pub struct TableDelete {
    pub from: Arc<TableSchema>,
    pub expr: Option<Expr>,
}

pub struct TableUpdate {
    pub schema: Arc<TableSchema>,
    pub values: Box<[(ColId, AlgebraicValue)]>,
    pub filter: Option<Expr>,
}

pub struct SetVar {
    pub name: String,
    pub value: AlgebraicValue,
}

pub struct ShowVar {
    pub name: String,
}

/// Type check an INSERT statement
pub fn type_insert(insert: SqlInsert, tx: &impl SchemaView) -> TypingResult<TableInsert> {
    let SqlInsert {
        table: SqlIdent(table_name),
        fields,
        values,
    } = insert;

    let schema = tx
        .schema(&table_name)
        .ok_or_else(|| Unresolved::table(&table_name))
        .map_err(TypingError::from)?;

    // Expect n fields
    let n = schema.columns().len();
    if fields.len() != schema.columns().len() {
        return Err(TypingError::from(InsertFieldsError {
            table: table_name.into_string(),
            nfields: fields.len(),
            ncols: schema.columns().len(),
        }));
    }

    let mut rows = Vec::new();
    for row in values.0 {
        // Expect each row to have n values
        if row.len() != n {
            return Err(TypingError::from(InsertValuesError {
                table: table_name.into_string(),
                values: row.len(),
                fields: n,
            }));
        }
        let mut values = Vec::new();
        for (value, ty) in row
            .into_iter()
            .zip(schema.columns().iter().map(|ColumnSchema { col_type, .. }| col_type))
        {
            match (value, ty) {
                (SqlLiteral::Bool(v), AlgebraicType::Bool) => {
                    values.push(AlgebraicValue::Bool(v));
                }
                (SqlLiteral::Str(v), AlgebraicType::String) => {
                    values.push(AlgebraicValue::String(v));
                }
                (SqlLiteral::Bool(_), _) => {
                    return Err(UnexpectedType::new(&AlgebraicType::Bool, ty).into());
                }
                (SqlLiteral::Str(_), _) => {
                    return Err(UnexpectedType::new(&AlgebraicType::String, ty).into());
                }
                (SqlLiteral::Hex(v), ty) | (SqlLiteral::Num(v), ty) => {
                    values.push(parse(v.into_string(), ty)?);
                }
            }
        }
        rows.push(values.into_boxed_slice());
    }
    let into = schema;
    let rows = rows.into_boxed_slice();
    Ok(TableInsert { into, rows })
}

/// Type check a DELETE statement
pub fn type_delete(delete: SqlDelete, tx: &impl SchemaView) -> TypingResult<TableDelete> {
    let SqlDelete {
        table: SqlIdent(table_name),
        filter,
    } = delete;
    let from = tx
        .schema(&table_name)
        .ok_or_else(|| Unresolved::table(&table_name))
        .map_err(TypingError::from)?;
    let mut vars = Relvars::default();
    vars.insert(table_name.clone(), from.clone());
    let expr = filter
        .map(|expr| type_expr(&vars, expr, Some(&AlgebraicType::Bool)))
        .transpose()?;
    Ok(TableDelete { from, expr })
}

/// Type check an UPDATE statement
pub fn type_update(update: SqlUpdate, tx: &impl SchemaView) -> TypingResult<TableUpdate> {
    let SqlUpdate {
        table: SqlIdent(table_name),
        assignments,
        filter,
    } = update;
    let schema = tx
        .schema(&table_name)
        .ok_or_else(|| Unresolved::table(&table_name))
        .map_err(TypingError::from)?;
    let mut values = Vec::new();
    for SqlSet(SqlIdent(field), lit) in assignments {
        let ColumnSchema {
            col_pos: col_id,
            col_type: ty,
            ..
        } = schema
            .get_column_by_name(&field)
            .ok_or_else(|| Unresolved::field(&table_name, &field))?;
        match (lit, ty) {
            (SqlLiteral::Bool(v), AlgebraicType::Bool) => {
                values.push((*col_id, AlgebraicValue::Bool(v)));
            }
            (SqlLiteral::Str(v), AlgebraicType::String) => {
                values.push((*col_id, AlgebraicValue::String(v)));
            }
            (SqlLiteral::Bool(_), _) => {
                return Err(UnexpectedType::new(&AlgebraicType::Bool, ty).into());
            }
            (SqlLiteral::Str(_), _) => {
                return Err(UnexpectedType::new(&AlgebraicType::String, ty).into());
            }
            (SqlLiteral::Hex(v), ty) | (SqlLiteral::Num(v), ty) => {
                values.push((*col_id, parse(v.into_string(), ty)?));
            }
        }
    }
    let mut vars = Relvars::default();
    vars.insert(table_name.clone(), schema.clone());
    let values = values.into_boxed_slice();
    let filter = filter
        .map(|expr| type_expr(&vars, expr, Some(&AlgebraicType::Bool)))
        .transpose()?;
    Ok(TableUpdate { schema, values, filter })
}

#[derive(Error, Debug)]
#[error("{name} is not a valid system variable")]
pub struct InvalidVar {
    pub name: String,
}

const VAR_ROW_LIMIT: &str = "row_limit";
const VAR_SLOW_QUERY: &str = "slow_ad_hoc_query_ms";
const VAR_SLOW_UPDATE: &str = "slow_tx_update_ms";
const VAR_SLOW_SUB: &str = "slow_subscription_query_ms";

fn is_var_valid(var: &str) -> bool {
    var == VAR_ROW_LIMIT || var == VAR_SLOW_QUERY || var == VAR_SLOW_UPDATE || var == VAR_SLOW_SUB
}

pub fn type_set(set: SqlSet) -> TypingResult<SetVar> {
    let SqlSet(SqlIdent(name), lit) = set;
    if !is_var_valid(&name) {
        return Err(InvalidVar {
            name: name.into_string(),
        }
        .into());
    }
    match lit {
        SqlLiteral::Bool(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::Bool).into()),
        SqlLiteral::Str(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::String).into()),
        SqlLiteral::Hex(_) => Err(UnexpectedType::new(&AlgebraicType::U64, &AlgebraicType::bytes()).into()),
        SqlLiteral::Num(n) => Ok(SetVar {
            name: name.into_string(),
            value: parse(n.into_string(), &AlgebraicType::U64)?,
        }),
    }
}

pub fn type_show(show: SqlShow) -> TypingResult<ShowVar> {
    let SqlShow(SqlIdent(name)) = show;
    if !is_var_valid(&name) {
        return Err(InvalidVar {
            name: name.into_string(),
        }
        .into());
    }
    Ok(ShowVar {
        name: name.into_string(),
    })
}

/// Type-checker for regular `SQL` queries
struct SqlChecker;

impl TypeChecker for SqlChecker {
    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)
            }
        }
    }
}

fn parse_and_type_sql(sql: &str, tx: &impl SchemaView) -> TypingResult<Statement> {
    match parse_sql(sql)? {
        SqlAst::Insert(insert) => Ok(Statement::Insert(type_insert(insert, tx)?)),
        SqlAst::Delete(delete) => Ok(Statement::Delete(type_delete(delete, tx)?)),
        SqlAst::Update(update) => Ok(Statement::Update(type_update(update, tx)?)),
        SqlAst::Select(ast) => Ok(Statement::Select(SqlChecker::type_ast(ast, tx)?)),
        SqlAst::Set(set) => Ok(Statement::Set(type_set(set)?)),
        SqlAst::Show(show) => Ok(Statement::Show(type_show(show)?)),
    }
}

/// Parse and type check a *general* query into a [StatementCtx].
pub fn compile_sql_stmt<'a>(sql: &'a str, tx: &impl SchemaView) -> TypingResult<StatementCtx<'a>> {
    let statement = parse_and_type_sql(sql, tx)?;
    Ok(StatementCtx {
        statement,
        sql,
        source: StatementSource::Query,
    })
}

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

    use crate::{
        check::test_utils::{build_module_def, SchemaViewer},
        statement::parse_and_type_sql,
    };

    fn module_def() -> ModuleDef {
        build_module_def(vec![
            (
                "t",
                ProductType::from([
                    ("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 str from t",
            "select str, arr from t",
            "select t.str, arr from t",
        ] {
            let result = parse_and_type_sql(sql, &tx);
            assert!(result.is_ok());
        }
    }

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

        // Unqualified columns in a join
        let sql = "select id, str from s join t";
        let result = parse_and_type_sql(sql, &tx);
        assert!(result.is_err());
    }
}