Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: literal hash #11508

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions crates/polars-plan/src/logical_plan/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,26 @@ pub fn lit<L: Literal>(t: L) -> Expr {

impl Hash for LiteralValue {
fn hash<H: Hasher>(&self, state: &mut H) {
if let Some(v) = self.to_anyvalue() {
v.hash_impl(state, true)
} else {
0.hash(state)
std::mem::discriminant(self).hash(state);
match self {
LiteralValue::Series(s) => {
s.dtype().hash(state);
s.len().hash(state);
},
LiteralValue::Range {
low,
high,
data_type,
} => {
low.hash(state);
high.hash(state);
data_type.hash(state)
},
_ => {
if let Some(v) = self.to_anyvalue() {
v.hash_impl(state, true)
}
},
}
}
}
27 changes: 27 additions & 0 deletions py-polars/tests/unit/test_cse.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,30 @@ def test_no_cse_in_with_context() -> None:
],
"label": [0, 1, 1],
}


def test_cse_is_in_11489() -> None:
df = pl.DataFrame(
{"cond": [1, 2, 3, 2, 1], "x": [1.0, 0.20, 3.0, 4.0, 0.50]}
).lazy()
any_cond = (
pl.when(pl.col("cond").is_in([2, 3]))
.then(True)
.when(pl.col("cond").is_in([1]))
.then(False)
.otherwise(None)
.alias("any_cond")
)
val = (
pl.when(any_cond)
.then(1.0)
.when(~any_cond)
.then(0.0)
.otherwise(None)
.alias("val")
)
assert df.select("cond", any_cond, val).collect().to_dict(False) == {
"cond": [1, 2, 3, 2, 1],
"any_cond": [False, True, True, True, False],
"val": [0.0, 1.0, 1.0, 1.0, 0.0],
}