Python backend contract¶
Python-defined database backends implement fsspec_db.AbstractDatabase. An
AbstractDatabaseFileSystem wraps the object and delegates filesystem behavior through the
Rust DatabaseFs bridge.
Contract¶
AbstractDatabase mirrors the Rust Database trait method set, except for Rust-only default
methods such as arrow_extraction. The object must be safe to call from the bridge while the
Python GIL is held.
Method |
Return value |
Contract |
|---|---|---|
|
|
One of |
|
|
All schemas visible to the backend. |
|
|
Tables and views in |
|
|
Columns for |
|
|
Indexes for |
|
|
Constraints for |
|
|
Metadata for one table or view. |
|
|
SQL text for one view. |
|
|
Query result for SQL plus optional scalar bind parameters. |
|
|
Number of rows inserted from |
Metadata Objects¶
Backends return the metadata classes exported by fsspec_db.
Class |
Constructor |
|---|---|
|
|
|
|
|
|
|
|
|
|
RelationInfo.kind is table or view. ConstraintInfo.kind is pk, fk, unique, or
check.
Query Boundary¶
query(sql, params=None) returns a pyarrow.Table. params contains values representable by
the bridge scalar type set: None, bool, int, float, str, and bytes.
AbstractDatabaseFileSystem.query() and open_query() call the backend’s query() through the
Rust bridge. Path reads such as /main/users.arrow?columns=id&limit=10 generate SQL in Rust and
then call the same backend method.
Insert Boundary¶
insert(schema, relation, table, mode="append") receives a pyarrow.Table decoded from a
relation data file. mode values are:
Mode |
Semantics |
|---|---|
|
Add rows to the existing relation. |
|
Replace relation rows before inserting table. |
The method returns the number of inserted rows.
Exceptions¶
The bridge maps common Python exceptions to fsspec/Rust error categories.
Python exception |
Meaning |
|---|---|
|
Missing schema, relation, or item. |
|
Permission-denied operation. |
|
Existing item for create semantics. |
|
Directory operation on a file. |
|
File operation on a directory. |
|
Invalid argument or unsupported value. |
Other exceptions |
Generic backend error. |
Filesystem wrappers¶
AbstractDatabaseFileSystem(db, **storage_options) and
PyDatabaseFileSystem(db, **storage_options) adapt a concrete AbstractDatabase.
AbstractDatabaseFileSystem routes paths through Rust DatabaseFs.
PyDatabaseFileSystem implements the path mapping directly in Python and does
not call the Rust bridge.
The backend must execute SQL generated for path projection, predicates, limits,
and offsets, as well as SQL supplied directly to query. Returning an unchanged
in-memory table for every query does not satisfy that contract.
Metadata must describe actual relation state. Insert behavior, rollback, and
connection lifetime remain backend responsibilities. Existing implementations in
fsspec_db.duckdb, fsspec_db.sqlalchemy, and fsspec_db.odbc are concrete adapters.