This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 00b27eea2f fix(arrow-string): saturate the length in
Utf8View/BinaryView substring (#12006)
00b27eea2f is described below
commit 00b27eea2fe4da951b3f32a86750586ab0363cfb
Author: hsdfat <[email protected]>
AuthorDate: Wed Nov 4 04:04:53 2026 +0000
fix(arrow-string): saturate the length in Utf8View/BinaryView substring
(#12006)
# Which issue does this PR close?
- Follow-up to #10995, which made the other `substring` arms saturate
large lengths. The `Utf8View` / `BinaryView` arms added in #10672 were
missed.
# Rationale for this change
`view_substring_range` computed the end as
`new_start.saturating_add(length as i64)`. For `length > i64::MAX` the
cast wraps negative, so the end lands before the start:
- `substring(&BinaryViewArray["hello"], 1, Some(u64::MAX))` panics with
`slice index starts at 1 but ends at 0`;
- the same call on `StringViewArray` returns `ComputeError("The offset
9223372036854775808 is at an invalid utf-8 boundary.")`.
`LargeUtf8` and `LargeBinary` return `"ello"` for the same input.
# What changes are included in this PR?
Use the existing `u64_to_i64_saturating(length)` in
`view_substring_range`, like the other arms.
# Are these changes tested?
Yes. A new test checks that `Utf8View` matches `LargeUtf8` and
`BinaryView` matches `LargeBinary` for several starts (including
negative and beyond the end) with lengths `2^63` and `u64::MAX`.
# Are there any significant user-facing changes?
No.
---
AI disclosure: the bug was found and the patch and test drafted with AI
assistance (Claude); I reviewed every line, verified the failing test
before/after the fix, and ran fmt/clippy/tests for the crate.
---
arrow-string/src/substring.rs | 41 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 40 insertions(+), 1 deletion(-)
diff --git a/arrow-string/src/substring.rs b/arrow-string/src/substring.rs
index dfc10e099c..3601f7e64f 100644
--- a/arrow-string/src/substring.rs
+++ b/arrow-string/src/substring.rs
@@ -271,7 +271,9 @@ fn view_substring_range(
Ordering::Less => (original_length + start).max(0),
};
let new_end = match substring_length {
- Some(length) => new_start.saturating_add(length as
i64).min(original_length),
+ Some(length) => new_start
+ .saturating_add(u64_to_i64_saturating(length))
+ .min(original_length),
None => original_length,
};
(new_start as usize, new_end as usize)
@@ -1328,4 +1330,41 @@ mod tests {
vec![Some("lo"), Some("ld"), None]
);
}
+
+ #[test]
+ fn view_lengths_beyond_i64_max_match_the_64_bit_arms() {
+ // a length that does not fit in an i64 must saturate, not wrap to a
+ // negative length that puts the end before the start
+ let values = vec![
+ Some("hello"),
+ Some("this one is longer than twelve bytes"),
+ None,
+ ];
+ let large = LargeStringArray::from(values.clone());
+ let view = StringViewArray::from(values.clone());
+ let large_binary =
+ LargeBinaryArray::from_iter(values.iter().map(|v| v.map(|s|
s.as_bytes())));
+ let binary_view =
+ BinaryViewArray::from_iter(values.iter().map(|v| v.map(|s|
s.as_bytes())));
+
+ for start in [0, 1, 3, -2, 100] {
+ for length in [Some(i64::MAX as u64 + 1), Some(u64::MAX)] {
+ let expected = substring(&large, start, length).unwrap();
+ let actual = substring(&view, start, length).unwrap();
+ assert_eq!(
+ expected.as_string::<i64>().iter().collect::<Vec<_>>(),
+ actual.as_string_view().iter().collect::<Vec<_>>(),
+ "LargeUtf8 and Utf8View disagree at start {start}, length
{length:?}"
+ );
+
+ let expected = substring(&large_binary, start,
length).unwrap();
+ let actual = substring(&binary_view, start, length).unwrap();
+ assert_eq!(
+ expected.as_binary::<i64>().iter().collect::<Vec<_>>(),
+ actual.as_binary_view().iter().collect::<Vec<_>>(),
+ "LargeBinary and BinaryView disagree at start {start},
length {length:?}"
+ );
+ }
+ }
+ }
}