wjones127 commented on code in PR #34133:
URL: https://github.com/apache/arrow/pull/34133#discussion_r1148438305


##########
csharp/src/Apache.Arrow/C/CArrowSchemaImporter.cs:
##########
@@ -0,0 +1,285 @@
+// 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 Apache.Arrow.Types;
+
+namespace Apache.Arrow.C
+{
+    public static class CArrowSchemaImporter
+    {
+        /// <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>
+        /// CArrowSchema* importedPtr = CArrowSchema.New();
+        /// foreign_export_function(importedPtr);
+        /// ArrowType importedType = 
CArrowSchemaImporter.ImportType(importedPtr);
+        /// CArrowSchema.Free(importedPtr);
+        /// </code>
+        /// </examples>
+        public static unsafe ArrowType ImportType(CArrowSchema* 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>
+        /// CArrowSchema* importedPtr = CArrowSchema.New();
+        /// foreign_export_function(importedPtr);
+        /// Field importedField = 
CArrowSchemaImporter.ImportField(importedPtr);
+        /// CArrowSchema.Free(importedPtr);
+        /// </code>
+        /// </examples>
+        public static unsafe Field ImportField(CArrowSchema* 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>
+        /// CArrowSchema* importedPtr = CArrowSchema.New();
+        /// foreign_export_function(importedPtr);
+        /// Field importedSchema = 
CArrowSchemaImporter.ImportSchema(importedPtr);
+        /// CArrowSchema.Free(importedPtr);
+        /// </code>
+        /// </examples>
+        public static unsafe Schema ImportSchema(CArrowSchema* ptr)
+        {
+            using var importedSchema = new ImportedArrowSchema(ptr);
+            return importedSchema.GetAsSchema();
+        }
+
+        private sealed unsafe class ImportedArrowSchema : IDisposable
+        {
+            private readonly CArrowSchema* _data;
+            private readonly bool _isRoot;
+
+            public ImportedArrowSchema(CArrowSchema* handle)
+            {
+                _data = (CArrowSchema*)handle;
+                if (_data->release == null)
+                {
+                    throw new ArgumentException("Tried to import a schema that 
has already been released.");
+                }
+                _isRoot = true;
+            }
+
+            public ImportedArrowSchema(CArrowSchema* 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 != null)
+                {
+                    _data->release(_data);

Review Comment:
   > Isn't the fact that the function call returned to the caller enough to 
know that I'm finished using those resources?
   
   I think this will make more sense when you look at array import rather than 
schema import. The main point of the C data interface is to allow one 
implementation (the producer) to share array buffers with another 
implementation (the consumer). If the lifetime of those buffers was limited to 
the import function call, then that means the consumer would either be limited 
to using the arrays during the import or else have to copy the buffers (at 
which point you might as well use IPC).
   
   As an example, imagine we used the C data interface to create bindings to 
DataFusion to run queries. 
   
   ```csharp
   RecordBatch batch = ... 
   DataFusionContext context;
   
   // Uses C data interface to import batch (as a struct array)
   context.ImportBatch("my_table", batch);
   
   // This query is applied to the imported array
   var result = context.Sql("SELECT sum(x * y) FROM my_table");
   ```
   
   Here, if we assumed it was safe to cleanup the batch after calling 
`ImportBatch`, then we couldn't call the subsequent query to do any computation 
on it. We need those buffers to live as long as the consumer still needs them.



-- 
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]

Reply via email to