Convert a partitioned table to liquid clustering
In Databricks Runtime 18.1 and above, to convert an existing partitioned Delta Lake table to liquid clustering, use REPLACE PARTITIONED BY WITH CLUSTER BY in an ALTER TABLE statement. Conversion minimizes reader and writer downtime and supports both external and managed tables. After conversion, the table supports reads with Databricks Runtime 13.3 LTS and above.
For managed Apache Iceberg tables, conversion is not necessary because these tables use partition definitions as liquid clustering keys. Running the conversion command raises an error.
The benefits of converting partitioned tables to liquid clustering include:
- Performance improvements for tables that suffer from poor data-skipping or over-partitioning.
- Automatic performance improvements, using
CLUSTER BY AUTO, for tables with frequently changing query patterns. - Clustering columns are flexible and simple to alter, whereas partitioning is rigid and difficult to alter.
- Reduced write conflicts because tables with liquid clustering allow for row-level concurrency. See Row-level concurrency.
Syntax
ALTER TABLE <table_name>
REPLACE PARTITIONED BY WITH CLUSTER BY [( <clustering_columns> ) | AUTO]
The CLUSTER BY clause supports the following options:
( <clustering_columns> ): Specifies new clustering columns. Databricks recommends keeping the new clustering columns similar to the original partition columns. Using very different columns triggers a large reclustering operation on the firstOPTIMIZErun.AUTO: Uses the current partition columns as the initial clustering columns and lets predictive optimization adapt over time. Only available for Unity Catalog managed tables. See Use automatic liquid clustering.- No options specified: Uses the current partition columns as the new clustering columns.
For guidance on choosing clustering keys when migrating from partitioned tables, see Choose keys when you migrate from partitioning or Z-order.
When you convert a partitioned table and keep the partition columns as clustering keys, those columns become hierarchical clustering keys. See Prioritize clustering keys with hierarchical clustering.
Examples
To cluster on different columns than the original partitions, such as for a table partitioned on (year, month, day), do the following:
ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (day, id);
OPTIMIZE t1;
To benefit from altering clustering columns, you must run OPTIMIZE.
To use automatic liquid clustering and start with the current partition columns, do the following:
ALTER TABLE t2 REPLACE PARTITIONED BY WITH CLUSTER BY AUTO;
To keep the current partition columns as clustering columns, do the following:
ALTER TABLE t3 REPLACE PARTITIONED BY WITH CLUSTER BY;
Handle concurrent reads and writes during conversion
After conversion, Databricks Runtime 13.3 LTS and above is supported for reads and writes. Databricks recommends Databricks Runtime 15.4 LTS and above for workloads that read or write to the table during conversion.
See the following table for how to handle concurrent read and write workloads during conversion:
Workload type | Reads during conversion | Writes during conversion |
|---|---|---|
Batch | No downtime. All Databricks Runtime versions can read the table during conversion. | No downtime on Databricks Runtime 15.4 LTS and above. For Databricks Runtime 15.3 and below, Databricks recommends that you pause workloads before converting and then restart workloads after the conversion completes. |
Streaming | With schema tracking: Restart the stream to resume without losing any commits. The stream might need a couple of restarts. Without schema tracking: The stream raises an exception. Restart with a new checkpoint location and start version. Commits aren't lost. If the table had column mapping enabled before conversion, the stream treats the conversion as a schema change. | Restart the stream without losing any commits. |
Verify or revert a conversion
To verify that a conversion was successful, run DESCRIBE EXTENDED to see the new clustering columns. Run DESCRIBE HISTORY to see a series of REORG operations, an UPGRADE PROTOCOL operation, and a REPLACE PARTITIONED BY WITH CLUSTER BY operation.
To revert changes, either recreate the table with a CREATE TABLE AS SELECT (CTAS) statement, or restore the table using table versions.
Recreate the table with CTAS
Recreate the table with CTAS to preserve data changes made after conversion. This approach rewrites the entire table and doesn't preserve all table metadata. To recreate the table, run the following:
ALTER TABLE my_table UNSET TBLPROPERTIES ('delta.liquid.hierarchicalClusteringColumns');
ALTER TABLE my_table CLUSTER BY NONE;
CREATE OR REPLACE TABLE my_table PARTITIONED BY (<partition_columns>) AS SELECT * FROM my_table;
Restore the table
Restore the table to preserve its state before the conversion to liquid clustering and avoid a full rewrite. This approach discards all data and metadata changes made after the version you restore. To restore, follow these steps:
Identify the version to restore
Use DESCRIBE HISTORY to identify the version to restore to.
DESCRIBE HISTORY my_table
Verify that the version is readable
Verify that the version is readable and its data files have not been removed by VACUUM:
SELECT * FROM my_table VERSION AS OF <version_before_conversion> LIMIT 10;
Revert the conversion
To revert the conversion, run the following:
ALTER TABLE my_table UNSET TBLPROPERTIES ('delta.liquid.hierarchicalClusteringColumns');
ALTER TABLE my_table CLUSTER BY NONE;
TRUNCATE TABLE my_table;
RESTORE TABLE my_table TO VERSION AS OF <version_before_conversion>;
This approach restores the full table state and discards all later changes, including writes, schema changes, table properties, and protocol changes. Preserve writes and plan to reapply metadata changes. Stop writes before rollback and keep them stopped until the restore finishes. Run RESTORE immediately after TRUNCATE so that the truncated files are not removed by VACUUM. Downstream streaming workloads might experience poor performance after the restore.
Convert a table partitioned by a timestamp column
To convert a table (t1) that is partitioned by a timestamp column (timestamp_col) and use the timestamp column as a clustering key, you must set additional configurations:
SET spark.databricks.delta.liquidConversion.statsGeneration.enabled = false;
ALTER TABLE t1 REPLACE PARTITIONED BY WITH CLUSTER BY (timestamp_col, id);
ANALYZE TABLE t1 COMPUTE DELTA STATISTICS;
If you try to convert a timestamp partition column to a clustering column without these configurations, the command raises an error:
ALTER TABLE REPLACE PARTITIONED BY WITH CLUSTER BY cannot auto-generate stats on table with column event_ts due to unsupported type: timestamp. Disable stats auto-generation by setting 'spark.databricks.delta.liquidConversion.statsGeneration.enabled' to 'false' and retry the command again. SQLSTATE: 42000
Limitations
The following limitations apply to the REPLACE PARTITIONED BY WITH CLUSTER BY conversion command:
- Streaming tables and materialized views created in Lakeflow pipelines are not supported. To use liquid clustering, you must update the pipeline definition to use
CLUSTER BYinstead ofPARTITIONED BY. - Tables that use OpenSharing with partition filtering are not supported. For information on partition filtering for OpenSharing, see Specify table partitions to share.