Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions datafusion/optimizer/src/simplify_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this checking for A / null (not null / A as the comments suggest)?
(as in I think the comments are backwards)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this, I will update.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// A / A --> 1 (if a is not nullable)
BinaryExpr {
left,
Expand Down Expand Up @@ -979,6 +985,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"));
Expand Down