Skip to content

[C#] Slicing a StructArray doesn't slice the child field arrays #40790

Closed

Description

@adamreeve

Describe the bug, including details regarding any error messages, version, and platform.

Code to reproduce, as an XUnit test:

[Fact]
public void TestSliceStructArray()
{
    const int numRows = 10;
    var fields = new List<Field>
    {
        new Field.Builder().Name("ints").DataType(new Int32Type()).Nullable(true).Build(),
    };
    var arrays = new List<IArrowArray>
    {
        new Int32Array.Builder().AppendRange(Enumerable.Range(0, numRows)).Build(),
    };

    var nullBitmap = new ArrowBuffer.BitmapBuilder().AppendRange(true, numRows).Build();
    var array = new StructArray(new StructType(fields), numRows, arrays, nullBitmap, nullCount: 0);

    var slicedArray = (StructArray) array.Slice(3, 4);

    Assert.Equal(4, slicedArray.Length);
    Assert.Single(slicedArray.Fields);
    var slicedField = slicedArray.Fields[0];
    Assert.Equal(4, slicedField.Length);  // Fails with "Actual:   10"
}

Is this expected behaviour? If not, I'm happy to look into fixing this. The existing behaviour should probably be kept for backwards compatibility though, in case users are slicing the child fields themselves to work around this.

Component(s)

C#

Activity

  1. CurtHagenlocher commented on Mar 26, 2024

    @CurtHagenlocher
    Contributor

    I think it's not structs per se that are the problem but lists (and fixed size lists and unions and maps).

  2. CurtHagenlocher commented on Mar 26, 2024

    @CurtHagenlocher
    Contributor

    Okay, after looking at the example I guess it's structs too...

  3. adamreeve commented on Mar 26, 2024

    @adamreeve
    ContributorAuthor

    I think this is actually specific to structs. With a ListArray, the child buffer/Array doesn't need to be sliced, the offset just needs to be accounted for when accessing the value offsets, so slicing a ListArray seems to work fine. Eg. this passes:

    [Fact]
    public void TestSliceListArray()
    {
        const int numRows = 10;
        var builder = new ListArray.Builder(new Int32Type());
        var valueBuilder = (Int32Array.Builder) builder.ValueBuilder;
        for (var rowIdx = 0; rowIdx < numRows; ++rowIdx)
        {
            builder.Append();
            var length = rowIdx % 3;
            for (var i = 0; i < length; ++i)
            {
                valueBuilder.Append(valueBuilder.Length);
            }
        }
    
        var array = builder.Build();
    
        var slicedArray = (ListArray) array.Slice(2, 4);
    
        Assert.Equal(4, slicedArray.Length);
        for (var rowIdx = 0; rowIdx < slicedArray.Length; ++rowIdx)
        {
            var slicedArrayList = (IReadOnlyList<int?>)slicedArray.GetSlicedValues(rowIdx);
            var sourceArrayList = (IReadOnlyList<int?>)array.GetSlicedValues(rowIdx + 2);
    
            Assert.Equal(sourceArrayList, slicedArrayList);
        }
    }
  4. added a commit that references this issue on Mar 27, 2024
  5. CurtHagenlocher commented on Mar 27, 2024

    @CurtHagenlocher
    Contributor

    Issue resolved by pull request 40805
    #40805

  6. added this to the 16.0.0 milestone on Mar 27, 2024
  7. added a commit that references this issue on May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions