fix: time ± interval returns a wrapped time instead of an interval#23279
Open
vismaytiwari wants to merge 1 commit into
Open
fix: time ± interval returns a wrapped time instead of an interval#23279vismaytiwari wants to merge 1 commit into
time ± interval returns a wrapped time instead of an interval#23279vismaytiwari wants to merge 1 commit into
Conversation
`time + interval`, `interval + time`, and `time - interval` previously widened the time operand into an interval, so `time '23:30' + interval '2 hours'` returned a `25 hours 30 mins` interval rather than wrapping within the 24-hour clock. Following the existing `Date - Date` special case, coercion now keeps the result as `time`, and a new `apply_time_interval` kernel adds/subtracts the interval's sub-day component modulo 24 hours to match PostgreSQL and DuckDB. Whole months and days are ignored, and all four time units are supported. Closes apache#22265
0a33c81 to
5b6be8b
Compare
Author
|
Fixed the rustfmt failure from the first run — fmt, the unit tests, and the datetime sqllogictests all pass locally. The workflows just need approval to run on the updated commit. |
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.
Which issue does this PR close?
time + intervalshould wrap within the 24-hour time domain #22265.Rationale for this change
time + interval(andinterval + time/time - interval) returned anIntervalinstead of atime, so the value was never wrapped within the 24-hour clock:PostgreSQL (and DuckDB) return a
timethat wraps around midnight:The root cause is in type coercion:
time <op> intervalwas coerced by widening thetimeoperand into anInterval, so the addition happened between two intervals and the result kept theIntervaltype.What changes are included in this PR?
Following the existing
Date - Datespecial case (in the same two files),time ± intervalis now handled explicitly:expr-common/src/type_coercion/binary.rs):time + interval,interval + time, andtime - intervalnow coerce to(time, interval)with atimeresult type (the interval is normalized toMonthDayNano).interval - time, which is not meaningful, is left unchanged.physical-expr/src/expressions/binary.rs): a newapply_time_intervaladds/subtracts the interval's sub-day component and wraps the result modulo 24 hours. All four time units (Time32(Second|Millisecond),Time64(Microsecond|Nanosecond)) and both operand orders are supported. Only the interval'snanosecondsaffect a time-of-day; whole months and days are ignored, matching PostgreSQL (e.g.time '10:00' + interval '1 day 2 hours'=12:00:00).time - time(→Interval) is unchanged.Are these changes tested?
Yes.
datafusion/sqllogictest/test_files/datetime/arith_time_interval.sltwas previously a characterization test that documented the incorrectIntervaloutput; it now asserts the correct wrappedtimeresults — including wrapping past midnight in both directions (22:00 + 3h → 01:00:00,02:00 - 3h → 23:00:00) and ignoring whole days.Are there any user-facing changes?
Yes —
time ± intervalnow returns atimevalue (wrapped within 24 hours) instead of anInterval, aligning DataFusion with PostgreSQL and DuckDB.