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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use super::*;
use toasty_core::{
    schema::{Builder, app},
    stmt,
};

use crate as toasty;
use crate::schema::Register;

#[allow(dead_code)]
#[derive(toasty::Model)]
struct User {
    #[key]
    id: String,
    name: String,
}

/// Composite primary key: user_id (partition) + status (sort).
#[allow(dead_code)]
#[derive(toasty::Model)]
struct Todo {
    #[key]
    user_id: String,
    #[key]
    status: String,
}

/// Composite primary key with a local (sort) column that accepts range predicates.
#[allow(dead_code)]
#[derive(toasty::Model)]
#[key(partition = id, local = rank)]
struct Ranked {
    id: String,
    rank: i64,
}

#[test]
fn pk_equality_goes_to_index_filter() -> Result<()> {
    let cx = sqlite_test_cx();

    // col[0] = 1  — pk column equality
    let filter = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(1i64)),
    );

    let plan = cx.plan_basic_query_with_filter(filter.clone())?;

    assert!(
        plan.index.primary_key,
        "should select the primary key index"
    );
    assert_eq!(
        plan.index_filter, filter,
        "pk equality should be the index filter"
    );
    assert!(
        plan.result_filter.is_none(),
        "no residual result filter expected"
    );
    assert!(plan.post_filter.is_none());
    Ok(())
}

#[test]
fn pk_equality_column_on_rhs_goes_to_index_filter() -> Result<()> {
    let cx = sqlite_test_cx();

    // 1 = col[0]  — pk column on RHS; index matching must commute the operator
    let filter = stmt::Expr::eq(
        stmt::Expr::Value(stmt::Value::from(1i64)),
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
    );

    let plan = cx.plan_basic_query_with_filter(filter)?;

    assert!(
        plan.index.primary_key,
        "should select the primary key index"
    );
    assert!(
        plan.result_filter.is_none(),
        "no residual result filter expected"
    );
    assert!(plan.post_filter.is_none());
    Ok(())
}

#[test]
fn pk_lt_column_on_rhs_commutes_to_gt() -> Result<()> {
    // Schema: Ranked { id (partition), rank (local) }
    // The local-scoped sort column accepts range predicates in the index.
    let cx = sqlite_test_cx_ranked();

    // id = "a" AND 10 < rank  — sort column on RHS with Lt
    // Index matching must commute the second operand to rank > 10.
    let filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::String("a".to_string())),
            ),
            stmt::Expr::BinaryOp(stmt::ExprBinaryOp {
                lhs: Box::new(stmt::Expr::Value(stmt::Value::from(10i64))),
                op: stmt::BinaryOp::Lt,
                rhs: Box::new(stmt::Expr::Reference(stmt::ExprReference::column(0, 1))),
            }),
        ],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    assert!(
        plan.index.primary_key,
        "should select the primary key index"
    );

    // The index filter should contain both conditions with the sort column
    // commuted to col > 10 (not the original 10 < col).
    let expected_index_filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::String("a".to_string())),
            ),
            stmt::Expr::BinaryOp(stmt::ExprBinaryOp {
                lhs: Box::new(stmt::Expr::Reference(stmt::ExprReference::column(0, 1))),
                op: stmt::BinaryOp::Gt,
                rhs: Box::new(stmt::Expr::Value(stmt::Value::from(10i64))),
            }),
        ],
    });
    assert_eq!(plan.index_filter, expected_index_filter);
    assert!(
        plan.result_filter.is_none(),
        "no residual result filter expected"
    );
    Ok(())
}

#[test]
fn and_splits_pk_to_index_and_name_to_result() -> Result<()> {
    let cx = sqlite_test_cx();

    // col[0] = 1 AND col[1] = 2 — pk equality AND name equality
    let pk_eq = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(1i64)),
    );
    let name_eq = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
        stmt::Expr::Value(stmt::Value::from(2i64)),
    );
    let filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![pk_eq.clone(), name_eq.clone()],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    assert!(plan.index.primary_key);
    assert_eq!(
        plan.index_filter, pk_eq,
        "only the pk condition goes to index filter"
    );
    assert_eq!(
        plan.result_filter.as_ref(),
        Some(&name_eq),
        "non-pk condition goes to result filter"
    );
    Ok(())
}

