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: binary agg should group aware if literal not a scalar #12043

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions crates/polars-lazy/src/physical_plan/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,15 @@ impl PhysicalExpr for BinaryExpr {
let ac_r = result_b?;

match (ac_l.agg_state(), ac_r.agg_state()) {
(
AggState::Literal(_) | AggState::NotAggregated(_),
AggState::Literal(_) | AggState::NotAggregated(_),
) => self.apply_elementwise(ac_l, ac_r, false),
(AggState::Literal(s), AggState::NotAggregated(_))
| (AggState::NotAggregated(_), AggState::Literal(s)) => match s.len() {
1 => self.apply_elementwise(ac_l, ac_r, false),
reswqa marked this conversation as resolved.
Show resolved Hide resolved
_ => self.apply_group_aware(ac_l, ac_r),
},
(AggState::Literal(_), AggState::Literal(_)) => self.apply_group_aware(ac_l, ac_r),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure about this(all literal), but just think they should return the same values for every group. 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Can we do something smart here? Compute only once and then expand as list over the groups.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's reasonable. I updated the code But not sure if this is what you want.

(AggState::NotAggregated(_), AggState::NotAggregated(_)) => {
self.apply_elementwise(ac_l, ac_r, false)
},
(
AggState::AggregatedScalar(_) | AggState::Literal(_),
AggState::AggregatedScalar(_) | AggState::Literal(_),
Expand Down
23 changes: 23 additions & 0 deletions py-polars/tests/unit/operations/test_group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,29 @@ def test_group_by_when_then_with_binary_and_agg_in_pred_6202() -> None:
}


def test_group_by_binary_agg_with_literal() -> None:
df = pl.DataFrame({"id": ["a", "a", "b", "b"], "value": [1, 2, 3, 4]})

out = df.group_by("id", maintain_order=True).agg(
pl.col("value") + pl.Series([1, 3])
)
assert out.to_dict(False) == {"id": ["a", "b"], "value": [[2, 5], [4, 7]]}

out = df.group_by("id", maintain_order=True).agg(pl.col("value") + pl.lit(1))
assert out.to_dict(False) == {"id": ["a", "b"], "value": [[2, 3], [4, 5]]}

out = df.group_by("id", maintain_order=True).agg(pl.lit(1) + pl.lit(2))
assert out.to_dict(False) == {"id": ["a", "b"], "literal": [3, 3]}

out = df.group_by("id", maintain_order=True).agg(pl.lit(1) + pl.Series([2, 3]))
assert out.to_dict(False) == {"id": ["a", "b"], "literal": [[3, 4], [3, 4]]}

out = df.group_by("id", maintain_order=True).agg(
value=pl.lit(pl.Series([1, 2])) + pl.lit(pl.Series([3, 4]))
)
assert out.to_dict(False) == {"id": ["a", "b"], "value": [[4, 6], [4, 6]]}


@pytest.mark.slow()
@pytest.mark.parametrize("dtype", [pl.Int32, pl.UInt32])
def test_overflow_mean_partitioned_group_by_5194(dtype: pl.PolarsDataType) -> None:
Expand Down