Skip to content

Table Types

Robin Kooyman edited this page Oct 30, 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. The plugin opens its own connections.

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.location The text of LOCATION, such as a URL.
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.

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

  • The table gets no Beacon object store. Open your own connections.
  • 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