jorisvandenbossche commented on code in PR #1346:
URL: https://github.com/apache/arrow-adbc/pull/1346#discussion_r1417590312


##########
python/adbc_driver_manager/adbc_driver_manager/_lib.pyx:
##########
@@ -316,23 +348,56 @@ cdef class ArrowSchemaHandle:
         """The address of the ArrowSchema."""
         return <uintptr_t> &self.schema
 
+    def __arrow_c_schema__(self) -> object:
+        """Consume this object to get a PyCapsule."""
+        # Reference:
+        # 
https://arrow.apache.org/docs/dev/format/CDataInterface/PyCapsuleInterface.html#create-a-pycapsule
+        cdef CArrowSchema* allocated = <CArrowSchema*> 
malloc(sizeof(CArrowSchema))
+        allocated.release = NULL
+        capsule = PyCapsule_New(
+            <void*>allocated, "arrow_schema", &pycapsule_schema_deleter,
+        )
+        memcpy(allocated, &self.schema, sizeof(CArrowSchema))
+        self.schema.release = NULL
+        return capsule
+
 
 cdef class ArrowArrayHandle:
     """
     A wrapper for an allocated ArrowArray.
+
+    This object implements the Arrow PyCapsule interface.
     """
     cdef:
         CArrowArray array
 
     @property
     def address(self) -> int:
-        """The address of the ArrowArray."""
+        """
+        The address of the ArrowArray.
+        """
         return <uintptr_t> &self.array
 
+    def __arrow_c_array__(self, requested_schema=None) -> object:
+        """Consume this object to get a PyCapsule."""
+        if requested_schema is not None:
+            raise NotImplementedError("requested_schema")
+
+        cdef CArrowArray* allocated = <CArrowArray*> 
malloc(sizeof(CArrowArray))
+        allocated.release = NULL
+        capsule = PyCapsule_New(
+            <void*>allocated, "arrow_array", pycapsule_array_deleter,
+        )
+        memcpy(allocated, &self.array, sizeof(CArrowArray))
+        self.array.release = NULL
+        return capsule

Review Comment:
   This is actually not being used at the moment, because I think none of the 
ADBC APIs are _returning_ an ArrowArray (only ArrowSchema or ArrowArrayStream). 
   This handle is currently only used internally for ingesting data (`bind`).
   
   So I could also remove `__arrow_c_array__`, given it is unused. If we 
require pyarrow >= 14, I could probably also remove this class entirely, 
because then we can use the capsule interface for ingesting data.



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