-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: time ± interval returns a wrapped time instead of an interval
#23279
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
Open
vismaytiwari
wants to merge
3
commits into
apache:main
Choose a base branch
from
vismaytiwari:time-interval-returns-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−26
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
84 changes: 58 additions & 26 deletions
84
datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,102 @@ | ||
| # postgresql behavior | ||
| # | ||
| # time + interval → time | ||
| # Add an interval to a time | ||
| # Add an interval to a time. The result is a `time` value that wraps within the | ||
| # 24-hour clock, matching PostgreSQL and DuckDB. | ||
| # time '01:00' + interval '3 hours' → 04:00:00 | ||
| # | ||
| # note that while the above reflects what postgresql does | ||
| # in the case of datafusion/arrow that is not the case. The | ||
| # result will be an interval, not a time. | ||
| # time '22:00' + interval '3 hours' → 01:00:00 (wraps past midnight) | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT '01:00'::time + interval '3 hours' | ||
| ---- | ||
| 4 hours | ||
| 04:00:00 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof('01:00'::time + interval '3 hours') | ||
| ---- | ||
| Interval(MonthDayNano) | ||
| Time64(ns) | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT '22:00'::time + interval '3 hours' | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT interval '3 hours' + '22:00'::time | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT arrow_cast('22:00', 'Time32(Second)') + interval '3 hours' | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| # Regardless of the input time unit, the result is normalized to Time64(ns). | ||
| query T | ||
| SELECT arrow_typeof(arrow_cast('22:00', 'Time32(Second)') + interval '3 hours') | ||
| ---- | ||
| Time64(ns) | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT arrow_cast('22:00', 'Time32(Millisecond)') + interval '3 hours' | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT arrow_cast('22:00', 'Time64(Microsecond)') + interval '3 hours' | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT arrow_cast('22:00', 'Time64(Nanosecond)') + interval '3 hours' | ||
| ---- | ||
| 25 hours | ||
| 01:00:00 | ||
|
|
||
| # Whole days and months in the interval do not affect a time-of-day (PostgreSQL). | ||
| query D | ||
| SELECT '10:00'::time + interval '1 day 2 hours' | ||
| ---- | ||
| 12:00:00 | ||
|
|
||
| # postgresql behavior | ||
| # | ||
| # time - interval → time | ||
| # Subtract an interval from a time | ||
| # Subtract an interval from a time, wrapping within the 24-hour clock. | ||
| # time '05:00' - interval '2 hours' → 03:00:00 | ||
| # time '02:00' - interval '3 hours' → 23:00:00 (wraps before midnight) | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT '05:00'::time - interval '2 hours' | ||
| ---- | ||
| 3 hours | ||
| 03:00:00 | ||
|
|
||
| query T | ||
| SELECT arrow_typeof('05:00'::time - interval '2 hours') | ||
| ---- | ||
| Interval(MonthDayNano) | ||
| Time64(ns) | ||
|
|
||
| query ? | ||
| query D | ||
| SELECT '02:00'::time - interval '3 hours' | ||
| ---- | ||
| -1 hours | ||
| 23:00:00 | ||
|
|
||
| # Array inputs (not only scalars) exercise the columnar path, including nulls. | ||
| statement ok | ||
| CREATE TABLE time_vals(id INT, t TIME) AS VALUES (1, '01:00'::time), (2, '22:00'::time), (3, NULL); | ||
|
|
||
| query D | ||
| SELECT t + interval '3 hours' FROM time_vals ORDER BY id | ||
| ---- | ||
| 04:00:00 | ||
| 01:00:00 | ||
| NULL | ||
|
|
||
| query D | ||
| SELECT t - interval '2 hours' FROM time_vals ORDER BY id | ||
| ---- | ||
| 23:00:00 | ||
| 20:00:00 | ||
| NULL | ||
|
|
||
| statement ok | ||
| DROP TABLE time_vals |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have some optimization opportunity here to operate directly on columnarvalue instead of converting to arrays; see
apply_date_subtractionfor referenceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — reworked it to match on
ColumnarValuedirectly (followingapply_date_subtraction), so the scalar paths no longer materialize a full array. Thanks for the pointer.