From f54eec5f3e9243252cd3ceaa29e952e111bdceb5 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Thu, 30 May 2024 17:29:32 +1000 Subject: [PATCH] fix: Crash using empty `Series` in `LazyFrame.select()` --- crates/polars-plan/src/logical_plan/lit.rs | 2 +- py-polars/tests/unit/test_cse.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/polars-plan/src/logical_plan/lit.rs b/crates/polars-plan/src/logical_plan/lit.rs index 9cdebad91911..4da749ecffdd 100644 --- a/crates/polars-plan/src/logical_plan/lit.rs +++ b/crates/polars-plan/src/logical_plan/lit.rs @@ -456,7 +456,7 @@ impl Hash for LiteralValue { s.null_count().hash(state); const RANDOM: u64 = 0x2c194fa5df32a367; let mut rng = (len as u64) ^ RANDOM; - for _ in 0..5 { + for _ in 0..std::cmp::min(5, len) { let idx = hash_to_partition(rng, len); s.get(idx).unwrap().hash(state); rng = rng.rotate_right(17).wrapping_add(RANDOM); diff --git a/py-polars/tests/unit/test_cse.py b/py-polars/tests/unit/test_cse.py index 8e8f8057cd35..be65492f374f 100644 --- a/py-polars/tests/unit/test_cse.py +++ b/py-polars/tests/unit/test_cse.py @@ -774,3 +774,9 @@ def test_nested_cache_no_panic_16553() -> None: assert pl.LazyFrame().select(a=[[[1]]]).collect(comm_subexpr_elim=True).to_dict( as_series=False ) == {"a": [[[[1]]]]} + + +def test_hash_empty_series_16577() -> None: + s = pl.Series(values=None) + out = pl.LazyFrame().select(s).collect() + assert out.equals(s.to_frame())