prql icon indicating copy to clipboard operation
prql copied to clipboard

feat: add 'date.to_start_of_interval' function for truncating values to interval boundaries

Open sunyeongchoi opened this issue 8 months ago • 12 comments

Closes https://github.com/PRQL/prql/issues/5734

Adds a to_start_of_interval function to provide native truncating values to interval boundaries capabilities in PRQL.

from events | select (event_time | date.to_start_of_interval 15 minute)

sunyeongchoi avatar Mar 26 '26 05:03 sunyeongchoi

how about using some form of round or floor here?

either literally or at least using the term as part of the name?

max-sixty avatar Mar 26 '26 05:03 max-sixty

@max-sixty Thanks for the feedback. I want to make sure I understood correctly. Are you proposing we use a term like 'floor' in the name (e.g., date.floor_interval) to make the floor logic more explicit?

sunyeongchoi avatar Mar 26 '26 05:03 sunyeongchoi

I want to make sure I understood correctly. Are you proposing we use a term like 'floor' in the name (e.g., date.floor_interval) to make the floor logic more explicit?

yes, something like this!

@prql-bot maybe you could do a survey of what is supported by other languages, around "rounding" to regular intervals

max-sixty avatar Mar 26 '26 17:03 max-sixty

Here's a survey of how other systems name their datetime truncation/rounding-to-interval functions:

SQL Databases

Database Standard Trunc Arbitrary Interval (e.g. 15 min)
PostgreSQL date_trunc('hour', ts) date_bin('15 min', ts, origin)
ClickHouse date_trunc('hour', ts) toStartOfInterval(ts, INTERVAL 15 MINUTE)
DuckDB date_trunc('hour', ts) time_bucket(INTERVAL '15 min', ts) / date_bin(...)
BigQuery TIMESTAMP_TRUNC(ts, HOUR) (manual arithmetic)
Snowflake DATE_TRUNC('hour', ts) TIME_SLICE(ts, 15, 'MINUTE')
Presto/Trino date_trunc('hour', ts) date_bin(INTERVAL '15' MINUTE, ts, origin)
SQL Server DATETRUNC(hour, ts) (manual DATEADD/DATEDIFF)
TimescaleDB date_trunc(...) time_bucket('15 minutes', ts)

Programming Languages / Data Tools

System Function Notes
pandas dt.floor('15min') Also has dt.round(), dt.ceil()
Polars dt.truncate('15m') Also has dt.round()
R (lubridate) floor_date(ts, '15 minutes') Also round_date(), ceiling_date()
Julia Dates.floor(ts, Minute(15)) Also round(), ceil()
Spark date_trunc('hour', ts) window(ts, '15 minutes') for arbitrary

Key takeaways

  1. All systems use floor semantics — truncation/floor is the universal default, not round-to-nearest.
  2. date_trunc is the dominant SQL name but only handles standard calendar units, not arbitrary intervals.
  3. For arbitrary intervals, naming is fragmented: date_bin (PG/Trino), time_bucket (TimescaleDB/DuckDB), toStartOfInterval (ClickHouse), TIME_SLICE (Snowflake).
  4. Programming languages uniformly use floor — and offer round/ceil as companions.
  5. toStartOfInterval is ClickHouse-specific — no other major system uses this naming convention.

Possible PRQL names

Candidate Pros Cons
date.floor Matches pandas/R/Julia; short; natural round/ceil family Less familiar to SQL-only users
date.truncate Matches Polars; close to SQL date_trunc Slightly verbose
date.bin Matches PostgreSQL/Trino date_bin Jargon-y; less intuitive
date.bucket Matches TimescaleDB time_bucket Narrow adoption
date.to_start_of_interval Very descriptive Verbose; ClickHouse-specific

date.floor seems strongest — it's short, widely used in the data ecosystem, and naturally extends to date.round / date.ceil later if needed.

prql-bot avatar Mar 26 '26 17:03 prql-bot

