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 floordiv / modulo with scalar 0 on LHS #19143

Merged
merged 1 commit into from
Oct 8, 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
30 changes: 15 additions & 15 deletions crates/polars-compute/src/arithmetic/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ macro_rules! impl_signed_arith_kernel {
}

fn prim_wrapping_floor_div_scalar_lhs(lhs: $T, rhs: PArr<$T>) -> PArr<$T> {
if lhs == 0 {
return rhs.fill_with(0);
}

let mask = rhs.tot_ne_kernel_broadcast(&0);
let valid = combine_validities_and(rhs.validity(), Some(&mask));
let ret = prim_unary_values(rhs, |x| lhs.wrapping_floor_div_mod(x).0);
let ret = if lhs == 0 {
rhs.fill_with(0)
} else {
prim_unary_values(rhs, |x| lhs.wrapping_floor_div_mod(x).0)
};
ret.with_validity(valid)
}

Expand All @@ -165,13 +165,13 @@ macro_rules! impl_signed_arith_kernel {
}

fn prim_wrapping_trunc_div_scalar_lhs(lhs: $T, rhs: PArr<$T>) -> PArr<$T> {
if lhs == 0 {
return rhs.fill_with(0);
}

let mask = rhs.tot_ne_kernel_broadcast(&0);
let valid = combine_validities_and(rhs.validity(), Some(&mask));
let ret = prim_unary_values(rhs, |x| if x != 0 { lhs.wrapping_div(x) } else { 0 });
let ret = if lhs == 0 {
rhs.fill_with(0)
} else {
prim_unary_values(rhs, |x| if x != 0 { lhs.wrapping_div(x) } else { 0 })
};
ret.with_validity(valid)
}

Expand Down Expand Up @@ -205,13 +205,13 @@ macro_rules! impl_signed_arith_kernel {
}

fn prim_wrapping_mod_scalar_lhs(lhs: $T, rhs: PArr<$T>) -> PArr<$T> {
if lhs == 0 {
return rhs.fill_with(0);
}

let mask = rhs.tot_ne_kernel_broadcast(&0);
let valid = combine_validities_and(rhs.validity(), Some(&mask));
let ret = prim_unary_values(rhs, |x| lhs.wrapping_floor_div_mod(x).1);
let ret = if lhs == 0 {
rhs.fill_with(0)
} else {
prim_unary_values(rhs, |x| lhs.wrapping_floor_div_mod(x).1)
};
ret.with_validity(valid)
}

Expand Down
20 changes: 10 additions & 10 deletions crates/polars-compute/src/arithmetic/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ macro_rules! impl_unsigned_arith_kernel {
}

fn prim_wrapping_floor_div_scalar_lhs(lhs: $T, rhs: PArr<$T>) -> PArr<$T> {
if lhs == 0 {
return rhs.fill_with(0);
}

let mask = rhs.tot_ne_kernel_broadcast(&0);
let valid = combine_validities_and(rhs.validity(), Some(&mask));
let ret = prim_unary_values(rhs, |x| if x != 0 { lhs / x } else { 0 });
let ret = if lhs == 0 {
rhs.fill_with(0)
} else {
prim_unary_values(rhs, |x| if x != 0 { lhs / x } else { 0 })
};
ret.with_validity(valid)
}

Expand All @@ -125,13 +125,13 @@ macro_rules! impl_unsigned_arith_kernel {
}

fn prim_wrapping_mod_scalar_lhs(lhs: $T, rhs: PArr<$T>) -> PArr<$T> {
if lhs == 0 {
return rhs.fill_with(0);
}

let mask = rhs.tot_ne_kernel_broadcast(&0);
let valid = combine_validities_and(rhs.validity(), Some(&mask));
let ret = prim_unary_values(rhs, |x| if x != 0 { lhs % x } else { 0 });
let ret = if lhs == 0 {
rhs.fill_with(0)
} else {
prim_unary_values(rhs, |x| if x != 0 { lhs % x } else { 0 })
};
ret.with_validity(valid)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,8 @@ def test_date_datetime_sub() -> None:
def test_raise_invalid_shape() -> None:
with pytest.raises(pl.exceptions.InvalidOperationError):
pl.DataFrame([[1, 2], [3, 4]]) * pl.DataFrame([1, 2, 3])


def test_integer_divide_scalar_zero_lhs_19142() -> None:
assert_series_equal(pl.Series([0]) // pl.Series([1, 0]), pl.Series([0, None]))
assert_series_equal(pl.Series([0]) % pl.Series([1, 0]), pl.Series([0, None]))
Loading