Repository navigation
feat(pg-functions): add justify_hours, justify_days and justify_interval - #435
Merged
sunng87 merged 1 commit intoOct 14, 2026
Merged
Conversation
Implement the PostgreSQL interval justification functions in the
datetime category, ported from interval_justify_{hours,days,interval} in
PostgreSQL's timestamp.c.
- Accept Interval(MonthDayNano), Interval(DayTime) and Interval(YearMonth)
and always return Interval(MonthDayNano).
- Raise "interval out of range" on month/day overflow, as PostgreSQL does.
- Keep Arrow's nanosecond precision and justify it exactly.
- Register the UDFs under the `datetime` feature, mark them implemented in
functions.md, and add datetime.slt (`datetime` is now a required feature
of the sqllogictest target).
Refs datafusion-contrib#434
sunng87
approved these changes
Oct 14, 2026
Member
|
Thank you @Ishaan400 |
Contributor
Author
|
Thanks for merging! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #434
Adds the PostgreSQL interval justification functions to the
datetimecategory ofdatafusion-pg-functions:justify_hours(interval)justify_days(interval)justify_interval(interval)Implementation
Each function is a direct port of
interval_justify_{hours,days,interval}from PostgreSQL'ssrc/backend/utils/adt/timestamp.c:Behavior for finite values is the same in PostgreSQL 16 through 18.
justify_intervalfollows PostgreSQL's own steps rather than callingjustify_days(justify_hours(x)). PostgreSQL moves days into months early when days and time share a sign. It also borrows a month when days are zero and time has the opposite sign, so1 mon -00:00:00.000001becomes29 days 23:59:59.999999.Code layout:
src/datetime/;src/datetime/interval.rsholding the shared signature, Arrow array iteration and constants;datetime::registerbehind thedatetimefeature.functions.mdmarks the three functions 🔧 and updates the Date/Time and TOTAL counts.Design choices (agreed in #434)
Interval(MonthDayNano),Interval(DayTime)andInterval(YearMonth)are accepted and converted losslessly. The result is alwaysInterval(MonthDayNano), matching PostgreSQL's singleintervaltype.interval out of range, as PostgreSQL does.datetimeis added to the sqllogictest target'srequired-features, along with a newdatetime.slt. The PostgreSQL 16.15 result is noted above each case.Sub-microsecond precision
MonthDayNanokeeps nanoseconds, and DataFusion can produce sub-µs values (e.g.INTERVAL '1 day -1 nanosecond').justify_hours(INTERVAL '1 day -1 nanosecond')returns23:59:59.999999999(clients see23:59:59.999999because arrow-pg truncates to µs), where PostgreSQL returns1 day.datetime.sltcases.Tests
Run locally on this branch (based on
master@ 4776015, DataFusion 55.2.0):cargo fmt -- --checkcargo clippy --features datafusion-pg-functions/full -- -D warningscargo clippy --all-features -- -D warningscargo clippy -p datafusion-pg-functions --features full --all-targets -- -D warningscargo testcargo test --features datafusion-pg-functions/fulldatetime.slt)cargo test --all-featuresI also compared months, days and microseconds field by field against a PostgreSQL 16.15 server. Results were identical for:
justify_days/justify_intervaloverflow errors;The integration suites that need nix and podman were not run locally.
Known limitations
Interval(DayTime)andInterval(YearMonth)can't be produced from SQL literals in DataFusion. Those inputs are covered by unit tests.Out of scope
datetimeindatafusion-postgres's default features. The server currently enables onlymath, so these functions aren't yet reachable through it.intervalvalues are encoded as00:00:00by arrow-pg.XX000.