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: bug preventing decimals in (-1, 1) from being inserted / queried #8720

Merged
merged 1 commit into from
Feb 5, 2022

Conversation

maheshba
Copy link
Member

@maheshba maheshba commented Feb 4, 2022

Description

Fix for bug in #8413 where ksqldb doesn't allow non-zero decimals between (-0.1, 0.1) to be inserted or queried. The root cause is that we use BigDecimal to parse the string, and it has a different behavior for precision than the SQL standard for (-1, 1): "0.005" has precision 1 in BigDecimal, whereas SQL expects it to have precision 4 (total number of digits). For anything in (-0.1, 0.1) this causes the code to throw since scale is greater than precision.

Something to note: the fix introduces a quirk in how decimals starting with a period are processed: "0.005" and ".005" are both treated as having precision 4, whereas SQL expects precision 3 for the latter. To fix this we'd have to stop using BigDecimal as the internal representation (since it parses both strings identically) or extend it somehow. This doesn't change ksql behavior for the worse since right now both strings would fail.

Testing done

Added a unit test and a functional test; locally deployed and verified the case described in #8413.

Reviewer checklist

  • Ensure docs are updated if necessary. (eg. if a user visible feature is being added or changed).
  • Ensure relevant issues are linked (description should include text like "Fixes #")

@maheshba maheshba requested a review from a team as a code owner February 4, 2022 08:33
Copy link
Contributor

@agavra agavra left a comment

Choose a reason for hiding this comment

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

nice! thanks for the fix @maheshba - I'll save the full review for @nateab but I had a spare cycle so figured I'd look at the code

@@ -357,6 +357,18 @@
{"topic": "test_topic", "key": 1, "value": {"V0": 0.00}}
]
},
{
"name": "should insert decimals between zero and one",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rest query translation tests take much longer to run than the standard QTT tests. instead it may make sense to just add a QTT test where we use a decimal literal that's 1 > x > -1

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you! Will do that. I'm still figuring out how to run the feature tests selectively locally (this test is breaking anyway right now) -- will fix and update!

Copy link
Contributor

Choose a reason for hiding this comment

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

Here are my configurations (the first runs all QTT tests, the second runs just a specific file). Also FYI there exists a decimal.json QTT test.

image
image

I think similar things work for RQTT

Copy link
Member

Choose a reason for hiding this comment

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

@agavra Would it be ok for me to steal these images and toss them in a debugging.md file?:)

Copy link
Member Author

Choose a reason for hiding this comment

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

@agavra Thank you, this is super helpful! I arrived at the same IntelliJ dialog after a bunch of messing around, but your setup is a lot cleaner!

Copy link
Contributor

Choose a reason for hiding this comment

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

@jnh5y go for it :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Interestingly the bug doesn't trigger on the query translation tests, only on the rest query translation tests. Leaving the rest-based test in here while I figure out exactly why the query translation tests don't trigger it...

Copy link
Member

@nateab nateab left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the fix, and congrats on your first ksqldb change!! 🎉

for this edge case, we just take the scale and add one and use that for the precision instead.
*/
if (decimal.compareTo(BigDecimal.ZERO) == 0) {
/* We can't use BigDecimal.precision() directly for all cases, since it defines
Copy link
Member

Choose a reason for hiding this comment

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

Great documentation and explanation! If we ever decide to strictly follow the SQL standard for precision, it will be clear where to start from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scale/Precision error with decimal literal between -1 and 1 (0 left of decimal point)
4 participants