Connectors
The server knows no database — your application hands it sources through one contract.
opengrid-server is a library. It checks tokens, narrows what each client may see, adds the
mandatory row filter and speaks HTTP. It does not know where your data lives. Your application
implements Connector (crate opengrid-connector) for whatever holds it — PostgreSQL, a file,
another service, something without a query language — and hands it to the server.
use opengrid_server::{RowFilter, Server, SourcePolicy};
let server = Server::builder()
.source("orders", my_connector, SourcePolicy {
allowed_fields: vec!["id".into(), "customer".into(), "amount".into()],
row_filter: Some(RowFilter::new("tenant_id", "eq", ":tenant")),
})
.token(std::env::var("ORDERS_TOKEN")?, [("tenant", "acme")])
.build()
.await?;
axum::serve(listener, server.router()).await?;
The contract
| Method | Required | Without it |
|---|---|---|
schema() |
yes | — |
capabilities() |
yes | — |
execute(ValidatedQuery) |
yes | — |
pivot(&ValidatedPivotQuery) |
no | one execute per level |
export(&ValidatedQuery, idle_limit) |
no | one execute with limit 0 to count, one to read |
concurrent_exports() |
no | the server picks its own bound |
Methods return BoxFuture — Box::pin(async move { … }) — so the server can hold every source
as Arc<dyn Connector>. A source written against SendDataSource becomes a connector with
FromSource(source).
execute answers with the requested page and total_count, the rows that matched before
offset and limit. The query’s semantics — NULL ordering, binary string comparison, exact
decimals — are in Query semantics; a connector that answers them
differently gives the browser different results from the engine in the tab.
Sources that only hand out rows
A file, a list in memory, a service without a query language: implement RowSource — schema()
and scan(), which hands out the rows in pieces — and wrap it in Rows. The engine then
answers every query on the server, natively: filter, sort, group, aggregate, paging.
use opengrid_connector::Rows;
let server = Server::builder()
.source("events", Rows::new(my_file).max_scan_rows(2_000_000), SourcePolicy::default())
// …
- Each piece holds the stored columns of the schema, in order; derived columns
(
"from": { "part": "year", … }) are computed by the engine. scangets the query’s filter, the row filter already in it. It is a hint: a source may use it to hand out fewer rows; the engine applies the filter again either way.- The rows stream: the server keeps only what the answer needs — the best
offset + limitrows for a page, the partial sums and counts per group for a grouping, a counter fortotal_count. Memory follows the answer, not the source. max_scan_rows(default 1 000 000) bounds the rows or groups one answer has to hold — every match of a query without a page, say. More is a413(limit_exceeded) that says so. How long a scan may take is the server’s timeout; the scan gives the runtime its turn between pieces, so the timeout fires even for a source whose pieces are always ready.
Measured (2026-09-28, Apple M4 Pro, release build, four columns generated on the fly — the time
includes making the rows; cargo run --release -p opengrid-connector --example rows_bench):
| Rows | Page (filter, sort, 50) | Group by country, sum + avg | Count of a filter |
|---|---|---|---|
| 1 000 000 | 0.15 s, 28 MiB | 0.17 s, 27 MiB | 0.14 s, 28 MiB |
| 10 000 000 | 1.3 s, 31 MiB | 1.6 s, 28 MiB | 1.4 s, 28 MiB |
| 100 000 000 | 13 s, 75 MiB | 16 s, 41 MiB | 14 s, 40 MiB |
Reading every row into one table first, as the tier did before, took 65 MiB for a million rows and 452 MiB for ten million — and would take about 4.5 GiB for a hundred million.
Proving a connector
opengrid-conformance runs the whole suite — every rule of the query semantics, one case each —
against any source. Load the fixture into yours, then in a test:
use opengrid_conformance::{check_source, fixture_csv, fixture_schema};
use opengrid_connector::AsSource;
// load fixture_csv() into your database under fixture_schema() first
let report = check_source(&AsSource(&my_connector)).await;
report.assert_ok(); // panics with one line per case that differs
All cases agree means the browser gets the same answers from your source as from the engine in the tab. The PostgreSQL, local-engine and rows-tier references run exactly this in their tests.
What a connector never has to do
Security. Before a query reaches the connector, the server has checked the token, validated the
query against the schema narrowed to allowed_fields, added the row filter with the value from
the token’s context, and validated the result against the full schema. A connector only ever
sees a ValidatedQuery that already carries every rule — it cannot forget one.
Names. A column may not start with __og_ or __grouping_: the connectors build helper columns
under those prefixes, and Schema::check refuses such a field. A PostgreSQL table in a schema is
PostgresDataSource::connect_in_schema(url, "sales", "orders", schema); a dot inside the table
name given to connect is part of the name.
Errors. A connector fails with the kind that is true: DataSourceError::Backend for an outage
(the caller never sees its text, only a reference), LimitExceeded when the caller can narrow the
query, Data when the data breaks a rule the query relies on (a tree with a key twice or a
cycle). The server tells the caller the message of the last two; give those no database text.
ServerBuilder::on_error receives the cause of every backend failure and every refused token.
One rule a connector must carry out itself: for a tree, the tenant rule is only in
tree.scope, not in query.filter (a filter would turn the tree into a filtered one — T5 shows
the ancestors of a match as context, and a flat export grows a match column). A connector that
reports the capability tree therefore has to apply tree.scope to every row it reads, as the
SQLite and PostgreSQL references do; one that honours only query.filter leaks other tenants’
rows. A connector without the capability is asked for the rows of the scope through
Connector::tree, whose default applies it.
Startup. Server::builder().build() checks every token against every source’s row filter: the
context value is read as the filtered column’s type ("7" for a bigint tenant), and a value of
another type, a key the token does not carry or an operator the column cannot answer is a build
error, as are a token value handed in twice and an allow_origin that is not a valid header
value.
Bounds. A pivot’s grouping sets arrive with a limit one past max_cells: a connector applies
limit like any other query’s and lets the pivot engine report the overflow (a connector that
answers a whole pivot in one statement, as PostgreSQL does, bounds the statement by the sum). A
tree the engine builds is counted first (max_tree_rows, default 5 000 000); a connector that answers trees itself
keeps the rows in the database.
Timeouts. The server drops a query’s future when its timeout runs out or the client leaves. A connector must then end the work behind it, not only forget the future: the PostgreSQL reference sends a cancel request for the connection and discards it, the SQLite reference interrupts the statement (it runs on a thread of its own, one query after the other). A connector that leaves a statement running holds a database connection and its CPU for as long as it likes.
PostgreSQL pool. PostgresDataSource::connect_with(url, table, schema, PoolOptions { max_size, wait_timeout, create_timeout, recycle_timeout }) sizes and times the pool; size
max_concurrent_exports against pool_size().
PostgreSQL over TLS. Without the tls feature the connection is made in clear text, and a
connection string with sslmode=require is refused at connect (use connect_with_tls). With
connect_with_tls(url, table, schema, PoolOptions::default(), tls) the connection requires TLS:
the string’s sslmode=prefer, libpq’s default, would fall back to clear text when the server does
not answer TLS, so it is lifted to require, and sslmode=disable is refused as a contradiction.
tls is any tokio-postgres TLS connector (postgres-native-tls, or your own); the cancel request
of a dropped statement uses the same one. The cargo feature tls of opengrid-datasource-postgres
(off by default) adds rustls_connector(extra_roots_pem): rustls with the ring provider, which
builds without system packages, trusting the Mozilla root certificates and, if you give them, the
roots of a private certificate authority (PEM). The server certificate is verified against the host
name of the connection string. The server and the examples do not enable the feature; a program that
builds its sources does.
A server in another language
The Rust contract helps Rust programs. A server written in .NET, Java or Node implements the
HTTP protocol instead, and proves it with the same suite over HTTP:
opengrid-conformance --endpoint <url> --token <token>.
Reference connectors
- PostgreSQL (
opengrid-datasource-postgres): the pivot as oneGROUPING SETSstatement, the export through a cursor. - SQLite (
opengrid-connector-sqlite): a file, no installation — SQLite is compiled in.create_tableandinsertwrite a table in the layout it reads: decimals as scaled integers, dates as days, timestamps as microseconds, NaN as the text'NaN'. It answers every conformance case. - A table in memory (
opengrid_connector::LocalConnector,from_csvfor a file).
A tree in the database, or in the engine
A tree’s level (rules T1–T7) is answered by default the way every source answers it: the
connector hands the rows of the tree’s scope to the engine, which builds the level. Both SQL
references can answer it themselves instead, with WITH RECURSIVE —
SqliteConnector::with_tree_pushdown(true), PostgresDataSource::with_tree_pushdown(true) —
and then report the capability tree. A flat tree (the export, T8) stays the engine’s either
way: its answer is the whole visible tree.
The pushdown is off by default because it is slower where the rows are close. A tree’s rules need the whole tree for every level — a key twice (T1), a cycle (T3), the matches with their ancestors (T5) — and the engine does that in one pass over rows it read once. Measured on a generated tree of 100 000 nodes, one level of 50 nodes, the median of five, PostgreSQL on the same machine:
| the engine’s way | pushed down | |
|---|---|---|
| SQLite, the roots | 74 ms | 900 ms |
| SQLite, a wide level with a sum | 76 ms | 961 ms |
| PostgreSQL, the roots | 168 ms | 540 ms |
| PostgreSQL, a wide level with a sum | 170 ms | 474 ms |
So it is worth it only where moving the scope’s rows costs more than that — a large tree over a
slow link to its database. cargo test --release -p opengrid-server --test tree -- --ignored --nocapture measures it again on any machine.
They are examples, not a list of supported databases: anything that can answer the contract
is one. cargo run -p opengrid-example-server -- demo-sqlite serves the demo from a SQLite
file.
The contract is frozen with its first release and grows only by methods with a default.