fix: truncate scalar timestamps in their own unit in date_trunc - #25498
namanjain24-sudo wants to merge 2 commits into
Conversation
A scalar timestamp was always converted to nanoseconds before truncating, so a Timestamp(Second), Timestamp(Millisecond) or Timestamp(Microsecond) value outside the nanosecond range failed with "out of range", while the same value in a column was truncated in its own unit and succeeded. The scalar path now takes the same route as the array path for the granularities that are plain arithmetic in the input's unit. The unit table and the checked truncation are shared between both paths.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25498 +/- ##
==========================================
- Coverage 82.38% 82.38% -0.01%
==========================================
Files 1138 1138
Lines 433733 434398 +665
Branches 433733 434398 +665
==========================================
+ Hits 357331 357872 +541
- Misses 54848 54889 +41
- Partials 21554 21637 +83 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| }) | ||
| })? | ||
| array | ||
| .try_unary(|value| date_trunc_fine_granularity(tu, value, granularity))? |
There was a problem hiding this comment.
IIUC we above do some work to turn tu into unit via fine_granularity_unit and then do the same computation again inside of this function date_trunc_fine_granularity. That feels like:
- duplicated work, and
- a potential source of confusion while reading the code
I think it is worth coming up with a simplification. What you think?
There was a problem hiding this comment.
Good catch, agreed. In 5c830eb the slow branch now passes the unit it already has to a small truncate_to_unit(value, unit, granularity), and the scalar path looks the unit up once, so date_trunc_fine_granularity is gone.
There was a problem hiding this comment.
worth seeing if there is a regression. Have you tried the date_trunc_minute_1000 benchmark?
There was a problem hiding this comment.
Ran it against the branch's base (ccfe704) on the same machine, together with the other date_trunc benches: date_trunc_minute_1000 went from 1.246 µs to 1.232 µs, and the rest are within noise. That matches the change, since the array hot loop is the same and only the unit table moved into a helper.
| ); | ||
| } | ||
|
|
||
| // The issue's example: `to_timestamp_seconds(10000000000)` |
There was a problem hiding this comment.
This isn't going to make sense as a comment once this is merged. I would either include the issue number for future reference or get rid of the mention of the issue, whatever is the standard in this repo (I'm not sure). Thanks!
There was a problem hiding this comment.
Fair point. I reworded it to say what it checks and added the issue link, the same way other comments under datetime/ link theirs.
…t comment The array path's slow branch already has the unit in hand, so the checked truncation now takes it instead of looking it up again for each value, and the scalar path looks it up once. The test comment now says what it checks and links the issue.
Which issue does this PR close?
Rationale for this change
process_scalaralways went throughgeneral_date_trunc, which converts the value to nanoseconds before truncating. ATimestamp(Second),Timestamp(Millisecond)orTimestamp(Microsecond)value outside the nanosecond range (after 2262 or before 1677) therefore failed withTimestamp ... out of range.process_arraydoes not convert for the fine granularities: whengranularity.is_fine_granularity(), or the value has no timezone and the granularity ishourorday, it truncates in the input's own unit with plain arithmetic. So the same value succeeded in a column and failed as a scalar:What changes are included in this PR?
The scalar path now takes the same route as the array path:
truncates_in_input_unitis the conditionprocess_arrayalready used, now shared by both paths.fine_granularity_unitis the unit table fromgeneral_date_trunc_array_fine_granularity, moved out so both paths read it.date_trunc_fine_granularitytruncates one value in its own unit, with the checked subtraction and error message the array path already used for values neari64::MIN. The array path's slow branch now calls it too.For those granularities, a scalar and a one-row column now run the same function, so they accept the same values and give the same result. Everything else (
week,month,quarter,year, andhour/daywith a timezone) still goes throughgeneral_date_truncon both paths, so it is unchanged and still limited to the nanosecond range for both scalars and columns.One existing test changes.
date_trunc('hour', arrow_cast(9223372036854775807, 'Timestamp(Second, None)'))was expected to fail withTimestamp 9223372036854775807 out of range. That test comes from #22262, whose goal was an error instead of a panic, and a column holding that value already succeeds. As a scalar it now succeeds too, so the test usesweek, which still converts to nanoseconds on both paths and so still covers the error.Are these changes tested?
scalar_and_array_accept_timestamps_beyond_nanosecond_rangeevaluatesdate_truncon a scalar and on a one-row column with the same value, for all three coarser-than-nanosecond units, with and without a timezone, and for values before 1677 and after 2262, and checks that both succeed with the same result.datetime/timestamps.sltcover the issue's scalar and column queries, plus millisecond and microsecond scalars.Timestamp 10000000000 out of range, etc.), while the column query passes as it does onmain.cargo test -p datafusion-functions(384 passed), the full sqllogictest suite (520 files),cargo fmtandcargo clippy -p datafusion-functions --all-targets --all-features -- -D warningspass.Are there any user-facing changes?
date_truncon a scalar timestamp outside the nanosecond range now returns the same result as on a column, instead of an out-of-range error, formicrosecond,millisecond,secondandminute, and forhouranddaywithout a timezone. No API changes.