Skip to content

fix: Decimal conversion with too many low digits #703

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

Merged
merged 2 commits into from
Apr 10, 2025

Conversation

peasee
Copy link
Contributor

@peasee peasee commented Apr 9, 2025

🗣 Description

  • Truncates decimal conversion for situations where the number of low digits are more than the specified scale.

For example, when performing an avg() query with a decimal source column, the output avg column receives a scale of (_, 6) but the actual data received was a decimal with 16 fractional digits (e.g. 12.3456789000000000). In this example, we truncate the digits to the specified scale for the field leaving behind the actual data of 12.345678.

Depending on the query, the existing behavior can result in panics from subtraction overflow. For example: spiceai/spiceai#1907

Copy link
Owner

@pacman82 pacman82 left a comment

Choose a reason for hiding this comment

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

Great PR, still have some questions / change requests though.

n
}

trait ToDecimal:
FromRadix10 + FromRadix10Signed + Add<Output = Self> + Sub<Output = Self> + MulAssign + Ord
FromRadix10 + FromRadix10Signed + Add<Output = Self> + Sub<Output = Self> + MulAssign + std::ops::Div<Output = Self> // Add Div requirement for right-shifting
Copy link
Owner

Choose a reason for hiding this comment

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

  • Could we use DivAssign instead of Copy and Div?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, no. Using DivAssign instead of Div and Copy results in:

cannot divide `I` by `I`

consider further restricting type parameter `I` with trait `Div`: ` + std::ops::Div<Output = I>`

n
}

trait ToDecimal:
FromRadix10 + FromRadix10Signed + Add<Output = Self> + Sub<Output = Self> + MulAssign + Ord
FromRadix10 + FromRadix10Signed + Add<Output = Self> + Sub<Output = Self> + MulAssign + std::ops::Div<Output = Self> // Add Div requirement for right-shifting
+ Ord
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we need Ord don't we only compare usize if we count digits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, Ord was a trait bound that already existed. I tried removing it, and some errors for equality occur:

binary operation `<` cannot be applied to type `I`
binary operation `==` cannot be applied to type `I`

We could use PartialEq and PartialOrd instead, but it's basically equivalent to Ord anyway.

@pacman82 pacman82 merged commit a1646fb into pacman82:main Apr 10, 2025
7 checks passed
@pacman82
Copy link
Owner

Merged and released, thanks for the contribution

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.

2 participants