This is an automated email from the ASF dual-hosted git repository.

curth pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 4b0f3fd8e4 GH-40792: [C#] Fix slicing a previously sliced array 
(#40793)
4b0f3fd8e4 is described below

commit 4b0f3fd8e40ecfe42e1eb961f50c0fe4affa0f18
Author: Adam Reeve <[email protected]>
AuthorDate: Wed Mar 27 01:30:28 2024 +1300

    GH-40792: [C#] Fix slicing a previously sliced array (#40793)
    
    ### Rationale for this change
    
    See #40792, slicing a previously sliced array would return incorrect 
results.
    
    ### What changes are included in this PR?
    
    Removes the duplicated logic accounting for the current offset in 
`ArrowArrayFactory`, which is already handled by `ArrayData.Slice`.
    
    ### Are these changes tested?
    
    Yes, I added a unit test.
    
    ### Are there any user-facing changes?
    
    Yes, this is a user-facing bug fix.
    * GitHub Issue: #40792
    
    Authored-by: Adam Reeve <[email protected]>
    Signed-off-by: Curt Hagenlocher <[email protected]>
---
 csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs |  8 --------
 csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs   | 17 +++++++++++++++++
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs 
b/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs
index 3d2ab1d212..67c4b21a2e 100644
--- a/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs
+++ b/csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs
@@ -104,14 +104,6 @@ namespace Apache.Arrow
 
         public static IArrowArray Slice(IArrowArray array, int offset, int 
length)
         {
-            if (offset > array.Length)
-            {
-                throw new ArgumentException($"Offset {offset} cannot be 
greater than Length {array.Length} for Array.Slice");
-            }
-
-            length = Math.Min(array.Data.Length - offset, length);
-            offset += array.Data.Offset;
-
             ArrayData newData = array.Data.Slice(offset, length);
             return BuildArray(newData);
         }
diff --git a/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs 
b/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs
index 269c2390a7..a0e90cbbc7 100644
--- a/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs
+++ b/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs
@@ -16,6 +16,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Linq;
 using System.Numerics;
 using Xunit;
 
@@ -122,6 +123,22 @@ namespace Apache.Arrow.Tests
             Assert.Equal(readOnlyList[1], 2);
         }
 
+        [Fact]
+        public void RecursiveArraySlice()
+        {
+            var initialValues = Enumerable.Range(0, 100).ToArray();
+            var array = new 
Int32Array.Builder().AppendRange(initialValues).Build();
+
+            var sliced = (Int32Array) array.Slice(20, 30);
+            var slicedAgain = (Int32Array) sliced.Slice(5, 10);
+
+            Assert.Equal(25, slicedAgain.Offset);
+            Assert.Equal(10, slicedAgain.Length);
+            Assert.Equal(
+                initialValues.Skip(25).Take(10).Select(val => (int?) 
val).ToArray(),
+                (IReadOnlyList<int?>) slicedAgain);
+        }
+
 #if NET5_0_OR_GREATER
         [Fact]
         public void SliceArray()

Reply via email to