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

Conversation

jonahgao
Copy link
Member

@jonahgao jonahgao commented Sep 9, 2023

Which issue does this PR close?

N/A

Rationale for this change

Similar to #7503.

Because of the presence of NaN, some simplification rules will no longer be applicable to floating-point types.

What changes are included in this PR?

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Sep 9, 2023
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

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @jonahgao -- these changes look good to me (though as I mention, I don't understand the behavior for % vs /).

As always, very nicely tested 🏅

@alamb alamb merged commit 87527c4 into apache:main Sep 11, 2023
@jonahgao jonahgao deleted the float_simplification branch September 12, 2023 03:28
@alamb
Copy link
Contributor

alamb commented Sep 12, 2023

Thanks for the clarification @jonahgao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants