feat: add 'date.to_start_of_interval' function for truncating values to interval boundaries
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)
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 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?
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
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
- All systems use floor semantics — truncation/floor is the universal default, not round-to-nearest.
date_truncis the dominant SQL name but only handles standard calendar units, not arbitrary intervals.- For arbitrary intervals, naming is fragmented:
date_bin(PG/Trino),time_bucket(TimescaleDB/DuckDB),toStartOfInterval(ClickHouse),TIME_SLICE(Snowflake). - Programming languages uniformly use
floor— and offerround/ceilas companions. toStartOfIntervalis 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.
@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)
sorry I missed this! I think the .floor is good, if you happen to see this!
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.
@max-sixty Thanks! As you suggested, I changed the name to date.floor
@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?
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.
ok now merge and reconcile any other non-essential diffs
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 onmainstill passes (insta just prints a deprecation note).documentation__book__project__target__version__0.snap—expression:field bumped0.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 changedlist item→array item. Same situation.documentation__book__reference__stdlib__date__date-functions__4.snap— droppedassertion_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.