sqlapache-sparkapache-spark-sqldatabricksdelta-lake

How to drop a column from a Databricks Delta table?


I have recently started discovering Databricks and faced a situation where I need to drop a certain column of a delta table. When I worked with PostgreSQL it was as easy as

ALTER TABLE main.metrics_table 
DROP COLUMN metric_1;

I was looking through Databricks documentation on DELETE but it covers only DELETE the rows that match a predicate.

I've also found docs on DROP database, DROP function and DROP table but absolutely nothing on how to delete a column from a delta table. What am I missing here? Is there a standard way to drop a column from a delta table?


Solution

  • There is no drop column option on Databricks tables: https://docs.databricks.com/spark/latest/spark-sql/language-manual/alter-table-or-view.html#delta-schema-constructs

    Remember that unlike a relational database there are physical parquet files in your storage, your "table" is just a schema that has been applied to them.

    In the relational world you can update the table metadata to remove a column easily, in a big data world you have to re-write the underlying files.

    Technically parquet can handle schema evolution (see Schema evolution in parquet format). But the Databricks implementation of Delta does not. It probably just too complicated to be worth it.

    Therefore the solution in this case is to create a new table and insert the columns you want to keep from the old table.