westonpace commented on code in PR #34133: URL: https://github.com/apache/arrow/pull/34133#discussion_r1122546409
########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; Review Comment: I doubt it will matter but maybe use longs here given the flags field is a long? ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; Review Comment: Not something that necessarily needs fixing but this pattern is probably obsolete. Nowadays you can just [switch on the type](https://systemoutofmemory.com/blogs/the-programmer-blog/c-sharp-switch-on-type). ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; Review Comment: Why not? ``` public string name; ``` Here and elsewhere. ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; Review Comment: ```suggestion FormatString = $"d:{datatype.Precision},{datatype.Scale},256"; ``` ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + // Timestamp type + public void Visit(TimestampType datatype) + { + FormatString = String.Format("ts{0}:{1}", TimeUnitComponent(datatype.Unit), datatype.Timezone); + } + // Nested + public void Visit(ListType _) => FormatString = "+l"; + public void Visit(StructType _) => FormatString = "+s"; + // Dictionary + public void Visit(DictionaryType datatype) + { + // format string is that of the indices + datatype.IndexType.Accept(this); + } + + // Unsupported + public void Visit(IArrowType type) + { + throw new NotImplementedException($"Exporting {type.Name} not implemented"); + } + } + + private sealed class ImportedArrowSchema : IDisposable + { + private readonly CArrowSchema _data; + private readonly IntPtr _handle; + private readonly bool _isRoot; + + public ImportedArrowSchema(IntPtr handle) + { + _data = Marshal.PtrToStructure<CArrowSchema>(handle); + if (_data.release == null) + { + throw new Exception("Tried to import a schema that has already been released."); + } + _handle = handle; + _isRoot = true; + } + + private ImportedArrowSchema(IntPtr handle, bool isRoot) : this(handle) + { + _isRoot = isRoot; + } + + public void Dispose() + { + // We only call release on a root-level schema, not child ones. + if (_isRoot) + { + _data.release(_handle); + } + } Review Comment: `Dispose` should be idempotent. Also, there is [a pattern](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose) that is typically used with `Dispose()` and `Dispose(bool)` though that is probably not strictly needed here since there is no managed state. ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) Review Comment: Maybe a comment here explaining that `tt`is reused because `Time32` and `Time64` will never have the same units? ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + // Timestamp type + public void Visit(TimestampType datatype) + { + FormatString = String.Format("ts{0}:{1}", TimeUnitComponent(datatype.Unit), datatype.Timezone); + } + // Nested + public void Visit(ListType _) => FormatString = "+l"; + public void Visit(StructType _) => FormatString = "+s"; + // Dictionary + public void Visit(DictionaryType datatype) + { + // format string is that of the indices + datatype.IndexType.Accept(this); + } + + // Unsupported + public void Visit(IArrowType type) + { + throw new NotImplementedException($"Exporting {type.Name} not implemented"); + } + } + + private sealed class ImportedArrowSchema : IDisposable + { + private readonly CArrowSchema _data; + private readonly IntPtr _handle; + private readonly bool _isRoot; + + public ImportedArrowSchema(IntPtr handle) + { + _data = Marshal.PtrToStructure<CArrowSchema>(handle); + if (_data.release == null) + { + throw new Exception("Tried to import a schema that has already been released."); + } + _handle = handle; + _isRoot = true; + } + + private ImportedArrowSchema(IntPtr handle, bool isRoot) : this(handle) + { + _isRoot = isRoot; + } + + public void Dispose() + { + // We only call release on a root-level schema, not child ones. + if (_isRoot) + { + _data.release(_handle); + } + } + + public ArrowType GetAsType() + { + var format = StringUtil.PtrToStringUtf8(_data.format); + if (_data.dictionary != IntPtr.Zero) + { + ArrowType indicesType = format switch + { + "c" => new Int8Type(), + "C" => new UInt8Type(), + "s" => new Int16Type(), + "S" => new UInt16Type(), + "i" => new Int32Type(), + "I" => new UInt32Type(), + "l" => new Int64Type(), + "L" => new UInt64Type(), + _ => throw new InvalidDataException($"Indices must be an integer, but got format string {format}"), + }; + + var dictionarySchema = new ImportedArrowSchema(_data.dictionary, /*is_root*/ false); + ArrowType dictionaryType = dictionarySchema.GetAsType(); + + bool ordered = (_data.flags & CArrowSchema.ArrowFlagNullable) == CArrowSchema.ArrowFlagNullable; Review Comment: ArrowFlagDictionaryOrdered? ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + // Timestamp type + public void Visit(TimestampType datatype) + { + FormatString = String.Format("ts{0}:{1}", TimeUnitComponent(datatype.Unit), datatype.Timezone); + } + // Nested + public void Visit(ListType _) => FormatString = "+l"; + public void Visit(StructType _) => FormatString = "+s"; + // Dictionary + public void Visit(DictionaryType datatype) + { + // format string is that of the indices + datatype.IndexType.Accept(this); + } + + // Unsupported + public void Visit(IArrowType type) + { + throw new NotImplementedException($"Exporting {type.Name} not implemented"); + } + } + + private sealed class ImportedArrowSchema : IDisposable + { + private readonly CArrowSchema _data; + private readonly IntPtr _handle; + private readonly bool _isRoot; + + public ImportedArrowSchema(IntPtr handle) + { + _data = Marshal.PtrToStructure<CArrowSchema>(handle); + if (_data.release == null) + { + throw new Exception("Tried to import a schema that has already been released."); + } + _handle = handle; + _isRoot = true; + } + + private ImportedArrowSchema(IntPtr handle, bool isRoot) : this(handle) + { + _isRoot = isRoot; + } + + public void Dispose() + { + // We only call release on a root-level schema, not child ones. + if (_isRoot) + { + _data.release(_handle); + } + } + + public ArrowType GetAsType() + { + var format = StringUtil.PtrToStringUtf8(_data.format); + if (_data.dictionary != IntPtr.Zero) + { + ArrowType indicesType = format switch + { + "c" => new Int8Type(), + "C" => new UInt8Type(), + "s" => new Int16Type(), + "S" => new UInt16Type(), + "i" => new Int32Type(), + "I" => new UInt32Type(), + "l" => new Int64Type(), + "L" => new UInt64Type(), + _ => throw new InvalidDataException($"Indices must be an integer, but got format string {format}"), + }; + + var dictionarySchema = new ImportedArrowSchema(_data.dictionary, /*is_root*/ false); Review Comment: ```suggestion var dictionarySchema = new ImportedArrowSchema(_data.dictionary, isRoot: false); ``` ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + // Timestamp type + public void Visit(TimestampType datatype) + { + FormatString = String.Format("ts{0}:{1}", TimeUnitComponent(datatype.Unit), datatype.Timezone); + } + // Nested + public void Visit(ListType _) => FormatString = "+l"; + public void Visit(StructType _) => FormatString = "+s"; + // Dictionary + public void Visit(DictionaryType datatype) + { + // format string is that of the indices + datatype.IndexType.Accept(this); + } + + // Unsupported + public void Visit(IArrowType type) + { + throw new NotImplementedException($"Exporting {type.Name} not implemented"); + } + } + + private sealed class ImportedArrowSchema : IDisposable + { + private readonly CArrowSchema _data; + private readonly IntPtr _handle; + private readonly bool _isRoot; + + public ImportedArrowSchema(IntPtr handle) + { + _data = Marshal.PtrToStructure<CArrowSchema>(handle); + if (_data.release == null) + { + throw new Exception("Tried to import a schema that has already been released."); + } + _handle = handle; + _isRoot = true; + } + + private ImportedArrowSchema(IntPtr handle, bool isRoot) : this(handle) + { + _isRoot = isRoot; + } + + public void Dispose() + { + // We only call release on a root-level schema, not child ones. + if (_isRoot) + { + _data.release(_handle); + } + } + + public ArrowType GetAsType() + { + var format = StringUtil.PtrToStringUtf8(_data.format); + if (_data.dictionary != IntPtr.Zero) + { + ArrowType indicesType = format switch + { + "c" => new Int8Type(), + "C" => new UInt8Type(), + "s" => new Int16Type(), + "S" => new UInt16Type(), + "i" => new Int32Type(), + "I" => new UInt32Type(), + "l" => new Int64Type(), + "L" => new UInt64Type(), + _ => throw new InvalidDataException($"Indices must be an integer, but got format string {format}"), + }; + + var dictionarySchema = new ImportedArrowSchema(_data.dictionary, /*is_root*/ false); + ArrowType dictionaryType = dictionarySchema.GetAsType(); + + bool ordered = (_data.flags & CArrowSchema.ArrowFlagNullable) == CArrowSchema.ArrowFlagNullable; + + return new DictionaryType(indicesType, dictionaryType, ordered); + } + + // Special handling for nested types + if (format == "+l") + { + if (_data.n_children != 1) + { + throw new Exception("Expected list type to have exactly one child."); + } + ImportedArrowSchema childSchema; + if (_data.GetChild(0) == IntPtr.Zero) + { + throw new Exception("Expected list type child to be non-null."); + } + childSchema = new ImportedArrowSchema(_data.GetChild(0)); Review Comment: `is_root=false`? ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,559 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] Review Comment: Ah, I read up some more on this. I don't think `StdCall` is the default (that would be `WinApi` which defaults to `StdCall` on Windows and `CDecl` on Linux). That being said, I don't think we need to worry about calling convention anywhere as it is only relevant when working with 32-bit binaries: https://stackoverflow.com/questions/34832679/is-the-callingconvention-ignored-in-64-bit-net-applications I'm pretty sure Arrow-C++ doesn't even build on 32-bit (https://github.com/apache/arrow/issues/32111) so this is probably a non-issue. With that in mind, perhaps skip the `UnamangedFunctionPointer` entirely? It seems its [only purpose](https://stackoverflow.com/questions/17364738/c-pinvoke-what-does-unmanagedfunctionpointer-do) is to specify the calling convention. ########## csharp/src/Apache.Arrow/C/StringUtil.cs: ########## @@ -0,0 +1,67 @@ +// 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; + +// Adapted from: +// https://github.com/G-Research/ParquetSharp/blob/467d99298fb5a5b9d5935b9c8dbde95e63954dd3/csharp/StringUtil.cs + +namespace Apache.Arrow.C +{ + + internal static class StringUtil + { + public static unsafe IntPtr ToCStringUtf8(string str) + { + var utf8 = System.Text.Encoding.UTF8; + int byteCount = utf8.GetByteCount(str); + byte* byteArray = (byte*)Marshal.AllocHGlobal(byteCount + 1); + + fixed (char* chars = str) + { + utf8.GetBytes(chars, str.Length, byteArray, byteCount); + } + + // Need to make sure it is nul-terminated. + byteArray[byteCount] = 0; + + return (IntPtr)byteArray; + } + + public static string PtrToStringUtf8(IntPtr ptr) + { +#if NETSTANDARD2_1_OR_GREATER + return Marshal.PtrToStringUTF8(ptr); +#else + if (ptr == IntPtr.Zero) + { + return null; + } + + unsafe + { + var s = (byte*)ptr; + int length; + for (length = 0; s[length] != '\0'; ++length) + { + } + + return System.Text.Encoding.UTF8.GetString(s, length); + } +#endif + } + } +} Review Comment: newline ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; Review Comment: Ah, nevermind. It seems that .NET tries to take ownership of the memory and free the string when it's done. ########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,584 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Apache.Arrow.Types; + +[UnmanagedFunctionPointer(CallingConvention.StdCall)] +public delegate void ReleaseCArrowSchema(IntPtr schema); + +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 struct CArrowSchema + { + public IntPtr format; + public IntPtr name; + public IntPtr metadata; + public long flags; + public long n_children; + public IntPtr children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + public ReleaseCArrowSchema release; + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.FormatString; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ArrowFlagNullable; + } + + if (datatype is DictionaryType dictionaryType) + { + if (dictionaryType.Ordered) + { + flags |= ArrowFlagDictionaryOrdered; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr ConstructChildren(IArrowType datatype, out long numChildren) + { + if (datatype is NestedType nestedType) + { + IReadOnlyList<Field> fields = nestedType.Fields; + int numFields = fields.Count; + numChildren = numFields; + + unsafe + { + IntPtr* pointerList = (IntPtr*)Marshal.AllocHGlobal(numFields * IntPtr.Size); + + for (var i = 0; i < numChildren; i++) + { + var cSchema = new CArrowSchema(fields[i]); + IntPtr exportedSchema = cSchema.AllocateAsPtr(); + pointerList[i] = exportedSchema; + } + + return (IntPtr)pointerList; + } + + } + else + { + numChildren = 0; + return IntPtr.Zero; + } + } + + private IntPtr GetChild(int i) + { + if (i >= n_children) + { + throw new Exception("Child index out of bounds."); + } + if (children == IntPtr.Zero) + { + throw new Exception("Children array is null."); + } + unsafe + { + return ((IntPtr*)children)[i]; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType dictType) + { + var cSchema = new CArrowSchema(dictType.ValueType); + return cSchema.AllocateAsPtr(); + } + else + { + return IntPtr.Zero; + } + } + + /// <summary> + /// Initialize the exported C schema as an Arrow type. + /// </summary> + /// <param name="datatype">The Arrow type to export.</param> + public CArrowSchema(IArrowType datatype) + { + format = StringUtil.ToCStringUtf8(GetFormat(datatype)); + name = IntPtr.Zero; + metadata = IntPtr.Zero; + flags = GetFlags(datatype); + + children = ConstructChildren(datatype, out var numChildren); + n_children = numChildren; + + dictionary = ConstructDictionary(datatype); + + release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + + Marshal.FreeHGlobal(schema.format); + Marshal.FreeHGlobal(schema.name); + Marshal.FreeHGlobal(schema.metadata); + + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.GetChild(i)); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + public CArrowSchema(Field field) : this(field.DataType) + { + name = StringUtil.ToCStringUtf8(field.Name); + // TODO: field metadata + metadata = IntPtr.Zero; + flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + public CArrowSchema(Schema schema) : this(new StructType(schema.Fields.Values.ToList())) + { + // TODO: top-level metadata + } + + /// <summary> + /// Allocate an unmanaged pointer and copy this instances data to it. + /// </summary> + /// <remarks> + /// To avoid a memory leak, you must call <see cref="FreePtr"/> on this + /// pointer when done using it. + /// </remarks> + public IntPtr AllocateAsPtr() + { + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this)); + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + return ptr; + } + + /// <summary> + /// Free a pointer that was allocated in <see cref="AllocateAsPtr"/>. + /// </summary> + /// <remarks> + /// Do not call this on a pointer that was allocated elsewhere. + /// </remarks> + public static void FreePtr(IntPtr ptr) + { + var schema = Marshal.PtrToStructure<CArrowSchema>(ptr); + if (schema.release != null) + { + // Call release if not already called. + schema.release(ptr); + } + Marshal.FreeHGlobal(ptr); + } + + /// <summary> + /// Export to an existing pointer + /// </summary> + /// <param name="ptr">An allocated but uninitialized pointer.</param> + public void Export(IntPtr ptr) + { + Marshal.StructureToPtr<CArrowSchema>(this, ptr, false); + } + + /// <summary> + /// Allocated a new pointer to an uninitialized CArrowSchema. + /// </summary> + /// <remarks>This is used to import schemas. See <see cref="ImportType"/>, + /// <see cref="ImportField"/>, and <see cref="ImportSchema"/>. Once data + /// is imported, this pointer must be cleaned up with <see cref="FreePtr"/>. + /// </remarks> + public static IntPtr AllocateUninitialized() + { + return Marshal.AllocHGlobal(Marshal.SizeOf<CArrowSchema>()); + } + + /// <summary> + /// Import C pointer as an <see cref="ArrowType"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// ArrowType importedType = CArrowSchema.ImportType(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static ArrowType ImportType(IntPtr ptr) + { + using var importedType = new ImportedArrowSchema(ptr); + return importedType.GetAsType(); + } + + /// <summary> + /// Import C pointer as an <see cref="Field"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedField = CArrowSchema.ImportField(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Field ImportField(IntPtr ptr) + { + using var importedField = new ImportedArrowSchema(ptr); + return importedField.GetAsField(); + } + + /// <summary> + /// Import C pointer as an <see cref="Schema"/>. + /// </summary> + /// <examples> + /// Typically, you will allocate a uninitialized CArrowSchema pointer, + /// pass that to external function, and then use this method to import + /// the result. + /// + /// <code> + /// IntPtr importedPtr = CArrowSchema.AllocateUninitialized(); + /// foreign_export_function(importedPtr); + /// Field importedSchema = CArrowSchema.ImportSchema(importedPtr); + /// CArrowSchema.FreePtr(importedPtr); + /// </code> + /// </examples> + public static Schema ImportSchema(IntPtr ptr) + { + using var importedSchema = new ImportedArrowSchema(ptr); + return importedSchema.GetAsSchema(); + } + + public const int ArrowFlagDictionaryOrdered = 1; + public const int ArrowFlagNullable = 2; + public const int ArrowFlagMapKeysSorted = 4; + + private class TypeFormatter : + IArrowTypeVisitor<NullType>, + IArrowTypeVisitor<BooleanType>, + IArrowTypeVisitor<Int8Type>, + IArrowTypeVisitor<Int16Type>, + IArrowTypeVisitor<Int32Type>, + IArrowTypeVisitor<Int64Type>, + IArrowTypeVisitor<UInt8Type>, + IArrowTypeVisitor<UInt16Type>, + IArrowTypeVisitor<UInt32Type>, + IArrowTypeVisitor<UInt64Type>, + IArrowTypeVisitor<HalfFloatType>, + IArrowTypeVisitor<FloatType>, + IArrowTypeVisitor<DoubleType>, + IArrowTypeVisitor<Decimal128Type>, + IArrowTypeVisitor<Decimal256Type>, + IArrowTypeVisitor<Date32Type>, + IArrowTypeVisitor<Date64Type>, + IArrowTypeVisitor<Time32Type>, + IArrowTypeVisitor<Time64Type>, + IArrowTypeVisitor<TimestampType>, + IArrowTypeVisitor<StringType>, + IArrowTypeVisitor<BinaryType>, + IArrowTypeVisitor<FixedSizeBinaryType>, + IArrowTypeVisitor<ListType>, + IArrowTypeVisitor<StructType>, + IArrowTypeVisitor<DictionaryType> + { + public string FormatString; + public void Visit(NullType _) => FormatString = "n"; + public void Visit(BooleanType _) => FormatString = "b"; + // Integers + public void Visit(Int8Type _) => FormatString = "c"; + public void Visit(UInt8Type _) => FormatString = "C"; + public void Visit(Int16Type _) => FormatString = "s"; + public void Visit(UInt16Type _) => FormatString = "S"; + public void Visit(Int32Type _) => FormatString = "i"; + public void Visit(UInt32Type _) => FormatString = "I"; + public void Visit(Int64Type _) => FormatString = "l"; + public void Visit(UInt64Type _) => FormatString = "L"; + // Floats + public void Visit(HalfFloatType _) => FormatString = "e"; + public void Visit(FloatType _) => FormatString = "f"; + public void Visit(DoubleType _) => FormatString = "g"; + // Binary + public void Visit(BinaryType _) => FormatString = "z"; + public void Visit(StringType _) => FormatString = "u"; + public void Visit(FixedSizeBinaryType datatype) + { + FormatString = $"w:{datatype.ByteWidth}"; + } + // Decimal + public void Visit(Decimal128Type datatype) + { + FormatString = $"d:{datatype.Precision},{datatype.Scale}"; + } + public void Visit(Decimal256Type datatype) + { + FormatString = $"w:{datatype.Precision},{datatype.Scale},256"; + } + // Date + public void Visit(Date32Type _) => FormatString = "tdD"; + public void Visit(Date64Type _) => FormatString = "tdm"; + + private char TimeUnitComponent(TimeUnit unit) => unit switch + { + TimeUnit.Second => 's', + TimeUnit.Millisecond => 'm', + TimeUnit.Microsecond => 'u', + TimeUnit.Nanosecond => 'n', + _ => throw new InvalidDataException($"Unsupported time unit for export: {unit}"), + }; + // Time + public void Visit(Time32Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + public void Visit(Time64Type datatype) + { + FormatString = String.Format("tt{0}", TimeUnitComponent(datatype.Unit)); + } + // Timestamp type + public void Visit(TimestampType datatype) + { + FormatString = String.Format("ts{0}:{1}", TimeUnitComponent(datatype.Unit), datatype.Timezone); + } + // Nested + public void Visit(ListType _) => FormatString = "+l"; + public void Visit(StructType _) => FormatString = "+s"; + // Dictionary + public void Visit(DictionaryType datatype) + { + // format string is that of the indices + datatype.IndexType.Accept(this); + } + + // Unsupported + public void Visit(IArrowType type) + { + throw new NotImplementedException($"Exporting {type.Name} not implemented"); + } + } + + private sealed class ImportedArrowSchema : IDisposable + { + private readonly CArrowSchema _data; + private readonly IntPtr _handle; + private readonly bool _isRoot; + + public ImportedArrowSchema(IntPtr handle) + { + _data = Marshal.PtrToStructure<CArrowSchema>(handle); + if (_data.release == null) + { + throw new Exception("Tried to import a schema that has already been released."); + } + _handle = handle; + _isRoot = true; + } + + private ImportedArrowSchema(IntPtr handle, bool isRoot) : this(handle) + { + _isRoot = isRoot; + } + + public void Dispose() + { + // We only call release on a root-level schema, not child ones. + if (_isRoot) + { + _data.release(_handle); + } + } + + public ArrowType GetAsType() + { + var format = StringUtil.PtrToStringUtf8(_data.format); + if (_data.dictionary != IntPtr.Zero) + { + ArrowType indicesType = format switch + { + "c" => new Int8Type(), + "C" => new UInt8Type(), + "s" => new Int16Type(), + "S" => new UInt16Type(), + "i" => new Int32Type(), + "I" => new UInt32Type(), + "l" => new Int64Type(), + "L" => new UInt64Type(), + _ => throw new InvalidDataException($"Indices must be an integer, but got format string {format}"), + }; + + var dictionarySchema = new ImportedArrowSchema(_data.dictionary, /*is_root*/ false); + ArrowType dictionaryType = dictionarySchema.GetAsType(); + + bool ordered = (_data.flags & CArrowSchema.ArrowFlagNullable) == CArrowSchema.ArrowFlagNullable; + + return new DictionaryType(indicesType, dictionaryType, ordered); + } + + // Special handling for nested types + if (format == "+l") + { + if (_data.n_children != 1) + { + throw new Exception("Expected list type to have exactly one child."); + } + ImportedArrowSchema childSchema; + if (_data.GetChild(0) == IntPtr.Zero) + { + throw new Exception("Expected list type child to be non-null."); + } + childSchema = new ImportedArrowSchema(_data.GetChild(0)); + + Field childField = childSchema.GetAsField(); + + return new ListType(childField); + } + else if (format == "+s") + { + var child_schemas = new ImportedArrowSchema[_data.n_children]; + + for (int i = 0; i < _data.n_children; i++) + { + if (_data.GetChild(i) == IntPtr.Zero) + { + throw new Exception("Expected struct type child to be non-null."); + } + child_schemas[i] = new ImportedArrowSchema(_data.GetChild(i)); Review Comment: `is_root=false`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
