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

feat: Raise informative error message when join would introduce duplicate column name #15042

Merged
merged 2 commits into from
Mar 14, 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
8 changes: 7 additions & 1 deletion crates/polars-ops/src/frame/join/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ pub fn _finish_join(
let suffix = get_suffix(suffix);

for name in rename_strs {
df_right.rename(&name, &_join_suffix_name(&name, suffix))?;
let new_name = _join_suffix_name(&name, suffix);
df_right.rename(&name, new_name.as_str()).map_err(|_| {
polars_err!(Duplicate: "column with name '{}' already exists\n\n\
You may want to try:\n\
- renaming the column prior to joining\n\
- using the `suffix` parameter to specify a suffix different to the default one ('_right')", new_name)
})?;
}

drop(left_names);
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,17 @@ def test_join_on_nth_error() -> None:
pl.ComputeError, match="nth column selection not supported at this point"
):
df.join(df2, on=pl.first())


def test_join_results_in_duplicate_names() -> None:
lhs = pl.DataFrame(
{
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [1, 2, 3],
"c_right": [1, 2, 3],
}
)
rhs = lhs.clone()
with pytest.raises(pl.DuplicateError, match="'c_right' already exists"):
lhs.join(rhs, on=["a", "b"], how="left")
Loading