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

fix(rust, python): do projection just before concat to ensure same sizes #7089

Merged
merged 1 commit into from
Feb 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ pub(super) fn process_generic(
let inputs = lp.get_inputs();
let exprs = lp.get_exprs();

let mut first_schema = None;
let mut names = None;
// let mut first_schema = None;
// let mut names = None;

let new_inputs = inputs
.iter()
.enumerate()
.map(|(i, &node)| {
.map(|&node| {
let alp = lp_arena.take(node);
let mut alp = proj_pd.push_down(
alp,
Expand All @@ -32,27 +31,23 @@ pub(super) fn process_generic(

// double projection can mess up the schema ordering
// here we ensure the ordering is maintained.
//
// Consider this query
// df1 => a, b
// df2 => a, b
//
// df3 = df1.join(df2, on = a, b)
//
// concat([df1, df3]).select(a)
//
// schema after projection pd
// df3 => a, b
// df1 => a
// so we ensure we do the 'a' projection again before we concatenate
if !acc_projections.is_empty() && inputs.len() > 1 {
let schema = alp.schema(lp_arena);
if i == 0 {
first_schema = Some(schema.into_owned());
} else if first_schema.as_ref().unwrap() != schema.as_ref() {
if names.is_none() {
names = Some(
first_schema
.as_ref()
.unwrap()
.iter()
.map(|(name, _)| {
expr_arena.add(AExpr::Column(Arc::from(name.as_str())))
})
.collect::<Vec<_>>(),
);
}
alp = ALogicalPlanBuilder::from_lp(alp, expr_arena, lp_arena)
.project(names.as_ref().unwrap().clone())
.build()
}
alp = ALogicalPlanBuilder::from_lp(alp, expr_arena, lp_arena)
.project(acc_projections.clone())
.build()
}
lp_arena.replace(node, alp);
Ok(node)
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/operations/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,15 @@ def test_join_frame_consistency() -> None:
_ = ldf.join_asof(df, on="A")
with pytest.raises(TypeError, match="Expected 'other'.* DataFrame"):
_ = df.join_asof(ldf, on="A")


def test_join_concat_projection_pd_case_7071() -> None:
ldf = pl.DataFrame({"id": [1, 2], "value": [100, 200]}).lazy()
ldf2 = pl.DataFrame({"id": [1, 3], "value": [100, 300]}).lazy()

ldf = ldf.join(ldf2, on=["id", "value"])
ldf = pl.concat([ldf, ldf2])
result = ldf.select("id")

expected = pl.DataFrame({"id": [1, 1, 3]}).lazy()
assert_frame_equal(result, expected)