Skip to content
Open
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
30 changes: 22 additions & 8 deletions crates/polars-ops/src/frame/join/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,44 @@ pub fn _coalesce_full_join(
suffix: Option<PlSmallStr>,
df_left: &DataFrame,
) -> DataFrame {
// No need to allocate the schema because we already
// know for certain that the column name for left is `name`
// and for right is `name + suffix`
let schema_left = if keys_left == keys_right {
Arc::new(Schema::default())
} else {
df_left.schema().clone()
};

let schema = df.schema().clone();
let mut to_remove = Vec::with_capacity(keys_right.len());

let columns = unsafe { df.columns_mut() };
let suffix = get_suffix(suffix);

let mut index_pairs: Vec<(usize, usize)> = Vec::with_capacity(keys_left.len());
for (l, r) in keys_left.iter().zip(keys_right.iter()) {
let pos_l = schema.get_full(l.as_str()).unwrap().0;
let pos_l = df
.columns()
.iter()
.enumerate()
.find(|(_, col)| col.name() == l.as_str())
.unwrap()
.0;

let r = if l == r || schema_left.contains(r.as_str()) {
_join_suffix_name(r.as_str(), suffix.as_str())
} else {
r.clone()
};
let pos_r = schema.get_full(&r).unwrap().0;
let pos_r = df
.columns()
.iter()
.enumerate()
.skip(pos_l + 1)
.find(|(_, col)| col.name() == r.as_str())
.unwrap()
.0;

index_pairs.push((pos_l, pos_r));
}

let columns = unsafe { df.columns_mut() };
for (pos_l, pos_r) in index_pairs {
let l = columns[pos_l].clone();
let r = columns[pos_r].clone();

Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -4025,3 +4025,20 @@ def test_full_join_rewrite_to_right_with_cast() -> None:
},
)
assert_frame_equal(out, ret, check_column_order=True, check_row_order=False)


def test_full_join_coalesce_empty_suffix_27368() -> None:
df1 = pl.DataFrame({"a": [0, 1], "b": [10, 11]})
df2 = pl.DataFrame({"a": [1, 2], "c": [11, 12]})

result = df1.join(df2, how="full", on="a", coalesce=True, suffix="")

expected = pl.DataFrame(
{
"a": [0, 1, 2],
"b": [10, 11, None],
"c": [None, 11, 12],
}
)

assert_frame_equal(result, expected, check_row_order=False)
Loading