Skip to content

Commit

Permalink
fix: Decimal from physical in horizontal min/max and shift (#20487)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Dec 28, 2024
1 parent 3aaf4c2 commit f43a7d4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/polars-ops/src/series/ops/floor_divide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ pub fn floor_div_series(a: &Series, b: &Series) -> PolarsResult<Series> {
floor_div_ca(a, b).into_series()
});

out.cast(logical_type)
unsafe { out.from_physical_unchecked(logical_type) }
}
10 changes: 6 additions & 4 deletions crates/polars-ops/src/series/ops/horizontal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ fn min_max_binary_columns(left: &Column, right: &Column, min: bool) -> PolarsRes
let a: &ChunkedArray<$T> = lhs.as_ref().as_ref().as_ref();
let b: &ChunkedArray<$T> = rhs.as_ref().as_ref().as_ref();

if min {
min_binary(a, b).into_series().cast(logical)
} else {
max_binary(a, b).into_series().cast(logical)
unsafe {
if min {
min_binary(a, b).into_series().from_physical_unchecked(logical)
} else {
max_binary(a, b).into_series().from_physical_unchecked(logical)
}
}
})
.map(Column::from)
Expand Down
10 changes: 6 additions & 4 deletions crates/polars-plan/src/dsl/function_expr/shift_and_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ pub(super) fn shift_and_fill(args: &[Column]) -> PolarsResult<Column> {
AnyValue::Null => None,
v => polars_bail!(ComputeError: "fill value '{}' is not supported", v),
};
ca.shift_and_fill(n, fill_value.as_ref())
.into_column()
.cast(logical)
unsafe {
ca.shift_and_fill(n, fill_value.as_ref())
.into_column()
.from_physical_unchecked(logical)
}
},
#[cfg(feature = "object")]
Object(_, _) => shift_and_fill_with_mask(s, n, fill_value_s),
Expand All @@ -102,7 +104,7 @@ pub(super) fn shift_and_fill(args: &[Column]) -> PolarsResult<Column> {
}};
}
let out = downcast_as_macro_arg_physical!(physical, dispatch, n, fill_value);
out.cast(logical)
unsafe { out.from_physical_unchecked(logical) }
},
dt => polars_bail!(opq = shift_and_fill, dt),
}
Expand Down
25 changes: 25 additions & 0 deletions py-polars/tests/unit/datatypes/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,28 @@ def test_decimal_arithmetic_schema_float_20369() -> None:
assert_series_equal(
(1.0 * s), pl.Series("literal", [1.0], dtype=pl.Decimal(None, 4))
)


def test_decimal_horizontal_20482() -> None:
b = pl.LazyFrame(
{
"a": [D("123.000000"), D("234.000000")],
"b": [D("123.000000"), D("234.000000")],
},
schema={
"a": pl.Decimal(18, 6),
"b": pl.Decimal(18, 6),
},
)

assert (
b.select(
min=pl.min_horizontal(pl.col("a"), pl.col("b")),
max=pl.max_horizontal(pl.col("a"), pl.col("b")),
sum=pl.sum_horizontal(pl.col("a"), pl.col("b")),
).collect()
).to_dict(as_series=False) == {
"min": [D("123.000000"), D("234.000000")],
"max": [D("123.000000"), D("234.000000")],
"sum": [D("246.000000"), D("468.000000")],
}

0 comments on commit f43a7d4

Please sign in to comment.