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(rust,python,cli): add SQL support for timestamp precision modifier #13936

Merged
merged 3 commits into from
Jan 24, 2024

Conversation

alexander-beedie
Copy link
Collaborator

Adds recognition of PostgreSQL compatible timestamp precision1 modifier.
Supports values that map to our Datetime time units, specifically 3 → 'ms', 6 → 'us', 9 → 'ns':

Example

from datetime import datetime
import polars as pl

df = pl.DataFrame({
    "ts": [
        datetime(2024, 1, 7, 1, 2, 3, 123456),
        datetime(1999, 1, 1, 23, 59, 59, 555555),
        datetime(2020, 12, 30, 10, 30, 45, 987654),
    ],
})

with pl.SQLContext(frame_data=df, eager_execution=True) as ctx:
    print(ctx.execute(
        f"SELECT ts::timestamp(3) FROM frame_data")
    )
    # shape: (3, 1)
    # ┌─────────────────────────┐
    # │ ts                      │
    # │ ---                     │
    # │ datetime[ms]            │  << precision 3 → 'ms'
    # ╞═════════════════════════╡
    # │ 2024-01-07 01:02:03.123 │
    # │ 1999-01-01 23:59:59.555 │
    # │ 2020-12-30 10:30:45.987 │
    # └─────────────────────────┘

Footnotes

  1. https://www.postgresql.org/docs/current/datatype-datetime.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 Jan 23, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jan 23, 2024
crates/polars-sql/src/sql_expr.rs Outdated Show resolved Hide resolved
match tz {
TimezoneInfo::None => DataType::Datetime(tu, None),
_ => {
polars_bail!(ComputeError: "Timestamp with timezone is not (yet) supported; found tz={}", tz)
Copy link
Member

Choose a reason for hiding this comment

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

idem.

Copy link
Collaborator Author

@alexander-beedie alexander-beedie Jan 24, 2024

Choose a reason for hiding this comment

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

Fixed all of these (and a further 20 or so after searching for them!) 👌

Related; I'll introduce a dedicated PolarsSQLError in a subsequent PR as it'll be useful to distinguish SQL-specific errors from engine/other ones 🤔

@ritchie46 ritchie46 merged commit 7e06238 into pola-rs:main Jan 24, 2024
22 checks passed
@alexander-beedie alexander-beedie deleted the sql-timestamp-precision branch January 24, 2024 08:39
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 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.

2 participants