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: Add SQL support for GROUP BY ALL syntax and fix several issues with aliased group keys #16179

Merged
merged 4 commits into from
May 13, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 12, 2024

Closes #15585 (ref: #14704 (comment))

Feature

GROUP BY ALL is a useful SQL construct (as implemented by BigQuery, DuckDB, Snowflake, etc) that allows for automatic grouping by column expressions in the SELECT clause that are not (and do not contain) aggregate/window functions.

from datetime import date
import polars as pl

dt1 = date(1999, 12, 31)
dt2 = date(2028, 7, 5)

df = pl.DataFrame({
    "key": ["xx", "yy", "xx", "yy", "xx", "xx"],
    "dt": [dt1, dt1, dt1, dt2, dt2, dt2],
    "value": [10.5, -5.5, 20.5, 8.0, -3.0, 5.0],
})

df.sql(
    """
    SELECT dt, key, SUM(value) AS total,
    FROM self
    GROUP BY ALL
    ORDER BY dt, key
    """
)
# shape: (4, 3)
# ┌────────────┬─────┬───────┐
# │ dt         ┆ key ┆ total │
# │ ---        ┆ --- ┆ ---   │
# │ date       ┆ str ┆ f64   │
# ╞════════════╪═════╪═══════╡
# │ 1999-12-31 ┆ xx  ┆ 31.0  │
# │ 1999-12-31 ┆ yy  ┆ -5.5  │
# │ 2028-07-05 ┆ xx  ┆ 2.0   │
# │ 2028-07-05 ┆ yy  ┆ 8.0   │
# └────────────┴─────┴───────┘

Fixes

  • While implementing this I came across several errors with group-key aliasing/tracking, resolved them, and added test coverage. I'm aware of one more similar gremlin, but it's very much an edge-case and I'll address it in a subsequent PR.
  • Use of ARRAY_AGG in conjunction with GROUP BY could result in a double-implode, due to our implicit array aggregation on bare agg columns in the regular API.
  • Attempting to group by ordinal values should have raised on negative integers - and now it does.

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels May 12, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label May 12, 2024
Copy link

codecov bot commented May 12, 2024

Codecov Report

Attention: Patch coverage is 95.50562% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 80.99%. Comparing base (3892c75) to head (b3231ea).
Report is 3 commits behind head on main.

Files Patch % Lines
crates/polars-sql/src/context.rs 94.66% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16179   +/-   ##
=======================================
  Coverage   80.99%   80.99%           
=======================================
  Files        1387     1392    +5     
  Lines      178832   178920   +88     
  Branches     2877     2892   +15     
=======================================
+ Hits       144844   144923   +79     
- Misses      33496    33500    +4     
- Partials      492      497    +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

crates/polars-plan/src/dsl/expr.rs Show resolved Hide resolved
crates/polars-sql/src/context.rs Show resolved Hide resolved
crates/polars-sql/src/context.rs Show resolved Hide resolved
crates/polars-sql/src/context.rs Show resolved Hide resolved
crates/polars-sql/src/context.rs Show resolved Hide resolved
crates/polars-sql/src/context.rs Show resolved Hide resolved
@ritchie46 ritchie46 merged commit db18aa9 into pola-rs:main May 13, 2024
26 checks passed
@alexander-beedie alexander-beedie deleted the sql-group-by-all branch May 13, 2024 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add GROUP BY ALL to the SQL engine
2 participants