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: Fix PartialEq for DataType::Unknown #15992

Merged
merged 1 commit into from
May 1, 2024
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
4 changes: 4 additions & 0 deletions crates/polars-core/src/datatypes/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl PartialEq for DataType {
(Array(left_inner, left_width), Array(right_inner, right_width)) => {
left_width == right_width && left_inner == right_inner
},
(Unknown(l), Unknown(r)) => match (l, r) {
(UnknownKind::Int(_), UnknownKind::Int(_)) => true,
_ => l == r,
},
_ => std::mem::discriminant(self) == std::mem::discriminant(other),
}
}
Expand Down
9 changes: 3 additions & 6 deletions crates/polars-plan/src/dsl/function_expr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,9 @@ impl<'a> FieldsMapper<'a> {
Some(dtype) => dtype,
// Supertype of `new` and `default`
None => {
let default = if let Some(default) = self.fields.get(3) {
default
} else {
&self.fields[0]
};
try_get_supertype(self.fields[2].data_type(), default.data_type())?
let column_type = &self.fields[0];
let new = &self.fields[2];
try_get_supertype(column_type.data_type(), new.data_type())?
},
};
self.with_dtype(dtype)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,7 @@ pub(super) fn process_binary(
right: node_right,
}));
},
(Unknown(lhs), Unknown(rhs)) if lhs == rhs => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't trust this.

// Materialize if both are dynamic
let left = unpack!(materialize(left));
let right = unpack!(materialize(right));
let left = expr_arena.add(left);
let right = expr_arena.add(right);

return Ok(Some(AExpr::BinaryExpr { left, op, right }));
},
(Unknown(lhs), Unknown(rhs)) if lhs == rhs => return Ok(None),
_ => {
unpack!(early_escape(&type_left, &type_right));
},
Expand Down Expand Up @@ -328,7 +320,6 @@ pub(super) fn process_binary(
Ok(None)
} else {
// Coerce types:

let st = unpack!(get_supertype(&type_left, &type_right));
let mut st = modify_supertype(st, left, right, &type_left, &type_right);

Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/functions/test_when_then.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,11 @@ def test_when_then_parametric(
assert_frame_equal(ref, ans)
else:
assert ref["if_true"].to_list() == ans["if_true"].to_list()


def test_when_then_supertype_15975() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})

assert df.with_columns(
pl.when(True).then(1 ** pl.col("a") + 1.0 * pl.col("a"))
).to_dict(as_series=False) == {"a": [1, 2, 3], "literal": [2.0, 3.0, 4.0]}
4 changes: 2 additions & 2 deletions py-polars/tests/unit/operations/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,14 @@ def test_replace_fast_path_many_to_one() -> None:
def test_replace_fast_path_many_to_one_default() -> None:
lf = pl.LazyFrame({"a": [1, 2, 2, 3]})
result = lf.select(pl.col("a").replace([2, 3], 100, default=-1))
expected = pl.LazyFrame({"a": [-1, 100, 100, 100]}, schema={"a": pl.Int32})
expected = pl.LazyFrame({"a": [-1, 100, 100, 100]}, schema={"a": pl.Int64})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was wrong as the original dtype was i64.

assert_frame_equal(result, expected)


def test_replace_fast_path_many_to_one_null() -> None:
lf = pl.LazyFrame({"a": [1, 2, 2, 3]})
result = lf.select(pl.col("a").replace([2, 3], None, default=-1))
expected = pl.LazyFrame({"a": [-1, None, None, None]}, schema={"a": pl.Int32})
expected = pl.LazyFrame({"a": [-1, None, None, None]}, schema={"a": pl.Int64})
assert_frame_equal(result, expected)


Expand Down
Loading