Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some simplification rules for floating-point arithmetic operations #7515

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
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
40 changes: 32 additions & 8 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,28 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
right: _,
}) if is_null(&left) => *left,

// A * 0 --> 0 (if A is not null)
// A * 0 --> 0 (if A is not null and not floating, since NAN * 0 -> NAN)
Expr::BinaryExpr(BinaryExpr {
left,
op: Multiply,
right,
}) if !info.nullable(&left)? && is_zero(&right) => *right,
// 0 * A --> 0 (if A is not null)
}) if !info.nullable(&left)?
&& !info.get_data_type(&left)?.is_floating()
&& is_zero(&right) =>
{
*right
}
// 0 * A --> 0 (if A is not null and not floating, since 0 * NAN -> NAN)
Expr::BinaryExpr(BinaryExpr {
left,
op: Multiply,
right,
}) if !info.nullable(&right)? && is_zero(&left) => *left,
}) if !info.nullable(&right)?
&& !info.get_data_type(&right)?.is_floating()
&& is_zero(&left) =>
{
*left
}

//
// Rules for Divide
Expand Down Expand Up @@ -734,19 +744,33 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
op: Modulo,
right: _,
}) if is_null(&left) => *left,
// A % 1 --> 0
// A % 1 --> 0 (if A is not nullable and not floating, since NAN % 1 --> NAN)
Expr::BinaryExpr(BinaryExpr {
left,
op: Modulo,
right,
}) if !info.nullable(&left)? && is_one(&right) => lit(0),
// A % 0 --> DivideByZero Error
}) if !info.nullable(&left)?
&& !info.get_data_type(&left)?.is_floating()
&& is_one(&right) =>
{
lit(0)
}
// A % 0 --> DivideByZero Error (if A is not floating and not null)
// A % 0 --> NAN (if A is floating and not null)
Expr::BinaryExpr(BinaryExpr {
left,
op: Modulo,
right,
}) if !info.nullable(&left)? && is_zero(&right) => {
return Err(DataFusionError::ArrowError(ArrowError::DivideByZero));
match info.get_data_type(&left)? {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really understand the rationale for float % float --> NaN (rather than error)

But on the other hand, postgres doesn't seem to support % on floating point values:

select 1.0::float % 0::float;

operator does not exist: double precision % double precision LINE 1: select 1.0::float % 0::float; ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

Copy link
Member Author

Choose a reason for hiding this comment

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

@alamb
DataFusion utilizes the rem() function from the arrow-rs to perform the Modulo operation.

The modification here ensures that it behaves the same as the rem() function in arrow-rs, i.e., float % 0. --> NAN.
https://github.com/apache/arrow-rs/blob/77455d48cd6609045a4728ba908123de9d0b62fd/arrow-arith/src/numeric.rs#L71-L77

And in the IEEE 754-2008 standard:

7.2 Invalid operation 7.2.0
For operations producing results in floating-point format, the default result of an operation that signals the
invalid operation exception shall be a quiet NaN...
These operations are:
...
f) remainder: remainder(x, y), when y is zero or x is infinite...
...

Ref: https://en.wikipedia.org/wiki/NaN#Operations_generating_NaN

DataType::Float32 => lit(f32::NAN),
DataType::Float64 => lit(f64::NAN),
_ => {
return Err(DataFusionError::ArrowError(
ArrowError::DivideByZero,
));
}
}
}

//
Expand Down
Loading