-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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-34737: [C#] C Data interface for schemas and types #34133
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cc1a47c
feat: import and export schemas over ffi
wjones127 6775bcc
docs: document C data interface
wjones127 05e1212
test: make Python dll available in CI
wjones127 afa56c6
chore: conform to c# style
wjones127 0a55436
ci: simplify python detection
wjones127 0eacb16
refactor: simplify import and export interface to a single class
wjones127 45c1427
test: optionally skip python test when local
wjones127 1c2c422
fix: use utf8 over c data interface
wjones127 1e70b98
docs: document allocating and freeing pointers
wjones127 8946c94
refactor: narrow use of unsafe
wjones127 c0a81e1
add support for timestamp and decimal; fix dictionary
wjones127 82d9847
add fsb and public GetFlag method
wjones127 8eaa31a
refactor: use pointers; separate imported and exporter classes
wjones127 c70cd0f
refactor: fix exception types
wjones127 4ac33fe
fix: stop using old method
wjones127 495a250
fix examples
wjones127 59b518c
pr feedback
wjones127 1e259fd
Apply suggestions from code review
wjones127 f141585
more PR feedback
wjones127 02ccfd3
refactor: split classes to new files
wjones127 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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; | ||
using Apache.Arrow.Types; | ||
|
||
namespace Apache.Arrow.C | ||
{ | ||
/// <summary> | ||
/// An Arrow C Data Interface Schema, which represents a type, field, or schema. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is used to export <see cref="ArrowType"/>, <see cref="Field"/>, or | ||
/// <see cref="Schema"/> to other languages. It matches the layout of the | ||
/// ArrowSchema struct described in https://github.com/apache/arrow/blob/main/cpp/src/arrow/c/abi.h. | ||
/// </remarks> | ||
[StructLayout(LayoutKind.Sequential)] | ||
public unsafe struct CArrowSchema | ||
{ | ||
public byte* format; | ||
public byte* name; | ||
public byte* metadata; | ||
public long flags; | ||
public long n_children; | ||
public CArrowSchema** children; | ||
public CArrowSchema* dictionary; | ||
public delegate* unmanaged[Stdcall]<CArrowSchema*, 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 CArrowSchema* Create() | ||
{ | ||
var ptr = (CArrowSchema*)Marshal.AllocHGlobal(sizeof(CArrowSchema)); | ||
|
||
ptr->format = null; | ||
ptr->name = null; | ||
ptr->metadata = null; | ||
ptr->flags = 0; | ||
ptr->n_children = 0; | ||
ptr->children = null; | ||
ptr->dictionary = null; | ||
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(CArrowSchema* schema) | ||
{ | ||
if (schema->release != null) | ||
{ | ||
// Call release if not already called. | ||
schema->release(schema); | ||
} | ||
Marshal.FreeHGlobal((IntPtr)schema); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// For dictionary-encoded types, whether the ordering of dictionary indices is semantically meaningful. | ||
/// </summary> | ||
public const long ArrowFlagDictionaryOrdered = 1; | ||
/// <summary> | ||
/// Whether this field is semantically nullable (regardless of whether it actually has null values) | ||
/// </summary> | ||
public const long ArrowFlagNullable = 2; | ||
/// <summary> | ||
/// For map types, whether the keys within each map value are sorted. | ||
/// </summary> | ||
public const long ArrowFlagMapKeysSorted = 4; | ||
|
||
/// <summary> | ||
/// Get the value of a particular flag. | ||
/// </summary> | ||
/// <remarks> | ||
/// Known valid flags are <see cref="ArrowFlagDictionaryOrdered" />, | ||
/// <see cref="ArrowFlagNullable" />, and <see cref="ArrowFlagMapKeysSorted" />. | ||
/// </remarks> | ||
public readonly bool GetFlag(long flag) | ||
{ | ||
return (flags & flag) == flag; | ||
} | ||
|
||
internal readonly CArrowSchema* GetChild(long i) | ||
{ | ||
if ((ulong)i >= (ulong)n_children) | ||
{ | ||
throw new ArgumentOutOfRangeException("Child index out of bounds."); | ||
wjones127 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (children == null) | ||
{ | ||
throw new ArgumentOutOfRangeException($"Child index '{i}' out of bounds."); | ||
} | ||
|
||
return children[i]; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future (when we start targeting net6.0+), I think we will want to drop the
[Stdcall]
here and just use the default:But that doesn't work in the older TFMs, so what we have now is fine.