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(rust): check dtypes of single-column 'by' parameter in asof-join #10284

Merged
merged 1 commit into from
Aug 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
9 changes: 7 additions & 2 deletions crates/polars-core/src/frame/asof_join/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,12 @@ fn dispatch_join<T: PolarsNumericType>(
tolerance: Option<AnyValue<'static>>,
) -> PolarsResult<Vec<Option<IdxSize>>> {
let out = if left_by.width() == 1 {
match left_by_s.dtype() {
let left_dtype = left_by_s.dtype();
Copy link
Contributor Author

@mcrumiller mcrumiller Aug 3, 2023

Choose a reason for hiding this comment

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

Looks like a dtype check was missed when the by parameter is a single column.

Copy link
Member

Choose a reason for hiding this comment

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

Right, good catch. Thanks!

let right_dtype = right_by_s.dtype();
polars_ensure!(left_dtype == right_dtype,
ComputeError: "mismatching dtypes in 'by' parameter of asof-join: `{}` and `{}`", left_dtype, right_dtype
);
match left_dtype {
DataType::Utf8 => asof_join_by_binary(
&left_by_s.utf8().unwrap().as_binary(),
&right_by_s.utf8().unwrap().as_binary(),
Expand Down Expand Up @@ -669,7 +674,7 @@ fn dispatch_join<T: PolarsNumericType>(
} else {
for (lhs, rhs) in left_by.get_columns().iter().zip(right_by.get_columns()) {
polars_ensure!(lhs.dtype() == rhs.dtype(),
ComputeError: "mismatching dtypes in 'on' parameter of asof-join: `{}` and `{}`", lhs.dtype(), rhs.dtype()
ComputeError: "mismatching dtypes in 'by' parameter of asof-join: `{}` and `{}`", lhs.dtype(), rhs.dtype()
);
#[cfg(feature = "dtype-categorical")]
_check_categorical_src(lhs.dtype(), rhs.dtype())?;
Expand Down
36 changes: 36 additions & 0 deletions py-polars/tests/unit/operations/test_join_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ def test_asof_join_schema_5684() -> None:
)


def test_join_asof_mismatched_dtypes() -> None:
# test 'on' dtype mismatch
df1 = pl.DataFrame(
{"a": pl.Series([1, 2, 3], dtype=pl.Int64), "b": ["a", "b", "c"]}
)
df2 = pl.DataFrame(
{"a": pl.Series([1, 2, 3], dtype=pl.Int32), "c": ["d", "e", "f"]}
)

with pytest.raises(
pl.exceptions.ComputeError, match="datatypes of join keys don't match"
):
df1.join_asof(df2, on="a", strategy="forward")

# test 'by' dtype mismatch
df1 = pl.DataFrame(
{
"time": pl.date_range(date(2018, 1, 1), date(2018, 1, 8), eager=True),
"group": pl.Series([1, 1, 1, 1, 2, 2, 2, 2], dtype=pl.Int32),
"value": [0, 0, None, None, 2, None, 1, None],
}
)
df2 = pl.DataFrame(
{
"time": pl.date_range(date(2018, 1, 1), date(2018, 1, 8), eager=True),
"group": pl.Series([1, 1, 1, 1, 2, 2, 2, 2], dtype=pl.Int64),
"value": [0, 0, None, None, 2, None, 1, None],
}
)

with pytest.raises(
pl.exceptions.ComputeError, match="mismatching dtypes in 'by' parameter"
):
df1.join_asof(df2, on="time", by="group", strategy="forward")


def test_join_asof_floats() -> None:
df1 = pl.DataFrame({"a": [1.0, 2.0, 3.0], "b": ["lrow1", "lrow2", "lrow3"]})
df2 = pl.DataFrame({"a": [0.59, 1.49, 2.89], "b": ["rrow1", "rrow2", "rrow3"]})
Expand Down