@max-sixty

What do you think of date.floor suggested by prql-bot? if you're okay with it, I'll go ahead and rename it.

from events | select (event_time | date.floor 15 minute)

sunyeongchoi avatar Mar 26 '26 23:03 sunyeongchoi

sorry I missed this! I think the .floor is good, if you happen to see this!

max-sixty avatar May 08 '26 18:05 max-sixty

CI is failing on cargo fmt --check — the date_floor_unsupported_dialects and date_floor_operator blocks in prqlc/prqlc/tests/integration/sql.rs have a few off-by-four-spaces indent issues and a wrapping that rustfmt wants on one line. Running cargo fmt --all locally and committing the result will fix it.

Happy to push the fix if useful — say the word.

prql-bot avatar May 20 '26 04:05 prql-bot

@max-sixty Thanks! As you suggested, I changed the name to date.floor

sunyeongchoi avatar May 20 '26 05:05 sunyeongchoi

@prql-bot looks like we have a lot of insta changes here — could you open a new PR which rewrites our insta snapshots to the latest version, and then hopefully this will become a more managable diff?

max-sixty avatar May 23 '26 18:05 max-sixty

Opened #5943 with that refresh — it normalises @r"…" → @"…" on inline snapshots that don't need raw-string semantics, re-indents the r#"…"# blocks that did, and refreshes a few stale __results__*.snap expression: fields. Once it lands, rebasing this PR onto main should drop the inline-snapshot churn and leave just the real date.floor changes.

For reproducibility, the diff comes from INSTA_FORCE_UPDATE=1 cargo insta test --accept --dnd -p prqlc-parser -p prqlc -p prqlc-python --features=default,test-dbs --test-runner=nextest on main.

prql-bot avatar May 23 '26 18:05 prql-bot

ok now merge and reconcile any other non-essential diffs

max-sixty avatar May 24 '26 00:05 max-sixty

I've tried locally — merging main cleans up the bulk of the insta churn (thanks to #5943), leaving the diff focused on the date.floor changes plus an unavoidable +1 ID shift across debug_lineage snapshots (because floor adds one declaration to std.prql).

After merging, there are four remaining files whose diffs are unrelated to date.floor — they look like stale-snapshot refreshes that crept in from cargo insta accept runs on this branch. Reverting them to match main is safe (I verified cargo test -p mdbook-prql and cargo insta test -p prqlc --test integration -p prqlc --lib still pass without them).

I can't push directly because the PR has "Allow edits by maintainers" off, but here are the commands to apply on top of main:

git fetch origin main
git merge origin/main
git checkout origin/main -- \
  web/book/src/lib.rs \
  web/book/tests/documentation/snapshots/documentation__book__project__target__version__0.snap \
  web/book/tests/documentation/snapshots/documentation__book__reference__stdlib__date__date-functions__4.snap \
  web/book/tests/documentation/snapshots/documentation__book__reference__syntax__pipes__ceci-nest-pas-une-pipe__0.snap
git commit -m "chore: revert non-essential snapshot/doc churn"
What each of those four reverts is
  • web/book/src/lib.rs — test_admonition's inline snapshot was changed from @r#"…"# to @"…" with an extra leading blank line. The legacy form on main still passes (insta just prints a deprecation note).
  • documentation__book__project__target__version__0.snap — expression: field bumped 0.13.4 → 0.13.12. Stale on both, but unrelated to this PR.
  • documentation__book__reference__syntax__pipes__ceci-nest-pas-une-pipe__0.snap — expression: field changed list item → array item. Same situation.
  • documentation__book__reference__stdlib__date__date-functions__4.snap — dropped assertion_line: 75. Same situation.

These all look like incidental refreshes that should land in a separate "refresh stale expression: fields" PR rather than here.

If you'd rather I push, flipping "Allow edits by maintainers" on the PR's right sidebar will let me do that.

prql-bot avatar May 24 '26 01:05 prql-bot