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
use toasty_core::stmt::{self, Expr, Value, ValueSet};
/// Cheap canonicalization for `x IN (..)`: empty list → false, null
/// propagation, literal dedup, single-item collapse.
///
/// Heavyweight rewrites (model-reference → primary-key-reference) live in
/// `simplify/expr_in_list.rs` and run after this fold pass on canonical
/// input.
pub(super) fn fold_expr_in_list(expr: &mut stmt::ExprInList) -> Option<Expr> {
// `x in ()` → `false`
if expr.list.is_list_empty() {
return Some(Expr::Value(Value::Bool(false)));
}
// Null propagation, `null in (x, y, z)` → `null`
if expr.expr.is_value_null() {
return Some(Expr::null());
}
// Deduplicate literal value lists: `x in (1, 1, 2)` → `x in (1, 2)`.
// Lists carry literal `Value`s only (no symbolic expressions), so value
// equality is safe — there is no non-determinism to preserve.
if let Expr::Value(Value::List(values)) = &mut *expr.list {
let mut seen = ValueSet::new();
values.retain(|v| seen.insert(v.clone()));
}
// Rewrite single-item lists into equalities.
rewrite_expr_in_list_with_single_item(expr)
}
fn rewrite_expr_in_list_with_single_item(expr: &mut stmt::ExprInList) -> Option<Expr> {
let rhs = match &mut *expr.list {
Expr::Value(value) => {
let values = match value {
Value::List(value) => &value[..],
_ => todo!("{value:#?}"),
};
if values.len() != 1 {
return None;
}
Expr::Value(values[0].clone())
}
Expr::List(expr_list) => {
if expr_list.items.len() != 1 {
return None;
}
expr_list.items[0].take()
}
Expr::Record(_) => todo!("should not happen"),
_ => return None,
};
Some(Expr::eq(expr.expr.take(), rhs))
}