If you have a big Delta table that was partitioned years ago, you probably know the feeling. Half the partitions are tiny, a few are huge, and queries that filter on anything other than the partition column scan far more than they should. Liquid clustering fixes most of that, but until recently getting there meant a full CREATE OR REPLACE ... AS SELECT and a nervous weekend.
On Databricks Runtime 18.1 and above there's a much gentler path: ALTER TABLE ... REPLACE PARTITIONED BY WITH CLUSTER BY. Here's how I'd use it on a real table, step by step.
Step 1: Look at what you have
DESCRIBE DETAIL main.sales.events; -- check partitionColumns, numFiles, sizeInBytes
DESCRIBE HISTORY main.sales.events; -- note the current version before you touch anything
Write down that version number. It's your way back.
Step 2: Pick clustering keys
The docs give a simple rule: start with your partition columns, and add any ZORDER columns you were using. If you partitioned on a generated column like event_date derived from event_ts, cluster on the original column instead of the generated one.
If the access patterns are all over the place and this is a Unity Catalog managed table, CLUSTER BY AUTO starts from the current partition columns and lets predictive optimization adjust the keys over time.
Step 3: Convert
-- keep the partition columns as clustering keys
ALTER TABLE main.sales.events REPLACE PARTITIONED BY WITH CLUSTER BY;
-- or choose your own keys
ALTER TABLE main.sales.events REPLACE PARTITIONED BY WITH CLUSTER BY (event_date, customer_id);
-- or let Databricks manage them (UC managed tables only)
ALTER TABLE main.sales.events REPLACE PARTITIONED BY WITH CLUSTER BY AUTO;
One gotcha: if a partition column is a timestamp and you want it as a clustering key, the command fails because it can't auto-generate stats for timestamps. Turn stats generation off for the conversion and compute them afterwards:
SET spark.databricks.delta.liquidConversion.statsGeneration.enabled = false;
ALTER TABLE main.sales.events REPLACE PARTITIONED BY WITH CLUSTER BY (event_ts, customer_id);
ANALYZE TABLE main.sales.events COMPUTE DELTA STATISTICS;
Step 4: Actually cluster the data
Converting changes the table's layout definition. The data only gets organized when OPTIMIZE runs:
OPTIMIZE main.sales.events;
On a large table, schedule this off-peak the first time. If predictive optimization is on, it takes over from there.
Step 5: Check it worked
DESCRIBE EXTENDED main.sales.events; -- should list the new clustering columns
DESCRIBE HISTORY main.sales.events; -- look for REORG, UPGRADE PROTOCOL and REPLACE PARTITIONED BY WITH CLUSTER BY
Then run one of your usual slow queries and compare files read in the query profile with the run before.
What happens to jobs using the table
Batch reads keep working during conversion on any runtime.
Batch writes have no downtime on DBR 15.4 and above. On older runtimes, pause writers and restart them after.
Streams with schema tracking just need a restart (sometimes a couple). Streams without it will throw, so plan a restart.
Streaming tables and materialized views created in Lakeflow pipelines can't be converted this way. Change PARTITIONED BY to CLUSTER BY in the pipeline definition instead.
If you need to back out
If nothing important was written since, you can restore to the version you noted in step 1:
ALTER TABLE main.sales.events CLUSTER BY NONE;
TRUNCATE TABLE main.sales.events;
RESTORE TABLE main.sales.events TO VERSION AS OF 1234;
Run the RESTORE straight after the TRUNCATE so VACUUM can't remove the files you need, and keep writers stopped until it finishes. If you set hierarchical clustering columns, unset delta.liquid.hierarchicalClusteringColumns first. If you need to keep newer writes, recreate the partitioned table with a CTAS instead.
Takeaways
- On DBR 18.1+, REPLACE PARTITIONED BY WITH CLUSTER BY converts in place, so no hand-written CTAS migration.
Start with partition columns plus old ZORDER columns as keys, or use AUTO on UC managed tables.
Timestamp partition columns need the stats-generation setting, then ANALYZE.
Nothing gets reorganized until OPTIMIZE runs.
Write down the table version before you start. It turns a scary change into an undoable one.
Reference: https://docs.databricks.com/aws/en/delta/clustering