Repository navigation
Table Types
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.
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;-
SELECTcallsscanof the table. -
INSERT INTOcallsinsert_intoof the table. -
DELETEandUPDATEare not available on plugin tables. -
DROP TABLE notesremoves the table.
| 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. |
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.
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.
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::ofworks with the session ofcreate,scanandinsert_into. It fails for a session that is not from Beacon. - Beacon resolves the
LOCATIONtext. Give the text toHost. Do not parse it yourself. - A relative
LOCATIONis 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 DataFusionListingTablefinds them. -
Hostaccepts 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
HostApiand build the host withHost::new.
example_memory keeps its rows in memory, so it does not use Host.
- 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.
- Beacon stores each table and calls
createagain at each start.example_memorytherefore starts empty after a restart. - After
UNINSTALL, a query on the table gives the errortable '<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.
A table type runs in a tokio runtime of the plugin, not in the Beacon runtime:
- The SDK runs
create,scan,insert_intoand the output streams in that runtime. - Tokio I/O,
tokio::spawnandtokio::timework there. AddtokiotoCargo.tomlwhen 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 callTableType::with_runtime(handle). Keep the runtime in the factory, and stop it withshutdown_backgroundinDrop.
This is different from a file format, which has no runtime in its calls.
- Use
Hostfor 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.
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.
For Beacon v2.1.0-rc4 (SDK ABI 7, DataFusion 55). Template repository: maris-development/beacon-plugin-template.