DataFusion-DuckLake
A DataFusion extension for reading and writing DuckLake catalogs. DuckLake is an integrated data lake and catalog format that stores metadata in a SQL database and data as Parquet files on disk or object storage.
The goal of this project is to make DuckLake a first-class, Arrow-native lakehouse format inside DataFusion.
This project is maintained by Hotdata with support from the community. Come talk to us on the Hotdata Discord.
- ๐ฆ crates.io: https://crates.io/crates/datafusion-ducklake
- ๐ API docs: https://docs.rs/datafusion-ducklake
- ๐งฉ Feature & backend support: see COMPATIBILITY.md
- ๐ฌ Project chat: DataFusion+DuckLake Discord โ development and usage discussion
- ๐งก Meet the team: Hotdata Discord
Quick start
Add the crate:
The default build includes the DuckDB catalog backend, statically bundled. Other backends and write support are opt-in via feature flags โ see COMPATIBILITY.md for the full matrix.
# Cargo.toml โ read PostgreSQL catalogs
# (for the experimental multi-catalog write path, use features = ["write-postgres"])
[]
= { = "0.5", = ["metadata-postgres"] }
The examples below also use datafusion, object_store, and url directly โ add them
to your [dependencies] as well (this crate does not re-export them). The write example
additionally uses sqlx (with its postgres and runtime-tokio features) to open the
connection pool.
Run a query against an existing PostgreSQL catalog with the bundled example:
(The example also accepts DuckDB, SQLite, and MySQL connection strings with the matching
metadata-* feature โ see COMPATIBILITY.md.)
Reading a catalog
Register a DuckLakeCatalog with a SessionContext and query it with normal SQL as
catalog.schema.table:
use RuntimeEnv;
use *;
use ;
use ObjectStore;
use AmazonS3Builder;
use Arc;
use Url;
// (inside an async fn)
// Read metadata from a PostgreSQL catalog
let provider = new.await?;
// Register object stores for any non-local data (S3 / MinIO)
let runtime = new;
let s3: = new;
runtime.register_object_store;
let catalog = new?;
let ctx = new_with_config_rt;
ctx.register_catalog;
let df = ctx.sql.await?;
df.show.await?;
Writing a catalog
PostgreSQL writes go through the experimental multi-catalog layout described in the
next section โ treat them as a preview. Tables
are created through the writer API; once a table exists, you append to it with SQL
INSERT INTO. (SQL CREATE TABLE / CTAS is not supported on this path โ DataFusion
cannot create the schema, so the first write goes through DuckLakeTableWriter.)
use *;
use MetadataWriter; // set_data_path
use ;
use PgPoolOptions;
use Arc;
let pool = new.connect.await?;
// One-time: bootstrap the multi-catalog tables, then create a named catalog
initialize_multicatalog_schema.await?;
let catalog_id = new.create_catalog.await?;
// Create a table by writing the first batch through the table writer
let writer = new;
writer.set_data_path?;
let object_store: =
new;
let table_writer = new?;
table_writer.write_table.await?; // `batch` is your RecordBatch
// Now append with SQL, reading the same catalog back through MulticatalogProvider
let provider = with_pool.await?;
let catalog = with_writer?;
let ctx = new;
ctx.register_catalog;
ctx.sql.await?.collect.await?;
ctx.sql.await?.show.await?;
Writer output is configurable (Parquet compression, row-group sizing by row count and
byte size). See DuckLakeTableWriter for the
writer options.
Writing to a standard, single-catalog DuckLake store (the spec-compliant layout) is supported today for SQLite via
SqliteMetadataWriter(featurewrite-sqlite), where SQLCREATE TABLE AS SELECTandINSERT INTOboth work. Seetests/sql_write_tests.rs.
Multi-catalog (PostgreSQL, experimental)
A single PostgreSQL metadata store can host multiple independent DuckLake catalogs โ useful for multi-tenant deployments or keeping many logical lakehouses in one database.
โ ๏ธ Experimental and library-specific. This multi-catalog layout is not part of the DuckLake specification and is not (yet) supported or accepted upstream. Catalogs written this way are read back only through this crate's
MulticatalogProviderโ they are not interchangeable with a standard single-catalog DuckLake store. The API and on-disk/in-catalog layout may change. PostgreSQL write support currently depends on this path, so treat it as a preview.
- Create and manage catalogs with
MulticatalogManager(featurewrite-postgres):initialize_multicatalog_schemabootstraps the shared tables, thencreate_catalog, anddrop_table_in_catalogmanage their contents. - Read a specific catalog with
MulticatalogProvider::with_pool(pool, "name")(featuremulticatalog-postgres), which plugs into aDuckLakeCataloglike any other metadata provider.
See examples/multicatalog_write.rs for an end-to-end
walkthrough (bootstrap โ create catalogs โ write โ read back).
Maintenance
The maintenance API handles lakehouse upkeep from Rust: expiring old snapshots,
cleaning up superseded files, and reclaiming orphaned files. The concrete entry points
are backend-gated (write-sqlite / write-postgres). DROP TABLE is available through
MetadataWriter. See
examples/maintenance_demo.rs and
examples/orphan_cleanup_demo.rs.
Compaction
Two explicit, triggered operations on DuckLakeTable rewrite a table's data files
into a better physical layout without changing its logical rows:
merge_adjacent_files(state, MergeOptions)coalesces several small files (of the same schema version) into fewer larger ones. A merged file spanning multiple origin snapshots is written as a DuckLake partial data file (preserving each row's original rowid and origin snapshot), so time travel and change feeds are unaffected.rewrite_data_files(state, RewriteOptions)rewrites a file whose deleted fraction exceeds a threshold (default0.95), dropping its deleted rows.
Both commit atomically in one snapshot and coexist with concurrent appends; superseded
files are scheduled for deletion and reclaimed later by cleanup_old_files. See
examples/compaction_demo.rs.
Compatibility
For the full breakdown of catalog backends, object stores, types, capabilities, and current limitations, see COMPATIBILITY.md.
A few highlights worth knowing up front:
- Reads work on DuckDB, SQLite, PostgreSQL, and MySQL; writes are SQLite/PostgreSQL only.
- Object stores: local filesystem and S3-compatible (S3, MinIO).
- Snapshots can be selected programmatically (
DuckLakeCatalog::with_snapshot), but there is no SQL-level time travel (AS OF) yet, and no partition-based file pruning. - Data inlined by DuckDB's ducklake extension is not read โ see COMPATIBILITY.md for
the
COUNT(*)undercount caveat and how to avoid it.
Project status
This project is in alpha and evolving alongside DataFusion and DuckLake. APIs may change as core abstractions are refined. See CHANGELOG.md for release history. Feedback, issues, and contributions are welcome.