fix: truncate scalar timestamps in their own unit in date_trunc - #25498
Open
namanjain24-sudo wants to merge 1 commit into
Open
namanjain24-sudo wants to merge 1 commit into
namanjain24-sudo wants to merge 1 commit 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 433823 +90
Branches 433733 433823 +90
==========================================
+ Hits 357331 357404 +73
- Misses 54848 54859 +11
- Partials 21554 21560 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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?
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.