This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/master by this push:
new 6d2b417de Simplify null division. (#3625)
6d2b417de is described below
commit 6d2b417def2d536e81eb80e082d9cc3ca4e7f5fc
Author: Remzi Yang <[email protected]>
AuthorDate: Sat Oct 1 04:07:45 2022 +0800
Simplify null division. (#3625)
* fix
Signed-off-by: remzi <[email protected]>
* fix comment
Signed-off-by: remzi <[email protected]>
Signed-off-by: remzi <[email protected]>
---
datafusion/optimizer/src/simplify_expressions.rs | 25 ++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/datafusion/optimizer/src/simplify_expressions.rs
b/datafusion/optimizer/src/simplify_expressions.rs
index a10f92c65..969fa0169 100644
--- a/datafusion/optimizer/src/simplify_expressions.rs
+++ b/datafusion/optimizer/src/simplify_expressions.rs
@@ -777,12 +777,18 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a,
S> {
op: Divide,
right,
} if is_one(&right) => *left,
- // A / null --> null
+ // null / A --> null
BinaryExpr {
left,
op: Divide,
+ right: _,
+ } if is_null(&left) => *left,
+ // A / null --> null
+ BinaryExpr {
+ left: _,
+ op: Divide,
right,
- } if left == right && is_null(&left) => *left,
+ } if is_null(&right) => *right,
// A / A --> 1 (if a is not nullable)
BinaryExpr {
left,
@@ -1039,6 +1045,21 @@ mod tests {
assert_eq!(simplify(expr), expected);
}
+ #[test]
+ fn test_simplify_divide_null() {
+ // A / null --> null
+ let null = Expr::Literal(ScalarValue::Null);
+ {
+ let expr = binary_expr(col("c"), Operator::Divide, null.clone());
+ assert_eq!(simplify(expr), null);
+ }
+ // null / A --> null
+ {
+ let expr = binary_expr(null.clone(), Operator::Divide, col("c"));
+ assert_eq!(simplify(expr), null);
+ }
+ }
+
#[test]
fn test_simplify_divide_by_same() {
let expr = binary_expr(col("c2"), Operator::Divide, col("c2"));