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-11546-85a11e30172dc64dfd0f3f4ff11b6dee1e6b1106
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 3eb725d40a30ddb805aafa5359ac752b1d33a0fd
Author: Jeffrey Vo <[email protected]>
AuthorDate: Wed Oct 14 18:20:18 2026 +0000

    fix: account for offsets in runarrays when using extend for 
MutableArrayData (#11546)
    
    # Which issue does this PR close?
    
    <!--
    It is not necessary to raise an issue, but it is encouraged if it is a
    non-trivial
    enhancement/bug.
    
    If this PR relates to an existing issue, but does not fully resolve it,
    please change
    the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of
    #NNN" to avoid
    false closing an issue.
    -->
    
    None
    
    # Rationale for this change
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the
    issue then this section is not needed. Explaining clearly why changes
    are proposed
    helps reviewers understand your changes and offer better suggestions for
    fixes.
    
    Please try to explain the problem you are trying to solve in terms of
    the user-visible
    behavior, rather than the implementation.
    -->
    
    Correctness edge case when using RunArrays in MutableArrayData
    
    # What changes are included in this PR?
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is
    sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    - Account for `ArrayData` offset of RunArrays when extending
    `MutableArrayData`
    - Checked conversion of usize length to Run index type to avoid wrapping
    
    # Are these changes tested?
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they
    covered by existing tests, if so identify these tests)?
    
    If this PR claims a performance improvement, please include evidence
    such as
    benchmark results.
    
    DO NOT simply paste a list of commands ran to verify the code; this is
    what our
    CI is for and it pollutes the PR. ONLY explain if testing was included
    with this
    PR, or why it wasn't.
    -->
    
    Yes
    
    # Are there any significant user-facing changes?
    
    <!--
    Significant user-facing changes include:
    - Breaking public API change
    - Significant change to existing functionality (e.g. function returns
    something different)
    
    These need to be explicitly called out to ensure we have proper
    documentation if
    required and to ensure we follow proper semantic versioning.
    
    DO NOT restate the changes of this PR; if a bug was fixed and already
    explained
    above, we don't need this to be restated as a user-facing change.
    -->
    
    No
---
 arrow-data/src/transform/run.rs | 72 +++++++++++++++++++++++++++++++++++------
 1 file changed, 63 insertions(+), 9 deletions(-)

diff --git a/arrow-data/src/transform/run.rs b/arrow-data/src/transform/run.rs
index cb239b9a2c..a8e2f4e150 100644
--- a/arrow-data/src/transform/run.rs
+++ b/arrow-data/src/transform/run.rs
@@ -16,7 +16,7 @@
 // under the License.
 
 use super::{_MutableArrayData, ArrayData, Extend};
-use arrow_buffer::{ArrowNativeType, Buffer, ToByteSlice};
+use arrow_buffer::{ArrowNativeType, ToByteSlice};
 use arrow_schema::{ArrowError, DataType};
 use num_traits::CheckedAdd;
 
