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 the DELETE statement #21190

Merged
merged 3 commits into from
Feb 12, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Feb 11, 2025

Adds SQL support for the DELETE FROM <table> [WHERE <constraint>] statement.
(Validated edge cases -such as constraints involving null values- against SQLite).

Example

import polars as pl

df = pl.DataFrame({
    "id": [100, 200, 300],
    "dt": [date(2020, 10, 10), date(1999, 1, 2), date(2001, 7, 5)],
    "v1": [3.5, -4.0, None],
    "v2": [10.0, 2.5, -1.5],
})
# ┌─────┬────────────┬──────┬──────┐
# │ id  ┆ dt         ┆ v1   ┆ v2   │
# │ --- ┆ ---        ┆ ---  ┆ ---  │
# │ i64 ┆ date       ┆ f64  ┆ f64  │
# ╞═════╪════════════╪══════╪══════╡
# │ 100 ┆ 2020-10-10 ┆ 3.5  ┆ 10.0 │
# │ 200 ┆ 1999-01-02 ┆ -4.0 ┆ 2.5  │
# │ 300 ┆ 2001-07-05 ┆ null ┆ -1.5 │
# └─────┴────────────┴──────┴──────┘
df.sql("DELETE FROM self WHERE v1 < 0")
# ┌─────┬────────────┬──────┬──────┐
# │ id  ┆ dt         ┆ v1   ┆ v2   │
# │ --- ┆ ---        ┆ ---  ┆ ---  │
# │ i64 ┆ date       ┆ f64  ┆ f64  │
# ╞═════╪════════════╪══════╪══════╡
# │ 100 ┆ 2020-10-10 ┆ 3.5  ┆ 10.0 │
# │ 300 ┆ 2001-07-05 ┆ null ┆ -1.5 │
# └─────┴────────────┴──────┴──────┘
df.sql("DELETE FROM self WHERE EXTRACT(year FROM dt) >= 2000")
# ┌─────┬────────────┬──────┬─────┐
# │ id  ┆ dt         ┆ v1   ┆ v2  │
# │ --- ┆ ---        ┆ ---  ┆ --- │
# │ i64 ┆ date       ┆ f64  ┆ f64 │
# ╞═════╪════════════╪══════╪═════╡
# │ 200 ┆ 1999-01-02 ┆ -4.0 ┆ 2.5 │
# └─────┴────────────┴──────┴─────┘

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

codecov bot commented Feb 11, 2025

Codecov Report

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

Project coverage is 79.88%. Comparing base (c1e493a) to head (3365d20).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-sql/src/context.rs 71.42% 16 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21190      +/-   ##
==========================================
- Coverage   79.88%   79.88%   -0.01%     
==========================================
  Files        1593     1593              
  Lines      227752   227801      +49     
  Branches     2600     2600              
==========================================
+ Hits       181936   181970      +34     
- Misses      45219    45234      +15     
  Partials      597      597              

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

@alexander-beedie alexander-beedie merged commit f69768d into pola-rs:main Feb 12, 2025
28 checks passed
@alexander-beedie alexander-beedie deleted the sql-delete branch February 12, 2025 04:14
braaannigan pushed a commit to braaannigan/polars that referenced this pull request Feb 12, 2025
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.

2 participants