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: Enable joins between compatible differing numeric key columns #20332

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
c
nameexhaustion committed Dec 17, 2024
commit 019c426697e3e282dbda75e27187129146994e5e
6 changes: 5 additions & 1 deletion crates/polars-ops/src/frame/join/hash_join/sort_merge.rs
Original file line number Diff line number Diff line change
@@ -70,13 +70,17 @@ pub(super) fn par_sorted_merge_left(
DataType::Int64 => {
par_sorted_merge_left_impl(s_left.i64().unwrap(), s_right.i64().unwrap())
},
#[cfg(feature = "dtype-i128")]
DataType::Int128 => {
par_sorted_merge_left_impl(s_left.i128().unwrap(), s_right.i128().unwrap())
},
DataType::Float32 => {
par_sorted_merge_left_impl(s_left.f32().unwrap(), s_right.f32().unwrap())
},
DataType::Float64 => {
par_sorted_merge_left_impl(s_left.f64().unwrap(), s_right.f64().unwrap())
},
_ => unreachable!(),
dt => panic!("{:?}", dt),
}
}
#[cfg(feature = "performant")]
65 changes: 41 additions & 24 deletions crates/polars-plan/src/plans/conversion/join.rs
Original file line number Diff line number Diff line change
@@ -76,10 +76,10 @@ pub fn resolve_join(
);
}

let input_left = input_left.map_right(Ok).right_or_else(|input| {
let mut input_left = input_left.map_right(Ok).right_or_else(|input| {
to_alp_impl(owned(input), ctxt).map_err(|e| e.context(failed_here!(join left)))
})?;
let input_right = input_right.map_right(Ok).right_or_else(|input| {
let mut input_right = input_right.map_right(Ok).right_or_else(|input| {
to_alp_impl(owned(input), ctxt).map_err(|e| e.context(failed_here!(join right)))
})?;

@@ -89,8 +89,8 @@ pub fn resolve_join(
let schema = det_join_schema(&schema_left, &schema_right, &left_on, &right_on, &options)
.map_err(|e| e.context(failed_here!(join schema resolving)))?;

let left_on = to_expr_irs_ignore_alias(left_on, ctxt.expr_arena)?;
let right_on = to_expr_irs_ignore_alias(right_on, ctxt.expr_arena)?;
let mut left_on = to_expr_irs_ignore_alias(left_on, ctxt.expr_arena)?;
let mut right_on = to_expr_irs_ignore_alias(right_on, ctxt.expr_arena)?;
let mut joined_on = PlHashSet::new();

#[cfg(feature = "iejoin")]
@@ -131,8 +131,9 @@ pub fn resolve_join(

let mut to_cast_left = vec![];
let mut to_cast_right = vec![];
let mut to_cast_indices = vec![];

for (lnode, rnode) in left_on.iter().zip(right_on.iter()) {
for (i, (lnode, rnode)) in left_on.iter().zip(right_on.iter()).enumerate() {
let ltype = get_dtype!(lnode, &schema_left)?;
let rtype = get_dtype!(rnode, &schema_right)?;

@@ -165,6 +166,8 @@ pub fn resolve_join(
))
};

to_cast_indices.push(i);

continue;
}
}
@@ -192,27 +195,41 @@ pub fn resolve_join(
let schema_left = schema_left.into_owned();
let schema_right = schema_right.into_owned();

let input_left = if to_cast_left.is_empty() {
input_left
} else {
ctxt.lp_arena.add(IR::HStack {
input: input_left,
exprs: to_cast_left,
schema: schema_left,
options: ProjectionOptions::default(),
})
};
let key_cols_coalesced =
options.args.should_coalesce() && matches!(&options.args.how, JoinType::Full);

let input_right = if to_cast_right.is_empty() {
input_right
if key_cols_coalesced {
Copy link
Collaborator Author

@nameexhaustion nameexhaustion Dec 17, 2024

Choose a reason for hiding this comment

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

I've made it so that we maintain the input column types in the output for all cases except for full-join with coalesce=True. Alternatively we could also just always use the supertype in the result.

Copy link
Member

Choose a reason for hiding this comment

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

I think that what you did now is correct. 👍

input_left = if to_cast_left.is_empty() {
input_left
} else {
ctxt.lp_arena.add(IR::HStack {
input: input_left,
exprs: to_cast_left,
schema: schema_left,
options: ProjectionOptions::default(),
})
};

input_right = if to_cast_right.is_empty() {
input_right
} else {
ctxt.lp_arena.add(IR::HStack {
input: input_right,
exprs: to_cast_right,
schema: schema_right,
options: ProjectionOptions::default(),
})
};
} else {
ctxt.lp_arena.add(IR::HStack {
input: input_right,
exprs: to_cast_right,
schema: schema_right,
options: ProjectionOptions::default(),
})
};
for ((i, ir_left), ir_right) in to_cast_indices
.into_iter()
.zip(to_cast_left)
.zip(to_cast_right)
{
left_on[i] = ir_left;
right_on[i] = ir_right;
}
}

let lp = IR::Join {
input_left,
22 changes: 13 additions & 9 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
@@ -1336,30 +1336,34 @@ def test_join_numeric_type_upcast_15338(

assert_frame_equal(
left.join(right, on="a", how="left").collect(),
pl.select(
a=pl.Series([1, 1, 3]).cast(supertype), b=pl.Series(["A", "A", None])
),
pl.select(a=pl.Series([1, 1, 3]).cast(ltype), b=pl.Series(["A", "A", None])),
)

assert_frame_equal(
left.join(right, on="a", how="left", coalesce=False).drop("a_right").collect(),
pl.select(a=pl.Series([1, 1, 3]).cast(ltype), b=pl.Series(["A", "A", None])),
)

assert_frame_equal(
right.join(left, on="a", how="full", coalesce=True).collect(),
left.join(right, on="a", how="full").collect(),
pl.select(
a=pl.Series([1, 1, 3]).cast(supertype), b=pl.Series(["A", "A", None])
a=pl.Series([1, 1, 3]).cast(ltype),
a_right=pl.Series([1, 1, None]).cast(rtype),
b=pl.Series(["A", "A", None]),
),
)

assert_frame_equal(
right.join(left, on="a", how="full").collect(),
left.join(right, on="a", how="full", coalesce=True).collect(),
pl.select(
a=pl.Series([1, 1, None]).cast(supertype),
a=pl.Series([1, 1, 3]).cast(supertype),
b=pl.Series(["A", "A", None]),
a_right=pl.Series([1, 1, 3]).cast(supertype),
),
)

assert_frame_equal(
left.join(right, on="a", how="semi").collect(),
pl.select(a=pl.Series([1, 1]).cast(supertype)),
pl.select(a=pl.Series([1, 1]).cast(ltype)),
)