@@ -58,8 +58,9 @@ pub fn extend_nulls(mutable: &mut _MutableArrayData, len: 
usize) -> Result<(), A
     macro_rules! extend_nulls_impl {
         ($run_end_type:ty) => {{
             let last_run_end = 
get_last_run_end::<$run_end_type>(&mutable.child_data[0]);
-            let new_value = last_run_end
-                .checked_add(<$run_end_type as ArrowNativeType>::usize_as(len))
+            let len = <$run_end_type as ArrowNativeType>::from_usize(len);
+            let new_value = len
+                .and_then(|len| last_run_end.checked_add(len))
                 .ok_or_else(|| {
                     ArrowError::InvalidArgumentError(
                         "run end overflow when extending RunEndEncoded array: \
@@ -92,7 +93,7 @@ type ExtendArrays = (Vec<u8>, Option<(usize, usize)>);
 
 /// Build run ends bytes and values range directly for batch processing
 fn build_extend_arrays<T: ArrowNativeType + std::ops::Add<Output = T> + 
CheckedAdd>(
-    buffer: &Buffer,
+    typed_slice: &[T],
     length: usize,
     start: usize,
     len: usize,
@@ -104,9 +105,6 @@ fn build_extend_arrays<T: ArrowNativeType + 
std::ops::Add<Output = T> + CheckedA
     let mut prev_end = 0;
     let mut current_run_end = dest_last_run_end;
 
-    // Convert buffer to typed slice once
-    let typed_slice: &[T] = buffer.typed_data();
-
     for i in 0..length {
         if i < typed_slice.len() {
             let run_end = typed_slice[i].to_usize().unwrap();
@@ -217,6 +215,8 @@ pub fn build_extend(array: &ArrayData) -> Extend<'_> {
 
             // We need to analyze the source array's run structure
             let source_run_ends = &array.child_data()[0];
+            let source_buffer_offset = source_run_ends.offset();
+            let source_buffer_len = source_run_ends.len();
             let source_buffer = &source_run_ends.buffers()[0];
 
             // Get the run end type from the mutable array
@@ -230,6 +230,8 @@ pub fn build_extend(array: &ArrayData) -> Extend<'_> {
             // Build run ends and values indices directly for batch processing
             macro_rules! build_and_process_impl {
                 ($run_end_type:ty) => {{
+                    let source_buffer = &source_buffer.typed_data()
+                        [source_buffer_offset..source_buffer_offset + 
source_buffer_len];
                     let dest_last_run_end =
                         
get_last_run_end::<$run_end_type>(&mutable.child_data[0]);
                     let (run_ends_bytes, values_range) = 
build_extend_arrays::<$run_end_type>(
@@ -566,7 +568,7 @@ mod tests {
     fn test_build_extend_arrays_int16() {
         let buffer = Buffer::from_vec(vec![3i16, 5i16, 8i16]);
         let (run_ends_bytes, values_range) =
-            build_extend_arrays::<i16>(&buffer, 3, 2, 4, 0i16).unwrap();
+            build_extend_arrays::<i16>(buffer.typed_data(), 3, 2, 4, 
0i16).unwrap();
 
         // Logical array: [A, A, A, B, B, C, C, C]
         // Requesting indices 2-6 should give us:
@@ -589,7 +591,7 @@ mod tests {
     fn test_build_extend_arrays_int64() {
         let buffer = Buffer::from_vec(vec![3i64, 5i64, 8i64]);
         let (run_ends_bytes, values_range) =
-            build_extend_arrays::<i64>(&buffer, 3, 2, 4, 0i64).unwrap();
+            build_extend_arrays::<i64>(buffer.typed_data(), 3, 2, 4, 
0i64).unwrap();
 
         // Same logic as above but with i64
         assert_eq!(run_ends_bytes.len(), 3 * std::mem::size_of::<i64>());
@@ -712,4 +714,56 @@ mod tests {
             "unexpected error: {err}"
         );
     }
+
+    #[test]
+    fn test_extend_nulls_overflow() {
+        let values = create_int32_array_data(vec![10]);
+        let array = create_run_array_data_int16(vec![100], values);
+
+        let mut mutable = MutableArrayData::new(vec![&array], true, 10);
+        let err = mutable.try_extend_nulls(i16::MAX as usize + 1).unwrap_err();
+        assert_eq!(
+            err.to_string(),
+            "Invalid argument error: run end overflow when extending 
RunEndEncoded array: use a larger run-end type (e.g. Int64 instead of Int32)"
+        );
+    }
+
+    #[test]
+    fn test_extend_accounts_for_offset() {
+        let dt = DataType::RunEndEncoded(
+            Arc::new(Field::new(
+                Field::REE_RUN_ENDS_FIELD_DEFAULT_NAME,
+                DataType::Int32,
+                false,
+            )),
+            Arc::new(Field::new(
+                Field::REE_VALUES_FIELD_DEFAULT_NAME,
+                DataType::Float32,
+                true,
+            )),
+        );
+
+        let runs = ArrayData::builder(DataType::Int32)
+            .len(2)
+            .offset(2)
+            .add_buffer(vec![-10_i32, -20, 1_i32, 4, 0, 100].into())
+            .build()
+            .unwrap();
+        let values = ArrayData::builder(DataType::Float32)
+            .len(2)
+            .add_buffer(vec![1.5, 2.5].into())
+            .build()
+            .unwrap();
+
+        let source_data = ArrayDataBuilder::new(dt)
+            .len(3)
+            .child_data(vec![runs, values])
+            .build()
+            .unwrap();
+
+        let mut mutable = MutableArrayData::new(vec![&source_data], true, 10);
+        mutable.try_extend(0, 0, 1).unwrap();
+        let data = mutable.freeze();
+        assert_eq!(data, source_data.slice(0, 1));
+    }
 }

Reply via email to