This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-23424-f87e8174c107f61740606fa8c38b8f446911a1e8 in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 13a6e3003f6ff1540a018fdfca2c9283fd7e51b9 Author: Raz Luvaton <[email protected]> AuthorDate: Mon Jul 13 13:40:04 2026 +0300 chore: use new `OffsetBuffer::subtract` helper (#23424) ## Which issue does this PR close? N/A ## Rationale for this change Replace manually written code with the newly added helper I added to arrow-rs in: - https://github.com/apache/arrow-rs/pull/10120 ## What changes are included in this PR? use `OffsetBuffer::subtract` ## Are these changes tested? Existing tests ## Are there any user-facing changes? No --- datafusion/common/src/utils/mod.rs | 11 +---------- datafusion/functions-nested/src/sort.rs | 7 +------ datafusion/functions/src/string/common.rs | 15 +++++---------- datafusion/functions/src/unicode/initcap.rs | 17 +++++------------ 4 files changed, 12 insertions(+), 38 deletions(-) diff --git a/datafusion/common/src/utils/mod.rs b/datafusion/common/src/utils/mod.rs index f71cf23d53..94bbb91a7f 100644 --- a/datafusion/common/src/utils/mod.rs +++ b/datafusion/common/src/utils/mod.rs @@ -1236,16 +1236,7 @@ pub fn adjust_offsets_for_slice<O: OffsetSizeTrait>( ) -> OffsetBuffer<O> { let offsets = list.offsets(); - if let (Some(first), Some(last)) = (offsets.first(), offsets.last()) - && (!first.is_zero() || last.as_usize() != list.values().len()) - { - let offsets = offsets.iter().map(|offset| *offset - *first).collect(); - - //todo: use unsafe Offset::new_unchecked? - return OffsetBuffer::new(offsets); - } - - offsets.clone() + offsets.clone().subtract(offsets[0]) } /// For lists and large lists, truncates the sublist of null values diff --git a/datafusion/functions-nested/src/sort.rs b/datafusion/functions-nested/src/sort.rs index 0a34cce6b9..ca9267bb88 100644 --- a/datafusion/functions-nested/src/sort.rs +++ b/datafusion/functions-nested/src/sort.rs @@ -471,12 +471,7 @@ fn take_by_indices<OffsetSize: OffsetSizeTrait>( fn rebase_offsets<OffsetSize: OffsetSizeTrait>( offsets: &OffsetBuffer<OffsetSize>, ) -> OffsetBuffer<OffsetSize> { - if offsets[0].as_usize() == 0 { - offsets.clone() - } else { - let rebased: Vec<OffsetSize> = offsets.iter().map(|o| *o - offsets[0]).collect(); - OffsetBuffer::new(rebased.into()) - } + offsets.clone().subtract(offsets[0]) } fn order_desc(modifier: &str) -> Result<bool> { diff --git a/datafusion/functions/src/string/common.rs b/datafusion/functions/src/string/common.rs index b51b92e9df..7a5d2573ec 100644 --- a/datafusion/functions/src/string/common.rs +++ b/datafusion/functions/src/string/common.rs @@ -27,7 +27,7 @@ use arrow::array::{ Array, ArrayRef, AsArray, GenericStringArray, NullBufferBuilder, OffsetSizeTrait, StringViewArray, new_null_array, }; -use arrow::buffer::{Buffer, OffsetBuffer, ScalarBuffer}; +use arrow::buffer::{Buffer, ScalarBuffer}; use arrow::datatypes::DataType; use datafusion_common::Result; use datafusion_common::cast::{as_generic_string_array, as_string_view_array}; @@ -636,15 +636,10 @@ fn case_conversion_ascii_array<O: OffsetSizeTrait>( let values = Buffer::from_vec(converted); // Shift offsets from `start`-based to 0-based so they index into `values`. - let offsets = if start == 0 { - string_array.offsets().clone() - } else { - let s = O::usize_as(start); - let rebased: Vec<O> = value_offsets.iter().map(|&o| o - s).collect(); - // SAFETY: subtracting a constant from monotonic offsets preserves - // monotonicity, and `start` is the minimum offset, so no underflow. - unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(rebased)) } - }; + let offsets = string_array + .offsets() + .clone() + .subtract(string_array.offsets()[0]); let nulls = string_array.nulls().cloned(); // SAFETY: offsets are monotonic and in-bounds for `values`; nulls diff --git a/datafusion/functions/src/unicode/initcap.rs b/datafusion/functions/src/unicode/initcap.rs index 9192f23844..8981d59aec 100644 --- a/datafusion/functions/src/unicode/initcap.rs +++ b/datafusion/functions/src/unicode/initcap.rs @@ -18,7 +18,7 @@ use std::sync::Arc; use arrow::array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait}; -use arrow::buffer::{Buffer, OffsetBuffer}; +use arrow::buffer::Buffer; use arrow::datatypes::DataType; use crate::strings::{GenericStringArrayBuilder, StringViewArrayBuilder}; @@ -217,17 +217,10 @@ fn initcap_ascii_array<T: OffsetSizeTrait>( } let values = Buffer::from_vec(out); - let out_offsets = if first_offset == 0 { - offsets.clone() - } else { - // For sliced arrays, we need to rebase the offsets to reflect that the - // output only contains the bytes in the visible slice. - let rebased_offsets = offsets - .iter() - .map(|offset| T::usize_as(offset.as_usize() - first_offset)) - .collect::<Vec<_>>(); - OffsetBuffer::<T>::new(rebased_offsets.into()) - }; + + // Rebase offsets for sliced arrays to reflect that the + // output only contains the bytes in the visible slice. + let out_offsets = offsets.clone().subtract(offsets[0]); // SAFETY: ASCII case conversion preserves byte length, so the original // string boundaries are preserved. `out_offsets` is either identical to --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
