Skip to content

Commit

Permalink
fix: Remove incorrect unsafe pointer cast for int -> enum (pola-rs#15740
Browse files Browse the repository at this point in the history
)
  • Loading branch information
reswqa authored Apr 18, 2024
1 parent ebea94e commit 96e1f01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 0 additions & 3 deletions crates/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ where
polars_bail!(OutOfBounds: "index {} is bigger than the number of categories {}",m,categories.len());
}
}
// SAFETY:
// we are guarded by the type system
let ca = unsafe { &*(self as *const ChunkedArray<T> as *const UInt32Chunked) };
// SAFETY: indices are in bound
unsafe {
Ok(CategoricalChunked::from_cats_and_rev_map_unchecked(
Expand Down
20 changes: 20 additions & 0 deletions py-polars/tests/unit/datatypes/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,23 @@ def test_category_comparison_subset() -> None:
assert out["dt1"].dtype == pl.Enum(["a"])
assert out["dt2"].dtype == pl.Enum(["a", "b"])
assert out["dt1"].dtype != out["dt2"].dtype


@pytest.mark.parametrize(
"dt",
[
pl.UInt8,
pl.UInt16,
pl.UInt32,
pl.UInt64,
pl.Int8,
pl.Int16,
pl.Int32,
pl.Int64,
],
)
def test_integer_cast_to_enum_15738(dt: pl.DataType) -> None:
s = pl.Series([0, 1, 2], dtype=dt).cast(pl.Enum(["a", "b", "c"]))
assert s.to_list() == ["a", "b", "c"]
expected_s = pl.Series(["a", "b", "c"], dtype=pl.Enum(["a", "b", "c"]))
assert_series_equal(s, expected_s)

0 comments on commit 96e1f01

Please sign in to comment.