import tempfile
import pyarrow as pa
import jammi
db = jammi.connect(f"file://{tempfile.mkdtemp()}")34 Mutable companion tables
End-to-end create / insert / select / drop on a Jammi mutable table — the OSS primitive for state that needs to live alongside read-only result tables.
When to use this pattern. You need a writable table that sits in the same SQL catalog as your registered sources and embedding tables — for caching enriched rows, holding cursor state, recording user feedback, or any “small table I want to UPDATE / DELETE / INSERT from SQL” workload — without standing up an external Postgres.
34.1 API surface exercised
Session.create_mutable_table(name, *, schema, primary_key, ...)Session.sql("INSERT INTO mutable.public.<name> ...")Session.sql("SELECT ... FROM mutable.public.<name>")Session.drop_mutable_table(name, *, if_exists=False)
The DataFusion namespace for mutable tables is always mutable.public.<name> — distinct from registered sources, which live under <source>.public.<table>, the table named after the file.
The program is cookbook/recipes/mutable_tables/example.py; its steps run here in order.
34.2 Create the table
A mutable table takes an Arrow schema and a primary key. Once created, the catalog resolves it as mutable.public.notes for SQL reads and DML.
schema = pa.schema(
[
pa.field("note_id", pa.int64(), nullable=False),
pa.field("body", pa.string(), nullable=False),
]
)
table_id = db.create_mutable_table("notes", schema=schema, primary_key=["note_id"])
assert table_id == "notes", f"expected 'notes', got {table_id}"34.3 Write and read it with SQL
Rows go in through SQL’s INSERT, and come back through SELECT like any other table’s.
db.sql("INSERT INTO mutable.public.notes (note_id, body) VALUES (1, 'one')")
db.sql("INSERT INTO mutable.public.notes (note_id, body) VALUES (2, 'two')")
db.sql("INSERT INTO mutable.public.notes (note_id, body) VALUES (3, 'three')")
count = db.sql("SELECT COUNT(*) AS n FROM mutable.public.notes").column("n").to_pylist()[0]
assert count == 3, f"expected 3 rows, got {count}"
bodies = db.sql("SELECT body FROM mutable.public.notes ORDER BY note_id").column("body").to_pylist()
assert bodies == ["one", "two", "three"], f"unexpected rows {bodies}"
print(bodies)['one', 'two', 'three']
34.4 Drop it
After drop_mutable_table, SQL no longer resolves the name. With if_exists=True, dropping a table that is already gone does not raise.
db.drop_mutable_table("notes")
try:
db.sql("SELECT COUNT(*) FROM mutable.public.notes")
except RuntimeError as dropped:
print(f"after the drop: {dropped}")
else:
raise AssertionError("post-drop SELECT must raise")
db.drop_mutable_table("notes", if_exists=True)after the drop: DataFusion error: Error during planning: table 'mutable.public.notes' not found
db.close()