#[test]
fn or_on_pk_stays_as_or_for_sql() -> Result<()> {
    let cx = sqlite_test_cx(); // SQLite — index_or_predicate = true

    // col[0] = 1 OR col[0] = 2
    let pk_eq_1 = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(1i64)),
    );
    let pk_eq_2 = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(2i64)),
    );
    let filter = stmt::Expr::Or(stmt::ExprOr {
        operands: vec![pk_eq_1, pk_eq_2],
    });

    let plan = cx.plan_basic_query_with_filter(filter.clone())?;

    assert!(plan.index.primary_key);
    assert_eq!(
        plan.index_filter, filter,
        "OR should be preserved as-is for SQL backends"
    );
    assert!(plan.result_filter.is_none());
    assert!(plan.post_filter.is_none());
    Ok(())
}

#[test]
fn or_on_pk_becomes_any_map_for_dynamodb() -> Result<()> {
    let cx = ddb_test_cx();

    // col[0] = 1 OR col[0] = 2
    let pk_eq_1 = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(1i64)),
    );
    let pk_eq_2 = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(2i64)),
    );
    let filter = stmt::Expr::Or(stmt::ExprOr {
        operands: vec![pk_eq_1, pk_eq_2],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    // Expected: ANY(MAP([1, 2], col[0][0] = arg(0)))
    let expected = stmt::Expr::any(stmt::Expr::map(
        stmt::Expr::Value(stmt::Value::List(vec![
            stmt::Value::from(1i64),
            stmt::Value::from(2i64),
        ])),
        stmt::Expr::eq(
            stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
            stmt::Expr::arg(0),
        ),
    ));

    assert!(plan.index.primary_key);
    assert_eq!(
        plan.index_filter, expected,
        "OR should be rewritten to ANY(MAP(...)) for DynamoDB"
    );
    assert!(plan.result_filter.is_none());
    assert!(plan.post_filter.is_none());
    Ok(())
}

#[test]
fn and_with_any_map_distributes_into_any_map_for_dynamodb() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    // Both columns are part of the primary key index, so both land in index_filter.
    //
    // Filter: col[0][1] = "active" AND ANY(MAP(arg[0], col[0][0] = arg[0]))
    //   col[0][0] = user_id (partition key), col[0][1] = status (sort key)
    //
    // Expected: ANY(MAP(arg[0], col[0][0] = arg[0] AND col[0][1] = "active"))
    let cx = ddb_test_cx_composite();

    let status_eq = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
        stmt::Expr::Value(stmt::Value::String("active".to_string())),
    );
    let user_id_any = stmt::Expr::any(stmt::Expr::map(
        stmt::Expr::arg(0),
        stmt::Expr::eq(
            stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
            stmt::Expr::arg(0),
        ),
    ));
    let filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![status_eq.clone(), user_id_any],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let expected = stmt::Expr::any(stmt::Expr::map(
        stmt::Expr::arg(0),
        stmt::Expr::And(stmt::ExprAnd {
            operands: vec![
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                    stmt::Expr::arg(0),
                ),
                status_eq,
            ],
        }),
    ));

    assert!(plan.index.primary_key);
    assert_eq!(
        plan.index_filter, expected,
        "AND with ANY(MAP) should distribute the non-Any operands into the map predicate"
    );
    assert!(plan.result_filter.is_none());
    assert!(plan.post_filter.is_none());
    Ok(())
}

// ── key_values extraction ─────────────────────────────────────────────────────

#[test]
fn pk_equality_sets_key_values() -> Result<()> {
    let cx = sqlite_test_cx();

    // col[0] = 1  — single PK column, literal equality
    let filter = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::from(1i64)),
    );

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let expected = stmt::Value::List(vec![stmt::Value::Record(stmt::ValueRecord::from_vec(
        vec![stmt::Value::from(1i64)],
    ))]);
    assert_eq!(plan.key_values, Some(stmt::Expr::Value(expected)));
    Ok(())
}

#[test]
fn pk_or_sets_key_values() -> Result<()> {
    let cx = sqlite_test_cx();

    // col[0] = 1 OR col[0] = 2  — two literal PK values
    let filter = stmt::Expr::Or(stmt::ExprOr {
        operands: vec![
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::from(1i64)),
            ),
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::from(2i64)),
            ),
        ],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let record =
        |v: i64| stmt::Value::Record(stmt::ValueRecord::from_vec(vec![stmt::Value::from(v)]));
    let expected = stmt::Value::List(vec![record(1), record(2)]);
    assert_eq!(plan.key_values, Some(stmt::Expr::Value(expected)));
    Ok(())
}

