diff --git a/crates/polars-compute/src/arithmetic/signed.rs b/crates/polars-compute/src/arithmetic/signed.rs index 968c82ef96e2..de253a0aeca7 100644 --- a/crates/polars-compute/src/arithmetic/signed.rs +++ b/crates/polars-compute/src/arithmetic/signed.rs @@ -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) } @@ -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) } @@ -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) } diff --git a/crates/polars-compute/src/arithmetic/unsigned.rs b/crates/polars-compute/src/arithmetic/unsigned.rs index 82406a2f94c9..46fc0037597d 100644 --- a/crates/polars-compute/src/arithmetic/unsigned.rs +++ b/crates/polars-compute/src/arithmetic/unsigned.rs @@ -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) } @@ -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) } diff --git a/py-polars/tests/unit/operations/arithmetic/test_arithmetic.py b/py-polars/tests/unit/operations/arithmetic/test_arithmetic.py index 360def065ca1..a4c8e7786f1b 100644 --- a/py-polars/tests/unit/operations/arithmetic/test_arithmetic.py +++ b/py-polars/tests/unit/operations/arithmetic/test_arithmetic.py @@ -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]))