Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] C Data Interface import computes incorrect buffer sizes when offset is non-zero #43267

Closed
adamreeve opened this issue Jul 16, 2024 · 1 comment · Fixed by #44117
Closed
Assignees
Milestone

Comments

@adamreeve
Copy link
Contributor

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

Code to reproduce the problem as an XUnit test:

[Fact]
public unsafe void RoundTripArrayWithOffset()
{
    Int32Array array = new Int32Array.Builder()
        .AppendRange(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })
        .Build();
    IArrowArray sliced = array.Slice(2, 6);
    CArrowArray* cArray = CArrowArray.Create();
    CArrowArrayExporter.ExportArray(sliced, cArray);
    using (var importedSlice = (Int32Array)CArrowArrayImporter.ImportArray(cArray, array.Data.DataType))
    {
        Assert.Equal(6, importedSlice.Length);  // OK
        Assert.Equal(2, importedSlice.Offset);  // OK
        Assert.Equal(2, importedSlice.GetValue(0));  // Throws
    }
    CArrowArray.Free(cArray);
}

This throws:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

System.ArgumentOutOfRangeException
Specified argument was out of the range of valid values.
   at Apache.Arrow.PrimitiveArray`1.get_Values() in /home/adam/dev/arrow/csharp/src/Apache.Arrow/Arrays/PrimitiveArray.cs:line 34
   at Apache.Arrow.Tests.CDataInterfaceDataTests.RoundTripArrayWithOffset() in /home/adam/dev/arrow/csharp/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs:line 83
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

The problem is that when importing the buffers, we assume the buffer length is at least array.length * typeByteWidth (see ImportFixedWidthBuffers), but really it should be (array.length + array.offset) * typeByteWidth. This example is for a fixed width array type, but other more complex array types appear to have the same problem.

I don't have any immediate plans to fix this myself, but noticed this problem as part of looking at #43266.

Component(s)

C#

@CurtHagenlocher CurtHagenlocher self-assigned this Jul 16, 2024
CurtHagenlocher added a commit that referenced this issue Sep 16, 2024
…rface (#44117)

### What changes are included in this PR?

Changes to the C Data importer to correctly handle nonzero offsets.

### Are these changes tested?

Yes

### Are there any user-facing changes?

No

Closes #43267 
* GitHub Issue: #43267

Authored-by: Curt Hagenlocher <[email protected]>
Signed-off-by: Curt Hagenlocher <[email protected]>
@CurtHagenlocher CurtHagenlocher added this to the 18.0.0 milestone Sep 16, 2024
@CurtHagenlocher
Copy link
Contributor

Issue resolved by pull request 44117
#44117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants