Skip to content

Add CREATE SCHEMA, DROP SCHEMA, DROP TABLE, and RENAME TABLE to execute_ducklake_sql - #362

Open
faysou wants to merge 1 commit into
datafusion-contrib:mainfrom
faysou:ducklake-catalog-ddl-sql-pr
Open

faysou wants to merge 1 commit into
datafusion-contrib:mainfrom
faysou:ducklake-catalog-ddl-sql-pr

Conversation

@faysou

@faysou faysou commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add CREATE SCHEMA, DROP SCHEMA, DROP TABLE, and ALTER TABLE ... RENAME TO to
execute_ducklake_sql. SQLite and experimental multicatalog PostgreSQL apply each effective
operation as one metadata-only transaction; IF EXISTS and IF NOT EXISTS no-ops create no
snapshot, and non-empty schema drops fail without changing the head.

Table rename now writes a new ducklake_table version with the same table ID, schema, path, and
optional UUID. The explicit SQLite layout migration removes a legacy table primary key
without changing its table IDs. Drop ends the live table and its dependent metadata.

Catalog DDL flow

flowchart LR
    SQL["Catalog DDL SQL"] --> Parse["DuckDB dialect parser"]
    Parse --> Writer["MetadataWriter catalog operation"]
    Writer --> Lock["SQLite transaction or PostgreSQL catalog lock"]
    Lock --> Snapshot["One schema-changing snapshot"]
    Snapshot --> Version["Version schema or table metadata"]
    Version --> Readers["Historical and current DataFusion or DuckDB readers"]
Loading

Supported contract

Surface Behavior
MetadataWriter::create_schema Create one schema with a fresh catalog-wide ID and schema-changing snapshot; return no snapshot for IF NOT EXISTS.
MetadataWriter::drop_schema End one empty schema; reject non-empty schemas; return no snapshot for a missing IF EXISTS target.
MetadataWriter::drop_table End the live table, partition and sort generations, columns, tags when present, data files, and delete files.
MetadataWriter::rename_table End the old table row and insert a new name with the same ID, path, schema, and optional UUID.
execute_ducklake_sql Parse one qualified or unqualified catalog DDL statement with normalized quoted and unquoted identifiers.
Legacy SQLite catalogs Run SqliteMetadataWriter::migrate_to_official_layout before the first rename if ducklake_table.table_id is a primary key.
PostgreSQL multicatalog Serialize each operation on the catalog row and keep schema/table lookup scoped to that catalog.

Catalog layout

  • A rename preserves the table ID, path, and earlier name by writing a new version of the catalog row.
  • PostgreSQL multicatalog stores at layout version 2 or earlier need an explicit upgrade through
    migrate_multicatalog_schema. Stop older processes first. Layout version 3 preserves the
    identity sequence and enforces one live row per table ID.
  • A legacy SQLite catalog with a primary key on ducklake_table.table_id needs
    SqliteMetadataWriter::migrate_to_official_layout before a rename. Newly created catalogs already
    support versioned table rows.

Commit and branch

  • Commit: 0480600.
  • Base: 24d9751.
  • Branch: faysou:ducklake-catalog-ddl-sql-pr.

Verification

  • cargo fmt --all -- --check and all-feature Clippy pass.
  • Five catalog DDL integration tests pass, including DuckDB 1.5.5 readback after rename
    and drop, and a live PostgreSQL migration followed by a rename with the same table ID.
  • The PostgreSQL layout version 1 migration test passes for both layouts. The multicatalog
    migration also passes the test that holds locks on every table it does not need to change.

Specification proof

DuckLake rule Implementation invariant Discriminating proof
CREATE SCHEMA inserts a versioned schema with a fresh ID. SQLite inserts the schema under a fresh key, increments schema_version, and records created_schema in one transaction; PostgreSQL performs the equivalent catalog-scoped commit. The focused test proves one snapshot and no snapshot for IF NOT EXISTS; the official DuckDB oracle sees exactly one Rust-created rust_schema.
DROP SCHEMA ends the schema row and permits only empty schemas. Both backends resolve the live schema under their write lock, reject any live table, and set end_snapshot only after validation. A non-empty drop errors with an unchanged head; after table drop, the schema ends in one snapshot, and missing IF EXISTS is a no-op.
DROP TABLE ends the table, partition info, columns, column tags, data files, delete files, and table tags. One drop snapshot ends all live table-owned rows; sort generations also end, and optional tag tables are handled when present. Raw SQLite assertions compare the exact drop snapshot across all seven specified categories; the official DuckDB catalog no longer exposes the table or schema.
Renaming tables changes only the visible name while prior snapshots retain the old name. Rename ends the live row and inserts a new generation with the same table_id, schema, path, and optional UUID. Current lookup resolves only archived_events; pre-rename lookup resolves only events; raw SQLite and PostgreSQL rows have the same exact table ID and adjacent bounds.
ducklake_table defines table visibility by begin_snapshot and exclusive end_snapshot. SQLite removes the incompatible row primary key and adds one-live-ID and one-live-name indexes; PostgreSQL applies the same live uniqueness after its migration. The legacy migration test preserves table ID 42, UUID, schema, name, and begin snapshot while proving table_id is no longer the row primary key.
Snapshot creation increments schema_version for schema changes. Every effective catalog DDL creates one snapshot and advances the schema version; no-ops and rejected operations leave no snapshot. Each SQLite snapshot copies the preceding allocator watermarks. Exact head assertions distinguish create, rename, drop, no-op, and rejected commits on SQLite and PostgreSQL; DuckDB accepts every Rust-written SQLite head.

Reference implementation

Schema creation and drop dispatch match
ducklake_catalog.cpp,
including no-op conflict handling and dependency validation. The branch now matches the
extension's
schema emptiness check
by rejecting a drop while a live table, view, or macro remains. Table rename keeps the stable
identity as in
ducklake_table_entry.cpp,
and the seven stable tombstones match
DropTables.
The branch deliberately omits CASCADE: the stable specification permits DROP SCHEMA only for
an empty schema.

@faysou faysou changed the title Add DuckLake SQL catalog DDL Add CREATE SCHEMA, DROP SCHEMA, DROP TABLE, and RENAME TABLE to execute_ducklake_sql Oct 5, 2026
@faysou
faysou force-pushed the ducklake-catalog-ddl-sql-pr branch 2 times, most recently from da178d5 to a9d258a Compare October 5, 2026 21:38
@faysou
faysou force-pushed the ducklake-catalog-ddl-sql-pr branch from a9d258a to 1819dee Compare November 23, 2026 15:00
…te_ducklake_sql

Handle the four statements through execute_ducklake_sql on SQLite and the PostgreSQL multicatalog writer. A rename keeps the table's stable id and writes a second ducklake_table row, so table_id is no longer the row primary key; a migration drops the old key on existing catalogs. A schema drop is refused while live tables, views, or macros remain. Each statement is one snapshot that bumps schema_version and records its change in the snapshot ledger. SQLite snapshots copy the preceding allocator watermarks, and a new schema takes an explicit id, so DuckDB reads catalogs it created.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant