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 TRUNCATE TABLE command #15513

Merged
merged 1 commit into from Apr 7, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Ref: #7227.

  • Adds SQL support for TRUNCATE <TABLE> xyz1 commands. This is an optimal way to drop all rows from a table while preserving the table itself (so the schema doesn't change, but the table is empty).
  • Adds test coverage for this and most of the other non-query directives (DROP, EXPLAIN, SHOW TABLES).

Example

import polars as pl

df = pl.DataFrame({
    "x": [1, 2, 3],
    "y": ["aaa", "bbb", "ccc"],
    "z": [date(2000, 12, 31), date(1978, 11, 15), date(2077, 10, 20)],
})

with pl.SQLContext(frame=df, eager_execution=True) as ctx:
    df1 = ctx.execute("TRUNCATE TABLE frame")
    df2 = ctx.execute("SELECT * FROM frame")
    
    df1
    # shape: (0, 3)
    # ┌─────┬─────┬──────┐
    # │ x   ┆ y   ┆ z    │
    # │ --- ┆ --- ┆ ---  │
    # │ i64 ┆ str ┆ date │
    # ╞═════╪═════╪══════╡
    # └─────┴─────┴──────┘
    
    df2
    # shape: (0, 3)
    # ┌─────┬─────┬──────┐
    # │ x   ┆ y   ┆ z    │
    # │ --- ┆ --- ┆ ---  │
    # │ i64 ┆ str ┆ date │
    # ╞═════╪═════╪══════╡
    # └─────┴─────┴──────┘

Footnotes

  1. https://www.postgresql.org/docs/current/sql-truncate.html

@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 76.47059% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 81.16%. Comparing base (cc6c642) to head (dd88b86).

Files Patch % Lines
crates/polars-sql/src/context.rs 76.47% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15513      +/-   ##
==========================================
+ Coverage   81.14%   81.16%   +0.01%     
==========================================
  Files        1362     1362              
  Lines      174989   175003      +14     
  Branches     2533     2533              
==========================================
+ Hits       142000   142034      +34     
+ Misses      32505    32485      -20     
  Partials      484      484              

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

@ritchie46 ritchie46 merged commit 4b94d2f into pola-rs:main Apr 7, 2024
27 checks passed
@alexander-beedie alexander-beedie deleted the sql-truncate-table branch April 7, 2024 08:11
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