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: Fix casting decimal to decimal for high precision #16049

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/polars-arrow/src/compute/cast/decimal_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ fn decimal_to_decimal_impl<F: Fn(i128) -> Option<i128>>(
to_precision: usize,
to_scale: usize,
) -> PrimitiveArray<i128> {
let min_for_precision = 9_i128
.saturating_pow(1 + to_precision as u32)
.saturating_neg();
let max_for_precision = 9_i128.saturating_pow(1 + to_precision as u32);
Comment on lines -15 to -18
Copy link
Contributor Author

@stinodego stinodego May 4, 2024

Choose a reason for hiding this comment

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

It's pretty bizarre to me that someone wrote this logic. I guess the intention was to define the bounds as e.g. 999 for precision 3, but it completely misses that mark. Am I missing something?

let upper_bound_for_precision = 10_i128.saturating_pow(to_precision as u32);
let lower_bound_for_precision = upper_bound_for_precision.saturating_neg();

let values = from.iter().map(|x| {
x.and_then(|x| {
op(*x).and_then(|x| {
if x > max_for_precision || x < min_for_precision {
if x >= upper_bound_for_precision || x <= lower_bound_for_precision {
None
} else {
Some(x)
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/operations/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,15 @@ def test_cast_array_to_different_width() -> None:
pl.InvalidOperationError, match="cannot cast Array to a different width"
):
s.cast(pl.Array(pl.Int16, 3))


def test_cast_decimal_to_decimal_high_precision() -> None:
precision = 22
values = [Decimal("9" * precision)]
s = pl.Series(values, dtype=pl.Decimal(None, 0))

target_dtype = pl.Decimal(precision, 0)
result = s.cast(target_dtype)

assert result.dtype == target_dtype
assert result.to_list() == values
16 changes: 7 additions & 9 deletions py-polars/tests/unit/sql/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_modulo() -> None:
("value", "sqltype", "prec_scale", "expected_value", "expected_dtype"),
[
(64.5, "numeric", "(3,1)", D("64.5"), pl.Decimal(3, 1)),
(512.5, "decimal", "(3,1)", D("512.5"), pl.Decimal(3, 1)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should have failed. New logic properly causes this to fail.

(512.5, "decimal", "(4,1)", D("512.5"), pl.Decimal(4, 1)),
(512.5, "numeric", "(4,0)", D("512"), pl.Decimal(4, 0)),
(-1024.75, "decimal", "(10,0)", D("-1024"), pl.Decimal(10, 0)),
(-1024.75, "numeric", "(10)", D("-1024"), pl.Decimal(10, 0)),
Expand All @@ -67,18 +67,16 @@ def test_numeric_decimal_type(
with pl.Config(activate_decimals=True):
df = pl.DataFrame({"n": [value]})
with pl.SQLContext(df=df) as ctx:
out = ctx.execute(
result = ctx.execute(
f"""
SELECT n::{sqltype}{prec_scale} AS "dec" FROM df
"""
)
assert_frame_equal(
out.collect(),
pl.DataFrame(
data={"dec": [expected_value]},
schema={"dec": expected_dtype},
),
)
expected = pl.LazyFrame(
data={"dec": [expected_value]},
schema={"dec": expected_dtype},
)
assert_frame_equal(result, expected)


@pytest.mark.parametrize(
Expand Down
Loading