Support casting strings to durations - #10516
Open
mayank172900 wants to merge 1 commit into
Open
Conversation
mayank172900
marked this pull request as ready for review
August 2, 2026 07:48
mayank172900
force-pushed
the
parse-string-durations
branch
from
August 2, 2026 07:53
ecf088d to
a955790
Compare
Jefffrey
reviewed
Aug 4, 2026
| None, | ||
| ]; | ||
|
|
||
| macro_rules! test_duration { |
Contributor
There was a problem hiding this comment.
this macro can probably be a generic
Comment on lines
+1113
to
+1116
| // Preserve the full i64 range for the common case of a unitless integer. | ||
| if let Ok(value) = value.parse::<i64>() { | ||
| return Ok(value); | ||
| } |
Contributor
There was a problem hiding this comment.
i'm not sure about this behaviour as it does seem a bit implicit 🤔
Comment on lines
+1118
to
+1136
| // Duration display currently emits ISO 8601 values as a number of seconds, | ||
| // for example `PT1.5S` or `-PT1.5S`. Convert this to the interval parser's | ||
| // human-readable syntax so both representations share the same validation. | ||
| let normalized; | ||
| let value = if let Some(seconds) = value | ||
| .strip_prefix("PT") | ||
| .and_then(|value| value.strip_suffix('S')) | ||
| { | ||
| normalized = format!("{seconds} seconds"); | ||
| normalized.as_str() | ||
| } else if let Some(seconds) = value | ||
| .strip_prefix("-PT") | ||
| .and_then(|value| value.strip_suffix('S')) | ||
| { | ||
| normalized = format!("-{seconds} seconds"); | ||
| normalized.as_str() | ||
| } else { | ||
| value | ||
| }; |
Contributor
There was a problem hiding this comment.
Do we need to consider other designators such as hours, minutes, etc.?
also is this possible to parse directly without reconstructing a string to pass through the interval parser?
| ) | ||
| }) { | ||
| return Err(ArrowError::CastError(format!( | ||
| "Cannot cast {value} to {}. Year and month fields are not supported.", |
Contributor
There was a problem hiding this comment.
probably need the error message to be more generic here, as it disallows century, decade, etc.
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
arrow-castcan format durations as strings but does not currently support casting those strings back to duration arrays. This leaves duration casts asymmetric and prevents round-tripping formatted values.What changes are included in this PR?
Utf8,LargeUtf8, andUtf8Viewvalues to cast to all four duration units.Are these changes tested?
Yes. Tests cover all duration units, all supported string representations, safe and unsafe behavior, invalid calendar units, and ISO 8601 and pretty-format round trips.
Validation performed:
cargo test -p arrow-cast --lib(363 passed)cargo clippy -p arrow-cast --lib --tests -- -D warningscargo fmt --all -- --checkAre there any user-facing changes?
Yes. Users can now cast string arrays to duration arrays. This adds functionality without changing an existing public API.
Tooling disclosure
Generative tooling assisted with the parser implementation, cast dispatch arms, tests, and PR wording. The full patch was locally reviewed and verified with the checks above.