Sub topic 13
The C data interface: handing arrays between libraries
This chapter assumes you know what an Arrow array, its buffers and a schema are, and what zero-copy sharing means. You don't need to write C.
The problem it solves
Suppose a Rust library produces an Arrow array and a Python library needs to read it. IPC would work, but it serializes bytes and parses them again. Linking each library against the other's Arrow implementation is fragile.
The Arrow C data interface is a small ABI (a binary-level contract) made of two C structs. Any library that can read C structs can use them, including Python extensions, Rust, Go, C++ and R. No Arrow library needs to be shared between the two sides.
The two structs
ArrowSchemadescribes a type. Itsformatfield is a short string such as"i"for int32,"l"for int64 or"u"for UTF-8 strings. It also holds the field name, flags, child schemas and areleasecallback.ArrowArraydescribes the data. It holdslength,null_count,offset, an array of pointers to buffers (validity bitmap, offsets, values), child arrays and areleasecallback.
The structs contain pointers to the producer's existing buffers. Handing them over copies no values.
Ownership through `release`
Each struct carries a release function pointer. The rules are:
- The producer fills in the structs and keeps the buffers alive.
- The consumer reads them and, when finished, calls
releaseexactly once. - After release, the struct is marked as released by setting
releaseto NULL, and its pointers must not be used.
This is how memory ownership crosses a language boundary. The buffers belong to whoever allocated them, and release tells the allocator when it may free them.
A round trip in pyarrow
pyarrow exposes the interface through private helper methods. The example below uses cffi to allocate the structs (pip install cffi). In real code the importing side would be a different library.
import pyarrow as pa
from pyarrow.cffi import ffi
c_schema = ffi.new("struct ArrowSchema*")
c_array = ffi.new("struct ArrowArray*")
ptr_schema = int(ffi.cast("uintptr_t", c_schema))
ptr_array = int(ffi.cast("uintptr_t", c_array))
arr = pa.array([1, None, 3], type=pa.int32())
arr._export_to_c(ptr_array, ptr_schema)
imported = pa.Array._import_from_c(ptr_array, ptr_schema)
print(imported)
print(imported.null_count)After the export, c_array points at the buffers of arr. After the import, imported owns the release responsibility, so the structs are marked released. Importing the same structs a second time is an error.
The PyCapsule interface
Most Python code should not handle raw pointers. Recent Python Arrow libraries instead agree on a protocol built on the C data interface. An object defines __arrow_c_array__, __arrow_c_schema__ or __arrow_c_stream__. Each method returns the structs wrapped in a Python capsule, which calls release if nobody consumes it. A consumer that accepts such objects, for example pyarrow when you build a table from a compatible object, needs no import of the producer library. The exact set of supported entry points depends on your pyarrow version, so check its documentation.
Common errors
- Using released data. Reading the structs after calling
releaseis undefined behavior and often crashes. - Releasing twice. Only the consumer releases, and only once.
- Wrong schema for the array. The
formatstring must match the buffers. A schema saying"l"over int32 buffers reads garbage. - Assuming a copy. Mutating the producer's buffers after export changes what the consumer sees. Arrow arrays are meant to be immutable, so treat exported data as read-only.
- Forgetting
offset. A sliced array shares buffers and usesoffset. Consumers must honor it.
Exercise
A consumer imports an ArrowArray, reads it, and never calls release. What goes wrong, and why is calling release twice also a bug?
Show answer
The producer never learns that the buffers are no longer needed, so the memory leaks. Calling release twice can free the buffers twice, or free memory the producer already reused, which can crash the process. The consumer must call release exactly once.