Skip to content

Commit

Permalink
fix: Correct hash and fmt for struct expr
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed Sep 14, 2023
1 parent 826a1e3 commit 18c2698
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/polars-plan/src/dsl/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ impl Hash for FunctionExpr {
FunctionExpr::Boolean(f) => f.hash(state),
#[cfg(feature = "strings")]
FunctionExpr::StringExpr(f) => f.hash(state),
#[cfg(feature = "dtype-struct")]
FunctionExpr::StructExpr(f) => f.hash(state),
#[cfg(feature = "random")]
FunctionExpr::Random { method, .. } => method.hash(state),
#[cfg(feature = "range")]
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-plan/src/dsl/function_expr/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl Display for StructFunction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use self::*;
match self {
StructFunction::FieldByIndex(_) => write!(f, "struct.field_by_name"),
StructFunction::FieldByName(_) => write!(f, "struct.field_by_index"),
StructFunction::FieldByIndex(index) => write!(f, "struct.field_by_index({index})"),
StructFunction::FieldByName(name) => write!(f, "struct.field_by_name({name})"),
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/test_cse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ def test_union_duplicates() -> None:
)


def test_cse_with_struct_expr_11116() -> None:
df = pl.DataFrame([{"s": {"a": 1, "b": 4}, "c": 3}]).lazy()
out = df.with_columns(
pl.col("s").struct.field("a").alias("s_a"),
pl.col("s").struct.field("b").alias("s_b"),
(
(pl.col("s").struct.field("a") <= pl.col("c"))
& (pl.col("s").struct.field("b") > pl.col("c"))
).alias("c_between_a_and_b"),
).collect(comm_subexpr_elim=True)
assert out.to_dict(False) == {
"s": [{"a": 1, "b": 4}],
"c": [3],
"s_a": [1],
"s_b": [4],
"c_between_a_and_b": [True],
}


def test_cse_schema_6081() -> None:
df = pl.DataFrame(
data=[
Expand Down

0 comments on commit 18c2698

Please sign in to comment.