Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions datafusion/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one
# Apache DataFusion Foreign Function Interface

This crate contains code to allow interoperability of [Apache DataFusion] with
functions from other libraries and/or DataFusion versions using a stable
interface.
functions from other libraries using a stable interface.

One of the limitations of the Rust programming language is that there is no
stable [Rust ABI] (Application Binary Interface). If a library is compiled with
Expand All @@ -17,28 +16,45 @@ program compiled by a different Rust compiler, there is no guarantee that you
can access the data structures. In order to share code between libraries loaded
at runtime, you need to use Rust's [FFI] (Foreign Function Interface (FFI)).

The purpose of this crate is to define interfaces between DataFusion libraries
that will remain stable across different versions of DataFusion. This allows
users to write libraries that can interface between each other at runtime rather
than require compiling all of the code into a single executable.
The purpose of this crate is to pass DataFusion runtime structures between
libraries running in the same process without being compiled into a single
executable, such as Python modules that are each a separate dynamically loaded
library.

In general, it is recommended to run the same version of DataFusion by both the
producer and consumer of the data and functions shared across the [FFI], but
this is not strictly required.
Both sides of the [FFI] boundary must use the same major version of this crate.
See [Version Compatibility](#version-compatibility) for details.

See [API Docs] for details and examples.

## Version Compatibility

The interfaces in this crate are **not** stable across major versions of

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure this is correct and the actual usecase of this crate. Please correct me if I am wrong

DataFusion, though they are stable across Rust compiler versions. Both the
producer and consumer of any struct shared across the [FFI] boundary must be
built against the same major version of `datafusion-ffi`.

The `FFI_*` structs can and do change between major versions. Nothing checks the
layout at runtime, so exchanging these structs between libraries built against
different major versions is undefined behavior and can result in a crash or
incorrect results.

When you upgrade DataFusion to a new major version, rebuild every library that
shares structs across the [FFI] boundary against that version.

To detect a mismatch, it is recommended that libraries expose a way to get the
[`datafusion_ffi::version`], and applications that load the library compare the
result with their own.

## Use Cases

Two use cases have been identified for this crate, but they are not intended to
be all inclusive.

1. [`datafusion-python`] which will use the FFI to provide external services such
1. [`datafusion-python`] which uses FFI to provide external services such
as a `TableProvider` without needing to re-export the entire `datafusion-python`
code base. With `datafusion-ffi` these packages do not need `datafusion-python`
as a dependency at all.
2. Users may want to create a modular interface that allows runtime loading of
libraries. For example, you may wish to design a program that only uses the
code base.
2. Users may want to create a modular interface that loads libraries at
runtime. For example, you may wish to design a program that only uses the
built in table sources, but also allows for extension from the community led
[datafusion-contrib] repositories. You could enable module loading so that
users could at runtime load a library to access additional data sources.
Expand Down Expand Up @@ -207,6 +223,7 @@ and it is easy to implement on any struct that implements `Session`.

[apache datafusion]: https://datafusion.apache.org/
[api docs]: http://docs.rs/datafusion-ffi/latest
[`datafusion_ffi::version`]: https://docs.rs/datafusion-ffi/latest/datafusion_ffi/fn.version.html
[rust abi]: https://doc.rust-lang.org/reference/abi.html
[ffi]: https://doc.rust-lang.org/nomicon/ffi.html
[stabby]: https://crates.io/crates/stabby
Expand Down
16 changes: 16 additions & 0 deletions datafusion/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! Stable interfaces for sharing [Apache DataFusion] structures such as
//! `TableProvider`s and user defined functions between libraries loaded at
//! runtime.
//!
//! # Version Compatibility
//!
//! The `FFI_*` structs in this crate are **not** stable across major versions
//! of DataFusion. Both sides of the FFI boundary must be built against the
//! same major version of this crate.
//!
//! You can use [`version`] to determine if a loaded library is compatible. See
//! the [README] for more details.
//!
//! [Apache DataFusion]: https://datafusion.apache.org/
//! [README]: https://github.com/apache/datafusion/blob/main/datafusion/ffi/README.md

#![doc(
html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
Expand Down