#[test]
fn pk_range_does_not_set_key_values() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    // Partition key has equality (required to match the index), but the sort key
    // has a range predicate — no exact key record can be formed, so key_values = None.
    let cx = ddb_test_cx_composite();

    let filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::String("u1".to_string())),
            ),
            stmt::Expr::BinaryOp(stmt::ExprBinaryOp {
                lhs: Box::new(stmt::Expr::Reference(stmt::ExprReference::column(0, 1))),
                op: stmt::BinaryOp::Ge,
                rhs: Box::new(stmt::Expr::Value(stmt::Value::String("s1".to_string()))),
            }),
        ],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    assert_eq!(plan.key_values, None);
    Ok(())
}

#[test]
fn composite_pk_full_equality_sets_key_values() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    // Both key columns have literal equality → key_values is populated.
    let cx = ddb_test_cx_composite();

    let filter = stmt::Expr::And(stmt::ExprAnd {
        operands: vec![
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                stmt::Expr::Value(stmt::Value::String("u1".to_string())),
            ),
            stmt::Expr::eq(
                stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
                stmt::Expr::Value(stmt::Value::String("s1".to_string())),
            ),
        ],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let expected = stmt::Value::List(vec![stmt::Value::Record(stmt::ValueRecord::from_vec(
        vec![
            stmt::Value::String("u1".to_string()),
            stmt::Value::String("s1".to_string()),
        ],
    ))]);
    assert_eq!(plan.key_values, Some(stmt::Expr::Value(expected)));
    Ok(())
}

#[test]
fn composite_pk_partition_key_only_does_not_set_key_values() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    // Only the partition key is specified — cannot form a full key record.
    let cx = ddb_test_cx_composite();

    let filter = stmt::Expr::eq(
        stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
        stmt::Expr::Value(stmt::Value::String("u1".to_string())),
    );

    let plan = cx.plan_basic_query_with_filter(filter)?;

    assert_eq!(plan.key_values, None);
    Ok(())
}

#[test]
fn any_map_with_arg_base_passes_through_for_dynamodb() {
    // ANY(MAP(arg[0], col[0][0] = arg[0])) — the batch-load canonical form produced by
    // rewrite_stmt_query_for_batch_load_nosql. It is already in the target form so
    // index_filter_to_any_map must return it unchanged.
    let filter = stmt::Expr::any(stmt::Expr::map(
        stmt::Expr::arg(0),
        stmt::Expr::eq(
            stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
            stmt::Expr::arg(0),
        ),
    ));

    let result = or_rewrite::index_filter_to_any_map(filter.clone());
    assert_eq!(
        result, filter,
        "batch-load ANY(MAP(arg, pred)) should pass through unchanged"
    );
}

