Apache DataFusion SQL Query Engine
Simplify null division. (#3625)
Remzi Yang6d2b417def2d
diff --git a/datafusion/optimizer/src/simplify_expressions.rs b/datafusion/optimizer/src/simplify_expressions.rsindex a10f92c652..969fa01693 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 --> nullBinaryExpr {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"));