Skip to content

Commit

Permalink
fix[python]: fix json_path_match dtype (#4915)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 21, 2022
1 parent da2b06d commit 8640c14
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion polars/polars-lazy/src/physical_plan/executors/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ impl Executor for FilterExec {
}
let df = self.input.execute(state)?;
let s = self.predicate.evaluate(&df, state)?;
let mask = s.bool().expect("filter predicate wasn't of type boolean");
let mask = s.bool().map_err(|e| {
PolarsError::ComputeError(
format!("Filter predicate must be of type Boolean, got: {:?}", e).into(),
)
})?;

let profile_name = if state.has_node_timer() {
Cow::Owned(format!(".filter({})", &self.predicate.as_ref()))
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ impl PyExpr {
};
self.clone()
.inner
.map(function, GetOutput::from_type(DataType::Boolean))
.map(function, GetOutput::from_type(DataType::Utf8))
.with_fmt("str.json_path_match")
.into()
}
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,11 @@ def test_error_on_double_agg() -> None:
def test_unique_on_list_df() -> None:
with pytest.raises(pl.InvalidOperationError):
pl.DataFrame({"a": [1, 2, 3, 4], "b": [[1, 1], [2], [3], [4, 4]]}).unique()


def test_filter_not_of_type_bool() -> None:
df = pl.DataFrame({"json_val": ['{"a":"hello"}', None, '{"a":"world"}']})
with pytest.raises(
pl.ComputeError, match="Filter predicate must be of type Boolean, got"
):
df.filter(pl.col("json_val").str.json_path_match("$.a"))
7 changes: 7 additions & 0 deletions py-polars/tests/unit/test_utf8.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ def test_min_max_agg_on_str() -> None:
strings = ["b", "a", "x"]
s = pl.Series(strings)
assert (s.min(), s.max()) == ("a", "x")


def test_json_path_match_type_4905() -> None:
df = pl.DataFrame({"json_val": ['{"a":"hello"}', None, '{"a":"world"}']})
assert df.filter(
pl.col("json_val").str.json_path_match("$.a").is_in(["hello"])
).to_dict(False) == {"json_val": ['{"a":"hello"}']}

0 comments on commit 8640c14

Please sign in to comment.