Skip to content

Commit

Permalink
fix[rust]: fix is_in empty list (#4640)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 31, 2022
1 parent 50eba3d commit 0433e98
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions polars/polars-lazy/src/dsl/function_expr/is_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
pub(super) fn is_in(s: &mut [Series]) -> Result<Series> {
let left = &s[0];
let other = &s[1];
dbg!(left, other);

left.is_in(other).map(|ca| ca.into_series())
}
14 changes: 8 additions & 6 deletions polars/polars-lazy/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,14 +1399,16 @@ impl Expr {
pub fn is_in<E: Into<Expr>>(self, other: E) -> Self {
let other = other.into();
let has_literal = has_root_literal_expr(&other);
if has_literal {
if let Expr::Literal(LiteralValue::Series(s)) = &other {
// nothing is in an empty list return all False
if s.is_empty() {
return Expr::Literal(LiteralValue::Boolean(false));
}
if has_literal
&& match &other {
Expr::Literal(LiteralValue::Series(s)) if s.is_empty() => true,
Expr::Literal(LiteralValue::Null) => true,
_ => false,
}
{
return Expr::Literal(LiteralValue::Boolean(false));
}

let arguments = &[other];
// we don't have to apply on groups, so this is faster
if has_literal {
Expand Down
15 changes: 14 additions & 1 deletion py-polars/tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,18 @@ def test_list_eval_type_coercion() -> None:
).to_dict(False) == {"col_last": [[3]]}


def test_is_empty_list_4559() -> None:
def test_is_in_empty_list_4559() -> None:
assert pl.Series(["a"]).is_in([]).to_list() == [False]


def test_is_in_empty_list_4639() -> None:
df = pl.DataFrame({"a": [1, None]})
empty_list: list[int] = []

assert df.with_columns([pl.col("a").is_in(empty_list).alias("a_in_list")]).to_dict(
False
) == {"a": [1, None], "a_in_list": [False, False]}
df = pl.DataFrame()
assert df.with_columns(
[pl.lit(None).cast(pl.Int64).is_in(empty_list).alias("in_empty_list")]
).to_dict(False) == {"in_empty_list": [False]}

0 comments on commit 0433e98

Please sign in to comment.