toasty 0.7.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
use super::test_schema;
use crate::engine::simplify::Simplify;
use toasty_core::stmt::{BinaryOp, Expr, ExprAnd, ExprOr};

// Cheap canonicalization (flatten, drop unit literals, null propagation,
// single/empty collapse on canonical input) is exercised in
// `engine::fold::tests::expr_and`. Tests here cover the heavyweight rules
// only: idempotent law, absorption, complement, range-to-equality,
// contradiction detection, and OR-branch pruning.

#[test]
fn idempotent_two_identical() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, a) → a`
    let mut expr = ExprAnd {
        operands: vec![Expr::arg(0), Expr::arg(0)],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert_eq!(result.unwrap(), Expr::arg(0));
}

#[test]
fn idempotent_three_identical() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, a, a) → a`
    let mut expr = ExprAnd {
        operands: vec![Expr::arg(0), Expr::arg(0), Expr::arg(0)],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert_eq!(result.unwrap(), Expr::arg(0));
}

#[test]
fn idempotent_with_different() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, b, a) → and(a, b)`
    let mut expr = ExprAnd {
        operands: vec![Expr::arg(0), Expr::arg(1), Expr::arg(0)],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert_eq!(expr.operands[0], Expr::arg(0));
    assert_eq!(expr.operands[1], Expr::arg(1));
}

#[test]
fn absorption_and_or() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, or(a, b))` → `a`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::arg(0),
            Expr::Or(ExprOr {
                operands: vec![Expr::arg(0), Expr::arg(1)],
            }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert_eq!(result.unwrap(), Expr::arg(0));
}

#[test]
fn absorption_with_multiple_operands() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, b, or(a, c))` → `and(a, b)`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::arg(0),
            Expr::arg(1),
            Expr::Or(ExprOr {
                operands: vec![Expr::arg(0), Expr::arg(2)],
            }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert_eq!(expr.operands[0], Expr::arg(0));
    assert_eq!(expr.operands[1], Expr::arg(1));
}

#[test]
fn absorption_two_and_three_or() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `and(a, b, or(a, c, d))` → `and(a, b)`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::arg(0),
            Expr::arg(1),
            Expr::Or(ExprOr {
                operands: vec![Expr::arg(0), Expr::arg(2), Expr::arg(3)],
            }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert_eq!(expr.operands[0], Expr::arg(0));
    assert_eq!(expr.operands[1], Expr::arg(1));
}

#[test]
fn complement_basic() {
    use toasty_core::stmt::ExprNot;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a and not(a)` → `false` (where a is a non-nullable comparison)
    let a = Expr::eq(Expr::arg(0), Expr::arg(1));
    let mut expr = ExprAnd {
        operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert!(result.unwrap().is_false());
}

