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

GH-33856: [C#] Implement C Data Interface for C# #35496

Merged
merged 14 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static IArrowArray BuildArray(ArrayData data)
{
switch (data.DataType.TypeId)
{
case ArrowTypeId.Null:
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
return new NullArray(data);
case ArrowTypeId.Boolean:
return new BooleanArray(data);
case ArrowTypeId.UInt8:
Expand Down
119 changes: 119 additions & 0 deletions csharp/src/Apache.Arrow/Arrays/NullArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Apache.Arrow.Types;
using Apache.Arrow.Memory;

namespace Apache.Arrow
{
public class NullArray : IArrowArray
{
public class Builder : IArrowArrayBuilder<NullArray, Builder>
{
private int _length;

public int Length => _length;
public int Capacity => _length;
public int NullCount => _length;

public Builder()
{
}

public Builder AppendNull()
{
_length++;
return this;
}

public NullArray Build(MemoryAllocator allocator = default)
{
return new NullArray(_length);
}

public Builder Clear()
{
_length = 0;
return this;
}

public Builder Reserve(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}

return this;
}

public Builder Resize(int length)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length));
}

_length = length;
return this;
}

private void CheckIndex(int index)
{
if (index < 0 || index >= Length)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
}
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
}

public ArrayData Data { get; }

public NullArray(ArrayData data)
{
data.EnsureDataType(ArrowTypeId.Null);
data.EnsureBufferCount(0);
Data = data;
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
}

public NullArray(int length)
: this(new ArrayData(NullType.Default, length, length, buffers: System.Array.Empty<ArrowBuffer>()))
{
}

public int Length => Data.Length;

public int Offset => Data.Offset;

public int NullCount => Data.NullCount;

public void Dispose() { }
public bool IsNull(int index) => true;
public bool IsValid(int index) => false;

public void Accept(IArrowArrayVisitor visitor)
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
{
if (visitor is IArrowArrayVisitor<NullArray> nullVisitor)
{
nullVisitor.Visit(this);
}
else
{
visitor.Visit(this);
}
}
}
}
19 changes: 19 additions & 0 deletions csharp/src/Apache.Arrow/ArrowBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,24 @@ public void Dispose()
{
_memoryOwner?.Dispose();
}

internal bool TryExport(ExportedAllocationOwner newOwner, out IntPtr ptr)
{
if (_memoryOwner == null && IsEmpty)
{
ptr = IntPtr.Zero;
return true;
}

if (_memoryOwner is IOwnableAllocation ownable && ownable.TryAcquire(out ptr, out int offset, out int length))
{
newOwner.Acquire(ptr, offset, length);
ptr += offset;
return true;
}

ptr = IntPtr.Zero;
return false;
}
}
}
84 changes: 84 additions & 0 deletions csharp/src/Apache.Arrow/C/CArrowArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Runtime.InteropServices;

namespace Apache.Arrow.C
{
/// <summary>
/// An Arrow C Data Interface Schema, which represents the data in an exported array or record batch.
/// </summary>
/// <remarks>
/// This is used to export <see cref="RecordBatch"/> or <see cref="IArrowArray"/> to other languages. It matches
/// the layout of the ArrowArray struct described in https://github.com/apache/arrow/blob/main/cpp/src/arrow/c/abi.h.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public unsafe struct CArrowArray
{
public long length;
public long null_count;
public long offset;
public long n_buffers;
public long n_children;
public byte** buffers;
public CArrowArray** children;
public CArrowArray* dictionary;
public delegate* unmanaged[Stdcall]<CArrowArray*, void> release;
public void* private_data;

/// <summary>
/// Allocate and zero-initialize an unmanaged pointer of this type.
/// </summary>
/// <remarks>
/// This pointer must later be freed by <see cref="Free"/>.
/// </remarks>
public static CArrowArray* Create()
{
var ptr = (CArrowArray*)Marshal.AllocHGlobal(sizeof(CArrowArray));

ptr->length = 0;
ptr->n_buffers = 0;
ptr->offset = 0;
ptr->buffers = null;
ptr->n_children = 0;
ptr->children = null;
ptr->dictionary = null;
ptr->null_count = 0;
ptr->release = null;
ptr->private_data = null;

return ptr;
}

/// <summary>
/// Free a pointer that was allocated in <see cref="Create"/>.
/// </summary>
/// <remarks>
/// Do not call this on a pointer that was allocated elsewhere.
/// </remarks>
public static void Free(CArrowArray* schema)
CurtHagenlocher marked this conversation as resolved.
Show resolved Hide resolved
{
if (schema->release != null)
{
// Call release if not already called.
schema->release(schema);
}
Marshal.FreeHGlobal((IntPtr)schema);
}
}
}
Loading