use super::test_schema;
use crate::engine::simplify::Simplify;
use toasty_core::stmt::{Expr, ExprOr};
#[test]
fn idempotent_two_identical() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![Expr::arg(0), Expr::arg(0)],
};
let result = simplify.simplify_expr_or(&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);
let mut expr = ExprOr {
operands: vec![Expr::arg(0), Expr::arg(0), Expr::arg(0)],
};
let result = simplify.simplify_expr_or(&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);
let mut expr = ExprOr {
operands: vec![Expr::arg(0), Expr::arg(1), Expr::arg(0)],
};
let result = simplify.simplify_expr_or(&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_or_and() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::arg(0),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
assert_eq!(result.unwrap(), Expr::arg(0));
}
#[test]
fn absorption_with_multiple_operands() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::arg(0),
Expr::arg(1),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(2)],
}),
],
};
let result = simplify.simplify_expr_or(&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_or_three_and() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::arg(0),
Expr::arg(1),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(2), Expr::arg(3)],
}),
],
};
let result = simplify.simplify_expr_or(&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 factoring_basic() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(2)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
let Some(Expr::And(and_expr)) = result else {
panic!("expected And");
};
assert_eq!(and_expr.operands.len(), 2);
assert_eq!(and_expr.operands[0], Expr::arg(0));
let Expr::Or(or_expr) = &and_expr.operands[1] else {
panic!("expected Or");
};
assert_eq!(or_expr.operands.len(), 2);
assert_eq!(or_expr.operands[0], Expr::arg(1));
assert_eq!(or_expr.operands[1], Expr::arg(2));
}
#[test]
fn factoring_multiple_common() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1), Expr::arg(2)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1), Expr::arg(3)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
let Some(Expr::And(and_expr)) = result else {
panic!("expected And");
};
assert_eq!(and_expr.operands.len(), 3);
assert_eq!(and_expr.operands[0], Expr::arg(0));
assert_eq!(and_expr.operands[1], Expr::arg(1));
let Expr::Or(or_expr) = &and_expr.operands[2] else {
panic!("expected Or");
};
assert_eq!(or_expr.operands.len(), 2);
assert_eq!(or_expr.operands[0], Expr::arg(2));
assert_eq!(or_expr.operands[1], Expr::arg(3));
}
#[test]
fn factoring_three_ands() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(2)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(3)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
let Some(Expr::And(and_expr)) = result else {
panic!("expected And");
};
assert_eq!(and_expr.operands.len(), 2);
assert_eq!(and_expr.operands[0], Expr::arg(0));
let Expr::Or(or_expr) = &and_expr.operands[1] else {
panic!("expected Or");
};
assert_eq!(or_expr.operands.len(), 3);
assert_eq!(or_expr.operands[0], Expr::arg(1));
assert_eq!(or_expr.operands[1], Expr::arg(2));
assert_eq!(or_expr.operands[2], Expr::arg(3));
}
#[test]
fn factoring_no_common() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::And(ExprAnd {
operands: vec![Expr::arg(0), Expr::arg(1)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::arg(2), Expr::arg(3)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
}
#[test]
fn complement_basic() {
use toasty_core::stmt::ExprNot;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let a = Expr::eq(Expr::arg(0), Expr::arg(1));
let mut expr = ExprOr {
operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
assert!(result.unwrap().is_true());
}
#[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);
let a = Expr::eq(Expr::arg(0), Expr::arg(1));
let mut expr = ExprOr {
operands: vec![
a.clone(),
Expr::arg(2),
Expr::Not(ExprNot { expr: Box::new(a) }),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
assert!(result.unwrap().is_true());
}
#[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);
let a = Expr::arg(0);
let mut expr = ExprOr {
operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
};
let result = simplify.simplify_expr_or(&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);
let a = Expr::eq(Expr::arg(0), Expr::arg(1));
let mut expr = ExprOr {
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_or(&mut expr);
assert!(result.is_some());
assert!(result.unwrap().is_true());
}
#[test]
fn or_to_in_basic() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(2i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(3i64))),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
let Some(Expr::InList(in_list)) = result else {
panic!("expected InList");
};
assert_eq!(*in_list.expr, Expr::arg(0));
}
#[test]
fn or_to_in_two_values() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(2i64))),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
assert!(matches!(result, Some(Expr::InList(_))));
}
#[test]
fn or_to_in_single_value_not_converted() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64)))],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_some());
assert!(matches!(result, Some(Expr::BinaryOp(_))));
}
#[test]
fn or_to_in_different_lhs_not_converted() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(1), Expr::Value(Value::from(2i64))),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
}
#[test]
fn or_to_in_mixed_keeps_other_operands() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(2i64))),
Expr::eq(Expr::arg(1), Expr::Value(Value::from(3i64))),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
let has_in_list = expr.operands.iter().any(|e| matches!(e, Expr::InList(_)));
let has_binary_op = expr.operands.iter().any(|e| matches!(e, Expr::BinaryOp(_)));
assert!(has_in_list);
assert!(has_binary_op);
}
#[test]
fn or_to_in_multiple_groups() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(2i64))),
Expr::eq(Expr::arg(1), Expr::Value(Value::from(3i64))),
Expr::eq(Expr::arg(1), Expr::Value(Value::from(4i64))),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
assert!(expr.operands.iter().all(|e| matches!(e, Expr::InList(_))));
}
#[test]
fn or_to_in_with_non_equality_preserved() {
use toasty_core::stmt::Value;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::Value(Value::from(1i64))),
Expr::eq(Expr::arg(0), Expr::Value(Value::from(2i64))),
Expr::arg(2), ],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
let has_in_list = expr.operands.iter().any(|e| matches!(e, Expr::InList(_)));
let has_arg = expr.operands.iter().any(|e| matches!(e, Expr::Arg(_)));
assert!(has_in_list);
assert!(has_arg);
}
#[test]
fn or_to_in_non_const_rhs_not_converted() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::arg(0), Expr::arg(1)),
Expr::eq(Expr::arg(0), Expr::arg(2)),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
assert!(expr.operands.iter().all(|e| matches!(e, Expr::BinaryOp(_))));
}
#[test]
fn idempotent_not_simplified_for_non_deterministic() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![Expr::last_insert_id(), Expr::last_insert_id()],
};
let result = simplify.simplify_expr_or(&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);
let a = Expr::eq(Expr::last_insert_id(), 1i64);
let mut expr = ExprOr {
operands: vec![a.clone(), Expr::Not(ExprNot { expr: Box::new(a) })],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
}
#[test]
fn factoring_not_simplified_for_non_deterministic() {
use toasty_core::stmt::ExprAnd;
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::And(ExprAnd {
operands: vec![Expr::last_insert_id(), Expr::arg(0)],
}),
Expr::And(ExprAnd {
operands: vec![Expr::last_insert_id(), Expr::arg(1)],
}),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
for operand in &expr.operands {
let Expr::And(and) = operand else {
panic!("expected AND branch, got {operand:?}");
};
assert_eq!(and.operands.len(), 2);
}
}
#[test]
fn or_to_in_list_not_simplified_for_non_deterministic() {
let schema = test_schema();
let mut simplify = Simplify::new(&schema, &toasty_core::driver::Capability::SQLITE);
let mut expr = ExprOr {
operands: vec![
Expr::eq(Expr::last_insert_id(), 1i64),
Expr::eq(Expr::last_insert_id(), 2i64),
],
};
let result = simplify.simplify_expr_or(&mut expr);
assert!(result.is_none());
assert_eq!(expr.operands.len(), 2);
for operand in &expr.operands {
assert!(matches!(operand, Expr::BinaryOp(_)));
}
}