#[test]
fn complement_with_other_operands() {
    use toasty_core::stmt::ExprNot;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a and b and not(a)` → `false`
    let a = Expr::eq(Expr::arg(0), Expr::arg(1));
    let mut expr = ExprAnd {
        operands: vec![
            a.clone(),
            Expr::arg(2),
            Expr::Not(ExprNot { expr: Box::new(a) }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert!(result.unwrap().is_false());
}

#[test]
fn complement_nullable_not_simplified() {
    use toasty_core::stmt::ExprNot;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a and not(a)` where `a` is an arg (nullable) → no change
    let a = Expr::arg(0);
    let mut expr = ExprAnd {
        operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
}

#[test]
fn complement_multiple_repetitions() {
    use toasty_core::stmt::ExprNot;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a and a and not(a) and not(a)` → `false`
    let a = Expr::eq(Expr::arg(0), Expr::arg(1));
    let mut expr = ExprAnd {
        operands: vec![
            a.clone(),
            a.clone(),
            Expr::Not(ExprNot {
                expr: Box::new(a.clone()),
            }),
            Expr::Not(ExprNot { expr: Box::new(a) }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert!(result.unwrap().is_false());
}

#[test]
fn range_to_equality_ge_le() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a >= 5 and a <= 5` → `a = 5`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(0), BinaryOp::Le, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    let Some(Expr::BinaryOp(bin_op)) = result else {
        panic!("expected binary op");
    };
    assert!(bin_op.op.is_eq());
}

#[test]
fn range_to_equality_le_ge() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a <= 5 and a >= 5` → `a = 5` (opposite order)
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::arg(0), BinaryOp::Le, 5i64),
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    let Some(Expr::BinaryOp(bin_op)) = result else {
        panic!("expected binary op");
    };
    assert!(bin_op.op.is_eq());
}

#[test]
fn range_to_equality_different_bounds_not_simplified() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a >= 5 and a <= 10` is not simplified (different bounds)
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(0), BinaryOp::Le, 10i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

#[test]
fn range_to_equality_different_exprs_not_simplified() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a >= 5 and b <= 5` is not simplified (different expressions)
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(1), BinaryOp::Le, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

#[test]
fn range_to_equality_with_other_operands() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `x and a >= 5 and a <= 5` → `x and a = 5`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::arg(0),
            Expr::binary_op(Expr::arg(1), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(1), BinaryOp::Le, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none()); // Still has multiple operands
    assert_eq!(expr.operands.len(), 2);

    // One should be arg(0), the other should be the equality
    let has_equality = expr
        .operands
        .iter()
        .any(|e| matches!(e, Expr::BinaryOp(op) if op.op.is_eq()));
    assert!(has_equality);
}

#[test]
fn range_to_equality_uneven_repetitions() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `a >= 5 and a >= 5 and a <= 5` → `a = 5`
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(0), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::arg(0), BinaryOp::Le, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    // All bounds collapse to a single equality
    let Some(Expr::BinaryOp(bin_op)) = result else {
        panic!("expected binary op");
    };
    assert!(bin_op.op.is_eq());
}

// --- AND-over-OR branch pruning tests ---

/// The core enum variant+field pattern: disc == 1 in the outer AND
/// contradicts disc != 1 in the else branch of the OR.
///
/// AND(disc == 1, OR(AND(disc == 1, addr == "alice"), AND(disc != 1, Error == "alice")))
///   → AND(disc == 1, addr == "alice")
#[test]
fn prune_or_branch_contradicting_outer_eq() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    let disc_eq_1 = Expr::eq(Expr::arg(0), Expr::from(1i64));
    let addr_eq_alice = Expr::eq(Expr::arg(1), Expr::from("alice"));
    let disc_ne_1 = Expr::ne(Expr::arg(0), Expr::from(1i64));
    let error_eq_alice = Expr::eq(Expr::error("unreachable"), Expr::from("alice"));

    let mut expr = ExprAnd {
        operands: vec![
            disc_eq_1.clone(),
            Expr::Or(ExprOr {
                operands: vec![
                    Expr::and(disc_eq_1.clone(), addr_eq_alice.clone()),
                    Expr::and(disc_ne_1, error_eq_alice),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    // Should simplify: OR collapses to single branch, then AND flattens
    // and deduplicates disc == 1.
    assert!(result.is_none()); // Still has 2 operands
    assert_eq!(expr.operands.len(), 2);

    // The two remaining operands should be disc == 1 and addr == "alice"
    assert!(expr.operands.contains(&disc_eq_1));
    assert!(expr.operands.contains(&addr_eq_alice));
}

/// Multiple OR branches where only the matching one survives.
///
/// AND(x == 1, OR(AND(x == 1, a), AND(x == 2, b), AND(x == 3, c)))
///   → AND(x == 1, a)
#[test]
fn prune_or_multiple_contradicting_branches() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    let x_eq_1 = Expr::eq(Expr::arg(0), Expr::from(1i64));
    let x_eq_2 = Expr::eq(Expr::arg(0), Expr::from(2i64));
    let x_eq_3 = Expr::eq(Expr::arg(0), Expr::from(3i64));

    let mut expr = ExprAnd {
        operands: vec![
            x_eq_1.clone(),
            Expr::Or(ExprOr {
                operands: vec![
                    Expr::and(x_eq_1.clone(), Expr::arg(1)),
                    Expr::and(x_eq_2, Expr::arg(2)),
                    Expr::and(x_eq_3, Expr::arg(3)),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert!(expr.operands.contains(&x_eq_1));
    assert!(expr.operands.contains(&Expr::arg(1)));
}

/// When no OR branch contradicts the outer constraint, nothing is pruned.
#[test]
fn prune_or_no_contradiction_preserved() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // AND(x == 1, OR(AND(y == 2, a), AND(y == 3, b)))
    // x == 1 doesn't contradict y == 2 or y == 3 — no pruning.
    let mut expr = ExprAnd {
        operands: vec![
            Expr::eq(Expr::arg(0), Expr::from(1i64)),
            Expr::Or(ExprOr {
                operands: vec![
                    Expr::and(Expr::eq(Expr::arg(1), Expr::from(2i64)), Expr::arg(2)),
                    Expr::and(Expr::eq(Expr::arg(1), Expr::from(3i64)), Expr::arg(3)),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    // No simplification — 2 operands remain, OR still has 2 branches.
    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert!(matches!(&expr.operands[1], Expr::Or(or) if or.operands.len() == 2));
}

/// All OR branches pruned → OR becomes false → AND becomes false.
#[test]
fn prune_or_all_branches_contradicted() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // AND(x == 1, OR(AND(x == 2, a), AND(x == 3, b)))
    // Both branches contradict x == 1, so OR → false → AND → false.
    let mut expr = ExprAnd {
        operands: vec![
            Expr::eq(Expr::arg(0), Expr::from(1i64)),
            Expr::Or(ExprOr {
                operands: vec![
                    Expr::and(Expr::eq(Expr::arg(0), Expr::from(2i64)), Expr::arg(1)),
                    Expr::and(Expr::eq(Expr::arg(0), Expr::from(3i64)), Expr::arg(2)),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_some());
    assert!(result.unwrap().is_false());
}

/// A non-AND branch in the OR is tested as a single-element constraint.
///
/// AND(x == 1, OR(x == 2, AND(x == 1, a)))
///   → prune `x == 2` (contradicts x == 1)
///   → AND(x == 1, a)
#[test]
fn prune_or_non_and_branch() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    let x_eq_1 = Expr::eq(Expr::arg(0), Expr::from(1i64));
    let x_eq_2 = Expr::eq(Expr::arg(0), Expr::from(2i64));

    let mut expr = ExprAnd {
        operands: vec![
            x_eq_1.clone(),
            Expr::Or(ExprOr {
                operands: vec![
                    x_eq_2, // bare expr, not wrapped in AND
                    Expr::and(x_eq_1.clone(), Expr::arg(1)),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert!(expr.operands.contains(&x_eq_1));
    assert!(expr.operands.contains(&Expr::arg(1)));
}

/// Multiple non-OR operands form the constraint set.
///
/// AND(x == 1, y == 2, OR(AND(x == 1, y == 3, a), AND(x == 1, y == 2, b)))
///   → prune first OR branch (y == 2 contradicts y == 3)
///   → AND(x == 1, y == 2, b)
#[test]
fn prune_or_multiple_constraints() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    let x_eq_1 = Expr::eq(Expr::arg(0), Expr::from(1i64));
    let y_eq_2 = Expr::eq(Expr::arg(1), Expr::from(2i64));
    let y_eq_3 = Expr::eq(Expr::arg(1), Expr::from(3i64));

    let mut expr = ExprAnd {
        operands: vec![
            x_eq_1.clone(),
            y_eq_2.clone(),
            Expr::Or(ExprOr {
                operands: vec![
                    Expr::and_from_vec(vec![x_eq_1.clone(), y_eq_3, Expr::arg(2)]),
                    Expr::and_from_vec(vec![x_eq_1.clone(), y_eq_2.clone(), Expr::arg(3)]),
                ],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    // After pruning + flatten + dedup: AND(x == 1, y == 2, arg(3))
    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 3);
    assert!(expr.operands.contains(&x_eq_1));
    assert!(expr.operands.contains(&y_eq_2));
    assert!(expr.operands.contains(&Expr::arg(3)));
}

/// When the AND has no non-OR operands, no pruning occurs.
#[test]
fn prune_or_no_constraints_no_change() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // AND(OR(a, b), OR(c, d)) — no non-OR constraints to propagate.
    let mut expr = ExprAnd {
        operands: vec![
            Expr::Or(ExprOr {
                operands: vec![Expr::arg(0), Expr::arg(1)],
            }),
            Expr::Or(ExprOr {
                operands: vec![Expr::arg(2), Expr::arg(3)],
            }),
        ],
    };

    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

/// End-to-end through visit_expr_mut: the full enum variant+field pattern.
///
/// AND(disc == 1, eq(Match(disc, [1 => addr, 2 => arg(2)], else: Error), "alice"))
///   → match elimination produces OR
///   → AND-over-OR pruning removes else branch
///   → AND(disc == 1, addr == "alice")
#[test]
fn prune_or_end_to_end_via_visit() {
    use toasty_core::stmt::{ExprMatch, MatchArm, Value, VisitMut};

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    let disc = Expr::arg(0);
    let addr = Expr::arg(1);

    let mut expr = Expr::and(
        Expr::eq(disc.clone(), Expr::from(1i64)),
        Expr::eq(
            Expr::Match(ExprMatch {
                subject: Box::new(disc.clone()),
                arms: vec![
                    MatchArm {
                        pattern: Value::from(1i64),
                        expr: addr.clone(),
                    },
                    MatchArm {
                        pattern: Value::from(2i64),
                        expr: Expr::arg(2),
                    },
                ],
                else_expr: Box::new(Expr::error("unreachable")),
            }),
            Expr::from("alice"),
        ),
    );

    simplify.visit_expr_mut(&mut expr);

    // Result should be AND(disc == 1, addr == "alice")
    let Expr::And(and) = &expr else {
        panic!("expected AND, got: {expr:?}");
    };
    assert_eq!(and.operands.len(), 2);
    assert!(and.operands.contains(&Expr::eq(disc, Expr::from(1i64))));
    assert!(and.operands.contains(&Expr::eq(addr, Expr::from("alice"))));
}

// ---------------------------------------------------------------------------
// Determinism-aware simplification (issue #236).
//
// `is_equivalent_to` gates every "syntactic identity implies semantic
// equivalence" rewrite on `Expr::is_stable`.  Non-deterministic expressions
// (e.g.  `LAST_INSERT_ID()`) are re-evaluated on each occurrence and must not
// be folded even when two occurrences are textually identical.
// ---------------------------------------------------------------------------

#[test]
fn idempotent_not_simplified_for_non_deterministic() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `LAST_INSERT_ID() AND LAST_INSERT_ID()` must retain both operands.
    let mut expr = ExprAnd {
        operands: vec![Expr::last_insert_id(), Expr::last_insert_id()],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
    assert_eq!(expr.operands[0], Expr::last_insert_id());
    assert_eq!(expr.operands[1], Expr::last_insert_id());
}

#[test]
fn absorption_not_simplified_for_non_deterministic() {
    use toasty_core::stmt::ExprOr;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `x AND (x OR y)` would absorb to `x` under PartialEq, but with a
    // non-deterministic `x` the two occurrences are independent draws, so
    // absorption must not fire.
    let x = Expr::eq(Expr::last_insert_id(), 1i64);
    let mut expr = ExprAnd {
        operands: vec![
            x.clone(),
            Expr::Or(ExprOr {
                operands: vec![x, Expr::arg(0)],
            }),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

#[test]
fn complement_not_simplified_for_non_deterministic() {
    use toasty_core::stmt::ExprNot;

    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `f() = 1 AND NOT (f() = 1)` — both `f()` calls are independent
    // evaluations, so complement must NOT fire.  (Compare with
    // `complement_basic`, which uses `arg` — stable.)
    let a = Expr::eq(Expr::last_insert_id(), 1i64);
    let mut expr = ExprAnd {
        operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

#[test]
fn range_to_equality_not_simplified_for_non_deterministic() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `f() >= 5 AND f() <= 5` — two independent draws of `f()` bracketed
    // by the same constant do not imply the draws are equal to 5.
    let mut expr = ExprAnd {
        operands: vec![
            Expr::binary_op(Expr::last_insert_id(), BinaryOp::Ge, 5i64),
            Expr::binary_op(Expr::last_insert_id(), BinaryOp::Le, 5i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}

#[test]
fn contradicting_eq_not_simplified_for_non_deterministic() {
    let schema = test_schema();
    let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);

    // `f() == 1 AND f() == 2` — two independent draws can produce 1 and 2
    // respectively, so this is NOT a contradiction.
    let mut expr = ExprAnd {
        operands: vec![
            Expr::eq(Expr::last_insert_id(), 1i64),
            Expr::eq(Expr::last_insert_id(), 2i64),
        ],
    };
    let result = simplify.simplify_expr_and(&mut expr);

    assert!(result.is_none());
    assert_eq!(expr.operands.len(), 2);
}