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: Allow implicit string → temporal conversion in SQL comparisons #15958

Merged

Conversation

alexander-beedie
Copy link
Collaborator

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

Closes #15956.

Where available, the internal active schema is now associated with SQLExprVisitor. This provides sufficient additional context for the binary comparison ops (and "IN" membership checks) to enable automatic temporal string casts (which PostgreSQL syntax allows).

Handles comparison with bare column names (eg: col > '2020-02-20') and cast expressions (eg: expr::date > '2020-02-20'), which catches most typical usage. We should be able to enhance further as needed.

Also:

Added col::datetime cast support (in addition to existing col::timestamp).

Example

import polars as pl

df = pl.DataFrame({
    "idx": [0, 1, 2],
    "dtm": [
        datetime(2024, 1, 7, 1, 2, 3, 123456),
        datetime(2006, 1, 1, 23, 59, 59, 555555),
        datetime(2020, 12, 30, 10, 30, 45, 987654),
    ],
    "dt": [
        date(2020, 12, 30),
        date(2077, 1, 1),
        date(1960, 1, 7),
    ],
})
df.sql("SELECT * FROM self WHERE dtm < '2010-01-01 01:02:03'")
# shape: (1, 3)
# ┌─────┬────────────────────────────┬────────────┐
# │ idx ┆ dtm                        ┆ dt         │
# │ --- ┆ ---                        ┆ ---        │
# │ i64 ┆ datetime[μs]               ┆ date       │
# ╞═════╪════════════════════════════╪════════════╡
# │ 1   ┆ 2006-01-01 23:59:59.555555 ┆ 2077-01-01 │
# └─────┴────────────────────────────┴────────────┘
df.sql("SELECT * FROM self WHERE dt IN ('1960-01-07','2077-01-01','2222-02-22')")
# shape: (2, 3)
# ┌─────┬────────────────────────────┬────────────┐
# │ idx ┆ dtm                        ┆ dt         │
# │ --- ┆ ---                        ┆ ---        │
# │ i64 ┆ datetime[μs]               ┆ date       │
# ╞═════╪════════════════════════════╪════════════╡
# │ 1   ┆ 2006-01-01 23:59:59.555555 ┆ 2077-01-01 │
# │ 2   ┆ 2020-12-30 10:30:45.987654 ┆ 1960-01-07 │
# └─────┴────────────────────────────┴────────────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Apr 29, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Apr 29, 2024
@alexander-beedie alexander-beedie marked this pull request as draft April 29, 2024 22:09
@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Apr 29, 2024

Need to check/fix a few tests in the morning; parking in Draft in the meantime…

Update: done.

@alexander-beedie alexander-beedie marked this pull request as ready for review April 30, 2024 11:49
@eitsupi
Copy link
Contributor

eitsupi commented Apr 30, 2024

I suppose this is not a specific to python? (feat: instead of feat(python):)

@alexander-beedie alexander-beedie changed the title feat(python): Allow implicit string → temporal conversion in SQL comparisons feat: Allow implicit string → temporal conversion in SQL comparisons Apr 30, 2024
@github-actions github-actions bot added the rust Related to Rust Polars label Apr 30, 2024
@alexander-beedie
Copy link
Collaborator Author

I suppose this is not a specific to python? (feat: instead of feat(python):)

Quite right; brain clearly on autopilot there - fixed ;)

@alexander-beedie alexander-beedie force-pushed the sql-implicit-temporal-strings branch 2 times, most recently from 2730afc to d2271db Compare April 30, 2024 12:13
Copy link

codspeed-hq bot commented Apr 30, 2024

CodSpeed Performance Report

Merging #15958 will not alter performance

Comparing alexander-beedie:sql-implicit-temporal-strings (86e7c7e) with main (c5489ac)

Summary

✅ 35 untouched benchmarks

@alexander-beedie alexander-beedie force-pushed the sql-implicit-temporal-strings branch 2 times, most recently from 2e03128 to 65f27e2 Compare April 30, 2024 13:15
Copy link

codecov bot commented Apr 30, 2024

Codecov Report

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

Project coverage is 80.92%. Comparing base (c5489ac) to head (7af4bfe).
Report is 2 commits behind head on main.

❗ Current head 7af4bfe differs from pull request most recent head 86e7c7e. Consider uploading reports for the commit 86e7c7e to get more accurate results

Files Patch % Lines
crates/polars-sql/src/sql_expr.rs 93.65% 4 Missing ⚠️
crates/polars-sql/src/functions.rs 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15958      +/-   ##
==========================================
- Coverage   80.98%   80.92%   -0.06%     
==========================================
  Files        1386     1385       -1     
  Lines      178479   178277     -202     
  Branches     2877     3050     +173     
==========================================
- Hits       144539   144276     -263     
- Misses      33448    33514      +66     
+ Partials      492      487       -5     
Flag Coverage Δ
python 74.41% <87.05%> (?)
rust 78.14% <94.11%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Nice one @alexander-beedie. I've left some comments.

crates/polars-sql/Cargo.toml Show resolved Hide resolved
crates/polars-sql/src/sql_expr.rs Outdated Show resolved Hide resolved
crates/polars-sql/src/sql_expr.rs Outdated Show resolved Hide resolved
crates/polars-sql/src/sql_expr.rs Outdated Show resolved Hide resolved
@ritchie46 ritchie46 merged commit 21b3d43 into pola-rs:main May 10, 2024
26 checks passed
@alexander-beedie alexander-beedie deleted the sql-implicit-temporal-strings branch May 11, 2024 08:24
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.

Filtering dates in SQL raises a ComputeError
3 participants