Skip to content

Table Types

Robin Kooyman edited this page Dec 2, 2026 · 2 revisions

A table type lets a table read from and write to another system, such as a remote catalog, a web service or a database. The user makes a table with CREATE EXTERNAL TABLE ... STORED AS <type>.

The template has one example: example_memory in native/src/memory.rs. It keeps the rows of a table in memory.

Use a table type

CREATE EXTERNAL TABLE notes (id BIGINT, note VARCHAR)
STORED AS example_memory LOCATION 'memory' OPTIONS ('partitions' '2');

INSERT INTO notes VALUES (1, 'first'), (2, 'second');
SELECT * FROM notes;
  • SELECT calls scan of the table.
  • INSERT INTO calls insert_into of the table.
  • DELETE and UPDATE are not available on plugin tables.
  • DROP TABLE notes removes the table.

Table type or file format?

Use When
File format The data is in files in a Beacon store, such as local disk or S3. Beacon lists and reads the files.
Table type The data is in another system, or in a layout that a file format cannot read. The table code reads and writes the data itself.

Write a table type

A table type is a DataFusion TableProviderFactory with a name.

use beacon_plugin_sdk::datafusion::catalog::{Session, TableProvider, TableProviderFactory};
use beacon_plugin_sdk::datafusion::datasource::MemTable;
use beacon_plugin_sdk::datafusion::logical_expr::CreateExternalTable;
use beacon_plugin_sdk::table_type::{TableType, table_option};

#[derive(Debug)]
struct MemoryFactory;

#[async_trait]
impl TableProviderFactory for MemoryFactory {
    async fn create(
        &self,
        _state: &dyn Session,
        cmd: &CreateExternalTable,
    ) -> Result<Arc<dyn TableProvider>> {
        if cmd.schema.fields().is_empty() {
            return plan_err!("example_memory tables need a column list");
        }
        let partitions = match table_option(cmd, "partitions") {
            None => 1,
            Some(text) => /* parse and check the value */,
        };
        let table = MemTable::try_new(Arc::clone(cmd.schema.inner()), vec![vec![]; partitions])?;
        Ok(Arc::new(table))
    }
}

pub fn tables() -> Vec<TableType> {
    vec![TableType::new("example_memory", Arc::new(MemoryFactory))]
}

beacon_plugin_sdk::export_plugin!(scalar = functions, formats = tsv::formats, tables = memory::tables);

create gets the CREATE EXTERNAL TABLE statement in cmd:

Field Contents
cmd.name The table name.
cmd.locations The text of LOCATION. It has exactly one entry. Beacon refuses a plugin table with more than one LOCATION.
cmd.options The OPTIONS. Read them with table_option(cmd, key).
cmd.schema The column list. It is empty when the statement has no column list.
cmd.table_partition_cols The columns of PARTITIONED BY.

create returns a TableProvider. Implement these methods of the provider:

Method Use
schema The columns of the table.
scan Reads the rows for SELECT. It gets the projection, the filters and the limit.
supports_filters_pushdown Tells Beacon which filters scan applies. Inexact lets Beacon apply a filter again.
insert_into Writes the rows of INSERT INTO. The DataFusion default refuses the write.

The example uses the DataFusion MemTable, which implements all of them.

Options

DataFusion adds the prefix format. to an option key without a .. So OPTIONS ('partitions' '2') arrives as format.partitions. table_option(cmd, "partitions") reads both forms.

Beacon stores LOCATION and OPTIONS as plain text in the table definition. Do not put a secret in them that the storage must not see.

Read the Beacon object stores

Host gives the table code the object stores of Beacon. Get it from the session with Host::of(state):

use beacon_plugin_sdk::host::Host;
use beacon_plugin_sdk::object_store::store::ObjectStoreExt;
use beacon_plugin_sdk::object_store::store::path::Path;

let host = Host::of(state)?;
let open = host.open(&cmd.locations[0])?;
let bytes = open.store.get(&Path::from(open.path)).await?.bytes().await?;
Method Gives
host.open(location) The store and the path. It fails for a path of the local machine.
host.listing_url(location) A DataFusion ListingTableUrl. Use it for a ListingTable, or for a location with a glob.
host.resolve(location) The store URL and the path. It opens nothing.
host.datasets_base_url() The location of the datasets store, such as s3://bucket/.
  • Host::of works with the session of create, scan and insert_into. It fails for a session that is not from Beacon.
  • Beacon resolves the LOCATION text. Give the text to Host. Do not parse it yourself.
  • A relative LOCATION is a path in the datasets store. datasets:// is the URL of the datasets store.
  • runtime_env().object_store(url) also gives the Beacon stores, so a DataFusion ListingTable finds them.
  • Host accepts each scheme and each absolute local path. Refuse the locations that your users must not read.
  • A store that the plugin registers changes only the plugin. It cannot replace a store of Beacon.
  • In a test, implement HostApi and build the host with Host::new.

example_memory keeps its rows in memory, so it does not use Host.

Names

  • A table type name has lowercase letters, digits and _ only.
  • It must not be a built-in table type, a file format or a table type of another plugin.
  • Start the name with the plugin name.

Restart and remove

  • Beacon stores each table and calls create again at each start. example_memory therefore starts empty after a restart.
  • After UNINSTALL, a query on the table gives the error table '<table>' needs plugin '<plugin>', and the plugin is uninstalled.
  • When the plugin is not installed at start, Beacon skips the table and keeps its definition. Install the plugin and restart the server to use the table again.

The runtime

A table type runs in a tokio runtime of the plugin, not in the Beacon runtime:

  • The SDK runs create, scan, insert_into and the output streams in that runtime.
  • Tokio I/O, tokio::spawn and tokio::time work there. Add tokio to Cargo.toml when you use it.
  • Each Beacon instance in a process gets its own runtime. Do not keep the state of one instance in a static.
  • To set the runtime yourself, such as its thread count, build it in tables() and call TableType::with_runtime(handle). Keep the runtime in the factory, and stop it with shutdown_background in Drop.

This is different from a file format, which has no runtime in its calls.

Rules for table code

  • Use Host for data in a Beacon store. Open your own connections for other systems.
  • Filters arrive as DataFusion expressions. Beacon sends only the filters that use DataFusion functions. It applies the other filters itself.
  • Return errors. The SDK changes a panic in a call, a future or a stream to a query error.

Test a table type

Put the factory in a DataFusion SessionContext under its upper-case name. Then run SQL:

fn session() -> SessionContext {
    let ctx = SessionContext::new();
    let state = ctx.state_ref();
    let mut state = state.write();
    for table_type in tables() {
        let name = table_type.name.to_ascii_uppercase();
        state.table_factories_mut().insert(name, table_type.factory);
    }
    drop(state);
    ctx
}

See the tests in native/src/memory.rs and Testing.

Clone this wiki locally