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

refactor!: remove IntermediateDecimal in favor of BigDecimal #283

Merged
merged 10 commits into from
Oct 21, 2024

Conversation

JayWhite2357
Copy link
Contributor

Rationale for this change

Currently, the code handling decimal parsing and conversions is somewhat scattered. This issue seeks to resolve this by using the bigdecimal::BigDecimal type for as much as possible.

What changes are included in this PR?

Remove IntermediateDecimal in favor of BigDecimal
Please read the individual commit messages for detailed changes.

Are these changes tested?

Yes.

Copy link
Contributor

@iajoiner iajoiner left a comment

Choose a reason for hiding this comment

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

@JayWhite2357 This is great!

`BigDecimal` gives "precision" and "scale" as 64-bit integers. `IntermediateDecimal` previously simply converted these to 8-bit integers, meaning that for precision or scale of 256 or 128 digits the code will return the wrong value. (Until recently, it actually wrapped the value, which is even more problematic.)
This commit fixes this by returning the proper value and requiring upstream usage to handle any casting explicitly as needed. To facilitate this, a conversion from `u64` to `Precision` is added.
…ermediateDecimal

Implementing `TryFrom<&str>` and `TryFrom<String>` is an anti-pattern since this is exactly what `FromStr` is for.
This commit removes the implementations and any uses of them.
… crate

precision and scale are unused within the `proof-of-sql-parser` crate, and as a result, it doesn't make sense to have that logic in the crate.
This commit moves it into the `proof-of-sql` crate via a `IntermediateDecimalExt` trait.

NOTE: this trait will be renamed to `BigDecimalExt` in a future commit.
…ermediate_decimal_to_scalar`

`try_into_to_scalar` is poorly named.
Even `try_convert_intermediate_decimal_to_scalar` is not great. This will be further renamed in another commit, but renaming it here helps clarify future code changes.
The only place within the intermediate AST that uses `IntermediateDecimal` is the `Literal`. We will replace it in the `Literal` itself in the next commit. This replaces it with `BigDecimal` in the utility function.
This actually does the replacement of `IntermediateDecimal` with `BigDecimal`.
All of the previous commits in this PR set up for this commit, which makes this commit very straightforward.

NOTE: care is taken to ensure that trailing and leading zeros are removed when needed.

The following commits will be cleanup.
This error type will be tidied up in a future PR, but it is at least moved out of the `proof-of-sql-parser` crate.
Fixing errors types is a bit more complex, so to keep this PR small, we opt to only do this here, even though it is not tidy.
As this extension trait now applied to `BigDecimal`, it is named appropriately.
This is an unrelated commit. The `Decimal` type is unused. We remove it here. If it is needed later, it can be added back.
@iajoiner iajoiner force-pushed the refactor/remove-intermediate-decimal branch from 2eb52a4 to 73077df Compare October 21, 2024 13:16
@iajoiner iajoiner merged commit a225c08 into main Oct 21, 2024
11 checks passed
@iajoiner iajoiner deleted the refactor/remove-intermediate-decimal branch October 21, 2024 13:34
Copy link

🎉 This PR is included in version 0.32.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants