Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Query Your Data with SQL

Measured companion: for the long-form, executed-and-measured Python treatment, see The Cookbook → Constructing the Graph.

Register data files as named sources, then query them with full SQL. Sources are persisted in the catalog and survive session restarts.

Register a source

Rust

#![allow(unused)]
fn main() {
extern crate jammi_db;
extern crate jammi_ai;
extern crate tokio;
use jammi_ai::session::InferenceSession;
async fn ex(session: &InferenceSession) -> jammi_db::error::Result<()> {
use jammi_db::source::{FileFormat, SourceConnection, SourceType};

session.add_source("patents", SourceType::File, SourceConnection {
    url: Some("file:///data/patents.parquet".into()),
    format: Some(FileFormat::Parquet),
    ..Default::default()
}).await?;
Ok(()) }
}

Python

db.add_source("patents", url="/data/patents.parquet", format="parquet")

CLI

jammi sources add patents --url /data/patents.parquet --format parquet

Supported formats

FormatRustPython/CLINotes
ParquetFileFormat::Parquet"parquet"Columnar, compressed, recommended for large datasets
CSVFileFormat::Csv"csv"Auto-detected schema
JSONFileFormat::Json"json"Line-delimited JSON
JSON LinesFileFormat::JsonLines"jsonl" or "ndjson"Same line-delimited reader as JSON; .jsonl preferred, .ndjson used when no .jsonl files match. Resolved once at registration and pinned — a later directory change never flips it.

Run a SQL query

Sources are accessible via three-part SQL names: <source_id>.public.<table_name>. The table name is derived from the file name (e.g., patents.parquet becomes patents).

Rust

#![allow(unused)]
fn main() {
extern crate jammi_db;
extern crate jammi_ai;
extern crate tokio;
use jammi_ai::session::InferenceSession;
async fn ex(session: &InferenceSession) -> jammi_db::error::Result<()> {
let results = session.sql(
    "SELECT id, title, year FROM patents.public.patents WHERE year > 2020 ORDER BY year"
).await?;

for batch in &results {
    println!("{batch:?}");
}
Ok(()) }
}

Python

table = db.sql("SELECT id, title, year FROM patents.public.patents WHERE year > 2020 ORDER BY year")
print(table)

CLI

jammi query "SELECT id, title, year FROM patents.public.patents WHERE year > 2020 ORDER BY year"

Materialize a query as a table

CREATE TABLE <name> AS <query> materializes the query’s rows as a result table — the same kind of table an embedding or an as-of join produces: bytes on the object store under the store’s root, a result_tables row of the statement kind carrying the table’s attestation, visible to every replica through the catalog. Read it as "jammi.<name>" (every result table’s relation), on any replica, from the moment the statement returns:

CREATE TABLE recent AS SELECT id, title FROM patents.public.patents WHERE year >= 2022;
SELECT id, title FROM "jammi.recent" ORDER BY id;
DROP TABLE recent;

The query and the write run in the process that received the statement. IF NOT EXISTS leaves an existing table as it is; a name already taken is refused otherwise.

CREATE OR REPLACE TABLE <name> AS <query> never loses the table it replaces. The new rows are built under a row of their own while the old table keeps serving under the name — on every replica — and once the new artifact is complete and attested, one catalog transaction removes the old row and moves the new one onto the name; only then are the old bytes reclaimed. A reader resolves the old table or the new one, never none: a replica whose binding of the name predates the swap rebinds at its next resolution, and a read already in flight finishes over the bytes it opened. A failure anywhere before that transaction — the query refused at planning, the input refusing mid-write — leaves the old table exactly as it was and discards the replacement’s row and bytes. A process that dies mid-replacement leaves the same: recovery reaps a replacement nobody is driving and never publishes it. OR REPLACE on a name nothing is under is a plain CREATE.

DROP TABLE <name> is the store’s drop of the result table under your tenant: the catalog row and its segment and version rows go, then the binding on the replica that ran it (every other replica drops its own at its next resolution, which finds no row), then every object the row referenced. A table another writer is still building is refused; IF EXISTS makes an absent table a no-op.

The table records its query as SQL, so it is a producer the engine replays: recompute(name) re-runs the query over the sources’ current rows as a CREATE OR REPLACE under the same name — so a recompute that fails keeps the table it was refreshing — and records fresh anchors on the relations it scans (see Materialization contract).

CREATE TABLE <name> (<columns>) — a column list and no query — is refused: a result table is what a query produced, and there are no empty ones. So is a qualified name (a.b): a result table is named by one identifier. So is a query the engine cannot render back to SQL — a WITH RECURSIVE query, a VALUES list — since the table could never replay; the refusal names the node.

Aggregations

SELECT category, COUNT(*) as count, AVG(citation_count) as avg_citations
FROM patents.public.patents
WHERE year > 2020
GROUP BY category
ORDER BY count DESC

Joins across sources

Register multiple sources and join them in a single query:

Rust

#![allow(unused)]
fn main() {
extern crate jammi_db;
extern crate jammi_ai;
extern crate tokio;
use jammi_ai::session::InferenceSession;
use jammi_db::source::{FileFormat, SourceConnection, SourceType};
async fn ex(session: &InferenceSession) -> jammi_db::error::Result<()> {
session.add_source("companies", SourceType::File, SourceConnection {
    url: Some("file:///data/companies.csv".into()),
    format: Some(FileFormat::Csv),
    ..Default::default()
}).await?;

let results = session.sql("
    SELECT p.title, c.company_name
    FROM patents.public.patents p
    JOIN companies.public.companies c ON p.assignee_id = c.id
").await?;
Ok(()) }
}

Python

db.add_source("companies", url="/data/companies.csv", format="csv")

table = db.sql("""
    SELECT p.title, c.company_name
    FROM patents.public.patents p
    JOIN companies.public.companies c ON p.assignee_id = c.id
""")

Source lifecycle

Rust

#![allow(unused)]
fn main() {
extern crate jammi_db;
extern crate jammi_ai;
extern crate tokio;
use jammi_ai::session::InferenceSession;
async fn ex(session: &InferenceSession) -> jammi_db::error::Result<()> {
// List registered sources
let sources = session.catalog().list_sources().await?;

// Remove a source
session.remove_source("patents").await?;
Ok(()) }
}

CLI

jammi sources list

Sources persist in the SQLite catalog at <artifact_dir>/catalog.db. Registering the same source ID twice returns an error — remove it first.

Execution plans

Use EXPLAIN (or the CLI explain command) to see how DataFusion will execute your query:

jammi explain "SELECT * FROM patents.public.patents WHERE year > 2020"