wjones127 commented on code in PR #34133: URL: https://github.com/apache/arrow/pull/34133#discussion_r1118987851
########## csharp/src/Apache.Arrow/C/CArrowSchema.cs: ########## @@ -0,0 +1,519 @@ +// 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.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)] + unsafe public struct CArrowSchema + { + [MarshalAs(UnmanagedType.LPStr)] + public string format; + [MarshalAs(UnmanagedType.LPStr)] + public string name; + [MarshalAs(UnmanagedType.LPStr)] + public string metadata; + public long flags; + public long n_children; + public IntPtr* children; + public IntPtr dictionary; + [MarshalAs(UnmanagedType.FunctionPtr)] + + public ReleaseCArrowSchema release; + // Check this out: https://github.com/G-Research/ParquetSharp/blob/386d91bd5e6fe6cb81583803447023c1359957c8/csharp/ParquetHandle.cs#L8 + public IntPtr private_data; + + private static string GetFormat(IArrowType datatype) + { + TypeFormatter formatter = new TypeFormatter(); + datatype.Accept(formatter); + return formatter.format_string; + } + + private static long GetFlags(IArrowType datatype, bool nullable = true) + { + long flags = 0; + + if (nullable) + { + flags |= ARROW_FLAG_NULLABLE; + } + + if (datatype is DictionaryType) + { + if (((DictionaryType)datatype).Ordered) + { + flags |= ARROW_FLAG_DICTIONARY_ORDERED; + } + } + + // TODO: when we implement MapType, make sure to set the KEYS_SORTED flag. + return flags; + } + + private static IntPtr* ConstructChildren(IArrowType datatype) + { + if (datatype is NestedType) + { + var fields = ((NestedType)datatype).Fields; + int n_fields = fields.Count; + + IntPtr* pointer_list = (IntPtr*)Marshal.AllocHGlobal(n_fields * sizeof(IntPtr)); + + for (var i = 0; i < n_fields; i++) + { + var c_schema = new CArrowSchema(); + CArrowSchema.ExportField(fields[i], out c_schema); + IntPtr exported_schema = c_schema.AllocateAsPtr(); + pointer_list[i] = exported_schema; + } + + return pointer_list; + } + else + { + return (IntPtr*)IntPtr.Zero; + } + } + + private static IntPtr ConstructDictionary(IArrowType datatype) + { + if (datatype is DictionaryType) + { + var c_schema = new CArrowSchema(); + var value_type = ((DictionaryType)datatype).ValueType; + CArrowSchema.ExportDataType(value_type, out c_schema); + return c_schema.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> + /// <param name="schema">An uninitialized CArrowSchema.</param> + public static void ExportDataType(IArrowType datatype, out CArrowSchema schema) + { + schema.format = GetFormat(datatype); + schema.name = null; + schema.metadata = null; + schema.flags = GetFlags(datatype); + + schema.children = ConstructChildren(datatype); + schema.n_children = datatype is NestedType ? ((NestedType)datatype).Fields.Count : 0; + + schema.dictionary = ConstructDictionary(datatype); + + schema.release = (IntPtr self) => + { + var schema = Marshal.PtrToStructure<CArrowSchema>(self); + if (schema.n_children > 0) + { + for (int i = 0; i < schema.n_children; i++) + { + FreePtr(schema.children[i]); + } + Marshal.FreeHGlobal((IntPtr)schema.children); + } + + if (schema.dictionary != IntPtr.Zero) + { + FreePtr(schema.dictionary); + } + Marshal.DestroyStructure<CArrowSchema>(self); + }; + + schema.private_data = IntPtr.Zero; + } + + /// <summary> + /// Initialize the exported C schema as a field. + /// </summary> + /// <param name="field">Field to export.</param> + /// <param name="schema">An uninitialized CArrowSchema.</param> + public static void ExportField(Field field, out CArrowSchema schema) + { + ExportDataType(field.DataType, out schema); + schema.name = field.Name; + // TODO: field metadata + schema.metadata = null; + schema.flags = GetFlags(field.DataType, field.IsNullable); + } + + /// <summary> + /// Initialize the exported C schema as a schema. + /// </summary> + /// <param name="schema">Schema to export.</param> + /// <param name="out_schema">An uninitialized CArrowSchema</param> + public static void ExportSchema(Schema schema, out CArrowSchema out_schema) + { + // TODO: top-level metadata + var struct_type = new StructType(schema.Fields.Values.ToList()); + ExportDataType(struct_type, out out_schema); + } + + /// <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"></param> + /// <returns></returns> Review Comment: Yes, filled this one. -- 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]
