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

Check the scale before converting xcontent long values, rather than the absolute value #111538

Merged
merged 3 commits into from
Aug 5, 2024

Conversation

thecoop
Copy link
Member

@thecoop thecoop commented Aug 2, 2024

Check the BigDecimal scale before trying to convert to a long - this is a faster check than converting to BigInteger and checking the resulting value

@thecoop thecoop added :Core/Infra/Core Core issues without another label >refactoring labels Aug 2, 2024
@thecoop thecoop requested a review from a team as a code owner August 2, 2024 09:16
@elasticsearchmachine elasticsearchmachine added Team:Core/Infra Meta label for core/infra team v8.16.0 labels Aug 2, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@thecoop thecoop added v8.15.0 auto-backport Automatically create backport pull requests when merged labels Aug 2, 2024
Copy link
Contributor

@mosche mosche left a comment

Choose a reason for hiding this comment

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

lgtm 💯

@thecoop thecoop merged commit 17f8192 into elastic:main Aug 5, 2024
2 of 3 checks passed
@thecoop thecoop deleted the long-scale-check branch August 5, 2024 09:31
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.15

thecoop added a commit to thecoop/elasticsearch that referenced this pull request Aug 5, 2024
…he absolute value (elastic#111538)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
thecoop added a commit to thecoop/elasticsearch that referenced this pull request Aug 5, 2024
…he absolute value (elastic#111538)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
thecoop added a commit that referenced this pull request Aug 5, 2024
…he absolute value (#111538) (#111583)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
thecoop added a commit that referenced this pull request Aug 5, 2024
…he absolute value (#111538) (#111584)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
// large scale -> very small number
if (bigDecimalValue.scale() > 19) {
if (coerce) {
bigIntegerValue = BigInteger.ZERO;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note to self: a BigDecimal with a scale of 19 can have a value as large as 0.9. When such a number reaches toBigInteger it will round down to zero. Hence, this logic is correct.

if (coerce) {
bigIntegerValue = BigInteger.ZERO;
} else {
throw new ArithmeticException("Number has a decimal part");
Copy link
Contributor

Choose a reason for hiding this comment

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

Note to self: the other IllegalArgumentException reports the actual number, but in this case, the conversion to string would itself cause the problem we're trying to avoid, so we can't really offer any more hints for the user.

builder.field("decimal", "5.5");
builder.field("expInRange", "5e18");
builder.field("expTooBig", "2e100");
builder.field("expTooSmall", "2e-100");
Copy link
Contributor

Choose a reason for hiding this comment

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

@thecoop - we should also test the negative versions of all these values.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been playing with it, and 1e-2147483648 throws an error instead of going to zero. Is that what we want?

Copy link
Contributor

Choose a reason for hiding this comment

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

I made #111641 in case that's helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thats a limitation of BigDecimal, the scale is far too small. I don't think this is a case we need to worry about...

mhl-b pushed a commit that referenced this pull request Aug 8, 2024
…he absolute value (#111538)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
cbuescher pushed a commit to cbuescher/elasticsearch that referenced this pull request Sep 4, 2024
…he absolute value (elastic#111538)

Large numbers are rejected, small numbers rounded to zero (if rounding enabled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged :Core/Infra/Core Core issues without another label >refactoring Team:Core/Infra Meta label for core/infra team v7.17.24 v8.15.0 v8.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants