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. The plugin opens its own connections. |
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.
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.
- 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.
- 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.
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.