MCPcopy Create free account
hub / github.com/apache/datafusion / simplify_not_expr

Function simplify_not_expr

datafusion/physical-expr/src/simplifier/not.rs:50–128  ·  view source on GitHub ↗
(
    expr: Arc<dyn PhysicalExpr>,
    schema: &Schema,
)

Source from the content-addressed store, hash-verified

48 note = "This function will be made private in a future release, please file an issue if you have a reason for keeping it public."
49)]
50pub fn simplify_not_expr(
51 expr: Arc<dyn PhysicalExpr>,
52 schema: &Schema,
53) -> Result<Transformed<Arc<dyn PhysicalExpr>>> {
54 // Check if this is a NOT expression
55 let not_expr = match expr.downcast_ref::<NotExpr>() {
56 Some(not_expr) => not_expr,
57 None => return Ok(Transformed::no(expr)),
58 };
59
60 let inner_expr = not_expr.arg();
61
62 // Handle NOT(NOT(expr)) -> expr (double negation elimination)
63 if let Some(inner_not) = inner_expr.downcast_ref::<NotExpr>() {
64 return Ok(Transformed::yes(Arc::clone(inner_not.arg())));
65 }
66
67 // Handle NOT(literal) -> !literal
68 if let Some(literal) = inner_expr.downcast_ref::<Literal>() {
69 if let ScalarValue::Boolean(Some(val)) = literal.value() {
70 return Ok(Transformed::yes(lit(ScalarValue::Boolean(Some(!val)))));
71 }
72 if let ScalarValue::Boolean(None) = literal.value() {
73 return Ok(Transformed::yes(lit(ScalarValue::Boolean(None))));
74 }
75 }
76
77 // Handle NOT(IN list) -> NOT IN list
78 if let Some(in_list_expr) = inner_expr.downcast_ref::<InListExpr>() {
79 let negated = !in_list_expr.negated();
80 let new_in_list = in_list(
81 Arc::clone(in_list_expr.expr()),
82 in_list_expr.list().to_vec(),
83 &negated,
84 schema,
85 )?;
86 return Ok(Transformed::yes(new_in_list));
87 }
88
89 // Handle NOT(binary_expr)
90 if let Some(binary_expr) = inner_expr.downcast_ref::<BinaryExpr>() {
91 if let Some(negated_op) = binary_expr.op().negate() {
92 let new_binary = Arc::new(BinaryExpr::new(
93 Arc::clone(binary_expr.left()),
94 negated_op,
95 Arc::clone(binary_expr.right()),
96 ));
97 return Ok(Transformed::yes(new_binary));
98 }
99
100 // Handle De Morgan's laws for AND/OR
101 match binary_expr.op() {
102 Operator::And => {
103 // NOT(A AND B) -> NOT A OR NOT B
104 let not_left: Arc<dyn PhysicalExpr> =
105 Arc::new(NotExpr::new(Arc::clone(binary_expr.left())));
106 let not_right: Arc<dyn PhysicalExpr> =
107 Arc::new(NotExpr::new(Arc::clone(binary_expr.right())));

Callers 1

simplifyMethod · 0.85

Calls 13

newFunction · 0.85
to_vecMethod · 0.80
litFunction · 0.50
in_listFunction · 0.50
argMethod · 0.45
valueMethod · 0.45
negatedMethod · 0.45
exprMethod · 0.45
listMethod · 0.45
negateMethod · 0.45
opMethod · 0.45
leftMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…