#[test]
fn composite_pk_or_sets_key_values() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    //
    // Filter: (user_id = "u1" AND status = "s1") OR (user_id = "u2" AND status = "s2")
    //
    // Both branches fully specify all key columns with equality → key_values is populated.
    let cx = ddb_test_cx_composite();

    let branch = |uid: &str, status: &str| {
        stmt::Expr::And(stmt::ExprAnd {
            operands: vec![
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                    stmt::Expr::Value(stmt::Value::String(uid.to_string())),
                ),
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
                    stmt::Expr::Value(stmt::Value::String(status.to_string())),
                ),
            ],
        })
    };
    let filter = stmt::Expr::Or(stmt::ExprOr {
        operands: vec![branch("u1", "s1"), branch("u2", "s2")],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let record = |uid: &str, status: &str| {
        stmt::Value::Record(stmt::ValueRecord::from_vec(vec![
            stmt::Value::String(uid.to_string()),
            stmt::Value::String(status.to_string()),
        ]))
    };
    let expected = stmt::Value::List(vec![record("u1", "s1"), record("u2", "s2")]);
    assert_eq!(plan.key_values, Some(stmt::Expr::Value(expected)));
    Ok(())
}

#[test]
fn composite_pk_or_becomes_any_map_for_dynamodb() -> Result<()> {
    // Schema: Todo { user_id (pk partition), status (pk sort) }
    //
    // Filter: (user_id = "u1" AND status = "s1") OR (user_id = "u2" AND status = "s2")
    //
    // Expected: ANY(MAP(
    //     [(u1,s1), (u2,s2)],
    //     user_id = arg(0) AND status = arg(1)
    // ))
    let cx = ddb_test_cx_composite();

    let branch = |uid: &str, status: &str| {
        stmt::Expr::And(stmt::ExprAnd {
            operands: vec![
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                    stmt::Expr::Value(stmt::Value::String(uid.to_string())),
                ),
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
                    stmt::Expr::Value(stmt::Value::String(status.to_string())),
                ),
            ],
        })
    };
    let filter = stmt::Expr::Or(stmt::ExprOr {
        operands: vec![branch("u1", "s1"), branch("u2", "s2")],
    });

    let plan = cx.plan_basic_query_with_filter(filter)?;

    let record = |uid: &str, status: &str| {
        stmt::Value::Record(stmt::ValueRecord::from_vec(vec![
            stmt::Value::String(uid.to_string()),
            stmt::Value::String(status.to_string()),
        ]))
    };
    let expected = stmt::Expr::any(stmt::Expr::map(
        stmt::Expr::Value(stmt::Value::List(vec![
            record("u1", "s1"),
            record("u2", "s2"),
        ])),
        stmt::Expr::And(stmt::ExprAnd {
            operands: vec![
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 0)),
                    stmt::Expr::arg(0),
                ),
                stmt::Expr::eq(
                    stmt::Expr::Reference(stmt::ExprReference::column(0, 1)),
                    stmt::Expr::arg(1),
                ),
            ],
        }),
    ));

    assert!(plan.index.primary_key);
    assert_eq!(
        plan.index_filter, expected,
        "composite OR should be rewritten to ANY(MAP([records], col0=arg(0) AND col1=arg(1)))"
    );
    assert!(plan.result_filter.is_none());
    assert!(plan.post_filter.is_none());
    Ok(())
}

struct TestCx {
    schema: toasty_core::Schema,
    capability: &'static Capability,
}

fn sqlite_test_cx() -> TestCx {
    test_cx_with_capability(&Capability::SQLITE)
}

fn ddb_test_cx() -> TestCx {
    test_cx_with_capability(&Capability::DYNAMODB)
}

fn ddb_test_cx_composite() -> TestCx {
    let app_schema =
        app::Schema::from_macro([Todo::schema()]).expect("schema should build from macro");
    let schema = Builder::new()
        .build(app_schema, &Capability::DYNAMODB)
        .expect("schema should build");
    TestCx {
        schema,
        capability: &Capability::DYNAMODB,
    }
}

fn sqlite_test_cx_ranked() -> TestCx {
    let app_schema =
        app::Schema::from_macro([Ranked::schema()]).expect("schema should build from macro");
    let schema = Builder::new()
        .build(app_schema, &Capability::SQLITE)
        .expect("schema should build");
    TestCx {
        schema,
        capability: &Capability::SQLITE,
    }
}

fn test_cx_with_capability(capability: &'static Capability) -> TestCx {
    let app_schema =
        app::Schema::from_macro([User::schema()]).expect("schema should build from macro");
    let schema = Builder::new()
        .build(app_schema, capability)
        .expect("schema should build");
    TestCx { schema, capability }
}

impl TestCx {
    /// Build a table-targeting `SELECT` statement with the given filter against
    /// the first table in the schema.  This mirrors the lowered statements that
    /// reach `plan_index_path` at runtime.
    fn basic_query_with_filter(&self, filter: stmt::Expr) -> stmt::Statement {
        let table_id = self.schema.db.tables[0].id;
        let source = stmt::SourceTable::new(
            vec![stmt::TableRef::Table(table_id)],
            stmt::TableWithJoins {
                relation: stmt::TableFactor::Table(stmt::SourceTableId(0)),
                joins: vec![],
            },
        );
        stmt::Statement::Query(stmt::Query {
            with: None,
            body: stmt::ExprSet::Select(Box::new(stmt::Select::new(source, filter))),
            single: false,
            order_by: None,
            limit: None,
            locks: vec![],
        })
    }

    fn plan_basic_query_with_filter(&self, filter: stmt::Expr) -> Result<IndexPlan<'_>> {
        let stmt = self.basic_query_with_filter(filter);
        plan_index_path(&self.schema, self.capability, &stmt)
    }
}