pepijnve commented on issue #19092:
URL: https://github.com/apache/datafusion/issues/19092#issuecomment-3679183461
I was tinkering a bit out of curiosity. With the addition of the
simplification in the collapsed details to the physical expression simplifier
<details>
```
pub fn simplify_and_or_expr(
expr: &Arc<dyn PhysicalExpr>,
) -> Result<Transformed<Arc<dyn PhysicalExpr>>> {
// Check if this is a NOT expression
let binary_expr = match expr.as_any().downcast_ref::<BinaryExpr>() {
Some(b) if b.op().eq(&Operator::And) => {
match (as_bool(b.left()), as_bool(b.right())) {
(Some(false), _) => Some(Arc::clone(b.left())),
(_, Some(false)) => Some(Arc::clone(b.right())),
(Some(true), _) => Some(Arc::clone(b.right())),
(_, Some(true)) => Some(Arc::clone(b.left())),
_ => None,
}
},
Some(b) if b.op().eq(&Operator::Or) => {
match (as_bool(b.left()), as_bool(b.right())) {
(Some(true), _) => Some(Arc::clone(b.left())),
(_, Some(true)) => Some(Arc::clone(b.right())),
(Some(false), _) => Some(Arc::clone(b.right())),
(_, Some(false)) => Some(Arc::clone(b.left())),
_ => None,
}
},
_ => None,
};
Ok(match binary_expr {
None => Transformed::no(Arc::clone(expr)),
Some(e) => Transformed::no(e),
})
}
fn as_bool(expr: &Arc<dyn PhysicalExpr>) -> Option<bool> {
match expr.as_any().downcast_ref::<Literal>() {
Some(l) => {
match l.value() {
ScalarValue::Boolean(b) => b.clone(),
_ => None,
}
},
_ => None,
}
}
```
</details>
this test case passes, so `NULL IS NOT NULL or c1 = 24` gets simplified to
`c1 = 24`. Not sure how useful this is, but seems like it might be nice to
strip away cruft after const evaluation.
```
#[test]
fn test_simplify_null_is_not_null_or() -> Result<()> {
let schema = test_schema();
let mut simplifier = PhysicalExprSimplifier::new(&schema);
let c1_is_24 = binary(
col("c1", &schema)?,
Operator::Eq,
lit(ScalarValue::Int32(Some(24))),
&schema,
)?;
let expr = binary(
is_not_null(lit(ScalarValue::Int32(None)))?,
Operator::Or,
Arc::clone(&c1_is_24),
&schema,
)?;
let result = simplifier.simplify(expr)?;
assert_eq!(&result, &c1_is_24);
Ok(())
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]