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 when joining on the same keys twice #16329

Merged
merged 3 commits into from
May 20, 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
2 changes: 1 addition & 1 deletion crates/polars-ops/src/frame/join/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl JoinValidation {
}
}

pub(super) fn is_valid_join(&self, join_type: &JoinType) -> PolarsResult<()> {
pub fn is_valid_join(&self, join_type: &JoinType) -> PolarsResult<()> {
if !self.needs_checks() {
return Ok(());
}
Expand Down
13 changes: 1 addition & 12 deletions crates/polars-ops/src/frame/join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ pub trait DataFrameJoinOps: IntoDf {
_verbose: bool,
) -> PolarsResult<DataFrame> {
let left_df = self.to_df();
args.validation.is_valid_join(&args.how)?;

let should_coalesce = args.coalesce.coalesce(&args.how);
assert_eq!(selected_left.len(), selected_right.len());

#[cfg(feature = "cross_join")]
if let JoinType::Cross = args.how {
Expand Down Expand Up @@ -163,16 +162,6 @@ pub trait DataFrameJoinOps: IntoDf {
}
}

polars_ensure!(
selected_left.len() == selected_right.len(),
ComputeError:
format!(
"the number of columns given as join key (left: {}, right:{}) should be equal",
selected_left.len(),
selected_right.len()
)
);

if let Some((l, r)) = selected_left
.iter()
.zip(&selected_right)
Expand Down
17 changes: 17 additions & 0 deletions crates/polars-plan/src/logical_plan/conversion/dsl_to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,23 @@ pub fn to_alp_impl(
}
}

let mut joined_on = PlHashSet::new();
for (l, r) in left_on.iter().zip(right_on.iter()) {
polars_ensure!(joined_on.insert((l, r)), InvalidOperation: "joins on same keys twice; already joined on {} and {}", l, r)
}
drop(joined_on);
options.args.validation.is_valid_join(&options.args.how)?;

polars_ensure!(
left_on.len() == right_on.len(),
ComputeError:
format!(
"the number of columns given as join key (left: {}, right:{}) should be equal",
left_on.len(),
right_on.len()
)
);

let input_left = to_alp_impl(owned(input_left), expr_arena, lp_arena, convert)
.map_err(|e| e.context(failed_input!(join left)))?;
let input_right = to_alp_impl(owned(input_right), expr_arena, lp_arena, convert)
Expand Down
4 changes: 0 additions & 4 deletions crates/polars/tests/it/core/joins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ fn test_join_err() -> PolarsResult<()> {
assert!(df1
.join(&df2, vec!["a", "b"], vec!["a", "b"], JoinType::Left.into())
.is_err());
// length of join keys don't match error
assert!(df1
.join(&df2, vec!["a"], vec!["a", "b"], JoinType::Left.into())
.is_err());
Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,10 @@ def test_join_empties(how: str) -> None:

df = df1.join(df2, on="col2", how=how)
assert df.height == 0


def test_join_raise_on_redundant_keys() -> None:
left = pl.DataFrame({"a": [1, 2, 3], "b": [3, 4, 5], "c": [5, 6, 7]})
right = pl.DataFrame({"a": [2, 3, 4], "c": [4, 5, 6]})
with pytest.raises(pl.InvalidOperationError, match="already joined on"):
left.join(right, on=["a", "a"], how="outer_coalesce")