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: Initial SQL support for INTERVAL strings #16732

Merged
merged 1 commit into from
Jun 5, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jun 4, 2024

Closes #16702.

Adds initial INTERVAL '...' support to the SQL interface, covering typical uses (follow-ups listed below).

Handles all the typical units in verbose and abbreviated form, is case-insensitive and whitespace tolerant, and also ignores commas (all of which are valid for PostgreSQL interval literals).

  • Extends our existing Duration object with a new parse_interval option.
  • Also adds "Literal" and "Neg" trait implementations to Duration.

Example

import polars as pl

with pl.SQLContext(df=None, eager=True) as ctx:
    df = ctx.execute(
        """
        SELECT
          INTERVAL '1w2h3m4s' AS i1,
          INTERVAL '100ms, 100us' AS i2,
          INTERVAL '1 quarter 2 months 987 microseconds' AS i3,
        FROM df
        """
    )
    # shape: (1, 3)
    # ┌──────────────┬──────────────┬──────────────┐
    # │ i1           ┆ i2           ┆ i3           │
    # │ ---          ┆ ---          ┆ ---          │
    # │ duration[ns] ┆ duration[ns] ┆ duration[ns] │
    # ╞══════════════╪══════════════╪══════════════╡
    # │ 7d 2h 3m 4s  ┆ 100100µs     ┆ 140d 987µs   │
    # └──────────────┴──────────────┴──────────────┘

Follow-ups

  • Parsing support for negative interval components, eg: '7d -1 h 30m -10s'.
  • Parsing support for the alternative "cast" form '1y6m'::interval.
  • Parsing support for non-constant intervals, eg: INTERVAL col || ' years'.
  • Better Duration behaviour (generally, not SQL-specific) with ops and incorrect supertype conversion (eg: Multiplying or dividing pl.duration converts to float #16514)

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

codecov bot commented Jun 4, 2024

Codecov Report

Attention: Patch coverage is 65.78947% with 39 lines in your changes missing coverage. Please review.

Project coverage is 81.43%. Comparing base (ddf8126) to head (54f9105).

Files Patch % Lines
crates/polars-time/src/windows/duration.rs 64.55% 28 Missing ⚠️
crates/polars-sql/src/sql_expr.rs 60.71% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16732      +/-   ##
==========================================
- Coverage   81.45%   81.43%   -0.03%     
==========================================
  Files        1413     1413              
  Lines      186096   186182      +86     
  Branches     2776     2776              
==========================================
+ Hits       151588   151608      +20     
- Misses      33988    34054      +66     
  Partials      520      520              

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

@ritchie46 ritchie46 merged commit a3f4ad9 into pola-rs:main Jun 5, 2024
32 checks passed
@alexander-beedie alexander-beedie deleted the sql-interval branch June 5, 2024 07:19
Wouittone pushed a commit to Wouittone/polars that referenced this pull request Jun 22, 2024
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.

Support interval expressions in Python SQL Context
2 participants