Skip to content

Move literal group_by keys out of the grouping #22623

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

Open
orlp opened this issue May 6, 2025 · 0 comments
Open

Move literal group_by keys out of the grouping #22623

orlp opened this issue May 6, 2025 · 0 comments
Labels
A-optimizer Area: plan optimization accepted Ready for implementation P-medium Priority: medium

Comments

@orlp
Copy link
Member

orlp commented May 6, 2025

If a group_by key is a literal it could be removed from the grouping. For example, before:

>>> df = pl.DataFrame({"x": [1, 2, 3], "g": [1, 1, 2]})
>>> df.group_by([pl.lit(1), "g"]).agg(pl.col.x.sum())
shape: (2, 3)
┌─────────┬─────┬─────┐
│ literalgx   │
│ --------- │
│ i32i64i64 │
╞═════════╪═════╪═════╡
│ 113   │
│ 123   │
└─────────┴─────┴─────┘

After:

>>> df = pl.DataFrame({"x": [1, 2, 3], "g": [1, 1, 2]})
>>> df.group_by("g").agg(pl.col.x.sum()).select(pl.lit(1), pl.col.g, pl.col.x)
shape: (2, 3)
┌─────────┬─────┬─────┐
│ literalgx   │
│ --------- │
│ i32i64i64 │
╞═════════╪═════╪═════╡
│ 113   │
│ 123   │
└─────────┴─────┴─────┘

In the event that the literal is the only grouping key, the entire .agg should be rewritten to a select.

@orlp orlp added accepted Ready for implementation P-medium Priority: medium A-optimizer Area: plan optimization labels May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-optimizer Area: plan optimization accepted Ready for implementation P-medium Priority: medium
Projects
None yet
Development

No branches or pull requests

1 participant