-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Decimal support for floor preimage #20099
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| // floor always returns an integer, so if value has a fractional part, there's no solution | ||
| // Check: value % one_scaled != 0 means fractional part exists | ||
| if scale > 0 && value % one_scaled != D::Native::ZERO { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about negative scales ?
Arrow supports them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, the logic here comes from the idea that floor(x) = 1.3 has no pre-image solution. We do not want to optimize it via this.
floor(x) will always return a floor data type but it will actually be an integer.
// floor always returns an integer, so if n has a fractional part, there's no solution
if n.fract() != F::zero() {
return None;
}
Same logic is carried over here for Decimal.
scale <= 0 is effectively an integer and hence only possibility is to check for overflow which we check in the next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review comment in the last PR: #20059 (comment)
|
|
||
| // Decimal32: i32::MAX | ||
| // For scale=2, we add 100, so i32::MAX - 50 would overflow | ||
| assert_preimage_none(ScalarValue::Decimal32(Some(i32::MAX - 50), 9, 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly is the idea of this test ?
2147483647 - 50 => 2147483597, has 10 digits and does not fit into precision=9
I think there is no call to add 100 at all here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, you are right.
The comment is misleading here. And the test case should move to test_floor_preimage_decimal_non_integer
Decimal32(Some(i32::MAX - 50), 9, 2) is exactly forcing the case explained in your earlier comment.
2147483597 = 21474835.97 which has fractional part and hence the preimage will be None. This test is not the right place for this.
But, 21474835.97 logically is not representable by Decimal(9,2). That is wrong as well. I didn't think too deep here.
Thanks. Let me fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed a bunch of repetitive tests and moved the tests around. Now it should make sense. Please let me know.
|
|
||
| // Decimal types | ||
| ScalarValue::Decimal32(Some(n), precision, scale) => { | ||
| decimal_preimage_bounds::<Decimal32Type>(*n, *precision, *scale).map( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can do a macros here? 🤔
comphead
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @devanshu0987 maybe we can also add overflow slt tests
Which issue does this PR close?
floorpreimage #20080Rationale for this change
What changes are included in this PR?
Are these changes tested?
Are there any user-facing changes?
No