toasty 0.4.0

An async ORM for Rust supporting SQL and NoSQL databases
Documentation
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::collections::{HashMap, hash_map};

use by_address::ByAddress;
use toasty_core::{schema::db::Index, stmt};

use crate::engine::index::PartitionCtx;

#[derive(Debug)]
pub(super) struct IndexMatch<'stmt> {
    /// The index in question
    pub(super) index: &'stmt Index,

    /// Restriction matches for each column
    pub(super) columns: Vec<IndexColumnMatch<'stmt>>,
}

#[derive(Debug)]
pub(super) struct IndexColumnMatch<'stmt> {
    pub(super) exprs: HashMap<ByAddress<&'stmt stmt::Expr>, ExprMatch>,
}

#[derive(Debug, Copy, Clone)]
pub(super) struct ExprMatch {
    pub(super) eq: bool,
}

type ExprPair = (stmt::Expr, stmt::Expr);

impl<'stmt> IndexMatch<'stmt> {
    pub(super) fn match_restriction(
        &mut self,
        cx: &stmt::ExprContext<'_>,
        expr: &'stmt stmt::Expr,
    ) -> bool {
        use stmt::Expr::*;

        match expr {
            BinaryOp(e) => match (&*e.lhs, &*e.rhs) {
                (stmt::Expr::Reference(lhs @ stmt::ExprReference::Column(_)), rhs) => {
                    assert!(
                        !rhs.is_expr_reference(),
                        "TODO: handle ExprReference on both sides"
                    );
                    self.match_expr_binary_op_column(cx, lhs, expr, e.op)
                }
                (_, stmt::Expr::Reference(rhs @ stmt::ExprReference::Column(_))) => {
                    self.match_expr_binary_op_column(cx, rhs, expr, e.op.commute())
                }
                _ => todo!("expr={:#?}", expr),
            },
            InList(e) => self.match_expr_in_list(cx, &e.expr, expr),
            IsNull(e) => match &*e.expr {
                stmt::Expr::Reference(expr_column @ stmt::ExprReference::Column(_)) => {
                    self.match_expr_binary_op_column(cx, expr_column, expr, stmt::BinaryOp::Eq)
                }
                _ => todo!("expr={:#?}", expr),
            },
            And(and_exprs) => {
                let matched = self.match_all_restrictions(cx, and_exprs);

                if matched {
                    // Union all matched columns for each operand
                    for column in &mut self.columns {
                        for and_expr in and_exprs {
                            if let Some(operand_match) =
                                column.exprs.get(&ByAddress(and_expr)).copied()
                            {
                                match column.exprs.entry(ByAddress(expr)) {
                                    hash_map::Entry::Vacant(e) => {
                                        e.insert(ExprMatch {
                                            eq: operand_match.eq,
                                        });
                                    }
                                    hash_map::Entry::Occupied(mut e) if operand_match.eq => {
                                        e.get_mut().eq = true;
                                    }
                                    _ => continue,
                                }

                                if operand_match.eq {
                                    break;
                                }
                            }
                        }
                    }
                }

                matched
            }
            Or(or_exprs) => {
                let matched_any_expr = self.match_all_restrictions(cx, or_exprs);
                let mut matched_operand = false;

                // For OR expressions, we need to ensure that the index match is
                // balanced for each column. To do this, if operand matched, then
                // we do a deeper search to see if any index column matched with
                // *all* or branches.
                if matched_any_expr {
                    'columns: for column in &mut self.columns {
                        let mut eq = true;

                        for or_expr in or_exprs {
                            if let Some(operand_match) = column.exprs.get(&ByAddress(or_expr)) {
                                eq &= operand_match.eq;
                            } else {
                                continue 'columns;
                            }
                        }

                        // At this point, we verified *each* operand matched a
                        // column, so we can consider the entire expression as
                        // having matched that column.
                        column.exprs.insert(ByAddress(expr), ExprMatch { eq });
                        matched_operand = true;
                    }
                }

                matched_operand
            }
            Any(expr_any) => {
                // Any operates on a Map expression that evaluates to bools.
                // It's similar to a dynamic OR expression. If the map expression
                // matches an index, the Any expression matches as well.
                // We don't need the "balanced" check like OR because each
                // evaluation of the map will have the same structure.
                let stmt::Expr::Map(expr_map) = &*expr_any.expr else {
                    todo!("expr_any.expr={:#?}", expr_any.expr);
                };

                // Try to match the map expression (the predicate applied to each element)
                if self.match_restriction(cx, &expr_map.map) {
                    // If the map expression matched, propagate that match to this Any expression
                    for column in &mut self.columns {
                        if let Some(operand_match) =
                            column.exprs.get(&ByAddress(&*expr_map.map)).copied()
                        {
                            column.exprs.insert(
                                ByAddress(expr),
                                ExprMatch {
                                    eq: operand_match.eq,
                                },
                            );
                        }
                    }
                    true
                } else {
                    false
                }
            }
            _ => {
                // Unsupported expression type for index matching - return false
                // to indicate this expression cannot be matched against an index
                false
            }
        }
    }

    /// Returns true if **any** expression in the provided list match with
    /// **any** index column.
    fn match_all_restrictions(
        &mut self,
        cx: &stmt::ExprContext<'_>,
        exprs: &'stmt [stmt::Expr],
    ) -> bool {
        let mut matched = false;

        for expr in exprs {
            matched |= self.match_restriction(cx, expr);
        }

        matched
    }

    fn match_expr_in_list(
        &mut self,
        cx: &stmt::ExprContext<'_>,
        lhs: &'stmt stmt::Expr,
        expr: &'stmt stmt::Expr,
    ) -> bool {
        match lhs {
            stmt::Expr::Reference(expr_column @ stmt::ExprReference::Column(_)) => {
                self.match_expr_binary_op_column(cx, expr_column, expr, stmt::BinaryOp::Eq)
            }
            stmt::Expr::Record(expr_record) => {
                let mut matched = false;

                for sub_expr in expr_record {
                    let stmt::Expr::Reference(expr_column @ stmt::ExprReference::Column(_)) =
                        sub_expr
                    else {
                        todo!()
                    };
                    matched |= self.match_expr_binary_op_column(
                        cx,
                        expr_column,
                        sub_expr,
                        stmt::BinaryOp::Eq,
                    );
                }

                if matched {
                    for column in &mut self.columns {
                        for sub_expr in expr_record {
                            if column.exprs.contains_key(&ByAddress(sub_expr)) {
                                column.exprs.insert(ByAddress(expr), ExprMatch { eq: true });

                                break;
                            }
                        }
                    }
                }

                matched
            }
            _ => todo!("expr={:#?}", expr),
        }
    }

    fn match_expr_binary_op_column(
        &mut self,
        cx: &stmt::ExprContext<'_>,
        expr_ref: &stmt::ExprReference,
        expr: &'stmt stmt::Expr,
        op: stmt::BinaryOp,
    ) -> bool {
        let mut matched = false;

        for (i, index_column) in self.index.columns.iter().enumerate() {
            // Check that the path matches an index column
            if cx.resolve_expr_reference(expr_ref).as_column_unwrap().id != index_column.column {
                continue;
            }

            // If the index column is scoped as a partition key, then only
            // equality predicates are supported.
            if index_column.scope.is_partition() && !op.is_eq() {
                continue;
            }

            self.columns[i]
                .exprs
                .insert(ByAddress(expr), ExprMatch { eq: op.is_eq() });

            matched = true;
        }

        matched
    }

    /// Copute the cost of using this index match to execute the query.
    pub(super) fn compute_cost(&self, filter: &stmt::Expr) -> usize {
        if self.index.unique {
            let mut cost = 0;

            for column in &self.columns {
                let Some(expr_match) = column.exprs.get(&ByAddress(filter)) else {
                    break;
                };

                if expr_match.eq {
                    cost += 1;
                } else {
                    cost += 10;
                }
            }

            cost
        } else {
            // Arbitrary
            10
        }
    }

    pub(super) fn partition_filter(
        &self,
        ctx: &mut PartitionCtx<'_>,
        expr: &stmt::Expr,
    ) -> ExprPair {
        use stmt::Expr::*;

        match expr {
            InList(_) | IsNull(_) | Not(_) => {
                if self
                    .columns
                    .iter()
                    .any(|f| f.exprs.contains_key(&ByAddress(expr)))
                {
                    (expr.clone(), true.into())
                } else {
                    (true.into(), expr.clone())
                }
            }
            BinaryOp(binary_op) => {
                if self
                    .columns
                    .iter()
                    .any(|f| f.exprs.contains_key(&ByAddress(expr)))
                {
                    if binary_op.op.is_ne() && !ctx.capability.primary_key_ne_predicate {
                        ctx.apply_result_filter_on_results = true;
                        return (true.into(), true.into());
                    }

                    // Normalize the expression to include the column on the LHS
                    let expr = match (&*binary_op.lhs, &*binary_op.rhs) {
                        (stmt::Expr::Reference(stmt::ExprReference::Column(_)), _) => expr.clone(),
                        (
                            lhs,
                            stmt::Expr::Reference(column_ref @ stmt::ExprReference::Column(_)),
                        ) => stmt::ExprBinaryOp {
                            lhs: Box::new(stmt::Expr::Reference(*column_ref)),
                            rhs: Box::new(lhs.clone()),
                            op: binary_op.op.commute(),
                        }
                        .into(),
                        _ => todo!("binary_op={binary_op:#?}"),
                    };

                    (expr, true.into())
                } else {
                    (true.into(), expr.clone())
                }
            }
            And(expr_and) => {
                let mut index_filter_operands = vec![];
                let mut result_filter_operands = vec![];

                for operand in &expr_and.operands {
                    let (index_filter, result_filter) = self.partition_filter(ctx, operand);

                    if !index_filter.is_true() {
                        index_filter_operands.push(index_filter);
                    }

                    if !result_filter.is_true() {
                        result_filter_operands.push(result_filter);
                    }
                }

                let index_filter = match index_filter_operands.len() {
                    0 => true.into(),
                    1 => index_filter_operands.into_iter().next().unwrap(),
                    _ => stmt::ExprAnd {
                        operands: index_filter_operands,
                    }
                    .into(),
                };
                let result_filter = match result_filter_operands.len() {
                    0 => true.into(),
                    1 => result_filter_operands.into_iter().next().unwrap(),
                    _ => stmt::ExprAnd {
                        operands: result_filter_operands,
                    }
                    .into(),
                };

                (index_filter, result_filter)
            }
            Or(expr_or) => {
                let mut index_filter_operands = vec![];
                let mut result_filter_operands = vec![];

                for operand in &expr_or.operands {
                    let (index_filter, result_filter) = self.partition_filter(ctx, operand);

                    if !index_filter.is_true() && !result_filter.is_true() {
                        todo!("expr_or={:#?}", expr_or);
                    }

                    if !index_filter.is_true() {
                        assert!(result_filter_operands.is_empty());
                        index_filter_operands.push(index_filter);
                    } else if !result_filter.is_true() {
                        assert!(index_filter_operands.is_empty());
                        result_filter_operands.push(result_filter);
                    } else {
                        todo!()
                    }
                }

                if !index_filter_operands.is_empty() {
                    (
                        stmt::ExprOr {
                            operands: index_filter_operands,
                        }
                        .into(),
                        true.into(),
                    )
                } else {
                    (
                        true.into(),
                        stmt::ExprOr {
                            operands: result_filter_operands,
                        }
                        .into(),
                    )
                }
            }
            Any(expr_any) => {
                // For Any expressions, partition the inner map expression
                let stmt::Expr::Map(expr_map) = &*expr_any.expr else {
                    todo!("expr_any.expr={:#?}", expr_any.expr);
                };

                let (index_filter, result_filter) = self.partition_filter(ctx, &expr_map.map);

                // If the map expression can be used as an index filter, reconstruct
                // the Any with the index filter version
                if !index_filter.is_true() {
                    let index_any = stmt::ExprAny {
                        expr: Box::new(stmt::Expr::Map(stmt::ExprMap {
                            base: expr_map.base.clone(),
                            map: Box::new(index_filter),
                        })),
                    };
                    (index_any.into(), true.into())
                } else if !result_filter.is_true() {
                    let result_any = stmt::ExprAny {
                        expr: Box::new(stmt::Expr::Map(stmt::ExprMap {
                            base: expr_map.base.clone(),
                            map: Box::new(result_filter),
                        })),
                    };
                    (true.into(), result_any.into())
                } else {
                    (true.into(), true.into())
                }
            }
            _ => todo!("partition_filter={:#?}", expr),
        }
    }
}