From 987d9b2fe4de8c5e56fcd107357df66be7c92970 Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Wed, 24 Jul 2024 09:26:59 +0200 Subject: [PATCH] fix: Fix right join schema (#17833) --- crates/polars-plan/src/plans/schema.rs | 2 +- .../tests/unit/operations/test_join_right.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/polars-plan/src/plans/schema.rs b/crates/polars-plan/src/plans/schema.rs index 9b0d0d52eb0a..0cfa854fb520 100644 --- a/crates/polars-plan/src/plans/schema.rs +++ b/crates/polars-plan/src/plans/schema.rs @@ -249,7 +249,7 @@ pub(crate) fn det_join_schema( let mut arena = Arena::with_capacity(8); let mut join_on_left: PlHashSet<_> = PlHashSet::with_capacity(left_on.len()); for e in left_on { - let field = e.to_field_amortized(schema_right, Context::Default, &mut arena)?; + let field = e.to_field_amortized(schema_left, Context::Default, &mut arena)?; join_on_left.insert(field.name); } diff --git a/py-polars/tests/unit/operations/test_join_right.py b/py-polars/tests/unit/operations/test_join_right.py index bd3c74d6cb5a..942717b649d2 100644 --- a/py-polars/tests/unit/operations/test_join_right.py +++ b/py-polars/tests/unit/operations/test_join_right.py @@ -71,3 +71,27 @@ def test_right_join_schemas_multikey() -> None: assert b.join(a, on=["a", "b"], how="right", coalesce=True).to_dict( as_series=False ) == {"c": [1, None, 3], "a": [1, 2, 3], "b": [1, 2, 3], "c_right": [1, 2, 3]} + + +def test_join_right_different_key() -> None: + df = pl.DataFrame( + { + "foo": [1, 2, 3], + "bar": [6.0, 7.0, 8.0], + "ham1": ["a", "b", "c"], + } + ) + other_df = pl.DataFrame( + { + "apple": ["x", "y", "z"], + "ham2": ["a", "b", "d"], + } + ) + assert df.join(other_df, left_on="ham1", right_on="ham2", how="right").to_dict( + as_series=False + ) == { + "foo": [1, 2, None], + "bar": [6.0, 7.0, None], + "apple": ["x", "y", "z"], + "ham2": ["a", "b", "d"], + }