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 GREATEST and LEAST #15511

Merged
merged 1 commit into from Apr 6, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Apr 6, 2024

(Lack of support for these functions was mentioned on the Discord).

SQL's GREATEST and LEAST functions map directly onto our max/min horizontal functions. We also already follow PostgreSQL's NULL handling (and compatible supertype) behaviour1 here, so no special adjustments needed.

Example

import polars as pl

df = pl.DataFrame({
    "a": [-100, None, 200, 99],
    "b": [None, -0.1, 99., 9.],
    "c": [50, -10, 125, -1000],
})

with pl.SQLContext(df=df) as ctx:
    df_greatest = ctx.execute(
        """
        SELECT
          GREATEST("a", "b")    AS max_abc,
          GREATEST("a", 0, "b") AS max_abc_zero, 
          LEAST("a", "b")       AS min_abc,
          LEAST("a", 0, "b")    AS min_abc_zero,  
        FROM df
        """
    ).collect()

    # shape: (4, 4)
    # ┌─────────┬──────────────┬─────────┬──────────────┐
    # │ max_abc ┆ max_abc_zero ┆ min_abc ┆ min_abc_zero │
    # │ ---     ┆ ---          ┆ ---     ┆ ---          │
    # │ f64     ┆ f64          ┆ f64     ┆ f64          │
    # ╞═════════╪══════════════╪═════════╪══════════════╡
    # │ -100.0  ┆ 0.0          ┆ -100.0  ┆ -100.0       │
    # │ -0.1    ┆ 0.0          ┆ -0.1    ┆ -0.1         │
    # │ 200.0   ┆ 200.0        ┆ 99.0    ┆ 0.0          │
    # │ 99.0    ┆ 99.0         ┆ 9.0     ┆ 0.0          │
    # └─────────┴──────────────┴─────────┴──────────────┘

Footnotes

  1. https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST

@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 Apr 6, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Apr 6, 2024
Copy link

codecov bot commented Apr 6, 2024

Codecov Report

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

Project coverage is 81.15%. Comparing base (7dfc53e) to head (6eece24).
Report is 3 commits behind head on main.

Files Patch % Lines
crates/polars-sql/src/functions.rs 71.42% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15511   +/-   ##
=======================================
  Coverage   81.15%   81.15%           
=======================================
  Files        1362     1362           
  Lines      174933   174989   +56     
  Branches     2531     2533    +2     
=======================================
+ Hits       141963   142010   +47     
- Misses      32487    32496    +9     
  Partials      483      483           

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

Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

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

The SQL wizard strikes again 😄

@alexander-beedie
Copy link
Collaborator Author

The SQL wizard strikes again 😄

Lol... and I'm not done yet - TRUNCATE support incoming :))

@alexander-beedie alexander-beedie merged commit cc6c642 into pola-rs:main Apr 6, 2024
27 checks passed
@alexander-beedie alexander-beedie deleted the sql-greatest-least branch April 6, 2024 20:48
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.

None yet

2 participants